2015-11-16 15:23:23 +00:00
|
|
|
/* builder-source-git.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Red Hat, Inc
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
2015-11-26 13:50:21 +00:00
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
2015-11-16 15:23:23 +00:00
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
2015-11-26 13:50:21 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2015-11-16 15:23:23 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
|
|
|
|
#include "builder-utils.h"
|
|
|
|
|
|
|
|
#include "builder-source-git.h"
|
|
|
|
#include "builder-utils.h"
|
2016-05-06 16:03:47 +00:00
|
|
|
#include "flatpak-utils.h"
|
2015-11-16 15:23:23 +00:00
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
struct BuilderSourceGit
|
|
|
|
{
|
2015-11-16 15:23:23 +00:00
|
|
|
BuilderSource parent;
|
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
char *url;
|
2016-07-29 19:52:47 +00:00
|
|
|
char *path;
|
2016-05-06 14:03:27 +00:00
|
|
|
char *branch;
|
2015-11-16 15:23:23 +00:00
|
|
|
};
|
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2015-11-16 15:23:23 +00:00
|
|
|
BuilderSourceClass parent_class;
|
|
|
|
} BuilderSourceGitClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (BuilderSourceGit, builder_source_git, BUILDER_TYPE_SOURCE);
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_URL,
|
2016-07-29 19:52:47 +00:00
|
|
|
PROP_PATH,
|
2015-11-16 15:23:23 +00:00
|
|
|
PROP_BRANCH,
|
|
|
|
LAST_PROP
|
|
|
|
};
|
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
static gboolean git_mirror_repo (const char *repo_url,
|
|
|
|
gboolean update,
|
|
|
|
const char *ref,
|
2015-11-30 20:09:56 +00:00
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error);
|
2015-11-30 20:09:56 +00:00
|
|
|
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
static void
|
|
|
|
builder_source_git_finalize (GObject *object)
|
|
|
|
{
|
2016-05-06 14:03:27 +00:00
|
|
|
BuilderSourceGit *self = (BuilderSourceGit *) object;
|
2015-11-16 15:23:23 +00:00
|
|
|
|
|
|
|
g_free (self->url);
|
2016-07-29 19:52:47 +00:00
|
|
|
g_free (self->path);
|
2015-11-16 15:23:23 +00:00
|
|
|
g_free (self->branch);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (builder_source_git_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_git_get_property (GObject *object,
|
2016-05-06 14:03:27 +00:00
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2015-11-16 15:23:23 +00:00
|
|
|
{
|
|
|
|
BuilderSourceGit *self = BUILDER_SOURCE_GIT (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_URL:
|
|
|
|
g_value_set_string (value, self->url);
|
|
|
|
break;
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
case PROP_PATH:
|
|
|
|
g_value_set_string (value, self->path);
|
|
|
|
break;
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
case PROP_BRANCH:
|
|
|
|
g_value_set_string (value, self->branch);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_git_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
BuilderSourceGit *self = BUILDER_SOURCE_GIT (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_URL:
|
|
|
|
g_free (self->url);
|
|
|
|
self->url = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
case PROP_PATH:
|
|
|
|
g_free (self->path);
|
|
|
|
self->path = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
case PROP_BRANCH:
|
|
|
|
g_free (self->branch);
|
|
|
|
self->branch = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-05-06 14:03:27 +00:00
|
|
|
git (GFile *dir,
|
|
|
|
char **output,
|
|
|
|
GError **error,
|
2015-11-16 15:23:23 +00:00
|
|
|
...)
|
|
|
|
{
|
2015-12-03 12:29:08 +00:00
|
|
|
gboolean res;
|
2015-11-16 15:23:23 +00:00
|
|
|
va_list ap;
|
|
|
|
|
2015-12-03 12:29:08 +00:00
|
|
|
va_start (ap, error);
|
2016-05-06 14:37:47 +00:00
|
|
|
res = flatpak_spawn (dir, output, error, "git", ap);
|
2015-11-16 15:23:23 +00:00
|
|
|
va_end (ap);
|
|
|
|
|
2015-12-03 12:29:08 +00:00
|
|
|
return res;
|
2015-11-16 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GFile *
|
2016-07-29 19:52:47 +00:00
|
|
|
git_get_mirror_dir (const char *url_or_path,
|
2015-11-30 20:09:56 +00:00
|
|
|
BuilderContext *context)
|
2015-11-16 15:23:23 +00:00
|
|
|
{
|
|
|
|
g_autoptr(GFile) git_dir = NULL;
|
|
|
|
g_autofree char *filename = NULL;
|
|
|
|
g_autofree char *git_dir_path = NULL;
|
|
|
|
|
|
|
|
git_dir = g_file_get_child (builder_context_get_state_dir (context),
|
|
|
|
"git");
|
|
|
|
|
|
|
|
git_dir_path = g_file_get_path (git_dir);
|
|
|
|
g_mkdir_with_parents (git_dir_path, 0755);
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
/* Technically a path isn't a uri but if it's absolute it should still be unique. */
|
|
|
|
filename = builder_uri_to_filename (url_or_path);
|
2015-11-16 15:23:23 +00:00
|
|
|
return g_file_get_child (git_dir, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_branch (BuilderSourceGit *self)
|
|
|
|
{
|
|
|
|
if (self->branch)
|
|
|
|
return self->branch;
|
|
|
|
else
|
|
|
|
return "master";
|
|
|
|
}
|
|
|
|
|
2015-12-08 13:34:34 +00:00
|
|
|
static char *
|
2016-07-29 19:52:47 +00:00
|
|
|
get_url_or_path (BuilderSourceGit *self,
|
|
|
|
BuilderContext *context,
|
|
|
|
GError **error)
|
2015-12-08 13:34:34 +00:00
|
|
|
{
|
2016-07-29 19:52:47 +00:00
|
|
|
g_autoptr(GFile) repo = NULL;
|
2015-12-08 13:34:34 +00:00
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
if (self->url == NULL && self->path == NULL)
|
2015-12-08 13:34:34 +00:00
|
|
|
{
|
2016-07-29 19:52:47 +00:00
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "No URL or path specified");
|
2015-12-08 13:34:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
if (self->url)
|
2015-12-08 13:34:34 +00:00
|
|
|
{
|
2016-07-29 19:52:47 +00:00
|
|
|
g_autofree char *scheme = NULL;
|
|
|
|
scheme = g_uri_parse_scheme (self->url);
|
|
|
|
if (scheme == NULL)
|
|
|
|
{
|
|
|
|
repo = g_file_resolve_relative_path (builder_context_get_base_dir (context),
|
|
|
|
self->url);
|
|
|
|
return g_file_get_uri (repo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_strdup (self->url);
|
2015-12-08 13:34:34 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
repo = g_file_resolve_relative_path (builder_context_get_base_dir (context),
|
|
|
|
self->path);
|
|
|
|
return g_file_get_path (repo);
|
2015-12-08 13:34:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
static char *
|
2016-05-06 14:03:27 +00:00
|
|
|
git_get_current_commit (GFile *repo_dir,
|
|
|
|
const char *branch,
|
2015-11-30 20:09:56 +00:00
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error)
|
2015-11-16 15:23:23 +00:00
|
|
|
{
|
|
|
|
char *output = NULL;
|
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
if (!git (repo_dir, &output, error,
|
|
|
|
"rev-parse", branch, NULL))
|
2015-11-16 15:23:23 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
/* Trim trailing whitespace */
|
|
|
|
g_strchomp (output);
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
char *
|
2016-07-29 19:52:47 +00:00
|
|
|
make_absolute (const char *orig_parent, const char *orig_relpath, GError **error)
|
2015-11-16 15:23:23 +00:00
|
|
|
{
|
2015-11-30 20:09:56 +00:00
|
|
|
g_autofree char *parent = g_strdup (orig_parent);
|
|
|
|
const char *relpath = orig_relpath;
|
2016-07-29 19:52:47 +00:00
|
|
|
char *start;
|
2015-11-30 20:09:56 +00:00
|
|
|
char *parent_path;
|
2015-11-16 15:23:23 +00:00
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
if (!g_str_has_prefix (relpath, "../"))
|
|
|
|
return g_strdup (orig_relpath);
|
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
if (parent[strlen (parent) - 1] == '/')
|
|
|
|
parent[strlen (parent) - 1] = 0;
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
if ((start = strstr (parent, "://")))
|
|
|
|
start = start + 3;
|
|
|
|
else
|
|
|
|
start = parent;
|
2015-11-16 15:23:23 +00:00
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
parent_path = strchr (start, '/');
|
2015-11-30 20:09:56 +00:00
|
|
|
if (parent_path == NULL)
|
|
|
|
{
|
2016-07-29 19:52:47 +00:00
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid uri or path %s", orig_parent);
|
2015-11-30 20:09:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (g_str_has_prefix (relpath, "../"))
|
|
|
|
{
|
|
|
|
char *last_slash = strrchr (parent_path, '/');
|
|
|
|
if (last_slash == NULL)
|
|
|
|
{
|
2016-07-29 19:52:47 +00:00
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid relative path %s for uri or path %s", orig_relpath, orig_parent);
|
2015-11-30 20:09:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
relpath += 3;
|
|
|
|
*last_slash = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_strconcat (parent, "/", relpath, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-07-29 19:52:47 +00:00
|
|
|
git_mirror_submodules (const char *repo_location,
|
2016-05-06 14:03:27 +00:00
|
|
|
gboolean update,
|
|
|
|
GFile *mirror_dir,
|
|
|
|
const char *revision,
|
2015-11-30 20:09:56 +00:00
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error)
|
2015-11-30 20:09:56 +00:00
|
|
|
{
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autoptr(GKeyFile) key_file = g_key_file_new ();
|
2016-11-01 04:26:23 +00:00
|
|
|
g_autofree gchar *rev_parse_output = NULL;
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autofree gchar *submodule_data = NULL;
|
|
|
|
g_autofree gchar **submodules = NULL;
|
|
|
|
g_autofree gchar *gitmodules = g_strconcat (revision, ":.gitmodules", NULL);
|
|
|
|
gsize num_submodules;
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-11-01 04:26:23 +00:00
|
|
|
if (!git (mirror_dir, &rev_parse_output, NULL, "rev-parse", "--verify", "--quiet", gitmodules, NULL))
|
|
|
|
return TRUE;
|
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
if (git (mirror_dir, &submodule_data, NULL, "show", gitmodules, NULL))
|
2015-11-30 20:09:56 +00:00
|
|
|
{
|
2016-08-12 12:43:18 +00:00
|
|
|
if (!g_key_file_load_from_data (key_file, submodule_data, -1,
|
|
|
|
G_KEY_FILE_NONE, error))
|
|
|
|
return FALSE;
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
submodules = g_key_file_get_groups (key_file, &num_submodules);
|
2015-11-30 20:09:56 +00:00
|
|
|
|
|
|
|
int i;
|
2016-08-12 12:43:18 +00:00
|
|
|
for (i = 0; i < num_submodules; i++)
|
2015-11-30 20:09:56 +00:00
|
|
|
{
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autofree gchar *submodule = NULL;
|
|
|
|
g_autofree gchar *path = NULL;
|
2016-08-24 19:06:00 +00:00
|
|
|
g_autofree gchar *relative_url = NULL;
|
|
|
|
g_autofree gchar *absolute_url = NULL;
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autofree gchar *ls_tree = NULL;
|
|
|
|
g_auto(GStrv) lines = NULL;
|
2015-11-30 20:09:56 +00:00
|
|
|
g_auto(GStrv) words = NULL;
|
2016-08-12 12:43:18 +00:00
|
|
|
|
|
|
|
submodule = submodules[i];
|
|
|
|
|
|
|
|
if (!g_str_has_prefix (submodule, "submodule \""))
|
2015-11-30 20:09:56 +00:00
|
|
|
continue;
|
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
path = g_key_file_get_string (key_file, submodule, "path", error);
|
|
|
|
if (path == NULL)
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2016-08-24 19:06:00 +00:00
|
|
|
relative_url = g_key_file_get_string (key_file, submodule, "url", error);
|
|
|
|
absolute_url = make_absolute (repo_location, relative_url, error);
|
|
|
|
if (absolute_url == NULL)
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
if (!git (mirror_dir, &ls_tree, error, "ls-tree", revision, path, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lines = g_strsplit (g_strstrip (ls_tree), "\n", 0);
|
|
|
|
if (g_strv_length (lines) != 1)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not a gitlink tree: %s", path);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
words = g_strsplit_set (lines[0], " \t", 4);
|
|
|
|
|
2016-09-12 21:07:07 +00:00
|
|
|
if (g_strcmp0 (words[0], "160000") != 0)
|
2016-09-12 21:08:41 +00:00
|
|
|
continue;
|
2016-08-12 12:43:18 +00:00
|
|
|
|
2016-08-24 19:06:00 +00:00
|
|
|
if (!git_mirror_repo (absolute_url, update, words[2], context, error))
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-07-29 19:52:47 +00:00
|
|
|
git_mirror_repo (const char *repo_location,
|
2016-05-06 14:03:27 +00:00
|
|
|
gboolean update,
|
|
|
|
const char *ref,
|
2015-11-30 20:09:56 +00:00
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error)
|
2015-11-30 20:09:56 +00:00
|
|
|
{
|
|
|
|
g_autoptr(GFile) mirror_dir = NULL;
|
|
|
|
g_autofree char *current_commit = NULL;
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
mirror_dir = git_get_mirror_dir (repo_location, context);
|
2015-11-16 15:23:23 +00:00
|
|
|
|
|
|
|
if (!g_file_query_exists (mirror_dir, NULL))
|
|
|
|
{
|
|
|
|
g_autofree char *filename = g_file_get_basename (mirror_dir);
|
|
|
|
g_autoptr(GFile) parent = g_file_get_parent (mirror_dir);
|
|
|
|
g_autofree char *filename_tmp = g_strconcat (filename, ".clone_tmp", NULL);
|
|
|
|
g_autoptr(GFile) mirror_dir_tmp = g_file_get_child (parent, filename_tmp);
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
g_print ("Cloning git repo %s\n", repo_location);
|
2015-12-04 07:49:43 +00:00
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
if (!git (parent, NULL, error,
|
2016-07-29 19:52:47 +00:00
|
|
|
"clone", "--mirror", repo_location, filename_tmp, NULL) ||
|
2015-11-16 15:23:23 +00:00
|
|
|
!g_file_move (mirror_dir_tmp, mirror_dir, 0, NULL, NULL, NULL, error))
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-12-21 09:39:23 +00:00
|
|
|
else if (update)
|
2015-11-16 15:23:23 +00:00
|
|
|
{
|
2016-07-29 19:52:47 +00:00
|
|
|
g_print ("Fetching git repo %s\n", repo_location);
|
2015-11-16 15:23:23 +00:00
|
|
|
if (!git (mirror_dir, NULL, error,
|
2015-12-04 07:51:14 +00:00
|
|
|
"fetch", "-p", NULL))
|
2015-11-16 15:23:23 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
current_commit = git_get_current_commit (mirror_dir, ref, context, error);
|
|
|
|
if (current_commit == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
if (!git_mirror_submodules (repo_location, update, mirror_dir, current_commit, context, error))
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-05-06 14:03:27 +00:00
|
|
|
builder_source_git_download (BuilderSource *source,
|
|
|
|
gboolean update_vcs,
|
2015-11-30 20:09:56 +00:00
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error)
|
2015-11-30 20:09:56 +00:00
|
|
|
{
|
|
|
|
BuilderSourceGit *self = BUILDER_SOURCE_GIT (source);
|
2016-07-29 19:52:47 +00:00
|
|
|
g_autofree char *location = NULL;
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
location = get_url_or_path (self, context, error);
|
|
|
|
if (location == NULL)
|
2015-12-08 13:34:34 +00:00
|
|
|
return FALSE;
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
if (!git_mirror_repo (location,
|
2015-12-21 09:39:23 +00:00
|
|
|
update_vcs,
|
2015-11-30 20:09:56 +00:00
|
|
|
get_branch (self),
|
|
|
|
context,
|
|
|
|
error))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-07-29 19:52:47 +00:00
|
|
|
git_extract_submodule (const char *repo_location,
|
2016-05-06 14:03:27 +00:00
|
|
|
GFile *checkout_dir,
|
2016-08-12 12:43:18 +00:00
|
|
|
const char *revision,
|
2015-11-30 20:09:56 +00:00
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error)
|
2015-11-30 20:09:56 +00:00
|
|
|
{
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autoptr(GKeyFile) key_file = g_key_file_new ();
|
2016-11-01 04:26:23 +00:00
|
|
|
g_autofree gchar *rev_parse_output = NULL;
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autofree gchar *submodule_data = NULL;
|
|
|
|
g_autofree gchar **submodules = NULL;
|
|
|
|
g_autofree gchar *gitmodules = g_strconcat (revision, ":.gitmodules", NULL);
|
|
|
|
gsize num_submodules;
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-11-01 04:26:23 +00:00
|
|
|
if (!git (checkout_dir, &rev_parse_output, NULL, "rev-parse", "--verify", "--quiet", gitmodules, NULL))
|
|
|
|
return TRUE;
|
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
if (git (checkout_dir, &submodule_data, NULL, "show", gitmodules, NULL))
|
2015-11-30 20:09:56 +00:00
|
|
|
{
|
2016-08-12 12:43:18 +00:00
|
|
|
if (!g_key_file_load_from_data (key_file, submodule_data, -1,
|
|
|
|
G_KEY_FILE_NONE, error))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
submodules = g_key_file_get_groups (key_file, &num_submodules);
|
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
int i;
|
2016-08-12 12:43:18 +00:00
|
|
|
for (i = 0; i < num_submodules; i++)
|
2015-11-30 20:09:56 +00:00
|
|
|
{
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autofree gchar *submodule = NULL;
|
|
|
|
g_autofree gchar *name = NULL;
|
|
|
|
g_autofree gchar *update_method = NULL;
|
|
|
|
g_autofree gchar *path = NULL;
|
2016-08-24 19:06:00 +00:00
|
|
|
g_autofree gchar *relative_url = NULL;
|
|
|
|
g_autofree gchar *absolute_url = NULL;
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autofree gchar *ls_tree = NULL;
|
|
|
|
g_auto(GStrv) lines = NULL;
|
|
|
|
g_auto(GStrv) words = NULL;
|
2015-11-30 20:09:56 +00:00
|
|
|
g_autoptr(GFile) mirror_dir = NULL;
|
|
|
|
g_autoptr(GFile) child_dir = NULL;
|
2016-08-12 12:43:18 +00:00
|
|
|
g_autofree gchar *mirror_dir_as_url = NULL;
|
|
|
|
g_autofree gchar *option = NULL;
|
|
|
|
gsize len;
|
|
|
|
|
|
|
|
submodule = submodules[i];
|
|
|
|
len = strlen (submodule);
|
|
|
|
|
|
|
|
if (!g_str_has_prefix (submodule, "submodule \""))
|
2015-11-30 20:09:56 +00:00
|
|
|
continue;
|
2016-08-12 12:43:18 +00:00
|
|
|
|
|
|
|
name = g_strndup (submodule + 11, len - 12);
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-01-28 20:52:45 +00:00
|
|
|
/* Skip any submodules that are disabled (have the update method set to "none")
|
|
|
|
Only check if the command succeeds. If it fails, the update method is not set. */
|
2016-08-12 12:43:18 +00:00
|
|
|
update_method = g_key_file_get_string (key_file, submodule, "update", NULL);
|
|
|
|
if (g_strcmp0 (update_method, "none") == 0)
|
|
|
|
continue;
|
2016-01-28 20:52:45 +00:00
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
path = g_key_file_get_string (key_file, submodule, "path", error);
|
|
|
|
if (path == NULL)
|
|
|
|
return FALSE;
|
2016-01-28 18:46:40 +00:00
|
|
|
|
2016-08-24 19:06:00 +00:00
|
|
|
relative_url = g_key_file_get_string (key_file, submodule, "url", error);
|
|
|
|
absolute_url = make_absolute (repo_location, relative_url, error);
|
|
|
|
if (absolute_url == NULL)
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
if (!git (checkout_dir, &ls_tree, error, "ls-tree", revision, path, NULL))
|
|
|
|
return FALSE;
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
lines = g_strsplit (g_strstrip (ls_tree), "\n", 0);
|
|
|
|
if (g_strv_length (lines) != 1)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not a gitlink tree: %s", path);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
words = g_strsplit_set (lines[0], " \t", 4);
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-09-12 21:07:07 +00:00
|
|
|
if (g_strcmp0 (words[0], "160000") != 0)
|
2016-09-12 21:08:41 +00:00
|
|
|
continue;
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-08-24 19:06:00 +00:00
|
|
|
mirror_dir = git_get_mirror_dir (absolute_url, context);
|
2015-11-30 20:09:56 +00:00
|
|
|
mirror_dir_as_url = g_file_get_uri (mirror_dir);
|
2016-08-12 12:43:18 +00:00
|
|
|
option = g_strdup_printf ("submodule.%s.url", name);
|
2015-11-30 20:09:56 +00:00
|
|
|
|
|
|
|
if (!git (checkout_dir, NULL, error,
|
|
|
|
"config", option, mirror_dir_as_url, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!git (checkout_dir, NULL, error,
|
2016-08-12 12:43:18 +00:00
|
|
|
"submodule", "update", "--init", path, NULL))
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
child_dir = g_file_resolve_relative_path (checkout_dir, path);
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2016-08-24 19:06:00 +00:00
|
|
|
if (!git_extract_submodule (absolute_url, child_dir, words[2], context, error))
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-05-06 14:03:27 +00:00
|
|
|
builder_source_git_extract (BuilderSource *source,
|
|
|
|
GFile *dest,
|
2016-09-05 20:01:56 +00:00
|
|
|
BuilderOptions *build_options,
|
2015-11-16 15:23:23 +00:00
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error)
|
2015-11-16 15:23:23 +00:00
|
|
|
{
|
|
|
|
BuilderSourceGit *self = BUILDER_SOURCE_GIT (source);
|
2016-05-06 14:03:27 +00:00
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
g_autoptr(GFile) mirror_dir = NULL;
|
|
|
|
g_autofree char *mirror_dir_path = NULL;
|
|
|
|
g_autofree char *dest_path = NULL;
|
2016-07-29 19:52:47 +00:00
|
|
|
g_autofree char *location = NULL;
|
2015-11-16 15:23:23 +00:00
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
location = get_url_or_path (self, context, error);
|
|
|
|
if (location == NULL)
|
2015-12-08 13:34:34 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
mirror_dir = git_get_mirror_dir (location, context);
|
2015-11-16 15:23:23 +00:00
|
|
|
|
|
|
|
mirror_dir_path = g_file_get_path (mirror_dir);
|
|
|
|
dest_path = g_file_get_path (dest);
|
|
|
|
|
|
|
|
if (!git (NULL, NULL, error,
|
2015-12-09 13:07:33 +00:00
|
|
|
"clone", mirror_dir_path, dest_path, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!git (dest, NULL, error,
|
|
|
|
"checkout", get_branch (self), NULL))
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2016-08-12 12:43:18 +00:00
|
|
|
if (!git_extract_submodule (location, dest, get_branch (self), context, error))
|
2015-11-16 15:23:23 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2015-12-09 20:32:18 +00:00
|
|
|
if (!git (dest, NULL, error,
|
2016-07-29 19:52:47 +00:00
|
|
|
"config", "--local", "remote.origin.url",
|
|
|
|
location, NULL))
|
2015-12-09 20:32:18 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_git_checksum (BuilderSource *source,
|
|
|
|
BuilderCache *cache,
|
|
|
|
BuilderContext *context)
|
|
|
|
{
|
|
|
|
BuilderSourceGit *self = BUILDER_SOURCE_GIT (source);
|
2016-05-06 14:03:27 +00:00
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
g_autoptr(GFile) mirror_dir = NULL;
|
2015-12-08 13:34:34 +00:00
|
|
|
g_autofree char *current_commit = NULL;
|
2015-11-16 15:23:23 +00:00
|
|
|
g_autoptr(GError) error = NULL;
|
2016-07-29 19:52:47 +00:00
|
|
|
g_autofree char *location = NULL;
|
2015-11-16 15:23:23 +00:00
|
|
|
|
|
|
|
builder_cache_checksum_str (cache, self->url);
|
2016-07-29 19:52:47 +00:00
|
|
|
builder_cache_checksum_str (cache, self->path);
|
2015-11-16 15:23:23 +00:00
|
|
|
builder_cache_checksum_str (cache, self->branch);
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
location = get_url_or_path (self, context, &error);
|
|
|
|
if (location != NULL)
|
2015-12-08 13:34:34 +00:00
|
|
|
{
|
2016-07-29 19:52:47 +00:00
|
|
|
mirror_dir = git_get_mirror_dir (location, context);
|
2015-11-30 20:09:56 +00:00
|
|
|
|
2015-12-08 13:34:34 +00:00
|
|
|
current_commit = git_get_current_commit (mirror_dir, get_branch (self), context, &error);
|
|
|
|
if (current_commit)
|
|
|
|
builder_cache_checksum_str (cache, current_commit);
|
|
|
|
else if (error)
|
|
|
|
g_warning ("Failed to get current git checksum: %s", error->message);
|
|
|
|
}
|
|
|
|
else
|
2016-05-06 14:03:27 +00:00
|
|
|
{
|
2016-07-29 19:52:47 +00:00
|
|
|
g_warning ("No url or path");
|
2016-05-06 14:03:27 +00:00
|
|
|
}
|
2015-11-16 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 13:26:41 +00:00
|
|
|
static gboolean
|
|
|
|
builder_source_git_update (BuilderSource *source,
|
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error)
|
2015-12-09 13:26:41 +00:00
|
|
|
{
|
|
|
|
BuilderSourceGit *self = BUILDER_SOURCE_GIT (source);
|
2016-05-06 14:03:27 +00:00
|
|
|
|
2015-12-09 13:26:41 +00:00
|
|
|
g_autoptr(GFile) mirror_dir = NULL;
|
|
|
|
char *current_commit;
|
2016-07-29 19:52:47 +00:00
|
|
|
g_autofree char *location = NULL;
|
2015-12-09 13:26:41 +00:00
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
location = get_url_or_path (self, context, error);
|
|
|
|
if (location == NULL)
|
2015-12-09 13:26:41 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2016-07-29 19:52:47 +00:00
|
|
|
mirror_dir = git_get_mirror_dir (location, context);
|
2015-12-09 13:26:41 +00:00
|
|
|
|
|
|
|
current_commit = git_get_current_commit (mirror_dir, get_branch (self), context, NULL);
|
|
|
|
if (current_commit)
|
|
|
|
{
|
|
|
|
g_free (self->branch);
|
|
|
|
self->branch = current_commit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
static void
|
|
|
|
builder_source_git_class_init (BuilderSourceGitClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
BuilderSourceClass *source_class = BUILDER_SOURCE_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = builder_source_git_finalize;
|
|
|
|
object_class->get_property = builder_source_git_get_property;
|
|
|
|
object_class->set_property = builder_source_git_set_property;
|
|
|
|
|
|
|
|
source_class->download = builder_source_git_download;
|
|
|
|
source_class->extract = builder_source_git_extract;
|
2015-12-09 13:26:41 +00:00
|
|
|
source_class->update = builder_source_git_update;
|
2015-11-16 15:23:23 +00:00
|
|
|
source_class->checksum = builder_source_git_checksum;
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_URL,
|
|
|
|
g_param_spec_string ("url",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
2016-07-29 19:52:47 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_PATH,
|
|
|
|
g_param_spec_string ("path",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
2015-11-16 15:23:23 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_BRANCH,
|
|
|
|
g_param_spec_string ("branch",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_git_init (BuilderSourceGit *self)
|
|
|
|
{
|
|
|
|
}
|