annotated_commit: provide a constructor from a revspec This extra constructor will be useful for the annotated versions of ref-modifying functions, as it allows us to create a commit with the extended sha syntax which was used to retrieve it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
diff --git a/include/git2/annotated_commit.h b/include/git2/annotated_commit.h
index e842d20..7fb896a 100644
--- a/include/git2/annotated_commit.h
+++ b/include/git2/annotated_commit.h
@@ -78,6 +78,23 @@ GIT_EXTERN(int) git_annotated_commit_lookup(
const git_oid *id);
/**
+ * Creates a `git_annotated_comit` from a revision string.
+ *
+ * See `man gitrevisions`, or
+ * http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions for
+ * information on the syntax accepted.
+ *
+ * @param out pointer to store the git_annotated_commit result in
+ * @param repo repository that contains the given commit
+ * @param revspec the extended sha syntax string to use to lookup the commit
+ * @return 0 on success or error code
+ */
+GIT_EXTERN(int) git_annotated_commit_from_revspec(
+ git_annotated_commit **out,
+ git_repository *repo,
+ const char *revspec);
+
+/**
* Gets the commit ID that the given `git_annotated_commit` refers to.
*
* @param commit the given annotated commit
diff --git a/src/annotated_commit.c b/src/annotated_commit.c
index 0a91780..3f2d2ed 100644
--- a/src/annotated_commit.c
+++ b/src/annotated_commit.c
@@ -12,6 +12,7 @@
#include "git2/refs.h"
#include "git2/repository.h"
#include "git2/annotated_commit.h"
+#include "git2/revparse.h"
static int annotated_commit_init(
git_annotated_commit **out,
@@ -96,6 +97,33 @@ int git_annotated_commit_from_fetchhead(
return annotated_commit_init(out, repo, id, branch_name, remote_url);
}
+int git_annotated_commit_from_revspec(
+ git_annotated_commit **out,
+ git_repository *repo,
+ const char *revspec)
+{
+ git_object *obj, *commit;
+ int error;
+
+ assert(out && repo && revspec);
+
+ if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
+ return error;
+
+ if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) {
+ git_object_free(obj);
+ return error;
+ }
+
+ error = annotated_commit_init(out, repo, git_object_id(commit), revspec, NULL);
+
+ git_object_free(obj);
+ git_object_free(commit);
+
+ return error;
+}
+
+
const git_oid *git_annotated_commit_id(
const git_annotated_commit *annotated_commit)
{