2015-11-16 15:23:23 +00:00
|
|
|
/* builder-source-bzr.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-bzr.h"
|
|
|
|
#include "builder-utils.h"
|
2015-12-03 12:29:08 +00:00
|
|
|
#include "xdg-app-utils.h"
|
2015-11-16 15:23:23 +00:00
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
struct BuilderSourceBzr
|
|
|
|
{
|
2015-11-16 15:23:23 +00:00
|
|
|
BuilderSource parent;
|
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
char *url;
|
|
|
|
char *revision;
|
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;
|
|
|
|
} BuilderSourceBzrClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (BuilderSourceBzr, builder_source_bzr, BUILDER_TYPE_SOURCE);
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_URL,
|
2015-12-09 13:06:59 +00:00
|
|
|
PROP_REVISION,
|
2015-11-16 15:23:23 +00:00
|
|
|
LAST_PROP
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_bzr_finalize (GObject *object)
|
|
|
|
{
|
2016-05-06 14:03:27 +00:00
|
|
|
BuilderSourceBzr *self = (BuilderSourceBzr *) object;
|
2015-11-16 15:23:23 +00:00
|
|
|
|
|
|
|
g_free (self->url);
|
2015-12-09 13:06:59 +00:00
|
|
|
g_free (self->revision);
|
2015-11-16 15:23:23 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (builder_source_bzr_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_bzr_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
|
|
|
{
|
|
|
|
BuilderSourceBzr *self = BUILDER_SOURCE_BZR (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_URL:
|
|
|
|
g_value_set_string (value, self->url);
|
|
|
|
break;
|
|
|
|
|
2015-12-09 13:06:59 +00:00
|
|
|
case PROP_REVISION:
|
|
|
|
g_value_set_string (value, self->revision);
|
|
|
|
break;
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_bzr_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
BuilderSourceBzr *self = BUILDER_SOURCE_BZR (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_URL:
|
|
|
|
g_free (self->url);
|
|
|
|
self->url = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
2015-12-09 13:06:59 +00:00
|
|
|
case PROP_REVISION:
|
|
|
|
g_free (self->revision);
|
|
|
|
self->revision = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-05-06 14:03:27 +00:00
|
|
|
bzr (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, "bzr", ap);
|
2015-12-03 12:29:08 +00:00
|
|
|
va_end (ap);
|
2015-11-16 15:23:23 +00:00
|
|
|
|
2015-12-03 12:29:08 +00:00
|
|
|
return res;
|
2015-11-16 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GFile *
|
|
|
|
get_mirror_dir (BuilderSourceBzr *self, BuilderContext *context)
|
|
|
|
{
|
|
|
|
g_autoptr(GFile) bzr_dir = NULL;
|
|
|
|
g_autofree char *filename = NULL;
|
|
|
|
g_autofree char *bzr_dir_path = NULL;
|
|
|
|
|
|
|
|
bzr_dir = g_file_get_child (builder_context_get_state_dir (context),
|
|
|
|
"bzr");
|
|
|
|
|
|
|
|
bzr_dir_path = g_file_get_path (bzr_dir);
|
|
|
|
g_mkdir_with_parents (bzr_dir_path, 0755);
|
|
|
|
|
|
|
|
filename = builder_uri_to_filename (self->url);
|
|
|
|
return g_file_get_child (bzr_dir, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_current_commit (BuilderSourceBzr *self, BuilderContext *context, GError **error)
|
|
|
|
{
|
|
|
|
g_autoptr(GFile) mirror_dir = NULL;
|
|
|
|
char *output = NULL;
|
|
|
|
|
|
|
|
mirror_dir = get_mirror_dir (self, context);
|
|
|
|
|
|
|
|
if (!bzr (mirror_dir, &output, error,
|
2016-05-06 14:03:27 +00:00
|
|
|
"revno", NULL))
|
2015-11-16 15:23:23 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-05-06 14:03:27 +00:00
|
|
|
builder_source_bzr_download (BuilderSource *source,
|
|
|
|
gboolean update_vcs,
|
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
|
|
|
{
|
|
|
|
BuilderSourceBzr *self = BUILDER_SOURCE_BZR (source);
|
2016-05-06 14:03:27 +00:00
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
g_autoptr(GFile) mirror_dir = NULL;
|
|
|
|
|
|
|
|
if (self->url == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "URL not specified");
|
2015-12-01 15:39:01 +00:00
|
|
|
return FALSE;
|
2015-11-16 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mirror_dir = get_mirror_dir (self, context);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2015-12-04 07:49:43 +00:00
|
|
|
g_print ("Getting bzr repo %s\n", self->url);
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
if (!bzr (parent, NULL, error,
|
|
|
|
"branch", self->url, filename_tmp, NULL) ||
|
|
|
|
!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_vcs)
|
2015-11-16 15:23:23 +00:00
|
|
|
{
|
2015-12-04 07:49:43 +00:00
|
|
|
g_print ("Updating bzr repo %s\n", self->url);
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
if (!bzr (mirror_dir, NULL, error,
|
|
|
|
"pull", NULL))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-05-06 14:03:27 +00:00
|
|
|
builder_source_bzr_extract (BuilderSource *source,
|
|
|
|
GFile *dest,
|
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
|
|
|
{
|
|
|
|
BuilderSourceBzr *self = BUILDER_SOURCE_BZR (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;
|
|
|
|
|
|
|
|
mirror_dir = get_mirror_dir (self, context);
|
|
|
|
|
|
|
|
mirror_dir_path = g_file_get_path (mirror_dir);
|
|
|
|
dest_path = g_file_get_path (dest);
|
|
|
|
|
|
|
|
if (!bzr (NULL, NULL, error,
|
|
|
|
"branch", "--stacked", mirror_dir_path, dest_path, "--use-existing-dir", NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
2015-12-09 13:06:59 +00:00
|
|
|
if (self->revision)
|
|
|
|
{
|
|
|
|
g_autofree char *revarg = g_strdup_printf ("-r%s", self->revision);
|
|
|
|
if (!bzr (dest, NULL, error,
|
|
|
|
"revert", revarg, NULL))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_bzr_checksum (BuilderSource *source,
|
|
|
|
BuilderCache *cache,
|
|
|
|
BuilderContext *context)
|
|
|
|
{
|
|
|
|
BuilderSourceBzr *self = BUILDER_SOURCE_BZR (source);
|
|
|
|
g_autofree char *current_commit;
|
2016-05-06 14:03:27 +00:00
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
g_autoptr(GError) error = NULL;
|
|
|
|
|
|
|
|
builder_cache_checksum_str (cache, self->url);
|
2015-12-09 13:06:59 +00:00
|
|
|
builder_cache_checksum_str (cache, self->revision);
|
2015-11-16 15:23:23 +00:00
|
|
|
|
|
|
|
current_commit = get_current_commit (self, context, &error);
|
|
|
|
if (current_commit)
|
|
|
|
builder_cache_checksum_str (cache, current_commit);
|
|
|
|
else if (error)
|
|
|
|
g_warning ("Failed to get current bzr checksum: %s", error->message);
|
|
|
|
}
|
|
|
|
|
2015-12-09 13:26:41 +00:00
|
|
|
static gboolean
|
|
|
|
builder_source_bzr_update (BuilderSource *source,
|
|
|
|
BuilderContext *context,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error)
|
2015-12-09 13:26:41 +00:00
|
|
|
{
|
|
|
|
BuilderSourceBzr *self = BUILDER_SOURCE_BZR (source);
|
|
|
|
char *current_commit;
|
|
|
|
|
|
|
|
current_commit = get_current_commit (self, context, NULL);
|
|
|
|
if (current_commit)
|
|
|
|
{
|
|
|
|
g_free (self->revision);
|
|
|
|
self->revision = current_commit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-16 15:23:23 +00:00
|
|
|
static void
|
|
|
|
builder_source_bzr_class_init (BuilderSourceBzrClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
BuilderSourceClass *source_class = BUILDER_SOURCE_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = builder_source_bzr_finalize;
|
|
|
|
object_class->get_property = builder_source_bzr_get_property;
|
|
|
|
object_class->set_property = builder_source_bzr_set_property;
|
|
|
|
|
|
|
|
source_class->download = builder_source_bzr_download;
|
|
|
|
source_class->extract = builder_source_bzr_extract;
|
2015-12-09 13:26:41 +00:00
|
|
|
source_class->update = builder_source_bzr_update;
|
2015-11-16 15:23:23 +00:00
|
|
|
source_class->checksum = builder_source_bzr_checksum;
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_URL,
|
|
|
|
g_param_spec_string ("url",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
2015-12-09 13:06:59 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_REVISION,
|
|
|
|
g_param_spec_string ("revision",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
2015-11-16 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
builder_source_bzr_init (BuilderSourceBzr *self)
|
|
|
|
{
|
|
|
|
}
|