2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.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"
42 #include "got_lib_hash.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_lockfile.h"
50 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 #define GOT_REF_HEADS "heads"
54 #define GOT_REF_TAGS "tags"
55 #define GOT_REF_REMOTES "remotes"
58 * We do not resolve tags yet, and don't yet care about sorting refs either,
59 * so packed-refs files we write contain a minimal header which disables all
60 * packed-refs "traits" supported by Git.
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
70 #define GOT_REF_RECURSE_MAX 20
72 /* A non-symbolic reference (there is no better designation). */
75 struct got_object_id id
;
78 /* A reference which points to an arbitrary object. */
79 struct got_reference
{
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
86 struct got_symref symref
;
89 struct got_lockfile
*lf
;
93 * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
94 * and got_ref_cmp_tags().
96 time_t committer_time
;
99 static const struct got_error
*
100 alloc_ref(struct got_reference
**ref
, const char *name
,
101 struct got_object_id
*id
, int flags
, time_t mtime
)
103 const struct got_error
*err
= NULL
;
105 *ref
= calloc(1, sizeof(**ref
));
107 return got_error_from_errno("calloc");
109 memcpy(&(*ref
)->ref
.ref
.id
, id
, sizeof((*ref
)->ref
.ref
.id
));
110 (*ref
)->flags
= flags
;
111 (*ref
)->ref
.ref
.name
= strdup(name
);
112 (*ref
)->mtime
= mtime
;
113 if ((*ref
)->ref
.ref
.name
== NULL
) {
114 err
= got_error_from_errno("strdup");
121 static const struct got_error
*
122 alloc_symref(struct got_reference
**ref
, const char *name
,
123 const char *target_ref
, int flags
)
125 const struct got_error
*err
= NULL
;
127 *ref
= calloc(1, sizeof(**ref
));
129 return got_error_from_errno("calloc");
131 (*ref
)->flags
= GOT_REF_IS_SYMBOLIC
| flags
;
132 (*ref
)->ref
.symref
.name
= strdup(name
);
133 if ((*ref
)->ref
.symref
.name
== NULL
) {
134 err
= got_error_from_errno("strdup");
139 (*ref
)->ref
.symref
.ref
= strdup(target_ref
);
140 if ((*ref
)->ref
.symref
.ref
== NULL
) {
141 err
= got_error_from_errno("strdup");
148 static const struct got_error
*
149 parse_symref(struct got_reference
**ref
, const char *name
, const char *line
)
152 return got_error(GOT_ERR_BAD_REF_DATA
);
154 return alloc_symref(ref
, name
, line
, 0);
157 static const struct got_error
*
158 parse_ref_line(struct got_reference
**ref
, const char *name
, const char *line
,
159 time_t mtime
, enum got_hash_algorithm algo
)
161 struct got_object_id id
;
163 if (strncmp(line
, "ref: ", 5) == 0) {
165 return parse_symref(ref
, name
, line
);
168 if (!got_parse_object_id(&id
, line
, algo
))
169 return got_error(GOT_ERR_BAD_REF_DATA
);
171 return alloc_ref(ref
, name
, &id
, 0, mtime
);
174 static const struct got_error
*
175 parse_ref_file(struct got_reference
**ref
, const char *name
,
176 const char *absname
, const char *abspath
, int lock
,
177 enum got_hash_algorithm algo
)
179 const struct got_error
*err
= NULL
;
184 struct got_lockfile
*lf
= NULL
;
188 err
= got_lockfile_lock(&lf
, abspath
, -1);
190 if (err
->code
== GOT_ERR_ERRNO
&& errno
== ENOENT
)
191 err
= got_error_not_ref(name
);
196 f
= fopen(abspath
, "rbe");
198 if (errno
!= ENOTDIR
&& errno
!= ENOENT
)
199 err
= got_error_from_errno2("fopen", abspath
);
201 err
= got_error_not_ref(name
);
203 got_lockfile_unlock(lf
, -1);
206 if (fstat(fileno(f
), &sb
) == -1) {
207 err
= got_error_from_errno2("fstat", abspath
);
211 linelen
= getline(&line
, &linesize
, f
);
214 err
= NULL
; /* ignore empty files (could be locks) */
217 err
= got_error(GOT_ERR_NOT_REF
);
219 err
= got_ferror(f
, GOT_ERR_IO
);
221 err
= got_error_from_errno2("getline", abspath
);
224 got_lockfile_unlock(lf
, -1);
227 while (linelen
> 0 && line
[linelen
- 1] == '\n') {
228 line
[linelen
- 1] = '\0';
232 err
= parse_ref_line(ref
, absname
, line
, sb
.st_mtime
, algo
);
235 got_lockfile_unlock(lf
, -1);
240 got_lockfile_unlock(lf
, -1);
245 if (fclose(f
) == EOF
&& err
== NULL
) {
246 err
= got_error_from_errno("fclose");
249 got_ref_unlock(*ref
);
258 is_well_known_ref(const char *refname
)
260 return (strcmp(refname
, GOT_REF_HEAD
) == 0 ||
261 strcmp(refname
, GOT_REF_ORIG_HEAD
) == 0 ||
262 strcmp(refname
, GOT_REF_MERGE_HEAD
) == 0 ||
263 strcmp(refname
, GOT_REF_FETCH_HEAD
) == 0);
267 get_refs_dir_path(struct got_repository
*repo
, const char *refname
)
269 if (is_well_known_ref(refname
) || strncmp(refname
, "refs/", 5) == 0)
270 return strdup(got_repo_get_path_git_dir(repo
));
272 return got_repo_get_path_refs(repo
);
275 const struct got_error
*
276 got_ref_alloc(struct got_reference
**ref
, const char *name
,
277 struct got_object_id
*id
)
279 if (!got_ref_name_is_valid(name
))
280 return got_error_path(name
, GOT_ERR_BAD_REF_NAME
);
282 return alloc_ref(ref
, name
, id
, 0, 0);
285 const struct got_error
*
286 got_ref_alloc_symref(struct got_reference
**ref
, const char *name
,
287 struct got_reference
*target_ref
)
289 if (!got_ref_name_is_valid(name
))
290 return got_error_path(name
, GOT_ERR_BAD_REF_NAME
);
292 return alloc_symref(ref
, name
, got_ref_get_name(target_ref
), 0);
295 static const struct got_error
*
296 parse_packed_ref_line(struct got_reference
**ref
, const char *abs_refname
,
297 const char *line
, time_t mtime
, enum got_hash_algorithm algo
)
299 struct got_object_id id
;
301 size_t digest_string_len
;
304 digest_string_len
= got_hash_digest_string_length(algo
);
306 if (line
[0] == '#' || line
[0] == '^')
309 if (!got_parse_object_id(&id
, line
, algo
))
310 return got_error(GOT_ERR_BAD_REF_DATA
);
313 if (strcmp(line
+ digest_string_len
, abs_refname
) != 0)
317 name
= line
+ digest_string_len
;
319 return alloc_ref(ref
, name
, &id
, GOT_REF_IS_PACKED
, mtime
);
322 static const struct got_error
*
323 open_packed_ref(struct got_reference
**ref
, FILE *f
, const char **subdirs
,
324 int nsubdirs
, const char *refname
, time_t mtime
,
325 enum got_hash_algorithm algo
)
327 const struct got_error
*err
= NULL
;
332 int i
, ref_is_absolute
= (strncmp(refname
, "refs/", 5) == 0);
337 abs_refname
= (char *)refname
;
339 linelen
= getline(&line
, &linesize
, f
);
343 err
= got_ferror(f
, GOT_ERR_BAD_REF_DATA
);
346 if (linelen
> 0 && line
[linelen
- 1] == '\n')
347 line
[linelen
- 1] = '\0';
348 for (i
= 0; i
< nsubdirs
; i
++) {
349 if (!ref_is_absolute
&&
350 asprintf(&abs_refname
, "refs/%s/%s", subdirs
[i
],
352 return got_error_from_errno("asprintf");
353 err
= parse_packed_ref_line(ref
, abs_refname
, line
,
355 if (!ref_is_absolute
)
357 if (err
|| *ref
!= NULL
)
362 } while (*ref
== NULL
);
368 static const struct got_error
*
369 open_ref(struct got_reference
**ref
, const char *path_refs
, const char *subdir
,
370 const char *name
, int lock
, enum got_hash_algorithm algo
)
372 const struct got_error
*err
= NULL
;
374 char *absname
= NULL
;
375 int ref_is_absolute
= (strncmp(name
, "refs/", 5) == 0);
376 int ref_is_well_known
= (subdir
[0] == '\0' && is_well_known_ref(name
));
380 if (!got_ref_name_is_valid(name
))
381 return got_error_path(name
, GOT_ERR_BAD_REF_NAME
);
383 if (ref_is_absolute
|| ref_is_well_known
) {
384 if (asprintf(&path
, "%s/%s", path_refs
, name
) == -1)
385 return got_error_from_errno("asprintf");
386 absname
= (char *)name
;
388 if (asprintf(&path
, "%s/%s%s%s", path_refs
, subdir
,
389 subdir
[0] ? "/" : "", name
) == -1)
390 return got_error_from_errno("asprintf");
392 if (asprintf(&absname
, "refs/%s%s%s",
393 subdir
, subdir
[0] ? "/" : "", name
) == -1) {
394 err
= got_error_from_errno("asprintf");
399 err
= parse_ref_file(ref
, name
, absname
, path
, lock
, algo
);
401 if (!ref_is_absolute
&& !ref_is_well_known
)
407 const struct got_error
*
408 got_ref_open(struct got_reference
**ref
, struct got_repository
*repo
,
409 const char *refname
, int lock
)
411 const struct got_error
*err
= NULL
;
412 char *packed_refs_path
= NULL
, *path_refs
= NULL
;
413 const char *subdirs
[] = {
414 GOT_REF_HEADS
, GOT_REF_TAGS
, GOT_REF_REMOTES
417 int well_known
= is_well_known_ref(refname
);
418 struct got_lockfile
*lf
= NULL
;
422 path_refs
= get_refs_dir_path(repo
, refname
);
423 if (path_refs
== NULL
) {
424 err
= got_error_from_errno2("get_refs_dir_path", refname
);
429 err
= open_ref(ref
, path_refs
, "", refname
, lock
,
430 got_repo_get_object_format(repo
));
434 /* Search on-disk refs before packed refs! */
435 for (i
= 0; i
< nitems(subdirs
); i
++) {
436 err
= open_ref(ref
, path_refs
, subdirs
[i
], refname
,
437 lock
, got_repo_get_object_format(repo
));
438 if ((err
&& err
->code
!= GOT_ERR_NOT_REF
) || *ref
)
442 packed_refs_path
= got_repo_get_path_packed_refs(repo
);
443 if (packed_refs_path
== NULL
) {
444 err
= got_error_from_errno(
445 "got_repo_get_path_packed_refs");
450 err
= got_lockfile_lock(&lf
, packed_refs_path
, -1);
454 f
= fopen(packed_refs_path
, "rbe");
457 if (fstat(fileno(f
), &sb
) == -1) {
458 err
= got_error_from_errno2("fstat",
462 err
= open_packed_ref(ref
, f
, subdirs
, nitems(subdirs
),
463 refname
, sb
.st_mtime
,
464 got_repo_get_object_format(repo
));
466 if (fclose(f
) == EOF
) {
467 err
= got_error_from_errno("fclose");
476 if (!err
&& *ref
== NULL
)
477 err
= got_error_not_ref(refname
);
479 got_lockfile_unlock(lf
, -1);
480 free(packed_refs_path
);
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
);
492 free(ref
->ref
.ref
.name
);
496 struct got_reference
*
497 got_ref_dup(struct got_reference
*ref
)
499 struct got_reference
*ret
;
501 ret
= calloc(1, sizeof(*ret
));
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
) {
512 ret
->ref
.symref
.ref
= strdup(ref
->ref
.symref
.ref
);
513 if (ret
->ref
.symref
.ref
== NULL
) {
514 free(ret
->ref
.symref
.name
);
519 ret
->ref
.ref
.name
= strdup(ref
->ref
.ref
.name
);
520 if (ret
->ref
.ref
.name
== NULL
) {
524 memcpy(&ret
->ref
.ref
.id
, &ref
->ref
.ref
.id
,
525 sizeof(ret
->ref
.ref
.id
));
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;
540 new = malloc(sizeof(*new));
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");
555 const struct got_error
*
556 got_ref_resolve_symbolic(struct got_reference
**resolved
,
557 struct got_repository
*repo
, struct got_reference
*ref
)
559 struct got_reference
*nextref
;
560 const struct got_error
*err
;
562 err
= got_ref_open(&nextref
, repo
, ref
->ref
.symref
.ref
, 0);
566 if (nextref
->flags
& GOT_REF_IS_SYMBOLIC
)
567 err
= got_ref_resolve_symbolic(resolved
, repo
, nextref
);
569 *resolved
= got_ref_dup(nextref
);
571 got_ref_close(nextref
);
575 static const struct got_error
*
576 ref_resolve(struct got_object_id
**id
, struct got_repository
*repo
,
577 struct got_reference
*ref
, int recursion
)
579 const struct got_error
*err
;
582 return got_error_msg(GOT_ERR_RECURSION
,
583 "reference recursion limit reached");
585 if (ref
->flags
& GOT_REF_IS_SYMBOLIC
) {
586 struct got_reference
*resolved
= NULL
;
587 err
= got_ref_resolve_symbolic(&resolved
, repo
, ref
);
589 err
= ref_resolve(id
, repo
, resolved
, --recursion
);
591 got_ref_close(resolved
);
595 *id
= calloc(1, sizeof(**id
));
597 return got_error_from_errno("calloc");
598 memcpy(*id
, &ref
->ref
.ref
.id
, sizeof(**id
));
602 const struct got_error
*
603 got_ref_resolve(struct got_object_id
**id
, struct got_repository
*repo
,
604 struct got_reference
*ref
)
606 return ref_resolve(id
, repo
, ref
, GOT_REF_RECURSE_MAX
);
610 got_ref_to_str(struct got_reference
*ref
)
614 if (ref
->flags
& GOT_REF_IS_SYMBOLIC
)
615 return strdup(ref
->ref
.symref
.ref
);
617 if (got_object_id_str(&str
, &ref
->ref
.ref
.id
) != NULL
)
624 got_ref_get_name(struct got_reference
*ref
)
626 if (ref
->flags
& GOT_REF_IS_SYMBOLIC
)
627 return ref
->ref
.symref
.name
;
629 return ref
->ref
.ref
.name
;
633 got_ref_get_symref_target(struct got_reference
*ref
)
635 if (ref
->flags
& GOT_REF_IS_SYMBOLIC
)
636 return ref
->ref
.symref
.ref
;
642 got_ref_get_mtime(struct got_reference
*ref
)
648 got_ref_get_cached_committer_time(struct got_reference
*ref
)
650 return ref
->committer_time
;
653 const struct got_error
*
654 got_ref_cmp_by_name(void *arg
, int *cmp
, struct got_reference
*re1
,
655 struct got_reference
* re2
)
657 const char *name1
= got_ref_get_name(re1
);
658 const char *name2
= got_ref_get_name(re2
);
660 *cmp
= got_path_cmp(name1
, name2
, strlen(name1
), strlen(name2
));
664 static const struct got_error
*
665 get_committer_time(struct got_reference
*ref
, struct got_repository
*repo
)
667 const struct got_error
*err
= NULL
;
669 struct got_commit_object
*commit
= NULL
;
670 struct got_tag_object
*tag
= NULL
;
671 struct got_object_id
*id
= NULL
;
673 err
= got_ref_resolve(&id
, repo
, ref
);
677 err
= got_object_get_type(&obj_type
, repo
, id
);
682 case GOT_OBJ_TYPE_COMMIT
:
683 err
= got_object_open_as_commit(&commit
, repo
, id
);
686 ref
->committer_time
=
687 got_object_commit_get_committer_time(commit
);
689 case GOT_OBJ_TYPE_TAG
:
690 err
= got_object_open_as_tag(&tag
, repo
, id
);
693 ref
->committer_time
= got_object_tag_get_tagger_time(tag
);
696 /* best effort for other object types */
697 ref
->committer_time
= got_ref_get_mtime(ref
);
703 got_object_commit_close(commit
);
705 got_object_tag_close(tag
);
709 const struct got_error
*
710 got_ref_cmp_tags(void *arg
, int *cmp
, struct got_reference
*ref1
,
711 struct got_reference
*ref2
)
713 const struct got_error
*err
= NULL
;
714 struct got_repository
*repo
= arg
;
718 if (ref1
->committer_time
== 0) {
719 err
= get_committer_time(ref1
, repo
);
723 if (ref2
->committer_time
== 0) {
724 err
= get_committer_time(ref2
, repo
);
729 /* Put latest tags first. */
730 if (ref1
->committer_time
< ref2
->committer_time
)
732 else if (ref1
->committer_time
> ref2
->committer_time
)
735 err
= got_ref_cmp_by_name(NULL
, cmp
, ref2
, ref1
);
740 const struct got_error
*
741 got_ref_cmp_by_commit_timestamp_descending(void *arg
, int *cmp
,
742 struct got_reference
*ref1
, struct got_reference
*ref2
)
744 const struct got_error
*err
= NULL
;
745 struct got_repository
*repo
= arg
;
749 if (ref1
->committer_time
== 0) {
750 err
= get_committer_time(ref1
, repo
);
754 if (ref2
->committer_time
== 0) {
755 err
= get_committer_time(ref2
, repo
);
760 if (ref1
->committer_time
< ref2
->committer_time
)
762 else if (ref2
->committer_time
< ref1
->committer_time
)
765 return got_ref_cmp_by_name(arg
, cmp
, ref1
, ref2
);
770 const struct got_error
*
771 got_reflist_insert(struct got_reflist_entry
**newp
,
772 struct got_reflist_head
*refs
, struct got_reference
*ref
,
773 got_ref_cmp_cb cmp_cb
, void *cmp_arg
)
775 const struct got_error
*err
;
776 struct got_reflist_entry
*new, *re
;
781 if (cmp_cb
!= got_ref_cmp_by_name
&& (ref
->flags
& GOT_REF_IS_PACKED
)) {
783 * If we are not sorting elements by name then we must still
784 * detect collisions between a packed ref and an on-disk ref
785 * using the same name. On-disk refs take precedence and are
786 * already present on the list before packed refs get added.
788 TAILQ_FOREACH(re
, refs
, entry
) {
789 err
= got_ref_cmp_by_name(NULL
, &cmp
, re
->ref
, ref
);
797 new = malloc(sizeof(*new));
799 return got_error_from_errno("malloc");
804 * We must de-duplicate entries on insert because packed-refs may
805 * contain redundant entries. On-disk refs take precedence.
806 * This code assumes that on-disk revs are read before packed-refs.
807 * We're iterating the list anyway, so insert elements sorted by name.
809 * Many callers will provide paths in a somewhat sorted order.
810 * Iterating backwards from the tail of the list should be more
811 * efficient than traversing through the entire list each time
812 * an element is inserted.
814 re
= TAILQ_LAST(refs
, got_reflist_head
);
816 err
= (*cmp_cb
)(cmp_arg
, &cmp
, re
->ref
, new->ref
);
824 } else if (cmp
< 0) {
825 TAILQ_INSERT_AFTER(refs
, re
, new, entry
);
828 re
= TAILQ_PREV(re
, got_reflist_head
, entry
);
831 TAILQ_INSERT_HEAD(refs
, new, entry
);
835 const struct got_error
*
836 got_reflist_sort(struct got_reflist_head
*refs
,
837 got_ref_cmp_cb cmp_cb
, void *cmp_arg
)
839 const struct got_error
*err
= NULL
;
840 struct got_reflist_entry
*re
, *tmp
, *new;
841 struct got_reflist_head sorted
;
845 TAILQ_FOREACH_SAFE(re
, refs
, entry
, tmp
) {
846 struct got_reference
*ref
= re
->ref
;
847 TAILQ_REMOVE(refs
, re
, entry
);
849 err
= got_reflist_insert(&new, &sorted
, ref
, cmp_cb
, cmp_arg
);
850 if (err
|| new == NULL
/* duplicate */)
856 TAILQ_CONCAT(refs
, &sorted
, entry
);
860 static const struct got_error
*
861 gather_on_disk_refs(struct got_reflist_head
*refs
, const char *path_refs
,
862 const char *subdir
, struct got_repository
*repo
,
863 got_ref_cmp_cb cmp_cb
, void *cmp_arg
)
865 const struct got_error
*err
= NULL
;
868 struct got_reference
*ref
;
869 struct got_reflist_entry
*new;
871 while (subdir
[0] == '/')
874 if (asprintf(&path_subdir
, "%s/%s", path_refs
, subdir
) == -1)
875 return got_error_from_errno("asprintf");
877 d
= opendir(path_subdir
);
881 if (errno
!= ENOTDIR
)
884 /* This could be a regular on-disk reference file. */
886 err
= got_path_dirname(&path_subdir
, subdir
);
889 err
= got_path_basename(&refname
, subdir
);
894 err
= open_ref(&ref
, path_refs
, path_subdir
, refname
,
895 0, got_repo_get_object_format(repo
));
899 if (err
->code
== GOT_ERR_NOT_REF
)
903 err
= got_reflist_insert(&new, refs
, ref
,
905 if (err
|| new == NULL
/* duplicate */)
919 if (strcmp(dent
->d_name
, ".") == 0 ||
920 strcmp(dent
->d_name
, "..") == 0)
923 err
= got_path_dirent_type(&type
, path_subdir
, dent
);
929 err
= open_ref(&ref
, path_refs
, subdir
, dent
->d_name
,
930 0, got_repo_get_object_format(repo
));
931 if (err
&& err
->code
== GOT_ERR_BAD_REF_NAME
)
936 err
= got_reflist_insert(&new, refs
, ref
,
938 if (err
|| new == NULL
/* duplicate */)
945 if (asprintf(&child
, "%s%s%s", subdir
,
946 subdir
[0] == '\0' ? "" : "/", dent
->d_name
) == -1) {
947 err
= got_error_from_errno("asprintf");
950 err
= gather_on_disk_refs(refs
, path_refs
, child
, repo
,
966 match_packed_ref(struct got_reference
*ref
, const char *ref_namespace
)
968 const char *name
= got_ref_get_name(ref
);
969 int namespace_is_absolute
= (strncmp(ref_namespace
, "refs/", 5) == 0);
971 if (namespace_is_absolute
) {
972 return (strcmp(name
, ref_namespace
) == 0 ||
973 got_path_is_child(name
, ref_namespace
,
974 strlen(ref_namespace
)));
977 /* Match all "subdirectories" as we do with on-disk refs. */
978 while (*name
!= '\0') {
981 if (strcmp(name
, ref_namespace
) == 0 ||
982 got_path_is_child(name
, ref_namespace
,
983 strlen(ref_namespace
)))
985 while (*name
!= '\0' && *name
!= '/')
992 const struct got_error
*
993 got_ref_list(struct got_reflist_head
*refs
, struct got_repository
*repo
,
994 const char *ref_namespace
, got_ref_cmp_cb cmp_cb
, void *cmp_arg
)
996 const struct got_error
*err
;
997 char *packed_refs_path
= NULL
, *path_refs
= NULL
;
998 char *abs_namespace
= NULL
, *buf
= NULL
;
999 const char *ondisk_ref_namespace
= NULL
;
1002 struct got_reference
*ref
;
1003 struct got_reflist_entry
*new;
1005 if (ref_namespace
== NULL
|| ref_namespace
[0] == '\0') {
1006 path_refs
= get_refs_dir_path(repo
, GOT_REF_HEAD
);
1007 if (path_refs
== NULL
) {
1008 err
= got_error_from_errno("get_refs_dir_path");
1011 err
= open_ref(&ref
, path_refs
, "", GOT_REF_HEAD
, 0,
1012 got_repo_get_object_format(repo
));
1015 err
= got_reflist_insert(&new, refs
, ref
, cmp_cb
, cmp_arg
);
1016 if (err
|| new == NULL
/* duplicate */)
1018 if (err
&& err
->code
!= GOT_ERR_NOT_REF
)
1021 /* Try listing a single reference. */
1022 err
= got_ref_open(&ref
, repo
, ref_namespace
, 0);
1024 if (err
->code
!= GOT_ERR_NOT_REF
)
1026 /* Try to look up references in a given namespace. */
1028 err
= got_reflist_insert(&new, refs
, ref
,
1030 if (err
|| new == NULL
/* duplicate */)
1036 if (ref_namespace
) {
1038 /* Canonicalize the path to eliminate double-slashes if any. */
1039 if (asprintf(&abs_namespace
, "/%s", ref_namespace
) == -1) {
1040 err
= got_error_from_errno("asprintf");
1043 len
= strlen(abs_namespace
) + 1;
1046 err
= got_error_from_errno("malloc");
1049 err
= got_canonpath(abs_namespace
, buf
, len
);
1052 ondisk_ref_namespace
= buf
;
1053 while (ondisk_ref_namespace
[0] == '/')
1054 ondisk_ref_namespace
++;
1055 if (strncmp(ondisk_ref_namespace
, "refs/", 5) == 0)
1056 ondisk_ref_namespace
+= 5;
1057 else if (strcmp(ondisk_ref_namespace
, "refs") == 0)
1058 ondisk_ref_namespace
= "";
1061 /* Gather on-disk refs before parsing packed-refs. */
1063 path_refs
= get_refs_dir_path(repo
, "");
1064 if (path_refs
== NULL
) {
1065 err
= got_error_from_errno("get_refs_dir_path");
1068 err
= gather_on_disk_refs(refs
, path_refs
,
1069 ondisk_ref_namespace
? ondisk_ref_namespace
: "", repo
,
1075 * The packed-refs file may contain redundant entries, in which
1076 * case on-disk refs take precedence.
1078 packed_refs_path
= got_repo_get_path_packed_refs(repo
);
1079 if (packed_refs_path
== NULL
) {
1080 err
= got_error_from_errno("got_repo_get_path_packed_refs");
1084 f
= fopen(packed_refs_path
, "re");
1086 size_t linesize
= 0;
1090 if (fstat(fileno(f
), &sb
) == -1) {
1091 err
= got_error_from_errno2("fstat", packed_refs_path
);
1095 linelen
= getline(&line
, &linesize
, f
);
1096 if (linelen
== -1) {
1099 err
= got_ferror(f
, GOT_ERR_BAD_REF_DATA
);
1102 if (linelen
> 0 && line
[linelen
- 1] == '\n')
1103 line
[linelen
- 1] = '\0';
1104 err
= parse_packed_ref_line(&ref
, NULL
, line
,
1105 sb
.st_mtime
, got_repo_get_object_format(repo
));
1109 if (ref_namespace
&&
1110 !match_packed_ref(ref
, ref_namespace
)) {
1114 err
= got_reflist_insert(&new, refs
, ref
,
1116 if (err
|| new == NULL
/* duplicate */)
1124 free(packed_refs_path
);
1125 free(abs_namespace
);
1129 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
1130 err
= got_error_from_errno("fclose");
1135 got_ref_list_free(struct got_reflist_head
*refs
)
1137 struct got_reflist_entry
*re
;
1139 while ((re
= TAILQ_FIRST(refs
))) {
1140 TAILQ_REMOVE(refs
, re
, entry
);
1141 got_ref_close(re
->ref
);
1147 got_ref_is_symbolic(struct got_reference
*ref
)
1149 return (ref
->flags
& GOT_REF_IS_SYMBOLIC
);
1152 const struct got_error
*
1153 got_ref_change_ref(struct got_reference
*ref
, struct got_object_id
*id
)
1155 if (ref
->flags
& GOT_REF_IS_SYMBOLIC
)
1156 return got_error(GOT_ERR_BAD_REF_TYPE
);
1158 memcpy(&ref
->ref
.ref
.id
, id
, sizeof(ref
->ref
.ref
.id
));
1162 const struct got_error
*
1163 got_ref_change_symref(struct got_reference
*ref
, const char *refname
)
1167 if ((ref
->flags
& GOT_REF_IS_SYMBOLIC
) == 0)
1168 return got_error(GOT_ERR_BAD_REF_TYPE
);
1170 new_name
= strdup(refname
);
1171 if (new_name
== NULL
)
1172 return got_error_from_errno("strdup");
1174 free(ref
->ref
.symref
.ref
);
1175 ref
->ref
.symref
.ref
= new_name
;
1179 const struct got_error
*
1180 got_ref_change_symref_to_ref(struct got_reference
*symref
,
1181 struct got_object_id
*id
)
1183 if ((symref
->flags
& GOT_REF_IS_SYMBOLIC
) == 0)
1184 return got_error(GOT_ERR_BAD_REF_TYPE
);
1186 symref
->ref
.ref
.name
= symref
->ref
.symref
.name
;
1187 memcpy(&symref
->ref
.ref
.id
, id
, sizeof(symref
->ref
.ref
.id
));
1188 symref
->flags
&= ~GOT_REF_IS_SYMBOLIC
;
1192 const struct got_error
*
1193 got_ref_write(struct got_reference
*ref
, struct got_repository
*repo
)
1195 const struct got_error
*err
= NULL
, *unlock_err
= NULL
;
1196 const char *name
= got_ref_get_name(ref
);
1197 char *path_refs
= NULL
, *path
= NULL
, *tmppath
= NULL
;
1198 struct got_lockfile
*lf
= NULL
;
1203 path_refs
= get_refs_dir_path(repo
, name
);
1204 if (path_refs
== NULL
) {
1205 err
= got_error_from_errno2("get_refs_dir_path", name
);
1209 if (asprintf(&path
, "%s/%s", path_refs
, name
) == -1) {
1210 err
= got_error_from_errno("asprintf");
1214 err
= got_opentemp_named(&tmppath
, &f
, path
, "");
1217 if (err
->code
== GOT_ERR_ERRNO
&& errno
== ENOTDIR
) {
1218 err
= got_error_fmt(GOT_ERR_BAD_REF_NAME
,
1219 "collision with an existing reference: %s",
1220 got_ref_get_name(ref
));
1223 if (!(err
->code
== GOT_ERR_ERRNO
&& errno
== ENOENT
))
1225 err
= got_path_dirname(&parent
, path
);
1228 err
= got_path_mkdir(parent
);
1232 err
= got_opentemp_named(&tmppath
, &f
, path
, "");
1237 if (ref
->flags
& GOT_REF_IS_SYMBOLIC
) {
1238 n
= fprintf(f
, "ref: %s\n", ref
->ref
.symref
.ref
);
1239 if (n
!= strlen(ref
->ref
.symref
.ref
) + 6) {
1240 err
= got_ferror(f
, GOT_ERR_IO
);
1247 err
= got_object_id_str(&hex
, &ref
->ref
.ref
.id
);
1251 n
= fprintf(f
, "%s\n", hex
);
1254 err
= got_ferror(f
, GOT_ERR_IO
);
1259 if (ref
->lf
== NULL
) {
1260 err
= got_lockfile_lock(&lf
, path
, -1);
1265 /* XXX: check if old content matches our expectations? */
1267 if (stat(path
, &sb
) != 0) {
1268 if (errno
!= ENOENT
) {
1269 err
= got_error_from_errno2("stat", path
);
1272 sb
.st_mode
= GOT_DEFAULT_FILE_MODE
;
1275 if (fchmod(fileno(f
), sb
.st_mode
) != 0) {
1276 err
= got_error_from_errno2("fchmod", tmppath
);
1280 if (rename(tmppath
, path
) != 0) {
1281 err
= got_error_from_errno3("rename", tmppath
, path
);
1287 if (stat(path
, &sb
) == -1) {
1288 err
= got_error_from_errno2("stat", path
);
1291 ref
->mtime
= sb
.st_mtime
;
1293 if (ref
->lf
== NULL
&& lf
)
1294 unlock_err
= got_lockfile_unlock(lf
, -1);
1296 if (fclose(f
) == EOF
&& err
== NULL
)
1297 err
= got_error_from_errno("fclose");
1302 if (unlink(tmppath
) == -1 && err
== NULL
)
1303 err
= got_error_from_errno2("unlink", tmppath
);
1306 return err
? err
: unlock_err
;
1309 static const struct got_error
*
1310 delete_packed_ref(struct got_reference
*delref
, struct got_repository
*repo
)
1312 const struct got_error
*err
= NULL
, *unlock_err
= NULL
;
1313 struct got_lockfile
*lf
= NULL
;
1314 FILE *f
= NULL
, *tmpf
= NULL
;
1315 char *line
= NULL
, *packed_refs_path
, *tmppath
= NULL
;
1316 size_t linesize
= 0;
1317 struct got_reflist_head refs
;
1318 int found_delref
= 0;
1320 /* The packed-refs file does not contain symbolic references. */
1321 if (delref
->flags
& GOT_REF_IS_SYMBOLIC
)
1322 return got_error(GOT_ERR_BAD_REF_DATA
);
1326 packed_refs_path
= got_repo_get_path_packed_refs(repo
);
1327 if (packed_refs_path
== NULL
)
1328 return got_error_from_errno("got_repo_get_path_packed_refs");
1330 err
= got_opentemp_named(&tmppath
, &tmpf
, packed_refs_path
, "");
1334 if (delref
->lf
== NULL
) {
1335 err
= got_lockfile_lock(&lf
, packed_refs_path
, -1);
1340 f
= fopen(packed_refs_path
, "re");
1342 err
= got_error_from_errno2("fopen", packed_refs_path
);
1347 struct got_reference
*ref
;
1348 struct got_reflist_entry
*new;
1350 linelen
= getline(&line
, &linesize
, f
);
1351 if (linelen
== -1) {
1354 err
= got_ferror(f
, GOT_ERR_BAD_REF_DATA
);
1357 if (linelen
> 0 && line
[linelen
- 1] == '\n')
1358 line
[linelen
- 1] = '\0';
1359 err
= parse_packed_ref_line(&ref
, NULL
, line
, 0,
1360 got_repo_get_object_format(repo
));
1366 if (strcmp(ref
->ref
.ref
.name
, delref
->ref
.ref
.name
) == 0 &&
1367 got_object_id_cmp(&ref
->ref
.ref
.id
,
1368 &delref
->ref
.ref
.id
) == 0) {
1374 err
= got_reflist_insert(&new, &refs
, ref
,
1375 got_ref_cmp_by_name
, NULL
);
1376 if (err
|| new == NULL
/* duplicate */)
1383 struct got_reflist_entry
*re
;
1387 n
= fprintf(tmpf
, "%s\n", GOT_PACKED_REFS_HEADER
);
1388 if (n
!= sizeof(GOT_PACKED_REFS_HEADER
)) {
1389 err
= got_ferror(f
, GOT_ERR_IO
);
1393 TAILQ_FOREACH(re
, &refs
, entry
) {
1397 err
= got_object_id_str(&hex
, &re
->ref
->ref
.ref
.id
);
1401 n
= fprintf(tmpf
, "%s ", hex
);
1404 err
= got_ferror(f
, GOT_ERR_IO
);
1408 n
= fprintf(tmpf
, "%s\n", re
->ref
->ref
.ref
.name
);
1409 if (n
!= strlen(re
->ref
->ref
.ref
.name
) + 1) {
1410 err
= got_ferror(f
, GOT_ERR_IO
);
1415 if (fflush(tmpf
) != 0) {
1416 err
= got_error_from_errno("fflush");
1420 if (fstat(fileno(f
), &sb
) != 0) {
1421 if (errno
!= ENOENT
) {
1422 err
= got_error_from_errno2("fstat",
1426 sb
.st_mode
= GOT_DEFAULT_FILE_MODE
;
1429 if (fchmod(fileno(tmpf
), sb
.st_mode
) != 0) {
1430 err
= got_error_from_errno2("fchmod", tmppath
);
1434 if (rename(tmppath
, packed_refs_path
) != 0) {
1435 err
= got_error_from_errno3("rename", tmppath
,
1443 if (delref
->lf
== NULL
&& lf
)
1444 unlock_err
= got_lockfile_unlock(lf
, -1);
1446 if (fclose(f
) == EOF
&& err
== NULL
)
1447 err
= got_error_from_errno("fclose");
1449 if (tmppath
&& unlink(tmppath
) == -1 && err
== NULL
)
1450 err
= got_error_from_errno2("unlink", tmppath
);
1451 if (tmpf
&& fclose(tmpf
) == EOF
&& err
== NULL
)
1452 err
= got_error_from_errno("fclose");
1454 free(packed_refs_path
);
1456 got_ref_list_free(&refs
);
1457 return err
? err
: unlock_err
;
1460 static const struct got_error
*
1461 delete_loose_ref(struct got_reference
*ref
, struct got_repository
*repo
)
1463 const struct got_error
*err
= NULL
, *unlock_err
= NULL
;
1464 const char *name
= got_ref_get_name(ref
);
1465 char *path_refs
= NULL
, *path
= NULL
;
1466 struct got_lockfile
*lf
= NULL
;
1468 path_refs
= get_refs_dir_path(repo
, name
);
1469 if (path_refs
== NULL
) {
1470 err
= got_error_from_errno2("get_refs_dir_path", name
);
1474 if (asprintf(&path
, "%s/%s", path_refs
, name
) == -1) {
1475 err
= got_error_from_errno("asprintf");
1479 if (ref
->lf
== NULL
) {
1480 err
= got_lockfile_lock(&lf
, path
, -1);
1485 /* XXX: check if old content matches our expectations? */
1487 if (unlink(path
) == -1)
1488 err
= got_error_from_errno2("unlink", path
);
1490 if (ref
->lf
== NULL
&& lf
)
1491 unlock_err
= got_lockfile_unlock(lf
, -1);
1495 return err
? err
: unlock_err
;
1498 const struct got_error
*
1499 got_ref_delete(struct got_reference
*ref
, struct got_repository
*repo
)
1501 const struct got_error
*err
= NULL
;
1502 struct got_reference
*ref2
;
1504 if (ref
->flags
& GOT_REF_IS_PACKED
) {
1505 err
= delete_packed_ref(ref
, repo
);
1509 err
= got_ref_open(&ref2
, repo
, got_ref_get_name(ref
), 0);
1511 if (err
->code
== GOT_ERR_NOT_REF
)
1516 err
= delete_loose_ref(ref2
, repo
);
1517 got_ref_close(ref2
);
1520 err
= delete_loose_ref(ref
, repo
);
1524 err
= got_ref_open(&ref2
, repo
, got_ref_get_name(ref
), 0);
1526 if (err
->code
== GOT_ERR_NOT_REF
)
1531 err
= delete_packed_ref(ref2
, repo
);
1532 got_ref_close(ref2
);
1537 const struct got_error
*
1538 got_ref_unlock(struct got_reference
*ref
)
1540 const struct got_error
*err
;
1541 err
= got_lockfile_unlock(ref
->lf
, -1);
1546 struct got_reflist_object_id_map
{
1547 struct got_object_idset
*idset
;
1550 struct got_reflist_object_id_map_entry
{
1551 struct got_reflist_head refs
;
1554 static const struct got_error
*
1555 add_object_id_map_entry(struct got_object_idset
*idset
,
1556 struct got_object_id
*id
, struct got_reflist_entry
*re
)
1558 const struct got_error
*err
= NULL
;
1559 struct got_reflist_object_id_map_entry
*ent
;
1560 struct got_reflist_entry
*new;
1562 ent
= got_object_idset_get(idset
, id
);
1564 ent
= malloc(sizeof(*ent
));
1566 return got_error_from_errno("malloc");
1568 TAILQ_INIT(&ent
->refs
);
1569 err
= got_object_idset_add(idset
, id
, ent
);
1576 err
= got_reflist_entry_dup(&new, re
);
1580 TAILQ_INSERT_TAIL(&ent
->refs
, new, entry
);
1584 const struct got_error
*
1585 got_reflist_object_id_map_create(struct got_reflist_object_id_map
**map
,
1586 struct got_reflist_head
*refs
, struct got_repository
*repo
)
1588 const struct got_error
*err
= NULL
;
1589 struct got_object_idset
*idset
;
1590 struct got_object_id
*id
= NULL
;
1591 struct got_reflist_entry
*re
;
1593 idset
= got_object_idset_alloc();
1595 return got_error_from_errno("got_object_idset_alloc");
1597 *map
= malloc(sizeof(**map
));
1599 got_object_idset_free(idset
);
1600 return got_error_from_errno("malloc");
1602 (*map
)->idset
= idset
;
1604 TAILQ_FOREACH(re
, refs
, entry
) {
1605 struct got_tag_object
*tag
= NULL
;
1607 err
= got_ref_resolve(&id
, repo
, re
->ref
);
1611 err
= add_object_id_map_entry(idset
, id
, re
);
1615 if (strstr(got_ref_get_name(re
->ref
), "/tags/") == NULL
) {
1621 err
= got_object_open_as_tag(&tag
, repo
, id
);
1623 if (err
->code
!= GOT_ERR_OBJ_TYPE
)
1625 /* Ref points at something other than a tag. */
1633 err
= add_object_id_map_entry(idset
,
1634 got_object_tag_get_object_id(tag
), re
);
1635 got_object_tag_close(tag
);
1645 got_reflist_object_id_map_free(*map
);
1651 struct got_reflist_head
*
1652 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map
*map
,
1653 struct got_object_id
*id
)
1655 struct got_reflist_object_id_map_entry
*ent
;
1656 ent
= got_object_idset_get(map
->idset
, id
);
1662 static const struct got_error
*
1663 free_id_map_entry(struct got_object_id
*id
, void *data
, void *arg
)
1665 struct got_reflist_object_id_map_entry
*ent
= data
;
1667 got_ref_list_free(&ent
->refs
);
1673 got_reflist_object_id_map_free(struct got_reflist_object_id_map
*map
)
1675 got_object_idset_for_each(map
->idset
, free_id_map_entry
, NULL
);
1676 got_object_idset_free(map
->idset
);