Blame


1 5261c201 2018-04-01 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 5261c201 2018-04-01 stsp *
4 5261c201 2018-04-01 stsp * Permission to use, copy, modify, and distribute this software for any
5 5261c201 2018-04-01 stsp * purpose with or without fee is hereby granted, provided that the above
6 5261c201 2018-04-01 stsp * copyright notice and this permission notice appear in all copies.
7 5261c201 2018-04-01 stsp *
8 5261c201 2018-04-01 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5261c201 2018-04-01 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5261c201 2018-04-01 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5261c201 2018-04-01 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5261c201 2018-04-01 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5261c201 2018-04-01 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5261c201 2018-04-01 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5261c201 2018-04-01 stsp */
16 5261c201 2018-04-01 stsp
17 5261c201 2018-04-01 stsp #include <sys/types.h>
18 5261c201 2018-04-01 stsp #include <sys/queue.h>
19 5261c201 2018-04-01 stsp
20 5261c201 2018-04-01 stsp #include <sha1.h>
21 5261c201 2018-04-01 stsp #include <stdio.h>
22 5261c201 2018-04-01 stsp #include <stdlib.h>
23 5261c201 2018-04-01 stsp #include <string.h>
24 5261c201 2018-04-01 stsp #include <util.h>
25 5261c201 2018-04-01 stsp #include <zlib.h>
26 788c352e 2018-06-16 stsp #include <time.h>
27 5261c201 2018-04-01 stsp
28 5261c201 2018-04-01 stsp #include "got_error.h"
29 5261c201 2018-04-01 stsp #include "got_object.h"
30 5261c201 2018-04-01 stsp #include "got_repository.h"
31 5261c201 2018-04-01 stsp #include "got_reference.h"
32 5261c201 2018-04-01 stsp
33 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
34 5261c201 2018-04-01 stsp #include "got_lib_path.h"
35 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
36 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
37 5261c201 2018-04-01 stsp #include "got_lib_object.h"
38 5261c201 2018-04-01 stsp
39 fb79db15 2019-02-01 stsp #ifndef nitems
40 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 fb79db15 2019-02-01 stsp #endif
42 fb79db15 2019-02-01 stsp
43 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
44 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
45 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
46 d1f2edc9 2018-06-13 stsp
47 5261c201 2018-04-01 stsp /* A symbolic reference. */
48 5261c201 2018-04-01 stsp struct got_symref {
49 5261c201 2018-04-01 stsp char *name;
50 5261c201 2018-04-01 stsp char *ref;
51 5261c201 2018-04-01 stsp };
52 5261c201 2018-04-01 stsp
53 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
54 5261c201 2018-04-01 stsp struct got_ref {
55 5261c201 2018-04-01 stsp char *name;
56 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
57 5261c201 2018-04-01 stsp };
58 5261c201 2018-04-01 stsp
59 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
60 5261c201 2018-04-01 stsp struct got_reference {
61 5261c201 2018-04-01 stsp unsigned int flags;
62 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
63 5261c201 2018-04-01 stsp
64 5261c201 2018-04-01 stsp union {
65 5261c201 2018-04-01 stsp struct got_ref ref;
66 5261c201 2018-04-01 stsp struct got_symref symref;
67 5261c201 2018-04-01 stsp } ref;
68 5261c201 2018-04-01 stsp };
69 5261c201 2018-04-01 stsp
70 5261c201 2018-04-01 stsp static const struct got_error *
71 5261c201 2018-04-01 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
72 5261c201 2018-04-01 stsp {
73 5261c201 2018-04-01 stsp struct got_symref *symref;
74 5261c201 2018-04-01 stsp char *symref_name;
75 5261c201 2018-04-01 stsp char *symref_ref;
76 5261c201 2018-04-01 stsp
77 5261c201 2018-04-01 stsp if (line[0] == '\0')
78 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
79 5261c201 2018-04-01 stsp
80 5261c201 2018-04-01 stsp symref_name = strdup(name);
81 5261c201 2018-04-01 stsp if (symref_name == NULL)
82 5261c201 2018-04-01 stsp return got_error_from_errno();
83 5261c201 2018-04-01 stsp symref_ref = strdup(line);
84 5261c201 2018-04-01 stsp if (symref_ref == NULL) {
85 5261c201 2018-04-01 stsp const struct got_error *err = got_error_from_errno();
86 5261c201 2018-04-01 stsp free(symref_name);
87 5261c201 2018-04-01 stsp return err;
88 5261c201 2018-04-01 stsp }
89 5261c201 2018-04-01 stsp
90 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
91 5261c201 2018-04-01 stsp if (*ref == NULL)
92 5261c201 2018-04-01 stsp return got_error_from_errno();
93 5261c201 2018-04-01 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
94 5261c201 2018-04-01 stsp symref = &((*ref)->ref.symref);
95 5261c201 2018-04-01 stsp symref->name = symref_name;
96 5261c201 2018-04-01 stsp symref->ref = symref_ref;
97 5261c201 2018-04-01 stsp return NULL;
98 5261c201 2018-04-01 stsp }
99 5261c201 2018-04-01 stsp
100 5261c201 2018-04-01 stsp static const struct got_error *
101 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
102 5261c201 2018-04-01 stsp {
103 5261c201 2018-04-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
104 5261c201 2018-04-01 stsp char *ref_name;
105 5261c201 2018-04-01 stsp
106 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
107 5261c201 2018-04-01 stsp line += 5;
108 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
109 5261c201 2018-04-01 stsp }
110 5261c201 2018-04-01 stsp
111 5261c201 2018-04-01 stsp ref_name = strdup(name);
112 5261c201 2018-04-01 stsp if (ref_name == NULL)
113 5261c201 2018-04-01 stsp return got_error_from_errno();
114 5261c201 2018-04-01 stsp
115 5261c201 2018-04-01 stsp if (!got_parse_sha1_digest(digest, line))
116 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
117 5261c201 2018-04-01 stsp
118 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
119 5261c201 2018-04-01 stsp if (*ref == NULL)
120 5261c201 2018-04-01 stsp return got_error_from_errno();
121 5261c201 2018-04-01 stsp (*ref)->ref.ref.name = ref_name;
122 5261c201 2018-04-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
123 5261c201 2018-04-01 stsp return NULL;
124 5261c201 2018-04-01 stsp }
125 5261c201 2018-04-01 stsp
126 5261c201 2018-04-01 stsp static const struct got_error *
127 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
128 5261c201 2018-04-01 stsp const char *abspath)
129 5261c201 2018-04-01 stsp {
130 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
131 5261c201 2018-04-01 stsp FILE *f = fopen(abspath, "rb");
132 5261c201 2018-04-01 stsp char *line;
133 5261c201 2018-04-01 stsp size_t len;
134 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
135 5261c201 2018-04-01 stsp
136 5261c201 2018-04-01 stsp if (f == NULL)
137 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
138 5261c201 2018-04-01 stsp
139 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
140 5261c201 2018-04-01 stsp if (line == NULL) {
141 5261c201 2018-04-01 stsp err = got_error(GOT_ERR_NOT_REF);
142 5261c201 2018-04-01 stsp goto done;
143 5261c201 2018-04-01 stsp }
144 5261c201 2018-04-01 stsp
145 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
146 5261c201 2018-04-01 stsp done:
147 5261c201 2018-04-01 stsp free(line);
148 5261c201 2018-04-01 stsp fclose(f);
149 5261c201 2018-04-01 stsp return err;
150 5261c201 2018-04-01 stsp }
151 5261c201 2018-04-01 stsp
152 5261c201 2018-04-01 stsp static char *
153 5261c201 2018-04-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
154 5261c201 2018-04-01 stsp {
155 5261c201 2018-04-01 stsp if (strcmp(refname, GOT_REF_HEAD) == 0 ||
156 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
157 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
158 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
159 5261c201 2018-04-01 stsp strncmp(refname, "refs/", 5) == 0)
160 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
161 5261c201 2018-04-01 stsp
162 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
163 5261c201 2018-04-01 stsp }
164 5261c201 2018-04-01 stsp
165 d1f2edc9 2018-06-13 stsp static const struct got_error *
166 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
167 fb79db15 2019-02-01 stsp const char *line)
168 fb79db15 2019-02-01 stsp {
169 fb79db15 2019-02-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
170 fb79db15 2019-02-01 stsp char *name;
171 fb79db15 2019-02-01 stsp
172 fb79db15 2019-02-01 stsp *ref = NULL;
173 fb79db15 2019-02-01 stsp
174 fb79db15 2019-02-01 stsp if (line[0] == '#')
175 fb79db15 2019-02-01 stsp return NULL;
176 fb79db15 2019-02-01 stsp
177 fb79db15 2019-02-01 stsp if (!got_parse_sha1_digest(digest, line))
178 fb79db15 2019-02-01 stsp return got_error(GOT_ERR_NOT_REF);
179 fb79db15 2019-02-01 stsp
180 fb79db15 2019-02-01 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
181 fb79db15 2019-02-01 stsp return NULL;
182 fb79db15 2019-02-01 stsp
183 fb79db15 2019-02-01 stsp name = strdup(abs_refname);
184 fb79db15 2019-02-01 stsp if (name == NULL)
185 fb79db15 2019-02-01 stsp return got_error_from_errno();
186 fb79db15 2019-02-01 stsp
187 fb79db15 2019-02-01 stsp *ref = calloc(1, sizeof(**ref));
188 fb79db15 2019-02-01 stsp if (*ref == NULL)
189 fb79db15 2019-02-01 stsp return got_error_from_errno();
190 fb79db15 2019-02-01 stsp (*ref)->ref.ref.name = name;;
191 fb79db15 2019-02-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
192 fb79db15 2019-02-01 stsp return NULL;
193 fb79db15 2019-02-01 stsp }
194 fb79db15 2019-02-01 stsp
195 fb79db15 2019-02-01 stsp static const struct got_error *
196 fb79db15 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char *subdir,
197 fb79db15 2019-02-01 stsp const char *refname)
198 fb79db15 2019-02-01 stsp {
199 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
200 fb79db15 2019-02-01 stsp char *abs_refname;
201 fb79db15 2019-02-01 stsp char *line;
202 fb79db15 2019-02-01 stsp size_t len;
203 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
204 fb79db15 2019-02-01 stsp
205 fb79db15 2019-02-01 stsp if (asprintf(&abs_refname, "refs/%s/%s", subdir, refname) == -1)
206 fb79db15 2019-02-01 stsp return got_error_from_errno();
207 fb79db15 2019-02-01 stsp
208 fb79db15 2019-02-01 stsp do {
209 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
210 fb79db15 2019-02-01 stsp if (line == NULL) {
211 fb79db15 2019-02-01 stsp err = got_error(GOT_ERR_NOT_REF);
212 fb79db15 2019-02-01 stsp break;
213 fb79db15 2019-02-01 stsp }
214 fb79db15 2019-02-01 stsp
215 fb79db15 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
216 fb79db15 2019-02-01 stsp free(line);
217 fb79db15 2019-02-01 stsp if (err)
218 fb79db15 2019-02-01 stsp break;
219 fb79db15 2019-02-01 stsp } while (*ref == NULL);
220 fb79db15 2019-02-01 stsp
221 fb79db15 2019-02-01 stsp free(abs_refname);
222 fb79db15 2019-02-01 stsp return err;
223 fb79db15 2019-02-01 stsp }
224 fb79db15 2019-02-01 stsp
225 fb79db15 2019-02-01 stsp static const struct got_error *
226 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
227 d1f2edc9 2018-06-13 stsp const char *refname)
228 d1f2edc9 2018-06-13 stsp {
229 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
230 d1f2edc9 2018-06-13 stsp char *path_ref;
231 d1f2edc9 2018-06-13 stsp char *normpath;
232 d1f2edc9 2018-06-13 stsp
233 d1f2edc9 2018-06-13 stsp if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
234 d1f2edc9 2018-06-13 stsp return got_error_from_errno();
235 d1f2edc9 2018-06-13 stsp
236 d1f2edc9 2018-06-13 stsp normpath = got_path_normalize(path_ref);
237 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
238 d1f2edc9 2018-06-13 stsp err = got_error(GOT_ERR_NOT_REF);
239 d1f2edc9 2018-06-13 stsp goto done;
240 d1f2edc9 2018-06-13 stsp }
241 d1f2edc9 2018-06-13 stsp
242 d1f2edc9 2018-06-13 stsp err = parse_ref_file(ref, refname, normpath);
243 d1f2edc9 2018-06-13 stsp done:
244 d1f2edc9 2018-06-13 stsp free(path_ref);
245 d1f2edc9 2018-06-13 stsp free(normpath);
246 d1f2edc9 2018-06-13 stsp return err;
247 d1f2edc9 2018-06-13 stsp }
248 d1f2edc9 2018-06-13 stsp
249 5261c201 2018-04-01 stsp const struct got_error *
250 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
251 5261c201 2018-04-01 stsp const char *refname)
252 5261c201 2018-04-01 stsp {
253 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
254 fb79db15 2019-02-01 stsp char *path_refs;
255 fb79db15 2019-02-01 stsp const char *subdirs[] = {
256 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
257 fb79db15 2019-02-01 stsp };
258 fb79db15 2019-02-01 stsp char *packed_refs_path = got_repo_get_path_packed_refs(repo);
259 fb79db15 2019-02-01 stsp FILE *f = fopen(packed_refs_path, "rb");
260 fb79db15 2019-02-01 stsp int i;
261 5261c201 2018-04-01 stsp
262 fb79db15 2019-02-01 stsp if (f) {
263 fb79db15 2019-02-01 stsp for (i = 0; i < nitems(subdirs); i++) {
264 fb79db15 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs[i], refname);
265 fb79db15 2019-02-01 stsp if (err == NULL)
266 fb79db15 2019-02-01 stsp return NULL;
267 fb79db15 2019-02-01 stsp rewind(f);
268 fb79db15 2019-02-01 stsp }
269 fb79db15 2019-02-01 stsp fclose(f);
270 fb79db15 2019-02-01 stsp f = NULL;
271 fb79db15 2019-02-01 stsp }
272 fb79db15 2019-02-01 stsp
273 fb79db15 2019-02-01 stsp path_refs = get_refs_dir_path(repo, refname);
274 5261c201 2018-04-01 stsp if (path_refs == NULL) {
275 5261c201 2018-04-01 stsp err = got_error_from_errno();
276 5261c201 2018-04-01 stsp goto done;
277 5261c201 2018-04-01 stsp }
278 5261c201 2018-04-01 stsp
279 fb79db15 2019-02-01 stsp for (i = 0; i < nitems(subdirs); i++) {
280 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
281 fb79db15 2019-02-01 stsp if (err == NULL)
282 fb79db15 2019-02-01 stsp goto done;
283 fb79db15 2019-02-01 stsp }
284 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
285 5261c201 2018-04-01 stsp done:
286 5261c201 2018-04-01 stsp free(path_refs);
287 5261c201 2018-04-01 stsp return err;
288 5261c201 2018-04-01 stsp }
289 5261c201 2018-04-01 stsp
290 5261c201 2018-04-01 stsp void
291 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
292 5261c201 2018-04-01 stsp {
293 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
294 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
295 5261c201 2018-04-01 stsp else
296 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
297 5261c201 2018-04-01 stsp free(ref);
298 5261c201 2018-04-01 stsp }
299 5261c201 2018-04-01 stsp
300 5261c201 2018-04-01 stsp struct got_reference *
301 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
302 5261c201 2018-04-01 stsp {
303 5261c201 2018-04-01 stsp struct got_reference *ret;
304 5261c201 2018-04-01 stsp
305 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
306 5261c201 2018-04-01 stsp if (ret == NULL)
307 5261c201 2018-04-01 stsp return NULL;
308 5261c201 2018-04-01 stsp
309 5261c201 2018-04-01 stsp ret->flags = ref->flags;
310 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
311 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
312 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
313 5261c201 2018-04-01 stsp free(ret);
314 5261c201 2018-04-01 stsp return NULL;
315 5261c201 2018-04-01 stsp }
316 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
317 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
318 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
319 5261c201 2018-04-01 stsp free(ret);
320 5261c201 2018-04-01 stsp return NULL;
321 5261c201 2018-04-01 stsp }
322 5261c201 2018-04-01 stsp } else {
323 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
324 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
325 5261c201 2018-04-01 stsp free(ret);
326 5261c201 2018-04-01 stsp return NULL;
327 5261c201 2018-04-01 stsp }
328 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
329 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
330 5261c201 2018-04-01 stsp }
331 5261c201 2018-04-01 stsp
332 5261c201 2018-04-01 stsp return ret;
333 5261c201 2018-04-01 stsp }
334 5261c201 2018-04-01 stsp
335 5261c201 2018-04-01 stsp static const struct got_error *
336 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
337 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
338 5261c201 2018-04-01 stsp {
339 5261c201 2018-04-01 stsp struct got_reference *nextref;
340 5261c201 2018-04-01 stsp const struct got_error *err;
341 5261c201 2018-04-01 stsp
342 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
343 5261c201 2018-04-01 stsp if (err)
344 5261c201 2018-04-01 stsp return err;
345 5261c201 2018-04-01 stsp
346 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
347 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
348 5261c201 2018-04-01 stsp else
349 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
350 5261c201 2018-04-01 stsp
351 5261c201 2018-04-01 stsp got_ref_close(nextref);
352 5261c201 2018-04-01 stsp return err;
353 5261c201 2018-04-01 stsp }
354 5261c201 2018-04-01 stsp
355 5261c201 2018-04-01 stsp const struct got_error *
356 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
357 5261c201 2018-04-01 stsp struct got_reference *ref)
358 5261c201 2018-04-01 stsp {
359 5261c201 2018-04-01 stsp const struct got_error *err;
360 5261c201 2018-04-01 stsp
361 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
362 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
363 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
364 5261c201 2018-04-01 stsp if (err == NULL)
365 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
366 5261c201 2018-04-01 stsp free(resolved);
367 5261c201 2018-04-01 stsp return err;
368 5261c201 2018-04-01 stsp }
369 5261c201 2018-04-01 stsp
370 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
371 5261c201 2018-04-01 stsp if (*id == NULL)
372 5261c201 2018-04-01 stsp return got_error_from_errno();
373 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
374 5261c201 2018-04-01 stsp return NULL;
375 5261c201 2018-04-01 stsp }
376 5261c201 2018-04-01 stsp
377 5261c201 2018-04-01 stsp char *
378 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
379 5261c201 2018-04-01 stsp {
380 5261c201 2018-04-01 stsp char *str;
381 271d2a38 2018-12-25 stsp
382 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
383 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
384 271d2a38 2018-12-25 stsp
385 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
386 271d2a38 2018-12-25 stsp if (str == NULL)
387 271d2a38 2018-12-25 stsp return NULL;
388 271d2a38 2018-12-25 stsp
389 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
390 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
391 271d2a38 2018-12-25 stsp free(str);
392 271d2a38 2018-12-25 stsp return NULL;
393 5261c201 2018-04-01 stsp }
394 5261c201 2018-04-01 stsp
395 5261c201 2018-04-01 stsp return str;
396 5261c201 2018-04-01 stsp }