[libgit2-glib] Add stash api



commit af4b399203d894648f709332b585a067cc4b2198
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Oct 30 15:32:46 2012 +0100

    Add stash api

 libgit2-glib/ggit-repository.c |  141 +++++++++++++++++++++++++++++++++++++++-
 libgit2-glib/ggit-repository.h |   15 ++++
 libgit2-glib/ggit-types.c      |    6 ++
 libgit2-glib/ggit-types.h      |   37 +++++++++++
 4 files changed, 198 insertions(+), 1 deletions(-)
---
diff --git a/libgit2-glib/ggit-repository.c b/libgit2-glib/ggit-repository.c
index f8757e9..39f58c9 100644
--- a/libgit2-glib/ggit-repository.c
+++ b/libgit2-glib/ggit-repository.c
@@ -29,6 +29,7 @@
 #include <git2/reset.h>
 #include <git2/submodule.h>
 #include <git2/revparse.h>
+#include <git2/stash.h>
 
 #include "ggit-error.h"
 #include "ggit-oid.h"
@@ -37,6 +38,7 @@
 #include "ggit-utils.h"
 #include "ggit-remote.h"
 #include "ggit-submodule.h"
+#include "ggit-signature.h"
 
 #define GGIT_REPOSITORY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GGIT_TYPE_REPOSITORY, GgitRepositoryPrivate))
 
@@ -473,7 +475,7 @@ ggit_repository_lookup (GgitRepository  *repository,
  * ggit_repository_revparse:
  * @repository: a #GgitRepository.
  * @spec: the revision specification.
- * @error: a #GError.
+ * @error: a #GError for error reporting, or %NULL.
  *
  * Find an object, as specified by a revision string. See `man gitrevisions`,
  * or the documentation for `git rev-parse` for information on the syntax
@@ -1611,4 +1613,141 @@ ggit_repository_reset (GgitRepository  *repository,
 	}
 }
 
+/**
+ * ggit_repository_save_stash:
+ * @repository: a #GgitRepository.
+ * @stasher: a #GgitSignature.
+ * @message: description along with the stashed state or %NULL to be autogenerated.
+ * @flags: a #GgitStashFlags to control the stashing process.
+ * @error: a #GError for error reporting, #GGIT_ENOTFOUND if there's nothing to stash or %NULL.
+ *
+ * Saves the local modifications to a new stash.
+ * It returns the commit containing the stashed state.
+ * This commit is also the target of the direct reference refs/stash.
+ *
+ * Returns: (transfer full): a new object id of the commit containing the stashed state.
+ */
+GgitOId *
+ggit_repository_save_stash (GgitRepository  *repository,
+                            GgitSignature   *stasher,
+                            const gchar     *message,
+                            GgitStashFlags   flags,
+                            GError         **error)
+{
+	gint ret;
+	git_oid oid;
+
+	g_return_val_if_fail (GGIT_IS_REPOSITORY (repository), NULL);
+	g_return_val_if_fail (GGIT_IS_SIGNATURE (stasher), NULL);
+	g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+	ret = git_stash_save (&oid,
+	                      _ggit_native_get (repository),
+	                      _ggit_native_get (stasher),
+	                      message,
+	                      flags);
+
+	if (ret != GIT_OK)
+	{
+		_ggit_error_set (error, ret);
+		return NULL;
+	}
+
+	return _ggit_oid_new (&oid);
+}
+
+/**
+ * ggit_repository_drop_stash:
+ * @repository: a #GgitRepository.
+ * @index: the position within the stash list. 0 points to the
+ * most recent stashed state.
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Removes a single stashed state from the stash list.
+ */
+void
+ggit_repository_drop_stash (GgitRepository  *repository,
+                            gsize            index,
+                            GError         **error)
+{
+	gint ret;
+
+	g_return_if_fail (GGIT_IS_REPOSITORY (repository));
+	g_return_if_fail (error == NULL || *error == NULL);
+
+	ret = git_stash_drop (_ggit_native_get (repository),
+	                      index);
+
+	if (ret != GIT_OK)
+	{
+		_ggit_error_set (error, ret);
+	}
+}
+
+typedef struct
+{
+	gpointer user_data;
+
+	GgitStashCallback callback;
+} StashCallbackWrapperData;
+
+static gint
+stash_callback_wrapper (gsize          index,
+                        const gchar   *message,
+                        const git_oid *stash_oid,
+                        gpointer       user_data)
+{
+	StashCallbackWrapperData *wrapper_data = (StashCallbackWrapperData *)user_data;
+	GgitOId *oid;
+	gint ret;
+
+	oid = _ggit_oid_new (stash_oid);
+
+	ret = wrapper_data->callback (index, message, oid, wrapper_data->user_data);
+	ggit_oid_free (oid);
+
+	return ret;
+}
+
+/*
+ * ggit_repository_stash_foreach:
+ * @repository: a #GgitRepository.
+ * @callback: (scope call): a #GgitStashCallback.
+ * @user_data: callback user data.
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Loops over all the stashed states and issue a callback for each one.
+ * If @callback returns a non-zero value, this will stop looping.
+ *
+ * Returns: %TRUE if there was no error, %FALSE otherwise.
+ */
+gboolean
+ggit_repository_stash_foreach (GgitRepository     *repository,
+                               GgitStashCallback   callback,
+                               gpointer            user_data,
+                               GError            **error)
+{
+	StashCallbackWrapperData wrapper_data;
+	gint ret;
+
+	g_return_val_if_fail (GGIT_IS_REPOSITORY (repository), FALSE);
+	g_return_val_if_fail (callback != NULL, FALSE);
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+	wrapper_data.user_data = user_data;
+	wrapper_data.callback = callback;
+
+	ret = git_stash_foreach (_ggit_native_get (repository),
+	                         stash_callback_wrapper,
+	                         &wrapper_data);
+
+	if (ret != GIT_OK)
+	{
+		_ggit_error_set (error, ret);
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
 /* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-repository.h b/libgit2-glib/ggit-repository.h
index fe1dfa8..ff2e996 100644
--- a/libgit2-glib/ggit-repository.h
+++ b/libgit2-glib/ggit-repository.h
@@ -214,6 +214,21 @@ GgitObject         *ggit_repository_revparse          (GgitRepository        *re
                                                        const gchar           *spec,
                                                        GError               **error);
 
+GgitOId            *ggit_repository_save_stash        (GgitRepository        *repository,
+                                                       GgitSignature         *stasher,
+                                                       const gchar           *message,
+                                                       GgitStashFlags         flags,
+                                                       GError               **error);
+
+void                ggit_repository_drop_stash        (GgitRepository        *repository,
+                                                       gsize                  index,
+                                                       GError               **error);
+
+gboolean            ggit_repository_stash_foreach     (GgitRepository        *repository,
+                                                       GgitStashCallback      callback,
+                                                       gpointer               user_data,
+                                                       GError               **error);
+
 G_END_DECLS
 
 #endif /* __GGIT_REPOSITORY_H__ */
diff --git a/libgit2-glib/ggit-types.c b/libgit2-glib/ggit-types.c
index 55ead72..b6b1c0d 100644
--- a/libgit2-glib/ggit-types.c
+++ b/libgit2-glib/ggit-types.c
@@ -25,6 +25,7 @@
 #include <git2/submodule.h>
 #include <git2/types.h>
 #include <git2/config.h>
+#include <git2/stash.h>
 
 #include "ggit-types.h"
 
@@ -111,6 +112,11 @@ ASSERT_ENUM (GGIT_REF_LISTALL,  GIT_REF_LISTALL);
 ASSERT_ENUM (GGIT_RESET_SOFT,  GIT_RESET_SOFT);
 ASSERT_ENUM (GGIT_RESET_MIXED, GIT_RESET_MIXED);
 
+ASSERT_ENUM (GGIT_STASH_DEFAULT, GIT_STASH_DEFAULT);
+ASSERT_ENUM (GGIT_STASH_KEEP_INDEX, GIT_STASH_KEEP_INDEX);
+ASSERT_ENUM (GGIT_STASH_INCLUDE_UNTRACKED, GIT_STASH_INCLUDE_UNTRACKED);
+ASSERT_ENUM (GGIT_STASH_INCLUDE_IGNORED, GIT_STASH_INCLUDE_IGNORED);
+
 ASSERT_ENUM (GGIT_SORT_NONE,        GIT_SORT_NONE);
 ASSERT_ENUM (GGIT_SORT_TOPOLOGICAL, GIT_SORT_TOPOLOGICAL);
 ASSERT_ENUM (GGIT_SORT_TIME,        GIT_SORT_TIME);
diff --git a/libgit2-glib/ggit-types.h b/libgit2-glib/ggit-types.h
index 237dd27..eb0532b 100644
--- a/libgit2-glib/ggit-types.h
+++ b/libgit2-glib/ggit-types.h
@@ -494,6 +494,25 @@ typedef enum {
 	GGIT_SORT_REVERSE     = 1 << 2
 } GgitSortMode;
 
+/**
+ * GgitStashFlags:
+ * @GGIT_STASH_DEFAULT: default stash.
+ * @GGIT_STASH_KEEP_INDEX: All changes already added to the index
+ * are left intact in the working directory.
+ * @GGIT_STASH_INCLUDE_UNTRACKED: All untracked files are also stashed and then
+ * cleaned up from the working directory.
+ * @GGIT_STASH_INCLUDE_IGNORED: All ignored files are also stashed and then
+ * cleaned up from the working directory.
+ *
+ * Describes how a stash should be applied.
+ */
+typedef enum {
+	GGIT_STASH_DEFAULT           = 0,
+	GGIT_STASH_KEEP_INDEX        = 1 << 0,
+	GGIT_STASH_INCLUDE_UNTRACKED = 1 << 1,
+	GGIT_STASH_INCLUDE_IGNORED   = 1 << 2
+} GgitStashFlags;
+
 /* NOTE: keep in sync with git2/status.h */
 /**
  * GgitStatusFlags:
@@ -721,6 +740,24 @@ typedef gint (* GgitRemoteListCallback) (const gchar *name,
                                          gpointer     user_data);
 
 /**
+ * GgitStashCallback:
+ * @index: the position within the stash list. 0 points to the
+ * most recent stashed state.
+ * @message: the stash message.
+ * @stash_oid: the commit oid of the stashed state.
+ * @user_data: (closure): user-suplied data.
+ *
+ * When iterating over all the stashed states, callback that will be
+ * issued per entry. See ggit_repository_foreach_stash().
+ *
+ * Returns: 0 to go continue or a #GgitError in case there was an error.
+ */
+typedef gint (* GgitStashCallback) (gsize        index,
+                                    const gchar *message,
+                                    GgitOId     *stash_oid,
+                                    gpointer    *user_data);
+
+/**
  * GgitStatusCallback:
  * @path: the file to retrieve status for, rooted at the repository working dir.
  * @status_flags: the status value.



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