Commit f02f218d authored by Christian Hergert's avatar Christian Hergert

clone: backport clone widget improvements from master

This brings in various changes from master that took a slight bit of
tweaking to apply cleanly.
parent 17e1b190
......@@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "gbp-flatpak-clone-widget"
#include <dazzle.h>
#include <glib/gi18n.h>
#include <json-glib/json-glib.h>
......@@ -25,8 +27,6 @@
#include "gbp-flatpak-clone-widget.h"
#include "gbp-flatpak-sources.h"
#define G_LOG_DOMAIN "gbp-flatpak-clone-widget"
#define ANIMATION_DURATION_MSEC 250
struct _GbpFlatpakCloneWidget
......@@ -289,6 +289,61 @@ gbp_flatpak_clone_widget_worker_completed (IdeTask *task,
g_timeout_add (ANIMATION_DURATION_MSEC, open_after_timeout, g_object_ref (task));
}
static gboolean
check_directory_exists_and_nonempty (GFile *directory,
gboolean *out_directory_exists_and_nonempty,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GError) local_error = NULL;
g_autoptr(GFileEnumerator) enumerator = NULL;
g_autoptr(GFileInfo) info = NULL;
g_autofree gchar *path = g_file_get_path (directory);
g_assert (G_IS_FILE (directory));
g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
g_assert (out_directory_exists_and_nonempty != NULL);
*out_directory_exists_and_nonempty = FALSE;
enumerator = g_file_enumerate_children (directory,
G_FILE_ATTRIBUTE_STANDARD_NAME,
G_FILE_QUERY_INFO_NONE,
cancellable,
&local_error);
if (enumerator == NULL)
{
if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
*out_directory_exists_and_nonempty = FALSE;
return TRUE;
}
g_propagate_error (error, g_steal_pointer (&local_error));
return FALSE;
}
/* Check for at least one child in the directory - if there is
* one then the directory is non-empty. Otherwise, the directory
* is either empty or an error occurred. */
info = g_file_enumerator_next_file (enumerator, cancellable, &local_error);
if (info != NULL)
{
*out_directory_exists_and_nonempty = TRUE;
return TRUE;
}
if (local_error != NULL)
{
g_propagate_error (error, g_steal_pointer (&local_error));
return FALSE;
}
*out_directory_exists_and_nonempty = FALSE;
return TRUE;
}
static void
gbp_flatpak_clone_widget_worker (IdeTask *task,
gpointer source_object,
......@@ -392,27 +447,58 @@ gbp_flatpak_clone_widget_worker (IdeTask *task,
}
else if (req->src->type == TYPE_ARCHIVE)
{
uristr = ide_vcs_uri_to_string (req->src->uri);
req->project_file = fetch_archive (uristr,
req->src->sha,
req->src->name,
req->destination,
self->strip_components,
&error);
if (error != NULL)
g_autoptr(GFile) source_dir = g_file_get_child (req->destination,
req->src->name);
gboolean exists_and_nonempty = FALSE;
if (!check_directory_exists_and_nonempty (source_dir,
&exists_and_nonempty,
cancellable,
&error))
{
ide_task_return_error (task, g_steal_pointer (&error));
return;
}
/*
* If the target directory already exists and was non-empty,
* then we have probably already checked out this project
* using GbpFlatpakCloneWidget. In that case, we don't want
* to overwrite it, we should get just return the source_dir
* directly.
*/
if (exists_and_nonempty)
{
g_debug ("Re-using non-empty source dir %s already at destination",
req->src->name);
req->project_file = g_steal_pointer (&source_dir);
}
else
{
uristr = ide_vcs_uri_to_string (req->src->uri);
g_debug ("Fetching source archive from %s", uristr);
req->project_file = gbp_flatpak_sources_fetch_archive (uristr,
req->src->sha,
req->src->name,
req->destination,
self->strip_components,
&error);
if (error != NULL)
{
ide_task_return_error (task, g_steal_pointer (&error));
return;
}
}
}
for (i = 0; req->src->patches[i]; i++)
{
if (!apply_patch (req->src->patches[i],
req->project_file,
self->strip_components,
&error))
{
if (!gbp_flatpak_sources_apply_patch (req->src->patches[i],
req->project_file,
self->strip_components,
&error))
{
ide_task_return_error (task, g_steal_pointer (&error));
return;
}
......@@ -493,7 +579,6 @@ get_source (GbpFlatpakCloneWidget *self,
JsonArray *sources = NULL;
guint num_modules;
ModuleSource *src;
g_autoptr(IdeVcsUri) uri = NULL;
GPtrArray *patches;
parser = json_parser_new ();
......@@ -578,7 +663,6 @@ gbp_flatpak_clone_widget_clone_async (GbpFlatpakCloneWidget *self,
g_autoptr(IdeTask) task = NULL;
g_autoptr(GFile) destination = NULL;
g_autoptr(GError) error = NULL;
g_autofree gchar *path = NULL;
DownloadRequest *req;
ModuleSource *src;
......
/* gbp-flatpak-sources.c
*
* Copyright © 2016 Endless Mobile, Inc.
* Copyright 2016 Endless Mobile, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -44,13 +44,13 @@ typedef enum {
ZIP
} ArchiveType;
gboolean
static gboolean
is_tar (ArchiveType type)
{
return (type >= TAR) && (type <= TAR_XZ);
}
const char *
static const char *
tar_decompress_flag (ArchiveType type)
{
if (type == TAR_GZIP)
......@@ -71,7 +71,7 @@ tar_decompress_flag (ArchiveType type)
return NULL;
}
ArchiveType
static ArchiveType
get_type (GFile *archivefile)
{
g_autofree gchar *base_name = NULL;
......@@ -318,12 +318,12 @@ patch (GFile *dir,
args = g_ptr_array_new ();
if (use_git)
{
g_ptr_array_add (args, "git");
g_ptr_array_add (args, "apply");
g_ptr_array_add (args, (gchar *) "git");
g_ptr_array_add (args, (gchar *) "apply");
}
else
{
g_ptr_array_add (args, "patch");
g_ptr_array_add (args, (gchar *) "patch");
}
while ((arg = va_arg (ap, const gchar *)))
......@@ -337,7 +337,7 @@ patch (GFile *dir,
}
else
{
g_ptr_array_add (args, "-i");
g_ptr_array_add (args, (gchar *) "-i");
g_ptr_array_add (args, (char *) patch_path);
}
......@@ -434,7 +434,7 @@ create_uncompress_directory (GFile *dest,
return uncompress_dest;
}
SoupSession *
static SoupSession *
get_soup_session (void)
{
return soup_session_new_with_options (SOUP_SESSION_USER_AGENT, PACKAGE_NAME,
......@@ -566,12 +566,12 @@ extract_archive (GFile *destination,
}
GFile *
fetch_archive (const gchar *url,
const gchar *sha,
const gchar *module_name,
GFile *destination,
guint strip_components,
GError **error)
gbp_flatpak_sources_fetch_archive (const gchar *url,
const gchar *sha,
const gchar *module_name,
GFile *destination,
guint strip_components,
GError **error)
{
g_autoptr(GFile) archive_file = NULL;
g_autoptr(GFile) source_dir = NULL;
......@@ -605,10 +605,10 @@ fetch_archive (const gchar *url,
}
gboolean
apply_patch (const gchar *path,
GFile *source_dir,
guint strip_components,
GError **error)
gbp_flatpak_sources_apply_patch (const gchar *path,
GFile *source_dir,
guint strip_components,
GError **error)
{
g_autoptr(GFile) patchfile = NULL;
g_autofree char *patch_path = NULL;
......
/* gbp-flatpak-sources.h
*
* Copyright © 2016 Endless Mobile, Inc.
* Copyright 2016 Endless Mobile, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -18,14 +18,13 @@
#pragma once
GFile *fetch_archive (const gchar *url,
const gchar *sha,
const gchar *module_name,
GFile *destination,
guint strip_components,
GError **error);
gboolean apply_patch (const gchar *path,
GFile *source_dir,
guint strip_components,
GError **error);
GFile *gbp_flatpak_sources_fetch_archive (const gchar *url,
const gchar *sha,
const gchar *module_name,
GFile *destination,
guint strip_components,
GError **error);
gboolean gbp_flatpak_sources_apply_patch (const gchar *path,
GFile *source_dir,
guint strip_components,
GError **error);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment