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 <util.h>
30 #include <zlib.h>
31 #include <time.h>
32 #include <libgen.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_opentemp.h"
39 #include "got_path.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_lockfile.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 #endif
51 #define GOT_REF_HEADS "heads"
52 #define GOT_REF_TAGS "tags"
53 #define GOT_REF_REMOTES "remotes"
55 /*
56 * We do not resolve tags yet, and don't yet care about sorting refs either,
57 * so packed-refs files we write contain a minimal header which disables all
58 * packed-refs "traits" supported by Git.
59 */
60 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
62 /* A symbolic reference. */
63 struct got_symref {
64 char *name;
65 char *ref;
66 };
68 #define GOT_REF_RECURSE_MAX 20
70 /* A non-symbolic reference (there is no better designation). */
71 struct got_ref {
72 char *name;
73 u_int8_t sha1[SHA1_DIGEST_LENGTH];
74 };
76 /* A reference which points to an arbitrary object. */
77 struct got_reference {
78 unsigned int flags;
79 #define GOT_REF_IS_SYMBOLIC 0x01
80 #define GOT_REF_IS_PACKED 0x02
82 union {
83 struct got_ref ref;
84 struct got_symref symref;
85 } ref;
87 struct got_lockfile *lf;
88 };
90 static const struct got_error *
91 alloc_ref(struct got_reference **ref, const char *name,
92 struct got_object_id *id, int flags)
93 {
94 const struct got_error *err = NULL;
96 *ref = calloc(1, sizeof(**ref));
97 if (*ref == NULL)
98 return got_error_from_errno("calloc");
100 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
101 (*ref)->flags = flags;
102 (*ref)->ref.ref.name = strdup(name);
103 if ((*ref)->ref.ref.name == NULL) {
104 err = got_error_from_errno("strdup");
105 got_ref_close(*ref);
106 *ref = NULL;
108 return err;
111 static const struct got_error *
112 alloc_symref(struct got_reference **ref, const char *name,
113 const char *target_ref, int flags)
115 const struct got_error *err = NULL;
117 *ref = calloc(1, sizeof(**ref));
118 if (*ref == NULL)
119 return got_error_from_errno("calloc");
121 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
122 (*ref)->ref.symref.name = strdup(name);
123 if ((*ref)->ref.symref.name == NULL) {
124 err = got_error_from_errno("strdup");
125 got_ref_close(*ref);
126 *ref = NULL;
127 return err;
129 (*ref)->ref.symref.ref = strdup(target_ref);
130 if ((*ref)->ref.symref.ref == NULL) {
131 err = got_error_from_errno("strdup");
132 got_ref_close(*ref);
133 *ref = NULL;
135 return err;
138 static const struct got_error *
139 parse_symref(struct got_reference **ref, const char *name, const char *line)
141 if (line[0] == '\0')
142 return got_error(GOT_ERR_BAD_REF_DATA);
144 return alloc_symref(ref, name, line, 0);
147 static const struct got_error *
148 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
150 struct got_object_id id;
152 if (strncmp(line, "ref: ", 5) == 0) {
153 line += 5;
154 return parse_symref(ref, name, line);
157 if (!got_parse_sha1_digest(id.sha1, line))
158 return got_error(GOT_ERR_BAD_REF_DATA);
160 return alloc_ref(ref, name, &id, 0);
163 static const struct got_error *
164 parse_ref_file(struct got_reference **ref, const char *name,
165 const char *abspath, int lock)
167 const struct got_error *err = NULL;
168 FILE *f;
169 char *line = NULL;
170 size_t linesize = 0;
171 ssize_t linelen;
172 struct got_lockfile *lf = NULL;
174 if (lock) {
175 err = got_lockfile_lock(&lf, abspath);
176 if (err)
177 return (err);
180 f = fopen(abspath, "rb");
181 if (f == NULL) {
182 if (lock)
183 got_lockfile_unlock(lf);
184 return NULL;
187 linelen = getline(&line, &linesize, f);
188 if (linelen == -1) {
189 if (feof(f))
190 err = NULL; /* ignore empty files (could be locks) */
191 else
192 err = got_error_from_errno2("getline", abspath);
193 if (lock)
194 got_lockfile_unlock(lf);
195 goto done;
197 while (linelen > 0 && line[linelen - 1] == '\n') {
198 line[linelen - 1] = '\0';
199 linelen--;
202 err = parse_ref_line(ref, name, line);
203 if (lock) {
204 if (err)
205 got_lockfile_unlock(lf);
206 else {
207 if (*ref)
208 (*ref)->lf = lf;
209 else
210 got_lockfile_unlock(lf);
213 done:
214 free(line);
215 if (fclose(f) != 0 && err == NULL) {
216 err = got_error_from_errno("fclose");
217 if (*ref) {
218 if (lock)
219 got_ref_unlock(*ref);
220 got_ref_close(*ref);
221 *ref = NULL;
224 return err;
227 static int
228 is_well_known_ref(const char *refname)
230 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
231 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
232 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
233 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
236 static char *
237 get_refs_dir_path(struct got_repository *repo, const char *refname)
239 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
240 return strdup(got_repo_get_path_git_dir(repo));
242 return got_repo_get_path_refs(repo);
245 static int
246 is_valid_ref_name(const char *name)
248 const char *s, *seg;
249 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
250 const char *forbidden_seq[] = { "//", "..", "@{" };
251 const char *lfs = GOT_LOCKFILE_SUFFIX;
252 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
253 int i;
255 if (name[0] == '@' && name[1] == '\0')
256 return 0;
258 s = name;
259 seg = s;
260 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
261 return 0;
262 while (*s) {
263 for (i = 0; i < nitems(forbidden); i++) {
264 if (*s == forbidden[i])
265 return 0;
267 for (i = 0; i < nitems(forbidden_seq); i++) {
268 if (s[0] == forbidden_seq[i][0] &&
269 s[1] == forbidden_seq[i][1])
270 return 0;
272 if (iscntrl((unsigned char)s[0]))
273 return 0;
274 if (s[0] == '.' && s[1] == '\0')
275 return 0;
276 if (*s == '/') {
277 const char *nextseg = s + 1;
278 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
279 nextseg[0] == '/')
280 return 0;
281 if (seg <= s - lfs_len &&
282 strncmp(s - lfs_len, lfs, lfs_len) == 0)
283 return 0;
284 seg = nextseg;
286 s++;
289 if (seg <= s - lfs_len &&
290 strncmp(s - lfs_len, lfs, lfs_len) == 0)
291 return 0;
293 return 1;
296 const struct got_error *
297 got_ref_alloc(struct got_reference **ref, const char *name,
298 struct got_object_id *id)
300 if (!is_valid_ref_name(name))
301 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
303 return alloc_ref(ref, name, id, 0);
306 const struct got_error *
307 got_ref_alloc_symref(struct got_reference **ref, const char *name,
308 struct got_reference *target_ref)
310 if (!is_valid_ref_name(name))
311 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
313 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
316 static const struct got_error *
317 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
318 const char *line)
320 struct got_object_id id;
321 const char *name;
323 *ref = NULL;
325 if (line[0] == '#' || line[0] == '^')
326 return NULL;
328 if (!got_parse_sha1_digest(id.sha1, line))
329 return got_error(GOT_ERR_BAD_REF_DATA);
331 if (abs_refname) {
332 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
333 return NULL;
334 name = abs_refname;
335 } else
336 name = line + SHA1_DIGEST_STRING_LENGTH;
338 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
341 static const struct got_error *
342 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
343 int nsubdirs, const char *refname)
345 const struct got_error *err = NULL;
346 char *abs_refname;
347 char *line;
348 size_t len;
349 const char delim[3] = {'\0', '\0', '\0'};
350 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
352 *ref = NULL;
354 if (ref_is_absolute)
355 abs_refname = (char *)refname;
356 do {
357 line = fparseln(f, &len, NULL, delim, 0);
358 if (line == NULL) {
359 if (feof(f))
360 break;
361 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
362 break;
364 for (i = 0; i < nsubdirs; i++) {
365 if (!ref_is_absolute &&
366 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
367 refname) == -1)
368 return got_error_from_errno("asprintf");
369 err = parse_packed_ref_line(ref, abs_refname, line);
370 if (!ref_is_absolute)
371 free(abs_refname);
372 if (err || *ref != NULL)
373 break;
375 free(line);
376 if (err)
377 break;
378 } while (*ref == NULL);
380 return err;
383 static const struct got_error *
384 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
385 const char *name, int lock)
387 const struct got_error *err = NULL;
388 char *path = NULL;
389 char *absname = NULL;
390 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
391 int ref_is_well_known = is_well_known_ref(name);
393 *ref = NULL;
395 if (ref_is_absolute || ref_is_well_known) {
396 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
397 return got_error_from_errno("asprintf");
398 absname = (char *)name;
399 } else {
400 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
401 subdir[0] ? "/" : "", name) == -1)
402 return got_error_from_errno("asprintf");
404 if (asprintf(&absname, "refs/%s%s%s",
405 subdir, subdir[0] ? "/" : "", name) == -1) {
406 err = got_error_from_errno("asprintf");
407 goto done;
411 err = parse_ref_file(ref, absname, path, lock);
412 done:
413 if (!ref_is_absolute && !ref_is_well_known)
414 free(absname);
415 free(path);
416 return err;
419 const struct got_error *
420 got_ref_open(struct got_reference **ref, struct got_repository *repo,
421 const char *refname, int lock)
423 const struct got_error *err = NULL;
424 char *path_refs = NULL;
425 const char *subdirs[] = {
426 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
427 };
428 int i, well_known = is_well_known_ref(refname);
429 struct got_lockfile *lf = NULL;
431 *ref = NULL;
433 path_refs = get_refs_dir_path(repo, refname);
434 if (path_refs == NULL) {
435 err = got_error_from_errno2("get_refs_dir_path", refname);
436 goto done;
439 if (well_known) {
440 err = open_ref(ref, path_refs, "", refname, lock);
441 } else {
442 char *packed_refs_path;
443 FILE *f;
445 /* Search on-disk refs before packed refs! */
446 for (i = 0; i < nitems(subdirs); i++) {
447 err = open_ref(ref, path_refs, subdirs[i], refname,
448 lock);
449 if (err || *ref)
450 goto done;
453 packed_refs_path = got_repo_get_path_packed_refs(repo);
454 if (packed_refs_path == NULL) {
455 err = got_error_from_errno(
456 "got_repo_get_path_packed_refs");
457 goto done;
460 if (lock) {
461 err = got_lockfile_lock(&lf, packed_refs_path);
462 if (err)
463 goto done;
465 f = fopen(packed_refs_path, "rb");
466 free(packed_refs_path);
467 if (f != NULL) {
468 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
469 refname);
470 if (!err) {
471 if (fclose(f) != 0) {
472 err = got_error_from_errno("fclose");
473 got_ref_close(*ref);
474 *ref = NULL;
475 } else if (*ref)
476 (*ref)->lf = lf;
480 done:
481 if (!err && *ref == NULL)
482 err = got_error_not_ref(refname);
483 if (err && lf)
484 got_lockfile_unlock(lf);
485 free(path_refs);
486 return err;
489 void
490 got_ref_close(struct got_reference *ref)
492 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
493 free(ref->ref.symref.name);
494 free(ref->ref.symref.ref);
495 } else
496 free(ref->ref.ref.name);
497 free(ref);
500 struct got_reference *
501 got_ref_dup(struct got_reference *ref)
503 struct got_reference *ret;
505 ret = calloc(1, sizeof(*ret));
506 if (ret == NULL)
507 return NULL;
509 ret->flags = ref->flags;
510 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
511 ret->ref.symref.name = strdup(ref->ref.symref.name);
512 if (ret->ref.symref.name == NULL) {
513 free(ret);
514 return NULL;
516 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
517 if (ret->ref.symref.ref == NULL) {
518 free(ret->ref.symref.name);
519 free(ret);
520 return NULL;
522 } else {
523 ref->ref.ref.name = strdup(ref->ref.ref.name);
524 if (ref->ref.ref.name == NULL) {
525 free(ret);
526 return NULL;
528 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
529 sizeof(ret->ref.ref.sha1));
532 return ret;
535 const struct got_error *
536 got_reflist_entry_dup(struct got_reflist_entry **newp,
537 struct got_reflist_entry *re)
539 const struct got_error *err = NULL;
540 struct got_reflist_entry *new;
542 *newp = NULL;
544 new = malloc(sizeof(*new));
545 if (new == NULL)
546 return got_error_from_errno("malloc");
548 new->ref = got_ref_dup(re->ref);
549 if (new->ref == NULL) {
550 err = got_error_from_errno("got_ref_dup");
551 free(new);
552 return err;
555 new->id = got_object_id_dup(re->id);
556 if (new->id == NULL) {
557 err = got_error_from_errno("got_ref_dup");
558 free(new->id);
559 free(new);
560 return err;
563 *newp = new;
564 return NULL;
567 static const struct got_error *
568 resolve_symbolic_ref(struct got_reference **resolved,
569 struct got_repository *repo, struct got_reference *ref)
571 struct got_reference *nextref;
572 const struct got_error *err;
574 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
575 if (err)
576 return err;
578 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
579 err = resolve_symbolic_ref(resolved, repo, nextref);
580 else
581 *resolved = got_ref_dup(nextref);
583 got_ref_close(nextref);
584 return err;
587 static const struct got_error *
588 ref_resolve(struct got_object_id **id, struct got_repository *repo,
589 struct got_reference *ref, int recursion)
591 const struct got_error *err;
593 if (recursion <= 0)
594 return got_error_msg(GOT_ERR_RECURSION,
595 "reference recursion limit reached");
597 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
598 struct got_reference *resolved = NULL;
599 err = resolve_symbolic_ref(&resolved, repo, ref);
600 if (err == NULL)
601 err = ref_resolve(id, repo, resolved, --recursion);
602 if (resolved)
603 got_ref_close(resolved);
604 return err;
607 *id = calloc(1, sizeof(**id));
608 if (*id == NULL)
609 return got_error_from_errno("calloc");
610 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
611 return NULL;
614 const struct got_error *
615 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
616 struct got_reference *ref)
618 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
621 char *
622 got_ref_to_str(struct got_reference *ref)
624 char *str;
626 if (ref->flags & GOT_REF_IS_SYMBOLIC)
627 return strdup(ref->ref.symref.ref);
629 str = malloc(SHA1_DIGEST_STRING_LENGTH);
630 if (str == NULL)
631 return NULL;
633 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
634 SHA1_DIGEST_STRING_LENGTH) == NULL) {
635 free(str);
636 return NULL;
639 return str;
642 const char *
643 got_ref_get_name(struct got_reference *ref)
645 if (ref->flags & GOT_REF_IS_SYMBOLIC)
646 return ref->ref.symref.name;
648 return ref->ref.ref.name;
651 const char *
652 got_ref_get_symref_target(struct got_reference *ref)
654 if (ref->flags & GOT_REF_IS_SYMBOLIC)
655 return ref->ref.symref.ref;
657 return NULL;
660 const struct got_error *
661 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
662 struct got_reference* re2)
664 const char *name1 = got_ref_get_name(re1);
665 const char *name2 = got_ref_get_name(re2);
667 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
668 return NULL;
671 const struct got_error *
672 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
673 struct got_reference *ref2)
675 const struct got_error *err = NULL;
676 struct got_repository *repo = arg;
677 struct got_object_id *id1, *id2 = NULL;
678 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
679 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
680 time_t time1, time2;
682 *cmp = 0;
684 err = got_ref_resolve(&id1, repo, ref1);
685 if (err)
686 return err;
687 err = got_object_open_as_tag(&tag1, repo, id1);
688 if (err) {
689 if (err->code != GOT_ERR_OBJ_TYPE)
690 goto done;
691 /* "lightweight" tag */
692 err = got_object_open_as_commit(&commit1, repo, id1);
693 if (err)
694 goto done;
695 time1 = got_object_commit_get_committer_time(commit1);
696 } else
697 time1 = got_object_tag_get_tagger_time(tag1);
699 err = got_ref_resolve(&id2, repo, ref2);
700 if (err)
701 goto done;
702 err = got_object_open_as_tag(&tag2, repo, id2);
703 if (err) {
704 if (err->code != GOT_ERR_OBJ_TYPE)
705 goto done;
706 /* "lightweight" tag */
707 err = got_object_open_as_commit(&commit2, repo, id2);
708 if (err)
709 goto done;
710 time2 = got_object_commit_get_committer_time(commit2);
711 } else
712 time2 = got_object_tag_get_tagger_time(tag2);
714 /* Put latest tags first. */
715 if (time1 < time2)
716 *cmp = 1;
717 else if (time1 > time2)
718 *cmp = -1;
719 else
720 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
721 done:
722 free(id1);
723 free(id2);
724 if (tag1)
725 got_object_tag_close(tag1);
726 if (tag2)
727 got_object_tag_close(tag2);
728 if (commit1)
729 got_object_commit_close(commit1);
730 if (commit2)
731 got_object_commit_close(commit2);
732 return err;
735 static const struct got_error *
736 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
737 struct got_reference *ref, struct got_repository *repo,
738 got_ref_cmp_cb cmp_cb, void *cmp_arg)
740 const struct got_error *err;
741 struct got_object_id *id;
742 struct got_reflist_entry *new, *re, *prev = NULL;
743 int cmp;
745 *newp = NULL;
747 err = got_ref_resolve(&id, repo, ref);
748 if (err)
749 return err;
751 new = malloc(sizeof(*new));
752 if (new == NULL) {
753 free(id);
754 return got_error_from_errno("malloc");
756 new->ref = ref;
757 new->id = id;
758 *newp = new;
760 /*
761 * We must de-duplicate entries on insert because packed-refs may
762 * contain redundant entries. On-disk refs take precedence.
763 * This code assumes that on-disk revs are read before packed-refs.
764 * We're iterating the list anyway, so insert elements sorted by name.
765 */
766 re = SIMPLEQ_FIRST(refs);
767 while (re) {
768 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
769 if (err)
770 return err;
771 if (cmp == 0) {
772 /* duplicate */
773 free(new->id);
774 free(new);
775 *newp = NULL;
776 return NULL;
777 } else if (cmp > 0) {
778 if (prev)
779 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
780 else
781 SIMPLEQ_INSERT_HEAD(refs, new, entry);
782 return NULL;
783 } else {
784 prev = re;
785 re = SIMPLEQ_NEXT(re, entry);
789 SIMPLEQ_INSERT_TAIL(refs, new, entry);
790 return NULL;
793 static const struct got_error *
794 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
795 const char *subdir, struct got_repository *repo,
796 got_ref_cmp_cb cmp_cb, void *cmp_arg)
798 const struct got_error *err = NULL;
799 DIR *d = NULL;
800 char *path_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;
814 dent = readdir(d);
815 if (dent == NULL)
816 break;
818 if (strcmp(dent->d_name, ".") == 0 ||
819 strcmp(dent->d_name, "..") == 0)
820 continue;
822 switch (dent->d_type) {
823 case DT_REG:
824 err = open_ref(&ref, path_refs, subdir, dent->d_name,
825 0);
826 if (err)
827 goto done;
828 if (ref) {
829 struct got_reflist_entry *new;
830 err = insert_ref(&new, refs, ref, repo,
831 cmp_cb, cmp_arg);
832 if (err || new == NULL /* duplicate */)
833 got_ref_close(ref);
834 if (err)
835 goto done;
837 break;
838 case DT_DIR:
839 if (asprintf(&child, "%s%s%s", subdir,
840 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
841 err = got_error_from_errno("asprintf");
842 break;
844 err = gather_on_disk_refs(refs, path_refs, child, repo,
845 cmp_cb, cmp_arg);
846 free(child);
847 break;
848 default:
849 break;
852 done:
853 if (d)
854 closedir(d);
855 free(path_subdir);
856 return err;
859 const struct got_error *
860 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
861 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
863 const struct got_error *err;
864 char *packed_refs_path, *path_refs = NULL;
865 const char *ondisk_ref_namespace = NULL;
866 FILE *f = NULL;
867 struct got_reference *ref;
868 struct got_reflist_entry *new;
870 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
871 /* HEAD ref should always exist. */
872 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
873 if (path_refs == NULL) {
874 err = got_error_from_errno("get_refs_dir_path");
875 goto done;
877 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
878 if (err)
879 goto done;
880 err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
881 if (err || new == NULL /* duplicate */)
882 got_ref_close(ref);
883 if (err)
884 goto done;
887 ondisk_ref_namespace = ref_namespace;
888 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
889 ondisk_ref_namespace += 5;
891 /* Gather on-disk refs before parsing packed-refs. */
892 free(path_refs);
893 path_refs = get_refs_dir_path(repo, "");
894 if (path_refs == NULL) {
895 err = got_error_from_errno("get_refs_dir_path");
896 goto done;
898 err = gather_on_disk_refs(refs, path_refs,
899 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
900 cmp_cb, cmp_arg);
901 if (err)
902 goto done;
904 /*
905 * The packed-refs file may contain redundant entries, in which
906 * case on-disk refs take precedence.
907 */
908 packed_refs_path = got_repo_get_path_packed_refs(repo);
909 if (packed_refs_path == NULL) {
910 err = got_error_from_errno("got_repo_get_path_packed_refs");
911 goto done;
914 f = fopen(packed_refs_path, "r");
915 free(packed_refs_path);
916 if (f) {
917 char *line;
918 size_t len;
919 const char delim[3] = {'\0', '\0', '\0'};
920 for (;;) {
921 line = fparseln(f, &len, NULL, delim, 0);
922 if (line == NULL) {
923 if (feof(f))
924 break;
925 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
926 goto done;
928 err = parse_packed_ref_line(&ref, NULL, line);
929 free(line);
930 if (err)
931 goto done;
932 if (ref) {
933 if (ref_namespace) {
934 const char *name;
935 name = got_ref_get_name(ref);
936 if (strncmp(name, ref_namespace,
937 strlen(ref_namespace)) != 0) {
938 got_ref_close(ref);
939 continue;
942 err = insert_ref(&new, refs, ref, repo,
943 cmp_cb, cmp_arg);
944 if (err || new == NULL /* duplicate */)
945 got_ref_close(ref);
946 if (err)
947 goto done;
951 done:
952 free(path_refs);
953 if (f && fclose(f) != 0 && err == NULL)
954 err = got_error_from_errno("fclose");
955 return err;
958 void
959 got_ref_list_free(struct got_reflist_head *refs)
961 struct got_reflist_entry *re;
963 while (!SIMPLEQ_EMPTY(refs)) {
964 re = SIMPLEQ_FIRST(refs);
965 SIMPLEQ_REMOVE_HEAD(refs, entry);
966 got_ref_close(re->ref);
967 free(re->id);
968 free(re);
973 int
974 got_ref_is_symbolic(struct got_reference *ref)
976 return (ref->flags & GOT_REF_IS_SYMBOLIC);
979 const struct got_error *
980 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
982 if (ref->flags & GOT_REF_IS_SYMBOLIC)
983 return got_error(GOT_ERR_BAD_REF_TYPE);
985 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
986 return NULL;
989 const struct got_error *
990 got_ref_change_symref(struct got_reference *ref, char *refname)
992 char *new_name;
994 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
995 return got_error(GOT_ERR_BAD_REF_TYPE);
997 new_name = strdup(refname);
998 if (new_name == NULL)
999 return got_error_from_errno("strdup");
1001 free(ref->ref.symref.name);
1002 ref->ref.symref.name = new_name;
1003 return NULL;
1006 const struct got_error *
1007 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1009 const struct got_error *err = NULL, *unlock_err = NULL;
1010 const char *name = got_ref_get_name(ref);
1011 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1012 struct got_lockfile *lf = NULL;
1013 FILE *f = NULL;
1014 size_t n;
1015 struct stat sb;
1017 path_refs = get_refs_dir_path(repo, name);
1018 if (path_refs == NULL) {
1019 err = got_error_from_errno2("get_refs_dir_path", name);
1020 goto done;
1023 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1024 err = got_error_from_errno("asprintf");
1025 goto done;
1028 err = got_opentemp_named(&tmppath, &f, path);
1029 if (err) {
1030 char *parent;
1031 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1032 goto done;
1033 err = got_path_dirname(&parent, path);
1034 if (err)
1035 goto done;
1036 err = got_path_mkdir(parent);
1037 free(parent);
1038 if (err)
1039 goto done;
1040 err = got_opentemp_named(&tmppath, &f, path);
1041 if (err)
1042 goto done;
1045 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1046 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1047 if (n != strlen(ref->ref.symref.ref) + 6) {
1048 err = got_ferror(f, GOT_ERR_IO);
1049 goto done;
1051 } else {
1052 char hex[SHA1_DIGEST_STRING_LENGTH];
1053 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1054 sizeof(hex)) == NULL) {
1055 err = got_error(GOT_ERR_BAD_REF_DATA);
1056 goto done;
1058 n = fprintf(f, "%s\n", hex);
1059 if (n != sizeof(hex)) {
1060 err = got_ferror(f, GOT_ERR_IO);
1061 goto done;
1065 if (ref->lf == NULL) {
1066 err = got_lockfile_lock(&lf, path);
1067 if (err)
1068 goto done;
1071 /* XXX: check if old content matches our expectations? */
1073 if (stat(path, &sb) != 0) {
1074 if (errno != ENOENT) {
1075 err = got_error_from_errno2("stat", path);
1076 goto done;
1078 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1081 if (rename(tmppath, path) != 0) {
1082 err = got_error_from_errno3("rename", tmppath, path);
1083 goto done;
1085 free(tmppath);
1086 tmppath = NULL;
1088 if (chmod(path, sb.st_mode) != 0) {
1089 err = got_error_from_errno2("chmod", path);
1090 goto done;
1092 done:
1093 if (ref->lf == NULL && lf)
1094 unlock_err = got_lockfile_unlock(lf);
1095 if (f) {
1096 if (fclose(f) != 0 && err == NULL)
1097 err = got_error_from_errno("fclose");
1099 free(path_refs);
1100 free(path);
1101 if (tmppath) {
1102 if (unlink(tmppath) != 0 && err == NULL)
1103 err = got_error_from_errno2("unlink", tmppath);
1104 free(tmppath);
1106 return err ? err : unlock_err;
1109 static const struct got_error *
1110 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1112 const struct got_error *err = NULL, *unlock_err = NULL;
1113 struct got_lockfile *lf = NULL;
1114 FILE *f = NULL, *tmpf = NULL;
1115 char *packed_refs_path, *tmppath = NULL;
1116 struct got_reflist_head refs;
1117 int found_delref = 0;
1119 /* The packed-refs file does not cotain symbolic references. */
1120 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1121 return got_error(GOT_ERR_BAD_REF_DATA);
1123 SIMPLEQ_INIT(&refs);
1125 packed_refs_path = got_repo_get_path_packed_refs(repo);
1126 if (packed_refs_path == NULL)
1127 return got_error_from_errno("got_repo_get_path_packed_refs");
1129 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1130 if (err)
1131 goto done;
1133 if (delref->lf == NULL) {
1134 err = got_lockfile_lock(&lf, packed_refs_path);
1135 if (err)
1136 goto done;
1139 f = fopen(packed_refs_path, "r");
1140 if (f == NULL) {
1141 err = got_error_from_errno2("fopen", packed_refs_path);
1142 goto done;
1144 for (;;) {
1145 char *line;
1146 size_t len;
1147 const char delim[3] = {'\0', '\0', '\0'};
1148 struct got_reference *ref;
1149 struct got_reflist_entry *new;
1151 line = fparseln(f, &len, NULL, delim, 0);
1152 if (line == NULL) {
1153 if (feof(f))
1154 break;
1155 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1156 goto done;
1158 err = parse_packed_ref_line(&ref, NULL, line);
1159 free(line);
1160 if (err)
1161 goto done;
1162 if (ref == NULL)
1163 continue;
1165 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1166 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1167 sizeof(delref->ref.ref.sha1)) == 0) {
1168 found_delref = 1;
1169 got_ref_close(ref);
1170 continue;
1173 err = insert_ref(&new, &refs, ref, repo,
1174 got_ref_cmp_by_name, NULL);
1175 if (err || new == NULL /* duplicate */)
1176 got_ref_close(ref);
1177 if (err)
1178 goto done;
1181 if (found_delref) {
1182 struct got_reflist_entry *re;
1183 size_t n;
1184 struct stat sb;
1186 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1187 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1188 err = got_ferror(f, GOT_ERR_IO);
1189 goto done;
1192 SIMPLEQ_FOREACH(re, &refs, entry) {
1193 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1195 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1196 sizeof(hex)) == NULL) {
1197 err = got_error(GOT_ERR_BAD_REF_DATA);
1198 goto done;
1200 n = fprintf(tmpf, "%s ", hex);
1201 if (n != sizeof(hex)) {
1202 err = got_ferror(f, GOT_ERR_IO);
1203 goto done;
1205 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1206 if (n != strlen(re->ref->ref.ref.name) + 1) {
1207 err = got_ferror(f, GOT_ERR_IO);
1208 goto done;
1212 if (fflush(tmpf) != 0) {
1213 err = got_error_from_errno("fflush");
1214 goto done;
1217 if (stat(packed_refs_path, &sb) != 0) {
1218 if (errno != ENOENT) {
1219 err = got_error_from_errno2("stat",
1220 packed_refs_path);
1221 goto done;
1223 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1226 if (rename(tmppath, packed_refs_path) != 0) {
1227 err = got_error_from_errno3("rename", tmppath,
1228 packed_refs_path);
1229 goto done;
1232 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1233 err = got_error_from_errno2("chmod",
1234 packed_refs_path);
1235 goto done;
1238 done:
1239 if (delref->lf == NULL && lf)
1240 unlock_err = got_lockfile_unlock(lf);
1241 if (f) {
1242 if (fclose(f) != 0 && err == NULL)
1243 err = got_error_from_errno("fclose");
1245 if (tmpf) {
1246 unlink(tmppath);
1247 if (fclose(tmpf) != 0 && err == NULL)
1248 err = got_error_from_errno("fclose");
1250 free(tmppath);
1251 free(packed_refs_path);
1252 got_ref_list_free(&refs);
1253 return err ? err : unlock_err;
1256 const struct got_error *
1257 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1259 const struct got_error *err = NULL, *unlock_err = NULL;
1260 const char *name = got_ref_get_name(ref);
1261 char *path_refs = NULL, *path = NULL;
1262 struct got_lockfile *lf = NULL;
1264 if (ref->flags & GOT_REF_IS_PACKED)
1265 return delete_packed_ref(ref, repo);
1267 path_refs = get_refs_dir_path(repo, name);
1268 if (path_refs == NULL) {
1269 err = got_error_from_errno2("get_refs_dir_path", name);
1270 goto done;
1273 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1274 err = got_error_from_errno("asprintf");
1275 goto done;
1278 if (ref->lf == NULL) {
1279 err = got_lockfile_lock(&lf, path);
1280 if (err)
1281 goto done;
1284 /* XXX: check if old content matches our expectations? */
1286 if (unlink(path) != 0)
1287 err = got_error_from_errno2("unlink", path);
1288 done:
1289 if (ref->lf == NULL && lf)
1290 unlock_err = got_lockfile_unlock(lf);
1292 free(path_refs);
1293 free(path);
1294 return err ? err : unlock_err;
1297 const struct got_error *
1298 got_ref_unlock(struct got_reference *ref)
1300 const struct got_error *err;
1301 err = got_lockfile_unlock(ref->lf);
1302 ref->lf = NULL;
1303 return err;