[gnome-builder] vcs: add API to query file status
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] vcs: add API to query file status
- Date: Sat, 9 Dec 2017 10:16:30 +0000 (UTC)
commit d3f1a506d545ef8fe0c585057580ec6bd54dc776
Author: Christian Hergert <chergert redhat com>
Date: Sat Dec 9 01:53:55 2017 -0800
vcs: add API to query file status
This adds a fairly simple API to query the status of files in
the project repository.
IdeVcsFileInfo has a GFile and the IdeVcsFileStatus enum value
as determined by the given backend.
We may want to just adopt git's 'pathspec' instead of the
GFile *directory_or_file parameter.
src/libide/ide-enums.c.in | 1 +
src/libide/ide.h | 1 +
src/libide/vcs/ide-vcs-file-info.c | 194 ++++++++++++++++++++++++++++++++++++
src/libide/vcs/ide-vcs-file-info.h | 53 ++++++++++
src/libide/vcs/ide-vcs.c | 105 +++++++++++++++++++
src/libide/vcs/ide-vcs.h | 22 ++++
src/libide/vcs/meson.build | 3 +
7 files changed, 379 insertions(+), 0 deletions(-)
---
diff --git a/src/libide/ide-enums.c.in b/src/libide/ide-enums.c.in
index 9f85d98..41d6471 100644
--- a/src/libide/ide-enums.c.in
+++ b/src/libide/ide-enums.c.in
@@ -20,6 +20,7 @@
#include "threading/ide-thread-pool.h"
#include "transfers/ide-transfer.h"
#include "vcs/ide-vcs-config.h"
+#include "vcs/ide-vcs-file-info.h"
/*** END file-header ***/
diff --git a/src/libide/ide.h b/src/libide/ide.h
index dcd6bde..37aa946 100644
--- a/src/libide/ide.h
+++ b/src/libide/ide.h
@@ -190,6 +190,7 @@ G_BEGIN_DECLS
#include "util/ide-ref-ptr.h"
#include "util/ide-uri.h"
#include "vcs/ide-vcs-config.h"
+#include "vcs/ide-vcs-file-info.h"
#include "vcs/ide-vcs-initializer.h"
#include "vcs/ide-vcs-uri.h"
#include "vcs/ide-vcs.h"
diff --git a/src/libide/vcs/ide-vcs-file-info.c b/src/libide/vcs/ide-vcs-file-info.c
new file mode 100644
index 0000000..4b0cc3e
--- /dev/null
+++ b/src/libide/vcs/ide-vcs-file-info.c
@@ -0,0 +1,194 @@
+/* ide-vcs-file-info.c
+ *
+ * Copyright © 2017 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-vcs-file-info"
+
+#include "ide-enums.h"
+
+#include "vcs/ide-vcs-file-info.h"
+
+typedef struct
+{
+ GFile *file;
+ IdeVcsFileStatus status;
+} IdeVcsFileInfoPrivate;
+
+enum {
+ PROP_0,
+ PROP_FILE,
+ PROP_STATUS,
+ N_PROPS
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (IdeVcsFileInfo, ide_vcs_file_info, G_TYPE_OBJECT)
+
+static GParamSpec *properties [N_PROPS];
+
+/**
+ * ide_vcs_file_info_get_file:
+ * @self: a #IdeVcsFileInfo
+ *
+ * Gets the file the #IdeVcsFileInfo describes.
+ *
+ * Returns: (transfer none): a #GFile
+ *
+ * Since: 3.28
+ */
+GFile *
+ide_vcs_file_info_get_file (IdeVcsFileInfo *self)
+{
+ IdeVcsFileInfoPrivate *priv = ide_vcs_file_info_get_instance_private (self);
+
+ g_return_val_if_fail (IDE_IS_VCS_FILE_INFO (self), NULL);
+
+ return priv->file;
+}
+
+static void
+ide_vcs_file_info_set_file (IdeVcsFileInfo *self,
+ GFile *file)
+{
+ IdeVcsFileInfoPrivate *priv = ide_vcs_file_info_get_instance_private (self);
+
+ g_return_if_fail (IDE_IS_VCS_FILE_INFO (self));
+ g_return_if_fail (G_IS_FILE (file));
+
+ g_set_object (&priv->file, file);
+}
+
+IdeVcsFileStatus
+ide_vcs_file_info_get_status (IdeVcsFileInfo *self)
+{
+ IdeVcsFileInfoPrivate *priv = ide_vcs_file_info_get_instance_private (self);
+
+ g_return_val_if_fail (IDE_IS_VCS_FILE_INFO (self), 0);
+
+ return priv->status;
+}
+
+void
+ide_vcs_file_info_set_status (IdeVcsFileInfo *self,
+ IdeVcsFileStatus status)
+{
+ IdeVcsFileInfoPrivate *priv = ide_vcs_file_info_get_instance_private (self);
+
+ g_return_if_fail (IDE_IS_VCS_FILE_INFO (self));
+
+ if (priv->status != status)
+ {
+ priv->status = status;
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_STATUS]);
+ }
+}
+
+static void
+ide_vcs_file_info_finalize (GObject *object)
+{
+ IdeVcsFileInfo *self = (IdeVcsFileInfo *)object;
+ IdeVcsFileInfoPrivate *priv = ide_vcs_file_info_get_instance_private (self);
+
+ g_clear_object (&priv->file);
+
+ G_OBJECT_CLASS (ide_vcs_file_info_parent_class)->finalize (object);
+}
+
+static void
+ide_vcs_file_info_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ IdeVcsFileInfo *self = IDE_VCS_FILE_INFO (object);
+
+ switch (prop_id)
+ {
+ case PROP_FILE:
+ g_value_set_object (value, ide_vcs_file_info_get_file (self));
+ break;
+
+ case PROP_STATUS:
+ g_value_set_enum (value, ide_vcs_file_info_get_status (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_vcs_file_info_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ IdeVcsFileInfo *self = IDE_VCS_FILE_INFO (object);
+
+ switch (prop_id)
+ {
+ case PROP_FILE:
+ ide_vcs_file_info_set_file (self, g_value_get_object (value));
+ break;
+
+ case PROP_STATUS:
+ ide_vcs_file_info_set_status (self, g_value_get_enum (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_vcs_file_info_class_init (IdeVcsFileInfoClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = ide_vcs_file_info_finalize;
+ object_class->get_property = ide_vcs_file_info_get_property;
+ object_class->set_property = ide_vcs_file_info_set_property;
+
+ properties [PROP_FILE] =
+ g_param_spec_object ("file",
+ "File",
+ "The file within the working directory",
+ G_TYPE_FILE,
+ (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+ properties [PROP_STATUS] =
+ g_param_spec_enum ("status",
+ "Status",
+ "The file status within the VCS",
+ IDE_TYPE_VCS_FILE_STATUS,
+ IDE_VCS_FILE_STATUS_UNCHANGED,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_vcs_file_info_init (IdeVcsFileInfo *self)
+{
+}
+
+IdeVcsFileInfo *
+ide_vcs_file_info_new (GFile *file)
+{
+ return g_object_new (IDE_TYPE_VCS_FILE_INFO,
+ "file", file,
+ NULL);
+}
diff --git a/src/libide/vcs/ide-vcs-file-info.h b/src/libide/vcs/ide-vcs-file-info.h
new file mode 100644
index 0000000..cabeecd
--- /dev/null
+++ b/src/libide/vcs/ide-vcs-file-info.h
@@ -0,0 +1,53 @@
+/* ide-vcs-file-info.h
+ *
+ * Copyright © 2017 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/>.
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_VCS_FILE_INFO (ide_vcs_file_info_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (IdeVcsFileInfo, ide_vcs_file_info, IDE, VCS_FILE_INFO, GObject)
+
+typedef enum
+{
+ IDE_VCS_FILE_STATUS_UNCHANGED,
+ IDE_VCS_FILE_STATUS_ADDED,
+ IDE_VCS_FILE_STATUS_CHANGED,
+ IDE_VCS_FILE_STATUS_DELETED,
+ IDE_VCS_FILE_STATUS_IGNORED,
+ IDE_VCS_FILE_STATUS_RENAMED,
+} IdeVcsFileStatus;
+
+struct _IdeVcsFileInfoClass
+{
+ GObjectClass parent_class;
+
+ /*< private >*/
+ gpointer padding[16];
+};
+
+IdeVcsFileInfo *ide_vcs_file_info_new (GFile *file);
+GFile *ide_vcs_file_info_get_file (IdeVcsFileInfo *self);
+IdeVcsFileStatus ide_vcs_file_info_get_status (IdeVcsFileInfo *self);
+void ide_vcs_file_info_set_status (IdeVcsFileInfo *self,
+ IdeVcsFileStatus status);
+
+G_END_DECLS
diff --git a/src/libide/vcs/ide-vcs.c b/src/libide/vcs/ide-vcs.c
index b04981b..225a936 100644
--- a/src/libide/vcs/ide-vcs.c
+++ b/src/libide/vcs/ide-vcs.c
@@ -49,8 +49,38 @@ ide_vcs_register_ignored (const gchar *pattern)
}
static void
+ide_vcs_real_list_status_async (IdeVcs *self,
+ GFile *directory_or_file,
+ gboolean include_descendants,
+ gint io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_task_report_new_error (self,
+ callback,
+ user_data,
+ ide_vcs_real_list_status_async,
+ G_IO_ERROR,
+ G_IO_ERROR_NOT_SUPPORTED,
+ "Not supported by %s",
+ G_OBJECT_TYPE_NAME (self));
+}
+
+static GListModel *
+ide_vcs_real_list_status_finish (IdeVcs *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+static void
ide_vcs_default_init (IdeVcsInterface *iface)
{
+ iface->list_status_async = ide_vcs_real_list_status_async;
+ iface->list_status_finish = ide_vcs_real_list_status_finish;
+
g_object_interface_install_property (iface,
g_param_spec_object ("context",
"Context",
@@ -385,3 +415,78 @@ ide_vcs_get_branch_name (IdeVcs *self)
return g_strdup ("primary");
}
+
+/**
+ * ide_vcs_list_status_async:
+ * @self: a #IdeVcs
+ * @directory_or_file: a #GFile containing a file or directory within the
+ * working tree to retrieve the status of.
+ * @include_descendants: if descendants of @directory_or_file should be
+ * included when retrieving status information.
+ * @io_priority: a priority for the IO, such as %G_PRIORITY_DEFAULT.
+ * @cancellable: (nullable): A #GCancellable or %NULL
+ * @callback: a callback for the operation
+ * @user_data: closure data for @callback
+ *
+ * Retrieves the status of the files matching the request. If
+ * @directory_or_file is a directory, then all files within that directory
+ * will be scanned for changes. If @include_descendants is %TRUE, the
+ * #IdeVcs will scan sub-directories for changes as well.
+ *
+ * The function specified by @callback should call ide_vcs_list_status_finish()
+ * to retrieve the result of this asynchronous operation.
+ *
+ * Since: 3.28
+ */
+void
+ide_vcs_list_status_async (IdeVcs *self,
+ GFile *directory_or_file,
+ gboolean include_descendants,
+ gint io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_return_if_fail (IDE_IS_VCS (self));
+ g_return_if_fail (!directory_or_file || G_IS_FILE (directory_or_file));
+ g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ if (directory_or_file == NULL)
+ directory_or_file = ide_vcs_get_working_directory (self);
+
+ IDE_VCS_GET_IFACE (self)->list_status_async (self,
+ directory_or_file,
+ include_descendants,
+ io_priority,
+ cancellable,
+ callback,
+ user_data);
+}
+
+/**
+ * ide_vcs_list_status_finish:
+ * @self: a #IdeVcs
+ * @result: a #GAsyncResult provided to the callback
+ * @error: a location for a #GError
+ *
+ * Completes an asynchronous request to ide_vcs_list_status_async().
+ *
+ * The result of this function is a #GListModel containing objects that are
+ * #IdeVcsFileInfo.
+ *
+ * Returns: (transfer full) (nullable):
+ * A #GListModel containing an #IdeVcsFileInfo for each of the files scanned
+ * by the #IdeVcs. Upon failure, %NULL is returned and @error is set.
+ *
+ * Since: 3.28
+ */
+GListModel *
+ide_vcs_list_status_finish (IdeVcs *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (IDE_IS_VCS (self), NULL);
+ g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
+
+ return IDE_VCS_GET_IFACE (self)->list_status_finish (self, result, error);
+}
diff --git a/src/libide/vcs/ide-vcs.h b/src/libide/vcs/ide-vcs.h
index 2878bfe..33aad1e 100644
--- a/src/libide/vcs/ide-vcs.h
+++ b/src/libide/vcs/ide-vcs.h
@@ -45,6 +45,16 @@ struct _IdeVcsInterface
void (*changed) (IdeVcs *self);
IdeVcsConfig *(*get_config) (IdeVcs *self);
gchar *(*get_branch_name) (IdeVcs *self);
+ void (*list_status_async) (IdeVcs *self,
+ GFile *directory_or_file,
+ gboolean include_descendants,
+ gint io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+ GListModel *(*list_status_finish) (IdeVcs *self,
+ GAsyncResult *result,
+ GError **error);
};
IDE_AVAILABLE_IN_ALL
@@ -79,5 +89,17 @@ IDE_AVAILABLE_IN_ALL
IdeVcsConfig *ide_vcs_get_config (IdeVcs *self);
IDE_AVAILABLE_IN_ALL
gchar *ide_vcs_get_branch_name (IdeVcs *self);
+IDE_AVAILABLE_IN_3_28
+void ide_vcs_list_status_async (IdeVcs *self,
+ GFile *directory_or_file,
+ gboolean include_descendants,
+ gint io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+IDE_AVAILABLE_IN_3_28
+GListModel *ide_vcs_list_status_finish (IdeVcs *self,
+ GAsyncResult *result,
+ GError **error);
G_END_DECLS
diff --git a/src/libide/vcs/meson.build b/src/libide/vcs/meson.build
index 0f01338..c715fc2 100644
--- a/src/libide/vcs/meson.build
+++ b/src/libide/vcs/meson.build
@@ -1,5 +1,6 @@
vcs_headers = [
'ide-vcs-config.h',
+ 'ide-vcs-file-info.h',
'ide-vcs-initializer.h',
'ide-vcs-uri.h',
'ide-vcs.h',
@@ -7,6 +8,7 @@ vcs_headers = [
vcs_sources = [
'ide-vcs-config.c',
+ 'ide-vcs-file-info.c',
'ide-vcs-initializer.c',
'ide-vcs-uri.c',
'ide-vcs.c',
@@ -14,6 +16,7 @@ vcs_sources = [
vcs_enums = [
'ide-vcs-config.h',
+ 'ide-vcs-file-info.h',
]
libide_public_headers += files(vcs_headers)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]