source src/annotated_commit.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
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 "annotated_commit.h" | ||
9 | - | |||
10 | - | #include "refs.h" | ||
11 | - | #include "cache.h" | ||
12 | - | |||
13 | - | #include "git2/commit.h" | ||
14 | - | #include "git2/refs.h" | ||
15 | - | #include "git2/repository.h" | ||
16 | - | #include "git2/annotated_commit.h" | ||
17 | - | #include "git2/revparse.h" | ||
18 | - | #include "git2/tree.h" | ||
19 | - | #include "git2/index.h" | ||
20 | - | |||
21 | 730 | 2 | static int annotated_commit_init( | |
22 | - | git_annotated_commit **out, | ||
23 | - | git_commit *commit, | ||
24 | - | const char *description) | ||
25 | - | { | ||
26 | - | git_annotated_commit *annotated_commit; | ||
27 | 730 | 2 | int error = 0; | |
28 | - | |||
29 | 730 | 2-4 | assert(out && commit); | |
30 | - | |||
31 | 730 | 5 | *out = NULL; | |
32 | - | |||
33 | 730 | 5 | annotated_commit = git__calloc(1, sizeof(git_annotated_commit)); | |
34 | 730 | 6,7 | GIT_ERROR_CHECK_ALLOC(annotated_commit); | |
35 | - | |||
36 | 730 | 8 | annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL; | |
37 | - | |||
38 | 730 | 8,9 | if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0) | |
39 | ##### | 10 | goto done; | |
40 | - | |||
41 | 730 | 11,12 | git_oid_fmt(annotated_commit->id_str, git_commit_id(commit)); | |
42 | 730 | 13 | annotated_commit->id_str[GIT_OID_HEXSZ] = '\0'; | |
43 | - | |||
44 | 730 | 13 | if (!description) | |
45 | 402 | 14 | description = annotated_commit->id_str; | |
46 | - | |||
47 | 730 | 15 | annotated_commit->description = git__strdup(description); | |
48 | 730 | 16,17 | GIT_ERROR_CHECK_ALLOC(annotated_commit->description); | |
49 | - | |||
50 | - | done: | ||
51 | 730 | 18 | if (!error) | |
52 | 730 | 19 | *out = annotated_commit; | |
53 | - | |||
54 | 730 | 20 | return error; | |
55 | - | } | ||
56 | - | |||
57 | 679 | 2 | static int annotated_commit_init_from_id( | |
58 | - | git_annotated_commit **out, | ||
59 | - | git_repository *repo, | ||
60 | - | const git_oid *id, | ||
61 | - | const char *description) | ||
62 | - | { | ||
63 | 679 | 2 | git_commit *commit = NULL; | |
64 | 679 | 2 | int error = 0; | |
65 | - | |||
66 | 679 | 2-5 | assert(out && repo && id); | |
67 | - | |||
68 | 679 | 6 | *out = NULL; | |
69 | - | |||
70 | 679 | 6,7 | if ((error = git_commit_lookup(&commit, repo, id)) < 0) | |
71 | ##### | 8 | goto done; | |
72 | - | |||
73 | 679 | 9 | error = annotated_commit_init(out, commit, description); | |
74 | - | |||
75 | - | done: | ||
76 | 679 | 10 | git_commit_free(commit); | |
77 | 679 | 11 | return error; | |
78 | - | } | ||
79 | - | |||
80 | 358 | 2 | int git_annotated_commit_lookup( | |
81 | - | git_annotated_commit **out, | ||
82 | - | git_repository *repo, | ||
83 | - | const git_oid *id) | ||
84 | - | { | ||
85 | 358 | 2 | return annotated_commit_init_from_id(out, repo, id, NULL); | |
86 | - | } | ||
87 | - | |||
88 | 44 | 2 | int git_annotated_commit_from_commit( | |
89 | - | git_annotated_commit **out, | ||
90 | - | git_commit *commit) | ||
91 | - | { | ||
92 | 44 | 2 | return annotated_commit_init(out, commit, NULL); | |
93 | - | } | ||
94 | - | |||
95 | 7 | 2 | int git_annotated_commit_from_revspec( | |
96 | - | git_annotated_commit **out, | ||
97 | - | git_repository *repo, | ||
98 | - | const char *revspec) | ||
99 | - | { | ||
100 | - | git_object *obj, *commit; | ||
101 | - | int error; | ||
102 | - | |||
103 | 7 | 2-5 | assert(out && repo && revspec); | |
104 | - | |||
105 | 7 | 6,7 | if ((error = git_revparse_single(&obj, repo, revspec)) < 0) | |
106 | ##### | 8 | return error; | |
107 | - | |||
108 | 7 | 9,10 | if ((error = git_object_peel(&commit, obj, GIT_OBJECT_COMMIT))) { | |
109 | ##### | 11 | git_object_free(obj); | |
110 | ##### | 12 | return error; | |
111 | - | } | ||
112 | - | |||
113 | 7 | 13 | error = annotated_commit_init(out, (git_commit *)commit, revspec); | |
114 | - | |||
115 | 7 | 14 | git_object_free(obj); | |
116 | 7 | 15 | git_object_free(commit); | |
117 | - | |||
118 | 7 | 16 | return error; | |
119 | - | } | ||
120 | - | |||
121 | 307 | 2 | int git_annotated_commit_from_ref( | |
122 | - | git_annotated_commit **out, | ||
123 | - | git_repository *repo, | ||
124 | - | const git_reference *ref) | ||
125 | - | { | ||
126 | - | git_object *peeled; | ||
127 | 307 | 2 | int error = 0; | |
128 | - | |||
129 | 307 | 2-5 | assert(out && repo && ref); | |
130 | - | |||
131 | 307 | 6 | *out = NULL; | |
132 | - | |||
133 | 307 | 6,7 | if ((error = git_reference_peel(&peeled, ref, GIT_OBJECT_COMMIT)) < 0) | |
134 | ##### | 8 | return error; | |
135 | - | |||
136 | 307 | 9-11 | error = annotated_commit_init_from_id(out, | |
137 | - | repo, | ||
138 | - | git_object_id(peeled), | ||
139 | - | git_reference_name(ref)); | ||
140 | - | |||
141 | 307 | 12 | if (!error) { | |
142 | 307 | 13,14 | (*out)->ref_name = git__strdup(git_reference_name(ref)); | |
143 | 307 | 15,16 | GIT_ERROR_CHECK_ALLOC((*out)->ref_name); | |
144 | - | } | ||
145 | - | |||
146 | 307 | 17 | git_object_free(peeled); | |
147 | 307 | 18 | return error; | |
148 | - | } | ||
149 | - | |||
150 | 122 | 2 | int git_annotated_commit_from_head( | |
151 | - | git_annotated_commit **out, | ||
152 | - | git_repository *repo) | ||
153 | - | { | ||
154 | - | git_reference *head; | ||
155 | - | int error; | ||
156 | - | |||
157 | 122 | 2-4 | assert(out && repo); | |
158 | - | |||
159 | 122 | 5 | *out = NULL; | |
160 | - | |||
161 | 122 | 5,6 | if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0) | |
162 | ##### | 7 | return -1; | |
163 | - | |||
164 | 122 | 8 | error = git_annotated_commit_from_ref(out, repo, head); | |
165 | - | |||
166 | 122 | 9 | git_reference_free(head); | |
167 | 122 | 10 | return error; | |
168 | - | } | ||
169 | - | |||
170 | 14 | 2 | int git_annotated_commit_from_fetchhead( | |
171 | - | git_annotated_commit **out, | ||
172 | - | git_repository *repo, | ||
173 | - | const char *branch_name, | ||
174 | - | const char *remote_url, | ||
175 | - | const git_oid *id) | ||
176 | - | { | ||
177 | 14 | 2-6 | assert(repo && id && branch_name && remote_url); | |
178 | - | |||
179 | 14 | 7,8 | if (annotated_commit_init_from_id(out, repo, id, branch_name) < 0) | |
180 | ##### | 9 | return -1; | |
181 | - | |||
182 | 14 | 10 | (*out)->ref_name = git__strdup(branch_name); | |
183 | 14 | 11,12 | GIT_ERROR_CHECK_ALLOC((*out)->ref_name); | |
184 | - | |||
185 | 14 | 13 | (*out)->remote_url = git__strdup(remote_url); | |
186 | 14 | 14,15 | GIT_ERROR_CHECK_ALLOC((*out)->remote_url); | |
187 | - | |||
188 | 14 | 16 | return 0; | |
189 | - | } | ||
190 | - | |||
191 | - | |||
192 | 514 | 2 | const git_oid *git_annotated_commit_id( | |
193 | - | const git_annotated_commit *annotated_commit) | ||
194 | - | { | ||
195 | 514 | 2,3 | assert(annotated_commit); | |
196 | 514 | 4 | return git_commit_id(annotated_commit->commit); | |
197 | - | } | ||
198 | - | |||
199 | ##### | 2 | const char *git_annotated_commit_ref( | |
200 | - | const git_annotated_commit *annotated_commit) | ||
201 | - | { | ||
202 | ##### | 2,3 | assert(annotated_commit); | |
203 | ##### | 4 | return annotated_commit->ref_name; | |
204 | - | } | ||
205 | - | |||
206 | 1303 | 2 | void git_annotated_commit_free(git_annotated_commit *annotated_commit) | |
207 | - | { | ||
208 | 1303 | 2 | if (annotated_commit == NULL) | |
209 | 1303 | 3,16 | return; | |
210 | - | |||
211 | 756 | 4 | switch (annotated_commit->type) { | |
212 | - | case GIT_ANNOTATED_COMMIT_REAL: | ||
213 | 730 | 5 | git_commit_free(annotated_commit->commit); | |
214 | 730 | 6 | git_tree_free(annotated_commit->tree); | |
215 | 730 | 7 | git__free((char *)annotated_commit->description); | |
216 | 730 | 8 | git__free((char *)annotated_commit->ref_name); | |
217 | 730 | 9 | git__free((char *)annotated_commit->remote_url); | |
218 | 730 | 14 | break; | |
219 | - | case GIT_ANNOTATED_COMMIT_VIRTUAL: | ||
220 | 26 | 10 | git_index_free(annotated_commit->index); | |
221 | 26 | 11 | git_array_clear(annotated_commit->parents); | |
222 | 26 | 12 | break; | |
223 | - | default: | ||
224 | - | 13 | abort(); | |
225 | - | } | ||
226 | - | |||
227 | 756 | 15 | git__free(annotated_commit); | |
228 | - | } |