[libgit2-glib/wip/adwait/commit_with_signature: 1/3] Allow to sign commits




commit 7f36e18f41e0b28b35c85fe8bf11d844a0001305
Author: Alberto Fanjul <albertofanjul gmail com>
Date:   Mon Oct 10 21:47:49 2022 +0200

    Allow to sign commits

 libgit2-glib/ggit-commit.c     |   6 +++
 libgit2-glib/ggit-commit.h     |   2 +
 libgit2-glib/ggit-ref.c        |   6 ++-
 libgit2-glib/ggit-repository.c | 118 +++++++++++++++++++++++++++++++++++++++++
 libgit2-glib/ggit-repository.h |  18 +++++++
 libgit2-glib/ggit-tree.c       |   6 +++
 libgit2-glib/ggit-tree.h       |   1 +
 7 files changed, 155 insertions(+), 2 deletions(-)
---
diff --git a/libgit2-glib/ggit-commit.c b/libgit2-glib/ggit-commit.c
index d3c73ea..ee62398 100644
--- a/libgit2-glib/ggit-commit.c
+++ b/libgit2-glib/ggit-commit.c
@@ -88,6 +88,12 @@ _ggit_commit_wrap (git_commit *commit,
        return gcommit;
 }
 
+const git_commit *
+_ggit_commit_get_commit (GgitCommit *commit)
+{
+       return _ggit_native_get (commit);
+}
+
 /**
  * ggit_commit_get_message_encoding:
  * @commit: a #GgitCommit.
diff --git a/libgit2-glib/ggit-commit.h b/libgit2-glib/ggit-commit.h
index 8e792db..5649cc0 100644
--- a/libgit2-glib/ggit-commit.h
+++ b/libgit2-glib/ggit-commit.h
@@ -51,6 +51,8 @@ struct _GgitCommitClass
 GgitCommit          *_ggit_commit_wrap                (git_commit        *commit,
                                                        gboolean           owned);
 
+const git_commit    *_ggit_commit_get_commit          (GgitCommit        *commit);
+
 const gchar         *ggit_commit_get_message_encoding (GgitCommit        *commit);
 
 const gchar         *ggit_commit_get_message          (GgitCommit        *commit);
diff --git a/libgit2-glib/ggit-ref.c b/libgit2-glib/ggit-ref.c
index 01a45f4..f8ba7fb 100644
--- a/libgit2-glib/ggit-ref.c
+++ b/libgit2-glib/ggit-ref.c
@@ -263,7 +263,8 @@ ggit_ref_get_owner (GgitRef *ref)
  * ggit_ref_set_symbolic_target:
  * @ref: a #GgitRef.
  * @target: The new target for the reference.
- * @log_message: The one line long message to be appended to the reflog.
+ * @log_message: (nullable): The one line long message to be appended to the
+ * reflog.
  * @error: a #GError for error reporting, or %NULL.
  *
  * Create a new reference with the same name as the given reference but a
@@ -308,7 +309,8 @@ ggit_ref_set_symbolic_target (GgitRef       *ref,
  * ggit_ref_set_target:
  * @ref: a #GgitRef.
  * @oid: a #GgitOId.
- * @log_message: The one line long message to be appended to the reflog.
+ * @log_message: (nullable): The one line long message to be appended to the
+ * reflog.
  * @error: a #GError for error reporting, or %NULL.
  *
  * Create a new reference with the same name as the given reference but a
diff --git a/libgit2-glib/ggit-repository.c b/libgit2-glib/ggit-repository.c
index f0c151d..77013dc 100644
--- a/libgit2-glib/ggit-repository.c
+++ b/libgit2-glib/ggit-repository.c
@@ -3266,6 +3266,124 @@ ggit_repository_create_commit_from_ids (GgitRepository  *repository,
        return _ggit_oid_wrap (&id);
 }
 
+/**
+ * ggit_repository_create_commit_buffer:
+ * @repository: a #GgitRepository.
+ * @author: author signature.
+ * @committer: committer signature (and time of commit).
+ * @message_encoding: (allow-none): message encoding.
+ * @message: commit message.
+ * @tree: the tree of objects to commit.
+ * @parents: (array length=parent_count): parent commits.
+ * @parent_count: number of parent commits in @parents.
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Create a commit as with git_commit_create() but instead of writing it to the objectdb,
+ * write the contents of the object into a buffer.
+ *
+ * Returns: (transfer full) (nullable): the commit object content or %NULL in case of
+ * an error.
+ */
+gchar*
+ggit_repository_create_commit_buffer(GgitRepository *repository,
+                                     GgitSignature  *author,
+                                     GgitSignature  *committer,
+                                     const gchar    *message_encoding,
+                                     const gchar    *message,
+                                     GgitTree       *tree,
+                                     GgitCommit    **parents,
+                                     gint            parent_count,
+                                     GError        **error)
+{
+       gint ret;
+       gchar *retval;
+       git_commit **parents_native;
+       gint i;
+       git_buf buf = {0,};
+
+       g_return_val_if_fail (GGIT_IS_REPOSITORY (repository), NULL);
+       g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+       parents_native = g_new0 (git_commit *, parent_count);
+
+       for (i = 0; i < parent_count; ++i)
+       {
+               parents_native[i] = (git_commit *)_ggit_commit_get_commit (parents[i]);
+       }
+
+       ret = git_commit_create_buffer (&buf,
+                                       _ggit_native_get (repository),
+                                       _ggit_native_get (author),
+                                       _ggit_native_get (committer),
+                                       message_encoding,
+                                       message,
+                                       _ggit_tree_get_tree (tree),
+                                       parent_count,
+                                       (git_commit const **)parents_native);
+
+       g_free (parents_native);
+
+       if (ret != GIT_OK)
+       {
+               _ggit_error_set (error, ret);
+       }
+
+       retval = g_strndup (buf.ptr, buf.size);
+#if LIBGIT2_VER_MAJOR > 0 || (LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR >= 28)
+       git_buf_dispose (&buf);
+#else
+       git_buf_free (&buf);
+#endif
+
+       return retval;
+}
+
+/**
+ * ggit_repository_create_commit_with_signature:
+ * @repository: a #GgitRepository.
+ * @commit_content: the content of the unsigned commit.
+ * @signature: (nullable): the signature to add to the commit.
+ * @signature_field: (nullable): which header field should contain this
+ * signature. Leave `NULL` for the default of "gpgsig".
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Given the unsigned commit object's contents, its signature and the header field
+ * in which to store the signature, attach the signature to the commit and write it
+ * into the given repositoryCreate a new commit using the (if not NULL) signature
+ * key and type of key provided.
+ *
+ * Returns: (transfer full) (nullable): the #GgitOId of the created commit object,
+ * or %NULL in case of an error.
+ *
+ */
+GgitOId *
+ggit_repository_create_commit_with_signature (GgitRepository *repository,
+                                              const gchar    *commit_content,
+                                              const gchar    *signature,
+                                              const gchar    *signature_field,
+                                              GError        **error)
+{
+       gint ret;
+       git_oid id;
+
+       g_return_val_if_fail (GGIT_IS_REPOSITORY (repository), NULL);
+       g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+       ret = git_commit_create_with_signature (&id,
+                                               _ggit_native_get (repository),
+                                               commit_content,
+                                               signature,
+                                               signature_field);
+
+       if (ret != GIT_OK)
+       {
+               _ggit_error_set (error, ret);
+               return NULL;
+       }
+
+       return _ggit_oid_wrap (&id);
+}
+
 /**
  * ggit_repository_create_tree_builder:
  * @repository: a #GgitRepository.
diff --git a/libgit2-glib/ggit-repository.h b/libgit2-glib/ggit-repository.h
index 361afba..d312303 100644
--- a/libgit2-glib/ggit-repository.h
+++ b/libgit2-glib/ggit-repository.h
@@ -161,6 +161,24 @@ GgitOId            *ggit_repository_create_commit_from_ids (
                                                        gint                   parent_count,
                                                        GError               **error);
 
+gchar                *ggit_repository_create_commit_buffer (
+                                                       GgitRepository        *repository,
+                                                       GgitSignature         *author,
+                                                       GgitSignature         *committer,
+                                                       const gchar           *message_encoding,
+                                                       const gchar           *message,
+                                                       GgitTree              *tree,
+                                                       GgitCommit           **parents,
+                                                       gint                   parent_count,
+                                                       GError               **error);
+
+GgitOId      *ggit_repository_create_commit_with_signature (
+                                                       GgitRepository  *repository,
+                                                       const gchar     *commit_content,
+                                                       const gchar     *signature,
+                                                       const gchar     *signature_field,
+                                                       GError         **error);
+
 GgitOId            *ggit_repository_create_tag        (GgitRepository        *repository,
                                                        const gchar           *tag_name,
                                                        GgitObject            *target,
diff --git a/libgit2-glib/ggit-tree.c b/libgit2-glib/ggit-tree.c
index 29e21c5..85b20b7 100644
--- a/libgit2-glib/ggit-tree.c
+++ b/libgit2-glib/ggit-tree.c
@@ -61,6 +61,12 @@ _ggit_tree_wrap (git_tree *tree,
        return gtree;
 }
 
+const git_tree *
+_ggit_tree_get_tree (GgitTree *tree)
+{
+       return _ggit_native_get (tree);
+}
+
 /**
  * ggit_tree_get_id:
  * @tree: a #GgitTree.
diff --git a/libgit2-glib/ggit-tree.h b/libgit2-glib/ggit-tree.h
index 43c184a..8ed39ae 100644
--- a/libgit2-glib/ggit-tree.h
+++ b/libgit2-glib/ggit-tree.h
@@ -47,6 +47,7 @@ struct _GgitTreeClass
 GgitTree      *_ggit_tree_wrap          (git_tree *tree,
                                          gboolean  owned);
 
+const git_tree *_ggit_tree_get_tree     (GgitTree *tree);
 GgitOId       *ggit_tree_get_id         (GgitTree *tree);
 
 GgitTreeEntry *ggit_tree_get            (GgitTree *tree,


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