[gnome-builder] git: add support for listing branches



commit 0d9a9bef5db650afd09a391188e8223d06f197e4
Author: Christian Hergert <chergert redhat com>
Date:   Sun Jan 27 15:14:23 2019 -0800

    git: add support for listing branches

 src/plugins/git/gbp-git-branch.c |  82 ++++++++++++++++++++++++++++++++
 src/plugins/git/gbp-git-branch.h |  33 +++++++++++++
 src/plugins/git/gbp-git-vcs.c    | 100 +++++++++++++++++++++++++++++++++++++++
 src/plugins/git/meson.build      |   1 +
 4 files changed, 216 insertions(+)
---
diff --git a/src/plugins/git/gbp-git-branch.c b/src/plugins/git/gbp-git-branch.c
new file mode 100644
index 000000000..a937890bc
--- /dev/null
+++ b/src/plugins/git/gbp-git-branch.c
@@ -0,0 +1,82 @@
+/* gbp-git-branch.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-git-branch"
+
+#include "config.h"
+
+#include <libide-vcs.h>
+
+#include "gbp-git-branch.h"
+
+struct _GbpGitBranch
+{
+  GObject parent_instance;
+  gchar *name;
+};
+
+static gchar *
+gbp_git_branch_get_name (IdeVcsBranch *branch)
+{
+  return g_strdup (GBP_GIT_BRANCH (branch)->name);
+}
+
+static void
+vcs_branch_iface_init (IdeVcsBranchInterface *iface)
+{
+  iface->get_name = gbp_git_branch_get_name;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpGitBranch, gbp_git_branch, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_VCS_BRANCH, vcs_branch_iface_init))
+
+static void
+gbp_git_branch_finalize (GObject *object)
+{
+  GbpGitBranch *self = (GbpGitBranch *)object;
+
+  g_clear_pointer (&self->name, g_free);
+
+  G_OBJECT_CLASS (gbp_git_branch_parent_class)->finalize (object);
+}
+
+static void
+gbp_git_branch_class_init (GbpGitBranchClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gbp_git_branch_finalize;
+}
+
+static void
+gbp_git_branch_init (GbpGitBranch *self)
+{
+}
+
+GbpGitBranch *
+gbp_git_branch_new (const gchar *name)
+{
+  GbpGitBranch *self;
+
+  self = g_object_new (GBP_TYPE_GIT_BRANCH, NULL);
+  self->name = g_strdup (name);
+
+  return g_steal_pointer (&self);
+}
diff --git a/src/plugins/git/gbp-git-branch.h b/src/plugins/git/gbp-git-branch.h
new file mode 100644
index 000000000..a5ac9edb1
--- /dev/null
+++ b/src/plugins/git/gbp-git-branch.h
@@ -0,0 +1,33 @@
+/* gbp-git-branch.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_GIT_BRANCH (gbp_git_branch_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpGitBranch, gbp_git_branch, GBP, GIT_BRANCH, GObject)
+
+GbpGitBranch *gbp_git_branch_new (const gchar *name);
+
+G_END_DECLS
diff --git a/src/plugins/git/gbp-git-vcs.c b/src/plugins/git/gbp-git-vcs.c
index b792eafda..8dd963c93 100644
--- a/src/plugins/git/gbp-git-vcs.c
+++ b/src/plugins/git/gbp-git-vcs.c
@@ -22,6 +22,7 @@
 
 #include "config.h"
 
+#include "gbp-git-branch.h"
 #include "gbp-git-vcs.h"
 #include "gbp-git-vcs-config.h"
 
@@ -445,6 +446,103 @@ gbp_git_vcs_list_status_finish (IdeVcs        *vcs,
   return ide_task_propagate_pointer (IDE_TASK (result), error);
 }
 
+static void
+gbp_git_vcs_list_branches_worker (IdeTask      *task,
+                                  gpointer      source_object,
+                                  gpointer      task_data,
+                                  GCancellable *cancellable)
+{
+  GbpGitVcs *self = source_object;
+  g_autoptr(GPtrArray) branches = NULL;
+
+  g_assert (IDE_IS_TASK (task));
+  g_assert (GBP_IS_GIT_VCS (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  branches = g_ptr_array_new_with_free_func (g_object_unref);
+
+  ide_object_lock (IDE_OBJECT (self));
+
+  if (self->repository != NULL)
+    {
+      g_autoptr(GgitBranchEnumerator) enumerator = NULL;
+      g_autoptr(GError) error = NULL;
+
+      if (!(enumerator = ggit_repository_enumerate_branches (self->repository,
+                                                             GGIT_BRANCH_LOCAL,
+                                                             &error)))
+        {
+          ide_task_return_error (task, g_steal_pointer (&error));
+          goto unlock;
+        }
+
+      while (ggit_branch_enumerator_next (enumerator))
+        {
+          g_autoptr(GgitRef) ref = ggit_branch_enumerator_get (enumerator);
+          const gchar *name = ggit_ref_get_name (ref);
+
+          g_ptr_array_add (branches, gbp_git_branch_new (name));
+        }
+
+      ide_task_return_pointer (task,
+                               g_steal_pointer (&branches),
+                               (GDestroyNotify)g_ptr_array_unref);
+    }
+  else
+    {
+      ide_task_return_new_error (task,
+                                 G_IO_ERROR,
+                                 G_IO_ERROR_FAILED,
+                                 "No repository to access");
+    }
+
+unlock:
+  ide_object_unlock (IDE_OBJECT (self));
+}
+
+static void
+gbp_git_vcs_list_branches_async (IdeVcs              *vcs,
+                                 GCancellable        *cancellable,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data)
+{
+  GbpGitVcs *self = (GbpGitVcs *)vcs;
+  g_autoptr(IdeTask) task = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_GIT_VCS (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, gbp_git_vcs_list_status_async);
+  ide_task_set_return_on_cancel (task, TRUE);
+
+  ide_object_lock (IDE_OBJECT (self));
+  if (self->repository == NULL)
+    ide_task_return_new_error (task,
+                               G_IO_ERROR,
+                               G_IO_ERROR_FAILED,
+                               "No repository loaded");
+  else
+    ide_task_run_in_thread (task, gbp_git_vcs_list_branches_worker);
+  ide_object_unlock (IDE_OBJECT (self));
+
+  IDE_EXIT;
+}
+
+static GPtrArray *
+gbp_git_vcs_list_branches_finish (IdeVcs        *vcs,
+                                  GAsyncResult  *result,
+                                  GError       **error)
+{
+  g_return_val_if_fail (GBP_IS_GIT_VCS (vcs), NULL);
+  g_return_val_if_fail (IDE_IS_TASK (result), NULL);
+
+  return ide_task_propagate_pointer (IDE_TASK (result), error);
+}
+
 static void
 vcs_iface_init (IdeVcsInterface *iface)
 {
@@ -454,6 +552,8 @@ vcs_iface_init (IdeVcsInterface *iface)
   iface->is_ignored = gbp_git_vcs_is_ignored;
   iface->list_status_async = gbp_git_vcs_list_status_async;
   iface->list_status_finish = gbp_git_vcs_list_status_finish;
+  iface->list_branches_async = gbp_git_vcs_list_branches_async;
+  iface->list_branches_finish = gbp_git_vcs_list_branches_finish;
 }
 
 static void
diff --git a/src/plugins/git/meson.build b/src/plugins/git/meson.build
index 752fef709..1cd512e99 100644
--- a/src/plugins/git/meson.build
+++ b/src/plugins/git/meson.build
@@ -2,6 +2,7 @@ if get_option('plugin_git')
 
 plugins_sources += files([
   'git-plugin.c',
+  'gbp-git-branch.c',
   'gbp-git-buffer-addin.c',
   'gbp-git-buffer-change-monitor.c',
   'gbp-git-dependency-updater.c',


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