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 <sha1.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <util.h>
29 #include <zlib.h>
30 #include <time.h>
31 #include <libgen.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_reference.h"
37 #include "got_opentemp.h"
38 #include "got_path.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_lockfile.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 #define GOT_REF_HEADS "heads"
51 #define GOT_REF_TAGS "tags"
52 #define GOT_REF_REMOTES "remotes"
54 /*
55 * We do not resolve tags yet, and don't yet care about sorting refs either,
56 * so packed-refs files we write contain a minimal header which disables all
57 * packed-refs "traits" supported by Git.
58 */
59 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
61 /* A symbolic reference. */
62 struct got_symref {
63 char *name;
64 char *ref;
65 };
67 #define GOT_REF_RECURSE_MAX 20
69 /* A non-symbolic reference (there is no better designation). */
70 struct got_ref {
71 char *name;
72 u_int8_t sha1[SHA1_DIGEST_LENGTH];
73 };
75 /* A reference which points to an arbitrary object. */
76 struct got_reference {
77 unsigned int flags;
78 #define GOT_REF_IS_SYMBOLIC 0x01
79 #define GOT_REF_IS_PACKED 0x02
81 union {
82 struct got_ref ref;
83 struct got_symref symref;
84 } ref;
86 struct got_lockfile *lf;
87 };
89 static const struct got_error *
90 alloc_ref(struct got_reference **ref, const char *name,
91 struct got_object_id *id, int flags)
92 {
93 const struct got_error *err = NULL;
95 *ref = calloc(1, sizeof(**ref));
96 if (*ref == NULL)
97 return got_error_from_errno("calloc");
99 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
100 (*ref)->flags = flags;
101 (*ref)->ref.ref.name = strdup(name);
102 if ((*ref)->ref.ref.name == NULL) {
103 err = got_error_from_errno("strdup");
104 got_ref_close(*ref);
105 *ref = NULL;
107 return err;
110 static const struct got_error *
111 alloc_symref(struct got_reference **ref, const char *name,
112 const char *target_ref, int flags)
114 const struct got_error *err = NULL;
116 *ref = calloc(1, sizeof(**ref));
117 if (*ref == NULL)
118 return got_error_from_errno("calloc");
120 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
121 (*ref)->ref.symref.name = strdup(name);
122 if ((*ref)->ref.symref.name == NULL) {
123 err = got_error_from_errno("strdup");
124 got_ref_close(*ref);
125 *ref = NULL;
126 return err;
128 (*ref)->ref.symref.ref = strdup(target_ref);
129 if ((*ref)->ref.symref.ref == NULL) {
130 err = got_error_from_errno("strdup");
131 got_ref_close(*ref);
132 *ref = NULL;
134 return err;
137 static const struct got_error *
138 parse_symref(struct got_reference **ref, const char *name, const char *line)
140 if (line[0] == '\0')
141 return got_error(GOT_ERR_BAD_REF_DATA);
143 return alloc_symref(ref, name, line, 0);
146 static const struct got_error *
147 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
149 struct got_object_id id;
151 if (strncmp(line, "ref: ", 5) == 0) {
152 line += 5;
153 return parse_symref(ref, name, line);
156 if (!got_parse_sha1_digest(id.sha1, line))
157 return got_error(GOT_ERR_BAD_REF_DATA);
159 return alloc_ref(ref, name, &id, 0);
162 static const struct got_error *
163 parse_ref_file(struct got_reference **ref, const char *name,
164 const char *abspath, int lock)
166 const struct got_error *err = NULL;
167 FILE *f;
168 char *line;
169 size_t len;
170 const char delim[3] = {'\0', '\0', '\0'};
171 struct got_lockfile *lf = NULL;
173 if (lock) {
174 err = got_lockfile_lock(&lf, abspath);
175 if (err)
176 return (err);
179 f = fopen(abspath, "rb");
180 if (f == NULL) {
181 if (lock)
182 got_lockfile_unlock(lf);
183 return NULL;
186 line = fparseln(f, &len, NULL, delim, 0);
187 if (line == NULL) {
188 err = got_error(GOT_ERR_BAD_REF_DATA);
189 if (lock)
190 got_lockfile_unlock(lf);
191 goto done;
194 err = parse_ref_line(ref, name, line);
195 if (lock) {
196 if (err)
197 got_lockfile_unlock(lf);
198 else {
199 if (*ref)
200 (*ref)->lf = lf;
201 else
202 got_lockfile_unlock(lf);
205 done:
206 free(line);
207 if (fclose(f) != 0 && err == NULL) {
208 err = got_error_from_errno("fclose");
209 if (*ref) {
210 if (lock)
211 got_ref_unlock(*ref);
212 got_ref_close(*ref);
213 *ref = NULL;
216 return err;
219 static int
220 is_well_known_ref(const char *refname)
222 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
223 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
224 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
225 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
228 static char *
229 get_refs_dir_path(struct got_repository *repo, const char *refname)
231 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
232 return strdup(got_repo_get_path_git_dir(repo));
234 return got_repo_get_path_refs(repo);
237 static int
238 is_valid_ref_name(const char *name)
240 const char *s, *slash, *seg;
241 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
242 const char *forbidden_seq[] = { "//", "..", "@{" };
243 const char *lfs = GOT_LOCKFILE_SUFFIX;
244 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
245 int i;
247 if (name[0] == '@' && name[1] == '\0')
248 return 0;
250 slash = strchr(name, '/');
251 if (slash == NULL)
252 return 0;
254 s = name;
255 seg = s;
256 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
257 return 0;
258 while (*s) {
259 for (i = 0; i < nitems(forbidden); i++) {
260 if (*s == forbidden[i])
261 return 0;
263 for (i = 0; i < nitems(forbidden_seq); i++) {
264 if (s[0] == forbidden_seq[i][0] &&
265 s[1] == forbidden_seq[i][1])
266 return 0;
268 if (iscntrl((unsigned char)s[0]))
269 return 0;
270 if (s[0] == '.' && s[1] == '\0')
271 return 0;
272 if (*s == '/') {
273 const char *nextseg = s + 1;
274 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
275 nextseg[0] == '/')
276 return 0;
277 if (seg <= s - lfs_len &&
278 strncmp(s - lfs_len, lfs, lfs_len) == 0)
279 return 0;
280 seg = nextseg;
282 s++;
285 if (seg <= s - lfs_len &&
286 strncmp(s - lfs_len, lfs, lfs_len) == 0)
287 return 0;
289 return 1;
292 const struct got_error *
293 got_ref_alloc(struct got_reference **ref, const char *name,
294 struct got_object_id *id)
296 if (!is_valid_ref_name(name))
297 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
299 return alloc_ref(ref, name, id, 0);
302 const struct got_error *
303 got_ref_alloc_symref(struct got_reference **ref, const char *name,
304 struct got_reference *target_ref)
306 if (!is_valid_ref_name(name))
307 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
309 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
312 static const struct got_error *
313 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
314 const char *line)
316 struct got_object_id id;
317 const char *name;
319 *ref = NULL;
321 if (line[0] == '#' || line[0] == '^')
322 return NULL;
324 if (!got_parse_sha1_digest(id.sha1, line))
325 return got_error(GOT_ERR_BAD_REF_DATA);
327 if (abs_refname) {
328 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
329 return NULL;
330 name = abs_refname;
331 } else
332 name = line + SHA1_DIGEST_STRING_LENGTH;
334 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
337 static const struct got_error *
338 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
339 int nsubdirs, const char *refname)
341 const struct got_error *err = NULL;
342 char *abs_refname;
343 char *line;
344 size_t len;
345 const char delim[3] = {'\0', '\0', '\0'};
346 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
348 *ref = NULL;
350 if (ref_is_absolute)
351 abs_refname = (char *)refname;
352 do {
353 line = fparseln(f, &len, NULL, delim, 0);
354 if (line == NULL) {
355 if (feof(f))
356 break;
357 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
358 break;
360 for (i = 0; i < nsubdirs; i++) {
361 if (!ref_is_absolute &&
362 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
363 refname) == -1)
364 return got_error_from_errno("asprintf");
365 err = parse_packed_ref_line(ref, abs_refname, line);
366 if (!ref_is_absolute)
367 free(abs_refname);
368 if (err || *ref != NULL)
369 break;
371 free(line);
372 if (err)
373 break;
374 } while (*ref == NULL);
376 return err;
379 static const struct got_error *
380 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
381 const char *name, int lock)
383 const struct got_error *err = NULL;
384 char *path = NULL;
385 char *absname = NULL;
386 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
387 int ref_is_well_known = is_well_known_ref(name);
389 *ref = NULL;
391 if (ref_is_absolute || ref_is_well_known) {
392 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
393 return got_error_from_errno("asprintf");
394 absname = (char *)name;
395 } else {
396 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
397 subdir[0] ? "/" : "", name) == -1)
398 return got_error_from_errno("asprintf");
400 if (asprintf(&absname, "refs/%s%s%s",
401 subdir, subdir[0] ? "/" : "", name) == -1) {
402 err = got_error_from_errno("asprintf");
403 goto done;
407 err = parse_ref_file(ref, absname, path, lock);
408 done:
409 if (!ref_is_absolute && !ref_is_well_known)
410 free(absname);
411 free(path);
412 return err;
415 const struct got_error *
416 got_ref_open(struct got_reference **ref, struct got_repository *repo,
417 const char *refname, int lock)
419 const struct got_error *err = NULL;
420 char *path_refs = NULL;
421 const char *subdirs[] = {
422 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
423 };
424 int i, well_known = is_well_known_ref(refname);
425 struct got_lockfile *lf = NULL;
427 *ref = NULL;
429 path_refs = get_refs_dir_path(repo, refname);
430 if (path_refs == NULL) {
431 err = got_error_from_errno2("get_refs_dir_path", refname);
432 goto done;
435 if (well_known) {
436 err = open_ref(ref, path_refs, "", refname, lock);
437 } else {
438 char *packed_refs_path;
439 FILE *f;
441 /* Search on-disk refs before packed refs! */
442 for (i = 0; i < nitems(subdirs); i++) {
443 err = open_ref(ref, path_refs, subdirs[i], refname,
444 lock);
445 if (err || *ref)
446 goto done;
449 packed_refs_path = got_repo_get_path_packed_refs(repo);
450 if (packed_refs_path == NULL) {
451 err = got_error_from_errno(
452 "got_repo_get_path_packed_refs");
453 goto done;
456 if (lock) {
457 err = got_lockfile_lock(&lf, packed_refs_path);
458 if (err)
459 goto done;
461 f = fopen(packed_refs_path, "rb");
462 free(packed_refs_path);
463 if (f != NULL) {
464 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
465 refname);
466 if (!err) {
467 if (fclose(f) != 0) {
468 err = got_error_from_errno("fclose");
469 got_ref_close(*ref);
470 *ref = NULL;
471 } else if (*ref)
472 (*ref)->lf = lf;
476 done:
477 if (!err && *ref == NULL)
478 err = got_error_not_ref(refname);
479 if (err && lf)
480 got_lockfile_unlock(lf);
481 free(path_refs);
482 return err;
485 void
486 got_ref_close(struct got_reference *ref)
488 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
489 free(ref->ref.symref.name);
490 free(ref->ref.symref.ref);
491 } else
492 free(ref->ref.ref.name);
493 free(ref);
496 struct got_reference *
497 got_ref_dup(struct got_reference *ref)
499 struct got_reference *ret;
501 ret = calloc(1, sizeof(*ret));
502 if (ret == NULL)
503 return NULL;
505 ret->flags = ref->flags;
506 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
507 ret->ref.symref.name = strdup(ref->ref.symref.name);
508 if (ret->ref.symref.name == NULL) {
509 free(ret);
510 return NULL;
512 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
513 if (ret->ref.symref.ref == NULL) {
514 free(ret->ref.symref.name);
515 free(ret);
516 return NULL;
518 } else {
519 ref->ref.ref.name = strdup(ref->ref.ref.name);
520 if (ref->ref.ref.name == NULL) {
521 free(ret);
522 return NULL;
524 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
525 sizeof(ret->ref.ref.sha1));
528 return ret;
531 const struct got_error *
532 got_reflist_entry_dup(struct got_reflist_entry **newp,
533 struct got_reflist_entry *re)
535 const struct got_error *err = NULL;
536 struct got_reflist_entry *new;
538 *newp = NULL;
540 new = malloc(sizeof(*new));
541 if (new == NULL)
542 return got_error_from_errno("malloc");
544 new->ref = got_ref_dup(re->ref);
545 if (new->ref == NULL) {
546 err = got_error_from_errno("got_ref_dup");
547 free(new);
548 return err;
551 new->id = got_object_id_dup(re->id);
552 if (new->id == NULL) {
553 err = got_error_from_errno("got_ref_dup");
554 free(new->id);
555 free(new);
556 return err;
559 *newp = new;
560 return NULL;
563 static const struct got_error *
564 resolve_symbolic_ref(struct got_reference **resolved,
565 struct got_repository *repo, struct got_reference *ref)
567 struct got_reference *nextref;
568 const struct got_error *err;
570 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
571 if (err)
572 return err;
574 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
575 err = resolve_symbolic_ref(resolved, repo, nextref);
576 else
577 *resolved = got_ref_dup(nextref);
579 got_ref_close(nextref);
580 return err;
583 static const struct got_error *
584 ref_resolve(struct got_object_id **id, struct got_repository *repo,
585 struct got_reference *ref, int recursion)
587 const struct got_error *err;
589 if (recursion <= 0)
590 return got_error_msg(GOT_ERR_RECURSION,
591 "reference recursion limit reached");
593 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
594 struct got_reference *resolved = NULL;
595 err = resolve_symbolic_ref(&resolved, repo, ref);
596 if (err == NULL)
597 err = ref_resolve(id, repo, resolved, --recursion);
598 if (resolved)
599 got_ref_close(resolved);
600 return err;
603 *id = calloc(1, sizeof(**id));
604 if (*id == NULL)
605 return got_error_from_errno("calloc");
606 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
607 return NULL;
610 const struct got_error *
611 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
612 struct got_reference *ref)
614 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
617 char *
618 got_ref_to_str(struct got_reference *ref)
620 char *str;
622 if (ref->flags & GOT_REF_IS_SYMBOLIC)
623 return strdup(ref->ref.symref.ref);
625 str = malloc(SHA1_DIGEST_STRING_LENGTH);
626 if (str == NULL)
627 return NULL;
629 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
630 SHA1_DIGEST_STRING_LENGTH) == NULL) {
631 free(str);
632 return NULL;
635 return str;
638 const char *
639 got_ref_get_name(struct got_reference *ref)
641 if (ref->flags & GOT_REF_IS_SYMBOLIC)
642 return ref->ref.symref.name;
644 return ref->ref.ref.name;
647 const char *
648 got_ref_get_symref_target(struct got_reference *ref)
650 if (ref->flags & GOT_REF_IS_SYMBOLIC)
651 return ref->ref.symref.ref;
653 return NULL;
656 const struct got_error *
657 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
658 struct got_reference* re2)
660 const char *name1 = got_ref_get_name(re1);
661 const char *name2 = got_ref_get_name(re2);
663 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
664 return NULL;
667 static const struct got_error *
668 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
669 struct got_reference *ref, struct got_repository *repo,
670 got_ref_cmp_cb cmp_cb, void *cmp_arg)
672 const struct got_error *err;
673 struct got_object_id *id;
674 struct got_reflist_entry *new, *re, *prev = NULL;
675 int cmp;
677 *newp = NULL;
679 err = got_ref_resolve(&id, repo, ref);
680 if (err)
681 return err;
683 new = malloc(sizeof(*new));
684 if (new == NULL) {
685 free(id);
686 return got_error_from_errno("malloc");
688 new->ref = ref;
689 new->id = id;
690 *newp = new;
692 /*
693 * We must de-duplicate entries on insert because packed-refs may
694 * contain redundant entries. On-disk refs take precedence.
695 * This code assumes that on-disk revs are read before packed-refs.
696 * We're iterating the list anyway, so insert elements sorted by name.
697 */
698 re = SIMPLEQ_FIRST(refs);
699 while (re) {
700 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
701 if (err)
702 return err;
703 if (cmp == 0) {
704 /* duplicate */
705 free(new->id);
706 free(new);
707 *newp = NULL;
708 return NULL;
709 } else if (cmp > 0) {
710 if (prev)
711 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
712 else
713 SIMPLEQ_INSERT_HEAD(refs, new, entry);
714 return NULL;
715 } else {
716 prev = re;
717 re = SIMPLEQ_NEXT(re, entry);
721 SIMPLEQ_INSERT_TAIL(refs, new, entry);
722 return NULL;
725 static const struct got_error *
726 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
727 const char *subdir, struct got_repository *repo,
728 got_ref_cmp_cb cmp_cb, void *cmp_arg)
730 const struct got_error *err = NULL;
731 DIR *d = NULL;
732 char *path_subdir;
734 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
735 return got_error_from_errno("asprintf");
737 d = opendir(path_subdir);
738 if (d == NULL)
739 goto done;
741 for (;;) {
742 struct dirent *dent;
743 struct got_reference *ref;
744 char *child;
746 dent = readdir(d);
747 if (dent == NULL)
748 break;
750 if (strcmp(dent->d_name, ".") == 0 ||
751 strcmp(dent->d_name, "..") == 0)
752 continue;
754 switch (dent->d_type) {
755 case DT_REG:
756 err = open_ref(&ref, path_refs, subdir, dent->d_name,
757 0);
758 if (err)
759 goto done;
760 if (ref) {
761 struct got_reflist_entry *new;
762 err = insert_ref(&new, refs, ref, repo,
763 cmp_cb, cmp_arg);
764 if (err || new == NULL /* duplicate */)
765 got_ref_close(ref);
766 if (err)
767 goto done;
769 break;
770 case DT_DIR:
771 if (asprintf(&child, "%s%s%s", subdir,
772 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
773 err = got_error_from_errno("asprintf");
774 break;
776 err = gather_on_disk_refs(refs, path_refs, child, repo,
777 cmp_cb, cmp_arg);
778 free(child);
779 break;
780 default:
781 break;
784 done:
785 if (d)
786 closedir(d);
787 free(path_subdir);
788 return err;
791 const struct got_error *
792 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
793 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
795 const struct got_error *err;
796 char *packed_refs_path, *path_refs = NULL;
797 const char *ondisk_ref_namespace = NULL;
798 FILE *f = NULL;
799 struct got_reference *ref;
800 struct got_reflist_entry *new;
802 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
803 /* HEAD ref should always exist. */
804 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
805 if (path_refs == NULL) {
806 err = got_error_from_errno("get_refs_dir_path");
807 goto done;
809 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
810 if (err)
811 goto done;
812 err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
813 if (err || new == NULL /* duplicate */)
814 got_ref_close(ref);
815 if (err)
816 goto done;
819 ondisk_ref_namespace = ref_namespace;
820 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
821 ondisk_ref_namespace += 5;
823 /* Gather on-disk refs before parsing packed-refs. */
824 free(path_refs);
825 path_refs = get_refs_dir_path(repo, "");
826 if (path_refs == NULL) {
827 err = got_error_from_errno("get_refs_dir_path");
828 goto done;
830 err = gather_on_disk_refs(refs, path_refs,
831 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
832 cmp_cb, cmp_arg);
833 if (err)
834 goto done;
836 /*
837 * The packed-refs file may contain redundant entries, in which
838 * case on-disk refs take precedence.
839 */
840 packed_refs_path = got_repo_get_path_packed_refs(repo);
841 if (packed_refs_path == NULL) {
842 err = got_error_from_errno("got_repo_get_path_packed_refs");
843 goto done;
846 f = fopen(packed_refs_path, "r");
847 free(packed_refs_path);
848 if (f) {
849 char *line;
850 size_t len;
851 const char delim[3] = {'\0', '\0', '\0'};
852 for (;;) {
853 line = fparseln(f, &len, NULL, delim, 0);
854 if (line == NULL) {
855 if (feof(f))
856 break;
857 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
858 goto done;
860 err = parse_packed_ref_line(&ref, NULL, line);
861 free(line);
862 if (err)
863 goto done;
864 if (ref) {
865 if (ref_namespace) {
866 const char *name;
867 name = got_ref_get_name(ref);
868 if (strncmp(name, ref_namespace,
869 strlen(ref_namespace)) != 0) {
870 got_ref_close(ref);
871 continue;
874 err = insert_ref(&new, refs, ref, repo,
875 cmp_cb, cmp_arg);
876 if (err || new == NULL /* duplicate */)
877 got_ref_close(ref);
878 if (err)
879 goto done;
883 done:
884 free(path_refs);
885 if (f && fclose(f) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
887 return err;
890 void
891 got_ref_list_free(struct got_reflist_head *refs)
893 struct got_reflist_entry *re;
895 while (!SIMPLEQ_EMPTY(refs)) {
896 re = SIMPLEQ_FIRST(refs);
897 SIMPLEQ_REMOVE_HEAD(refs, entry);
898 got_ref_close(re->ref);
899 free(re->id);
900 free(re);
905 int
906 got_ref_is_symbolic(struct got_reference *ref)
908 return (ref->flags & GOT_REF_IS_SYMBOLIC);
911 const struct got_error *
912 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
914 if (ref->flags & GOT_REF_IS_SYMBOLIC)
915 return got_error(GOT_ERR_BAD_REF_TYPE);
917 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
918 return NULL;
921 const struct got_error *
922 got_ref_change_symref(struct got_reference *ref, char *refname)
924 char *new_name;
926 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
927 return got_error(GOT_ERR_BAD_REF_TYPE);
929 new_name = strdup(refname);
930 if (new_name == NULL)
931 return got_error_from_errno("strdup");
933 free(ref->ref.symref.name);
934 ref->ref.symref.name = new_name;
935 return NULL;
938 const struct got_error *
939 got_ref_write(struct got_reference *ref, struct got_repository *repo)
941 const struct got_error *err = NULL, *unlock_err = NULL;
942 const char *name = got_ref_get_name(ref);
943 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
944 struct got_lockfile *lf = NULL;
945 FILE *f = NULL;
946 size_t n;
947 struct stat sb;
949 path_refs = get_refs_dir_path(repo, name);
950 if (path_refs == NULL) {
951 err = got_error_from_errno2("get_refs_dir_path", name);
952 goto done;
955 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
956 err = got_error_from_errno("asprintf");
957 goto done;
960 err = got_opentemp_named(&tmppath, &f, path);
961 if (err) {
962 char *parent;
963 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
964 goto done;
965 err = got_path_dirname(&parent, path);
966 if (err)
967 goto done;
968 err = got_path_mkdir(parent);
969 free(parent);
970 if (err)
971 goto done;
972 err = got_opentemp_named(&tmppath, &f, path);
973 if (err)
974 goto done;
977 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
978 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
979 if (n != strlen(ref->ref.symref.ref) + 6) {
980 err = got_ferror(f, GOT_ERR_IO);
981 goto done;
983 } else {
984 char hex[SHA1_DIGEST_STRING_LENGTH];
985 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
986 sizeof(hex)) == NULL) {
987 err = got_error(GOT_ERR_BAD_REF_DATA);
988 goto done;
990 n = fprintf(f, "%s\n", hex);
991 if (n != sizeof(hex)) {
992 err = got_ferror(f, GOT_ERR_IO);
993 goto done;
997 if (ref->lf == NULL) {
998 err = got_lockfile_lock(&lf, path);
999 if (err)
1000 goto done;
1003 /* XXX: check if old content matches our expectations? */
1005 if (stat(path, &sb) != 0) {
1006 if (errno != ENOENT) {
1007 err = got_error_from_errno2("stat", path);
1008 goto done;
1010 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1013 if (rename(tmppath, path) != 0) {
1014 err = got_error_from_errno3("rename", tmppath, path);
1015 goto done;
1017 free(tmppath);
1018 tmppath = NULL;
1020 if (chmod(path, sb.st_mode) != 0) {
1021 err = got_error_from_errno2("chmod", path);
1022 goto done;
1024 done:
1025 if (ref->lf == NULL && lf)
1026 unlock_err = got_lockfile_unlock(lf);
1027 if (f) {
1028 if (fclose(f) != 0 && err == NULL)
1029 err = got_error_from_errno("fclose");
1031 free(path_refs);
1032 free(path);
1033 if (tmppath) {
1034 if (unlink(tmppath) != 0 && err == NULL)
1035 err = got_error_from_errno2("unlink", tmppath);
1036 free(tmppath);
1038 return err ? err : unlock_err;
1041 static const struct got_error *
1042 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1044 const struct got_error *err = NULL, *unlock_err = NULL;
1045 struct got_lockfile *lf = NULL;
1046 FILE *f = NULL, *tmpf = NULL;
1047 char *packed_refs_path, *tmppath = NULL;
1048 struct got_reflist_head refs;
1049 int found_delref = 0;
1051 /* The packed-refs file does not cotain symbolic references. */
1052 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1053 return got_error(GOT_ERR_BAD_REF_DATA);
1055 SIMPLEQ_INIT(&refs);
1057 packed_refs_path = got_repo_get_path_packed_refs(repo);
1058 if (packed_refs_path == NULL)
1059 return got_error_from_errno("got_repo_get_path_packed_refs");
1061 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1062 if (err)
1063 goto done;
1065 if (delref->lf == NULL) {
1066 err = got_lockfile_lock(&lf, packed_refs_path);
1067 if (err)
1068 goto done;
1071 f = fopen(packed_refs_path, "r");
1072 if (f == NULL) {
1073 err = got_error_from_errno2("fopen", packed_refs_path);
1074 goto done;
1076 for (;;) {
1077 char *line;
1078 size_t len;
1079 const char delim[3] = {'\0', '\0', '\0'};
1080 struct got_reference *ref;
1081 struct got_reflist_entry *new;
1083 line = fparseln(f, &len, NULL, delim, 0);
1084 if (line == NULL) {
1085 if (feof(f))
1086 break;
1087 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1088 goto done;
1090 err = parse_packed_ref_line(&ref, NULL, line);
1091 free(line);
1092 if (err)
1093 goto done;
1094 if (ref == NULL)
1095 continue;
1097 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1098 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1099 sizeof(delref->ref.ref.sha1)) == 0) {
1100 found_delref = 1;
1101 got_ref_close(ref);
1102 continue;
1105 err = insert_ref(&new, &refs, ref, repo,
1106 got_ref_cmp_by_name, NULL);
1107 if (err || new == NULL /* duplicate */)
1108 got_ref_close(ref);
1109 if (err)
1110 goto done;
1113 if (found_delref) {
1114 struct got_reflist_entry *re;
1115 size_t n;
1116 struct stat sb;
1118 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1119 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1120 err = got_ferror(f, GOT_ERR_IO);
1121 goto done;
1124 SIMPLEQ_FOREACH(re, &refs, entry) {
1125 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1127 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1128 sizeof(hex)) == NULL) {
1129 err = got_error(GOT_ERR_BAD_REF_DATA);
1130 goto done;
1132 n = fprintf(tmpf, "%s ", hex);
1133 if (n != sizeof(hex)) {
1134 err = got_ferror(f, GOT_ERR_IO);
1135 goto done;
1137 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1138 if (n != strlen(re->ref->ref.ref.name) + 1) {
1139 err = got_ferror(f, GOT_ERR_IO);
1140 goto done;
1144 if (fflush(tmpf) != 0) {
1145 err = got_error_from_errno("fflush");
1146 goto done;
1149 if (stat(packed_refs_path, &sb) != 0) {
1150 if (errno != ENOENT) {
1151 err = got_error_from_errno2("stat",
1152 packed_refs_path);
1153 goto done;
1155 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1158 if (rename(tmppath, packed_refs_path) != 0) {
1159 err = got_error_from_errno3("rename", tmppath,
1160 packed_refs_path);
1161 goto done;
1164 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1165 err = got_error_from_errno2("chmod",
1166 packed_refs_path);
1167 goto done;
1170 done:
1171 if (delref->lf == NULL && lf)
1172 unlock_err = got_lockfile_unlock(lf);
1173 if (f) {
1174 if (fclose(f) != 0 && err == NULL)
1175 err = got_error_from_errno("fclose");
1177 if (tmpf) {
1178 unlink(tmppath);
1179 if (fclose(tmpf) != 0 && err == NULL)
1180 err = got_error_from_errno("fclose");
1182 free(tmppath);
1183 free(packed_refs_path);
1184 got_ref_list_free(&refs);
1185 return err ? err : unlock_err;
1188 const struct got_error *
1189 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1191 const struct got_error *err = NULL, *unlock_err = NULL;
1192 const char *name = got_ref_get_name(ref);
1193 char *path_refs = NULL, *path = NULL;
1194 struct got_lockfile *lf = NULL;
1196 if (ref->flags & GOT_REF_IS_PACKED)
1197 return delete_packed_ref(ref, repo);
1199 path_refs = get_refs_dir_path(repo, name);
1200 if (path_refs == NULL) {
1201 err = got_error_from_errno2("get_refs_dir_path", name);
1202 goto done;
1205 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1206 err = got_error_from_errno("asprintf");
1207 goto done;
1210 if (ref->lf == NULL) {
1211 err = got_lockfile_lock(&lf, path);
1212 if (err)
1213 goto done;
1216 /* XXX: check if old content matches our expectations? */
1218 if (unlink(path) != 0)
1219 err = got_error_from_errno2("unlink", path);
1220 done:
1221 if (ref->lf == NULL && lf)
1222 unlock_err = got_lockfile_unlock(lf);
1224 free(path_refs);
1225 free(path);
1226 return err ? err : unlock_err;
1229 const struct got_error *
1230 got_ref_unlock(struct got_reference *ref)
1232 const struct got_error *err;
1233 err = got_lockfile_unlock(ref->lf);
1234 ref->lf = NULL;
1235 return err;