Raw
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "../git-compat-util.h"
4 #include "../config.h"
5 #include "../dir.h"
6 #include "../fsck.h"
7 #include "../gettext.h"
8 #include "../hash.h"
9 #include "../hex.h"
10 #include "../refs.h"
11 #include "refs-internal.h"
12 #include "packed-backend.h"
13 #include "../iterator.h"
14 #include "../lockfile.h"
15 #include "../chdir-notify.h"
16 #include "../statinfo.h"
17 #include "../worktree.h"
18 #include "../wrapper.h"
19 #include "../write-or-die.h"
20 #include "../trace2.h"
21
22 enum mmap_strategy {
23 /*
24 * Don't use mmap() at all for reading `packed-refs`.
25 */
26 MMAP_NONE,
27
28 /*
29 * Can use mmap() for reading `packed-refs`, but the file must
30 * not remain mmapped. This is the usual option on Windows,
31 * where you cannot rename a new version of a file onto a file
32 * that is currently mmapped.
33 */
34 MMAP_TEMPORARY,
35
36 /*
37 * It is OK to leave the `packed-refs` file mmapped while
38 * arbitrary other code is running.
39 */
40 MMAP_OK
41 };
42
43 #if defined(NO_MMAP)
44 static enum mmap_strategy mmap_strategy = MMAP_NONE;
45 #elif defined(MMAP_PREVENTS_DELETE)
46 static enum mmap_strategy mmap_strategy = MMAP_TEMPORARY;
47 #else
48 static enum mmap_strategy mmap_strategy = MMAP_OK;
49 #endif
50
51 struct packed_ref_store;
52
53 /*
54 * A `snapshot` represents one snapshot of a `packed-refs` file.
55 *
56 * Normally, this will be a mmapped view of the contents of the
57 * `packed-refs` file at the time the snapshot was created. However,
58 * if the `packed-refs` file was not sorted, this might point at heap
59 * memory holding the contents of the `packed-refs` file with its
60 * records sorted by refname.
61 *
62 * `snapshot` instances are reference counted (via
63 * `acquire_snapshot()` and `release_snapshot()`). This is to prevent
64 * an instance from disappearing while an iterator is still iterating
65 * over it. Instances are garbage collected when their `referrers`
66 * count goes to zero.
67 *
68 * The most recent `snapshot`, if available, is referenced by the
69 * `packed_ref_store`. Its freshness is checked whenever
70 * `get_snapshot()` is called; if the existing snapshot is obsolete, a
71 * new snapshot is taken.
72 */
73 struct snapshot {
74 /*
75 * A back-pointer to the packed_ref_store with which this
76 * snapshot is associated:
77 */
78 struct packed_ref_store *refs;
79
80 /* Is the `packed-refs` file currently mmapped? */
81 int mmapped;
82
83 /*
84 * The contents of the `packed-refs` file:
85 *
86 * - buf -- a pointer to the start of the memory
87 * - start -- a pointer to the first byte of actual references
88 * (i.e., after the header line, if one is present)
89 * - eof -- a pointer just past the end of the reference
90 * contents
91 *
92 * If the `packed-refs` file was already sorted, `buf` points
93 * at the mmapped contents of the file. If not, it points at
94 * heap-allocated memory containing the contents, sorted. If
95 * there were no contents (e.g., because the file didn't
96 * exist), `buf`, `start`, and `eof` are all NULL.
97 */
98 char *buf, *start, *eof;
99
100 /*
101 * What is the peeled state of the `packed-refs` file that
102 * this snapshot represents? (This is usually determined from
103 * the file's header.)
104 */
105 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled;
106
107 /*
108 * Count of references to this instance, including the pointer
109 * from `packed_ref_store::snapshot`, if any. The instance
110 * will not be freed as long as the reference count is
111 * nonzero.
112 */
113 unsigned int referrers;
114
115 /*
116 * The metadata of the `packed-refs` file from which this
117 * snapshot was created, used to tell if the file has been
118 * replaced since we read it.
119 */
120 struct stat_validity validity;
121 };
122
123 /*
124 * A `ref_store` representing references stored in a `packed-refs`
125 * file. It implements the `ref_store` interface, though it has some
126 * limitations:
127 *
128 * - It cannot store symbolic references.
129 *
130 * - It cannot store reflogs.
131 *
132 * - It does not support reference renaming (though it could).
133 *
134 * On the other hand, it can be locked outside of a reference
135 * transaction. In that case, it remains locked even after the
136 * transaction is done and the new `packed-refs` file is activated.
137 */
138 struct packed_ref_store {
139 struct ref_store base;
140
141 unsigned int store_flags;
142
143 /* The path of the "packed-refs" file: */
144 char *path;
145
146 /*
147 * A snapshot of the values read from the `packed-refs` file,
148 * if it might still be current; otherwise, NULL.
149 */
150 struct snapshot *snapshot;
151
152 /*
153 * Lock used for the "packed-refs" file. Note that this (and
154 * thus the enclosing `packed_ref_store`) must not be freed.
155 */
156 struct lock_file lock;
157
158 /*
159 * Temporary file used when rewriting new contents to the
160 * "packed-refs" file. Note that this (and thus the enclosing
161 * `packed_ref_store`) must not be freed.
162 */
163 struct tempfile *tempfile;
164
165 /*
166 * Timeout when taking the "packed-refs.lock" file. configurable via
167 * "core.packedRefsTimeout".
168 */
169 bool timeout_configured;
170 int timeout_value;
171 };
172
173 /*
174 * Increment the reference count of `*snapshot`.
175 */
176 static void acquire_snapshot(struct snapshot *snapshot)
177 {
178 snapshot->referrers++;
179 }
180
181 /*
182 * If the buffer in `snapshot` is active, then either munmap the
183 * memory and close the file, or free the memory. Then set the buffer
184 * pointers to NULL.
185 */
186 static void clear_snapshot_buffer(struct snapshot *snapshot)
187 {
188 if (snapshot->mmapped) {
189 if (munmap(snapshot->buf, snapshot->eof - snapshot->buf))
190 die_errno("error ummapping packed-refs file %s",
191 snapshot->refs->path);
192 snapshot->mmapped = 0;
193 } else {
194 free(snapshot->buf);
195 }
196 snapshot->buf = snapshot->start = snapshot->eof = NULL;
197 }
198
199 /*
200 * Decrease the reference count of `*snapshot`. If it goes to zero,
201 * free `*snapshot` and return true; otherwise return false.
202 */
203 static int release_snapshot(struct snapshot *snapshot)
204 {
205 if (!--snapshot->referrers) {
206 stat_validity_clear(&snapshot->validity);
207 clear_snapshot_buffer(snapshot);
208 free(snapshot);
209 return 1;
210 } else {
211 return 0;
212 }
213 }
214
215 static size_t snapshot_hexsz(const struct snapshot *snapshot)
216 {
217 return snapshot->refs->base.repo->hash_algo->hexsz;
218 }
219
220 static void packed_ref_store_reparent(const char *name UNUSED,
221 const char *old_cwd,
222 const char *new_cwd,
223 void *payload)
224 {
225 struct packed_ref_store *refs = payload;
226 char *tmp;
227
228 tmp = reparent_relative_path(old_cwd, new_cwd, refs->path);
229 free(refs->path);
230 refs->path = tmp;
231 }
232
233 /*
234 * Since packed-refs is only stored in the common dir, don't parse the
235 * payload and rely on the files-backend to set 'gitdir' correctly.
236 */
237 struct ref_store *packed_ref_store_init(struct repository *repo,
238 const char *payload UNUSED,
239 const char *gitdir,
240 const struct ref_store_init_options *opts)
241 {
242 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
243 struct ref_store *ref_store = (struct ref_store *)refs;
244 struct strbuf sb = STRBUF_INIT;
245
246 base_ref_store_init(ref_store, repo, gitdir, &refs_be_packed);
247 refs->store_flags = opts->access_flags;
248
249 strbuf_addf(&sb, "%s/packed-refs", gitdir);
250 refs->path = strbuf_detach(&sb, NULL);
251 chdir_notify_register(NULL, packed_ref_store_reparent, refs);
252 return ref_store;
253 }
254
255 /*
256 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
257 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
258 * support at least the flags specified in `required_flags`. `caller`
259 * is used in any necessary error messages.
260 */
261 static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
262 unsigned int required_flags,
263 const char *caller)
264 {
265 struct packed_ref_store *refs;
266
267 if (ref_store->be != &refs_be_packed)
268 BUG("ref_store is type \"%s\" not \"packed\" in %s",
269 ref_store->be->name, caller);
270
271 refs = (struct packed_ref_store *)ref_store;
272
273 if ((refs->store_flags & required_flags) != required_flags)
274 BUG("unallowed operation (%s), requires %x, has %x\n",
275 caller, required_flags, refs->store_flags);
276
277 return refs;
278 }
279
280 static void clear_snapshot(struct packed_ref_store *refs)
281 {
282 if (refs->snapshot) {
283 struct snapshot *snapshot = refs->snapshot;
284
285 refs->snapshot = NULL;
286 release_snapshot(snapshot);
287 }
288 }
289
290 static void packed_ref_store_release(struct ref_store *ref_store)
291 {
292 struct packed_ref_store *refs = packed_downcast(ref_store, 0, "release");
293 clear_snapshot(refs);
294 rollback_lock_file(&refs->lock);
295 delete_tempfile(&refs->tempfile);
296 chdir_notify_unregister(NULL, packed_ref_store_reparent, refs);
297 free(refs->path);
298 }
299
300 static NORETURN void die_unterminated_line(const char *path,
301 const char *p, size_t len)
302 {
303 if (len < 80)
304 die("unterminated line in %s: %.*s", path, (int)len, p);
305 else
306 die("unterminated line in %s: %.75s...", path, p);
307 }
308
309 static NORETURN void die_invalid_line(const char *path,
310 const char *p, size_t len)
311 {
312 const char *eol = memchr(p, '\n', len);
313
314 if (!eol)
315 die_unterminated_line(path, p, len);
316 else if (eol - p < 80)
317 die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
318 else
319 die("unexpected line in %s: %.75s...", path, p);
320
321 }
322
323 struct snapshot_record {
324 const char *start;
325 size_t len;
326 };
327
328
329 static int cmp_packed_refname(const char *r1, const char *r2)
330 {
331 while (1) {
332 if (*r1 == '\n')
333 return *r2 == '\n' ? 0 : -1;
334 if (*r1 != *r2) {
335 if (*r2 == '\n')
336 return 1;
337 else
338 return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1;
339 }
340 r1++;
341 r2++;
342 }
343 }
344
345 static int cmp_packed_ref_records(const void *v1, const void *v2,
346 void *cb_data)
347 {
348 const struct snapshot *snapshot = cb_data;
349 const struct snapshot_record *e1 = v1, *e2 = v2;
350 const char *r1 = e1->start + snapshot_hexsz(snapshot) + 1;
351 const char *r2 = e2->start + snapshot_hexsz(snapshot) + 1;
352
353 return cmp_packed_refname(r1, r2);
354 }
355
356 /*
357 * Compare a snapshot record at `rec` to the specified NUL-terminated
358 * refname.
359 */
360 static int cmp_record_to_refname(const char *rec, const char *refname,
361 int start, const struct snapshot *snapshot)
362 {
363 const char *r1 = rec + snapshot_hexsz(snapshot) + 1;
364 const char *r2 = refname;
365
366 while (1) {
367 if (*r1 == '\n')
368 return *r2 ? -1 : 0;
369 if (!*r2)
370 return start ? 1 : -1;
371 if (*r1 != *r2)
372 return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1;
373 r1++;
374 r2++;
375 }
376 }
377
378 /*
379 * `snapshot->buf` is not known to be sorted. Check whether it is, and
380 * if not, sort it into new memory and munmap/free the old storage.
381 */
382 static void sort_snapshot(struct snapshot *snapshot)
383 {
384 struct snapshot_record *records = NULL;
385 size_t alloc = 0, nr = 0;
386 int sorted = 1;
387 const char *pos, *eof, *eol;
388 size_t len, i;
389 char *new_buffer, *dst;
390
391 pos = snapshot->start;
392 eof = snapshot->eof;
393
394 if (pos == eof)
395 return;
396
397 len = eof - pos;
398
399 /*
400 * Initialize records based on a crude estimate of the number
401 * of references in the file (we'll grow it below if needed):
402 */
403 ALLOC_GROW(records, len / 80 + 20, alloc);
404
405 while (pos < eof) {
406 eol = memchr(pos, '\n', eof - pos);
407 if (!eol)
408 /* The safety check should prevent this. */
409 BUG("unterminated line found in packed-refs");
410 if (eol - pos < snapshot_hexsz(snapshot) + 2)
411 die_invalid_line(snapshot->refs->path,
412 pos, eof - pos);
413 eol++;
414 if (eol < eof && *eol == '^') {
415 /*
416 * Keep any peeled line together with its
417 * reference:
418 */
419 const char *peeled_start = eol;
420
421 eol = memchr(peeled_start, '\n', eof - peeled_start);
422 if (!eol)
423 /* The safety check should prevent this. */
424 BUG("unterminated peeled line found in packed-refs");
425 eol++;
426 }
427
428 ALLOC_GROW(records, nr + 1, alloc);
429 records[nr].start = pos;
430 records[nr].len = eol - pos;
431 nr++;
432
433 if (sorted &&
434 nr > 1 &&
435 cmp_packed_ref_records(&records[nr - 2],
436 &records[nr - 1], snapshot) >= 0)
437 sorted = 0;
438
439 pos = eol;
440 }
441
442 if (sorted)
443 goto cleanup;
444
445 /* We need to sort the memory. First we sort the records array: */
446 QSORT_S(records, nr, cmp_packed_ref_records, snapshot);
447
448 /*
449 * Allocate a new chunk of memory, and copy the old memory to
450 * the new in the order indicated by `records` (not bothering
451 * with the header line):
452 */
453 new_buffer = xmalloc(len);
454 for (dst = new_buffer, i = 0; i < nr; i++) {
455 memcpy(dst, records[i].start, records[i].len);
456 dst += records[i].len;
457 }
458
459 /*
460 * Now munmap the old buffer and use the sorted buffer in its
461 * place:
462 */
463 clear_snapshot_buffer(snapshot);
464 snapshot->buf = snapshot->start = new_buffer;
465 snapshot->eof = new_buffer + len;
466
467 cleanup:
468 free(records);
469 }
470
471 /*
472 * Return a pointer to the start of the record that contains the
473 * character `*p` (which must be within the buffer). If no other
474 * record start is found, return `buf`.
475 */
476 static const char *find_start_of_record(const char *buf, const char *p)
477 {
478 while (p > buf && (p[-1] != '\n' || p[0] == '^'))
479 p--;
480 return p;
481 }
482
483 /*
484 * Return a pointer to the start of the record following the record
485 * that contains `*p`. If none is found before `end`, return `end`.
486 */
487 static const char *find_end_of_record(const char *p, const char *end)
488 {
489 while (++p < end && (p[-1] != '\n' || p[0] == '^'))
490 ;
491 return p;
492 }
493
494 /*
495 * We want to be able to compare mmapped reference records quickly,
496 * without totally parsing them. We can do so because the records are
497 * LF-terminated, and the refname should start exactly (GIT_SHA1_HEXSZ
498 * + 1) bytes past the beginning of the record.
499 *
500 * But what if the `packed-refs` file contains garbage? We're willing
501 * to tolerate not detecting the problem, as long as we don't produce
502 * totally garbled output (we can't afford to check the integrity of
503 * the whole file during every Git invocation). But we do want to be
504 * sure that we never read past the end of the buffer in memory and
505 * perform an illegal memory access.
506 *
507 * Guarantee that minimum level of safety by verifying that the last
508 * record in the file is LF-terminated, and that it has at least
509 * (GIT_SHA1_HEXSZ + 1) characters before the LF. Die if either of
510 * these checks fails.
511 */
512 static void verify_buffer_safe(struct snapshot *snapshot)
513 {
514 const char *start = snapshot->start;
515 const char *eof = snapshot->eof;
516 const char *last_line;
517
518 if (start == eof)
519 return;
520
521 last_line = find_start_of_record(start, eof - 1);
522 if (*(eof - 1) != '\n' ||
523 eof - last_line < snapshot_hexsz(snapshot) + 2)
524 die_invalid_line(snapshot->refs->path,
525 last_line, eof - last_line);
526 }
527
528 /*
529 * When parsing the "packed-refs" file, we will parse it line by line.
530 * Because we know the start pointer of the refname and the next
531 * newline pointer, we could calculate the length of the refname by
532 * subtracting the two pointers. However, there is a corner case where
533 * the refname contains corrupted embedded NUL characters. And
534 * `check_refname_format()` will not catch this when the truncated
535 * refname is still a valid refname. To prevent this, we need to check
536 * whether the refname contains the NUL characters.
537 */
538 static int refname_contains_nul(struct strbuf *refname)
539 {
540 return !!memchr(refname->buf, '\0', refname->len);
541 }
542
543 #define SMALL_FILE_SIZE (32*1024)
544
545 static int allocate_snapshot_buffer(struct snapshot *snapshot, int fd, struct stat *st)
546 {
547 ssize_t bytes_read;
548 size_t size;
549
550 size = xsize_t(st->st_size);
551 if (!size)
552 return 0;
553
554 if (mmap_strategy == MMAP_NONE || size <= SMALL_FILE_SIZE) {
555 snapshot->buf = xmalloc(size);
556 bytes_read = read_in_full(fd, snapshot->buf, size);
557 if (bytes_read < 0 || bytes_read != size)
558 die_errno("couldn't read %s", snapshot->refs->path);
559 snapshot->mmapped = 0;
560 } else {
561 snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
562 snapshot->mmapped = 1;
563 }
564
565 snapshot->start = snapshot->buf;
566 snapshot->eof = snapshot->buf + size;
567
568 return 1;
569 }
570
571 /*
572 * Depending on `mmap_strategy`, either mmap or read the contents of
573 * the `packed-refs` file into the snapshot. Return 1 if the file
574 * existed and was read, or 0 if the file was absent or empty. Die on
575 * errors.
576 */
577 static int load_contents(struct snapshot *snapshot)
578 {
579 struct stat st;
580 int ret;
581 int fd;
582
583 fd = open(snapshot->refs->path, O_RDONLY);
584 if (fd < 0) {
585 if (errno == ENOENT) {
586 /*
587 * This is OK; it just means that no
588 * "packed-refs" file has been written yet,
589 * which is equivalent to it being empty,
590 * which is its state when initialized with
591 * zeros.
592 */
593 return 0;
594 } else {
595 die_errno("couldn't read %s", snapshot->refs->path);
596 }
597 }
598
599 stat_validity_update(&snapshot->validity, fd);
600
601 if (fstat(fd, &st) < 0)
602 die_errno("couldn't stat %s", snapshot->refs->path);
603
604 ret = allocate_snapshot_buffer(snapshot, fd, &st);
605
606 close(fd);
607 return ret;
608 }
609
610 static const char *find_reference_location_1(struct snapshot *snapshot,
611 const char *refname, int mustexist,
612 int start)
613 {
614 /*
615 * This is not *quite* a garden-variety binary search, because
616 * the data we're searching is made up of records, and we
617 * always need to find the beginning of a record to do a
618 * comparison. A "record" here is one line for the reference
619 * itself and zero or one peel lines that start with '^'. Our
620 * loop invariant is described in the next two comments.
621 */
622
623 /*
624 * A pointer to the character at the start of a record whose
625 * preceding records all have reference names that come
626 * *before* `refname`.
627 */
628 const char *lo = snapshot->start;
629
630 /*
631 * A pointer to a the first character of a record whose
632 * reference name comes *after* `refname`.
633 */
634 const char *hi = snapshot->eof;
635
636 while (lo != hi) {
637 const char *mid, *rec;
638 int cmp;
639
640 mid = lo + (hi - lo) / 2;
641 rec = find_start_of_record(lo, mid);
642 cmp = cmp_record_to_refname(rec, refname, start, snapshot);
643 if (cmp < 0) {
644 lo = find_end_of_record(mid, hi);
645 } else if (cmp > 0) {
646 hi = rec;
647 } else {
648 return rec;
649 }
650 }
651
652 if (mustexist)
653 return NULL;
654 else
655 return lo;
656 }
657
658 /*
659 * Find the place in `snapshot->buf` where the start of the record for
660 * `refname` starts. If `mustexist` is true and the reference doesn't
661 * exist, then return NULL. If `mustexist` is false and the reference
662 * doesn't exist, then return the point where that reference would be
663 * inserted, or `snapshot->eof` (which might be NULL) if it would be
664 * inserted at the end of the file. In the latter mode, `refname`
665 * doesn't have to be a proper reference name; for example, one could
666 * search for "refs/replace/" to find the start of any replace
667 * references.
668 *
669 * The record is sought using a binary search, so `snapshot->buf` must
670 * be sorted.
671 */
672 static const char *find_reference_location(struct snapshot *snapshot,
673 const char *refname, int mustexist)
674 {
675 return find_reference_location_1(snapshot, refname, mustexist, 1);
676 }
677
678 /*
679 * Find the place in `snapshot->buf` after the end of the record for
680 * `refname`. In other words, find the location of first thing *after*
681 * `refname`.
682 *
683 * Other semantics are identical to the ones in
684 * `find_reference_location()`.
685 */
686 static const char *find_reference_location_end(struct snapshot *snapshot,
687 const char *refname,
688 int mustexist)
689 {
690 return find_reference_location_1(snapshot, refname, mustexist, 0);
691 }
692
693 /*
694 * Create a newly-allocated `snapshot` of the `packed-refs` file in
695 * its current state and return it. The return value will already have
696 * its reference count incremented.
697 *
698 * A comment line of the form "# pack-refs with: " may contain zero or
699 * more traits. We interpret the traits as follows:
700 *
701 * Neither `peeled` nor `fully-peeled`:
702 *
703 * Probably no references are peeled. But if the file contains a
704 * peeled value for a reference, we will use it.
705 *
706 * `peeled`:
707 *
708 * References under "refs/tags/", if they *can* be peeled, *are*
709 * peeled in this file. References outside of "refs/tags/" are
710 * probably not peeled even if they could have been, but if we find
711 * a peeled value for such a reference we will use it.
712 *
713 * `fully-peeled`:
714 *
715 * All references in the file that can be peeled are peeled.
716 * Inversely (and this is more important), any references in the
717 * file for which no peeled value is recorded is not peelable. This
718 * trait should typically be written alongside "peeled" for
719 * compatibility with older clients, but we do not require it
720 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
721 *
722 * `sorted`:
723 *
724 * The references in this file are known to be sorted by refname.
725 */
726 static struct snapshot *create_snapshot(struct packed_ref_store *refs)
727 {
728 struct snapshot *snapshot = xcalloc(1, sizeof(*snapshot));
729 int sorted = 0;
730
731 snapshot->refs = refs;
732 acquire_snapshot(snapshot);
733 snapshot->peeled = PEELED_NONE;
734
735 if (!load_contents(snapshot))
736 return snapshot;
737
738 /* If the file has a header line, process it: */
739 if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') {
740 char *tmp, *p, *eol;
741 struct string_list traits = STRING_LIST_INIT_NODUP;
742
743 eol = memchr(snapshot->buf, '\n',
744 snapshot->eof - snapshot->buf);
745 if (!eol)
746 die_unterminated_line(refs->path,
747 snapshot->buf,
748 snapshot->eof - snapshot->buf);
749
750 tmp = xmemdupz(snapshot->buf, eol - snapshot->buf);
751
752 if (!skip_prefix(tmp, "# pack-refs with: ", (const char **)&p))
753 die_invalid_line(refs->path,
754 snapshot->buf,
755 snapshot->eof - snapshot->buf);
756
757 string_list_split_in_place(&traits, p, " ", -1);
758
759 if (unsorted_string_list_has_string(&traits, "fully-peeled"))
760 snapshot->peeled = PEELED_FULLY;
761 else if (unsorted_string_list_has_string(&traits, "peeled"))
762 snapshot->peeled = PEELED_TAGS;
763
764 sorted = unsorted_string_list_has_string(&traits, "sorted");
765
766 /* perhaps other traits later as well */
767
768 /* The "+ 1" is for the LF character. */
769 snapshot->start = eol + 1;
770
771 string_list_clear(&traits, 0);
772 free(tmp);
773 }
774
775 verify_buffer_safe(snapshot);
776
777 if (!sorted) {
778 sort_snapshot(snapshot);
779
780 /*
781 * Reordering the records might have moved a short one
782 * to the end of the buffer, so verify the buffer's
783 * safety again:
784 */
785 verify_buffer_safe(snapshot);
786 }
787
788 if (mmap_strategy != MMAP_OK && snapshot->mmapped) {
789 /*
790 * We don't want to leave the file mmapped, so we are
791 * forced to make a copy now:
792 */
793 size_t size = snapshot->eof - snapshot->start;
794 char *buf_copy = xmalloc(size);
795
796 memcpy(buf_copy, snapshot->start, size);
797 clear_snapshot_buffer(snapshot);
798 snapshot->buf = snapshot->start = buf_copy;
799 snapshot->eof = buf_copy + size;
800 }
801
802 return snapshot;
803 }
804
805 /*
806 * Check that `refs->snapshot` (if present) still reflects the
807 * contents of the `packed-refs` file. If not, clear the snapshot.
808 */
809 static void validate_snapshot(struct packed_ref_store *refs)
810 {
811 if (refs->snapshot &&
812 !stat_validity_check(&refs->snapshot->validity, refs->path))
813 clear_snapshot(refs);
814 }
815
816 /*
817 * Get the `snapshot` for the specified packed_ref_store, creating and
818 * populating it if it hasn't been read before or if the file has been
819 * changed (according to its `validity` field) since it was last read.
820 * On the other hand, if we hold the lock, then assume that the file
821 * hasn't been changed out from under us, so skip the extra `stat()`
822 * call in `stat_validity_check()`. This function does *not* increase
823 * the snapshot's reference count on behalf of the caller.
824 */
825 static struct snapshot *get_snapshot(struct packed_ref_store *refs)
826 {
827 if (!is_lock_file_locked(&refs->lock))
828 validate_snapshot(refs);
829
830 if (!refs->snapshot)
831 refs->snapshot = create_snapshot(refs);
832
833 return refs->snapshot;
834 }
835
836 static int packed_read_raw_ref(struct ref_store *ref_store, const char *refname,
837 struct object_id *oid, struct strbuf *referent UNUSED,
838 unsigned int *type, int *failure_errno)
839 {
840 struct packed_ref_store *refs =
841 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
842 struct snapshot *snapshot = get_snapshot(refs);
843 const char *rec;
844
845 *type = 0;
846
847 rec = find_reference_location(snapshot, refname, 1);
848
849 if (!rec) {
850 /* refname is not a packed reference. */
851 *failure_errno = ENOENT;
852 return -1;
853 }
854
855 if (get_oid_hex_algop(rec, oid, ref_store->repo->hash_algo))
856 die_invalid_line(refs->path, rec, snapshot->eof - rec);
857
858 *type = REF_ISPACKED;
859 return 0;
860 }
861
862 /*
863 * This value is set in `base.flags` if the peeled value of the
864 * current reference is known. In that case, `peeled` contains the
865 * correct peeled value for the reference, which might be `null_oid`
866 * if the reference is not a tag or if it is broken.
867 */
868 #define REF_KNOWS_PEELED 0x40
869
870 /*
871 * An iterator over a snapshot of a `packed-refs` file.
872 */
873 struct packed_ref_iterator {
874 struct ref_iterator base;
875
876 struct snapshot *snapshot;
877
878 char *prefix;
879
880 /* The current position in the snapshot's buffer: */
881 const char *pos;
882
883 /* The end of the part of the buffer that will be iterated over: */
884 const char *eof;
885
886 struct jump_list_entry {
887 const char *start;
888 const char *end;
889 } *jump;
890 size_t jump_nr, jump_alloc;
891 size_t jump_cur;
892
893 /* Scratch space for current values: */
894 struct object_id oid, peeled;
895 struct strbuf refname_buf;
896
897 struct repository *repo;
898 unsigned int flags;
899 };
900
901 /*
902 * Move the iterator to the next record in the snapshot. Adjust the fields in
903 * `iter` and return `ITER_OK` or `ITER_DONE`. This function does not free the
904 * iterator in the case of `ITER_DONE`.
905 */
906 static int next_record(struct packed_ref_iterator *iter)
907 {
908 const char *p, *eol;
909
910 memset(&iter->base.ref, 0, sizeof(iter->base.ref));
911 strbuf_reset(&iter->refname_buf);
912
913 /*
914 * If iter->pos is contained within a skipped region, jump past
915 * it.
916 *
917 * Note that each skipped region is considered at most once,
918 * since they are ordered based on their starting position.
919 */
920 while (iter->jump_cur < iter->jump_nr) {
921 struct jump_list_entry *curr = &iter->jump[iter->jump_cur];
922 if (iter->pos < curr->start)
923 break; /* not to the next jump yet */
924
925 iter->jump_cur++;
926 if (iter->pos < curr->end) {
927 iter->pos = curr->end;
928 trace2_counter_add(TRACE2_COUNTER_ID_PACKED_REFS_JUMPS, 1);
929 /* jumps are coalesced, so only one jump is necessary */
930 break;
931 }
932 }
933
934 if (iter->pos == iter->eof)
935 return ITER_DONE;
936
937 iter->base.ref.flags = REF_ISPACKED;
938 p = iter->pos;
939
940 if (iter->eof - p < snapshot_hexsz(iter->snapshot) + 2 ||
941 parse_oid_hex_algop(p, &iter->oid, &p, iter->repo->hash_algo) ||
942 !isspace(*p++))
943 die_invalid_line(iter->snapshot->refs->path,
944 iter->pos, iter->eof - iter->pos);
945 iter->base.ref.oid = &iter->oid;
946
947 eol = memchr(p, '\n', iter->eof - p);
948 if (!eol)
949 die_unterminated_line(iter->snapshot->refs->path,
950 iter->pos, iter->eof - iter->pos);
951
952 strbuf_add(&iter->refname_buf, p, eol - p);
953 iter->base.ref.name = iter->refname_buf.buf;
954
955 if (refname_contains_nul(&iter->refname_buf))
956 die("packed refname contains embedded NULL: %s", iter->base.ref.name);
957
958 if (check_refname_format(iter->base.ref.name, REFNAME_ALLOW_ONELEVEL)) {
959 if (!refname_is_safe(iter->base.ref.name))
960 die("packed refname is dangerous: %s",
961 iter->base.ref.name);
962 oidclr(&iter->oid, iter->repo->hash_algo);
963 iter->base.ref.flags |= REF_BAD_NAME | REF_ISBROKEN;
964 }
965 if (iter->snapshot->peeled == PEELED_FULLY ||
966 (iter->snapshot->peeled == PEELED_TAGS &&
967 starts_with(iter->base.ref.name, "refs/tags/")))
968 iter->base.ref.flags |= REF_KNOWS_PEELED;
969
970 iter->pos = eol + 1;
971
972 if (iter->pos < iter->eof && *iter->pos == '^') {
973 p = iter->pos + 1;
974 if (iter->eof - p < snapshot_hexsz(iter->snapshot) + 1 ||
975 parse_oid_hex_algop(p, &iter->peeled, &p, iter->repo->hash_algo) ||
976 *p++ != '\n')
977 die_invalid_line(iter->snapshot->refs->path,
978 iter->pos, iter->eof - iter->pos);
979 iter->pos = p;
980
981 /*
982 * Regardless of what the file header said, we
983 * definitely know the value of *this* reference. But
984 * we suppress it if the reference is broken:
985 */
986 if ((iter->base.ref.flags & REF_ISBROKEN)) {
987 oidclr(&iter->peeled, iter->repo->hash_algo);
988 iter->base.ref.flags &= ~REF_KNOWS_PEELED;
989 } else {
990 iter->base.ref.flags |= REF_KNOWS_PEELED;
991 iter->base.ref.peeled_oid = &iter->peeled;
992 }
993 } else {
994 oidclr(&iter->peeled, iter->repo->hash_algo);
995 }
996
997 return ITER_OK;
998 }
999
1000 static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
1001 {
1002 struct packed_ref_iterator *iter =
1003 (struct packed_ref_iterator *)ref_iterator;
1004 int ok;
1005
1006 while ((ok = next_record(iter)) == ITER_OK) {
1007 const char *refname = iter->base.ref.name;
1008 const char *prefix = iter->prefix;
1009
1010 if (iter->flags & REFS_FOR_EACH_PER_WORKTREE_ONLY &&
1011 !is_per_worktree_ref(iter->base.ref.name))
1012 continue;
1013
1014 if (!(iter->flags & REFS_FOR_EACH_INCLUDE_BROKEN) &&
1015 !ref_resolves_to_object(iter->base.ref.name, iter->repo,
1016 &iter->oid, iter->flags))
1017 continue;
1018
1019 while (prefix && *prefix) {
1020 if ((unsigned char)*refname < (unsigned char)*prefix)
1021 BUG("packed-refs backend yielded reference preceding its prefix");
1022 else if ((unsigned char)*refname > (unsigned char)*prefix)
1023 return ITER_DONE;
1024 prefix++;
1025 refname++;
1026 }
1027
1028 return ITER_OK;
1029 }
1030
1031 return ok;
1032 }
1033
1034 static int packed_ref_iterator_seek(struct ref_iterator *ref_iterator,
1035 const char *refname, unsigned int flags)
1036 {
1037 struct packed_ref_iterator *iter =
1038 (struct packed_ref_iterator *)ref_iterator;
1039 const char *start;
1040
1041 if (refname && *refname)
1042 start = find_reference_location(iter->snapshot, refname, 0);
1043 else
1044 start = iter->snapshot->start;
1045
1046 /* Unset any previously set prefix */
1047 FREE_AND_NULL(iter->prefix);
1048
1049 if (flags & REF_ITERATOR_SEEK_SET_PREFIX)
1050 iter->prefix = xstrdup_or_null(refname);
1051
1052 iter->pos = start;
1053 iter->eof = iter->snapshot->eof;
1054
1055 return 0;
1056 }
1057
1058 static void packed_ref_iterator_release(struct ref_iterator *ref_iterator)
1059 {
1060 struct packed_ref_iterator *iter =
1061 (struct packed_ref_iterator *)ref_iterator;
1062 strbuf_release(&iter->refname_buf);
1063 free(iter->jump);
1064 free(iter->prefix);
1065 release_snapshot(iter->snapshot);
1066 }
1067
1068 static struct ref_iterator_vtable packed_ref_iterator_vtable = {
1069 .advance = packed_ref_iterator_advance,
1070 .seek = packed_ref_iterator_seek,
1071 .release = packed_ref_iterator_release,
1072 };
1073
1074 static int jump_list_entry_cmp(const void *va, const void *vb)
1075 {
1076 const struct jump_list_entry *a = va;
1077 const struct jump_list_entry *b = vb;
1078
1079 if (a->start < b->start)
1080 return -1;
1081 if (a->start > b->start)
1082 return 1;
1083 return 0;
1084 }
1085
1086 static int has_glob_special(const char *str)
1087 {
1088 const char *p;
1089 for (p = str; *p; p++) {
1090 if (is_glob_special(*p))
1091 return 1;
1092 }
1093 return 0;
1094 }
1095
1096 static void populate_excluded_jump_list(struct packed_ref_iterator *iter,
1097 struct snapshot *snapshot,
1098 const char **excluded_patterns)
1099 {
1100 size_t i, j;
1101 const char **pattern;
1102 struct jump_list_entry *last_disjoint;
1103
1104 if (!excluded_patterns)
1105 return;
1106
1107 for (pattern = excluded_patterns; *pattern; pattern++) {
1108 struct jump_list_entry *e;
1109 const char *start, *end;
1110
1111 /*
1112 * We can't feed any excludes with globs in them to the
1113 * refs machinery. It only understands prefix matching.
1114 * We likewise can't even feed the string leading up to
1115 * the first meta-character, as something like "foo[a]"
1116 * should not exclude "foobar" (but the prefix "foo"
1117 * would match that and mark it for exclusion).
1118 */
1119 if (has_glob_special(*pattern))
1120 continue;
1121
1122 start = find_reference_location(snapshot, *pattern, 0);
1123 end = find_reference_location_end(snapshot, *pattern, 0);
1124
1125 if (start == end)
1126 continue; /* nothing to jump over */
1127
1128 ALLOC_GROW(iter->jump, iter->jump_nr + 1, iter->jump_alloc);
1129
1130 e = &iter->jump[iter->jump_nr++];
1131 e->start = start;
1132 e->end = end;
1133 }
1134
1135 if (!iter->jump_nr) {
1136 /*
1137 * Every entry in exclude_patterns has a meta-character,
1138 * nothing to do here.
1139 */
1140 return;
1141 }
1142
1143 QSORT(iter->jump, iter->jump_nr, jump_list_entry_cmp);
1144
1145 /*
1146 * As an optimization, merge adjacent entries in the jump list
1147 * to jump forwards as far as possible when entering a skipped
1148 * region.
1149 *
1150 * For example, if we have two skipped regions:
1151 *
1152 * [[A, B], [B, C]]
1153 *
1154 * we want to combine that into a single entry jumping from A to
1155 * C.
1156 */
1157 last_disjoint = iter->jump;
1158
1159 for (i = 1, j = 1; i < iter->jump_nr; i++) {
1160 struct jump_list_entry *ours = &iter->jump[i];
1161 if (ours->start <= last_disjoint->end) {
1162 /* overlapping regions extend the previous one */
1163 last_disjoint->end = last_disjoint->end > ours->end
1164 ? last_disjoint->end : ours->end;
1165 } else {
1166 /* otherwise, insert a new region */
1167 iter->jump[j++] = *ours;
1168 last_disjoint = ours;
1169 }
1170 }
1171
1172 iter->jump_nr = j;
1173 iter->jump_cur = 0;
1174 }
1175
1176 static struct ref_iterator *packed_ref_iterator_begin(
1177 struct ref_store *ref_store,
1178 const char *prefix, const char **exclude_patterns,
1179 unsigned int flags)
1180 {
1181 struct packed_ref_store *refs;
1182 struct snapshot *snapshot;
1183 struct packed_ref_iterator *iter;
1184 struct ref_iterator *ref_iterator;
1185 unsigned int required_flags = REF_STORE_READ;
1186
1187 if (!(flags & REFS_FOR_EACH_INCLUDE_BROKEN))
1188 required_flags |= REF_STORE_ODB;
1189 refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
1190
1191 /*
1192 * Note that `get_snapshot()` internally checks whether the
1193 * snapshot is up to date with what is on disk, and re-reads
1194 * it if not.
1195 */
1196 snapshot = get_snapshot(refs);
1197
1198 CALLOC_ARRAY(iter, 1);
1199 ref_iterator = &iter->base;
1200 base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable);
1201
1202 if (exclude_patterns)
1203 populate_excluded_jump_list(iter, snapshot, exclude_patterns);
1204
1205 iter->snapshot = snapshot;
1206 acquire_snapshot(snapshot);
1207 strbuf_init(&iter->refname_buf, 0);
1208 iter->repo = ref_store->repo;
1209 iter->flags = flags;
1210
1211 if (packed_ref_iterator_seek(&iter->base, prefix,
1212 REF_ITERATOR_SEEK_SET_PREFIX) < 0) {
1213 ref_iterator_free(&iter->base);
1214 return NULL;
1215 }
1216
1217 return ref_iterator;
1218 }
1219
1220 /*
1221 * Write an entry to the packed-refs file for the specified refname.
1222 * If peeled is non-NULL, write it as the entry's peeled value. On
1223 * error, return a nonzero value and leave errno set at the value left
1224 * by the failing call to `fprintf()`.
1225 */
1226 static int write_packed_entry(FILE *fh, const char *refname,
1227 const struct object_id *oid,
1228 const struct object_id *peeled)
1229 {
1230 if (fprintf(fh, "%s %s\n", oid_to_hex(oid), refname) < 0 ||
1231 (peeled && fprintf(fh, "^%s\n", oid_to_hex(peeled)) < 0))
1232 return -1;
1233
1234 return 0;
1235 }
1236
1237 int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
1238 {
1239 struct packed_ref_store *refs =
1240 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
1241 "packed_refs_lock");
1242
1243 if (!refs->timeout_configured) {
1244 if (repo_config_get_int(ref_store->repo, "core.packedrefstimeout",
1245 &refs->timeout_value))
1246 refs->timeout_value = 1000;
1247 refs->timeout_configured = true;
1248 }
1249
1250 /*
1251 * Note that we close the lockfile immediately because we
1252 * don't write new content to it, but rather to a separate
1253 * tempfile.
1254 */
1255 if (repo_hold_lock_file_for_update_timeout(ref_store->repo, &refs->lock,
1256 refs->path, flags,
1257 refs->timeout_value) < 0) {
1258 unable_to_lock_message(refs->path, errno, err);
1259 return -1;
1260 }
1261
1262 if (close_lock_file_gently(&refs->lock)) {
1263 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
1264 rollback_lock_file(&refs->lock);
1265 return -1;
1266 }
1267
1268 /*
1269 * There is a stat-validity problem might cause `update-ref -d`
1270 * lost the newly commit of a ref, because a new `packed-refs`
1271 * file might has the same on-disk file attributes such as
1272 * timestamp, file size and inode value, but has a changed
1273 * ref value.
1274 *
1275 * This could happen with a very small chance when
1276 * `update-ref -d` is called and at the same time another
1277 * `pack-refs --all` process is running.
1278 *
1279 * Now that we hold the `packed-refs` lock, it is important
1280 * to make sure we could read the latest version of
1281 * `packed-refs` file no matter we have just mmap it or not.
1282 * So what need to do is clear the snapshot if we hold it
1283 * already.
1284 */
1285 clear_snapshot(refs);
1286
1287 /*
1288 * Now make sure that the packed-refs file as it exists in the
1289 * locked state is loaded into the snapshot:
1290 */
1291 get_snapshot(refs);
1292 return 0;
1293 }
1294
1295 void packed_refs_unlock(struct ref_store *ref_store)
1296 {
1297 struct packed_ref_store *refs = packed_downcast(
1298 ref_store,
1299 REF_STORE_READ | REF_STORE_WRITE,
1300 "packed_refs_unlock");
1301
1302 if (!is_lock_file_locked(&refs->lock))
1303 BUG("packed_refs_unlock() called when not locked");
1304 rollback_lock_file(&refs->lock);
1305 }
1306
1307 int packed_refs_is_locked(struct ref_store *ref_store)
1308 {
1309 struct packed_ref_store *refs = packed_downcast(
1310 ref_store,
1311 REF_STORE_READ | REF_STORE_WRITE,
1312 "packed_refs_is_locked");
1313
1314 return is_lock_file_locked(&refs->lock);
1315 }
1316
1317 int packed_refs_size(struct ref_store *ref_store,
1318 size_t *out)
1319 {
1320 struct packed_ref_store *refs = packed_downcast(ref_store, REF_STORE_READ,
1321 "packed_refs_size");
1322 struct stat st;
1323
1324 if (stat(refs->path, &st) < 0) {
1325 if (errno != ENOENT)
1326 return -1;
1327 *out = 0;
1328 return 0;
1329 }
1330
1331 *out = st.st_size;
1332 return 0;
1333 }
1334
1335 /*
1336 * The packed-refs header line that we write out. Perhaps other traits
1337 * will be added later.
1338 *
1339 * Note that earlier versions of Git used to parse these traits by
1340 * looking for " trait " in the line. For this reason, the space after
1341 * the colon and the trailing space are required.
1342 */
1343 static const char PACKED_REFS_HEADER[] =
1344 "# pack-refs with: peeled fully-peeled sorted \n";
1345
1346 static int packed_ref_store_create_on_disk(struct ref_store *ref_store UNUSED,
1347 int flags UNUSED,
1348 struct strbuf *err UNUSED)
1349 {
1350 /* Nothing to do. */
1351 return 0;
1352 }
1353
1354 static int packed_ref_store_remove_on_disk(struct ref_store *ref_store,
1355 struct strbuf *err)
1356 {
1357 struct packed_ref_store *refs = packed_downcast(ref_store, 0, "remove");
1358
1359 if (remove_path(refs->path) < 0) {
1360 strbuf_addstr(err, "could not delete packed-refs");
1361 return -1;
1362 }
1363
1364 return 0;
1365 }
1366
1367 /*
1368 * Write the packed refs from the current snapshot to the packed-refs
1369 * tempfile, incorporating any changes from `updates`. `updates` must
1370 * be a sorted string list whose keys are the refnames and whose util
1371 * values are `struct ref_update *`. On error, rollback the tempfile,
1372 * write an error message to `err`, and return a nonzero value.
1373 *
1374 * The packfile must be locked before calling this function and will
1375 * remain locked when it is done.
1376 */
1377 static enum ref_transaction_error write_with_updates(struct packed_ref_store *refs,
1378 struct ref_transaction *transaction,
1379 struct strbuf *err)
1380 {
1381 enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;
1382 struct string_list *updates = &transaction->refnames;
1383 struct ref_iterator *iter = NULL;
1384 size_t i;
1385 int ok;
1386 FILE *out;
1387 struct strbuf sb = STRBUF_INIT;
1388 char *packed_refs_path;
1389
1390 if (!is_lock_file_locked(&refs->lock))
1391 BUG("write_with_updates() called while unlocked");
1392
1393 /*
1394 * If packed-refs is a symlink, we want to overwrite the
1395 * symlinked-to file, not the symlink itself. Also, put the
1396 * staging file next to it:
1397 */
1398 packed_refs_path = get_locked_file_path(&refs->lock);
1399 strbuf_addf(&sb, "%s.new", packed_refs_path);
1400 free(packed_refs_path);
1401 refs->tempfile = repo_create_tempfile(refs->base.repo, sb.buf);
1402 if (!refs->tempfile) {
1403 strbuf_addf(err, "unable to create file %s: %s",
1404 sb.buf, strerror(errno));
1405 strbuf_release(&sb);
1406 return REF_TRANSACTION_ERROR_GENERIC;
1407 }
1408 strbuf_release(&sb);
1409
1410 out = fdopen_tempfile(refs->tempfile, "w");
1411 if (!out) {
1412 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
1413 strerror(errno));
1414 goto error;
1415 }
1416
1417 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
1418 goto write_error;
1419
1420 /*
1421 * We iterate in parallel through the current list of refs and
1422 * the list of updates, processing an entry from at least one
1423 * of the lists each time through the loop. When the current
1424 * list of refs is exhausted, set iter to NULL. When the list
1425 * of updates is exhausted, leave i set to updates->nr.
1426 */
1427 iter = packed_ref_iterator_begin(&refs->base, "", NULL,
1428 REFS_FOR_EACH_INCLUDE_BROKEN);
1429 if ((ok = ref_iterator_advance(iter)) != ITER_OK) {
1430 ref_iterator_free(iter);
1431 iter = NULL;
1432 }
1433
1434 i = 0;
1435
1436 while (iter || i < updates->nr) {
1437 struct ref_update *update = NULL;
1438 int cmp;
1439
1440 if (i >= updates->nr) {
1441 cmp = -1;
1442 } else {
1443 update = updates->items[i].util;
1444
1445 if (!iter)
1446 cmp = +1;
1447 else
1448 cmp = strcmp(iter->ref.name, update->refname);
1449 }
1450
1451 if (!cmp) {
1452 /*
1453 * There is both an old value and an update
1454 * for this reference. Check the old value if
1455 * necessary:
1456 */
1457 if ((update->flags & REF_HAVE_OLD)) {
1458 if (is_null_oid(&update->old_oid)) {
1459 strbuf_addf(err, "cannot update ref '%s': "
1460 "reference already exists",
1461 update->refname);
1462 ret = REF_TRANSACTION_ERROR_CREATE_EXISTS;
1463
1464 if (ref_transaction_maybe_set_rejected(transaction, i,
1465 ret, err)) {
1466 ret = 0;
1467 continue;
1468 }
1469
1470 goto error;
1471 } else if (!oideq(&update->old_oid, iter->ref.oid)) {
1472 strbuf_addf(err, "cannot update ref '%s': "
1473 "is at %s but expected %s",
1474 update->refname,
1475 oid_to_hex(iter->ref.oid),
1476 oid_to_hex(&update->old_oid));
1477 ret = REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;
1478
1479 if (ref_transaction_maybe_set_rejected(transaction, i,
1480 ret, err)) {
1481 ret = 0;
1482 continue;
1483 }
1484
1485 goto error;
1486 }
1487 }
1488
1489 /* Now figure out what to use for the new value: */
1490 if ((update->flags & REF_HAVE_NEW)) {
1491 /*
1492 * The update takes precedence. Skip
1493 * the iterator over the unneeded
1494 * value.
1495 */
1496 if ((ok = ref_iterator_advance(iter)) != ITER_OK) {
1497 ref_iterator_free(iter);
1498 iter = NULL;
1499 }
1500 cmp = +1;
1501 } else {
1502 /*
1503 * The update doesn't actually want to
1504 * change anything. We're done with it.
1505 */
1506 i++;
1507 cmp = -1;
1508 }
1509 } else if (cmp > 0) {
1510 /*
1511 * There is no old value but there is an
1512 * update for this reference. Make sure that
1513 * the update didn't expect an existing value:
1514 */
1515 if ((update->flags & REF_HAVE_OLD) &&
1516 !is_null_oid(&update->old_oid)) {
1517 strbuf_addf(err, "cannot update ref '%s': "
1518 "reference is missing but expected %s",
1519 update->refname,
1520 oid_to_hex(&update->old_oid));
1521 ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;
1522
1523 if (ref_transaction_maybe_set_rejected(transaction, i,
1524 ret, err)) {
1525 ret = 0;
1526 continue;
1527 }
1528
1529 goto error;
1530 }
1531 }
1532
1533 if (cmp < 0) {
1534 /* Pass the old reference through. */
1535 if (write_packed_entry(out, iter->ref.name,
1536 iter->ref.oid, iter->ref.peeled_oid))
1537 goto write_error;
1538
1539 if ((ok = ref_iterator_advance(iter)) != ITER_OK) {
1540 ref_iterator_free(iter);
1541 iter = NULL;
1542 }
1543 } else if (is_null_oid(&update->new_oid)) {
1544 /*
1545 * The update wants to delete the reference,
1546 * and the reference either didn't exist or we
1547 * have already skipped it. So we're done with
1548 * the update (and don't have to write
1549 * anything).
1550 */
1551 i++;
1552 } else {
1553 bool peeled = update->flags & REF_HAVE_PEELED;
1554
1555 if (write_packed_entry(out, update->refname,
1556 &update->new_oid,
1557 peeled ? &update->peeled : NULL))
1558 goto write_error;
1559
1560 i++;
1561 }
1562 }
1563
1564 if (ok != ITER_DONE) {
1565 strbuf_addstr(err, "unable to write packed-refs file: "
1566 "error iterating over old contents");
1567 goto error;
1568 }
1569
1570 if (fflush(out) ||
1571 fsync_component(FSYNC_COMPONENT_REFERENCE, get_tempfile_fd(refs->tempfile)) ||
1572 close_tempfile_gently(refs->tempfile)) {
1573 strbuf_addf(err, "error closing file %s: %s",
1574 get_tempfile_path(refs->tempfile),
1575 strerror(errno));
1576 strbuf_release(&sb);
1577 delete_tempfile(&refs->tempfile);
1578 return REF_TRANSACTION_ERROR_GENERIC;
1579 }
1580
1581 return 0;
1582
1583 write_error:
1584 strbuf_addf(err, "error writing to %s: %s",
1585 get_tempfile_path(refs->tempfile), strerror(errno));
1586 ret = REF_TRANSACTION_ERROR_GENERIC;
1587
1588 error:
1589 ref_iterator_free(iter);
1590 delete_tempfile(&refs->tempfile);
1591 return ret;
1592 }
1593
1594 int is_packed_transaction_needed(struct ref_store *ref_store,
1595 struct ref_transaction *transaction)
1596 {
1597 struct packed_ref_store *refs = packed_downcast(
1598 ref_store,
1599 REF_STORE_READ,
1600 "is_packed_transaction_needed");
1601 struct strbuf referent = STRBUF_INIT;
1602 size_t i;
1603 int ret;
1604
1605 if (!is_lock_file_locked(&refs->lock))
1606 BUG("is_packed_transaction_needed() called while unlocked");
1607
1608 /*
1609 * We're only going to bother returning false for the common,
1610 * trivial case that references are only being deleted, their
1611 * old values are not being checked, and the old `packed-refs`
1612 * file doesn't contain any of those reference(s). This gives
1613 * false positives for some other cases that could
1614 * theoretically be optimized away:
1615 *
1616 * 1. It could be that the old value is being verified without
1617 * setting a new value. In this case, we could verify the
1618 * old value here and skip the update if it agrees. If it
1619 * disagrees, we could either let the update go through
1620 * (the actual commit would re-detect and report the
1621 * problem), or come up with a way of reporting such an
1622 * error to *our* caller.
1623 *
1624 * 2. It could be that a new value is being set, but that it
1625 * is identical to the current packed value of the
1626 * reference.
1627 *
1628 * Neither of these cases will come up in the current code,
1629 * because the only caller of this function passes to it a
1630 * transaction that only includes `delete` updates with no
1631 * `old_id`. Even if that ever changes, false positives only
1632 * cause an optimization to be missed; they do not affect
1633 * correctness.
1634 */
1635
1636 /*
1637 * Start with the cheap checks that don't require old
1638 * reference values to be read:
1639 */
1640 for (i = 0; i < transaction->nr; i++) {
1641 struct ref_update *update = transaction->updates[i];
1642
1643 if (update->flags & REF_HAVE_OLD)
1644 /* Have to check the old value -> needed. */
1645 return 1;
1646
1647 if ((update->flags & REF_HAVE_NEW) && !is_null_oid(&update->new_oid))
1648 /* Have to set a new value -> needed. */
1649 return 1;
1650 }
1651
1652 /*
1653 * The transaction isn't checking any old values nor is it
1654 * setting any nonzero new values, so it still might be able
1655 * to be skipped. Now do the more expensive check: the update
1656 * is needed if any of the updates is a delete, and the old
1657 * `packed-refs` file contains a value for that reference.
1658 */
1659 ret = 0;
1660 for (i = 0; i < transaction->nr; i++) {
1661 struct ref_update *update = transaction->updates[i];
1662 int failure_errno;
1663 unsigned int type;
1664 struct object_id oid;
1665
1666 if (!(update->flags & REF_HAVE_NEW))
1667 /*
1668 * This reference isn't being deleted -> not
1669 * needed.
1670 */
1671 continue;
1672
1673 if (!refs_read_raw_ref(ref_store, update->refname, &oid,
1674 &referent, &type, &failure_errno) ||
1675 failure_errno != ENOENT) {
1676 /*
1677 * We have to actually delete that reference
1678 * -> this transaction is needed.
1679 */
1680 ret = 1;
1681 break;
1682 }
1683 }
1684
1685 strbuf_release(&referent);
1686 return ret;
1687 }
1688
1689 struct packed_transaction_backend_data {
1690 /* True iff the transaction owns the packed-refs lock. */
1691 int own_lock;
1692 };
1693
1694 static void packed_transaction_cleanup(struct packed_ref_store *refs,
1695 struct ref_transaction *transaction)
1696 {
1697 struct packed_transaction_backend_data *data = transaction->backend_data;
1698
1699 if (data) {
1700 if (is_tempfile_active(refs->tempfile))
1701 delete_tempfile(&refs->tempfile);
1702
1703 if (data->own_lock && is_lock_file_locked(&refs->lock)) {
1704 packed_refs_unlock(&refs->base);
1705 data->own_lock = 0;
1706 }
1707
1708 free(data);
1709 transaction->backend_data = NULL;
1710 }
1711
1712 transaction->state = REF_TRANSACTION_CLOSED;
1713 }
1714
1715 static int packed_transaction_prepare(struct ref_store *ref_store,
1716 struct ref_transaction *transaction,
1717 struct strbuf *err)
1718 {
1719 struct packed_ref_store *refs = packed_downcast(
1720 ref_store,
1721 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1722 "ref_transaction_prepare");
1723 struct packed_transaction_backend_data *data;
1724 enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;
1725
1726 /*
1727 * Note that we *don't* skip transactions with zero updates,
1728 * because such a transaction might be executed for the side
1729 * effect of ensuring that all of the references are peeled or
1730 * ensuring that the `packed-refs` file is sorted. If the
1731 * caller wants to optimize away empty transactions, it should
1732 * do so itself.
1733 */
1734
1735 CALLOC_ARRAY(data, 1);
1736
1737 transaction->backend_data = data;
1738
1739 if (!is_lock_file_locked(&refs->lock)) {
1740 if (packed_refs_lock(ref_store, 0, err))
1741 goto failure;
1742 data->own_lock = 1;
1743 }
1744
1745 ret = write_with_updates(refs, transaction, err);
1746 if (ret)
1747 goto failure;
1748
1749 transaction->state = REF_TRANSACTION_PREPARED;
1750 return 0;
1751
1752 failure:
1753 packed_transaction_cleanup(refs, transaction);
1754 return ret;
1755 }
1756
1757 static int packed_transaction_abort(struct ref_store *ref_store,
1758 struct ref_transaction *transaction,
1759 struct strbuf *err UNUSED)
1760 {
1761 struct packed_ref_store *refs = packed_downcast(
1762 ref_store,
1763 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1764 "ref_transaction_abort");
1765
1766 packed_transaction_cleanup(refs, transaction);
1767 return 0;
1768 }
1769
1770 static int packed_transaction_finish(struct ref_store *ref_store,
1771 struct ref_transaction *transaction,
1772 struct strbuf *err)
1773 {
1774 struct packed_ref_store *refs = packed_downcast(
1775 ref_store,
1776 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1777 "ref_transaction_finish");
1778 int ret = REF_TRANSACTION_ERROR_GENERIC;
1779 char *packed_refs_path;
1780
1781 clear_snapshot(refs);
1782
1783 packed_refs_path = get_locked_file_path(&refs->lock);
1784 if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
1785 strbuf_addf(err, "error replacing %s: %s",
1786 refs->path, strerror(errno));
1787 goto cleanup;
1788 }
1789
1790 ret = 0;
1791
1792 cleanup:
1793 free(packed_refs_path);
1794 packed_transaction_cleanup(refs, transaction);
1795 return ret;
1796 }
1797
1798 static int packed_optimize(struct ref_store *ref_store UNUSED,
1799 struct refs_optimize_opts *opts UNUSED)
1800 {
1801 /*
1802 * Packed refs are already packed. It might be that loose refs
1803 * are packed *into* a packed refs store, but that is done by
1804 * updating the packed references via a transaction.
1805 */
1806 return 0;
1807 }
1808
1809 static int packed_optimize_required(struct ref_store *ref_store UNUSED,
1810 struct refs_optimize_opts *opts UNUSED,
1811 bool *required)
1812 {
1813 /*
1814 * Packed refs are already optimized.
1815 */
1816 *required = false;
1817 return 0;
1818 }
1819
1820 static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store UNUSED)
1821 {
1822 return empty_ref_iterator_begin();
1823 }
1824
1825 static int packed_fsck_ref_next_line(struct fsck_options *o,
1826 unsigned long line_number, const char *start,
1827 const char *eof, const char **eol)
1828 {
1829 int ret = 0;
1830
1831 *eol = memchr(start, '\n', eof - start);
1832 if (!*eol) {
1833 struct strbuf packed_entry = STRBUF_INIT;
1834 struct fsck_ref_report report = { 0 };
1835
1836 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
1837 report.path = packed_entry.buf;
1838 ret = fsck_report_ref(o, &report,
1839 FSCK_MSG_PACKED_REF_ENTRY_NOT_TERMINATED,
1840 "'%.*s' is not terminated with a newline",
1841 (int)(eof - start), start);
1842
1843 /*
1844 * There is no newline but we still want to parse it to the end of
1845 * the buffer.
1846 */
1847 *eol = eof;
1848 strbuf_release(&packed_entry);
1849 }
1850
1851 return ret;
1852 }
1853
1854 static int packed_fsck_ref_header(struct fsck_options *o,
1855 const char *start, const char *eol,
1856 unsigned int *sorted)
1857 {
1858 struct string_list traits = STRING_LIST_INIT_NODUP;
1859 char *tmp_line;
1860 int ret = 0;
1861 char *p;
1862
1863 tmp_line = xmemdupz(start, eol - start);
1864 if (!skip_prefix(tmp_line, "# pack-refs with: ", (const char **)&p)) {
1865 struct fsck_ref_report report = { 0 };
1866 report.path = "packed-refs.header";
1867
1868 ret = fsck_report_ref(o, &report,
1869 FSCK_MSG_BAD_PACKED_REF_HEADER,
1870 "'%.*s' does not start with '# pack-refs with: '",
1871 (int)(eol - start), start);
1872 goto cleanup;
1873 }
1874
1875 string_list_split_in_place(&traits, p, " ", -1);
1876 *sorted = unsorted_string_list_has_string(&traits, "sorted");
1877
1878 cleanup:
1879 free(tmp_line);
1880 string_list_clear(&traits, 0);
1881 return ret;
1882 }
1883
1884 static int packed_fsck_ref_peeled_line(struct fsck_options *o,
1885 struct ref_store *ref_store,
1886 unsigned long line_number,
1887 const char *start, const char *eol)
1888 {
1889 struct strbuf packed_entry = STRBUF_INIT;
1890 struct fsck_ref_report report = { 0 };
1891 struct object_id peeled;
1892 const char *p;
1893 int ret = 0;
1894
1895 /*
1896 * Skip the '^' and parse the peeled oid.
1897 */
1898 start++;
1899 if (parse_oid_hex_algop(start, &peeled, &p, ref_store->repo->hash_algo)) {
1900 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
1901 report.path = packed_entry.buf;
1902
1903 ret = fsck_report_ref(o, &report,
1904 FSCK_MSG_BAD_PACKED_REF_ENTRY,
1905 "'%.*s' has invalid peeled oid",
1906 (int)(eol - start), start);
1907 goto cleanup;
1908 }
1909
1910 if (p != eol) {
1911 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
1912 report.path = packed_entry.buf;
1913
1914 ret = fsck_report_ref(o, &report,
1915 FSCK_MSG_BAD_PACKED_REF_ENTRY,
1916 "has trailing garbage after peeled oid '%.*s'",
1917 (int)(eol - p), p);
1918 goto cleanup;
1919 }
1920
1921 cleanup:
1922 strbuf_release(&packed_entry);
1923 return ret;
1924 }
1925
1926 static int packed_fsck_ref_main_line(struct fsck_options *o,
1927 struct ref_store *ref_store,
1928 unsigned long line_number,
1929 struct strbuf *refname,
1930 const char *start, const char *eol)
1931 {
1932 struct strbuf packed_entry = STRBUF_INIT;
1933 struct fsck_ref_report report = { 0 };
1934 struct object_id oid;
1935 const char *p;
1936 int ret = 0;
1937
1938 if (parse_oid_hex_algop(start, &oid, &p, ref_store->repo->hash_algo)) {
1939 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
1940 report.path = packed_entry.buf;
1941
1942 ret = fsck_report_ref(o, &report,
1943 FSCK_MSG_BAD_PACKED_REF_ENTRY,
1944 "'%.*s' has invalid oid",
1945 (int)(eol - start), start);
1946 goto cleanup;
1947 }
1948
1949 if (p == eol || !isspace(*p)) {
1950 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
1951 report.path = packed_entry.buf;
1952
1953 ret = fsck_report_ref(o, &report,
1954 FSCK_MSG_BAD_PACKED_REF_ENTRY,
1955 "has no space after oid '%s' but with '%.*s'",
1956 oid_to_hex(&oid), (int)(eol - p), p);
1957 goto cleanup;
1958 }
1959
1960 p++;
1961 strbuf_reset(refname);
1962 strbuf_add(refname, p, eol - p);
1963 if (refname_contains_nul(refname)) {
1964 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
1965 report.path = packed_entry.buf;
1966
1967 ret = fsck_report_ref(o, &report,
1968 FSCK_MSG_BAD_PACKED_REF_ENTRY,
1969 "refname '%s' contains NULL binaries",
1970 refname->buf);
1971 }
1972
1973 if (check_refname_format(refname->buf, 0)) {
1974 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
1975 report.path = packed_entry.buf;
1976
1977 ret = fsck_report_ref(o, &report,
1978 FSCK_MSG_BAD_REF_NAME,
1979 "has bad refname '%s'", refname->buf);
1980 }
1981
1982 cleanup:
1983 strbuf_release(&packed_entry);
1984 return ret;
1985 }
1986
1987 static int packed_fsck_ref_sorted(struct fsck_options *o,
1988 struct ref_store *ref_store,
1989 const char *start, const char *eof)
1990 {
1991 size_t hexsz = ref_store->repo->hash_algo->hexsz;
1992 struct strbuf packed_entry = STRBUF_INIT;
1993 struct fsck_ref_report report = { 0 };
1994 struct strbuf refname1 = STRBUF_INIT;
1995 struct strbuf refname2 = STRBUF_INIT;
1996 unsigned long line_number = 1;
1997 const char *former = NULL;
1998 const char *current;
1999 const char *eol;
2000 int ret = 0;
2001
2002 if (*start == '#') {
2003 eol = memchr(start, '\n', eof - start);
2004 start = eol + 1;
2005 line_number++;
2006 }
2007
2008 for (; start < eof; line_number++, start = eol + 1) {
2009 eol = memchr(start, '\n', eof - start);
2010
2011 if (*start == '^')
2012 continue;
2013
2014 if (!former) {
2015 former = start + hexsz + 1;
2016 continue;
2017 }
2018
2019 current = start + hexsz + 1;
2020 if (cmp_packed_refname(former, current) >= 0) {
2021 const char *err_fmt =
2022 "refname '%s' is less than previous refname '%s'";
2023
2024 eol = memchr(former, '\n', eof - former);
2025 strbuf_add(&refname1, former, eol - former);
2026 eol = memchr(current, '\n', eof - current);
2027 strbuf_add(&refname2, current, eol - current);
2028
2029 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number);
2030 report.path = packed_entry.buf;
2031 ret = fsck_report_ref(o, &report,
2032 FSCK_MSG_PACKED_REF_UNSORTED,
2033 err_fmt, refname2.buf, refname1.buf);
2034 goto cleanup;
2035 }
2036 former = current;
2037 }
2038
2039 cleanup:
2040 strbuf_release(&packed_entry);
2041 strbuf_release(&refname1);
2042 strbuf_release(&refname2);
2043 return ret;
2044 }
2045
2046 static int packed_fsck_ref_content(struct fsck_options *o,
2047 struct ref_store *ref_store,
2048 unsigned int *sorted,
2049 const char *start, const char *eof)
2050 {
2051 struct strbuf refname = STRBUF_INIT;
2052 unsigned long line_number = 1;
2053 const char *eol;
2054 int ret = 0;
2055
2056 ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol);
2057 if (*start == '#') {
2058 ret |= packed_fsck_ref_header(o, start, eol, sorted);
2059
2060 start = eol + 1;
2061 line_number++;
2062 }
2063
2064 while (start < eof) {
2065 ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol);
2066 ret |= packed_fsck_ref_main_line(o, ref_store, line_number, &refname, start, eol);
2067 start = eol + 1;
2068 line_number++;
2069 if (start < eof && *start == '^') {
2070 ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol);
2071 ret |= packed_fsck_ref_peeled_line(o, ref_store, line_number,
2072 start, eol);
2073 start = eol + 1;
2074 line_number++;
2075 }
2076 }
2077
2078 strbuf_release(&refname);
2079 return ret;
2080 }
2081
2082 static int packed_fsck(struct ref_store *ref_store,
2083 struct fsck_options *o,
2084 struct worktree *wt)
2085 {
2086 struct packed_ref_store *refs = packed_downcast(ref_store,
2087 REF_STORE_READ, "fsck");
2088 struct snapshot snapshot = { 0 };
2089 unsigned int sorted = 0;
2090 struct stat st;
2091 int ret = 0;
2092 int fd = -1;
2093
2094 if (!is_main_worktree(wt))
2095 goto cleanup;
2096
2097 if (o->verbose)
2098 fprintf_ln(stderr, "Checking packed-refs file %s", refs->path);
2099
2100 fd = open_nofollow(refs->path, O_RDONLY);
2101 if (fd < 0) {
2102 /*
2103 * If the packed-refs file doesn't exist, there's nothing
2104 * to check.
2105 */
2106 if (errno == ENOENT)
2107 goto cleanup;
2108
2109 if (errno == ELOOP) {
2110 struct fsck_ref_report report = { 0 };
2111 report.path = "packed-refs";
2112 ret = fsck_report_ref(o, &report,
2113 FSCK_MSG_BAD_REF_FILETYPE,
2114 "not a regular file but a symlink");
2115 goto cleanup;
2116 }
2117
2118 ret = error_errno(_("unable to open '%s'"), refs->path);
2119 goto cleanup;
2120 } else if (fstat(fd, &st) < 0) {
2121 ret = error_errno(_("unable to stat '%s'"), refs->path);
2122 goto cleanup;
2123 } else if (!S_ISREG(st.st_mode)) {
2124 struct fsck_ref_report report = { 0 };
2125 report.path = "packed-refs";
2126 ret = fsck_report_ref(o, &report,
2127 FSCK_MSG_BAD_REF_FILETYPE,
2128 "not a regular file");
2129 goto cleanup;
2130 }
2131
2132 if (!allocate_snapshot_buffer(&snapshot, fd, &st)) {
2133 struct fsck_ref_report report = { 0 };
2134 report.path = "packed-refs";
2135 ret = fsck_report_ref(o, &report,
2136 FSCK_MSG_EMPTY_PACKED_REFS_FILE,
2137 "file is empty");
2138 goto cleanup;
2139 }
2140
2141 ret = packed_fsck_ref_content(o, ref_store, &sorted, snapshot.start,
2142 snapshot.eof);
2143 if (!ret && sorted)
2144 ret = packed_fsck_ref_sorted(o, ref_store, snapshot.start,
2145 snapshot.eof);
2146
2147 cleanup:
2148 if (fd >= 0)
2149 close(fd);
2150 clear_snapshot_buffer(&snapshot);
2151 return ret;
2152 }
2153
2154 struct ref_storage_be refs_be_packed = {
2155 .name = "packed",
2156 .init = packed_ref_store_init,
2157 .release = packed_ref_store_release,
2158 .create_on_disk = packed_ref_store_create_on_disk,
2159 .remove_on_disk = packed_ref_store_remove_on_disk,
2160
2161 .transaction_prepare = packed_transaction_prepare,
2162 .transaction_finish = packed_transaction_finish,
2163 .transaction_abort = packed_transaction_abort,
2164
2165 .optimize = packed_optimize,
2166 .optimize_required = packed_optimize_required,
2167
2168 .rename_ref = NULL,
2169 .copy_ref = NULL,
2170
2171 .iterator_begin = packed_ref_iterator_begin,
2172 .read_raw_ref = packed_read_raw_ref,
2173 .read_symbolic_ref = NULL,
2174
2175 .reflog_iterator_begin = packed_reflog_iterator_begin,
2176 .for_each_reflog_ent = NULL,
2177 .for_each_reflog_ent_reverse = NULL,
2178 .reflog_exists = NULL,
2179 .create_reflog = NULL,
2180 .delete_reflog = NULL,
2181 .reflog_expire = NULL,
2182
2183 .fsck = packed_fsck,
2184 };