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 new->id = got_object_id_dup(re->id);
570 if (new->id == NULL) {
571 err = got_error_from_errno("got_ref_dup");
572 free(new->id);
573 free(new);
574 return err;
577 *newp = new;
578 return NULL;
581 static const struct got_error *
582 resolve_symbolic_ref(struct got_reference **resolved,
583 struct got_repository *repo, struct got_reference *ref)
585 struct got_reference *nextref;
586 const struct got_error *err;
588 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
589 if (err)
590 return err;
592 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
593 err = resolve_symbolic_ref(resolved, repo, nextref);
594 else
595 *resolved = got_ref_dup(nextref);
597 got_ref_close(nextref);
598 return err;
601 static const struct got_error *
602 ref_resolve(struct got_object_id **id, struct got_repository *repo,
603 struct got_reference *ref, int recursion)
605 const struct got_error *err;
607 if (recursion <= 0)
608 return got_error_msg(GOT_ERR_RECURSION,
609 "reference recursion limit reached");
611 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
612 struct got_reference *resolved = NULL;
613 err = resolve_symbolic_ref(&resolved, repo, ref);
614 if (err == NULL)
615 err = ref_resolve(id, repo, resolved, --recursion);
616 if (resolved)
617 got_ref_close(resolved);
618 return err;
621 *id = calloc(1, sizeof(**id));
622 if (*id == NULL)
623 return got_error_from_errno("calloc");
624 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
625 return NULL;
628 const struct got_error *
629 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
630 struct got_reference *ref)
632 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
635 char *
636 got_ref_to_str(struct got_reference *ref)
638 char *str;
640 if (ref->flags & GOT_REF_IS_SYMBOLIC)
641 return strdup(ref->ref.symref.ref);
643 str = malloc(SHA1_DIGEST_STRING_LENGTH);
644 if (str == NULL)
645 return NULL;
647 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
648 SHA1_DIGEST_STRING_LENGTH) == NULL) {
649 free(str);
650 return NULL;
653 return str;
656 const char *
657 got_ref_get_name(struct got_reference *ref)
659 if (ref->flags & GOT_REF_IS_SYMBOLIC)
660 return ref->ref.symref.name;
662 return ref->ref.ref.name;
665 const char *
666 got_ref_get_symref_target(struct got_reference *ref)
668 if (ref->flags & GOT_REF_IS_SYMBOLIC)
669 return ref->ref.symref.ref;
671 return NULL;
674 const struct got_error *
675 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
676 struct got_reference* re2)
678 const char *name1 = got_ref_get_name(re1);
679 const char *name2 = got_ref_get_name(re2);
681 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
682 return NULL;
685 const struct got_error *
686 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
687 struct got_reference *ref2)
689 const struct got_error *err = NULL;
690 struct got_repository *repo = arg;
691 struct got_object_id *id1, *id2 = NULL;
692 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
693 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
694 time_t time1, time2;
696 *cmp = 0;
698 err = got_ref_resolve(&id1, repo, ref1);
699 if (err)
700 return err;
701 err = got_object_open_as_tag(&tag1, repo, id1);
702 if (err) {
703 if (err->code != GOT_ERR_OBJ_TYPE)
704 goto done;
705 /* "lightweight" tag */
706 err = got_object_open_as_commit(&commit1, repo, id1);
707 if (err)
708 goto done;
709 time1 = got_object_commit_get_committer_time(commit1);
710 } else
711 time1 = got_object_tag_get_tagger_time(tag1);
713 err = got_ref_resolve(&id2, repo, ref2);
714 if (err)
715 goto done;
716 err = got_object_open_as_tag(&tag2, repo, id2);
717 if (err) {
718 if (err->code != GOT_ERR_OBJ_TYPE)
719 goto done;
720 /* "lightweight" tag */
721 err = got_object_open_as_commit(&commit2, repo, id2);
722 if (err)
723 goto done;
724 time2 = got_object_commit_get_committer_time(commit2);
725 } else
726 time2 = got_object_tag_get_tagger_time(tag2);
728 /* Put latest tags first. */
729 if (time1 < time2)
730 *cmp = 1;
731 else if (time1 > time2)
732 *cmp = -1;
733 else
734 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
735 done:
736 free(id1);
737 free(id2);
738 if (tag1)
739 got_object_tag_close(tag1);
740 if (tag2)
741 got_object_tag_close(tag2);
742 if (commit1)
743 got_object_commit_close(commit1);
744 if (commit2)
745 got_object_commit_close(commit2);
746 return err;
749 static const struct got_error *
750 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
751 struct got_reference *ref, struct got_repository *repo,
752 got_ref_cmp_cb cmp_cb, void *cmp_arg)
754 const struct got_error *err;
755 struct got_object_id *id;
756 struct got_reflist_entry *new, *re, *prev = NULL;
757 int cmp;
759 *newp = NULL;
761 err = got_ref_resolve(&id, repo, ref);
762 if (err)
763 return err;
765 new = malloc(sizeof(*new));
766 if (new == NULL) {
767 free(id);
768 return got_error_from_errno("malloc");
770 new->ref = ref;
771 new->id = id;
772 *newp = new;
774 /*
775 * We must de-duplicate entries on insert because packed-refs may
776 * contain redundant entries. On-disk refs take precedence.
777 * This code assumes that on-disk revs are read before packed-refs.
778 * We're iterating the list anyway, so insert elements sorted by name.
779 */
780 re = SIMPLEQ_FIRST(refs);
781 while (re) {
782 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
783 if (err)
784 return err;
785 if (cmp == 0) {
786 /* duplicate */
787 free(new->id);
788 free(new);
789 *newp = NULL;
790 return NULL;
791 } else if (cmp > 0) {
792 if (prev)
793 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
794 else
795 SIMPLEQ_INSERT_HEAD(refs, new, entry);
796 return NULL;
797 } else {
798 prev = re;
799 re = SIMPLEQ_NEXT(re, entry);
803 SIMPLEQ_INSERT_TAIL(refs, new, entry);
804 return NULL;
807 static const struct got_error *
808 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
809 const char *subdir, struct got_repository *repo,
810 got_ref_cmp_cb cmp_cb, void *cmp_arg)
812 const struct got_error *err = NULL;
813 DIR *d = NULL;
814 char *path_subdir;
816 while (subdir[0] == '/')
817 subdir++;
819 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
820 return got_error_from_errno("asprintf");
822 d = opendir(path_subdir);
823 if (d == NULL)
824 goto done;
826 for (;;) {
827 struct dirent *dent;
828 struct got_reference *ref;
829 char *child;
830 int type;
832 dent = readdir(d);
833 if (dent == NULL)
834 break;
836 if (strcmp(dent->d_name, ".") == 0 ||
837 strcmp(dent->d_name, "..") == 0)
838 continue;
840 err = got_path_dirent_type(&type, path_subdir, dent);
841 if (err)
842 break;
844 switch (type) {
845 case DT_REG:
846 err = open_ref(&ref, path_refs, subdir, dent->d_name,
847 0);
848 if (err)
849 goto done;
850 if (ref) {
851 struct got_reflist_entry *new;
852 err = insert_ref(&new, refs, ref, repo,
853 cmp_cb, cmp_arg);
854 if (err || new == NULL /* duplicate */)
855 got_ref_close(ref);
856 if (err)
857 goto done;
859 break;
860 case DT_DIR:
861 if (asprintf(&child, "%s%s%s", subdir,
862 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
863 err = got_error_from_errno("asprintf");
864 break;
866 err = gather_on_disk_refs(refs, path_refs, child, repo,
867 cmp_cb, cmp_arg);
868 free(child);
869 break;
870 default:
871 break;
874 done:
875 if (d)
876 closedir(d);
877 free(path_subdir);
878 return err;
881 const struct got_error *
882 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
883 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
885 const struct got_error *err;
886 char *packed_refs_path, *path_refs = NULL;
887 char *abs_namespace = NULL;
888 char *buf = NULL, *ondisk_ref_namespace = NULL;
889 FILE *f = NULL;
890 struct got_reference *ref;
891 struct got_reflist_entry *new;
893 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
894 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
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, "", GOT_REF_HEAD, 0);
900 if (err)
901 goto done;
902 err = insert_ref(&new, refs, ref, repo,
903 cmp_cb, cmp_arg);
904 if (err || new == NULL /* duplicate */)
905 got_ref_close(ref);
906 if (err && err->code != GOT_ERR_NOT_REF)
907 goto done;
908 } else {
909 /* Try listing a single reference. */
910 const char *refname = ref_namespace;
911 path_refs = get_refs_dir_path(repo, refname);
912 if (path_refs == NULL) {
913 err = got_error_from_errno("get_refs_dir_path");
914 goto done;
916 err = open_ref(&ref, path_refs, "", refname, 0);
917 if (err) {
918 if (err->code != GOT_ERR_NOT_REF)
919 goto done;
920 /* Try to look up references in a given namespace. */
921 } else {
922 err = insert_ref(&new, refs, ref, repo,
923 cmp_cb, cmp_arg);
924 if (err || new == NULL /* duplicate */)
925 got_ref_close(ref);
926 return err;
930 if (ref_namespace) {
931 size_t len;
932 /* Canonicalize the path to eliminate double-slashes if any. */
933 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
934 err = got_error_from_errno("asprintf");
935 goto done;
937 len = strlen(abs_namespace) + 1;
938 buf = malloc(len);
939 if (buf == NULL) {
940 err = got_error_from_errno("malloc");
941 goto done;
943 err = got_canonpath(abs_namespace, buf, len);
944 if (err)
945 goto done;
946 ondisk_ref_namespace = buf;
947 while (ondisk_ref_namespace[0] == '/')
948 ondisk_ref_namespace++;
949 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
950 ondisk_ref_namespace += 5;
951 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
952 ondisk_ref_namespace = "";
955 /* Gather on-disk refs before parsing packed-refs. */
956 free(path_refs);
957 path_refs = get_refs_dir_path(repo, "");
958 if (path_refs == NULL) {
959 err = got_error_from_errno("get_refs_dir_path");
960 goto done;
962 err = gather_on_disk_refs(refs, path_refs,
963 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
964 cmp_cb, cmp_arg);
965 if (err)
966 goto done;
968 /*
969 * The packed-refs file may contain redundant entries, in which
970 * case on-disk refs take precedence.
971 */
972 packed_refs_path = got_repo_get_path_packed_refs(repo);
973 if (packed_refs_path == NULL) {
974 err = got_error_from_errno("got_repo_get_path_packed_refs");
975 goto done;
978 f = fopen(packed_refs_path, "r");
979 free(packed_refs_path);
980 if (f) {
981 char *line;
982 size_t len;
983 const char delim[3] = {'\0', '\0', '\0'};
984 for (;;) {
985 line = fparseln(f, &len, NULL, delim, 0);
986 if (line == NULL) {
987 if (feof(f))
988 break;
989 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
990 goto done;
992 err = parse_packed_ref_line(&ref, NULL, line);
993 free(line);
994 if (err)
995 goto done;
996 if (ref) {
997 if (ref_namespace) {
998 const char *name;
999 name = got_ref_get_name(ref);
1000 if (!got_path_is_child(name,
1001 ref_namespace,
1002 strlen(ref_namespace))) {
1003 got_ref_close(ref);
1004 continue;
1007 err = insert_ref(&new, refs, ref, repo,
1008 cmp_cb, cmp_arg);
1009 if (err || new == NULL /* duplicate */)
1010 got_ref_close(ref);
1011 if (err)
1012 goto done;
1016 done:
1017 free(abs_namespace);
1018 free(buf);
1019 free(path_refs);
1020 if (f && fclose(f) != 0 && err == NULL)
1021 err = got_error_from_errno("fclose");
1022 return err;
1025 void
1026 got_ref_list_free(struct got_reflist_head *refs)
1028 struct got_reflist_entry *re;
1030 while (!SIMPLEQ_EMPTY(refs)) {
1031 re = SIMPLEQ_FIRST(refs);
1032 SIMPLEQ_REMOVE_HEAD(refs, entry);
1033 got_ref_close(re->ref);
1034 free(re->id);
1035 free(re);
1040 int
1041 got_ref_is_symbolic(struct got_reference *ref)
1043 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1046 const struct got_error *
1047 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1049 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1050 return got_error(GOT_ERR_BAD_REF_TYPE);
1052 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1053 return NULL;
1056 const struct got_error *
1057 got_ref_change_symref(struct got_reference *ref, const char *refname)
1059 char *new_name;
1061 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1062 return got_error(GOT_ERR_BAD_REF_TYPE);
1064 new_name = strdup(refname);
1065 if (new_name == NULL)
1066 return got_error_from_errno("strdup");
1068 free(ref->ref.symref.ref);
1069 ref->ref.symref.ref = new_name;
1070 return NULL;
1073 const struct got_error *
1074 got_ref_change_symref_to_ref(struct got_reference *symref,
1075 struct got_object_id *id)
1077 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1078 return got_error(GOT_ERR_BAD_REF_TYPE);
1080 symref->ref.ref.name = symref->ref.symref.name;
1081 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1082 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1083 return NULL;
1086 const struct got_error *
1087 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1089 const struct got_error *err = NULL, *unlock_err = NULL;
1090 const char *name = got_ref_get_name(ref);
1091 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1092 struct got_lockfile *lf = NULL;
1093 FILE *f = NULL;
1094 size_t n;
1095 struct stat sb;
1097 path_refs = get_refs_dir_path(repo, name);
1098 if (path_refs == NULL) {
1099 err = got_error_from_errno2("get_refs_dir_path", name);
1100 goto done;
1103 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1104 err = got_error_from_errno("asprintf");
1105 goto done;
1108 err = got_opentemp_named(&tmppath, &f, path);
1109 if (err) {
1110 char *parent;
1111 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1112 goto done;
1113 err = got_path_dirname(&parent, path);
1114 if (err)
1115 goto done;
1116 err = got_path_mkdir(parent);
1117 free(parent);
1118 if (err)
1119 goto done;
1120 err = got_opentemp_named(&tmppath, &f, path);
1121 if (err)
1122 goto done;
1125 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1126 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1127 if (n != strlen(ref->ref.symref.ref) + 6) {
1128 err = got_ferror(f, GOT_ERR_IO);
1129 goto done;
1131 } else {
1132 char hex[SHA1_DIGEST_STRING_LENGTH];
1133 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1134 sizeof(hex)) == NULL) {
1135 err = got_error(GOT_ERR_BAD_REF_DATA);
1136 goto done;
1138 n = fprintf(f, "%s\n", hex);
1139 if (n != sizeof(hex)) {
1140 err = got_ferror(f, GOT_ERR_IO);
1141 goto done;
1145 if (ref->lf == NULL) {
1146 err = got_lockfile_lock(&lf, path);
1147 if (err)
1148 goto done;
1151 /* XXX: check if old content matches our expectations? */
1153 if (stat(path, &sb) != 0) {
1154 if (errno != ENOENT) {
1155 err = got_error_from_errno2("stat", path);
1156 goto done;
1158 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1161 if (rename(tmppath, path) != 0) {
1162 err = got_error_from_errno3("rename", tmppath, path);
1163 goto done;
1165 free(tmppath);
1166 tmppath = NULL;
1168 if (chmod(path, sb.st_mode) != 0) {
1169 err = got_error_from_errno2("chmod", path);
1170 goto done;
1172 done:
1173 if (ref->lf == NULL && lf)
1174 unlock_err = got_lockfile_unlock(lf);
1175 if (f) {
1176 if (fclose(f) != 0 && err == NULL)
1177 err = got_error_from_errno("fclose");
1179 free(path_refs);
1180 free(path);
1181 if (tmppath) {
1182 if (unlink(tmppath) != 0 && err == NULL)
1183 err = got_error_from_errno2("unlink", tmppath);
1184 free(tmppath);
1186 return err ? err : unlock_err;
1189 static const struct got_error *
1190 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1192 const struct got_error *err = NULL, *unlock_err = NULL;
1193 struct got_lockfile *lf = NULL;
1194 FILE *f = NULL, *tmpf = NULL;
1195 char *packed_refs_path, *tmppath = NULL;
1196 struct got_reflist_head refs;
1197 int found_delref = 0;
1199 /* The packed-refs file does not cotain symbolic references. */
1200 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1201 return got_error(GOT_ERR_BAD_REF_DATA);
1203 SIMPLEQ_INIT(&refs);
1205 packed_refs_path = got_repo_get_path_packed_refs(repo);
1206 if (packed_refs_path == NULL)
1207 return got_error_from_errno("got_repo_get_path_packed_refs");
1209 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1210 if (err)
1211 goto done;
1213 if (delref->lf == NULL) {
1214 err = got_lockfile_lock(&lf, packed_refs_path);
1215 if (err)
1216 goto done;
1219 f = fopen(packed_refs_path, "r");
1220 if (f == NULL) {
1221 err = got_error_from_errno2("fopen", packed_refs_path);
1222 goto done;
1224 for (;;) {
1225 char *line;
1226 size_t len;
1227 const char delim[3] = {'\0', '\0', '\0'};
1228 struct got_reference *ref;
1229 struct got_reflist_entry *new;
1231 line = fparseln(f, &len, NULL, delim, 0);
1232 if (line == NULL) {
1233 if (feof(f))
1234 break;
1235 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1236 goto done;
1238 err = parse_packed_ref_line(&ref, NULL, line);
1239 free(line);
1240 if (err)
1241 goto done;
1242 if (ref == NULL)
1243 continue;
1245 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1246 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1247 sizeof(delref->ref.ref.sha1)) == 0) {
1248 found_delref = 1;
1249 got_ref_close(ref);
1250 continue;
1253 err = insert_ref(&new, &refs, ref, repo,
1254 got_ref_cmp_by_name, NULL);
1255 if (err || new == NULL /* duplicate */)
1256 got_ref_close(ref);
1257 if (err)
1258 goto done;
1261 if (found_delref) {
1262 struct got_reflist_entry *re;
1263 size_t n;
1264 struct stat sb;
1266 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1267 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1268 err = got_ferror(f, GOT_ERR_IO);
1269 goto done;
1272 SIMPLEQ_FOREACH(re, &refs, entry) {
1273 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1275 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1276 sizeof(hex)) == NULL) {
1277 err = got_error(GOT_ERR_BAD_REF_DATA);
1278 goto done;
1280 n = fprintf(tmpf, "%s ", hex);
1281 if (n != sizeof(hex)) {
1282 err = got_ferror(f, GOT_ERR_IO);
1283 goto done;
1285 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1286 if (n != strlen(re->ref->ref.ref.name) + 1) {
1287 err = got_ferror(f, GOT_ERR_IO);
1288 goto done;
1292 if (fflush(tmpf) != 0) {
1293 err = got_error_from_errno("fflush");
1294 goto done;
1297 if (stat(packed_refs_path, &sb) != 0) {
1298 if (errno != ENOENT) {
1299 err = got_error_from_errno2("stat",
1300 packed_refs_path);
1301 goto done;
1303 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1306 if (rename(tmppath, packed_refs_path) != 0) {
1307 err = got_error_from_errno3("rename", tmppath,
1308 packed_refs_path);
1309 goto done;
1312 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1313 err = got_error_from_errno2("chmod",
1314 packed_refs_path);
1315 goto done;
1318 done:
1319 if (delref->lf == NULL && lf)
1320 unlock_err = got_lockfile_unlock(lf);
1321 if (f) {
1322 if (fclose(f) != 0 && err == NULL)
1323 err = got_error_from_errno("fclose");
1325 if (tmpf) {
1326 unlink(tmppath);
1327 if (fclose(tmpf) != 0 && err == NULL)
1328 err = got_error_from_errno("fclose");
1330 free(tmppath);
1331 free(packed_refs_path);
1332 got_ref_list_free(&refs);
1333 return err ? err : unlock_err;
1336 const struct got_error *
1337 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1339 const struct got_error *err = NULL, *unlock_err = NULL;
1340 const char *name = got_ref_get_name(ref);
1341 char *path_refs = NULL, *path = NULL;
1342 struct got_lockfile *lf = NULL;
1344 if (ref->flags & GOT_REF_IS_PACKED)
1345 return delete_packed_ref(ref, repo);
1347 path_refs = get_refs_dir_path(repo, name);
1348 if (path_refs == NULL) {
1349 err = got_error_from_errno2("get_refs_dir_path", name);
1350 goto done;
1353 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1354 err = got_error_from_errno("asprintf");
1355 goto done;
1358 if (ref->lf == NULL) {
1359 err = got_lockfile_lock(&lf, path);
1360 if (err)
1361 goto done;
1364 /* XXX: check if old content matches our expectations? */
1366 if (unlink(path) != 0)
1367 err = got_error_from_errno2("unlink", path);
1368 done:
1369 if (ref->lf == NULL && lf)
1370 unlock_err = got_lockfile_unlock(lf);
1372 free(path_refs);
1373 free(path);
1374 return err ? err : unlock_err;
1377 const struct got_error *
1378 got_ref_unlock(struct got_reference *ref)
1380 const struct got_error *err;
1381 err = got_lockfile_unlock(ref->lf);
1382 ref->lf = NULL;
1383 return err;