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 <dirent.h>
21 #include <sha1.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <util.h>
26 #include <zlib.h>
27 #include <time.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_reference.h"
34 #include "got_lib_sha1.h"
35 #include "got_lib_path.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #endif
44 #define GOT_REF_HEADS "heads"
45 #define GOT_REF_TAGS "tags"
46 #define GOT_REF_REMOTES "remotes"
48 /* A symbolic reference. */
49 struct got_symref {
50 char *name;
51 char *ref;
52 };
54 /* A non-symbolic reference (there is no better designation). */
55 struct got_ref {
56 char *name;
57 u_int8_t sha1[SHA1_DIGEST_LENGTH];
58 };
60 /* A reference which points to an arbitrary object. */
61 struct got_reference {
62 unsigned int flags;
63 #define GOT_REF_IS_SYMBOLIC 0x01
65 union {
66 struct got_ref ref;
67 struct got_symref symref;
68 } ref;
69 };
71 static const struct got_error *
72 parse_symref(struct got_reference **ref, const char *name, const char *line)
73 {
74 struct got_symref *symref;
75 char *symref_name;
76 char *symref_ref;
78 if (line[0] == '\0')
79 return got_error(GOT_ERR_BAD_REF_DATA);
81 symref_name = strdup(name);
82 if (symref_name == NULL)
83 return got_error_from_errno();
84 symref_ref = strdup(line);
85 if (symref_ref == NULL) {
86 const struct got_error *err = got_error_from_errno();
87 free(symref_name);
88 return err;
89 }
91 *ref = calloc(1, sizeof(**ref));
92 if (*ref == NULL)
93 return got_error_from_errno();
94 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
95 symref = &((*ref)->ref.symref);
96 symref->name = symref_name;
97 symref->ref = symref_ref;
98 return NULL;
99 }
101 static const struct got_error *
102 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
104 struct got_object_id id;
106 if (strncmp(line, "ref: ", 5) == 0) {
107 line += 5;
108 return parse_symref(ref, name, line);
111 if (!got_parse_sha1_digest(id.sha1, line))
112 return got_error(GOT_ERR_BAD_REF_DATA);
114 return got_ref_alloc(ref, name, &id);
117 static const struct got_error *
118 parse_ref_file(struct got_reference **ref, const char *name,
119 const char *abspath)
121 const struct got_error *err = NULL;
122 FILE *f = fopen(abspath, "rb");
123 char *line;
124 size_t len;
125 const char delim[3] = {'\0', '\0', '\0'};
127 if (f == NULL)
128 return NULL;
130 line = fparseln(f, &len, NULL, delim, 0);
131 if (line == NULL) {
132 err = got_error(GOT_ERR_BAD_REF_DATA);
133 goto done;
136 err = parse_ref_line(ref, name, line);
137 done:
138 free(line);
139 if (fclose(f) != 0 && err == NULL)
140 err = got_error_from_errno();
141 return err;
144 static int
145 is_well_known_ref(const char *refname)
147 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
148 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
149 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
150 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
153 static char *
154 get_refs_dir_path(struct got_repository *repo, const char *refname)
156 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
157 return strdup(got_repo_get_path_git_dir(repo));
159 return got_repo_get_path_refs(repo);
162 const struct got_error *
163 got_ref_alloc(struct got_reference **ref, const char *name,
164 struct got_object_id *id)
166 const struct got_error *err = NULL;
168 *ref = calloc(1, sizeof(**ref));
169 if (*ref == NULL)
170 return got_error_from_errno();
172 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
173 (*ref)->ref.ref.name = strdup(name);
174 if ((*ref)->ref.ref.name == NULL) {
175 err = got_error_from_errno();
176 free(*ref);
177 *ref = NULL;
179 return err;
182 static const struct got_error *
183 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
184 const char *line)
186 struct got_object_id id;
187 const char *name;
189 *ref = NULL;
191 if (line[0] == '#' || line[0] == '^')
192 return NULL;
194 if (!got_parse_sha1_digest(id.sha1, line))
195 return got_error(GOT_ERR_BAD_REF_DATA);
197 if (abs_refname) {
198 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
199 return NULL;
200 name = abs_refname;
201 } else
202 name = line + SHA1_DIGEST_STRING_LENGTH;
204 return got_ref_alloc(ref, name, &id);
207 static const struct got_error *
208 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
209 int nsubdirs, const char *refname)
211 const struct got_error *err = NULL;
212 char *abs_refname;
213 char *line;
214 size_t len;
215 const char delim[3] = {'\0', '\0', '\0'};
216 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
218 *ref = NULL;
220 if (ref_is_absolute)
221 abs_refname = (char *)refname;
222 do {
223 line = fparseln(f, &len, NULL, delim, 0);
224 if (line == NULL)
225 break;
226 for (i = 0; i < nsubdirs; i++) {
227 if (!ref_is_absolute &&
228 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
229 refname) == -1)
230 return got_error_from_errno();
231 err = parse_packed_ref_line(ref, abs_refname, line);
232 if (!ref_is_absolute)
233 free(abs_refname);
234 if (err || *ref != NULL)
235 break;
237 free(line);
238 if (err)
239 break;
240 } while (*ref == NULL);
242 return err;
245 static const struct got_error *
246 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
247 const char *name)
249 const struct got_error *err = NULL;
250 char *path = NULL;
251 char *normpath = NULL;
252 char *absname = NULL;
253 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
254 int ref_is_well_known = is_well_known_ref(name);
256 *ref = NULL;
258 if (ref_is_absolute || ref_is_well_known) {
259 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
260 return got_error_from_errno();
261 absname = (char *)name;
262 } else {
263 if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
264 return got_error_from_errno();
266 if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
267 err = got_error_from_errno();
268 goto done;
272 normpath = got_path_normalize(path);
273 if (normpath == NULL) {
274 err = got_error_from_errno();
275 goto done;
278 err = parse_ref_file(ref, absname, normpath);
279 done:
280 if (!ref_is_absolute && !ref_is_well_known)
281 free(absname);
282 free(path);
283 free(normpath);
284 return err;
287 const struct got_error *
288 got_ref_open(struct got_reference **ref, struct got_repository *repo,
289 const char *refname)
291 const struct got_error *err = NULL;
292 char *path_refs = NULL;
293 const char *subdirs[] = {
294 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
295 };
296 int i, well_known = is_well_known_ref(refname);
298 *ref = NULL;
300 path_refs = get_refs_dir_path(repo, refname);
301 if (path_refs == NULL) {
302 err = got_error_from_errno();
303 goto done;
306 if (!well_known) {
307 char *packed_refs_path;
308 FILE *f;
310 /* Search on-disk refs before packed refs! */
311 for (i = 0; i < nitems(subdirs); i++) {
312 err = open_ref(ref, path_refs, subdirs[i], refname);
313 if (err || *ref)
314 goto done;
317 packed_refs_path = got_repo_get_path_packed_refs(repo);
318 if (packed_refs_path == NULL) {
319 err = got_error_from_errno();
320 goto done;
323 f = fopen(packed_refs_path, "rb");
324 free(packed_refs_path);
325 if (f != NULL) {
326 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
327 refname);
328 if (fclose(f) != 0 && err == NULL)
329 err = got_error_from_errno();
330 if (err || *ref)
331 goto done;
335 err = open_ref(ref, path_refs, "", refname);
336 if (err)
337 goto done;
338 done:
339 if (*ref == NULL)
340 err = got_error_not_ref(refname);
341 free(path_refs);
342 return err;
345 void
346 got_ref_close(struct got_reference *ref)
348 if (ref->flags & GOT_REF_IS_SYMBOLIC)
349 free(ref->ref.symref.name);
350 else
351 free(ref->ref.ref.name);
352 free(ref);
355 struct got_reference *
356 got_ref_dup(struct got_reference *ref)
358 struct got_reference *ret;
360 ret = calloc(1, sizeof(*ret));
361 if (ret == NULL)
362 return NULL;
364 ret->flags = ref->flags;
365 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
366 ret->ref.symref.name = strdup(ref->ref.symref.name);
367 if (ret->ref.symref.name == NULL) {
368 free(ret);
369 return NULL;
371 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
372 if (ret->ref.symref.ref == NULL) {
373 free(ret->ref.symref.name);
374 free(ret);
375 return NULL;
377 } else {
378 ref->ref.ref.name = strdup(ref->ref.ref.name);
379 if (ref->ref.ref.name == NULL) {
380 free(ret);
381 return NULL;
383 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
384 SHA1_DIGEST_LENGTH);
387 return ret;
390 static const struct got_error *
391 resolve_symbolic_ref(struct got_reference **resolved,
392 struct got_repository *repo, struct got_reference *ref)
394 struct got_reference *nextref;
395 const struct got_error *err;
397 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
398 if (err)
399 return err;
401 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
402 err = resolve_symbolic_ref(resolved, repo, nextref);
403 else
404 *resolved = got_ref_dup(nextref);
406 got_ref_close(nextref);
407 return err;
410 const struct got_error *
411 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
412 struct got_reference *ref)
414 const struct got_error *err;
416 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
417 struct got_reference *resolved = NULL;
418 err = resolve_symbolic_ref(&resolved, repo, ref);
419 if (err == NULL)
420 err = got_ref_resolve(id, repo, resolved);
421 free(resolved);
422 return err;
425 *id = calloc(1, sizeof(**id));
426 if (*id == NULL)
427 return got_error_from_errno();
428 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
429 return NULL;
432 char *
433 got_ref_to_str(struct got_reference *ref)
435 char *str;
437 if (ref->flags & GOT_REF_IS_SYMBOLIC)
438 return strdup(ref->ref.symref.ref);
440 str = malloc(SHA1_DIGEST_STRING_LENGTH);
441 if (str == NULL)
442 return NULL;
444 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
445 SHA1_DIGEST_STRING_LENGTH) == NULL) {
446 free(str);
447 return NULL;
450 return str;
453 const char *
454 got_ref_get_name(struct got_reference *ref)
456 if (ref->flags & GOT_REF_IS_SYMBOLIC)
457 return ref->ref.symref.name;
459 return ref->ref.ref.name;
462 static const struct got_error *
463 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
464 struct got_repository *repo)
466 const struct got_error *err;
467 struct got_object_id *id;
468 struct got_reflist_entry *new, *re, *prev;
469 int cmp;
471 err = got_ref_resolve(&id, repo, ref);
472 if (err)
473 return err;
475 new = malloc(sizeof(*re));
476 if (new == NULL) {
477 free(id);
478 return got_error_from_errno();
480 new->ref = ref;
481 new->id = id;
483 /*
484 * We must de-duplicate entries on insert because packed-refs may
485 * contain redundant entries. On-disk refs take precedence.
486 * This code assumes that on-disk revs are read before packed-refs.
487 * We're iterating the list anyway, so insert elements sorted by name.
488 */
489 re = SIMPLEQ_FIRST(refs);
490 while (re) {
491 cmp = got_path_cmp(got_ref_get_name(re->ref),
492 got_ref_get_name(ref));
493 if (cmp == 0) {
494 free(ref); /* duplicate */
495 return NULL;
496 } else if (cmp > 0) {
497 if (prev)
498 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
499 else
500 SIMPLEQ_INSERT_HEAD(refs, new, entry);
501 return NULL;
502 } else {
503 prev = re;
504 re = SIMPLEQ_NEXT(re, entry);
508 SIMPLEQ_INSERT_TAIL(refs, new, entry);
509 return NULL;
512 static const struct got_error *
513 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
514 const char *subdir, struct got_repository *repo)
516 const struct got_error *err = NULL;
517 DIR *d = NULL;
518 char *path_subdir;
520 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
521 return got_error_from_errno();
523 d = opendir(path_subdir);
524 if (d == NULL)
525 goto done;
527 while (1) {
528 struct dirent *dent;
529 struct got_reference *ref;
530 char *child;
532 dent = readdir(d);
533 if (dent == NULL)
534 break;
536 if (strcmp(dent->d_name, ".") == 0 ||
537 strcmp(dent->d_name, "..") == 0)
538 continue;
540 switch (dent->d_type) {
541 case DT_REG:
542 err = open_ref(&ref, path_refs, subdir, dent->d_name);
543 if (err)
544 goto done;
545 if (ref) {
546 err = insert_ref(refs, ref, repo);
547 if (err)
548 goto done;
550 break;
551 case DT_DIR:
552 if (asprintf(&child, "%s%s%s", subdir,
553 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
554 err = got_error_from_errno();
555 break;
557 err = gather_on_disk_refs(refs, path_refs, child, repo);
558 free(child);
559 break;
560 default:
561 break;
564 done:
565 if (d)
566 closedir(d);
567 free(path_subdir);
568 return err;
571 const struct got_error *
572 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
574 const struct got_error *err;
575 char *packed_refs_path, *path_refs = NULL;
576 FILE *f = NULL;
577 struct got_reference *ref;
579 /* HEAD ref should always exist. */
580 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
581 if (path_refs == NULL) {
582 err = got_error_from_errno();
583 goto done;
585 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
586 if (err)
587 goto done;
588 err = insert_ref(refs, ref, repo);
589 if (err)
590 goto done;
592 /* Gather on-disk refs before parsing packed-refs. */
593 free(path_refs);
594 path_refs = get_refs_dir_path(repo, "");
595 if (path_refs == NULL) {
596 err = got_error_from_errno();
597 goto done;
599 err = gather_on_disk_refs(refs, path_refs, "", repo);
600 if (err)
601 goto done;
603 /*
604 * The packed-refs file may contain redundant entries, in which
605 * case on-disk refs take precedence.
606 */
607 packed_refs_path = got_repo_get_path_packed_refs(repo);
608 if (packed_refs_path == NULL) {
609 err = got_error_from_errno();
610 goto done;
613 f = fopen(packed_refs_path, "r");
614 free(packed_refs_path);
615 if (f) {
616 char *line;
617 size_t len;
618 const char delim[3] = {'\0', '\0', '\0'};
619 while (1) {
620 line = fparseln(f, &len, NULL, delim, 0);
621 if (line == NULL)
622 break;
623 err = parse_packed_ref_line(&ref, NULL, line);
624 if (err)
625 goto done;
626 if (ref) {
627 err = insert_ref(refs, ref, repo);
628 if (err)
629 goto done;
633 done:
634 free(path_refs);
635 if (f && fclose(f) != 0 && err == NULL)
636 err = got_error_from_errno();
637 return err;