]>
git.proxmox.com Git - libgit2.git/blob - src/annotated_commit.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
9 #include "annotated_commit.h"
11 #include "git2/commit.h"
12 #include "git2/refs.h"
13 #include "git2/repository.h"
14 #include "git2/annotated_commit.h"
15 #include "git2/revparse.h"
17 static int annotated_commit_init(
18 git_annotated_commit
**out
,
22 const char *remote_url
)
24 git_annotated_commit
*annotated_commit
;
31 annotated_commit
= git__calloc(1, sizeof(git_annotated_commit
));
32 GITERR_CHECK_ALLOC(annotated_commit
);
35 annotated_commit
->ref_name
= git__strdup(ref_name
);
36 GITERR_CHECK_ALLOC(annotated_commit
->ref_name
);
40 annotated_commit
->remote_url
= git__strdup(remote_url
);
41 GITERR_CHECK_ALLOC(annotated_commit
->remote_url
);
44 git_oid_fmt(annotated_commit
->id_str
, id
);
45 annotated_commit
->id_str
[GIT_OID_HEXSZ
] = '\0';
47 if ((error
= git_commit_lookup(&annotated_commit
->commit
, repo
, id
)) < 0) {
48 git_annotated_commit_free(annotated_commit
);
52 *out
= annotated_commit
;
56 int git_annotated_commit_from_ref(
57 git_annotated_commit
**out
,
59 const git_reference
*ref
)
61 git_reference
*resolved
;
64 assert(out
&& repo
&& ref
);
68 if ((error
= git_reference_resolve(&resolved
, ref
)) < 0)
71 error
= annotated_commit_init(out
, repo
, git_reference_target(resolved
),
72 git_reference_name(ref
), NULL
);
74 git_reference_free(resolved
);
78 int git_annotated_commit_lookup(
79 git_annotated_commit
**out
,
83 assert(out
&& repo
&& id
);
85 return annotated_commit_init(out
, repo
, id
, NULL
, NULL
);
88 int git_annotated_commit_from_fetchhead(
89 git_annotated_commit
**out
,
91 const char *branch_name
,
92 const char *remote_url
,
95 assert(repo
&& id
&& branch_name
&& remote_url
);
97 return annotated_commit_init(out
, repo
, id
, branch_name
, remote_url
);
100 int git_annotated_commit_from_revspec(
101 git_annotated_commit
**out
,
102 git_repository
*repo
,
105 git_object
*obj
, *commit
;
108 assert(out
&& repo
&& revspec
);
110 if ((error
= git_revparse_single(&obj
, repo
, revspec
)) < 0)
113 if ((error
= git_object_peel(&commit
, obj
, GIT_OBJ_COMMIT
))) {
114 git_object_free(obj
);
118 error
= annotated_commit_init(out
, repo
, git_object_id(commit
), revspec
, NULL
);
120 git_object_free(obj
);
121 git_object_free(commit
);
127 const git_oid
*git_annotated_commit_id(
128 const git_annotated_commit
*annotated_commit
)
130 assert(annotated_commit
);
131 return git_commit_id(annotated_commit
->commit
);
134 void git_annotated_commit_free(git_annotated_commit
*annotated_commit
)
136 if (annotated_commit
== NULL
)
139 if (annotated_commit
->commit
!= NULL
)
140 git_commit_free(annotated_commit
->commit
);
142 if (annotated_commit
->ref_name
!= NULL
)
143 git__free(annotated_commit
->ref_name
);
145 if (annotated_commit
->remote_url
!= NULL
)
146 git__free(annotated_commit
->remote_url
);
148 git__free(annotated_commit
);