4 #include "refs-internal.h"
5 #include "packed-backend.h"
6 #include "../iterator.h"
7 #include "../lockfile.h"
11 * Don't use mmap() at all for reading `packed-refs`.
16 * Can use mmap() for reading `packed-refs`, but the file must
17 * not remain mmapped. This is the usual option on Windows,
18 * where you cannot rename a new version of a file onto a file
19 * that is currently mmapped.
24 * It is OK to leave the `packed-refs` file mmapped while
25 * arbitrary other code is running.
31 static enum mmap_strategy mmap_strategy = MMAP_NONE;
32 #elif defined(MMAP_PREVENTS_DELETE)
33 static enum mmap_strategy mmap_strategy = MMAP_TEMPORARY;
35 static enum mmap_strategy mmap_strategy = MMAP_OK;
38 struct packed_ref_store;
41 * A `snapshot` represents one snapshot of a `packed-refs` file.
43 * Normally, this will be a mmapped view of the contents of the
44 * `packed-refs` file at the time the snapshot was created. However,
45 * if the `packed-refs` file was not sorted, this might point at heap
46 * memory holding the contents of the `packed-refs` file with its
47 * records sorted by refname.
49 * `snapshot` instances are reference counted (via
50 * `acquire_snapshot()` and `release_snapshot()`). This is to prevent
51 * an instance from disappearing while an iterator is still iterating
52 * over it. Instances are garbage collected when their `referrers`
55 * The most recent `snapshot`, if available, is referenced by the
56 * `packed_ref_store`. Its freshness is checked whenever
57 * `get_snapshot()` is called; if the existing snapshot is obsolete, a
58 * new snapshot is taken.
62 * A back-pointer to the packed_ref_store with which this
63 * snapshot is associated:
65 struct packed_ref_store *refs;
67 /* Is the `packed-refs` file currently mmapped? */
71 * The contents of the `packed-refs` file. If the file was
72 * already sorted, this points at the mmapped contents of the
73 * file. If not, this points at heap-allocated memory
74 * containing the contents, sorted. If there were no contents
75 * (e.g., because the file didn't exist), `buf` and `eof` are
80 /* The size of the header line, if any; otherwise, 0: */
84 * What is the peeled state of the `packed-refs` file that
85 * this snapshot represents? (This is usually determined from
88 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled;
91 * Count of references to this instance, including the pointer
92 * from `packed_ref_store::snapshot`, if any. The instance
93 * will not be freed as long as the reference count is
96 unsigned int referrers;
99 * The metadata of the `packed-refs` file from which this
100 * snapshot was created, used to tell if the file has been
101 * replaced since we read it.
103 struct stat_validity validity;
107 * A `ref_store` representing references stored in a `packed-refs`
108 * file. It implements the `ref_store` interface, though it has some
111 * - It cannot store symbolic references.
113 * - It cannot store reflogs.
115 * - It does not support reference renaming (though it could).
117 * On the other hand, it can be locked outside of a reference
118 * transaction. In that case, it remains locked even after the
119 * transaction is done and the new `packed-refs` file is activated.
121 struct packed_ref_store {
122 struct ref_store base;
124 unsigned int store_flags;
126 /* The path of the "packed-refs" file: */
130 * A snapshot of the values read from the `packed-refs` file,
131 * if it might still be current; otherwise, NULL.
133 struct snapshot *snapshot;
136 * Lock used for the "packed-refs" file. Note that this (and
137 * thus the enclosing `packed_ref_store`) must not be freed.
139 struct lock_file lock;
142 * Temporary file used when rewriting new contents to the
143 * "packed-refs" file. Note that this (and thus the enclosing
144 * `packed_ref_store`) must not be freed.
146 struct tempfile *tempfile;
150 * Increment the reference count of `*snapshot`.
152 static void acquire_snapshot(struct snapshot *snapshot)
154 snapshot->referrers++;
158 * If the buffer in `snapshot` is active, then either munmap the
159 * memory and close the file, or free the memory. Then set the buffer
162 static void clear_snapshot_buffer(struct snapshot *snapshot)
164 if (snapshot->mmapped) {
165 if (munmap(snapshot->buf, snapshot->eof - snapshot->buf))
166 die_errno("error ummapping packed-refs file %s",
167 snapshot->refs->path);
168 snapshot->mmapped = 0;
172 snapshot->buf = snapshot->eof = NULL;
173 snapshot->header_len = 0;
177 * Decrease the reference count of `*snapshot`. If it goes to zero,
178 * free `*snapshot` and return true; otherwise return false.
180 static int release_snapshot(struct snapshot *snapshot)
182 if (!--snapshot->referrers) {
183 stat_validity_clear(&snapshot->validity);
184 clear_snapshot_buffer(snapshot);
192 struct ref_store *packed_ref_store_create(const char *path,
193 unsigned int store_flags)
195 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
196 struct ref_store *ref_store = (struct ref_store *)refs;
198 base_ref_store_init(ref_store, &refs_be_packed);
199 refs->store_flags = store_flags;
201 refs->path = xstrdup(path);
206 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
207 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
208 * support at least the flags specified in `required_flags`. `caller`
209 * is used in any necessary error messages.
211 static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
212 unsigned int required_flags,
215 struct packed_ref_store *refs;
217 if (ref_store->be != &refs_be_packed)
218 die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
219 ref_store->be->name, caller);
221 refs = (struct packed_ref_store *)ref_store;
223 if ((refs->store_flags & required_flags) != required_flags)
224 die("BUG: unallowed operation (%s), requires %x, has %x\n",
225 caller, required_flags, refs->store_flags);
230 static void clear_snapshot(struct packed_ref_store *refs)
232 if (refs->snapshot) {
233 struct snapshot *snapshot = refs->snapshot;
235 refs->snapshot = NULL;
236 release_snapshot(snapshot);
240 static NORETURN void die_unterminated_line(const char *path,
241 const char *p, size_t len)
244 die("unterminated line in %s: %.*s", path, (int)len, p);
246 die("unterminated line in %s: %.75s...", path, p);
249 static NORETURN void die_invalid_line(const char *path,
250 const char *p, size_t len)
252 const char *eol = memchr(p, '\n', len);
255 die_unterminated_line(path, p, len);
256 else if (eol - p < 80)
257 die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
259 die("unexpected line in %s: %.75s...", path, p);
263 struct snapshot_record {
268 static int cmp_packed_ref_records(const void *v1, const void *v2)
270 const struct snapshot_record *e1 = v1, *e2 = v2;
271 const char *r1 = e1->start + GIT_SHA1_HEXSZ + 1;
272 const char *r2 = e2->start + GIT_SHA1_HEXSZ + 1;
276 return *r2 == '\n' ? 0 : -1;
281 return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1;
289 * Compare a snapshot record at `rec` to the specified NUL-terminated
292 static int cmp_record_to_refname(const char *rec, const char *refname)
294 const char *r1 = rec + GIT_SHA1_HEXSZ + 1;
295 const char *r2 = refname;
303 return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1;
310 * `snapshot->buf` is not known to be sorted. Check whether it is, and
311 * if not, sort it into new memory and munmap/free the old storage.
313 static void sort_snapshot(struct snapshot *snapshot)
315 struct snapshot_record *records = NULL;
316 size_t alloc = 0, nr = 0;
318 const char *pos, *eof, *eol;
320 char *new_buffer, *dst;
322 pos = snapshot->buf + snapshot->header_len;
330 * Initialize records based on a crude estimate of the number
331 * of references in the file (we'll grow it below if needed):
333 ALLOC_GROW(records, len / 80 + 20, alloc);
336 eol = memchr(pos, '\n', eof - pos);
338 /* The safety check should prevent this. */
339 BUG("unterminated line found in packed-refs");
340 if (eol - pos < GIT_SHA1_HEXSZ + 2)
341 die_invalid_line(snapshot->refs->path,
344 if (eol < eof && *eol == '^') {
346 * Keep any peeled line together with its
349 const char *peeled_start = eol;
351 eol = memchr(peeled_start, '\n', eof - peeled_start);
353 /* The safety check should prevent this. */
354 BUG("unterminated peeled line found in packed-refs");
358 ALLOC_GROW(records, nr + 1, alloc);
359 records[nr].start = pos;
360 records[nr].len = eol - pos;
365 cmp_packed_ref_records(&records[nr - 2],
366 &records[nr - 1]) >= 0)
375 /* We need to sort the memory. First we sort the records array: */
376 QSORT(records, nr, cmp_packed_ref_records);
379 * Allocate a new chunk of memory, and copy the old memory to
380 * the new in the order indicated by `records` (not bothering
381 * with the header line):
383 new_buffer = xmalloc(len);
384 for (dst = new_buffer, i = 0; i < nr; i++) {
385 memcpy(dst, records[i].start, records[i].len);
386 dst += records[i].len;
390 * Now munmap the old buffer and use the sorted buffer in its
393 clear_snapshot_buffer(snapshot);
394 snapshot->buf = new_buffer;
395 snapshot->eof = new_buffer + len;
396 snapshot->header_len = 0;
403 * Return a pointer to the start of the record that contains the
404 * character `*p` (which must be within the buffer). If no other
405 * record start is found, return `buf`.
407 static const char *find_start_of_record(const char *buf, const char *p)
409 while (p > buf && (p[-1] != '\n' || p[0] == '^'))
415 * Return a pointer to the start of the record following the record
416 * that contains `*p`. If none is found before `end`, return `end`.
418 static const char *find_end_of_record(const char *p, const char *end)
420 while (++p < end && (p[-1] != '\n' || p[0] == '^'))
426 * We want to be able to compare mmapped reference records quickly,
427 * without totally parsing them. We can do so because the records are
428 * LF-terminated, and the refname should start exactly (GIT_SHA1_HEXSZ
429 * + 1) bytes past the beginning of the record.
431 * But what if the `packed-refs` file contains garbage? We're willing
432 * to tolerate not detecting the problem, as long as we don't produce
433 * totally garbled output (we can't afford to check the integrity of
434 * the whole file during every Git invocation). But we do want to be
435 * sure that we never read past the end of the buffer in memory and
436 * perform an illegal memory access.
438 * Guarantee that minimum level of safety by verifying that the last
439 * record in the file is LF-terminated, and that it has at least
440 * (GIT_SHA1_HEXSZ + 1) characters before the LF. Die if either of
441 * these checks fails.
443 static void verify_buffer_safe(struct snapshot *snapshot)
445 const char *buf = snapshot->buf + snapshot->header_len;
446 const char *eof = snapshot->eof;
447 const char *last_line;
452 last_line = find_start_of_record(buf, eof - 1);
453 if (*(eof - 1) != '\n' || eof - last_line < GIT_SHA1_HEXSZ + 2)
454 die_invalid_line(snapshot->refs->path,
455 last_line, eof - last_line);
459 * Depending on `mmap_strategy`, either mmap or read the contents of
460 * the `packed-refs` file into the snapshot. Return 1 if the file
461 * existed and was read, or 0 if the file was absent. Die on errors.
463 static int load_contents(struct snapshot *snapshot)
470 fd = open(snapshot->refs->path, O_RDONLY);
472 if (errno == ENOENT) {
474 * This is OK; it just means that no
475 * "packed-refs" file has been written yet,
476 * which is equivalent to it being empty,
477 * which is its state when initialized with
482 die_errno("couldn't read %s", snapshot->refs->path);
486 stat_validity_update(&snapshot->validity, fd);
488 if (fstat(fd, &st) < 0)
489 die_errno("couldn't stat %s", snapshot->refs->path);
490 size = xsize_t(st.st_size);
492 switch (mmap_strategy) {
494 snapshot->buf = xmalloc(size);
495 bytes_read = read_in_full(fd, snapshot->buf, size);
496 if (bytes_read < 0 || bytes_read != size)
497 die_errno("couldn't read %s", snapshot->refs->path);
498 snapshot->eof = snapshot->buf + size;
499 snapshot->mmapped = 0;
503 snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
504 snapshot->eof = snapshot->buf + size;
505 snapshot->mmapped = 1;
514 * Find the place in `snapshot->buf` where the start of the record for
515 * `refname` starts. If `mustexist` is true and the reference doesn't
516 * exist, then return NULL. If `mustexist` is false and the reference
517 * doesn't exist, then return the point where that reference would be
518 * inserted. In the latter mode, `refname` doesn't have to be a proper
519 * reference name; for example, one could search for "refs/replace/"
520 * to find the start of any replace references.
522 * The record is sought using a binary search, so `snapshot->buf` must
525 static const char *find_reference_location(struct snapshot *snapshot,
526 const char *refname, int mustexist)
529 * This is not *quite* a garden-variety binary search, because
530 * the data we're searching is made up of records, and we
531 * always need to find the beginning of a record to do a
532 * comparison. A "record" here is one line for the reference
533 * itself and zero or one peel lines that start with '^'. Our
534 * loop invariant is described in the next two comments.
538 * A pointer to the character at the start of a record whose
539 * preceding records all have reference names that come
540 * *before* `refname`.
542 const char *lo = snapshot->buf + snapshot->header_len;
545 * A pointer to a the first character of a record whose
546 * reference name comes *after* `refname`.
548 const char *hi = snapshot->eof;
551 const char *mid, *rec;
554 mid = lo + (hi - lo) / 2;
555 rec = find_start_of_record(lo, mid);
556 cmp = cmp_record_to_refname(rec, refname);
558 lo = find_end_of_record(mid, hi);
559 } else if (cmp > 0) {
573 * Create a newly-allocated `snapshot` of the `packed-refs` file in
574 * its current state and return it. The return value will already have
575 * its reference count incremented.
577 * A comment line of the form "# pack-refs with: " may contain zero or
578 * more traits. We interpret the traits as follows:
580 * Neither `peeled` nor `fully-peeled`:
582 * Probably no references are peeled. But if the file contains a
583 * peeled value for a reference, we will use it.
587 * References under "refs/tags/", if they *can* be peeled, *are*
588 * peeled in this file. References outside of "refs/tags/" are
589 * probably not peeled even if they could have been, but if we find
590 * a peeled value for such a reference we will use it.
594 * All references in the file that can be peeled are peeled.
595 * Inversely (and this is more important), any references in the
596 * file for which no peeled value is recorded is not peelable. This
597 * trait should typically be written alongside "peeled" for
598 * compatibility with older clients, but we do not require it
599 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
603 * The references in this file are known to be sorted by refname.
605 static struct snapshot *create_snapshot(struct packed_ref_store *refs)
607 struct snapshot *snapshot = xcalloc(1, sizeof(*snapshot));
610 snapshot->refs = refs;
611 acquire_snapshot(snapshot);
612 snapshot->peeled = PEELED_NONE;
614 if (!load_contents(snapshot))
617 /* If the file has a header line, process it: */
618 if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') {
619 struct strbuf tmp = STRBUF_INIT;
622 struct string_list traits = STRING_LIST_INIT_NODUP;
624 eol = memchr(snapshot->buf, '\n',
625 snapshot->eof - snapshot->buf);
627 die_unterminated_line(refs->path,
629 snapshot->eof - snapshot->buf);
631 strbuf_add(&tmp, snapshot->buf, eol - snapshot->buf);
633 if (!skip_prefix(tmp.buf, "# pack-refs with:", (const char **)&p))
634 die_invalid_line(refs->path,
636 snapshot->eof - snapshot->buf);
638 string_list_split_in_place(&traits, p, ' ', -1);
640 if (unsorted_string_list_has_string(&traits, "fully-peeled"))
641 snapshot->peeled = PEELED_FULLY;
642 else if (unsorted_string_list_has_string(&traits, "peeled"))
643 snapshot->peeled = PEELED_TAGS;
645 sorted = unsorted_string_list_has_string(&traits, "sorted");
647 /* perhaps other traits later as well */
649 /* The "+ 1" is for the LF character. */
650 snapshot->header_len = eol + 1 - snapshot->buf;
652 string_list_clear(&traits, 0);
653 strbuf_release(&tmp);
656 verify_buffer_safe(snapshot);
659 sort_snapshot(snapshot);
662 * Reordering the records might have moved a short one
663 * to the end of the buffer, so verify the buffer's
666 verify_buffer_safe(snapshot);
669 if (mmap_strategy != MMAP_OK && snapshot->mmapped) {
671 * We don't want to leave the file mmapped, so we are
672 * forced to make a copy now:
674 size_t size = snapshot->eof -
675 (snapshot->buf + snapshot->header_len);
676 char *buf_copy = xmalloc(size);
678 memcpy(buf_copy, snapshot->buf + snapshot->header_len, size);
679 clear_snapshot_buffer(snapshot);
680 snapshot->buf = buf_copy;
681 snapshot->eof = buf_copy + size;
688 * Check that `refs->snapshot` (if present) still reflects the
689 * contents of the `packed-refs` file. If not, clear the snapshot.
691 static void validate_snapshot(struct packed_ref_store *refs)
693 if (refs->snapshot &&
694 !stat_validity_check(&refs->snapshot->validity, refs->path))
695 clear_snapshot(refs);
699 * Get the `snapshot` for the specified packed_ref_store, creating and
700 * populating it if it hasn't been read before or if the file has been
701 * changed (according to its `validity` field) since it was last read.
702 * On the other hand, if we hold the lock, then assume that the file
703 * hasn't been changed out from under us, so skip the extra `stat()`
704 * call in `stat_validity_check()`. This function does *not* increase
705 * the snapshot's reference count on behalf of the caller.
707 static struct snapshot *get_snapshot(struct packed_ref_store *refs)
709 if (!is_lock_file_locked(&refs->lock))
710 validate_snapshot(refs);
713 refs->snapshot = create_snapshot(refs);
715 return refs->snapshot;
718 static int packed_read_raw_ref(struct ref_store *ref_store,
719 const char *refname, struct object_id *oid,
720 struct strbuf *referent, unsigned int *type)
722 struct packed_ref_store *refs =
723 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
724 struct snapshot *snapshot = get_snapshot(refs);
729 rec = find_reference_location(snapshot, refname, 1);
732 /* refname is not a packed reference. */
737 if (get_oid_hex(rec, oid))
738 die_invalid_line(refs->path, rec, snapshot->eof - rec);
740 *type = REF_ISPACKED;
745 * This value is set in `base.flags` if the peeled value of the
746 * current reference is known. In that case, `peeled` contains the
747 * correct peeled value for the reference, which might be `null_oid`
748 * if the reference is not a tag or if it is broken.
750 #define REF_KNOWS_PEELED 0x40
753 * An iterator over a snapshot of a `packed-refs` file.
755 struct packed_ref_iterator {
756 struct ref_iterator base;
758 struct snapshot *snapshot;
760 /* The current position in the snapshot's buffer: */
763 /* The end of the part of the buffer that will be iterated over: */
766 /* Scratch space for current values: */
767 struct object_id oid, peeled;
768 struct strbuf refname_buf;
774 * Move the iterator to the next record in the snapshot, without
775 * respect for whether the record is actually required by the current
776 * iteration. Adjust the fields in `iter` and return `ITER_OK` or
777 * `ITER_DONE`. This function does not free the iterator in the case
780 static int next_record(struct packed_ref_iterator *iter)
782 const char *p = iter->pos, *eol;
784 strbuf_reset(&iter->refname_buf);
786 if (iter->pos == iter->eof)
789 iter->base.flags = REF_ISPACKED;
791 if (iter->eof - p < GIT_SHA1_HEXSZ + 2 ||
792 parse_oid_hex(p, &iter->oid, &p) ||
794 die_invalid_line(iter->snapshot->refs->path,
795 iter->pos, iter->eof - iter->pos);
797 eol = memchr(p, '\n', iter->eof - p);
799 die_unterminated_line(iter->snapshot->refs->path,
800 iter->pos, iter->eof - iter->pos);
802 strbuf_add(&iter->refname_buf, p, eol - p);
803 iter->base.refname = iter->refname_buf.buf;
805 if (check_refname_format(iter->base.refname, REFNAME_ALLOW_ONELEVEL)) {
806 if (!refname_is_safe(iter->base.refname))
807 die("packed refname is dangerous: %s",
810 iter->base.flags |= REF_BAD_NAME | REF_ISBROKEN;
812 if (iter->snapshot->peeled == PEELED_FULLY ||
813 (iter->snapshot->peeled == PEELED_TAGS &&
814 starts_with(iter->base.refname, "refs/tags/")))
815 iter->base.flags |= REF_KNOWS_PEELED;
819 if (iter->pos < iter->eof && *iter->pos == '^') {
821 if (iter->eof - p < GIT_SHA1_HEXSZ + 1 ||
822 parse_oid_hex(p, &iter->peeled, &p) ||
824 die_invalid_line(iter->snapshot->refs->path,
825 iter->pos, iter->eof - iter->pos);
829 * Regardless of what the file header said, we
830 * definitely know the value of *this* reference. But
831 * we suppress it if the reference is broken:
833 if ((iter->base.flags & REF_ISBROKEN)) {
834 oidclr(&iter->peeled);
835 iter->base.flags &= ~REF_KNOWS_PEELED;
837 iter->base.flags |= REF_KNOWS_PEELED;
840 oidclr(&iter->peeled);
846 static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
848 struct packed_ref_iterator *iter =
849 (struct packed_ref_iterator *)ref_iterator;
852 while ((ok = next_record(iter)) == ITER_OK) {
853 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
854 ref_type(iter->base.refname) != REF_TYPE_PER_WORKTREE)
857 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
858 !ref_resolves_to_object(iter->base.refname, &iter->oid,
865 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
871 static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
872 struct object_id *peeled)
874 struct packed_ref_iterator *iter =
875 (struct packed_ref_iterator *)ref_iterator;
877 if ((iter->base.flags & REF_KNOWS_PEELED)) {
878 oidcpy(peeled, &iter->peeled);
879 return is_null_oid(&iter->peeled) ? -1 : 0;
880 } else if ((iter->base.flags & (REF_ISBROKEN | REF_ISSYMREF))) {
883 return !!peel_object(&iter->oid, peeled);
887 static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
889 struct packed_ref_iterator *iter =
890 (struct packed_ref_iterator *)ref_iterator;
893 strbuf_release(&iter->refname_buf);
894 release_snapshot(iter->snapshot);
895 base_ref_iterator_free(ref_iterator);
899 static struct ref_iterator_vtable packed_ref_iterator_vtable = {
900 packed_ref_iterator_advance,
901 packed_ref_iterator_peel,
902 packed_ref_iterator_abort
905 static struct ref_iterator *packed_ref_iterator_begin(
906 struct ref_store *ref_store,
907 const char *prefix, unsigned int flags)
909 struct packed_ref_store *refs;
910 struct snapshot *snapshot;
912 struct packed_ref_iterator *iter;
913 struct ref_iterator *ref_iterator;
914 unsigned int required_flags = REF_STORE_READ;
916 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
917 required_flags |= REF_STORE_ODB;
918 refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
921 * Note that `get_snapshot()` internally checks whether the
922 * snapshot is up to date with what is on disk, and re-reads
925 snapshot = get_snapshot(refs);
928 return empty_ref_iterator_begin();
930 iter = xcalloc(1, sizeof(*iter));
931 ref_iterator = &iter->base;
932 base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
934 iter->snapshot = snapshot;
935 acquire_snapshot(snapshot);
937 if (prefix && *prefix)
938 start = find_reference_location(snapshot, prefix, 0);
940 start = snapshot->buf + snapshot->header_len;
943 iter->eof = snapshot->eof;
944 strbuf_init(&iter->refname_buf, 0);
946 iter->base.oid = &iter->oid;
950 if (prefix && *prefix)
951 /* Stop iteration after we've gone *past* prefix: */
952 ref_iterator = prefix_ref_iterator_begin(ref_iterator, prefix, 0);
958 * Write an entry to the packed-refs file for the specified refname.
959 * If peeled is non-NULL, write it as the entry's peeled value. On
960 * error, return a nonzero value and leave errno set at the value left
961 * by the failing call to `fprintf()`.
963 static int write_packed_entry(FILE *fh, const char *refname,
964 const struct object_id *oid,
965 const struct object_id *peeled)
967 if (fprintf(fh, "%s %s\n", oid_to_hex(oid), refname) < 0 ||
968 (peeled && fprintf(fh, "^%s\n", oid_to_hex(peeled)) < 0))
974 int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
976 struct packed_ref_store *refs =
977 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
979 static int timeout_configured = 0;
980 static int timeout_value = 1000;
982 if (!timeout_configured) {
983 git_config_get_int("core.packedrefstimeout", &timeout_value);
984 timeout_configured = 1;
988 * Note that we close the lockfile immediately because we
989 * don't write new content to it, but rather to a separate
992 if (hold_lock_file_for_update_timeout(
995 flags, timeout_value) < 0) {
996 unable_to_lock_message(refs->path, errno, err);
1000 if (close_lock_file_gently(&refs->lock)) {
1001 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
1002 rollback_lock_file(&refs->lock);
1007 * Now that we hold the `packed-refs` lock, make sure that our
1008 * snapshot matches the current version of the file. Normally
1009 * `get_snapshot()` does that for us, but that function
1010 * assumes that when the file is locked, any existing snapshot
1011 * is still valid. We've just locked the file, but it might
1012 * have changed the moment *before* we locked it.
1014 validate_snapshot(refs);
1017 * Now make sure that the packed-refs file as it exists in the
1018 * locked state is loaded into the snapshot:
1024 void packed_refs_unlock(struct ref_store *ref_store)
1026 struct packed_ref_store *refs = packed_downcast(
1028 REF_STORE_READ | REF_STORE_WRITE,
1029 "packed_refs_unlock");
1031 if (!is_lock_file_locked(&refs->lock))
1032 die("BUG: packed_refs_unlock() called when not locked");
1033 rollback_lock_file(&refs->lock);
1036 int packed_refs_is_locked(struct ref_store *ref_store)
1038 struct packed_ref_store *refs = packed_downcast(
1040 REF_STORE_READ | REF_STORE_WRITE,
1041 "packed_refs_is_locked");
1043 return is_lock_file_locked(&refs->lock);
1047 * The packed-refs header line that we write out. Perhaps other traits
1048 * will be added later.
1050 * Note that earlier versions of Git used to parse these traits by
1051 * looking for " trait " in the line. For this reason, the space after
1052 * the colon and the trailing space are required.
1054 static const char PACKED_REFS_HEADER[] =
1055 "# pack-refs with: peeled fully-peeled sorted \n";
1057 static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
1059 /* Nothing to do. */
1064 * Write the packed refs from the current snapshot to the packed-refs
1065 * tempfile, incorporating any changes from `updates`. `updates` must
1066 * be a sorted string list whose keys are the refnames and whose util
1067 * values are `struct ref_update *`. On error, rollback the tempfile,
1068 * write an error message to `err`, and return a nonzero value.
1070 * The packfile must be locked before calling this function and will
1071 * remain locked when it is done.
1073 static int write_with_updates(struct packed_ref_store *refs,
1074 struct string_list *updates,
1077 struct ref_iterator *iter = NULL;
1081 struct strbuf sb = STRBUF_INIT;
1082 char *packed_refs_path;
1084 if (!is_lock_file_locked(&refs->lock))
1085 die("BUG: write_with_updates() called while unlocked");
1088 * If packed-refs is a symlink, we want to overwrite the
1089 * symlinked-to file, not the symlink itself. Also, put the
1090 * staging file next to it:
1092 packed_refs_path = get_locked_file_path(&refs->lock);
1093 strbuf_addf(&sb, "%s.new", packed_refs_path);
1094 free(packed_refs_path);
1095 refs->tempfile = create_tempfile(sb.buf);
1096 if (!refs->tempfile) {
1097 strbuf_addf(err, "unable to create file %s: %s",
1098 sb.buf, strerror(errno));
1099 strbuf_release(&sb);
1102 strbuf_release(&sb);
1104 out = fdopen_tempfile(refs->tempfile, "w");
1106 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
1111 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
1115 * We iterate in parallel through the current list of refs and
1116 * the list of updates, processing an entry from at least one
1117 * of the lists each time through the loop. When the current
1118 * list of refs is exhausted, set iter to NULL. When the list
1119 * of updates is exhausted, leave i set to updates->nr.
1121 iter = packed_ref_iterator_begin(&refs->base, "",
1122 DO_FOR_EACH_INCLUDE_BROKEN);
1123 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
1128 while (iter || i < updates->nr) {
1129 struct ref_update *update = NULL;
1132 if (i >= updates->nr) {
1135 update = updates->items[i].util;
1140 cmp = strcmp(iter->refname, update->refname);
1145 * There is both an old value and an update
1146 * for this reference. Check the old value if
1149 if ((update->flags & REF_HAVE_OLD)) {
1150 if (is_null_oid(&update->old_oid)) {
1151 strbuf_addf(err, "cannot update ref '%s': "
1152 "reference already exists",
1155 } else if (oidcmp(&update->old_oid, iter->oid)) {
1156 strbuf_addf(err, "cannot update ref '%s': "
1157 "is at %s but expected %s",
1159 oid_to_hex(iter->oid),
1160 oid_to_hex(&update->old_oid));
1165 /* Now figure out what to use for the new value: */
1166 if ((update->flags & REF_HAVE_NEW)) {
1168 * The update takes precedence. Skip
1169 * the iterator over the unneeded
1172 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
1177 * The update doesn't actually want to
1178 * change anything. We're done with it.
1183 } else if (cmp > 0) {
1185 * There is no old value but there is an
1186 * update for this reference. Make sure that
1187 * the update didn't expect an existing value:
1189 if ((update->flags & REF_HAVE_OLD) &&
1190 !is_null_oid(&update->old_oid)) {
1191 strbuf_addf(err, "cannot update ref '%s': "
1192 "reference is missing but expected %s",
1194 oid_to_hex(&update->old_oid));
1200 /* Pass the old reference through. */
1202 struct object_id peeled;
1203 int peel_error = ref_iterator_peel(iter, &peeled);
1205 if (write_packed_entry(out, iter->refname,
1207 peel_error ? NULL : &peeled))
1210 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
1212 } else if (is_null_oid(&update->new_oid)) {
1214 * The update wants to delete the reference,
1215 * and the reference either didn't exist or we
1216 * have already skipped it. So we're done with
1217 * the update (and don't have to write
1222 struct object_id peeled;
1223 int peel_error = peel_object(&update->new_oid,
1226 if (write_packed_entry(out, update->refname,
1228 peel_error ? NULL : &peeled))
1235 if (ok != ITER_DONE) {
1236 strbuf_addstr(err, "unable to write packed-refs file: "
1237 "error iterating over old contents");
1241 if (close_tempfile_gently(refs->tempfile)) {
1242 strbuf_addf(err, "error closing file %s: %s",
1243 get_tempfile_path(refs->tempfile),
1245 strbuf_release(&sb);
1246 delete_tempfile(&refs->tempfile);
1253 strbuf_addf(err, "error writing to %s: %s",
1254 get_tempfile_path(refs->tempfile), strerror(errno));
1258 ref_iterator_abort(iter);
1260 delete_tempfile(&refs->tempfile);
1264 int is_packed_transaction_needed(struct ref_store *ref_store,
1265 struct ref_transaction *transaction)
1267 struct packed_ref_store *refs = packed_downcast(
1270 "is_packed_transaction_needed");
1271 struct strbuf referent = STRBUF_INIT;
1275 if (!is_lock_file_locked(&refs->lock))
1276 BUG("is_packed_transaction_needed() called while unlocked");
1279 * We're only going to bother returning false for the common,
1280 * trivial case that references are only being deleted, their
1281 * old values are not being checked, and the old `packed-refs`
1282 * file doesn't contain any of those reference(s). This gives
1283 * false positives for some other cases that could
1284 * theoretically be optimized away:
1286 * 1. It could be that the old value is being verified without
1287 * setting a new value. In this case, we could verify the
1288 * old value here and skip the update if it agrees. If it
1289 * disagrees, we could either let the update go through
1290 * (the actual commit would re-detect and report the
1291 * problem), or come up with a way of reporting such an
1292 * error to *our* caller.
1294 * 2. It could be that a new value is being set, but that it
1295 * is identical to the current packed value of the
1298 * Neither of these cases will come up in the current code,
1299 * because the only caller of this function passes to it a
1300 * transaction that only includes `delete` updates with no
1301 * `old_id`. Even if that ever changes, false positives only
1302 * cause an optimization to be missed; they do not affect
1307 * Start with the cheap checks that don't require old
1308 * reference values to be read:
1310 for (i = 0; i < transaction->nr; i++) {
1311 struct ref_update *update = transaction->updates[i];
1313 if (update->flags & REF_HAVE_OLD)
1314 /* Have to check the old value -> needed. */
1317 if ((update->flags & REF_HAVE_NEW) && !is_null_oid(&update->new_oid))
1318 /* Have to set a new value -> needed. */
1323 * The transaction isn't checking any old values nor is it
1324 * setting any nonzero new values, so it still might be able
1325 * to be skipped. Now do the more expensive check: the update
1326 * is needed if any of the updates is a delete, and the old
1327 * `packed-refs` file contains a value for that reference.
1330 for (i = 0; i < transaction->nr; i++) {
1331 struct ref_update *update = transaction->updates[i];
1333 struct object_id oid;
1335 if (!(update->flags & REF_HAVE_NEW))
1337 * This reference isn't being deleted -> not
1342 if (!refs_read_raw_ref(ref_store, update->refname,
1343 &oid, &referent, &type) ||
1346 * We have to actually delete that reference
1347 * -> this transaction is needed.
1354 strbuf_release(&referent);
1358 struct packed_transaction_backend_data {
1359 /* True iff the transaction owns the packed-refs lock. */
1362 struct string_list updates;
1365 static void packed_transaction_cleanup(struct packed_ref_store *refs,
1366 struct ref_transaction *transaction)
1368 struct packed_transaction_backend_data *data = transaction->backend_data;
1371 string_list_clear(&data->updates, 0);
1373 if (is_tempfile_active(refs->tempfile))
1374 delete_tempfile(&refs->tempfile);
1376 if (data->own_lock && is_lock_file_locked(&refs->lock)) {
1377 packed_refs_unlock(&refs->base);
1382 transaction->backend_data = NULL;
1385 transaction->state = REF_TRANSACTION_CLOSED;
1388 static int packed_transaction_prepare(struct ref_store *ref_store,
1389 struct ref_transaction *transaction,
1392 struct packed_ref_store *refs = packed_downcast(
1394 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1395 "ref_transaction_prepare");
1396 struct packed_transaction_backend_data *data;
1398 int ret = TRANSACTION_GENERIC_ERROR;
1401 * Note that we *don't* skip transactions with zero updates,
1402 * because such a transaction might be executed for the side
1403 * effect of ensuring that all of the references are peeled or
1404 * ensuring that the `packed-refs` file is sorted. If the
1405 * caller wants to optimize away empty transactions, it should
1409 data = xcalloc(1, sizeof(*data));
1410 string_list_init(&data->updates, 0);
1412 transaction->backend_data = data;
1415 * Stick the updates in a string list by refname so that we
1418 for (i = 0; i < transaction->nr; i++) {
1419 struct ref_update *update = transaction->updates[i];
1420 struct string_list_item *item =
1421 string_list_append(&data->updates, update->refname);
1423 /* Store a pointer to update in item->util: */
1424 item->util = update;
1426 string_list_sort(&data->updates);
1428 if (ref_update_reject_duplicates(&data->updates, err))
1431 if (!is_lock_file_locked(&refs->lock)) {
1432 if (packed_refs_lock(ref_store, 0, err))
1437 if (write_with_updates(refs, &data->updates, err))
1440 transaction->state = REF_TRANSACTION_PREPARED;
1444 packed_transaction_cleanup(refs, transaction);
1448 static int packed_transaction_abort(struct ref_store *ref_store,
1449 struct ref_transaction *transaction,
1452 struct packed_ref_store *refs = packed_downcast(
1454 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1455 "ref_transaction_abort");
1457 packed_transaction_cleanup(refs, transaction);
1461 static int packed_transaction_finish(struct ref_store *ref_store,
1462 struct ref_transaction *transaction,
1465 struct packed_ref_store *refs = packed_downcast(
1467 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1468 "ref_transaction_finish");
1469 int ret = TRANSACTION_GENERIC_ERROR;
1470 char *packed_refs_path;
1472 clear_snapshot(refs);
1474 packed_refs_path = get_locked_file_path(&refs->lock);
1475 if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
1476 strbuf_addf(err, "error replacing %s: %s",
1477 refs->path, strerror(errno));
1484 free(packed_refs_path);
1485 packed_transaction_cleanup(refs, transaction);
1489 static int packed_initial_transaction_commit(struct ref_store *ref_store,
1490 struct ref_transaction *transaction,
1493 return ref_transaction_commit(transaction, err);
1496 static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
1497 struct string_list *refnames, unsigned int flags)
1499 struct packed_ref_store *refs =
1500 packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1501 struct strbuf err = STRBUF_INIT;
1502 struct ref_transaction *transaction;
1503 struct string_list_item *item;
1506 (void)refs; /* We need the check above, but don't use the variable */
1512 * Since we don't check the references' old_oids, the
1513 * individual updates can't fail, so we can pack all of the
1514 * updates into a single transaction.
1517 transaction = ref_store_transaction_begin(ref_store, &err);
1521 for_each_string_list_item(item, refnames) {
1522 if (ref_transaction_delete(transaction, item->string, NULL,
1523 flags, msg, &err)) {
1524 warning(_("could not delete reference %s: %s"),
1525 item->string, err.buf);
1530 ret = ref_transaction_commit(transaction, &err);
1533 if (refnames->nr == 1)
1534 error(_("could not delete reference %s: %s"),
1535 refnames->items[0].string, err.buf);
1537 error(_("could not delete references: %s"), err.buf);
1540 ref_transaction_free(transaction);
1541 strbuf_release(&err);
1545 static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
1548 * Packed refs are already packed. It might be that loose refs
1549 * are packed *into* a packed refs store, but that is done by
1550 * updating the packed references via a transaction.
1555 static int packed_create_symref(struct ref_store *ref_store,
1556 const char *refname, const char *target,
1559 die("BUG: packed reference store does not support symrefs");
1562 static int packed_rename_ref(struct ref_store *ref_store,
1563 const char *oldrefname, const char *newrefname,
1566 die("BUG: packed reference store does not support renaming references");
1569 static int packed_copy_ref(struct ref_store *ref_store,
1570 const char *oldrefname, const char *newrefname,
1573 die("BUG: packed reference store does not support copying references");
1576 static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
1578 return empty_ref_iterator_begin();
1581 static int packed_for_each_reflog_ent(struct ref_store *ref_store,
1582 const char *refname,
1583 each_reflog_ent_fn fn, void *cb_data)
1588 static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1589 const char *refname,
1590 each_reflog_ent_fn fn,
1596 static int packed_reflog_exists(struct ref_store *ref_store,
1597 const char *refname)
1602 static int packed_create_reflog(struct ref_store *ref_store,
1603 const char *refname, int force_create,
1606 die("BUG: packed reference store does not support reflogs");
1609 static int packed_delete_reflog(struct ref_store *ref_store,
1610 const char *refname)
1615 static int packed_reflog_expire(struct ref_store *ref_store,
1616 const char *refname, const struct object_id *oid,
1618 reflog_expiry_prepare_fn prepare_fn,
1619 reflog_expiry_should_prune_fn should_prune_fn,
1620 reflog_expiry_cleanup_fn cleanup_fn,
1621 void *policy_cb_data)
1626 struct ref_storage_be refs_be_packed = {
1629 packed_ref_store_create,
1631 packed_transaction_prepare,
1632 packed_transaction_finish,
1633 packed_transaction_abort,
1634 packed_initial_transaction_commit,
1637 packed_create_symref,
1642 packed_ref_iterator_begin,
1643 packed_read_raw_ref,
1645 packed_reflog_iterator_begin,
1646 packed_for_each_reflog_ent,
1647 packed_for_each_reflog_ent_reverse,
1648 packed_reflog_exists,
1649 packed_create_reflog,
1650 packed_delete_reflog,
1651 packed_reflog_expire