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>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <util.h>
31 #include <zlib.h>
32 #include <time.h>
33 #include <libgen.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
40 #include "got_path.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_lockfile.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #endif
52 #define GOT_REF_HEADS "heads"
53 #define GOT_REF_TAGS "tags"
54 #define GOT_REF_REMOTES "remotes"
56 /*
57 * We do not resolve tags yet, and don't yet care about sorting refs either,
58 * so packed-refs files we write contain a minimal header which disables all
59 * packed-refs "traits" supported by Git.
60 */
61 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 /* A symbolic reference. */
64 struct got_symref {
65 char *name;
66 char *ref;
67 };
69 #define GOT_REF_RECURSE_MAX 20
71 /* A non-symbolic reference (there is no better designation). */
72 struct got_ref {
73 char *name;
74 u_int8_t sha1[SHA1_DIGEST_LENGTH];
75 };
77 /* A reference which points to an arbitrary object. */
78 struct got_reference {
79 unsigned int flags;
80 #define GOT_REF_IS_SYMBOLIC 0x01
81 #define GOT_REF_IS_PACKED 0x02
83 union {
84 struct got_ref ref;
85 struct got_symref symref;
86 } ref;
88 struct got_lockfile *lf;
89 };
91 static const struct got_error *
92 alloc_ref(struct got_reference **ref, const char *name,
93 struct got_object_id *id, int flags)
94 {
95 const struct got_error *err = NULL;
97 *ref = calloc(1, sizeof(**ref));
98 if (*ref == NULL)
99 return got_error_from_errno("calloc");
101 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
102 (*ref)->flags = flags;
103 (*ref)->ref.ref.name = strdup(name);
104 if ((*ref)->ref.ref.name == NULL) {
105 err = got_error_from_errno("strdup");
106 got_ref_close(*ref);
107 *ref = NULL;
109 return err;
112 static const struct got_error *
113 alloc_symref(struct got_reference **ref, const char *name,
114 const char *target_ref, int flags)
116 const struct got_error *err = NULL;
118 *ref = calloc(1, sizeof(**ref));
119 if (*ref == NULL)
120 return got_error_from_errno("calloc");
122 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
123 (*ref)->ref.symref.name = strdup(name);
124 if ((*ref)->ref.symref.name == NULL) {
125 err = got_error_from_errno("strdup");
126 got_ref_close(*ref);
127 *ref = NULL;
128 return err;
130 (*ref)->ref.symref.ref = strdup(target_ref);
131 if ((*ref)->ref.symref.ref == NULL) {
132 err = got_error_from_errno("strdup");
133 got_ref_close(*ref);
134 *ref = NULL;
136 return err;
139 static const struct got_error *
140 parse_symref(struct got_reference **ref, const char *name, const char *line)
142 if (line[0] == '\0')
143 return got_error(GOT_ERR_BAD_REF_DATA);
145 return alloc_symref(ref, name, line, 0);
148 static const struct got_error *
149 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
151 struct got_object_id id;
153 if (strncmp(line, "ref: ", 5) == 0) {
154 line += 5;
155 return parse_symref(ref, name, line);
158 if (!got_parse_sha1_digest(id.sha1, line))
159 return got_error(GOT_ERR_BAD_REF_DATA);
161 return alloc_ref(ref, name, &id, 0);
164 static const struct got_error *
165 parse_ref_file(struct got_reference **ref, const char *name,
166 const char *absname, const char *abspath, int lock)
168 const struct got_error *err = NULL;
169 FILE *f;
170 char *line = NULL;
171 size_t linesize = 0;
172 ssize_t linelen;
173 struct got_lockfile *lf = NULL;
175 if (lock) {
176 err = got_lockfile_lock(&lf, abspath);
177 if (err) {
178 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
179 err = got_error_not_ref(name);
180 return err;
184 f = fopen(abspath, "rb");
185 if (f == NULL) {
186 if (errno != ENOTDIR && errno != ENOENT)
187 err = got_error_from_errno2("fopen", abspath);
188 else
189 err = got_error_not_ref(name);
190 if (lock)
191 got_lockfile_unlock(lf);
192 return err;
195 linelen = getline(&line, &linesize, f);
196 if (linelen == -1) {
197 if (feof(f))
198 err = NULL; /* ignore empty files (could be locks) */
199 else {
200 if (errno == EISDIR)
201 err = got_error(GOT_ERR_NOT_REF);
202 else if (ferror(f))
203 err = got_ferror(f, GOT_ERR_IO);
204 else
205 err = got_error_from_errno2("getline", abspath);
207 if (lock)
208 got_lockfile_unlock(lf);
209 goto done;
211 while (linelen > 0 && line[linelen - 1] == '\n') {
212 line[linelen - 1] = '\0';
213 linelen--;
216 err = parse_ref_line(ref, absname, line);
217 if (lock) {
218 if (err)
219 got_lockfile_unlock(lf);
220 else {
221 if (*ref)
222 (*ref)->lf = lf;
223 else
224 got_lockfile_unlock(lf);
227 done:
228 free(line);
229 if (fclose(f) != 0 && err == NULL) {
230 err = got_error_from_errno("fclose");
231 if (*ref) {
232 if (lock)
233 got_ref_unlock(*ref);
234 got_ref_close(*ref);
235 *ref = NULL;
238 return err;
241 static int
242 is_well_known_ref(const char *refname)
244 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
245 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
247 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
250 static char *
251 get_refs_dir_path(struct got_repository *repo, const char *refname)
253 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
254 return strdup(got_repo_get_path_git_dir(repo));
256 return got_repo_get_path_refs(repo);
259 static int
260 is_valid_ref_name(const char *name)
262 const char *s, *seg;
263 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
264 const char *forbidden_seq[] = { "//", "..", "@{" };
265 const char *lfs = GOT_LOCKFILE_SUFFIX;
266 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
267 int i;
269 if (name[0] == '@' && name[1] == '\0')
270 return 0;
272 s = name;
273 seg = s;
274 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
275 return 0;
276 while (*s) {
277 for (i = 0; i < nitems(forbidden); i++) {
278 if (*s == forbidden[i])
279 return 0;
281 for (i = 0; i < nitems(forbidden_seq); i++) {
282 if (s[0] == forbidden_seq[i][0] &&
283 s[1] == forbidden_seq[i][1])
284 return 0;
286 if (iscntrl((unsigned char)s[0]))
287 return 0;
288 if (s[0] == '.' && s[1] == '\0')
289 return 0;
290 if (*s == '/') {
291 const char *nextseg = s + 1;
292 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
293 nextseg[0] == '/')
294 return 0;
295 if (seg <= s - lfs_len &&
296 strncmp(s - lfs_len, lfs, lfs_len) == 0)
297 return 0;
298 seg = nextseg;
300 s++;
303 if (seg <= s - lfs_len &&
304 strncmp(s - lfs_len, lfs, lfs_len) == 0)
305 return 0;
307 return 1;
310 const struct got_error *
311 got_ref_alloc(struct got_reference **ref, const char *name,
312 struct got_object_id *id)
314 if (!is_valid_ref_name(name))
315 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
317 return alloc_ref(ref, name, id, 0);
320 const struct got_error *
321 got_ref_alloc_symref(struct got_reference **ref, const char *name,
322 struct got_reference *target_ref)
324 if (!is_valid_ref_name(name))
325 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
327 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
330 static const struct got_error *
331 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
332 const char *line)
334 struct got_object_id id;
335 const char *name;
337 *ref = NULL;
339 if (line[0] == '#' || line[0] == '^')
340 return NULL;
342 if (!got_parse_sha1_digest(id.sha1, line))
343 return got_error(GOT_ERR_BAD_REF_DATA);
345 if (abs_refname) {
346 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
347 return NULL;
348 name = abs_refname;
349 } else
350 name = line + SHA1_DIGEST_STRING_LENGTH;
352 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
355 static const struct got_error *
356 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
357 int nsubdirs, const char *refname)
359 const struct got_error *err = NULL;
360 char *abs_refname;
361 char *line;
362 size_t len;
363 const char delim[3] = {'\0', '\0', '\0'};
364 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
366 *ref = NULL;
368 if (ref_is_absolute)
369 abs_refname = (char *)refname;
370 do {
371 line = fparseln(f, &len, NULL, delim, 0);
372 if (line == NULL) {
373 if (feof(f))
374 break;
375 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
376 break;
378 for (i = 0; i < nsubdirs; i++) {
379 if (!ref_is_absolute &&
380 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
381 refname) == -1)
382 return got_error_from_errno("asprintf");
383 err = parse_packed_ref_line(ref, abs_refname, line);
384 if (!ref_is_absolute)
385 free(abs_refname);
386 if (err || *ref != NULL)
387 break;
389 free(line);
390 if (err)
391 break;
392 } while (*ref == NULL);
394 return err;
397 static const struct got_error *
398 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
399 const char *name, int lock)
401 const struct got_error *err = NULL;
402 char *path = NULL;
403 char *absname = NULL;
404 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
405 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
407 *ref = NULL;
409 if (ref_is_absolute || ref_is_well_known) {
410 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
411 return got_error_from_errno("asprintf");
412 absname = (char *)name;
413 } else {
414 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
415 subdir[0] ? "/" : "", name) == -1)
416 return got_error_from_errno("asprintf");
418 if (asprintf(&absname, "refs/%s%s%s",
419 subdir, subdir[0] ? "/" : "", name) == -1) {
420 err = got_error_from_errno("asprintf");
421 goto done;
425 err = parse_ref_file(ref, name, absname, path, lock);
426 done:
427 if (!ref_is_absolute && !ref_is_well_known)
428 free(absname);
429 free(path);
430 return err;
433 const struct got_error *
434 got_ref_open(struct got_reference **ref, struct got_repository *repo,
435 const char *refname, int lock)
437 const struct got_error *err = NULL;
438 char *path_refs = NULL;
439 const char *subdirs[] = {
440 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
441 };
442 int i, well_known = is_well_known_ref(refname);
443 struct got_lockfile *lf = NULL;
445 *ref = NULL;
447 path_refs = get_refs_dir_path(repo, refname);
448 if (path_refs == NULL) {
449 err = got_error_from_errno2("get_refs_dir_path", refname);
450 goto done;
453 if (well_known) {
454 err = open_ref(ref, path_refs, "", refname, lock);
455 } else {
456 char *packed_refs_path;
457 FILE *f;
459 /* Search on-disk refs before packed refs! */
460 for (i = 0; i < nitems(subdirs); i++) {
461 err = open_ref(ref, path_refs, subdirs[i], refname,
462 lock);
463 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
464 goto done;
467 packed_refs_path = got_repo_get_path_packed_refs(repo);
468 if (packed_refs_path == NULL) {
469 err = got_error_from_errno(
470 "got_repo_get_path_packed_refs");
471 goto done;
474 if (lock) {
475 err = got_lockfile_lock(&lf, packed_refs_path);
476 if (err)
477 goto done;
479 f = fopen(packed_refs_path, "rb");
480 free(packed_refs_path);
481 if (f != NULL) {
482 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
483 refname);
484 if (!err) {
485 if (fclose(f) != 0) {
486 err = got_error_from_errno("fclose");
487 got_ref_close(*ref);
488 *ref = NULL;
489 } else if (*ref)
490 (*ref)->lf = lf;
494 done:
495 if (!err && *ref == NULL)
496 err = got_error_not_ref(refname);
497 if (err && lf)
498 got_lockfile_unlock(lf);
499 free(path_refs);
500 return err;
503 void
504 got_ref_close(struct got_reference *ref)
506 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
507 free(ref->ref.symref.name);
508 free(ref->ref.symref.ref);
509 } else
510 free(ref->ref.ref.name);
511 free(ref);
514 struct got_reference *
515 got_ref_dup(struct got_reference *ref)
517 struct got_reference *ret;
519 ret = calloc(1, sizeof(*ret));
520 if (ret == NULL)
521 return NULL;
523 ret->flags = ref->flags;
524 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
525 ret->ref.symref.name = strdup(ref->ref.symref.name);
526 if (ret->ref.symref.name == NULL) {
527 free(ret);
528 return NULL;
530 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
531 if (ret->ref.symref.ref == NULL) {
532 free(ret->ref.symref.name);
533 free(ret);
534 return NULL;
536 } else {
537 ref->ref.ref.name = strdup(ref->ref.ref.name);
538 if (ref->ref.ref.name == NULL) {
539 free(ret);
540 return NULL;
542 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
543 sizeof(ret->ref.ref.sha1));
546 return ret;
549 const struct got_error *
550 got_reflist_entry_dup(struct got_reflist_entry **newp,
551 struct got_reflist_entry *re)
553 const struct got_error *err = NULL;
554 struct got_reflist_entry *new;
556 *newp = NULL;
558 new = malloc(sizeof(*new));
559 if (new == NULL)
560 return got_error_from_errno("malloc");
562 new->ref = got_ref_dup(re->ref);
563 if (new->ref == NULL) {
564 err = got_error_from_errno("got_ref_dup");
565 free(new);
566 return err;
569 *newp = new;
570 return NULL;
573 static const struct got_error *
574 resolve_symbolic_ref(struct got_reference **resolved,
575 struct got_repository *repo, struct got_reference *ref)
577 struct got_reference *nextref;
578 const struct got_error *err;
580 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
581 if (err)
582 return err;
584 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
585 err = resolve_symbolic_ref(resolved, repo, nextref);
586 else
587 *resolved = got_ref_dup(nextref);
589 got_ref_close(nextref);
590 return err;
593 static const struct got_error *
594 ref_resolve(struct got_object_id **id, struct got_repository *repo,
595 struct got_reference *ref, int recursion)
597 const struct got_error *err;
599 if (recursion <= 0)
600 return got_error_msg(GOT_ERR_RECURSION,
601 "reference recursion limit reached");
603 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
604 struct got_reference *resolved = NULL;
605 err = resolve_symbolic_ref(&resolved, repo, ref);
606 if (err == NULL)
607 err = ref_resolve(id, repo, resolved, --recursion);
608 if (resolved)
609 got_ref_close(resolved);
610 return err;
613 *id = calloc(1, sizeof(**id));
614 if (*id == NULL)
615 return got_error_from_errno("calloc");
616 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
617 return NULL;
620 const struct got_error *
621 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
622 struct got_reference *ref)
624 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
627 char *
628 got_ref_to_str(struct got_reference *ref)
630 char *str;
632 if (ref->flags & GOT_REF_IS_SYMBOLIC)
633 return strdup(ref->ref.symref.ref);
635 str = malloc(SHA1_DIGEST_STRING_LENGTH);
636 if (str == NULL)
637 return NULL;
639 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
640 SHA1_DIGEST_STRING_LENGTH) == NULL) {
641 free(str);
642 return NULL;
645 return str;
648 const char *
649 got_ref_get_name(struct got_reference *ref)
651 if (ref->flags & GOT_REF_IS_SYMBOLIC)
652 return ref->ref.symref.name;
654 return ref->ref.ref.name;
657 const char *
658 got_ref_get_symref_target(struct got_reference *ref)
660 if (ref->flags & GOT_REF_IS_SYMBOLIC)
661 return ref->ref.symref.ref;
663 return NULL;
666 const struct got_error *
667 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
668 struct got_reference* re2)
670 const char *name1 = got_ref_get_name(re1);
671 const char *name2 = got_ref_get_name(re2);
673 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
674 return NULL;
677 const struct got_error *
678 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
679 struct got_reference *ref2)
681 const struct got_error *err = NULL;
682 struct got_repository *repo = arg;
683 struct got_object_id *id1, *id2 = NULL;
684 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
685 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
686 time_t time1, time2;
688 *cmp = 0;
690 err = got_ref_resolve(&id1, repo, ref1);
691 if (err)
692 return err;
693 err = got_object_open_as_tag(&tag1, repo, id1);
694 if (err) {
695 if (err->code != GOT_ERR_OBJ_TYPE)
696 goto done;
697 /* "lightweight" tag */
698 err = got_object_open_as_commit(&commit1, repo, id1);
699 if (err)
700 goto done;
701 time1 = got_object_commit_get_committer_time(commit1);
702 } else
703 time1 = got_object_tag_get_tagger_time(tag1);
705 err = got_ref_resolve(&id2, repo, ref2);
706 if (err)
707 goto done;
708 err = got_object_open_as_tag(&tag2, repo, id2);
709 if (err) {
710 if (err->code != GOT_ERR_OBJ_TYPE)
711 goto done;
712 /* "lightweight" tag */
713 err = got_object_open_as_commit(&commit2, repo, id2);
714 if (err)
715 goto done;
716 time2 = got_object_commit_get_committer_time(commit2);
717 } else
718 time2 = got_object_tag_get_tagger_time(tag2);
720 /* Put latest tags first. */
721 if (time1 < time2)
722 *cmp = 1;
723 else if (time1 > time2)
724 *cmp = -1;
725 else
726 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
727 done:
728 free(id1);
729 free(id2);
730 if (tag1)
731 got_object_tag_close(tag1);
732 if (tag2)
733 got_object_tag_close(tag2);
734 if (commit1)
735 got_object_commit_close(commit1);
736 if (commit2)
737 got_object_commit_close(commit2);
738 return err;
741 static const struct got_error *
742 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
743 struct got_reference *ref, struct got_repository *repo,
744 got_ref_cmp_cb cmp_cb, void *cmp_arg)
746 const struct got_error *err;
747 struct got_reflist_entry *new, *re, *prev = NULL;
748 int cmp;
750 *newp = NULL;
752 new = malloc(sizeof(*new));
753 if (new == NULL)
754 return got_error_from_errno("malloc");
755 new->ref = ref;
756 *newp = new;
758 /*
759 * We must de-duplicate entries on insert because packed-refs may
760 * contain redundant entries. On-disk refs take precedence.
761 * This code assumes that on-disk revs are read before packed-refs.
762 * We're iterating the list anyway, so insert elements sorted by name.
763 */
764 re = SIMPLEQ_FIRST(refs);
765 while (re) {
766 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
767 if (err)
768 return err;
769 if (cmp == 0) {
770 /* duplicate */
771 free(new);
772 *newp = NULL;
773 return NULL;
774 } else if (cmp > 0) {
775 if (prev)
776 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
777 else
778 SIMPLEQ_INSERT_HEAD(refs, new, entry);
779 return NULL;
780 } else {
781 prev = re;
782 re = SIMPLEQ_NEXT(re, entry);
786 SIMPLEQ_INSERT_TAIL(refs, new, entry);
787 return NULL;
790 static const struct got_error *
791 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
792 const char *subdir, struct got_repository *repo,
793 got_ref_cmp_cb cmp_cb, void *cmp_arg)
795 const struct got_error *err = NULL;
796 DIR *d = NULL;
797 char *path_subdir;
799 while (subdir[0] == '/')
800 subdir++;
802 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
803 return got_error_from_errno("asprintf");
805 d = opendir(path_subdir);
806 if (d == NULL)
807 goto done;
809 for (;;) {
810 struct dirent *dent;
811 struct got_reference *ref;
812 char *child;
813 int type;
815 dent = readdir(d);
816 if (dent == NULL)
817 break;
819 if (strcmp(dent->d_name, ".") == 0 ||
820 strcmp(dent->d_name, "..") == 0)
821 continue;
823 err = got_path_dirent_type(&type, path_subdir, dent);
824 if (err)
825 break;
827 switch (type) {
828 case DT_REG:
829 err = open_ref(&ref, path_refs, subdir, dent->d_name,
830 0);
831 if (err)
832 goto done;
833 if (ref) {
834 struct got_reflist_entry *new;
835 err = insert_ref(&new, refs, ref, repo,
836 cmp_cb, cmp_arg);
837 if (err || new == NULL /* duplicate */)
838 got_ref_close(ref);
839 if (err)
840 goto done;
842 break;
843 case DT_DIR:
844 if (asprintf(&child, "%s%s%s", subdir,
845 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
846 err = got_error_from_errno("asprintf");
847 break;
849 err = gather_on_disk_refs(refs, path_refs, child, repo,
850 cmp_cb, cmp_arg);
851 free(child);
852 break;
853 default:
854 break;
857 done:
858 if (d)
859 closedir(d);
860 free(path_subdir);
861 return err;
864 const struct got_error *
865 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
866 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
868 const struct got_error *err;
869 char *packed_refs_path, *path_refs = NULL;
870 char *abs_namespace = NULL;
871 char *buf = NULL, *ondisk_ref_namespace = NULL;
872 FILE *f = NULL;
873 struct got_reference *ref;
874 struct got_reflist_entry *new;
876 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
877 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
878 if (path_refs == NULL) {
879 err = got_error_from_errno("get_refs_dir_path");
880 goto done;
882 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
883 if (err)
884 goto done;
885 err = insert_ref(&new, refs, ref, repo,
886 cmp_cb, cmp_arg);
887 if (err || new == NULL /* duplicate */)
888 got_ref_close(ref);
889 if (err && err->code != GOT_ERR_NOT_REF)
890 goto done;
891 } else {
892 /* Try listing a single reference. */
893 const char *refname = ref_namespace;
894 path_refs = get_refs_dir_path(repo, refname);
895 if (path_refs == NULL) {
896 err = got_error_from_errno("get_refs_dir_path");
897 goto done;
899 err = open_ref(&ref, path_refs, "", refname, 0);
900 if (err) {
901 if (err->code != GOT_ERR_NOT_REF)
902 goto done;
903 /* Try to look up references in a given namespace. */
904 } else {
905 err = insert_ref(&new, refs, ref, repo,
906 cmp_cb, cmp_arg);
907 if (err || new == NULL /* duplicate */)
908 got_ref_close(ref);
909 return err;
913 if (ref_namespace) {
914 size_t len;
915 /* Canonicalize the path to eliminate double-slashes if any. */
916 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
917 err = got_error_from_errno("asprintf");
918 goto done;
920 len = strlen(abs_namespace) + 1;
921 buf = malloc(len);
922 if (buf == NULL) {
923 err = got_error_from_errno("malloc");
924 goto done;
926 err = got_canonpath(abs_namespace, buf, len);
927 if (err)
928 goto done;
929 ondisk_ref_namespace = buf;
930 while (ondisk_ref_namespace[0] == '/')
931 ondisk_ref_namespace++;
932 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
933 ondisk_ref_namespace += 5;
934 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
935 ondisk_ref_namespace = "";
938 /* Gather on-disk refs before parsing packed-refs. */
939 free(path_refs);
940 path_refs = get_refs_dir_path(repo, "");
941 if (path_refs == NULL) {
942 err = got_error_from_errno("get_refs_dir_path");
943 goto done;
945 err = gather_on_disk_refs(refs, path_refs,
946 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
947 cmp_cb, cmp_arg);
948 if (err)
949 goto done;
951 /*
952 * The packed-refs file may contain redundant entries, in which
953 * case on-disk refs take precedence.
954 */
955 packed_refs_path = got_repo_get_path_packed_refs(repo);
956 if (packed_refs_path == NULL) {
957 err = got_error_from_errno("got_repo_get_path_packed_refs");
958 goto done;
961 f = fopen(packed_refs_path, "r");
962 free(packed_refs_path);
963 if (f) {
964 char *line;
965 size_t len;
966 const char delim[3] = {'\0', '\0', '\0'};
967 for (;;) {
968 line = fparseln(f, &len, NULL, delim, 0);
969 if (line == NULL) {
970 if (feof(f))
971 break;
972 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
973 goto done;
975 err = parse_packed_ref_line(&ref, NULL, line);
976 free(line);
977 if (err)
978 goto done;
979 if (ref) {
980 if (ref_namespace) {
981 const char *name;
982 name = got_ref_get_name(ref);
983 if (!got_path_is_child(name,
984 ref_namespace,
985 strlen(ref_namespace))) {
986 got_ref_close(ref);
987 continue;
990 err = insert_ref(&new, refs, ref, repo,
991 cmp_cb, cmp_arg);
992 if (err || new == NULL /* duplicate */)
993 got_ref_close(ref);
994 if (err)
995 goto done;
999 done:
1000 free(abs_namespace);
1001 free(buf);
1002 free(path_refs);
1003 if (f && fclose(f) != 0 && err == NULL)
1004 err = got_error_from_errno("fclose");
1005 return err;
1008 void
1009 got_ref_list_free(struct got_reflist_head *refs)
1011 struct got_reflist_entry *re;
1013 while (!SIMPLEQ_EMPTY(refs)) {
1014 re = SIMPLEQ_FIRST(refs);
1015 SIMPLEQ_REMOVE_HEAD(refs, entry);
1016 got_ref_close(re->ref);
1017 free(re);
1022 int
1023 got_ref_is_symbolic(struct got_reference *ref)
1025 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1028 const struct got_error *
1029 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1031 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1032 return got_error(GOT_ERR_BAD_REF_TYPE);
1034 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1035 return NULL;
1038 const struct got_error *
1039 got_ref_change_symref(struct got_reference *ref, const char *refname)
1041 char *new_name;
1043 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1044 return got_error(GOT_ERR_BAD_REF_TYPE);
1046 new_name = strdup(refname);
1047 if (new_name == NULL)
1048 return got_error_from_errno("strdup");
1050 free(ref->ref.symref.ref);
1051 ref->ref.symref.ref = new_name;
1052 return NULL;
1055 const struct got_error *
1056 got_ref_change_symref_to_ref(struct got_reference *symref,
1057 struct got_object_id *id)
1059 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1060 return got_error(GOT_ERR_BAD_REF_TYPE);
1062 symref->ref.ref.name = symref->ref.symref.name;
1063 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1064 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1065 return NULL;
1068 const struct got_error *
1069 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1071 const struct got_error *err = NULL, *unlock_err = NULL;
1072 const char *name = got_ref_get_name(ref);
1073 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1074 struct got_lockfile *lf = NULL;
1075 FILE *f = NULL;
1076 size_t n;
1077 struct stat sb;
1079 path_refs = get_refs_dir_path(repo, name);
1080 if (path_refs == NULL) {
1081 err = got_error_from_errno2("get_refs_dir_path", name);
1082 goto done;
1085 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1086 err = got_error_from_errno("asprintf");
1087 goto done;
1090 err = got_opentemp_named(&tmppath, &f, path);
1091 if (err) {
1092 char *parent;
1093 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1094 goto done;
1095 err = got_path_dirname(&parent, path);
1096 if (err)
1097 goto done;
1098 err = got_path_mkdir(parent);
1099 free(parent);
1100 if (err)
1101 goto done;
1102 err = got_opentemp_named(&tmppath, &f, path);
1103 if (err)
1104 goto done;
1107 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1108 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1109 if (n != strlen(ref->ref.symref.ref) + 6) {
1110 err = got_ferror(f, GOT_ERR_IO);
1111 goto done;
1113 } else {
1114 char hex[SHA1_DIGEST_STRING_LENGTH];
1115 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1116 sizeof(hex)) == NULL) {
1117 err = got_error(GOT_ERR_BAD_REF_DATA);
1118 goto done;
1120 n = fprintf(f, "%s\n", hex);
1121 if (n != sizeof(hex)) {
1122 err = got_ferror(f, GOT_ERR_IO);
1123 goto done;
1127 if (ref->lf == NULL) {
1128 err = got_lockfile_lock(&lf, path);
1129 if (err)
1130 goto done;
1133 /* XXX: check if old content matches our expectations? */
1135 if (stat(path, &sb) != 0) {
1136 if (errno != ENOENT) {
1137 err = got_error_from_errno2("stat", path);
1138 goto done;
1140 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1143 if (fchmod(fileno(f), sb.st_mode) != 0) {
1144 err = got_error_from_errno2("fchmod", tmppath);
1145 goto done;
1148 if (rename(tmppath, path) != 0) {
1149 err = got_error_from_errno3("rename", tmppath, path);
1150 goto done;
1152 free(tmppath);
1153 tmppath = NULL;
1154 done:
1155 if (ref->lf == NULL && lf)
1156 unlock_err = got_lockfile_unlock(lf);
1157 if (f) {
1158 if (fclose(f) != 0 && err == NULL)
1159 err = got_error_from_errno("fclose");
1161 free(path_refs);
1162 free(path);
1163 if (tmppath) {
1164 if (unlink(tmppath) != 0 && err == NULL)
1165 err = got_error_from_errno2("unlink", tmppath);
1166 free(tmppath);
1168 return err ? err : unlock_err;
1171 static const struct got_error *
1172 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1174 const struct got_error *err = NULL, *unlock_err = NULL;
1175 struct got_lockfile *lf = NULL;
1176 FILE *f = NULL, *tmpf = NULL;
1177 char *packed_refs_path, *tmppath = NULL;
1178 struct got_reflist_head refs;
1179 int found_delref = 0;
1181 /* The packed-refs file does not cotain symbolic references. */
1182 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1183 return got_error(GOT_ERR_BAD_REF_DATA);
1185 SIMPLEQ_INIT(&refs);
1187 packed_refs_path = got_repo_get_path_packed_refs(repo);
1188 if (packed_refs_path == NULL)
1189 return got_error_from_errno("got_repo_get_path_packed_refs");
1191 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1192 if (err)
1193 goto done;
1195 if (delref->lf == NULL) {
1196 err = got_lockfile_lock(&lf, packed_refs_path);
1197 if (err)
1198 goto done;
1201 f = fopen(packed_refs_path, "r");
1202 if (f == NULL) {
1203 err = got_error_from_errno2("fopen", packed_refs_path);
1204 goto done;
1206 for (;;) {
1207 char *line;
1208 size_t len;
1209 const char delim[3] = {'\0', '\0', '\0'};
1210 struct got_reference *ref;
1211 struct got_reflist_entry *new;
1213 line = fparseln(f, &len, NULL, delim, 0);
1214 if (line == NULL) {
1215 if (feof(f))
1216 break;
1217 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1218 goto done;
1220 err = parse_packed_ref_line(&ref, NULL, line);
1221 free(line);
1222 if (err)
1223 goto done;
1224 if (ref == NULL)
1225 continue;
1227 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1228 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1229 sizeof(delref->ref.ref.sha1)) == 0) {
1230 found_delref = 1;
1231 got_ref_close(ref);
1232 continue;
1235 err = insert_ref(&new, &refs, ref, repo,
1236 got_ref_cmp_by_name, NULL);
1237 if (err || new == NULL /* duplicate */)
1238 got_ref_close(ref);
1239 if (err)
1240 goto done;
1243 if (found_delref) {
1244 struct got_reflist_entry *re;
1245 size_t n;
1246 struct stat sb;
1248 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1249 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1250 err = got_ferror(f, GOT_ERR_IO);
1251 goto done;
1254 SIMPLEQ_FOREACH(re, &refs, entry) {
1255 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1257 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1258 sizeof(hex)) == NULL) {
1259 err = got_error(GOT_ERR_BAD_REF_DATA);
1260 goto done;
1262 n = fprintf(tmpf, "%s ", hex);
1263 if (n != sizeof(hex)) {
1264 err = got_ferror(f, GOT_ERR_IO);
1265 goto done;
1267 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1268 if (n != strlen(re->ref->ref.ref.name) + 1) {
1269 err = got_ferror(f, GOT_ERR_IO);
1270 goto done;
1274 if (fflush(tmpf) != 0) {
1275 err = got_error_from_errno("fflush");
1276 goto done;
1279 if (fstat(fileno(f), &sb) != 0) {
1280 if (errno != ENOENT) {
1281 err = got_error_from_errno2("fstat",
1282 packed_refs_path);
1283 goto done;
1285 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1288 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1289 err = got_error_from_errno2("fchmod", tmppath);
1290 goto done;
1293 if (rename(tmppath, packed_refs_path) != 0) {
1294 err = got_error_from_errno3("rename", tmppath,
1295 packed_refs_path);
1296 goto done;
1299 done:
1300 if (delref->lf == NULL && lf)
1301 unlock_err = got_lockfile_unlock(lf);
1302 if (f) {
1303 if (fclose(f) != 0 && err == NULL)
1304 err = got_error_from_errno("fclose");
1306 if (tmpf) {
1307 unlink(tmppath);
1308 if (fclose(tmpf) != 0 && err == NULL)
1309 err = got_error_from_errno("fclose");
1311 free(tmppath);
1312 free(packed_refs_path);
1313 got_ref_list_free(&refs);
1314 return err ? err : unlock_err;
1317 static const struct got_error *
1318 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1320 const struct got_error *err = NULL, *unlock_err = NULL;
1321 const char *name = got_ref_get_name(ref);
1322 char *path_refs = NULL, *path = NULL;
1323 struct got_lockfile *lf = NULL;
1325 path_refs = get_refs_dir_path(repo, name);
1326 if (path_refs == NULL) {
1327 err = got_error_from_errno2("get_refs_dir_path", name);
1328 goto done;
1331 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1332 err = got_error_from_errno("asprintf");
1333 goto done;
1336 if (ref->lf == NULL) {
1337 err = got_lockfile_lock(&lf, path);
1338 if (err)
1339 goto done;
1342 /* XXX: check if old content matches our expectations? */
1344 if (unlink(path) != 0)
1345 err = got_error_from_errno2("unlink", path);
1346 done:
1347 if (ref->lf == NULL && lf)
1348 unlock_err = got_lockfile_unlock(lf);
1350 free(path_refs);
1351 free(path);
1352 return err ? err : unlock_err;
1355 const struct got_error *
1356 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1358 const struct got_error *err = NULL;
1359 struct got_reference *ref2;
1361 if (ref->flags & GOT_REF_IS_PACKED) {
1362 err = delete_packed_ref(ref, repo);
1363 if (err)
1364 return err;
1366 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1367 if (err) {
1368 if (err->code == GOT_ERR_NOT_REF)
1369 return NULL;
1370 return err;
1373 err = delete_loose_ref(ref2, repo);
1374 got_ref_close(ref2);
1375 return err;
1376 } else {
1377 err = delete_loose_ref(ref, repo);
1378 if (err)
1379 return err;
1381 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1382 if (err) {
1383 if (err->code == GOT_ERR_NOT_REF)
1384 return NULL;
1385 return err;
1388 err = delete_packed_ref(ref2, repo);
1389 got_ref_close(ref2);
1390 return err;
1394 const struct got_error *
1395 got_ref_unlock(struct got_reference *ref)
1397 const struct got_error *err;
1398 err = got_lockfile_unlock(ref->lf);
1399 ref->lf = NULL;
1400 return err;