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"
|
2017-02-20 15:36:11 +00:00
|
|
|
#include "builder-git.h"
|
2015-11-16 15:23:23 +00:00
|
|
|
#include "builder-utils.h"
|
2017-08-25 07:35:34 +00:00
|
|
|
#include "builder-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;
|
2017-04-04 13:21:11 +00:00
|
|
|
char *commit;
|
2017-04-04 12:31:48 +00:00
|
|
|
gboolean disable_fsckobjects;
|
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,
|
2017-04-04 13:21:11 +00:00
|
|
|
PROP_COMMIT,
|
2017-04-04 12:31:48 +00:00
|
|
|
PROP_DISABLE_FSCKOBJECTS,
|
2015-11-16 15:23:23 +00:00
|
|
|
LAST_PROP
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2017-04-04 13:21:11 +00:00
|
|
|
g_free (self->commit);
|
2015-11-16 15:23:23 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-04-04 13:21:11 +00:00
|
|
|
case PROP_COMMIT:
|
|
|
|
g_value_set_string (value, self->commit);
|
|
|
|
break;
|
|
|
|
|
2017-04-04 12:31:48 +00:00
|
|
|
case PROP_DISABLE_FSCKOBJECTS:
|
|
|
|
g_value_set_boolean (value, self->disable_fsckobjects);
|
|
|
|
break;
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
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;
|
|
|
|
|
2017-04-04 13:21:11 +00:00
|
|
|
case PROP_COMMIT:
|
|
|
|
g_free (self->commit);
|
|
|
|
self->commit = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
2017-04-04 12:31:48 +00:00
|
|
|
case PROP_DISABLE_FSCKOBJECTS:
|
|
|
|
self->disable_fsckobjects = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_branch (BuilderSourceGit *self)
|
|
|
|
{
|
|
|
|
if (self->branch)
|
|
|
|
return self->branch;
|
2017-04-04 13:21:11 +00:00
|
|
|
else if (self->commit)
|
|
|
|
return self->commit;
|
2015-11-16 15:23:23 +00:00
|
|
|
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;
|
2017-04-19 16:54:33 +00:00
|
|
|
GFile *base_dir = BUILDER_SOURCE (self)->base_dir;
|
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)
|
|
|
|
{
|
2017-04-19 16:54:33 +00:00
|
|
|
repo = g_file_resolve_relative_path (base_dir, self->url);
|
2016-07-29 19:52:47 +00:00
|
|
|
return g_file_get_uri (repo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_strdup (self->url);
|
2015-12-08 13:34:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 16:54:33 +00:00
|
|
|
repo = g_file_resolve_relative_path (base_dir,
|
2016-07-29 19:52:47 +00:00
|
|
|
self->path);
|
|
|
|
return g_file_get_path (repo);
|
2015-12-08 13:34:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
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
|
|
|
|
2017-02-20 15:36:11 +00:00
|
|
|
if (!builder_git_mirror_repo (location,
|
2017-04-10 08:24:36 +00:00
|
|
|
NULL,
|
2017-04-04 12:31:48 +00:00
|
|
|
update_vcs, TRUE, self->disable_fsckobjects,
|
2017-02-20 15:36:11 +00:00
|
|
|
get_branch (self),
|
|
|
|
context,
|
|
|
|
error))
|
2015-11-30 20:09:56 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2017-04-04 13:21:11 +00:00
|
|
|
if (self->commit != NULL && self->branch != NULL)
|
|
|
|
{
|
2017-05-11 08:52:30 +00:00
|
|
|
/* We want to support the commit being both a tag object and the real commit object that it points too */
|
|
|
|
g_autofree char *current_commit = builder_git_get_current_commit (location,get_branch (self), FALSE, context, error);
|
|
|
|
g_autofree char *current_commit2 = builder_git_get_current_commit (location,get_branch (self), TRUE, context, error);
|
|
|
|
if (current_commit == NULL || current_commit2 == NULL)
|
2017-04-04 13:21:11 +00:00
|
|
|
return FALSE;
|
2017-05-11 08:52:30 +00:00
|
|
|
if (strcmp (current_commit, self->commit) != 0 && strcmp (current_commit2, self->commit) != 0)
|
|
|
|
return flatpak_fail (error, "Git commit for branch %s is %s, but expected %s\n", self->branch, current_commit2, self->commit);
|
2017-04-04 13:21:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 20:09:56 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
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-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;
|
|
|
|
|
2017-02-20 15:36:11 +00:00
|
|
|
if (!builder_git_checkout (location, get_branch (self),
|
|
|
|
dest, context, error))
|
2015-12-09 20:32:18 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-03-31 05:56:10 +00:00
|
|
|
static gboolean
|
|
|
|
builder_source_git_bundle (BuilderSource *source,
|
|
|
|
BuilderContext *context,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
BuilderSourceGit *self = BUILDER_SOURCE_GIT (source);
|
|
|
|
|
|
|
|
g_autofree char *location = NULL;
|
|
|
|
g_autoptr(GFile) mirror_dir = NULL;
|
|
|
|
|
|
|
|
location = get_url_or_path (self, context, error);
|
|
|
|
|
2017-04-24 11:54:53 +00:00
|
|
|
g_print ("builder_source_git_bundle %s\n", location);
|
|
|
|
|
2017-03-31 05:56:10 +00:00
|
|
|
if (location == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-24 11:54:53 +00:00
|
|
|
mirror_dir = flatpak_build_file (builder_context_get_app_dir (context),
|
|
|
|
"sources/git",
|
|
|
|
NULL);
|
2017-04-21 14:38:05 +00:00
|
|
|
if (!flatpak_mkdir_p (mirror_dir, NULL, error))
|
2017-03-31 05:56:10 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!builder_git_mirror_repo (location,
|
2017-04-24 11:54:53 +00:00
|
|
|
flatpak_file_get_path_cached (mirror_dir),
|
2017-04-10 08:24:36 +00:00
|
|
|
FALSE, TRUE, FALSE,
|
2017-03-31 05:56:10 +00:00
|
|
|
get_branch (self),
|
|
|
|
context,
|
|
|
|
error))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
static void
|
|
|
|
builder_source_git_checksum (BuilderSource *source,
|
|
|
|
BuilderCache *cache,
|
|
|
|
BuilderContext *context)
|
|
|
|
{
|
|
|
|
BuilderSourceGit *self = BUILDER_SOURCE_GIT (source);
|
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);
|
2017-04-04 13:21:11 +00:00
|
|
|
builder_cache_checksum_compat_str (cache, self->commit);
|
2017-04-04 12:31:48 +00:00
|
|
|
builder_cache_checksum_compat_boolean (cache, self->disable_fsckobjects);
|
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
|
|
|
{
|
2017-05-11 08:52:30 +00:00
|
|
|
current_commit = builder_git_get_current_commit (location,get_branch (self), FALSE, context, &error);
|
2015-12-08 13:34:34 +00:00
|
|
|
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);
|
|
|
|
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;
|
|
|
|
|
2017-05-11 08:52:30 +00:00
|
|
|
current_commit = builder_git_get_current_commit (location, get_branch (self), FALSE, context, NULL);
|
2015-12-09 13:26:41 +00:00
|
|
|
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;
|
2017-03-31 05:56:10 +00:00
|
|
|
source_class->bundle = builder_source_git_bundle;
|
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));
|
2017-04-04 13:21:11 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_COMMIT,
|
|
|
|
g_param_spec_string ("commit",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
2017-04-04 12:31:48 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_DISABLE_FSCKOBJECTS,
|
|
|
|
g_param_spec_boolean ("disable-fsckobjects",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE));
|
2015-11-16 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_git_init (BuilderSourceGit *self)
|
|
|
|
{
|
|
|
|
}
|