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 = NULL;
169 size_t linesize = 0;
170 ssize_t linelen;
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 linelen = getline(&line, &linesize, f);
187 if (linelen == -1) {
188 if (feof(f))
189 err = NULL; /* ignore empty files (could be locks) */
190 else
191 err = got_error_from_errno2("getline", abspath);
192 if (lock)
193 got_lockfile_unlock(lf);
194 goto done;
196 while (linelen > 0 && line[linelen - 1] == '\n') {
197 line[linelen - 1] = '\0';
198 linelen--;
201 err = parse_ref_line(ref, name, line);
202 if (lock) {
203 if (err)
204 got_lockfile_unlock(lf);
205 else {
206 if (*ref)
207 (*ref)->lf = lf;
208 else
209 got_lockfile_unlock(lf);
212 done:
213 free(line);
214 if (fclose(f) != 0 && err == NULL) {
215 err = got_error_from_errno("fclose");
216 if (*ref) {
217 if (lock)
218 got_ref_unlock(*ref);
219 got_ref_close(*ref);
220 *ref = NULL;
223 return err;
226 static int
227 is_well_known_ref(const char *refname)
229 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
230 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
231 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
232 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
235 static char *
236 get_refs_dir_path(struct got_repository *repo, const char *refname)
238 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
239 return strdup(got_repo_get_path_git_dir(repo));
241 return got_repo_get_path_refs(repo);
244 static int
245 is_valid_ref_name(const char *name)
247 const char *s, *slash, *seg;
248 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
249 const char *forbidden_seq[] = { "//", "..", "@{" };
250 const char *lfs = GOT_LOCKFILE_SUFFIX;
251 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
252 int i;
254 if (name[0] == '@' && name[1] == '\0')
255 return 0;
257 slash = strchr(name, '/');
258 if (slash == NULL)
259 return 0;
261 s = name;
262 seg = s;
263 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
264 return 0;
265 while (*s) {
266 for (i = 0; i < nitems(forbidden); i++) {
267 if (*s == forbidden[i])
268 return 0;
270 for (i = 0; i < nitems(forbidden_seq); i++) {
271 if (s[0] == forbidden_seq[i][0] &&
272 s[1] == forbidden_seq[i][1])
273 return 0;
275 if (iscntrl((unsigned char)s[0]))
276 return 0;
277 if (s[0] == '.' && s[1] == '\0')
278 return 0;
279 if (*s == '/') {
280 const char *nextseg = s + 1;
281 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
282 nextseg[0] == '/')
283 return 0;
284 if (seg <= s - lfs_len &&
285 strncmp(s - lfs_len, lfs, lfs_len) == 0)
286 return 0;
287 seg = nextseg;
289 s++;
292 if (seg <= s - lfs_len &&
293 strncmp(s - lfs_len, lfs, lfs_len) == 0)
294 return 0;
296 return 1;
299 const struct got_error *
300 got_ref_alloc(struct got_reference **ref, const char *name,
301 struct got_object_id *id)
303 if (!is_valid_ref_name(name))
304 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
306 return alloc_ref(ref, name, id, 0);
309 const struct got_error *
310 got_ref_alloc_symref(struct got_reference **ref, const char *name,
311 struct got_reference *target_ref)
313 if (!is_valid_ref_name(name))
314 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
316 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
319 static const struct got_error *
320 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
321 const char *line)
323 struct got_object_id id;
324 const char *name;
326 *ref = NULL;
328 if (line[0] == '#' || line[0] == '^')
329 return NULL;
331 if (!got_parse_sha1_digest(id.sha1, line))
332 return got_error(GOT_ERR_BAD_REF_DATA);
334 if (abs_refname) {
335 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
336 return NULL;
337 name = abs_refname;
338 } else
339 name = line + SHA1_DIGEST_STRING_LENGTH;
341 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
344 static const struct got_error *
345 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
346 int nsubdirs, const char *refname)
348 const struct got_error *err = NULL;
349 char *abs_refname;
350 char *line;
351 size_t len;
352 const char delim[3] = {'\0', '\0', '\0'};
353 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
355 *ref = NULL;
357 if (ref_is_absolute)
358 abs_refname = (char *)refname;
359 do {
360 line = fparseln(f, &len, NULL, delim, 0);
361 if (line == NULL) {
362 if (feof(f))
363 break;
364 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
365 break;
367 for (i = 0; i < nsubdirs; i++) {
368 if (!ref_is_absolute &&
369 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
370 refname) == -1)
371 return got_error_from_errno("asprintf");
372 err = parse_packed_ref_line(ref, abs_refname, line);
373 if (!ref_is_absolute)
374 free(abs_refname);
375 if (err || *ref != NULL)
376 break;
378 free(line);
379 if (err)
380 break;
381 } while (*ref == NULL);
383 return err;
386 static const struct got_error *
387 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
388 const char *name, int lock)
390 const struct got_error *err = NULL;
391 char *path = NULL;
392 char *absname = NULL;
393 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
394 int ref_is_well_known = is_well_known_ref(name);
396 *ref = NULL;
398 if (ref_is_absolute || ref_is_well_known) {
399 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
400 return got_error_from_errno("asprintf");
401 absname = (char *)name;
402 } else {
403 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
404 subdir[0] ? "/" : "", name) == -1)
405 return got_error_from_errno("asprintf");
407 if (asprintf(&absname, "refs/%s%s%s",
408 subdir, subdir[0] ? "/" : "", name) == -1) {
409 err = got_error_from_errno("asprintf");
410 goto done;
414 err = parse_ref_file(ref, absname, path, lock);
415 done:
416 if (!ref_is_absolute && !ref_is_well_known)
417 free(absname);
418 free(path);
419 return err;
422 const struct got_error *
423 got_ref_open(struct got_reference **ref, struct got_repository *repo,
424 const char *refname, int lock)
426 const struct got_error *err = NULL;
427 char *path_refs = NULL;
428 const char *subdirs[] = {
429 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
430 };
431 int i, well_known = is_well_known_ref(refname);
432 struct got_lockfile *lf = NULL;
434 *ref = NULL;
436 path_refs = get_refs_dir_path(repo, refname);
437 if (path_refs == NULL) {
438 err = got_error_from_errno2("get_refs_dir_path", refname);
439 goto done;
442 if (well_known) {
443 err = open_ref(ref, path_refs, "", refname, lock);
444 } else {
445 char *packed_refs_path;
446 FILE *f;
448 /* Search on-disk refs before packed refs! */
449 for (i = 0; i < nitems(subdirs); i++) {
450 err = open_ref(ref, path_refs, subdirs[i], refname,
451 lock);
452 if (err || *ref)
453 goto done;
456 packed_refs_path = got_repo_get_path_packed_refs(repo);
457 if (packed_refs_path == NULL) {
458 err = got_error_from_errno(
459 "got_repo_get_path_packed_refs");
460 goto done;
463 if (lock) {
464 err = got_lockfile_lock(&lf, packed_refs_path);
465 if (err)
466 goto done;
468 f = fopen(packed_refs_path, "rb");
469 free(packed_refs_path);
470 if (f != NULL) {
471 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
472 refname);
473 if (!err) {
474 if (fclose(f) != 0) {
475 err = got_error_from_errno("fclose");
476 got_ref_close(*ref);
477 *ref = NULL;
478 } else if (*ref)
479 (*ref)->lf = lf;
483 done:
484 if (!err && *ref == NULL)
485 err = got_error_not_ref(refname);
486 if (err && lf)
487 got_lockfile_unlock(lf);
488 free(path_refs);
489 return err;
492 void
493 got_ref_close(struct got_reference *ref)
495 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
496 free(ref->ref.symref.name);
497 free(ref->ref.symref.ref);
498 } else
499 free(ref->ref.ref.name);
500 free(ref);
503 struct got_reference *
504 got_ref_dup(struct got_reference *ref)
506 struct got_reference *ret;
508 ret = calloc(1, sizeof(*ret));
509 if (ret == NULL)
510 return NULL;
512 ret->flags = ref->flags;
513 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
514 ret->ref.symref.name = strdup(ref->ref.symref.name);
515 if (ret->ref.symref.name == NULL) {
516 free(ret);
517 return NULL;
519 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
520 if (ret->ref.symref.ref == NULL) {
521 free(ret->ref.symref.name);
522 free(ret);
523 return NULL;
525 } else {
526 ref->ref.ref.name = strdup(ref->ref.ref.name);
527 if (ref->ref.ref.name == NULL) {
528 free(ret);
529 return NULL;
531 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
532 sizeof(ret->ref.ref.sha1));
535 return ret;
538 const struct got_error *
539 got_reflist_entry_dup(struct got_reflist_entry **newp,
540 struct got_reflist_entry *re)
542 const struct got_error *err = NULL;
543 struct got_reflist_entry *new;
545 *newp = NULL;
547 new = malloc(sizeof(*new));
548 if (new == NULL)
549 return got_error_from_errno("malloc");
551 new->ref = got_ref_dup(re->ref);
552 if (new->ref == NULL) {
553 err = got_error_from_errno("got_ref_dup");
554 free(new);
555 return err;
558 new->id = got_object_id_dup(re->id);
559 if (new->id == NULL) {
560 err = got_error_from_errno("got_ref_dup");
561 free(new->id);
562 free(new);
563 return err;
566 *newp = new;
567 return NULL;
570 static const struct got_error *
571 resolve_symbolic_ref(struct got_reference **resolved,
572 struct got_repository *repo, struct got_reference *ref)
574 struct got_reference *nextref;
575 const struct got_error *err;
577 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
578 if (err)
579 return err;
581 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
582 err = resolve_symbolic_ref(resolved, repo, nextref);
583 else
584 *resolved = got_ref_dup(nextref);
586 got_ref_close(nextref);
587 return err;
590 static const struct got_error *
591 ref_resolve(struct got_object_id **id, struct got_repository *repo,
592 struct got_reference *ref, int recursion)
594 const struct got_error *err;
596 if (recursion <= 0)
597 return got_error_msg(GOT_ERR_RECURSION,
598 "reference recursion limit reached");
600 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
601 struct got_reference *resolved = NULL;
602 err = resolve_symbolic_ref(&resolved, repo, ref);
603 if (err == NULL)
604 err = ref_resolve(id, repo, resolved, --recursion);
605 if (resolved)
606 got_ref_close(resolved);
607 return err;
610 *id = calloc(1, sizeof(**id));
611 if (*id == NULL)
612 return got_error_from_errno("calloc");
613 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
614 return NULL;
617 const struct got_error *
618 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
619 struct got_reference *ref)
621 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
624 char *
625 got_ref_to_str(struct got_reference *ref)
627 char *str;
629 if (ref->flags & GOT_REF_IS_SYMBOLIC)
630 return strdup(ref->ref.symref.ref);
632 str = malloc(SHA1_DIGEST_STRING_LENGTH);
633 if (str == NULL)
634 return NULL;
636 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
637 SHA1_DIGEST_STRING_LENGTH) == NULL) {
638 free(str);
639 return NULL;
642 return str;
645 const char *
646 got_ref_get_name(struct got_reference *ref)
648 if (ref->flags & GOT_REF_IS_SYMBOLIC)
649 return ref->ref.symref.name;
651 return ref->ref.ref.name;
654 const char *
655 got_ref_get_symref_target(struct got_reference *ref)
657 if (ref->flags & GOT_REF_IS_SYMBOLIC)
658 return ref->ref.symref.ref;
660 return NULL;
663 const struct got_error *
664 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
665 struct got_reference* re2)
667 const char *name1 = got_ref_get_name(re1);
668 const char *name2 = got_ref_get_name(re2);
670 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
671 return NULL;
674 static const struct got_error *
675 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
676 struct got_reference *ref, struct got_repository *repo,
677 got_ref_cmp_cb cmp_cb, void *cmp_arg)
679 const struct got_error *err;
680 struct got_object_id *id;
681 struct got_reflist_entry *new, *re, *prev = NULL;
682 int cmp;
684 *newp = NULL;
686 err = got_ref_resolve(&id, repo, ref);
687 if (err)
688 return err;
690 new = malloc(sizeof(*new));
691 if (new == NULL) {
692 free(id);
693 return got_error_from_errno("malloc");
695 new->ref = ref;
696 new->id = id;
697 *newp = new;
699 /*
700 * We must de-duplicate entries on insert because packed-refs may
701 * contain redundant entries. On-disk refs take precedence.
702 * This code assumes that on-disk revs are read before packed-refs.
703 * We're iterating the list anyway, so insert elements sorted by name.
704 */
705 re = SIMPLEQ_FIRST(refs);
706 while (re) {
707 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
708 if (err)
709 return err;
710 if (cmp == 0) {
711 /* duplicate */
712 free(new->id);
713 free(new);
714 *newp = NULL;
715 return NULL;
716 } else if (cmp > 0) {
717 if (prev)
718 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
719 else
720 SIMPLEQ_INSERT_HEAD(refs, new, entry);
721 return NULL;
722 } else {
723 prev = re;
724 re = SIMPLEQ_NEXT(re, entry);
728 SIMPLEQ_INSERT_TAIL(refs, new, entry);
729 return NULL;
732 static const struct got_error *
733 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
734 const char *subdir, struct got_repository *repo,
735 got_ref_cmp_cb cmp_cb, void *cmp_arg)
737 const struct got_error *err = NULL;
738 DIR *d = NULL;
739 char *path_subdir;
741 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
742 return got_error_from_errno("asprintf");
744 d = opendir(path_subdir);
745 if (d == NULL)
746 goto done;
748 for (;;) {
749 struct dirent *dent;
750 struct got_reference *ref;
751 char *child;
753 dent = readdir(d);
754 if (dent == NULL)
755 break;
757 if (strcmp(dent->d_name, ".") == 0 ||
758 strcmp(dent->d_name, "..") == 0)
759 continue;
761 switch (dent->d_type) {
762 case DT_REG:
763 err = open_ref(&ref, path_refs, subdir, dent->d_name,
764 0);
765 if (err)
766 goto done;
767 if (ref) {
768 struct got_reflist_entry *new;
769 err = insert_ref(&new, refs, ref, repo,
770 cmp_cb, cmp_arg);
771 if (err || new == NULL /* duplicate */)
772 got_ref_close(ref);
773 if (err)
774 goto done;
776 break;
777 case DT_DIR:
778 if (asprintf(&child, "%s%s%s", subdir,
779 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
780 err = got_error_from_errno("asprintf");
781 break;
783 err = gather_on_disk_refs(refs, path_refs, child, repo,
784 cmp_cb, cmp_arg);
785 free(child);
786 break;
787 default:
788 break;
791 done:
792 if (d)
793 closedir(d);
794 free(path_subdir);
795 return err;
798 const struct got_error *
799 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
800 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
802 const struct got_error *err;
803 char *packed_refs_path, *path_refs = NULL;
804 const char *ondisk_ref_namespace = NULL;
805 FILE *f = NULL;
806 struct got_reference *ref;
807 struct got_reflist_entry *new;
809 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
810 /* HEAD ref should always exist. */
811 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
812 if (path_refs == NULL) {
813 err = got_error_from_errno("get_refs_dir_path");
814 goto done;
816 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
817 if (err)
818 goto done;
819 err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
820 if (err || new == NULL /* duplicate */)
821 got_ref_close(ref);
822 if (err)
823 goto done;
826 ondisk_ref_namespace = ref_namespace;
827 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
828 ondisk_ref_namespace += 5;
830 /* Gather on-disk refs before parsing packed-refs. */
831 free(path_refs);
832 path_refs = get_refs_dir_path(repo, "");
833 if (path_refs == NULL) {
834 err = got_error_from_errno("get_refs_dir_path");
835 goto done;
837 err = gather_on_disk_refs(refs, path_refs,
838 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
839 cmp_cb, cmp_arg);
840 if (err)
841 goto done;
843 /*
844 * The packed-refs file may contain redundant entries, in which
845 * case on-disk refs take precedence.
846 */
847 packed_refs_path = got_repo_get_path_packed_refs(repo);
848 if (packed_refs_path == NULL) {
849 err = got_error_from_errno("got_repo_get_path_packed_refs");
850 goto done;
853 f = fopen(packed_refs_path, "r");
854 free(packed_refs_path);
855 if (f) {
856 char *line;
857 size_t len;
858 const char delim[3] = {'\0', '\0', '\0'};
859 for (;;) {
860 line = fparseln(f, &len, NULL, delim, 0);
861 if (line == NULL) {
862 if (feof(f))
863 break;
864 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
865 goto done;
867 err = parse_packed_ref_line(&ref, NULL, line);
868 free(line);
869 if (err)
870 goto done;
871 if (ref) {
872 if (ref_namespace) {
873 const char *name;
874 name = got_ref_get_name(ref);
875 if (strncmp(name, ref_namespace,
876 strlen(ref_namespace)) != 0) {
877 got_ref_close(ref);
878 continue;
881 err = insert_ref(&new, refs, ref, repo,
882 cmp_cb, cmp_arg);
883 if (err || new == NULL /* duplicate */)
884 got_ref_close(ref);
885 if (err)
886 goto done;
890 done:
891 free(path_refs);
892 if (f && fclose(f) != 0 && err == NULL)
893 err = got_error_from_errno("fclose");
894 return err;
897 void
898 got_ref_list_free(struct got_reflist_head *refs)
900 struct got_reflist_entry *re;
902 while (!SIMPLEQ_EMPTY(refs)) {
903 re = SIMPLEQ_FIRST(refs);
904 SIMPLEQ_REMOVE_HEAD(refs, entry);
905 got_ref_close(re->ref);
906 free(re->id);
907 free(re);
912 int
913 got_ref_is_symbolic(struct got_reference *ref)
915 return (ref->flags & GOT_REF_IS_SYMBOLIC);
918 const struct got_error *
919 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
921 if (ref->flags & GOT_REF_IS_SYMBOLIC)
922 return got_error(GOT_ERR_BAD_REF_TYPE);
924 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
925 return NULL;
928 const struct got_error *
929 got_ref_change_symref(struct got_reference *ref, char *refname)
931 char *new_name;
933 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
934 return got_error(GOT_ERR_BAD_REF_TYPE);
936 new_name = strdup(refname);
937 if (new_name == NULL)
938 return got_error_from_errno("strdup");
940 free(ref->ref.symref.name);
941 ref->ref.symref.name = new_name;
942 return NULL;
945 const struct got_error *
946 got_ref_write(struct got_reference *ref, struct got_repository *repo)
948 const struct got_error *err = NULL, *unlock_err = NULL;
949 const char *name = got_ref_get_name(ref);
950 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
951 struct got_lockfile *lf = NULL;
952 FILE *f = NULL;
953 size_t n;
954 struct stat sb;
956 path_refs = get_refs_dir_path(repo, name);
957 if (path_refs == NULL) {
958 err = got_error_from_errno2("get_refs_dir_path", name);
959 goto done;
962 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
963 err = got_error_from_errno("asprintf");
964 goto done;
967 err = got_opentemp_named(&tmppath, &f, path);
968 if (err) {
969 char *parent;
970 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
971 goto done;
972 err = got_path_dirname(&parent, path);
973 if (err)
974 goto done;
975 err = got_path_mkdir(parent);
976 free(parent);
977 if (err)
978 goto done;
979 err = got_opentemp_named(&tmppath, &f, path);
980 if (err)
981 goto done;
984 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
985 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
986 if (n != strlen(ref->ref.symref.ref) + 6) {
987 err = got_ferror(f, GOT_ERR_IO);
988 goto done;
990 } else {
991 char hex[SHA1_DIGEST_STRING_LENGTH];
992 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
993 sizeof(hex)) == NULL) {
994 err = got_error(GOT_ERR_BAD_REF_DATA);
995 goto done;
997 n = fprintf(f, "%s\n", hex);
998 if (n != sizeof(hex)) {
999 err = got_ferror(f, GOT_ERR_IO);
1000 goto done;
1004 if (ref->lf == NULL) {
1005 err = got_lockfile_lock(&lf, path);
1006 if (err)
1007 goto done;
1010 /* XXX: check if old content matches our expectations? */
1012 if (stat(path, &sb) != 0) {
1013 if (errno != ENOENT) {
1014 err = got_error_from_errno2("stat", path);
1015 goto done;
1017 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1020 if (rename(tmppath, path) != 0) {
1021 err = got_error_from_errno3("rename", tmppath, path);
1022 goto done;
1024 free(tmppath);
1025 tmppath = NULL;
1027 if (chmod(path, sb.st_mode) != 0) {
1028 err = got_error_from_errno2("chmod", path);
1029 goto done;
1031 done:
1032 if (ref->lf == NULL && lf)
1033 unlock_err = got_lockfile_unlock(lf);
1034 if (f) {
1035 if (fclose(f) != 0 && err == NULL)
1036 err = got_error_from_errno("fclose");
1038 free(path_refs);
1039 free(path);
1040 if (tmppath) {
1041 if (unlink(tmppath) != 0 && err == NULL)
1042 err = got_error_from_errno2("unlink", tmppath);
1043 free(tmppath);
1045 return err ? err : unlock_err;
1048 static const struct got_error *
1049 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1051 const struct got_error *err = NULL, *unlock_err = NULL;
1052 struct got_lockfile *lf = NULL;
1053 FILE *f = NULL, *tmpf = NULL;
1054 char *packed_refs_path, *tmppath = NULL;
1055 struct got_reflist_head refs;
1056 int found_delref = 0;
1058 /* The packed-refs file does not cotain symbolic references. */
1059 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1060 return got_error(GOT_ERR_BAD_REF_DATA);
1062 SIMPLEQ_INIT(&refs);
1064 packed_refs_path = got_repo_get_path_packed_refs(repo);
1065 if (packed_refs_path == NULL)
1066 return got_error_from_errno("got_repo_get_path_packed_refs");
1068 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1069 if (err)
1070 goto done;
1072 if (delref->lf == NULL) {
1073 err = got_lockfile_lock(&lf, packed_refs_path);
1074 if (err)
1075 goto done;
1078 f = fopen(packed_refs_path, "r");
1079 if (f == NULL) {
1080 err = got_error_from_errno2("fopen", packed_refs_path);
1081 goto done;
1083 for (;;) {
1084 char *line;
1085 size_t len;
1086 const char delim[3] = {'\0', '\0', '\0'};
1087 struct got_reference *ref;
1088 struct got_reflist_entry *new;
1090 line = fparseln(f, &len, NULL, delim, 0);
1091 if (line == NULL) {
1092 if (feof(f))
1093 break;
1094 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1095 goto done;
1097 err = parse_packed_ref_line(&ref, NULL, line);
1098 free(line);
1099 if (err)
1100 goto done;
1101 if (ref == NULL)
1102 continue;
1104 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1105 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1106 sizeof(delref->ref.ref.sha1)) == 0) {
1107 found_delref = 1;
1108 got_ref_close(ref);
1109 continue;
1112 err = insert_ref(&new, &refs, ref, repo,
1113 got_ref_cmp_by_name, NULL);
1114 if (err || new == NULL /* duplicate */)
1115 got_ref_close(ref);
1116 if (err)
1117 goto done;
1120 if (found_delref) {
1121 struct got_reflist_entry *re;
1122 size_t n;
1123 struct stat sb;
1125 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1126 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1127 err = got_ferror(f, GOT_ERR_IO);
1128 goto done;
1131 SIMPLEQ_FOREACH(re, &refs, entry) {
1132 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1134 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1135 sizeof(hex)) == NULL) {
1136 err = got_error(GOT_ERR_BAD_REF_DATA);
1137 goto done;
1139 n = fprintf(tmpf, "%s ", hex);
1140 if (n != sizeof(hex)) {
1141 err = got_ferror(f, GOT_ERR_IO);
1142 goto done;
1144 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1145 if (n != strlen(re->ref->ref.ref.name) + 1) {
1146 err = got_ferror(f, GOT_ERR_IO);
1147 goto done;
1151 if (fflush(tmpf) != 0) {
1152 err = got_error_from_errno("fflush");
1153 goto done;
1156 if (stat(packed_refs_path, &sb) != 0) {
1157 if (errno != ENOENT) {
1158 err = got_error_from_errno2("stat",
1159 packed_refs_path);
1160 goto done;
1162 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1165 if (rename(tmppath, packed_refs_path) != 0) {
1166 err = got_error_from_errno3("rename", tmppath,
1167 packed_refs_path);
1168 goto done;
1171 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1172 err = got_error_from_errno2("chmod",
1173 packed_refs_path);
1174 goto done;
1177 done:
1178 if (delref->lf == NULL && lf)
1179 unlock_err = got_lockfile_unlock(lf);
1180 if (f) {
1181 if (fclose(f) != 0 && err == NULL)
1182 err = got_error_from_errno("fclose");
1184 if (tmpf) {
1185 unlink(tmppath);
1186 if (fclose(tmpf) != 0 && err == NULL)
1187 err = got_error_from_errno("fclose");
1189 free(tmppath);
1190 free(packed_refs_path);
1191 got_ref_list_free(&refs);
1192 return err ? err : unlock_err;
1195 const struct got_error *
1196 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1198 const struct got_error *err = NULL, *unlock_err = NULL;
1199 const char *name = got_ref_get_name(ref);
1200 char *path_refs = NULL, *path = NULL;
1201 struct got_lockfile *lf = NULL;
1203 if (ref->flags & GOT_REF_IS_PACKED)
1204 return delete_packed_ref(ref, repo);
1206 path_refs = get_refs_dir_path(repo, name);
1207 if (path_refs == NULL) {
1208 err = got_error_from_errno2("get_refs_dir_path", name);
1209 goto done;
1212 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1213 err = got_error_from_errno("asprintf");
1214 goto done;
1217 if (ref->lf == NULL) {
1218 err = got_lockfile_lock(&lf, path);
1219 if (err)
1220 goto done;
1223 /* XXX: check if old content matches our expectations? */
1225 if (unlink(path) != 0)
1226 err = got_error_from_errno2("unlink", path);
1227 done:
1228 if (ref->lf == NULL && lf)
1229 unlock_err = got_lockfile_unlock(lf);
1231 free(path_refs);
1232 free(path);
1233 return err ? err : unlock_err;
1236 const struct got_error *
1237 got_ref_unlock(struct got_reference *ref)
1239 const struct got_error *err;
1240 err = got_lockfile_unlock(ref->lf);
1241 ref->lf = NULL;
1242 return err;