Skip to content

Commit 62dd4d7

Browse files
committed
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 parent 62d38a1 commit 62dd4d7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

include/git2/annotated_commit.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ GIT_EXTERN(int) git_annotated_commit_lookup(
7777
git_repository *repo,
7878
const git_oid *id);
7979

80+
/**
81+
* Creates a `git_annotated_comit` from a revision string.
82+
*
83+
* See `man gitrevisions`, or
84+
* http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions for
85+
* information on the syntax accepted.
86+
*
87+
* @param out pointer to store the git_annotated_commit result in
88+
* @param repo repository that contains the given commit
89+
* @param revspec the extended sha syntax string to use to lookup the commit
90+
* @return 0 on success or error code
91+
*/
92+
GIT_EXTERN(int) git_annotated_commit_from_revspec(
93+
git_annotated_commit **out,
94+
git_repository *repo,
95+
const char *revspec);
96+
8097
/**
8198
* Gets the commit ID that the given `git_annotated_commit` refers to.
8299
*

src/annotated_commit.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "git2/refs.h"
1313
#include "git2/repository.h"
1414
#include "git2/annotated_commit.h"
15+
#include "git2/revparse.h"
1516

1617
static int annotated_commit_init(
1718
git_annotated_commit **out,
@@ -96,6 +97,33 @@ int git_annotated_commit_from_fetchhead(
9697
return annotated_commit_init(out, repo, id, branch_name, remote_url);
9798
}
9899

100+
int git_annotated_commit_from_revspec(
101+
git_annotated_commit **out,
102+
git_repository *repo,
103+
const char *revspec)
104+
{
105+
git_object *obj, *commit;
106+
int error;
107+
108+
assert(out && repo && revspec);
109+
110+
if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
111+
return error;
112+
113+
if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) {
114+
git_object_free(obj);
115+
return error;
116+
}
117+
118+
error = annotated_commit_init(out, repo, git_object_id(commit), revspec, NULL);
119+
120+
git_object_free(obj);
121+
git_object_free(commit);
122+
123+
return error;
124+
}
125+
126+
99127
const git_oid *git_annotated_commit_id(
100128
const git_annotated_commit *annotated_commit)
101129
{

0 commit comments

Comments
 (0)