Blame src/builder-git.c

rpm-build c487f7
/* builder-git.c
rpm-build c487f7
 *
rpm-build c487f7
 * Copyright (C) 2015 Red Hat, Inc
rpm-build c487f7
 *
rpm-build c487f7
 * This file is free software; you can redistribute it and/or modify it
rpm-build c487f7
 * under the terms of the GNU Lesser General Public License as
rpm-build c487f7
 * published by the Free Software Foundation; either version 2 of the
rpm-build c487f7
 * License, or (at your option) any later version.
rpm-build c487f7
 *
rpm-build c487f7
 * This file is distributed in the hope that it will be useful, but
rpm-build c487f7
 * WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build c487f7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build c487f7
 * Lesser General Public License for more details.
rpm-build c487f7
 *
rpm-build c487f7
 * You should have received a copy of the GNU Lesser General Public License
rpm-build c487f7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
rpm-build c487f7
 *
rpm-build c487f7
 * Authors:
rpm-build c487f7
 *       Alexander Larsson <alexl@redhat.com>
rpm-build c487f7
 */
rpm-build c487f7
rpm-build c487f7
#include "config.h"
rpm-build c487f7
rpm-build c487f7
#include <string.h>
rpm-build c487f7
#include <fcntl.h>
rpm-build c487f7
#include <stdio.h>
rpm-build c487f7
#include <stdlib.h>
rpm-build c487f7
#include <sys/statfs.h>
rpm-build c487f7
rpm-build c487f7
#include "builder-utils.h"
rpm-build c487f7
rpm-build c487f7
#include "builder-git.h"
rpm-build c487f7
#include "builder-utils.h"
rpm-build c487f7
#include "builder-flatpak-utils.h"
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
git (GFile   *dir,
rpm-build c487f7
     char   **output,
rpm-build c487f7
     GSubprocessFlags flags,
rpm-build c487f7
     GError **error,
rpm-build c487f7
     ...)
rpm-build c487f7
{
rpm-build c487f7
  gboolean res;
rpm-build c487f7
  va_list ap;
rpm-build c487f7
rpm-build c487f7
  va_start (ap, error);
rpm-build c487f7
  res = flatpak_spawn (dir, output, flags, error, "git", ap);
rpm-build c487f7
  va_end (ap);
rpm-build c487f7
rpm-build c487f7
  return res;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
cp (GError **error,
rpm-build c487f7
     ...)
rpm-build c487f7
{
rpm-build c487f7
  gboolean res;
rpm-build c487f7
  va_list ap;
rpm-build c487f7
rpm-build c487f7
  va_start (ap, error);
rpm-build c487f7
  res = flatpak_spawn (NULL, NULL, 0, error, "cp", ap);
rpm-build c487f7
  va_end (ap);
rpm-build c487f7
rpm-build c487f7
  return res;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
git_get_version (GError **error,
rpm-build c487f7
                 int *major,
rpm-build c487f7
                 int *minor,
rpm-build c487f7
                 int *micro,
rpm-build c487f7
                 int *extra)
rpm-build c487f7
{
rpm-build c487f7
  g_autofree char *output = NULL;
rpm-build c487f7
  char *p;
rpm-build c487f7
rpm-build c487f7
  *major = *minor = *micro = *extra = 0;
rpm-build c487f7
rpm-build c487f7
  if (!git (NULL, &output, 0, error,
rpm-build c487f7
            "--version", NULL))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  /* Trim trailing whitespace */
rpm-build c487f7
  g_strchomp (output);
rpm-build c487f7
rpm-build c487f7
  if (!g_str_has_prefix (output, "git version "))
rpm-build c487f7
    return flatpak_fail (error, "Unexpected git --version output");
rpm-build c487f7
rpm-build c487f7
  p = output + strlen ("git version ");
rpm-build c487f7
rpm-build c487f7
  *major = strtol (p, &p, 10);
rpm-build c487f7
  while (*p == '.')
rpm-build c487f7
    p++;
rpm-build c487f7
  *minor = strtol (p, &p, 10);
rpm-build c487f7
  while (*p == '.')
rpm-build c487f7
    p++;
rpm-build c487f7
  *micro = strtol (p, &p, 10);
rpm-build c487f7
  while (*p == '.')
rpm-build c487f7
    p++;
rpm-build c487f7
  *extra = strtol (p, &p, 10);
rpm-build c487f7
  while (*p == '.')
rpm-build c487f7
    p++;
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
git_has_version (int major,
rpm-build c487f7
                 int minor,
rpm-build c487f7
                 int micro,
rpm-build c487f7
                 int extra)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GError) error = NULL;
rpm-build c487f7
  int git_major, git_minor, git_micro, git_extra;
rpm-build c487f7
rpm-build c487f7
  if (!git_get_version (&error, &git_major, &git_minor, &git_micro, &git_extra))
rpm-build c487f7
    {
rpm-build c487f7
      g_warning ("Failed to get git version: %s\n", error->message);
rpm-build c487f7
      return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  g_debug ("Git version: %d.%d.%d.%d", git_major, git_minor, git_micro, git_extra);
rpm-build c487f7
rpm-build c487f7
  if (git_major > major)
rpm-build c487f7
    return TRUE;
rpm-build c487f7
  if (git_major < major)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
  /* git_major == major */
rpm-build c487f7
rpm-build c487f7
  if (git_minor > minor)
rpm-build c487f7
    return TRUE;
rpm-build c487f7
  if (git_minor < minor)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
  /* git_minor == minor */
rpm-build c487f7
rpm-build c487f7
  if (git_micro > micro)
rpm-build c487f7
    return TRUE;
rpm-build c487f7
  if (git_micro < micro)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
  /* git_micro == micro */
rpm-build c487f7
rpm-build c487f7
  if (git_extra > extra)
rpm-build c487f7
    return TRUE;
rpm-build c487f7
  if (git_extra < extra)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
  /* git_extra == extra */
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
git_version_supports_fsck_and_shallow (void)
rpm-build c487f7
{
rpm-build c487f7
  return git_has_version (1,8,3,2);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
git_version_supports_fetch_from_shallow (void)
rpm-build c487f7
{
rpm-build c487f7
  return git_has_version (1,9,0,0);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
git_repo_is_shallow (GFile *repo_dir)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GFile) shallow_file = g_file_get_child (repo_dir, "shallow");
rpm-build c487f7
  if (g_file_query_exists (shallow_file, NULL))
rpm-build c487f7
    return TRUE;
rpm-build c487f7
rpm-build c487f7
  return FALSE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static GHashTable *
rpm-build c487f7
git_ls_remote (GFile *repo_dir,
rpm-build c487f7
               const char *remote,
rpm-build c487f7
               GError **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autofree char *output = NULL;
rpm-build c487f7
  g_autoptr(GHashTable) refs = NULL;
rpm-build c487f7
  g_auto(GStrv) lines = NULL;
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  if (!git (repo_dir, &output, 0, error,
rpm-build c487f7
            "ls-remote", remote, NULL))
rpm-build c487f7
    return NULL;
rpm-build c487f7
rpm-build c487f7
  refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
rpm-build c487f7
  lines = g_strsplit (output, "\n", -1);
rpm-build c487f7
  for (i = 0; lines[i] != NULL; i++)
rpm-build c487f7
    {
rpm-build c487f7
      g_autofree char **line = g_strsplit (lines[i], "\t", 2);
rpm-build c487f7
      if (line[0] != NULL && line[1] != NULL)
rpm-build c487f7
        g_hash_table_insert (refs, line[1], line[0]);
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return g_steal_pointer (&refs);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static char *
rpm-build c487f7
lookup_full_ref (GHashTable *refs, const char *ref)
rpm-build c487f7
{
rpm-build c487f7
  int i;
rpm-build c487f7
  const char *prefixes[] = {
rpm-build c487f7
    "",
rpm-build c487f7
    "refs/",
rpm-build c487f7
    "refs/tags/",
rpm-build c487f7
    "refs/heads/"
rpm-build c487f7
  };
rpm-build c487f7
  GHashTableIter iter;
rpm-build c487f7
  gpointer key, value;
rpm-build c487f7
rpm-build c487f7
  for (i = 0; i < G_N_ELEMENTS(prefixes); i++)
rpm-build c487f7
    {
rpm-build c487f7
      g_autofree char *full_ref = g_strconcat (prefixes[i], ref, NULL);
rpm-build c487f7
      if (g_hash_table_contains (refs, full_ref))
rpm-build c487f7
        return g_steal_pointer (&full_ref);
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  g_hash_table_iter_init (&iter, refs);
rpm-build c487f7
  while (g_hash_table_iter_next (&iter, &key, &value))
rpm-build c487f7
    {
rpm-build c487f7
      const char *key_ref = key;
rpm-build c487f7
      const char *commit = value;
rpm-build c487f7
rpm-build c487f7
      if (g_str_has_prefix (key_ref, "refs/") &&
rpm-build c487f7
          g_ascii_strncasecmp (commit, ref, strlen (ref)) == 0)
rpm-build c487f7
        {
rpm-build c487f7
          char *full_ref = g_strdup (key_ref);
rpm-build c487f7
          if (g_str_has_suffix (full_ref, "^{}"))
rpm-build c487f7
            full_ref[strlen (full_ref) - 3] = 0;
rpm-build c487f7
          return full_ref;
rpm-build c487f7
        }
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return NULL;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static GFile *
rpm-build c487f7
git_get_mirror_dir (const char     *url_or_path,
rpm-build c487f7
                    BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GFile) git_dir = NULL;
rpm-build c487f7
  g_autofree char *filename = NULL;
rpm-build c487f7
  g_autofree char *git_dir_path = NULL;
rpm-build c487f7
rpm-build c487f7
  git_dir = g_file_get_child (builder_context_get_state_dir (context),
rpm-build c487f7
                              "git");
rpm-build c487f7
rpm-build c487f7
  git_dir_path = g_file_get_path (git_dir);
rpm-build c487f7
  g_mkdir_with_parents (git_dir_path, 0755);
rpm-build c487f7
rpm-build c487f7
  /* Technically a path isn't a uri but if it's absolute it should still be unique. */
rpm-build c487f7
  filename = builder_uri_to_filename (url_or_path);
rpm-build c487f7
  return g_file_get_child (git_dir, filename);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static char *
rpm-build c487f7
git_get_current_commit (GFile          *repo_dir,
rpm-build c487f7
                        const char     *branch,
rpm-build c487f7
                        gboolean        ensure_commit,
rpm-build c487f7
                        BuilderContext *context,
rpm-build c487f7
                        GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  char *output = NULL;
rpm-build c487f7
  g_autofree char *arg = NULL;
rpm-build c487f7
rpm-build c487f7
  if (ensure_commit)
rpm-build c487f7
    arg = g_strconcat (branch, "^{commit}", NULL);
rpm-build c487f7
  else
rpm-build c487f7
    arg = g_strdup (branch);
rpm-build c487f7
rpm-build c487f7
  if (!git (repo_dir, &output, 0, error,
rpm-build c487f7
            "rev-parse", arg, NULL))
rpm-build c487f7
    return NULL;
rpm-build c487f7
rpm-build c487f7
  /* Trim trailing whitespace */
rpm-build c487f7
  g_strchomp (output);
rpm-build c487f7
rpm-build c487f7
  return output;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
char *
rpm-build c487f7
builder_git_get_current_commit (const char     *repo_location,
rpm-build c487f7
                                const char     *branch,
rpm-build c487f7
                                gboolean        ensure_commit,
rpm-build c487f7
                                BuilderContext *context,
rpm-build c487f7
                                GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GFile) mirror_dir = NULL;
rpm-build c487f7
rpm-build c487f7
  mirror_dir = git_get_mirror_dir (repo_location, context);
rpm-build c487f7
  return git_get_current_commit (mirror_dir, branch, ensure_commit, context, error);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static char *
rpm-build c487f7
make_absolute (const char *orig_parent, const char *orig_relpath, GError **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autofree char *parent = g_strdup (orig_parent);
rpm-build c487f7
  const char *relpath = orig_relpath;
rpm-build c487f7
  char *start;
rpm-build c487f7
  char *parent_path;
rpm-build c487f7
rpm-build c487f7
  if (!g_str_has_prefix (relpath, "../"))
rpm-build c487f7
    return g_strdup (orig_relpath);
rpm-build c487f7
rpm-build c487f7
  if (parent[strlen (parent) - 1] == '/')
rpm-build c487f7
    parent[strlen (parent) - 1] = 0;
rpm-build c487f7
rpm-build c487f7
  if ((start = strstr (parent, "://")))
rpm-build c487f7
    start = start + 3;
rpm-build c487f7
  else
rpm-build c487f7
    start = parent;
rpm-build c487f7
rpm-build c487f7
  parent_path = strchr (start, '/');
rpm-build c487f7
  if (parent_path == NULL)
rpm-build c487f7
    {
rpm-build c487f7
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid uri or path %s", orig_parent);
rpm-build c487f7
      return NULL;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  while (g_str_has_prefix (relpath, "../"))
rpm-build c487f7
    {
rpm-build c487f7
      char *last_slash = strrchr (parent_path, '/');
rpm-build c487f7
      if (last_slash == NULL)
rpm-build c487f7
        {
rpm-build c487f7
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid relative path %s for uri or path %s", orig_relpath, orig_parent);
rpm-build c487f7
          return NULL;
rpm-build c487f7
        }
rpm-build c487f7
      relpath += 3;
rpm-build c487f7
      *last_slash = 0;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return g_strconcat (parent, "/", relpath, NULL);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
git_mirror_submodules (const char     *repo_location,
rpm-build c487f7
                       const char     *destination_path,
rpm-build c487f7
                       gboolean        shallow,
rpm-build c487f7
                       FlatpakGitMirrorFlags flags,
rpm-build c487f7
                       GFile          *mirror_dir,
rpm-build c487f7
                       const char     *revision,
rpm-build c487f7
                       BuilderContext *context,
rpm-build c487f7
                       GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GKeyFile) key_file = g_key_file_new ();
rpm-build c487f7
  g_autofree gchar *rev_parse_output = NULL;
rpm-build c487f7
  g_autofree gchar *submodule_data = NULL;
rpm-build c487f7
  g_autofree gchar **submodules = NULL;
rpm-build c487f7
  g_autofree gchar *gitmodules = g_strconcat (revision, ":.gitmodules", NULL);
rpm-build c487f7
  gsize num_submodules;
rpm-build c487f7
rpm-build c487f7
  /* The submodule update will fetch from this repo */
rpm-build c487f7
  flags |= FLATPAK_GIT_MIRROR_FLAGS_WILL_FETCH_FROM;
rpm-build c487f7
rpm-build c487f7
  if (!git (mirror_dir, &rev_parse_output, 0, NULL, "rev-parse", "--verify", "--quiet", gitmodules, NULL))
rpm-build c487f7
    return TRUE;
rpm-build c487f7
rpm-build c487f7
  if (git (mirror_dir, &submodule_data, 0, NULL, "show", gitmodules, NULL))
rpm-build c487f7
    {
rpm-build c487f7
      if (!g_key_file_load_from_data (key_file, submodule_data, -1,
rpm-build c487f7
                                      G_KEY_FILE_NONE, error))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      submodules = g_key_file_get_groups (key_file, &num_submodules);
rpm-build c487f7
rpm-build c487f7
      int i;
rpm-build c487f7
      for (i = 0; i < num_submodules; i++)
rpm-build c487f7
        {
rpm-build c487f7
          g_autofree gchar *submodule = NULL;
rpm-build c487f7
          g_autofree gchar *path = NULL;
rpm-build c487f7
          g_autofree gchar *relative_url = NULL;
rpm-build c487f7
          g_autofree gchar *absolute_url = NULL;
rpm-build c487f7
          g_autofree gchar *ls_tree = NULL;
rpm-build c487f7
          g_auto(GStrv) lines = NULL;
rpm-build c487f7
          g_auto(GStrv) words = NULL;
rpm-build c487f7
rpm-build c487f7
          submodule = submodules[i];
rpm-build c487f7
rpm-build c487f7
          if (!g_str_has_prefix (submodule, "submodule \""))
rpm-build c487f7
            continue;
rpm-build c487f7
rpm-build c487f7
          path = g_key_file_get_string (key_file, submodule, "path", error);
rpm-build c487f7
          if (path == NULL)
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          relative_url = g_key_file_get_string (key_file, submodule, "url", error);
rpm-build c487f7
          /* Remove any trailing whitespace */
rpm-build c487f7
          g_strchomp (relative_url);
rpm-build c487f7
          absolute_url = make_absolute (repo_location, relative_url, error);
rpm-build c487f7
          if (absolute_url == NULL)
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          if (!git (mirror_dir, &ls_tree, 0, error, "ls-tree", revision, path, NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          lines = g_strsplit (g_strstrip (ls_tree), "\n", 0);
rpm-build c487f7
          if (g_strv_length (lines) != 1)
rpm-build c487f7
            {
rpm-build c487f7
              g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not a gitlink tree: %s", path);
rpm-build c487f7
              return FALSE;
rpm-build c487f7
            }
rpm-build c487f7
rpm-build c487f7
          words = g_strsplit_set (lines[0], " \t", 4);
rpm-build c487f7
rpm-build c487f7
          if (g_strcmp0 (words[0], "160000") != 0)
rpm-build c487f7
            continue;
rpm-build c487f7
rpm-build c487f7
          g_debug ("mirror submodule %s at revision %s\n", absolute_url, words[2]);
rpm-build c487f7
          if (shallow)
rpm-build c487f7
            {
rpm-build c487f7
              if (!builder_git_shallow_mirror_ref (absolute_url, destination_path, words[2], context, error))
rpm-build c487f7
                return FALSE;
rpm-build c487f7
            }
rpm-build c487f7
          else
rpm-build c487f7
            {
rpm-build c487f7
              if (!builder_git_mirror_repo (absolute_url, destination_path, flags, words[2], context, error))
rpm-build c487f7
                return FALSE;
rpm-build c487f7
            }
rpm-build c487f7
        }
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
/* This mirrors the repo given by repo_location in a local
rpm-build c487f7
   directory. It tries to mirror only "ref", in a shallow way.
rpm-build c487f7
   However, this only works if ref is a tag or branch, or
rpm-build c487f7
   a commit id that is currently at the tip of a remote ref.
rpm-build c487f7
   If it is just a random commit id then we're forced to do
rpm-build c487f7
   a deep fetch of the entire remote repo.
rpm-build c487f7
*/
rpm-build c487f7
gboolean
rpm-build c487f7
builder_git_mirror_repo (const char     *repo_location,
rpm-build c487f7
                         const char     *destination_path,
rpm-build c487f7
                         FlatpakGitMirrorFlags flags,
rpm-build c487f7
                         const char     *ref,
rpm-build c487f7
                         BuilderContext *context,
rpm-build c487f7
                         GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GFile) cache_mirror_dir = NULL;
rpm-build c487f7
  g_autoptr(GFile) mirror_dir = NULL;
rpm-build c487f7
  g_autoptr(GFile) real_mirror_dir = NULL;
rpm-build c487f7
  g_autoptr(FlatpakTempDir) tmp_mirror_dir = NULL;
rpm-build c487f7
  g_autofree char *current_commit = NULL;
rpm-build c487f7
  g_autoptr(GHashTable) refs = NULL;
rpm-build c487f7
  gboolean already_exists = FALSE;
rpm-build c487f7
  gboolean created = FALSE;
rpm-build c487f7
  gboolean was_shallow = FALSE;
rpm-build c487f7
  gboolean do_disable_shallow = FALSE;
rpm-build c487f7
  gboolean update = (flags & FLATPAK_GIT_MIRROR_FLAGS_UPDATE) != 0;
rpm-build c487f7
  gboolean disable_fsck = (flags & FLATPAK_GIT_MIRROR_FLAGS_DISABLE_FSCK) != 0;
rpm-build c487f7
rpm-build c487f7
  gboolean git_supports_fsck_and_shallow = git_version_supports_fsck_and_shallow ();
rpm-build c487f7
rpm-build c487f7
  cache_mirror_dir = git_get_mirror_dir (repo_location, context);
rpm-build c487f7
rpm-build c487f7
  if (destination_path != NULL)
rpm-build c487f7
    {
rpm-build c487f7
      g_autofree char *file_name = g_file_get_basename (cache_mirror_dir);
rpm-build c487f7
      g_autofree char *destination_file_path = g_build_filename (destination_path,
rpm-build c487f7
                                                                 file_name,
rpm-build c487f7
                                                                 NULL);
rpm-build c487f7
      mirror_dir = g_file_new_for_path (destination_file_path);
rpm-build c487f7
    }
rpm-build c487f7
  else
rpm-build c487f7
    mirror_dir = g_object_ref (cache_mirror_dir);
rpm-build c487f7
rpm-build c487f7
  if (!g_file_query_exists (mirror_dir, NULL))
rpm-build c487f7
    {
rpm-build c487f7
      g_autofree char *tmpdir = g_strconcat (flatpak_file_get_path_cached (mirror_dir), "-XXXXXX", NULL);
rpm-build c487f7
rpm-build c487f7
      if (g_mkdtemp_full (tmpdir, 0755) == NULL)
rpm-build c487f7
        return flatpak_fail (error, "Can't create temporary directory");
rpm-build c487f7
rpm-build c487f7
      tmp_mirror_dir = g_file_new_for_path (tmpdir);
rpm-build c487f7
      real_mirror_dir = g_steal_pointer (&mirror_dir);
rpm-build c487f7
      mirror_dir = g_object_ref (tmp_mirror_dir);
rpm-build c487f7
rpm-build c487f7
      if (!git (NULL, NULL, 0, error,
rpm-build c487f7
                "init", "--bare",
rpm-build c487f7
                (char *)flatpak_file_get_path_cached (mirror_dir), NULL))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      if (!git (mirror_dir, NULL, 0, error,
rpm-build c487f7
                "remote", "add", "--mirror=fetch", "origin",
rpm-build c487f7
                repo_location, NULL))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      created = TRUE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  was_shallow = git_repo_is_shallow (mirror_dir);
rpm-build c487f7
rpm-build c487f7
  if (git (mirror_dir, NULL, G_SUBPROCESS_FLAGS_STDERR_SILENCE, NULL,
rpm-build c487f7
           "cat-file", "-e", ref, NULL))
rpm-build c487f7
    already_exists = TRUE;
rpm-build c487f7
rpm-build c487f7
  do_disable_shallow = (flags & FLATPAK_GIT_MIRROR_FLAGS_DISABLE_SHALLOW) != 0;
rpm-build c487f7
rpm-build c487f7
  /* If we ever pulled non-shallow, then keep doing so, because
rpm-build c487f7
     otherwise old git clients break */
rpm-build c487f7
  if (!created && !was_shallow)
rpm-build c487f7
    do_disable_shallow = TRUE;
rpm-build c487f7
rpm-build c487f7
  /* Older versions of git can't fetch from shallow repos, so for
rpm-build c487f7
     those, always clone deeply anything we will later fetch from.
rpm-build c487f7
     (This is typically submodules and regular repos if we're bundling
rpm-build c487f7
     sources) */
rpm-build c487f7
  if ((flags & FLATPAK_GIT_MIRROR_FLAGS_WILL_FETCH_FROM) != 0 &&
rpm-build c487f7
      !git_version_supports_fetch_from_shallow ())
rpm-build c487f7
    {
rpm-build c487f7
      do_disable_shallow = TRUE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
    {
rpm-build c487f7
      g_autofree char *full_ref = NULL;
rpm-build c487f7
      g_autoptr(GFile) cached_git_dir = NULL;
rpm-build c487f7
      g_autofree char *origin = NULL;
rpm-build c487f7
      g_autoptr(GFile) alternates = NULL;
rpm-build c487f7
      g_autofree char *cache_filename = NULL;
rpm-build c487f7
rpm-build c487f7
      if (real_mirror_dir)
rpm-build c487f7
        cache_filename = g_file_get_basename (real_mirror_dir);
rpm-build c487f7
      else
rpm-build c487f7
        cache_filename = g_file_get_basename (mirror_dir);
rpm-build c487f7
rpm-build c487f7
      /* If we're doing a regular download, look for cache sources */
rpm-build c487f7
      if (destination_path == NULL)
rpm-build c487f7
        cached_git_dir = builder_context_find_in_sources_dirs (context, "git", cache_filename, NULL);
rpm-build c487f7
      else
rpm-build c487f7
        cached_git_dir = g_object_ref (cache_mirror_dir);
rpm-build c487f7
rpm-build c487f7
      /* If the ref already exists (it may not with a shallow mirror
rpm-build c487f7
       * if it has changed) and we're not updating, only pull from
rpm-build c487f7
       * cache to avoid network i/o. */
rpm-build c487f7
      if (already_exists && !update)
rpm-build c487f7
        {
rpm-build c487f7
          if (cached_git_dir)
rpm-build c487f7
            origin = g_file_get_uri (cached_git_dir);
rpm-build c487f7
          else if (!created)
rpm-build c487f7
            return TRUE;
rpm-build c487f7
        }
rpm-build c487f7
rpm-build c487f7
      if (origin == NULL)
rpm-build c487f7
        origin = g_strdup ("origin");
rpm-build c487f7
rpm-build c487f7
      refs = git_ls_remote (mirror_dir, origin, error);
rpm-build c487f7
      if (refs == NULL)
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      if (update && cached_git_dir)
rpm-build c487f7
        {
rpm-build c487f7
          const char *data = flatpak_file_get_path_cached (cached_git_dir);
rpm-build c487f7
          /* If we're updating, use the cache as a source of git objects */
rpm-build c487f7
          alternates = g_file_resolve_relative_path (mirror_dir, "objects/info/alternates");
rpm-build c487f7
          if (!g_file_replace_contents (alternates,
rpm-build c487f7
                                        data, strlen (data),
rpm-build c487f7
                                        NULL, FALSE,
rpm-build c487f7
                                        G_FILE_CREATE_REPLACE_DESTINATION,
rpm-build c487f7
                                        NULL, NULL, error))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
        }
rpm-build c487f7
rpm-build c487f7
      if (!git (mirror_dir, NULL, 0, error,
rpm-build c487f7
                "config", "transfer.fsckObjects", (disable_fsck || (!git_supports_fsck_and_shallow && !do_disable_shallow)) ? "0" : "1", NULL))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      if (!do_disable_shallow)
rpm-build c487f7
        full_ref = lookup_full_ref (refs, ref);
rpm-build c487f7
      if (full_ref)
rpm-build c487f7
        {
rpm-build c487f7
          g_autofree char *full_ref_mapping = g_strdup_printf ("+%s:%s", full_ref, full_ref);
rpm-build c487f7
rpm-build c487f7
          g_print ("Fetching git repo %s, ref %s\n", repo_location, full_ref);
rpm-build c487f7
          if (!git (mirror_dir, NULL, 0, error,
rpm-build c487f7
                    "fetch", "-p", "--no-recurse-submodules", "--depth=1", "-f",
rpm-build c487f7
                    origin, full_ref_mapping, NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
	  /* It turns out that older versions of git (at least 2.7.4)
rpm-build c487f7
	   * cannot check out a commit unless a real tag/branch points
rpm-build c487f7
	   * to it, which is not the case for e.g. gitbug pull requests.
rpm-build c487f7
	   * So, to make this work we fake a branch for these cases.
rpm-build c487f7
	   * See https://github.com/flatpak/flatpak/issues/1133
rpm-build c487f7
	   */
rpm-build c487f7
	  if (!g_str_has_prefix (full_ref, "refs/heads") &&
rpm-build c487f7
	      !g_str_has_prefix (full_ref, "refs/tags"))
rpm-build c487f7
	    {
rpm-build c487f7
	      g_autofree char *fake_ref = g_strdup_printf ("refs/heads/flatpak-builder-internal/%s", full_ref);
rpm-build c487f7
	      g_autofree char *peeled_full_ref = g_strdup_printf ("%s^{}", full_ref);
rpm-build c487f7
rpm-build c487f7
	      if (!git (mirror_dir, NULL, 0, NULL,
rpm-build c487f7
			"update-ref", fake_ref, peeled_full_ref, NULL))
rpm-build c487f7
		return FALSE;
rpm-build c487f7
	    }
rpm-build c487f7
        }
rpm-build c487f7
      else if (!already_exists || do_disable_shallow)
rpm-build c487f7
        /* We don't fetch everything if it already exists (and we're not disabling shallow), because
rpm-build c487f7
           since it failed to resolve to full_ref it is a commit id
rpm-build c487f7
           which can't change and thus need no updates */
rpm-build c487f7
        {
rpm-build c487f7
          g_print ("Fetching full git repo %s\n", repo_location);
rpm-build c487f7
          if (!git (mirror_dir, NULL, 0, error,
rpm-build c487f7
                    "fetch", "-f", "-p", "--no-recurse-submodules", "--tags", origin, "*:*",
rpm-build c487f7
                    was_shallow ? "--unshallow" : NULL,
rpm-build c487f7
                    NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
        }
rpm-build c487f7
rpm-build c487f7
      if (alternates)
rpm-build c487f7
        {
rpm-build c487f7
          g_autoptr(GError) local_error = NULL;
rpm-build c487f7
rpm-build c487f7
          /* Ensure we copy the files from the cache, to be safe if the extra source changes */
rpm-build c487f7
          if (!git (mirror_dir, NULL, 0, error,
rpm-build c487f7
                    "repack", "-a", "-d", NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          if (!g_file_delete (alternates, NULL, &local_error) &&
rpm-build c487f7
              !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
rpm-build c487f7
            g_debug ("Error deleting alternates file: %s", local_error->message);
rpm-build c487f7
        }
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  if (real_mirror_dir)
rpm-build c487f7
    {
rpm-build c487f7
      if (!flatpak_file_rename (mirror_dir, real_mirror_dir, NULL, error))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
      g_clear_object (&mirror_dir);
rpm-build c487f7
      mirror_dir = g_steal_pointer (&real_mirror_dir);
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  if (flags & FLATPAK_GIT_MIRROR_FLAGS_MIRROR_SUBMODULES)
rpm-build c487f7
    {
rpm-build c487f7
      current_commit = git_get_current_commit (mirror_dir, ref, FALSE, context, error);
rpm-build c487f7
      if (current_commit == NULL)
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      if (!git_mirror_submodules (repo_location, destination_path, FALSE, flags,
rpm-build c487f7
                                  mirror_dir, current_commit, context, error))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
/* In contrast with builder_git_mirror_repo this always does a shallow
rpm-build c487f7
   mirror. However, it only works for sources that are local, because
rpm-build c487f7
   it handles the case builder_git_mirror_repo fails at by creating refs
rpm-build c487f7
   in the source repo. */
rpm-build c487f7
gboolean
rpm-build c487f7
builder_git_shallow_mirror_ref (const char     *repo_location,
rpm-build c487f7
                                const char     *destination_path,
rpm-build c487f7
                                const char     *ref,
rpm-build c487f7
                                BuilderContext *context,
rpm-build c487f7
                                GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GFile) cache_mirror_dir = NULL;
rpm-build c487f7
  g_autoptr(GFile) mirror_dir = NULL;
rpm-build c487f7
  g_autofree char *current_commit = NULL;
rpm-build c487f7
  g_autofree char *file_name = NULL;
rpm-build c487f7
  g_autofree char *destination_file_path = NULL;
rpm-build c487f7
  g_autofree char *full_ref = NULL;
rpm-build c487f7
  g_autofree char *full_ref_colon_full_ref = NULL;
rpm-build c487f7
rpm-build c487f7
  cache_mirror_dir = git_get_mirror_dir (repo_location, context);
rpm-build c487f7
rpm-build c487f7
  file_name = g_file_get_basename (cache_mirror_dir);
rpm-build c487f7
  destination_file_path = g_build_filename (destination_path,
rpm-build c487f7
                                            file_name,
rpm-build c487f7
                                            NULL);
rpm-build c487f7
  mirror_dir = g_file_new_for_path (destination_file_path);
rpm-build c487f7
rpm-build c487f7
  if (!g_file_query_exists (mirror_dir, NULL))
rpm-build c487f7
    {
rpm-build c487f7
      if (!git (NULL, NULL, 0, error,
rpm-build c487f7
                "init", "--bare", destination_file_path, NULL))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
      if (!git (mirror_dir, NULL, 0, error,
rpm-build c487f7
                "remote", "add", "--mirror=fetch", "origin",
rpm-build c487f7
                (char *)flatpak_file_get_path_cached (cache_mirror_dir), NULL))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  if (!git (cache_mirror_dir, &full_ref, 0, error,
rpm-build c487f7
            "rev-parse", "--symbolic-full-name", ref, NULL))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  /* Trim trailing whitespace */
rpm-build c487f7
  g_strchomp (full_ref);
rpm-build c487f7
rpm-build c487f7
  if (*full_ref == 0)
rpm-build c487f7
    {
rpm-build c487f7
      g_autofree char *peeled_ref = g_strdup_printf ("%s^{}", ref);
rpm-build c487f7
rpm-build c487f7
      g_free (full_ref);
rpm-build c487f7
      /* We can't pull the commit id, so we create a ref we can pull */
rpm-build c487f7
      full_ref = g_strdup_printf ("refs/heads/flatpak-builder-internal/commit/%s", ref);
rpm-build c487f7
      if (!git (cache_mirror_dir, NULL, 0, error,
rpm-build c487f7
                "update-ref", full_ref, peeled_ref, NULL))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  full_ref_colon_full_ref = g_strdup_printf ("%s:%s", full_ref, full_ref);
rpm-build c487f7
  if (!git (mirror_dir, NULL, 0, error,
rpm-build c487f7
            "fetch", "--depth", "1", "origin", full_ref_colon_full_ref, NULL))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  /* Always mirror submodules */
rpm-build c487f7
  current_commit = git_get_current_commit (mirror_dir, ref, FALSE, context, error);
rpm-build c487f7
  if (current_commit == NULL)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  if (!git_mirror_submodules (repo_location, destination_path, TRUE,
rpm-build c487f7
                              FLATPAK_GIT_MIRROR_FLAGS_MIRROR_SUBMODULES | FLATPAK_GIT_MIRROR_FLAGS_DISABLE_FSCK,
rpm-build c487f7
                              mirror_dir, current_commit, context, error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
git_extract_submodule (const char     *repo_location,
rpm-build c487f7
                       GFile          *checkout_dir,
rpm-build c487f7
                       const char     *revision,
rpm-build c487f7
                       BuilderContext *context,
rpm-build c487f7
                       GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GKeyFile) key_file = g_key_file_new ();
rpm-build c487f7
  g_autofree gchar *rev_parse_output = NULL;
rpm-build c487f7
  g_autofree gchar *submodule_data = NULL;
rpm-build c487f7
  g_autofree gchar **submodules = NULL;
rpm-build c487f7
  g_autofree gchar *gitmodules = g_strconcat (revision, ":.gitmodules", NULL);
rpm-build c487f7
  gsize num_submodules;
rpm-build c487f7
rpm-build c487f7
  if (!git (checkout_dir, &rev_parse_output, 0, NULL, "rev-parse", "--verify", "--quiet", gitmodules, NULL))
rpm-build c487f7
    return TRUE;
rpm-build c487f7
rpm-build c487f7
  if (git (checkout_dir, &submodule_data, 0, NULL, "show", gitmodules, NULL))
rpm-build c487f7
    {
rpm-build c487f7
      if (!g_key_file_load_from_data (key_file, submodule_data, -1,
rpm-build c487f7
                                      G_KEY_FILE_NONE, error))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      submodules = g_key_file_get_groups (key_file, &num_submodules);
rpm-build c487f7
rpm-build c487f7
      int i;
rpm-build c487f7
      for (i = 0; i < num_submodules; i++)
rpm-build c487f7
        {
rpm-build c487f7
          g_autofree gchar *submodule = NULL;
rpm-build c487f7
          g_autofree gchar *name = NULL;
rpm-build c487f7
          g_autofree gchar *update_method = NULL;
rpm-build c487f7
          g_autofree gchar *path = NULL;
rpm-build c487f7
          g_autofree gchar *relative_url = NULL;
rpm-build c487f7
          g_autofree gchar *absolute_url = NULL;
rpm-build c487f7
          g_autofree gchar *ls_tree = NULL;
rpm-build c487f7
          g_auto(GStrv) lines = NULL;
rpm-build c487f7
          g_auto(GStrv) words = NULL;
rpm-build c487f7
          g_autoptr(GFile) mirror_dir = NULL;
rpm-build c487f7
          g_autoptr(GFile) child_dir = NULL;
rpm-build c487f7
          g_autofree gchar *mirror_dir_as_url = NULL;
rpm-build c487f7
          g_autofree gchar *option = NULL;
rpm-build c487f7
          gsize len;
rpm-build c487f7
rpm-build c487f7
          submodule = submodules[i];
rpm-build c487f7
          len = strlen (submodule);
rpm-build c487f7
rpm-build c487f7
          if (!g_str_has_prefix (submodule, "submodule \""))
rpm-build c487f7
            continue;
rpm-build c487f7
rpm-build c487f7
          name = g_strndup (submodule + 11, len - 12);
rpm-build c487f7
rpm-build c487f7
          /* Skip any submodules that are disabled (have the update method set to "none")
rpm-build c487f7
             Only check if the command succeeds. If it fails, the update method is not set. */
rpm-build c487f7
          update_method = g_key_file_get_string (key_file, submodule, "update", NULL);
rpm-build c487f7
          if (g_strcmp0 (update_method, "none") == 0)
rpm-build c487f7
            continue;
rpm-build c487f7
rpm-build c487f7
          path = g_key_file_get_string (key_file, submodule, "path", error);
rpm-build c487f7
          if (path == NULL)
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          relative_url = g_key_file_get_string (key_file, submodule, "url", error);
rpm-build c487f7
          absolute_url = make_absolute (repo_location, relative_url, error);
rpm-build c487f7
          if (absolute_url == NULL)
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          if (!git (checkout_dir, &ls_tree, 0, error, "ls-tree", revision, path, NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          lines = g_strsplit (g_strstrip (ls_tree), "\n", 0);
rpm-build c487f7
          if (g_strv_length (lines) != 1)
rpm-build c487f7
            {
rpm-build c487f7
              g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not a gitlink tree: %s", path);
rpm-build c487f7
              return FALSE;
rpm-build c487f7
            }
rpm-build c487f7
rpm-build c487f7
          words = g_strsplit_set (lines[0], " \t", 4);
rpm-build c487f7
rpm-build c487f7
          if (g_strcmp0 (words[0], "160000") != 0)
rpm-build c487f7
            continue;
rpm-build c487f7
rpm-build c487f7
          mirror_dir = git_get_mirror_dir (absolute_url, context);
rpm-build c487f7
          mirror_dir_as_url = g_file_get_uri (mirror_dir);
rpm-build c487f7
          option = g_strdup_printf ("submodule.%s.url", name);
rpm-build c487f7
rpm-build c487f7
          if (!git (checkout_dir, NULL, 0, error,
rpm-build c487f7
                    "config", option, mirror_dir_as_url, NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          if (!git (checkout_dir, NULL, 0, error,
rpm-build c487f7
                    "submodule", "update", "--init", path, NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          child_dir = g_file_resolve_relative_path (checkout_dir, path);
rpm-build c487f7
rpm-build c487f7
          if (!git_extract_submodule (absolute_url, child_dir, words[2], context, error))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
        }
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
gboolean
rpm-build c487f7
builder_git_checkout (const char     *repo_location,
rpm-build c487f7
                      const char     *branch,
rpm-build c487f7
                      GFile          *dest,
rpm-build c487f7
                      BuilderContext *context,
rpm-build c487f7
                      GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GFile) mirror_dir = NULL;
rpm-build c487f7
  g_autofree char *mirror_dir_path = NULL;
rpm-build c487f7
  g_autofree char *dest_path = NULL;
rpm-build c487f7
  g_autofree char *dest_path_git = NULL;
rpm-build c487f7
rpm-build c487f7
  mirror_dir = git_get_mirror_dir (repo_location, context);
rpm-build c487f7
rpm-build c487f7
  mirror_dir_path = g_file_get_path (mirror_dir);
rpm-build c487f7
  dest_path = g_file_get_path (dest);
rpm-build c487f7
  dest_path_git = g_build_filename (dest_path, ".git", NULL);
rpm-build c487f7
rpm-build c487f7
  g_mkdir_with_parents (dest_path, 0755);
rpm-build c487f7
rpm-build c487f7
  if (!cp (error,
rpm-build c487f7
           "-al",
rpm-build c487f7
           mirror_dir_path, dest_path_git, NULL))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  /* Then we need to convert to regular */
rpm-build c487f7
  if (!git (dest, NULL, 0, error,
rpm-build c487f7
            "config", "--bool", "core.bare", "false", NULL))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  if (!git (dest, NULL, 0, error,
rpm-build c487f7
            "checkout", branch, NULL))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  if (!git_extract_submodule (repo_location, dest, branch, context, error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}