]> git.proxmox.com Git - libgit2.git/blob - src/annotated_commit.c
Merge pull request #3097 from libgit2/cmn/submodule-config-state
[libgit2.git] / src / annotated_commit.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #include "common.h"
9 #include "annotated_commit.h"
10
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"
16
17 static int annotated_commit_init(
18 git_annotated_commit **out,
19 git_repository *repo,
20 const git_oid *id,
21 const char *ref_name,
22 const char *remote_url)
23 {
24 git_annotated_commit *annotated_commit;
25 int error = 0;
26
27 assert(out && id);
28
29 *out = NULL;
30
31 annotated_commit = git__calloc(1, sizeof(git_annotated_commit));
32 GITERR_CHECK_ALLOC(annotated_commit);
33
34 if (ref_name) {
35 annotated_commit->ref_name = git__strdup(ref_name);
36 GITERR_CHECK_ALLOC(annotated_commit->ref_name);
37 }
38
39 if (remote_url) {
40 annotated_commit->remote_url = git__strdup(remote_url);
41 GITERR_CHECK_ALLOC(annotated_commit->remote_url);
42 }
43
44 git_oid_fmt(annotated_commit->id_str, id);
45 annotated_commit->id_str[GIT_OID_HEXSZ] = '\0';
46
47 if ((error = git_commit_lookup(&annotated_commit->commit, repo, id)) < 0) {
48 git_annotated_commit_free(annotated_commit);
49 return error;
50 }
51
52 *out = annotated_commit;
53 return error;
54 }
55
56 int git_annotated_commit_from_ref(
57 git_annotated_commit **out,
58 git_repository *repo,
59 const git_reference *ref)
60 {
61 git_reference *resolved;
62 int error = 0;
63
64 assert(out && repo && ref);
65
66 *out = NULL;
67
68 if ((error = git_reference_resolve(&resolved, ref)) < 0)
69 return error;
70
71 error = annotated_commit_init(out, repo, git_reference_target(resolved),
72 git_reference_name(ref), NULL);
73
74 git_reference_free(resolved);
75 return error;
76 }
77
78 int git_annotated_commit_lookup(
79 git_annotated_commit **out,
80 git_repository *repo,
81 const git_oid *id)
82 {
83 assert(out && repo && id);
84
85 return annotated_commit_init(out, repo, id, NULL, NULL);
86 }
87
88 int git_annotated_commit_from_fetchhead(
89 git_annotated_commit **out,
90 git_repository *repo,
91 const char *branch_name,
92 const char *remote_url,
93 const git_oid *id)
94 {
95 assert(repo && id && branch_name && remote_url);
96
97 return annotated_commit_init(out, repo, id, branch_name, remote_url);
98 }
99
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
127 const git_oid *git_annotated_commit_id(
128 const git_annotated_commit *annotated_commit)
129 {
130 assert(annotated_commit);
131 return git_commit_id(annotated_commit->commit);
132 }
133
134 void git_annotated_commit_free(git_annotated_commit *annotated_commit)
135 {
136 if (annotated_commit == NULL)
137 return;
138
139 if (annotated_commit->commit != NULL)
140 git_commit_free(annotated_commit->commit);
141
142 if (annotated_commit->ref_name != NULL)
143 git__free(annotated_commit->ref_name);
144
145 if (annotated_commit->remote_url != NULL)
146 git__free(annotated_commit->remote_url);
147
148 git__free(annotated_commit);
149 }