Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sha1.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <util.h>
25 #include <zlib.h>
26 #include <time.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
31 #include "got_reference.h"
33 #include "got_lib_sha1.h"
34 #include "got_lib_path.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
39 #ifndef nitems
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #endif
43 #define GOT_REF_HEADS "heads"
44 #define GOT_REF_TAGS "tags"
45 #define GOT_REF_REMOTES "remotes"
47 /* A symbolic reference. */
48 struct got_symref {
49 char *name;
50 char *ref;
51 };
53 /* A non-symbolic reference (there is no better designation). */
54 struct got_ref {
55 char *name;
56 u_int8_t sha1[SHA1_DIGEST_LENGTH];
57 };
59 /* A reference which points to an arbitrary object. */
60 struct got_reference {
61 unsigned int flags;
62 #define GOT_REF_IS_SYMBOLIC 0x01
64 union {
65 struct got_ref ref;
66 struct got_symref symref;
67 } ref;
68 };
70 static const struct got_error *
71 parse_symref(struct got_reference **ref, const char *name, const char *line)
72 {
73 struct got_symref *symref;
74 char *symref_name;
75 char *symref_ref;
77 if (line[0] == '\0')
78 return got_error(GOT_ERR_NOT_REF);
80 symref_name = strdup(name);
81 if (symref_name == NULL)
82 return got_error_from_errno();
83 symref_ref = strdup(line);
84 if (symref_ref == NULL) {
85 const struct got_error *err = got_error_from_errno();
86 free(symref_name);
87 return err;
88 }
90 *ref = calloc(1, sizeof(**ref));
91 if (*ref == NULL)
92 return got_error_from_errno();
93 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
94 symref = &((*ref)->ref.symref);
95 symref->name = symref_name;
96 symref->ref = symref_ref;
97 return NULL;
98 }
100 static const struct got_error *
101 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
103 uint8_t digest[SHA1_DIGEST_LENGTH];
104 char *ref_name;
106 if (strncmp(line, "ref: ", 5) == 0) {
107 line += 5;
108 return parse_symref(ref, name, line);
111 ref_name = strdup(name);
112 if (ref_name == NULL)
113 return got_error_from_errno();
115 if (!got_parse_sha1_digest(digest, line))
116 return got_error(GOT_ERR_NOT_REF);
118 *ref = calloc(1, sizeof(**ref));
119 if (*ref == NULL)
120 return got_error_from_errno();
121 (*ref)->ref.ref.name = ref_name;
122 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
123 return NULL;
126 static const struct got_error *
127 parse_ref_file(struct got_reference **ref, const char *name,
128 const char *abspath)
130 const struct got_error *err = NULL;
131 FILE *f = fopen(abspath, "rb");
132 char *line;
133 size_t len;
134 const char delim[3] = {'\0', '\0', '\0'};
136 if (f == NULL)
137 return got_error(GOT_ERR_NOT_REF);
139 line = fparseln(f, &len, NULL, delim, 0);
140 if (line == NULL) {
141 err = got_error(GOT_ERR_NOT_REF);
142 goto done;
145 err = parse_ref_line(ref, name, line);
146 done:
147 free(line);
148 fclose(f);
149 return err;
152 static int
153 is_well_known_ref(const char *refname)
155 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
156 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
157 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
158 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
161 static char *
162 get_refs_dir_path(struct got_repository *repo, const char *refname)
164 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
165 return strdup(got_repo_get_path_git_dir(repo));
167 return got_repo_get_path_refs(repo);
170 static const struct got_error *
171 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
172 const char *line)
174 uint8_t digest[SHA1_DIGEST_LENGTH];
175 char *name;
177 *ref = NULL;
179 if (line[0] == '#' || line[0] == '^')
180 return NULL;
182 if (!got_parse_sha1_digest(digest, line))
183 return got_error(GOT_ERR_NOT_REF);
185 if (abs_refname) {
186 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
187 return NULL;
189 name = strdup(abs_refname);
190 if (name == NULL)
191 return got_error_from_errno();
192 } else
193 name = strdup(line + SHA1_DIGEST_STRING_LENGTH);
195 *ref = calloc(1, sizeof(**ref));
196 if (*ref == NULL)
197 return got_error_from_errno();
198 (*ref)->ref.ref.name = name;;
199 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
200 return NULL;
203 static const struct got_error *
204 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
205 int nsubdirs, const char *refname)
207 const struct got_error *err = NULL;
208 char *abs_refname;
209 char *line;
210 size_t len;
211 const char delim[3] = {'\0', '\0', '\0'};
212 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
214 if (ref_is_absolute)
215 abs_refname = (char *)refname;
216 do {
217 line = fparseln(f, &len, NULL, delim, 0);
218 if (line == NULL) {
219 err = got_error(GOT_ERR_NOT_REF);
220 break;
222 for (i = 0; i < nsubdirs; i++) {
223 if (!ref_is_absolute &&
224 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
225 refname) == -1)
226 return got_error_from_errno();
227 err = parse_packed_ref_line(ref, abs_refname, line);
228 if (!ref_is_absolute)
229 free(abs_refname);
230 if (err || *ref != NULL)
231 break;
233 free(line);
234 if (err)
235 break;
236 } while (*ref == NULL);
238 return err;
241 static const struct got_error *
242 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
243 const char *refname)
245 const struct got_error *err = NULL;
246 char *path_ref;
247 char *normpath;
249 if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
250 return got_error_from_errno();
252 normpath = got_path_normalize(path_ref);
253 if (normpath == NULL) {
254 err = got_error(GOT_ERR_NOT_REF);
255 goto done;
258 err = parse_ref_file(ref, refname, normpath);
259 done:
260 free(path_ref);
261 free(normpath);
262 return err;
265 const struct got_error *
266 got_ref_open(struct got_reference **ref, struct got_repository *repo,
267 const char *refname)
269 const struct got_error *err = NULL;
270 char *path_refs = NULL;
271 const char *subdirs[] = {
272 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
273 };
274 int i, well_known = is_well_known_ref(refname);
276 if (!well_known) {
277 char *packed_refs_path;
278 FILE *f;
280 packed_refs_path = got_repo_get_path_packed_refs(repo);
281 if (packed_refs_path == NULL)
282 return got_error_from_errno();
284 f = fopen(packed_refs_path, "rb");
285 free(packed_refs_path);
286 if (f != NULL) {
287 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
288 refname);
289 fclose(f);
290 if (err == NULL)
291 goto done;
295 path_refs = get_refs_dir_path(repo, refname);
296 if (path_refs == NULL) {
297 err = got_error_from_errno();
298 goto done;
301 if (!well_known) {
302 for (i = 0; i < nitems(subdirs); i++) {
303 err = open_ref(ref, path_refs, subdirs[i], refname);
304 if (err == NULL)
305 goto done;
309 err = open_ref(ref, path_refs, "", refname);
310 done:
311 free(path_refs);
312 return err;
315 void
316 got_ref_close(struct got_reference *ref)
318 if (ref->flags & GOT_REF_IS_SYMBOLIC)
319 free(ref->ref.symref.name);
320 else
321 free(ref->ref.ref.name);
322 free(ref);
325 struct got_reference *
326 got_ref_dup(struct got_reference *ref)
328 struct got_reference *ret;
330 ret = calloc(1, sizeof(*ret));
331 if (ret == NULL)
332 return NULL;
334 ret->flags = ref->flags;
335 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
336 ret->ref.symref.name = strdup(ref->ref.symref.name);
337 if (ret->ref.symref.name == NULL) {
338 free(ret);
339 return NULL;
341 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
342 if (ret->ref.symref.ref == NULL) {
343 free(ret->ref.symref.name);
344 free(ret);
345 return NULL;
347 } else {
348 ref->ref.ref.name = strdup(ref->ref.ref.name);
349 if (ref->ref.ref.name == NULL) {
350 free(ret);
351 return NULL;
353 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
354 SHA1_DIGEST_LENGTH);
357 return ret;
360 static const struct got_error *
361 resolve_symbolic_ref(struct got_reference **resolved,
362 struct got_repository *repo, struct got_reference *ref)
364 struct got_reference *nextref;
365 const struct got_error *err;
367 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
368 if (err)
369 return err;
371 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
372 err = resolve_symbolic_ref(resolved, repo, nextref);
373 else
374 *resolved = got_ref_dup(nextref);
376 got_ref_close(nextref);
377 return err;
380 const struct got_error *
381 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
382 struct got_reference *ref)
384 const struct got_error *err;
386 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
387 struct got_reference *resolved = NULL;
388 err = resolve_symbolic_ref(&resolved, repo, ref);
389 if (err == NULL)
390 err = got_ref_resolve(id, repo, resolved);
391 free(resolved);
392 return err;
395 *id = calloc(1, sizeof(**id));
396 if (*id == NULL)
397 return got_error_from_errno();
398 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
399 return NULL;
402 char *
403 got_ref_to_str(struct got_reference *ref)
405 char *str;
407 if (ref->flags & GOT_REF_IS_SYMBOLIC)
408 return strdup(ref->ref.symref.ref);
410 str = malloc(SHA1_DIGEST_STRING_LENGTH);
411 if (str == NULL)
412 return NULL;
414 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
415 SHA1_DIGEST_STRING_LENGTH) == NULL) {
416 free(str);
417 return NULL;
420 return str;
423 const char *
424 got_ref_get_name(struct got_reference *ref)
426 if (ref->flags & GOT_REF_IS_SYMBOLIC)
427 return ref->ref.symref.name;
429 return ref->ref.ref.name;
432 static const struct got_error *
433 append_ref(struct got_reflist_head *refs, struct got_reference *ref,
434 struct got_repository *repo)
436 const struct got_error *err;
437 struct got_object_id *id;
438 struct got_reflist_entry *entry;
440 err = got_ref_resolve(&id, repo, ref);
441 if (err)
442 return err;
443 entry = malloc(sizeof(*entry));
444 if (entry == NULL)
445 return got_error_from_errno();
446 entry->ref = ref;
447 entry->id = id;
448 SIMPLEQ_INSERT_TAIL(refs, entry, entry);
449 return NULL;
452 const struct got_error *
453 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
455 const struct got_error *err;
456 char *packed_refs_path, *path_refs;
457 FILE *f;
458 struct got_reference *ref;
460 packed_refs_path = got_repo_get_path_packed_refs(repo);
461 if (packed_refs_path == NULL)
462 return got_error_from_errno();
464 f = fopen(packed_refs_path, "r");
465 free(packed_refs_path);
466 if (f) {
467 char *line;
468 size_t len;
469 const char delim[3] = {'\0', '\0', '\0'};
470 while (1) {
471 line = fparseln(f, &len, NULL, delim, 0);
472 if (line == NULL)
473 break;
474 err = parse_packed_ref_line(&ref, NULL, line);
475 if (err)
476 goto done;
477 if (ref)
478 append_ref(refs, ref, repo);
482 /* HEAD ref should always exist. */
483 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
484 if (path_refs == NULL) {
485 err = got_error_from_errno();
486 goto done;
488 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
489 free(path_refs);
490 if (err)
491 goto done;
492 append_ref(refs, ref, repo);
494 done:
495 if (f)
496 fclose(f);
497 return err;