[gnome-builder/wip/chergert/git-oop: 13/13] add api to create a new repository



commit 7bf623a19a3b10fab8cee0f58b5a3d23ace83731
Author: Christian Hergert <chergert redhat com>
Date:   Wed Mar 20 18:29:00 2019 -0700

    add api to create a new repository

 src/plugins/git/gbp-git-client.c    | 66 ++++++++++++++++++++++++++++++++++
 src/plugins/git/gbp-git-client.h    |  9 +++++
 src/plugins/git/gbp-git.c           | 70 +++++++++++++++++++++++++++++++++++++
 src/plugins/git/gbp-git.h           |  9 +++++
 src/plugins/git/gnome-builder-git.c | 63 +++++++++++++++++++++++++++++++++
 5 files changed, 217 insertions(+)
---
diff --git a/src/plugins/git/gbp-git-client.c b/src/plugins/git/gbp-git-client.c
index 96afe5538..0ae5857f4 100644
--- a/src/plugins/git/gbp-git-client.c
+++ b/src/plugins/git/gbp-git-client.c
@@ -956,3 +956,69 @@ gbp_git_client_read_config (GbpGitClient  *self,
 
   return g_steal_pointer (&reply);
 }
+
+static void
+gbp_git_client_create_repo_cb (GObject      *object,
+                               GAsyncResult *result,
+                               gpointer      user_data)
+{
+  GbpGitClient *self = (GbpGitClient *)object;
+  g_autoptr(IdeTask) task = user_data;
+  g_autoptr(GVariant) reply = NULL;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_GIT_CLIENT (self));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_TASK (task));
+
+  if (!gbp_git_client_call_finish (self, result, &reply, &error))
+    ide_task_return_error (task, g_steal_pointer (&error));
+  else
+    ide_task_return_boolean (task, TRUE);
+}
+
+void
+gbp_git_client_create_repo_async (GbpGitClient        *self,
+                                  GFile               *in_directory,
+                                  gboolean             bare,
+                                  GCancellable        *cancellable,
+                                  GAsyncReadyCallback  callback,
+                                  gpointer             user_data)
+{
+  g_autoptr(IdeTask) task = NULL;
+  g_autoptr(GVariant) command = NULL;
+  g_autofree gchar *uri = NULL;
+
+  g_return_if_fail (GBP_IS_GIT_CLIENT (self));
+  g_return_if_fail (G_IS_FILE (in_directory));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, gbp_git_client_create_repo_async);
+
+  uri = g_file_get_uri (in_directory);
+
+  command = JSONRPC_MESSAGE_NEW (
+    "location", JSONRPC_MESSAGE_PUT_STRING (uri),
+    "bare", JSONRPC_MESSAGE_PUT_BOOLEAN (bare)
+  );
+
+  gbp_git_client_call_async (self,
+                             "git/createRepo",
+                             command,
+                             cancellable,
+                             gbp_git_client_create_repo_cb,
+                             g_steal_pointer (&task));
+}
+
+gboolean
+gbp_git_client_create_repo_finish (GbpGitClient  *self,
+                                   GAsyncResult  *result,
+                                   GError       **error)
+{
+  g_return_val_if_fail (GBP_IS_GIT_CLIENT (self), FALSE);
+  g_return_val_if_fail (IDE_IS_TASK (result), FALSE);
+
+  return ide_task_propagate_boolean (IDE_TASK (result), error);
+}
diff --git a/src/plugins/git/gbp-git-client.h b/src/plugins/git/gbp-git-client.h
index 48b151770..493a6ca70 100644
--- a/src/plugins/git/gbp-git-client.h
+++ b/src/plugins/git/gbp-git-client.h
@@ -80,6 +80,15 @@ gboolean      gbp_git_client_switch_branch_finish     (GbpGitClient           *s
                                                        GAsyncResult           *result,
                                                        gchar                 **switch_to_directory,
                                                        GError                **error);
+void          gbp_git_client_create_repo_async        (GbpGitClient           *self,
+                                                       GFile                  *in_directory,
+                                                       gboolean                bare,
+                                                       GCancellable           *cancellable,
+                                                       GAsyncReadyCallback     callback,
+                                                       gpointer                user_data);
+gboolean      gbp_git_client_create_repo_finish       (GbpGitClient           *self,
+                                                       GAsyncResult           *result,
+                                                       GError                **error);
 void          gbp_git_client_clone_url_async          (GbpGitClient           *self,
                                                        const gchar            *url,
                                                        GFile                  *destination,
diff --git a/src/plugins/git/gbp-git.c b/src/plugins/git/gbp-git.c
index 82b56068e..69b9ba89f 100644
--- a/src/plugins/git/gbp-git.c
+++ b/src/plugins/git/gbp-git.c
@@ -704,3 +704,73 @@ gbp_git_read_config_finish (GbpGit        *self,
 
   return g_task_propagate_pointer (G_TASK (result), error);
 }
+
+typedef struct
+{
+  GFile *in_directory;
+  guint  bare : 1;
+} CreateRepo;
+
+static void
+create_repo_free (CreateRepo *state)
+{
+  g_clear_object (&state->in_directory);
+  g_slice_free (CreateRepo, state);
+}
+
+static void
+gbp_git_create_repo_worker (GTask        *task,
+                            gpointer      source_object,
+                            gpointer      task_data,
+                            GCancellable *cancellable)
+{
+  CreateRepo *state = task_data;
+  g_autoptr(GgitRepository) repository = NULL;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (G_IS_TASK (task));
+  g_assert (GBP_IS_GIT (source_object));
+  g_assert (state != NULL);
+  g_assert (G_IS_FILE (state->in_directory));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  if (!(repository = ggit_repository_init_repository (state->in_directory, state->bare, &error)))
+    g_task_return_error (task, g_steal_pointer (&error));
+  else
+    g_task_return_boolean (task, TRUE);
+}
+
+void
+gbp_git_create_repo_async (GbpGit              *self,
+                           GFile               *in_directory,
+                           gboolean             bare,
+                           GCancellable        *cancellable,
+                           GAsyncReadyCallback  callback,
+                           gpointer             user_data)
+{
+  g_autoptr(GTask) task = NULL;
+  CreateRepo *state;
+
+  g_assert (GBP_IS_GIT (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  state = g_slice_new0 (CreateRepo);
+  state->in_directory = g_file_dup (in_directory);
+  state->bare = !!bare;
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, gbp_git_create_repo_async);
+  g_task_set_task_data (task, state, (GDestroyNotify)create_repo_free);
+  g_task_run_in_thread (task, gbp_git_create_repo_worker);
+}
+
+gboolean
+gbp_git_create_repo_finish (GbpGit        *self,
+                            GAsyncResult  *result,
+                            GError       **error)
+{
+  g_assert (GBP_IS_GIT (self));
+  g_assert (G_IS_TASK (result));
+
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
diff --git a/src/plugins/git/gbp-git.h b/src/plugins/git/gbp-git.h
index 63cd0fc70..1d9bad1c9 100644
--- a/src/plugins/git/gbp-git.h
+++ b/src/plugins/git/gbp-git.h
@@ -99,6 +99,15 @@ void       gbp_git_clone_url_async          (GbpGit                      *self,
 gboolean   gbp_git_clone_url_finish         (GbpGit                      *self,
                                              GAsyncResult                *result,
                                              GError                     **error);
+void       gbp_git_create_repo_async        (GbpGit                      *self,
+                                             GFile                       *in_directory,
+                                             gboolean                     bare,
+                                             GCancellable                *cancellable,
+                                             GAsyncReadyCallback          callback,
+                                             gpointer                     user_data);
+gboolean   gbp_git_create_repo_finish       (GbpGit                      *self,
+                                             GAsyncResult                *result,
+                                             GError                     **error);
 void       gbp_git_update_submodules_async  (GbpGit                      *self,
                                              GgitSubmoduleUpdateOptions  *options,
                                              GCancellable                *cancellable,
diff --git a/src/plugins/git/gnome-builder-git.c b/src/plugins/git/gnome-builder-git.c
index bca512761..8c929a922 100644
--- a/src/plugins/git/gnome-builder-git.c
+++ b/src/plugins/git/gnome-builder-git.c
@@ -844,6 +844,68 @@ handle_read_config (JsonrpcServer *server,
                              client_op_ref (op));
 }
 
+/* Handle Create Repository {{{1 */
+
+static void
+handle_create_repo_cb (GObject      *object,
+                       GAsyncResult *result,
+                       gpointer      user_data)
+{
+  GbpGit *git = (GbpGit *)object;
+  g_autoptr(ClientOp) op = user_data;
+  g_autoptr(GVariant) value = NULL;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (GBP_IS_GIT (git));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (op != NULL);
+
+  if (!gbp_git_create_repo_finish (git, result, &error))
+    client_op_error (op, error);
+  else
+    client_op_reply (op, g_variant_new_boolean (TRUE));
+}
+
+static void
+handle_create_repo (JsonrpcServer *server,
+                    JsonrpcClient *client,
+                    const gchar   *method,
+                    GVariant      *id,
+                    GVariant      *params,
+                    GbpGit        *git)
+{
+  g_autoptr(ClientOp) op = NULL;
+  g_autoptr(GFile) location = NULL;
+  const gchar *uri;
+  gboolean bare;
+
+  g_assert (JSONRPC_IS_SERVER (server));
+  g_assert (JSONRPC_IS_CLIENT (client));
+  g_assert (g_str_equal (method, "git/createRepo"));
+  g_assert (id != NULL);
+  g_assert (GBP_IS_GIT (git));
+
+  op = client_op_new (client, id);
+
+  if (!JSONRPC_MESSAGE_PARSE (params, "location", JSONRPC_MESSAGE_GET_STRING (&uri)))
+    {
+      client_op_bad_params (op);
+      return;
+    }
+
+  if (!JSONRPC_MESSAGE_PARSE (params, "bare", JSONRPC_MESSAGE_GET_BOOLEAN (&bare)))
+    bare = FALSE;
+
+  location = g_file_new_for_uri (uri);
+
+  gbp_git_create_repo_async (git,
+                             location,
+                             bare,
+                             op->cancellable,
+                             (GAsyncReadyCallback)handle_create_repo_cb,
+                             client_op_ref (op));
+}
+
 /* Main Loop and Setup {{{1 */
 
 gint
@@ -886,6 +948,7 @@ main (gint argc,
 
   ADD_HANDLER ("initialize", handle_initialize);
   ADD_HANDLER ("git/cloneUrl", handle_clone_url);
+  ADD_HANDLER ("git/createRepo", handle_create_repo);
   ADD_HANDLER ("git/readConfig", handle_read_config);
   ADD_HANDLER ("git/isIgnored", handle_is_ignored);
   ADD_HANDLER ("git/listRefsByKind", handle_list_refs_by_kind);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]