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;
127 (*ref)->ref.symref.ref = strdup(target_ref);
128 if ((*ref)->ref.symref.ref == NULL) {
129 err = got_error_from_errno("strdup");
130 got_ref_close(*ref);
131 *ref = NULL;
133 return err;
136 static const struct got_error *
137 parse_symref(struct got_reference **ref, const char *name, const char *line)
139 if (line[0] == '\0')
140 return got_error(GOT_ERR_BAD_REF_DATA);
142 return alloc_symref(ref, name, line, 0);
145 static const struct got_error *
146 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
148 struct got_object_id id;
150 if (strncmp(line, "ref: ", 5) == 0) {
151 line += 5;
152 return parse_symref(ref, name, line);
155 if (!got_parse_sha1_digest(id.sha1, line))
156 return got_error(GOT_ERR_BAD_REF_DATA);
158 return alloc_ref(ref, name, &id, 0);
161 static const struct got_error *
162 parse_ref_file(struct got_reference **ref, const char *name,
163 const char *abspath, int lock)
165 const struct got_error *err = NULL;
166 FILE *f;
167 char *line;
168 size_t len;
169 const char delim[3] = {'\0', '\0', '\0'};
170 struct got_lockfile *lf = NULL;
172 if (lock) {
173 err = got_lockfile_lock(&lf, abspath);
174 if (err)
175 return (err);
178 f = fopen(abspath, "rb");
179 if (f == NULL) {
180 if (lock)
181 got_lockfile_unlock(lf);
182 return NULL;
185 line = fparseln(f, &len, NULL, delim, 0);
186 if (line == NULL) {
187 err = got_error(GOT_ERR_BAD_REF_DATA);
188 if (lock)
189 got_lockfile_unlock(lf);
190 goto done;
193 err = parse_ref_line(ref, name, line);
194 if (lock) {
195 if (err)
196 got_lockfile_unlock(lf);
197 else {
198 if (*ref)
199 (*ref)->lf = lf;
200 else
201 got_lockfile_unlock(lf);
204 done:
205 free(line);
206 if (fclose(f) != 0 && err == NULL) {
207 err = got_error_from_errno("fclose");
208 if (*ref) {
209 if (lock)
210 got_ref_unlock(*ref);
211 got_ref_close(*ref);
212 *ref = NULL;
215 return err;
218 static int
219 is_well_known_ref(const char *refname)
221 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
222 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
223 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
224 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
227 static char *
228 get_refs_dir_path(struct got_repository *repo, const char *refname)
230 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
231 return strdup(got_repo_get_path_git_dir(repo));
233 return got_repo_get_path_refs(repo);
236 static int
237 is_valid_ref_name(const char *name)
239 const char *s, *slash, *seg;
240 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
241 const char *forbidden_seq[] = { "//", "..", "@{" };
242 const char *lfs = GOT_LOCKFILE_SUFFIX;
243 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
244 int i;
246 if (name[0] == '@' && name[1] == '\0')
247 return 0;
249 slash = strchr(name, '/');
250 if (slash == NULL)
251 return 0;
253 s = name;
254 seg = s;
255 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
256 return 0;
257 while (*s) {
258 for (i = 0; i < nitems(forbidden); i++) {
259 if (*s == forbidden[i])
260 return 0;
262 for (i = 0; i < nitems(forbidden_seq); i++) {
263 if (s[0] == forbidden_seq[i][0] &&
264 s[1] == forbidden_seq[i][1])
265 return 0;
267 if (iscntrl((unsigned char)s[0]))
268 return 0;
269 if (s[0] == '.' && s[1] == '\0')
270 return 0;
271 if (*s == '/') {
272 const char *nextseg = s + 1;
273 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
274 nextseg[0] == '/')
275 return 0;
276 if (seg <= s - lfs_len &&
277 strncmp(s - lfs_len, lfs, lfs_len) == 0)
278 return 0;
279 seg = nextseg;
281 s++;
284 if (seg <= s - lfs_len &&
285 strncmp(s - lfs_len, lfs, lfs_len) == 0)
286 return 0;
288 return 1;
291 const struct got_error *
292 got_ref_alloc(struct got_reference **ref, const char *name,
293 struct got_object_id *id)
295 if (!is_valid_ref_name(name))
296 return got_error(GOT_ERR_BAD_REF_NAME);
298 return alloc_ref(ref, name, id, 0);
301 const struct got_error *
302 got_ref_alloc_symref(struct got_reference **ref, const char *name,
303 struct got_reference *target_ref)
305 if (!is_valid_ref_name(name))
306 return got_error(GOT_ERR_BAD_REF_NAME);
308 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
311 static const struct got_error *
312 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
313 const char *line)
315 struct got_object_id id;
316 const char *name;
318 *ref = NULL;
320 if (line[0] == '#' || line[0] == '^')
321 return NULL;
323 if (!got_parse_sha1_digest(id.sha1, line))
324 return got_error(GOT_ERR_BAD_REF_DATA);
326 if (abs_refname) {
327 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
328 return NULL;
329 name = abs_refname;
330 } else
331 name = line + SHA1_DIGEST_STRING_LENGTH;
333 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
336 static const struct got_error *
337 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
338 int nsubdirs, const char *refname)
340 const struct got_error *err = NULL;
341 char *abs_refname;
342 char *line;
343 size_t len;
344 const char delim[3] = {'\0', '\0', '\0'};
345 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
347 *ref = NULL;
349 if (ref_is_absolute)
350 abs_refname = (char *)refname;
351 do {
352 line = fparseln(f, &len, NULL, delim, 0);
353 if (line == NULL) {
354 if (feof(f))
355 break;
356 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
357 break;
359 for (i = 0; i < nsubdirs; i++) {
360 if (!ref_is_absolute &&
361 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
362 refname) == -1)
363 return got_error_from_errno("asprintf");
364 err = parse_packed_ref_line(ref, abs_refname, line);
365 if (!ref_is_absolute)
366 free(abs_refname);
367 if (err || *ref != NULL)
368 break;
370 free(line);
371 if (err)
372 break;
373 } while (*ref == NULL);
375 return err;
378 static const struct got_error *
379 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
380 const char *name, int lock)
382 const struct got_error *err = NULL;
383 char *path = NULL;
384 char *absname = NULL;
385 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
386 int ref_is_well_known = is_well_known_ref(name);
388 *ref = NULL;
390 if (ref_is_absolute || ref_is_well_known) {
391 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
392 return got_error_from_errno("asprintf");
393 absname = (char *)name;
394 } else {
395 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
396 subdir[0] ? "/" : "", name) == -1)
397 return got_error_from_errno("asprintf");
399 if (asprintf(&absname, "refs/%s%s%s",
400 subdir, subdir[0] ? "/" : "", name) == -1) {
401 err = got_error_from_errno("asprintf");
402 goto done;
406 err = parse_ref_file(ref, absname, path, lock);
407 done:
408 if (!ref_is_absolute && !ref_is_well_known)
409 free(absname);
410 free(path);
411 return err;
414 const struct got_error *
415 got_ref_open(struct got_reference **ref, struct got_repository *repo,
416 const char *refname, int lock)
418 const struct got_error *err = NULL;
419 char *path_refs = NULL;
420 const char *subdirs[] = {
421 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
422 };
423 int i, well_known = is_well_known_ref(refname);
424 struct got_lockfile *lf = NULL;
426 *ref = NULL;
428 path_refs = get_refs_dir_path(repo, refname);
429 if (path_refs == NULL) {
430 err = got_error_from_errno2("get_refs_dir_path", refname);
431 goto done;
434 if (well_known) {
435 err = open_ref(ref, path_refs, "", refname, lock);
436 } else {
437 char *packed_refs_path;
438 FILE *f;
440 /* Search on-disk refs before packed refs! */
441 for (i = 0; i < nitems(subdirs); i++) {
442 err = open_ref(ref, path_refs, subdirs[i], refname,
443 lock);
444 if (err || *ref)
445 goto done;
448 packed_refs_path = got_repo_get_path_packed_refs(repo);
449 if (packed_refs_path == NULL) {
450 err = got_error_from_errno(
451 "got_repo_get_path_packed_refs");
452 goto done;
455 if (lock) {
456 err = got_lockfile_lock(&lf, packed_refs_path);
457 if (err)
458 goto done;
460 f = fopen(packed_refs_path, "rb");
461 free(packed_refs_path);
462 if (f != NULL) {
463 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
464 refname);
465 if (!err) {
466 if (fclose(f) != 0) {
467 err = got_error_from_errno("fclose");
468 got_ref_close(*ref);
469 *ref = NULL;
470 } else if (*ref)
471 (*ref)->lf = lf;
475 done:
476 if (!err && *ref == NULL)
477 err = got_error_not_ref(refname);
478 if (err && lf)
479 got_lockfile_unlock(lf);
480 free(path_refs);
481 return err;
484 void
485 got_ref_close(struct got_reference *ref)
487 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
488 free(ref->ref.symref.name);
489 free(ref->ref.symref.ref);
490 } else
491 free(ref->ref.ref.name);
492 free(ref);
495 struct got_reference *
496 got_ref_dup(struct got_reference *ref)
498 struct got_reference *ret;
500 ret = calloc(1, sizeof(*ret));
501 if (ret == NULL)
502 return NULL;
504 ret->flags = ref->flags;
505 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
506 ret->ref.symref.name = strdup(ref->ref.symref.name);
507 if (ret->ref.symref.name == NULL) {
508 free(ret);
509 return NULL;
511 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
512 if (ret->ref.symref.ref == NULL) {
513 free(ret->ref.symref.name);
514 free(ret);
515 return NULL;
517 } else {
518 ref->ref.ref.name = strdup(ref->ref.ref.name);
519 if (ref->ref.ref.name == NULL) {
520 free(ret);
521 return NULL;
523 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
524 sizeof(ret->ref.ref.sha1));
527 return ret;
530 const struct got_error *
531 got_reflist_entry_dup(struct got_reflist_entry **newp,
532 struct got_reflist_entry *re)
534 const struct got_error *err = NULL;
535 struct got_reflist_entry *new;
537 *newp = NULL;
539 new = malloc(sizeof(*new));
540 if (new == NULL)
541 return got_error_from_errno("malloc");
543 new->ref = got_ref_dup(re->ref);
544 if (new->ref == NULL) {
545 err = got_error_from_errno("got_ref_dup");
546 free(new);
547 return err;
550 new->id = got_object_id_dup(re->id);
551 if (new->id == NULL) {
552 err = got_error_from_errno("got_ref_dup");
553 free(new->id);
554 free(new);
555 return err;
558 *newp = new;
559 return NULL;
562 static const struct got_error *
563 resolve_symbolic_ref(struct got_reference **resolved,
564 struct got_repository *repo, struct got_reference *ref)
566 struct got_reference *nextref;
567 const struct got_error *err;
569 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
570 if (err)
571 return err;
573 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
574 err = resolve_symbolic_ref(resolved, repo, nextref);
575 else
576 *resolved = got_ref_dup(nextref);
578 got_ref_close(nextref);
579 return err;
582 static const struct got_error *
583 ref_resolve(struct got_object_id **id, struct got_repository *repo,
584 struct got_reference *ref, int recursion)
586 const struct got_error *err;
588 if (recursion <= 0)
589 return got_error_msg(GOT_ERR_RECURSION,
590 "reference recursion limit reached");
592 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
593 struct got_reference *resolved = NULL;
594 err = resolve_symbolic_ref(&resolved, repo, ref);
595 if (err == NULL)
596 err = ref_resolve(id, repo, resolved, --recursion);
597 if (resolved)
598 got_ref_close(resolved);
599 return err;
602 *id = calloc(1, sizeof(**id));
603 if (*id == NULL)
604 return got_error_from_errno("calloc");
605 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
606 return NULL;
609 const struct got_error *
610 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
611 struct got_reference *ref)
613 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
616 char *
617 got_ref_to_str(struct got_reference *ref)
619 char *str;
621 if (ref->flags & GOT_REF_IS_SYMBOLIC)
622 return strdup(ref->ref.symref.ref);
624 str = malloc(SHA1_DIGEST_STRING_LENGTH);
625 if (str == NULL)
626 return NULL;
628 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
629 SHA1_DIGEST_STRING_LENGTH) == NULL) {
630 free(str);
631 return NULL;
634 return str;
637 const char *
638 got_ref_get_name(struct got_reference *ref)
640 if (ref->flags & GOT_REF_IS_SYMBOLIC)
641 return ref->ref.symref.name;
643 return ref->ref.ref.name;
646 const char *
647 got_ref_get_symref_target(struct got_reference *ref)
649 if (ref->flags & GOT_REF_IS_SYMBOLIC)
650 return ref->ref.symref.ref;
652 return NULL;
655 const struct got_error *
656 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
657 struct got_reference* re2)
659 const char *name1 = got_ref_get_name(re1);
660 const char *name2 = got_ref_get_name(re2);
662 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
663 return NULL;
666 static const struct got_error *
667 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
668 struct got_reference *ref, struct got_repository *repo,
669 got_ref_cmp_cb cmp_cb, void *cmp_arg)
671 const struct got_error *err;
672 struct got_object_id *id;
673 struct got_reflist_entry *new, *re, *prev = NULL;
674 int cmp;
676 *newp = NULL;
678 err = got_ref_resolve(&id, repo, ref);
679 if (err)
680 return err;
682 new = malloc(sizeof(*new));
683 if (new == NULL) {
684 free(id);
685 return got_error_from_errno("malloc");
687 new->ref = ref;
688 new->id = id;
689 *newp = new;
691 /*
692 * We must de-duplicate entries on insert because packed-refs may
693 * contain redundant entries. On-disk refs take precedence.
694 * This code assumes that on-disk revs are read before packed-refs.
695 * We're iterating the list anyway, so insert elements sorted by name.
696 */
697 re = SIMPLEQ_FIRST(refs);
698 while (re) {
699 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
700 if (err)
701 return err;
702 if (cmp == 0) {
703 /* duplicate */
704 free(new->id);
705 free(new);
706 *newp = NULL;
707 return NULL;
708 } else if (cmp > 0) {
709 if (prev)
710 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
711 else
712 SIMPLEQ_INSERT_HEAD(refs, new, entry);
713 return NULL;
714 } else {
715 prev = re;
716 re = SIMPLEQ_NEXT(re, entry);
720 SIMPLEQ_INSERT_TAIL(refs, new, entry);
721 return NULL;
724 static const struct got_error *
725 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
726 const char *subdir, struct got_repository *repo,
727 got_ref_cmp_cb cmp_cb, void *cmp_arg)
729 const struct got_error *err = NULL;
730 DIR *d = NULL;
731 char *path_subdir;
733 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
734 return got_error_from_errno("asprintf");
736 d = opendir(path_subdir);
737 if (d == NULL)
738 goto done;
740 for (;;) {
741 struct dirent *dent;
742 struct got_reference *ref;
743 char *child;
745 dent = readdir(d);
746 if (dent == NULL)
747 break;
749 if (strcmp(dent->d_name, ".") == 0 ||
750 strcmp(dent->d_name, "..") == 0)
751 continue;
753 switch (dent->d_type) {
754 case DT_REG:
755 err = open_ref(&ref, path_refs, subdir, dent->d_name,
756 0);
757 if (err)
758 goto done;
759 if (ref) {
760 struct got_reflist_entry *new;
761 err = insert_ref(&new, refs, ref, repo,
762 cmp_cb, cmp_arg);
763 if (err || new == NULL /* duplicate */)
764 got_ref_close(ref);
765 if (err)
766 goto done;
768 break;
769 case DT_DIR:
770 if (asprintf(&child, "%s%s%s", subdir,
771 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
772 err = got_error_from_errno("asprintf");
773 break;
775 err = gather_on_disk_refs(refs, path_refs, child, repo,
776 cmp_cb, cmp_arg);
777 free(child);
778 break;
779 default:
780 break;
783 done:
784 if (d)
785 closedir(d);
786 free(path_subdir);
787 return err;
790 const struct got_error *
791 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
792 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
794 const struct got_error *err;
795 char *packed_refs_path, *path_refs = NULL;
796 FILE *f = NULL;
797 struct got_reference *ref;
798 struct got_reflist_entry *new;
800 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
801 /* HEAD ref should always exist. */
802 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
803 if (path_refs == NULL) {
804 err = got_error_from_errno("get_refs_dir_path");
805 goto done;
807 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
808 if (err)
809 goto done;
810 err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
811 if (err || new == NULL /* duplicate */)
812 got_ref_close(ref);
813 if (err)
814 goto done;
817 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
818 ref_namespace += 5;
820 /* Gather on-disk refs before parsing packed-refs. */
821 free(path_refs);
822 path_refs = get_refs_dir_path(repo, "");
823 if (path_refs == NULL) {
824 err = got_error_from_errno("get_refs_dir_path");
825 goto done;
827 err = gather_on_disk_refs(refs, path_refs,
828 ref_namespace ? ref_namespace : "", repo, cmp_cb, cmp_arg);
829 if (err)
830 goto done;
832 /*
833 * The packed-refs file may contain redundant entries, in which
834 * case on-disk refs take precedence.
835 */
836 packed_refs_path = got_repo_get_path_packed_refs(repo);
837 if (packed_refs_path == NULL) {
838 err = got_error_from_errno("got_repo_get_path_packed_refs");
839 goto done;
842 f = fopen(packed_refs_path, "r");
843 free(packed_refs_path);
844 if (f) {
845 char *line;
846 size_t len;
847 const char delim[3] = {'\0', '\0', '\0'};
848 for (;;) {
849 line = fparseln(f, &len, NULL, delim, 0);
850 if (line == NULL) {
851 if (feof(f))
852 break;
853 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
854 goto done;
856 err = parse_packed_ref_line(&ref, NULL, line);
857 free(line);
858 if (err)
859 goto done;
860 if (ref) {
861 if (ref_namespace) {
862 const char *name;
863 name = got_ref_get_name(ref);
864 if (strncmp(name, ref_namespace,
865 strlen(ref_namespace)) != 0) {
866 got_ref_close(ref);
867 continue;
870 err = insert_ref(&new, refs, ref, repo,
871 cmp_cb, cmp_arg);
872 if (err || new == NULL /* duplicate */)
873 got_ref_close(ref);
874 if (err)
875 goto done;
879 done:
880 free(path_refs);
881 if (f && fclose(f) != 0 && err == NULL)
882 err = got_error_from_errno("fclose");
883 return err;
886 void
887 got_ref_list_free(struct got_reflist_head *refs)
889 struct got_reflist_entry *re;
891 while (!SIMPLEQ_EMPTY(refs)) {
892 re = SIMPLEQ_FIRST(refs);
893 SIMPLEQ_REMOVE_HEAD(refs, entry);
894 got_ref_close(re->ref);
895 free(re->id);
896 free(re);
901 int
902 got_ref_is_symbolic(struct got_reference *ref)
904 return (ref->flags & GOT_REF_IS_SYMBOLIC);
907 const struct got_error *
908 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
910 if (ref->flags & GOT_REF_IS_SYMBOLIC)
911 return got_error(GOT_ERR_BAD_REF_TYPE);
913 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
914 return NULL;
917 const struct got_error *
918 got_ref_change_symref(struct got_reference *ref, char *refname)
920 char *new_name;
922 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
923 return got_error(GOT_ERR_BAD_REF_TYPE);
925 new_name = strdup(refname);
926 if (new_name == NULL)
927 return got_error_from_errno("strdup");
929 free(ref->ref.symref.name);
930 ref->ref.symref.name = new_name;
931 return NULL;
934 const struct got_error *
935 got_ref_write(struct got_reference *ref, struct got_repository *repo)
937 const struct got_error *err = NULL, *unlock_err = NULL;
938 const char *name = got_ref_get_name(ref);
939 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
940 struct got_lockfile *lf = NULL;
941 FILE *f = NULL;
942 size_t n;
943 struct stat sb;
945 path_refs = get_refs_dir_path(repo, name);
946 if (path_refs == NULL) {
947 err = got_error_from_errno2("get_refs_dir_path", name);
948 goto done;
951 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
952 err = got_error_from_errno("asprintf");
953 goto done;
956 err = got_opentemp_named(&tmppath, &f, path);
957 if (err) {
958 char *parent;
959 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
960 goto done;
961 err = got_path_dirname(&parent, path);
962 if (err)
963 goto done;
964 err = got_path_mkdir(parent);
965 free(parent);
966 if (err)
967 goto done;
968 err = got_opentemp_named(&tmppath, &f, path);
969 if (err)
970 goto done;
973 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
974 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
975 if (n != strlen(ref->ref.symref.ref) + 6) {
976 err = got_ferror(f, GOT_ERR_IO);
977 goto done;
979 } else {
980 char hex[SHA1_DIGEST_STRING_LENGTH];
981 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
982 sizeof(hex)) == NULL) {
983 err = got_error(GOT_ERR_BAD_REF_DATA);
984 goto done;
986 n = fprintf(f, "%s\n", hex);
987 if (n != sizeof(hex)) {
988 err = got_ferror(f, GOT_ERR_IO);
989 goto done;
993 if (ref->lf == NULL) {
994 err = got_lockfile_lock(&lf, path);
995 if (err)
996 goto done;
999 /* XXX: check if old content matches our expectations? */
1001 if (stat(path, &sb) != 0) {
1002 if (errno != ENOENT) {
1003 err = got_error_from_errno2("stat", path);
1004 goto done;
1006 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1009 if (rename(tmppath, path) != 0) {
1010 err = got_error_from_errno3("rename", tmppath, path);
1011 goto done;
1013 free(tmppath);
1014 tmppath = NULL;
1016 if (chmod(path, sb.st_mode) != 0) {
1017 err = got_error_from_errno2("chmod", path);
1018 goto done;
1020 done:
1021 if (ref->lf == NULL && lf)
1022 unlock_err = got_lockfile_unlock(lf);
1023 if (f) {
1024 if (fclose(f) != 0 && err == NULL)
1025 err = got_error_from_errno("fclose");
1027 free(path_refs);
1028 free(path);
1029 if (tmppath) {
1030 if (unlink(tmppath) != 0 && err == NULL)
1031 err = got_error_from_errno2("unlink", tmppath);
1032 free(tmppath);
1034 return err ? err : unlock_err;
1037 static const struct got_error *
1038 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1040 const struct got_error *err = NULL, *unlock_err = NULL;
1041 struct got_lockfile *lf = NULL;
1042 FILE *f = NULL, *tmpf = NULL;
1043 char *packed_refs_path, *tmppath = NULL;
1044 struct got_reflist_head refs;
1045 int found_delref = 0;
1047 /* The packed-refs file does not cotain symbolic references. */
1048 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1049 return got_error(GOT_ERR_BAD_REF_DATA);
1051 SIMPLEQ_INIT(&refs);
1053 packed_refs_path = got_repo_get_path_packed_refs(repo);
1054 if (packed_refs_path == NULL)
1055 return got_error_from_errno("got_repo_get_path_packed_refs");
1057 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1058 if (err)
1059 goto done;
1061 if (delref->lf == NULL) {
1062 err = got_lockfile_lock(&lf, packed_refs_path);
1063 if (err)
1064 goto done;
1067 f = fopen(packed_refs_path, "r");
1068 if (f == NULL) {
1069 err = got_error_from_errno2("fopen", packed_refs_path);
1070 goto done;
1072 for (;;) {
1073 char *line;
1074 size_t len;
1075 const char delim[3] = {'\0', '\0', '\0'};
1076 struct got_reference *ref;
1077 struct got_reflist_entry *new;
1079 line = fparseln(f, &len, NULL, delim, 0);
1080 if (line == NULL) {
1081 if (feof(f))
1082 break;
1083 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1084 goto done;
1086 err = parse_packed_ref_line(&ref, NULL, line);
1087 free(line);
1088 if (err)
1089 goto done;
1090 if (ref == NULL)
1091 continue;
1093 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1094 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1095 sizeof(delref->ref.ref.sha1)) == 0) {
1096 found_delref = 1;
1097 got_ref_close(ref);
1098 continue;
1101 err = insert_ref(&new, &refs, ref, repo,
1102 got_ref_cmp_by_name, NULL);
1103 if (err || new == NULL /* duplicate */)
1104 got_ref_close(ref);
1105 if (err)
1106 goto done;
1109 if (found_delref) {
1110 struct got_reflist_entry *re;
1111 size_t n;
1112 struct stat sb;
1114 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1115 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1116 err = got_ferror(f, GOT_ERR_IO);
1117 goto done;
1120 SIMPLEQ_FOREACH(re, &refs, entry) {
1121 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1123 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1124 sizeof(hex)) == NULL) {
1125 err = got_error(GOT_ERR_BAD_REF_DATA);
1126 goto done;
1128 n = fprintf(tmpf, "%s ", hex);
1129 if (n != sizeof(hex)) {
1130 err = got_ferror(f, GOT_ERR_IO);
1131 goto done;
1133 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1134 if (n != strlen(re->ref->ref.ref.name) + 1) {
1135 err = got_ferror(f, GOT_ERR_IO);
1136 goto done;
1140 if (fflush(tmpf) != 0) {
1141 err = got_error_from_errno("fflush");
1142 goto done;
1145 if (stat(packed_refs_path, &sb) != 0) {
1146 if (errno != ENOENT) {
1147 err = got_error_from_errno2("stat",
1148 packed_refs_path);
1149 goto done;
1151 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1154 if (rename(tmppath, packed_refs_path) != 0) {
1155 err = got_error_from_errno3("rename", tmppath,
1156 packed_refs_path);
1157 goto done;
1160 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1161 err = got_error_from_errno2("chmod",
1162 packed_refs_path);
1163 goto done;
1166 done:
1167 if (delref->lf == NULL && lf)
1168 unlock_err = got_lockfile_unlock(lf);
1169 if (f) {
1170 if (fclose(f) != 0 && err == NULL)
1171 err = got_error_from_errno("fclose");
1173 if (tmpf) {
1174 unlink(tmppath);
1175 if (fclose(tmpf) != 0 && err == NULL)
1176 err = got_error_from_errno("fclose");
1178 free(tmppath);
1179 free(packed_refs_path);
1180 got_ref_list_free(&refs);
1181 return err ? err : unlock_err;
1184 const struct got_error *
1185 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1187 const struct got_error *err = NULL, *unlock_err = NULL;
1188 const char *name = got_ref_get_name(ref);
1189 char *path_refs = NULL, *path = NULL;
1190 struct got_lockfile *lf = NULL;
1192 if (ref->flags & GOT_REF_IS_PACKED)
1193 return delete_packed_ref(ref, repo);
1195 path_refs = get_refs_dir_path(repo, name);
1196 if (path_refs == NULL) {
1197 err = got_error_from_errno2("get_refs_dir_path", name);
1198 goto done;
1201 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1202 err = got_error_from_errno("asprintf");
1203 goto done;
1206 if (ref->lf == NULL) {
1207 err = got_lockfile_lock(&lf, path);
1208 if (err)
1209 goto done;
1212 /* XXX: check if old content matches our expectations? */
1214 if (unlink(path) != 0)
1215 err = got_error_from_errno2("unlink", path);
1216 done:
1217 if (ref->lf == NULL && lf)
1218 unlock_err = got_lockfile_unlock(lf);
1220 free(path_refs);
1221 free(path);
1222 return err ? err : unlock_err;
1225 const struct got_error *
1226 got_ref_unlock(struct got_reference *ref)
1228 const struct got_error *err;
1229 err = got_lockfile_unlock(ref->lf);
1230 ref->lf = NULL;
1231 return err;