]> git.ipfire.org Git - thirdparty/git.git/blame - refs/packed-backend.c
Start the 2.46 cycle
[thirdparty/git.git] / refs / packed-backend.c
CommitLineData
bc5c5ec0 1#include "../git-compat-util.h"
44c2339e 2#include "../config.h"
f394e093 3#include "../gettext.h"
d1cbe1e6 4#include "../hash.h"
41771fa4 5#include "../hex.h"
67be7c5a
MH
6#include "../refs.h"
7#include "refs-internal.h"
67be7c5a
MH
8#include "packed-backend.h"
9#include "../iterator.h"
10#include "../lockfile.h"
fb9c2d27 11#include "../chdir-notify.h"
90cbae9c 12#include "../statinfo.h"
65156bb7 13#include "../wrapper.h"
d48be35c 14#include "../write-or-die.h"
c489f47a 15#include "../trace2.h"
67be7c5a 16
5b633610
MH
17enum mmap_strategy {
18 /*
19 * Don't use mmap() at all for reading `packed-refs`.
20 */
21 MMAP_NONE,
67be7c5a
MH
22
23 /*
5b633610
MH
24 * Can use mmap() for reading `packed-refs`, but the file must
25 * not remain mmapped. This is the usual option on Windows,
26 * where you cannot rename a new version of a file onto a file
27 * that is currently mmapped.
67be7c5a 28 */
5b633610 29 MMAP_TEMPORARY,
67be7c5a 30
5b633610
MH
31 /*
32 * It is OK to leave the `packed-refs` file mmapped while
33 * arbitrary other code is running.
34 */
35 MMAP_OK
67be7c5a
MH
36};
37
5b633610
MH
38#if defined(NO_MMAP)
39static enum mmap_strategy mmap_strategy = MMAP_NONE;
40#elif defined(MMAP_PREVENTS_DELETE)
41static enum mmap_strategy mmap_strategy = MMAP_TEMPORARY;
42#else
43static enum mmap_strategy mmap_strategy = MMAP_OK;
44#endif
45
f0a7dc86 46struct packed_ref_store;
67be7c5a
MH
47
48/*
cff28ca9
MH
49 * A `snapshot` represents one snapshot of a `packed-refs` file.
50 *
51 * Normally, this will be a mmapped view of the contents of the
52 * `packed-refs` file at the time the snapshot was created. However,
53 * if the `packed-refs` file was not sorted, this might point at heap
54 * memory holding the contents of the `packed-refs` file with its
55 * records sorted by refname.
56 *
57 * `snapshot` instances are reference counted (via
58 * `acquire_snapshot()` and `release_snapshot()`). This is to prevent
59 * an instance from disappearing while an iterator is still iterating
60 * over it. Instances are garbage collected when their `referrers`
61 * count goes to zero.
62 *
63 * The most recent `snapshot`, if available, is referenced by the
64 * `packed_ref_store`. Its freshness is checked whenever
65 * `get_snapshot()` is called; if the existing snapshot is obsolete, a
66 * new snapshot is taken.
67be7c5a 67 */
cff28ca9 68struct snapshot {
f0a7dc86
MH
69 /*
70 * A back-pointer to the packed_ref_store with which this
cff28ca9 71 * snapshot is associated:
f0a7dc86
MH
72 */
73 struct packed_ref_store *refs;
74
5b633610
MH
75 /* Is the `packed-refs` file currently mmapped? */
76 int mmapped;
77
78 /*
4a2854f7
MH
79 * The contents of the `packed-refs` file:
80 *
81 * - buf -- a pointer to the start of the memory
82 * - start -- a pointer to the first byte of actual references
83 * (i.e., after the header line, if one is present)
84 * - eof -- a pointer just past the end of the reference
85 * contents
86 *
87 * If the `packed-refs` file was already sorted, `buf` points
88 * at the mmapped contents of the file. If not, it points at
89 * heap-allocated memory containing the contents, sorted. If
90 * there were no contents (e.g., because the file didn't
91 * exist), `buf`, `start`, and `eof` are all NULL.
5b633610 92 */
4a2854f7 93 char *buf, *start, *eof;
5b633610 94
daa45408 95 /*
cff28ca9
MH
96 * What is the peeled state of the `packed-refs` file that
97 * this snapshot represents? (This is usually determined from
98 * the file's header.)
daa45408
MH
99 */
100 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled;
101
67be7c5a 102 /*
cff28ca9
MH
103 * Count of references to this instance, including the pointer
104 * from `packed_ref_store::snapshot`, if any. The instance
105 * will not be freed as long as the reference count is
106 * nonzero.
67be7c5a
MH
107 */
108 unsigned int referrers;
109
cff28ca9
MH
110 /*
111 * The metadata of the `packed-refs` file from which this
112 * snapshot was created, used to tell if the file has been
113 * replaced since we read it.
114 */
67be7c5a
MH
115 struct stat_validity validity;
116};
67be7c5a
MH
117
118/*
cff28ca9
MH
119 * A `ref_store` representing references stored in a `packed-refs`
120 * file. It implements the `ref_store` interface, though it has some
121 * limitations:
122 *
123 * - It cannot store symbolic references.
124 *
125 * - It cannot store reflogs.
126 *
127 * - It does not support reference renaming (though it could).
128 *
129 * On the other hand, it can be locked outside of a reference
130 * transaction. In that case, it remains locked even after the
131 * transaction is done and the new `packed-refs` file is activated.
67be7c5a
MH
132 */
133struct packed_ref_store {
e0cc8ac8
MH
134 struct ref_store base;
135
67be7c5a
MH
136 unsigned int store_flags;
137
138 /* The path of the "packed-refs" file: */
139 char *path;
140
141 /*
cff28ca9
MH
142 * A snapshot of the values read from the `packed-refs` file,
143 * if it might still be current; otherwise, NULL.
67be7c5a 144 */
cff28ca9 145 struct snapshot *snapshot;
67be7c5a
MH
146
147 /*
148 * Lock used for the "packed-refs" file. Note that this (and
149 * thus the enclosing `packed_ref_store`) must not be freed.
150 */
151 struct lock_file lock;
42dfa7ec
MH
152
153 /*
154 * Temporary file used when rewriting new contents to the
155 * "packed-refs" file. Note that this (and thus the enclosing
156 * `packed_ref_store`) must not be freed.
157 */
076aa2cb 158 struct tempfile *tempfile;
67be7c5a
MH
159};
160
14b3c344 161/*
cff28ca9 162 * Increment the reference count of `*snapshot`.
14b3c344 163 */
cff28ca9 164static void acquire_snapshot(struct snapshot *snapshot)
14b3c344 165{
cff28ca9 166 snapshot->referrers++;
14b3c344
MH
167}
168
5b633610 169/*
cff28ca9 170 * If the buffer in `snapshot` is active, then either munmap the
5b633610
MH
171 * memory and close the file, or free the memory. Then set the buffer
172 * pointers to NULL.
173 */
cff28ca9 174static void clear_snapshot_buffer(struct snapshot *snapshot)
5b633610 175{
cff28ca9
MH
176 if (snapshot->mmapped) {
177 if (munmap(snapshot->buf, snapshot->eof - snapshot->buf))
5b633610 178 die_errno("error ummapping packed-refs file %s",
cff28ca9
MH
179 snapshot->refs->path);
180 snapshot->mmapped = 0;
5b633610 181 } else {
cff28ca9 182 free(snapshot->buf);
5b633610 183 }
4a2854f7 184 snapshot->buf = snapshot->start = snapshot->eof = NULL;
5b633610
MH
185}
186
14b3c344 187/*
cff28ca9
MH
188 * Decrease the reference count of `*snapshot`. If it goes to zero,
189 * free `*snapshot` and return true; otherwise return false.
14b3c344 190 */
cff28ca9 191static int release_snapshot(struct snapshot *snapshot)
14b3c344 192{
cff28ca9
MH
193 if (!--snapshot->referrers) {
194 stat_validity_clear(&snapshot->validity);
195 clear_snapshot_buffer(snapshot);
196 free(snapshot);
14b3c344
MH
197 return 1;
198 } else {
199 return 0;
200 }
201}
202
34224e14 203struct ref_store *packed_ref_store_create(struct repository *repo,
99f0d97b 204 const char *gitdir,
e0cc8ac8 205 unsigned int store_flags)
67be7c5a
MH
206{
207 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
e0cc8ac8 208 struct ref_store *ref_store = (struct ref_store *)refs;
99f0d97b 209 struct strbuf sb = STRBUF_INIT;
67be7c5a 210
f9f7fd3b 211 base_ref_store_init(ref_store, repo, gitdir, &refs_be_packed);
67be7c5a 212 refs->store_flags = store_flags;
f9f7fd3b 213
99f0d97b
HWN
214 strbuf_addf(&sb, "%s/packed-refs", gitdir);
215 refs->path = strbuf_detach(&sb, NULL);
fb9c2d27 216 chdir_notify_reparent("packed-refs", &refs->path);
e0cc8ac8 217 return ref_store;
67be7c5a
MH
218}
219
e0cc8ac8
MH
220/*
221 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
222 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
223 * support at least the flags specified in `required_flags`. `caller`
224 * is used in any necessary error messages.
225 */
226static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
227 unsigned int required_flags,
228 const char *caller)
229{
230 struct packed_ref_store *refs;
231
232 if (ref_store->be != &refs_be_packed)
033abf97 233 BUG("ref_store is type \"%s\" not \"packed\" in %s",
e0cc8ac8
MH
234 ref_store->be->name, caller);
235
236 refs = (struct packed_ref_store *)ref_store;
237
238 if ((refs->store_flags & required_flags) != required_flags)
033abf97 239 BUG("unallowed operation (%s), requires %x, has %x\n",
e0cc8ac8
MH
240 caller, required_flags, refs->store_flags);
241
242 return refs;
243}
244
cff28ca9 245static void clear_snapshot(struct packed_ref_store *refs)
67be7c5a 246{
cff28ca9
MH
247 if (refs->snapshot) {
248 struct snapshot *snapshot = refs->snapshot;
67be7c5a 249
cff28ca9
MH
250 refs->snapshot = NULL;
251 release_snapshot(snapshot);
67be7c5a
MH
252 }
253}
254
735267aa
MH
255static NORETURN void die_unterminated_line(const char *path,
256 const char *p, size_t len)
67be7c5a 257{
735267aa
MH
258 if (len < 80)
259 die("unterminated line in %s: %.*s", path, (int)len, p);
260 else
261 die("unterminated line in %s: %.75s...", path, p);
262}
67be7c5a 263
735267aa
MH
264static NORETURN void die_invalid_line(const char *path,
265 const char *p, size_t len)
266{
267 const char *eol = memchr(p, '\n', len);
268
269 if (!eol)
270 die_unterminated_line(path, p, len);
271 else if (eol - p < 80)
272 die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
273 else
274 die("unexpected line in %s: %.75s...", path, p);
275
276}
277
cff28ca9 278struct snapshot_record {
02b920f3
MH
279 const char *start;
280 size_t len;
281};
282
cff28ca9 283static int cmp_packed_ref_records(const void *v1, const void *v2)
02b920f3 284{
cff28ca9 285 const struct snapshot_record *e1 = v1, *e2 = v2;
49d16608 286 const char *r1 = e1->start + the_hash_algo->hexsz + 1;
287 const char *r2 = e2->start + the_hash_algo->hexsz + 1;
02b920f3
MH
288
289 while (1) {
290 if (*r1 == '\n')
291 return *r2 == '\n' ? 0 : -1;
292 if (*r1 != *r2) {
293 if (*r2 == '\n')
294 return 1;
295 else
296 return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1;
297 }
298 r1++;
299 r2++;
67be7c5a
MH
300 }
301}
302
d1cf1551 303/*
cff28ca9
MH
304 * Compare a snapshot record at `rec` to the specified NUL-terminated
305 * refname.
d1cf1551 306 */
59c35fac
TB
307static int cmp_record_to_refname(const char *rec, const char *refname,
308 int start)
d1cf1551 309{
49d16608 310 const char *r1 = rec + the_hash_algo->hexsz + 1;
d1cf1551
MH
311 const char *r2 = refname;
312
313 while (1) {
314 if (*r1 == '\n')
315 return *r2 ? -1 : 0;
316 if (!*r2)
59c35fac 317 return start ? 1 : -1;
d1cf1551
MH
318 if (*r1 != *r2)
319 return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1;
320 r1++;
321 r2++;
322 }
323}
67be7c5a
MH
324
325/*
cff28ca9
MH
326 * `snapshot->buf` is not known to be sorted. Check whether it is, and
327 * if not, sort it into new memory and munmap/free the old storage.
67be7c5a 328 */
cff28ca9 329static void sort_snapshot(struct snapshot *snapshot)
67be7c5a 330{
cff28ca9 331 struct snapshot_record *records = NULL;
02b920f3
MH
332 size_t alloc = 0, nr = 0;
333 int sorted = 1;
334 const char *pos, *eof, *eol;
335 size_t len, i;
336 char *new_buffer, *dst;
67be7c5a 337
4a2854f7 338 pos = snapshot->start;
cff28ca9 339 eof = snapshot->eof;
67be7c5a 340
4a2854f7 341 if (pos == eof)
02b920f3 342 return;
67be7c5a 343
4a2854f7
MH
344 len = eof - pos;
345
02b920f3 346 /*
cff28ca9 347 * Initialize records based on a crude estimate of the number
02b920f3
MH
348 * of references in the file (we'll grow it below if needed):
349 */
cff28ca9 350 ALLOC_GROW(records, len / 80 + 20, alloc);
02b920f3
MH
351
352 while (pos < eof) {
353 eol = memchr(pos, '\n', eof - pos);
354 if (!eol)
355 /* The safety check should prevent this. */
356 BUG("unterminated line found in packed-refs");
49d16608 357 if (eol - pos < the_hash_algo->hexsz + 2)
cff28ca9 358 die_invalid_line(snapshot->refs->path,
02b920f3
MH
359 pos, eof - pos);
360 eol++;
361 if (eol < eof && *eol == '^') {
362 /*
363 * Keep any peeled line together with its
364 * reference:
365 */
366 const char *peeled_start = eol;
367
368 eol = memchr(peeled_start, '\n', eof - peeled_start);
369 if (!eol)
370 /* The safety check should prevent this. */
371 BUG("unterminated peeled line found in packed-refs");
372 eol++;
373 }
374
cff28ca9
MH
375 ALLOC_GROW(records, nr + 1, alloc);
376 records[nr].start = pos;
377 records[nr].len = eol - pos;
02b920f3
MH
378 nr++;
379
380 if (sorted &&
381 nr > 1 &&
cff28ca9
MH
382 cmp_packed_ref_records(&records[nr - 2],
383 &records[nr - 1]) >= 0)
02b920f3 384 sorted = 0;
67be7c5a 385
02b920f3
MH
386 pos = eol;
387 }
388
389 if (sorted)
390 goto cleanup;
391
cff28ca9
MH
392 /* We need to sort the memory. First we sort the records array: */
393 QSORT(records, nr, cmp_packed_ref_records);
02b920f3
MH
394
395 /*
396 * Allocate a new chunk of memory, and copy the old memory to
cff28ca9 397 * the new in the order indicated by `records` (not bothering
02b920f3
MH
398 * with the header line):
399 */
400 new_buffer = xmalloc(len);
401 for (dst = new_buffer, i = 0; i < nr; i++) {
cff28ca9
MH
402 memcpy(dst, records[i].start, records[i].len);
403 dst += records[i].len;
02b920f3
MH
404 }
405
406 /*
407 * Now munmap the old buffer and use the sorted buffer in its
408 * place:
409 */
cff28ca9 410 clear_snapshot_buffer(snapshot);
4a2854f7 411 snapshot->buf = snapshot->start = new_buffer;
cff28ca9 412 snapshot->eof = new_buffer + len;
02b920f3
MH
413
414cleanup:
cff28ca9 415 free(records);
67be7c5a
MH
416}
417
418/*
02b920f3
MH
419 * Return a pointer to the start of the record that contains the
420 * character `*p` (which must be within the buffer). If no other
421 * record start is found, return `buf`.
422 */
423static const char *find_start_of_record(const char *buf, const char *p)
424{
425 while (p > buf && (p[-1] != '\n' || p[0] == '^'))
426 p--;
427 return p;
428}
429
d1cf1551
MH
430/*
431 * Return a pointer to the start of the record following the record
432 * that contains `*p`. If none is found before `end`, return `end`.
433 */
434static const char *find_end_of_record(const char *p, const char *end)
435{
436 while (++p < end && (p[-1] != '\n' || p[0] == '^'))
437 ;
438 return p;
439}
440
02b920f3
MH
441/*
442 * We want to be able to compare mmapped reference records quickly,
443 * without totally parsing them. We can do so because the records are
444 * LF-terminated, and the refname should start exactly (GIT_SHA1_HEXSZ
445 * + 1) bytes past the beginning of the record.
446 *
447 * But what if the `packed-refs` file contains garbage? We're willing
448 * to tolerate not detecting the problem, as long as we don't produce
449 * totally garbled output (we can't afford to check the integrity of
450 * the whole file during every Git invocation). But we do want to be
451 * sure that we never read past the end of the buffer in memory and
452 * perform an illegal memory access.
453 *
454 * Guarantee that minimum level of safety by verifying that the last
455 * record in the file is LF-terminated, and that it has at least
456 * (GIT_SHA1_HEXSZ + 1) characters before the LF. Die if either of
457 * these checks fails.
458 */
cff28ca9 459static void verify_buffer_safe(struct snapshot *snapshot)
02b920f3 460{
4a2854f7 461 const char *start = snapshot->start;
cff28ca9 462 const char *eof = snapshot->eof;
02b920f3
MH
463 const char *last_line;
464
4a2854f7 465 if (start == eof)
02b920f3
MH
466 return;
467
4a2854f7 468 last_line = find_start_of_record(start, eof - 1);
49d16608 469 if (*(eof - 1) != '\n' || eof - last_line < the_hash_algo->hexsz + 2)
cff28ca9 470 die_invalid_line(snapshot->refs->path,
02b920f3
MH
471 last_line, eof - last_line);
472}
473
ba41a8b6
KG
474#define SMALL_FILE_SIZE (32*1024)
475
5b633610
MH
476/*
477 * Depending on `mmap_strategy`, either mmap or read the contents of
cff28ca9 478 * the `packed-refs` file into the snapshot. Return 1 if the file
01caf20d
MH
479 * existed and was read, or 0 if the file was absent or empty. Die on
480 * errors.
5b633610 481 */
cff28ca9 482static int load_contents(struct snapshot *snapshot)
5b633610
MH
483{
484 int fd;
485 struct stat st;
486 size_t size;
487 ssize_t bytes_read;
488
cff28ca9 489 fd = open(snapshot->refs->path, O_RDONLY);
5b633610
MH
490 if (fd < 0) {
491 if (errno == ENOENT) {
492 /*
493 * This is OK; it just means that no
494 * "packed-refs" file has been written yet,
495 * which is equivalent to it being empty,
496 * which is its state when initialized with
497 * zeros.
498 */
499 return 0;
500 } else {
cff28ca9 501 die_errno("couldn't read %s", snapshot->refs->path);
5b633610
MH
502 }
503 }
504
cff28ca9 505 stat_validity_update(&snapshot->validity, fd);
5b633610
MH
506
507 if (fstat(fd, &st) < 0)
cff28ca9 508 die_errno("couldn't stat %s", snapshot->refs->path);
5b633610
MH
509 size = xsize_t(st.st_size);
510
01caf20d 511 if (!size) {
09427e83 512 close(fd);
01caf20d 513 return 0;
ba41a8b6 514 } else if (mmap_strategy == MMAP_NONE || size <= SMALL_FILE_SIZE) {
cff28ca9
MH
515 snapshot->buf = xmalloc(size);
516 bytes_read = read_in_full(fd, snapshot->buf, size);
5b633610 517 if (bytes_read < 0 || bytes_read != size)
cff28ca9 518 die_errno("couldn't read %s", snapshot->refs->path);
cff28ca9 519 snapshot->mmapped = 0;
01caf20d 520 } else {
cff28ca9 521 snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
cff28ca9 522 snapshot->mmapped = 1;
5b633610
MH
523 }
524 close(fd);
525
4a2854f7
MH
526 snapshot->start = snapshot->buf;
527 snapshot->eof = snapshot->buf + size;
528
5b633610
MH
529 return 1;
530}
531
d22d941a 532static const char *find_reference_location_1(struct snapshot *snapshot,
59c35fac
TB
533 const char *refname, int mustexist,
534 int start)
d1cf1551
MH
535{
536 /*
537 * This is not *quite* a garden-variety binary search, because
538 * the data we're searching is made up of records, and we
539 * always need to find the beginning of a record to do a
540 * comparison. A "record" here is one line for the reference
541 * itself and zero or one peel lines that start with '^'. Our
542 * loop invariant is described in the next two comments.
543 */
544
545 /*
546 * A pointer to the character at the start of a record whose
547 * preceding records all have reference names that come
548 * *before* `refname`.
549 */
4a2854f7 550 const char *lo = snapshot->start;
d1cf1551
MH
551
552 /*
553 * A pointer to a the first character of a record whose
554 * reference name comes *after* `refname`.
555 */
cff28ca9 556 const char *hi = snapshot->eof;
d1cf1551 557
4a14f8d0 558 while (lo != hi) {
d1cf1551
MH
559 const char *mid, *rec;
560 int cmp;
561
562 mid = lo + (hi - lo) / 2;
563 rec = find_start_of_record(lo, mid);
59c35fac 564 cmp = cmp_record_to_refname(rec, refname, start);
d1cf1551
MH
565 if (cmp < 0) {
566 lo = find_end_of_record(mid, hi);
567 } else if (cmp > 0) {
568 hi = rec;
569 } else {
570 return rec;
571 }
572 }
573
574 if (mustexist)
575 return NULL;
576 else
577 return lo;
578}
579
d22d941a
TB
580/*
581 * Find the place in `snapshot->buf` where the start of the record for
582 * `refname` starts. If `mustexist` is true and the reference doesn't
583 * exist, then return NULL. If `mustexist` is false and the reference
584 * doesn't exist, then return the point where that reference would be
585 * inserted, or `snapshot->eof` (which might be NULL) if it would be
586 * inserted at the end of the file. In the latter mode, `refname`
587 * doesn't have to be a proper reference name; for example, one could
588 * search for "refs/replace/" to find the start of any replace
589 * references.
590 *
591 * The record is sought using a binary search, so `snapshot->buf` must
592 * be sorted.
593 */
594static const char *find_reference_location(struct snapshot *snapshot,
595 const char *refname, int mustexist)
596{
59c35fac
TB
597 return find_reference_location_1(snapshot, refname, mustexist, 1);
598}
599
600/*
601 * Find the place in `snapshot->buf` after the end of the record for
602 * `refname`. In other words, find the location of first thing *after*
603 * `refname`.
604 *
605 * Other semantics are identical to the ones in
606 * `find_reference_location()`.
607 */
608static const char *find_reference_location_end(struct snapshot *snapshot,
609 const char *refname,
610 int mustexist)
611{
612 return find_reference_location_1(snapshot, refname, mustexist, 0);
d22d941a
TB
613}
614
67be7c5a 615/*
cff28ca9
MH
616 * Create a newly-allocated `snapshot` of the `packed-refs` file in
617 * its current state and return it. The return value will already have
618 * its reference count incremented.
67be7c5a
MH
619 *
620 * A comment line of the form "# pack-refs with: " may contain zero or
621 * more traits. We interpret the traits as follows:
622 *
02b920f3 623 * Neither `peeled` nor `fully-peeled`:
67be7c5a
MH
624 *
625 * Probably no references are peeled. But if the file contains a
626 * peeled value for a reference, we will use it.
627 *
02b920f3 628 * `peeled`:
67be7c5a
MH
629 *
630 * References under "refs/tags/", if they *can* be peeled, *are*
631 * peeled in this file. References outside of "refs/tags/" are
632 * probably not peeled even if they could have been, but if we find
633 * a peeled value for such a reference we will use it.
634 *
02b920f3 635 * `fully-peeled`:
67be7c5a
MH
636 *
637 * All references in the file that can be peeled are peeled.
638 * Inversely (and this is more important), any references in the
639 * file for which no peeled value is recorded is not peelable. This
640 * trait should typically be written alongside "peeled" for
641 * compatibility with older clients, but we do not require it
642 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
02b920f3
MH
643 *
644 * `sorted`:
645 *
646 * The references in this file are known to be sorted by refname.
67be7c5a 647 */
cff28ca9 648static struct snapshot *create_snapshot(struct packed_ref_store *refs)
67be7c5a 649{
cff28ca9 650 struct snapshot *snapshot = xcalloc(1, sizeof(*snapshot));
02b920f3 651 int sorted = 0;
67be7c5a 652
cff28ca9
MH
653 snapshot->refs = refs;
654 acquire_snapshot(snapshot);
655 snapshot->peeled = PEELED_NONE;
67be7c5a 656
cff28ca9
MH
657 if (!load_contents(snapshot))
658 return snapshot;
67be7c5a 659
36f23534 660 /* If the file has a header line, process it: */
cff28ca9 661 if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') {
27a41841 662 char *tmp, *p, *eol;
a8811695 663 struct string_list traits = STRING_LIST_INIT_NODUP;
9308b7f3 664
cff28ca9
MH
665 eol = memchr(snapshot->buf, '\n',
666 snapshot->eof - snapshot->buf);
36f23534 667 if (!eol)
5b633610 668 die_unterminated_line(refs->path,
cff28ca9
MH
669 snapshot->buf,
670 snapshot->eof - snapshot->buf);
67be7c5a 671
27a41841 672 tmp = xmemdupz(snapshot->buf, eol - snapshot->buf);
67be7c5a 673
27a41841 674 if (!skip_prefix(tmp, "# pack-refs with:", (const char **)&p))
5b633610 675 die_invalid_line(refs->path,
cff28ca9
MH
676 snapshot->buf,
677 snapshot->eof - snapshot->buf);
36f23534 678
52acddf3 679 string_list_split_in_place(&traits, p, " ", -1);
a8811695
MH
680
681 if (unsorted_string_list_has_string(&traits, "fully-peeled"))
cff28ca9 682 snapshot->peeled = PEELED_FULLY;
a8811695 683 else if (unsorted_string_list_has_string(&traits, "peeled"))
cff28ca9 684 snapshot->peeled = PEELED_TAGS;
02b920f3
MH
685
686 sorted = unsorted_string_list_has_string(&traits, "sorted");
687
36f23534
MH
688 /* perhaps other traits later as well */
689
690 /* The "+ 1" is for the LF character. */
4a2854f7 691 snapshot->start = eol + 1;
a8811695
MH
692
693 string_list_clear(&traits, 0);
27a41841 694 free(tmp);
67be7c5a
MH
695 }
696
cff28ca9 697 verify_buffer_safe(snapshot);
67be7c5a 698
02b920f3 699 if (!sorted) {
cff28ca9 700 sort_snapshot(snapshot);
02b920f3
MH
701
702 /*
703 * Reordering the records might have moved a short one
704 * to the end of the buffer, so verify the buffer's
705 * safety again:
706 */
cff28ca9 707 verify_buffer_safe(snapshot);
02b920f3
MH
708 }
709
cff28ca9 710 if (mmap_strategy != MMAP_OK && snapshot->mmapped) {
02b920f3
MH
711 /*
712 * We don't want to leave the file mmapped, so we are
713 * forced to make a copy now:
714 */
4a2854f7 715 size_t size = snapshot->eof - snapshot->start;
02b920f3
MH
716 char *buf_copy = xmalloc(size);
717
4a2854f7 718 memcpy(buf_copy, snapshot->start, size);
cff28ca9 719 clear_snapshot_buffer(snapshot);
4a2854f7 720 snapshot->buf = snapshot->start = buf_copy;
cff28ca9 721 snapshot->eof = buf_copy + size;
02b920f3
MH
722 }
723
cff28ca9 724 return snapshot;
67be7c5a
MH
725}
726
727/*
cff28ca9
MH
728 * Check that `refs->snapshot` (if present) still reflects the
729 * contents of the `packed-refs` file. If not, clear the snapshot.
67be7c5a 730 */
cff28ca9 731static void validate_snapshot(struct packed_ref_store *refs)
67be7c5a 732{
cff28ca9
MH
733 if (refs->snapshot &&
734 !stat_validity_check(&refs->snapshot->validity, refs->path))
735 clear_snapshot(refs);
67be7c5a
MH
736}
737
738/*
cff28ca9
MH
739 * Get the `snapshot` for the specified packed_ref_store, creating and
740 * populating it if it hasn't been read before or if the file has been
741 * changed (according to its `validity` field) since it was last read.
742 * On the other hand, if we hold the lock, then assume that the file
743 * hasn't been changed out from under us, so skip the extra `stat()`
744 * call in `stat_validity_check()`. This function does *not* increase
745 * the snapshot's reference count on behalf of the caller.
67be7c5a 746 */
cff28ca9 747static struct snapshot *get_snapshot(struct packed_ref_store *refs)
67be7c5a
MH
748{
749 if (!is_lock_file_locked(&refs->lock))
cff28ca9 750 validate_snapshot(refs);
67be7c5a 751
cff28ca9
MH
752 if (!refs->snapshot)
753 refs->snapshot = create_snapshot(refs);
67be7c5a 754
cff28ca9 755 return refs->snapshot;
67be7c5a
MH
756}
757
5b12e16b 758static int packed_read_raw_ref(struct ref_store *ref_store, const char *refname,
5cf88fd8 759 struct object_id *oid, struct strbuf *referent UNUSED,
5b12e16b 760 unsigned int *type, int *failure_errno)
67be7c5a 761{
e0cc8ac8
MH
762 struct packed_ref_store *refs =
763 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
cff28ca9 764 struct snapshot *snapshot = get_snapshot(refs);
f3987ab3 765 const char *rec;
67be7c5a
MH
766
767 *type = 0;
768
cff28ca9 769 rec = find_reference_location(snapshot, refname, 1);
f3987ab3
MH
770
771 if (!rec) {
772 /* refname is not a packed reference. */
5b12e16b 773 *failure_errno = ENOENT;
67be7c5a
MH
774 return -1;
775 }
776
99afe91a 777 if (get_oid_hex(rec, oid))
cff28ca9 778 die_invalid_line(refs->path, rec, snapshot->eof - rec);
f3987ab3 779
67be7c5a
MH
780 *type = REF_ISPACKED;
781 return 0;
782}
783
523ee2d7
MH
784/*
785 * This value is set in `base.flags` if the peeled value of the
786 * current reference is known. In that case, `peeled` contains the
78fb4579 787 * correct peeled value for the reference, which might be `null_oid`
523ee2d7
MH
788 * if the reference is not a tag or if it is broken.
789 */
790#define REF_KNOWS_PEELED 0x40
67be7c5a 791
523ee2d7 792/*
cff28ca9 793 * An iterator over a snapshot of a `packed-refs` file.
523ee2d7 794 */
67be7c5a
MH
795struct packed_ref_iterator {
796 struct ref_iterator base;
797
cff28ca9 798 struct snapshot *snapshot;
523ee2d7 799
cff28ca9 800 /* The current position in the snapshot's buffer: */
523ee2d7
MH
801 const char *pos;
802
cff28ca9 803 /* The end of the part of the buffer that will be iterated over: */
523ee2d7
MH
804 const char *eof;
805
59c35fac
TB
806 struct jump_list_entry {
807 const char *start;
808 const char *end;
809 } *jump;
810 size_t jump_nr, jump_alloc;
811 size_t jump_cur;
812
cff28ca9 813 /* Scratch space for current values: */
523ee2d7 814 struct object_id oid, peeled;
523ee2d7
MH
815 struct strbuf refname_buf;
816
9bc45a28 817 struct repository *repo;
67be7c5a
MH
818 unsigned int flags;
819};
820
cff28ca9
MH
821/*
822 * Move the iterator to the next record in the snapshot, without
823 * respect for whether the record is actually required by the current
824 * iteration. Adjust the fields in `iter` and return `ITER_OK` or
825 * `ITER_DONE`. This function does not free the iterator in the case
826 * of `ITER_DONE`.
827 */
523ee2d7
MH
828static int next_record(struct packed_ref_iterator *iter)
829{
59c35fac 830 const char *p, *eol;
523ee2d7
MH
831
832 strbuf_reset(&iter->refname_buf);
833
59c35fac
TB
834 /*
835 * If iter->pos is contained within a skipped region, jump past
836 * it.
837 *
838 * Note that each skipped region is considered at most once,
839 * since they are ordered based on their starting position.
840 */
841 while (iter->jump_cur < iter->jump_nr) {
842 struct jump_list_entry *curr = &iter->jump[iter->jump_cur];
843 if (iter->pos < curr->start)
844 break; /* not to the next jump yet */
845
846 iter->jump_cur++;
847 if (iter->pos < curr->end) {
848 iter->pos = curr->end;
c489f47a 849 trace2_counter_add(TRACE2_COUNTER_ID_PACKED_REFS_JUMPS, 1);
59c35fac
TB
850 /* jumps are coalesced, so only one jump is necessary */
851 break;
852 }
853 }
854
523ee2d7
MH
855 if (iter->pos == iter->eof)
856 return ITER_DONE;
857
858 iter->base.flags = REF_ISPACKED;
59c35fac 859 p = iter->pos;
523ee2d7 860
49d16608 861 if (iter->eof - p < the_hash_algo->hexsz + 2 ||
523ee2d7
MH
862 parse_oid_hex(p, &iter->oid, &p) ||
863 !isspace(*p++))
cff28ca9 864 die_invalid_line(iter->snapshot->refs->path,
523ee2d7
MH
865 iter->pos, iter->eof - iter->pos);
866
867 eol = memchr(p, '\n', iter->eof - p);
868 if (!eol)
cff28ca9 869 die_unterminated_line(iter->snapshot->refs->path,
523ee2d7
MH
870 iter->pos, iter->eof - iter->pos);
871
872 strbuf_add(&iter->refname_buf, p, eol - p);
873 iter->base.refname = iter->refname_buf.buf;
874
875 if (check_refname_format(iter->base.refname, REFNAME_ALLOW_ONELEVEL)) {
876 if (!refname_is_safe(iter->base.refname))
877 die("packed refname is dangerous: %s",
878 iter->base.refname);
879 oidclr(&iter->oid);
880 iter->base.flags |= REF_BAD_NAME | REF_ISBROKEN;
881 }
cff28ca9
MH
882 if (iter->snapshot->peeled == PEELED_FULLY ||
883 (iter->snapshot->peeled == PEELED_TAGS &&
523ee2d7
MH
884 starts_with(iter->base.refname, "refs/tags/")))
885 iter->base.flags |= REF_KNOWS_PEELED;
886
887 iter->pos = eol + 1;
888
889 if (iter->pos < iter->eof && *iter->pos == '^') {
890 p = iter->pos + 1;
49d16608 891 if (iter->eof - p < the_hash_algo->hexsz + 1 ||
523ee2d7
MH
892 parse_oid_hex(p, &iter->peeled, &p) ||
893 *p++ != '\n')
cff28ca9 894 die_invalid_line(iter->snapshot->refs->path,
523ee2d7
MH
895 iter->pos, iter->eof - iter->pos);
896 iter->pos = p;
897
898 /*
899 * Regardless of what the file header said, we
900 * definitely know the value of *this* reference. But
901 * we suppress it if the reference is broken:
902 */
903 if ((iter->base.flags & REF_ISBROKEN)) {
904 oidclr(&iter->peeled);
905 iter->base.flags &= ~REF_KNOWS_PEELED;
906 } else {
907 iter->base.flags |= REF_KNOWS_PEELED;
908 }
909 } else {
910 oidclr(&iter->peeled);
911 }
912
913 return ITER_OK;
914}
915
67be7c5a
MH
916static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
917{
918 struct packed_ref_iterator *iter =
919 (struct packed_ref_iterator *)ref_iterator;
920 int ok;
921
523ee2d7 922 while ((ok = next_record(iter)) == ITER_OK) {
67be7c5a 923 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
71e54734 924 !is_per_worktree_ref(iter->base.refname))
67be7c5a
MH
925 continue;
926
927 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
9bc45a28
JT
928 !ref_resolves_to_object(iter->base.refname, iter->repo,
929 &iter->oid, iter->flags))
67be7c5a
MH
930 continue;
931
67be7c5a
MH
932 return ITER_OK;
933 }
934
67be7c5a
MH
935 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
936 ok = ITER_ERROR;
937
938 return ok;
939}
940
941static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
942 struct object_id *peeled)
943{
944 struct packed_ref_iterator *iter =
945 (struct packed_ref_iterator *)ref_iterator;
946
8788195c
JT
947 if (iter->repo != the_repository)
948 BUG("peeling for non-the_repository is not supported");
949
523ee2d7
MH
950 if ((iter->base.flags & REF_KNOWS_PEELED)) {
951 oidcpy(peeled, &iter->peeled);
952 return is_null_oid(&iter->peeled) ? -1 : 0;
953 } else if ((iter->base.flags & (REF_ISBROKEN | REF_ISSYMREF))) {
954 return -1;
955 } else {
617480d7 956 return peel_object(&iter->oid, peeled) ? -1 : 0;
523ee2d7 957 }
67be7c5a
MH
958}
959
960static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
961{
962 struct packed_ref_iterator *iter =
963 (struct packed_ref_iterator *)ref_iterator;
964 int ok = ITER_DONE;
965
523ee2d7 966 strbuf_release(&iter->refname_buf);
59c35fac 967 free(iter->jump);
cff28ca9 968 release_snapshot(iter->snapshot);
67be7c5a
MH
969 base_ref_iterator_free(ref_iterator);
970 return ok;
971}
972
973static struct ref_iterator_vtable packed_ref_iterator_vtable = {
e2f8acb6
ÆAB
974 .advance = packed_ref_iterator_advance,
975 .peel = packed_ref_iterator_peel,
976 .abort = packed_ref_iterator_abort
67be7c5a
MH
977};
978
59c35fac
TB
979static int jump_list_entry_cmp(const void *va, const void *vb)
980{
981 const struct jump_list_entry *a = va;
982 const struct jump_list_entry *b = vb;
983
984 if (a->start < b->start)
985 return -1;
986 if (a->start > b->start)
987 return 1;
988 return 0;
989}
990
991static int has_glob_special(const char *str)
992{
993 const char *p;
994 for (p = str; *p; p++) {
995 if (is_glob_special(*p))
996 return 1;
997 }
998 return 0;
999}
1000
1001static void populate_excluded_jump_list(struct packed_ref_iterator *iter,
1002 struct snapshot *snapshot,
1003 const char **excluded_patterns)
1004{
1005 size_t i, j;
1006 const char **pattern;
1007 struct jump_list_entry *last_disjoint;
1008
1009 if (!excluded_patterns)
1010 return;
1011
1012 for (pattern = excluded_patterns; *pattern; pattern++) {
1013 struct jump_list_entry *e;
1014 const char *start, *end;
1015
1016 /*
1017 * We can't feed any excludes with globs in them to the
1018 * refs machinery. It only understands prefix matching.
1019 * We likewise can't even feed the string leading up to
1020 * the first meta-character, as something like "foo[a]"
1021 * should not exclude "foobar" (but the prefix "foo"
1022 * would match that and mark it for exclusion).
1023 */
1024 if (has_glob_special(*pattern))
1025 continue;
1026
1027 start = find_reference_location(snapshot, *pattern, 0);
1028 end = find_reference_location_end(snapshot, *pattern, 0);
1029
1030 if (start == end)
1031 continue; /* nothing to jump over */
1032
1033 ALLOC_GROW(iter->jump, iter->jump_nr + 1, iter->jump_alloc);
1034
1035 e = &iter->jump[iter->jump_nr++];
1036 e->start = start;
1037 e->end = end;
1038 }
1039
1040 if (!iter->jump_nr) {
1041 /*
1042 * Every entry in exclude_patterns has a meta-character,
1043 * nothing to do here.
1044 */
1045 return;
1046 }
1047
1048 QSORT(iter->jump, iter->jump_nr, jump_list_entry_cmp);
1049
1050 /*
1051 * As an optimization, merge adjacent entries in the jump list
1052 * to jump forwards as far as possible when entering a skipped
1053 * region.
1054 *
1055 * For example, if we have two skipped regions:
1056 *
1057 * [[A, B], [B, C]]
1058 *
1059 * we want to combine that into a single entry jumping from A to
1060 * C.
1061 */
1062 last_disjoint = iter->jump;
1063
1064 for (i = 1, j = 1; i < iter->jump_nr; i++) {
1065 struct jump_list_entry *ours = &iter->jump[i];
1066 if (ours->start <= last_disjoint->end) {
1067 /* overlapping regions extend the previous one */
1068 last_disjoint->end = last_disjoint->end > ours->end
1069 ? last_disjoint->end : ours->end;
1070 } else {
1071 /* otherwise, insert a new region */
1072 iter->jump[j++] = *ours;
1073 last_disjoint = ours;
1074 }
1075 }
1076
1077 iter->jump_nr = j;
1078 iter->jump_cur = 0;
1079}
1080
e0cc8ac8
MH
1081static struct ref_iterator *packed_ref_iterator_begin(
1082 struct ref_store *ref_store,
b269ac53
TB
1083 const char *prefix, const char **exclude_patterns,
1084 unsigned int flags)
67be7c5a 1085{
e0cc8ac8 1086 struct packed_ref_store *refs;
cff28ca9 1087 struct snapshot *snapshot;
d1cf1551 1088 const char *start;
67be7c5a
MH
1089 struct packed_ref_iterator *iter;
1090 struct ref_iterator *ref_iterator;
e0cc8ac8
MH
1091 unsigned int required_flags = REF_STORE_READ;
1092
1093 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
1094 required_flags |= REF_STORE_ODB;
1095 refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
67be7c5a 1096
cff28ca9
MH
1097 /*
1098 * Note that `get_snapshot()` internally checks whether the
1099 * snapshot is up to date with what is on disk, and re-reads
1100 * it if not.
1101 */
1102 snapshot = get_snapshot(refs);
523ee2d7 1103
f3424297
MH
1104 if (prefix && *prefix)
1105 start = find_reference_location(snapshot, prefix, 0);
1106 else
1107 start = snapshot->start;
1108
1109 if (start == snapshot->eof)
523ee2d7
MH
1110 return empty_ref_iterator_begin();
1111
ca56dadb 1112 CALLOC_ARRAY(iter, 1);
67be7c5a 1113 ref_iterator = &iter->base;
5e01d838 1114 base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable);
67be7c5a 1115
59c35fac
TB
1116 if (exclude_patterns)
1117 populate_excluded_jump_list(iter, snapshot, exclude_patterns);
1118
cff28ca9
MH
1119 iter->snapshot = snapshot;
1120 acquire_snapshot(snapshot);
67be7c5a 1121
523ee2d7 1122 iter->pos = start;
cff28ca9 1123 iter->eof = snapshot->eof;
523ee2d7
MH
1124 strbuf_init(&iter->refname_buf, 0);
1125
1126 iter->base.oid = &iter->oid;
67be7c5a 1127
9bc45a28 1128 iter->repo = ref_store->repo;
67be7c5a
MH
1129 iter->flags = flags;
1130
d1cf1551
MH
1131 if (prefix && *prefix)
1132 /* Stop iteration after we've gone *past* prefix: */
1133 ref_iterator = prefix_ref_iterator_begin(ref_iterator, prefix, 0);
1134
67be7c5a
MH
1135 return ref_iterator;
1136}
1137
1138/*
1139 * Write an entry to the packed-refs file for the specified refname.
3478983b
MH
1140 * If peeled is non-NULL, write it as the entry's peeled value. On
1141 * error, return a nonzero value and leave errno set at the value left
1142 * by the failing call to `fprintf()`.
67be7c5a 1143 */
3478983b 1144static int write_packed_entry(FILE *fh, const char *refname,
41701882
MH
1145 const struct object_id *oid,
1146 const struct object_id *peeled)
67be7c5a 1147{
41701882
MH
1148 if (fprintf(fh, "%s %s\n", oid_to_hex(oid), refname) < 0 ||
1149 (peeled && fprintf(fh, "^%s\n", oid_to_hex(peeled)) < 0))
3478983b
MH
1150 return -1;
1151
1152 return 0;
67be7c5a
MH
1153}
1154
c8bed835 1155int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
67be7c5a 1156{
e0cc8ac8
MH
1157 struct packed_ref_store *refs =
1158 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
b7de57d8 1159 "packed_refs_lock");
67be7c5a
MH
1160 static int timeout_configured = 0;
1161 static int timeout_value = 1000;
67be7c5a 1162
67be7c5a
MH
1163 if (!timeout_configured) {
1164 git_config_get_int("core.packedrefstimeout", &timeout_value);
1165 timeout_configured = 1;
1166 }
1167
42dfa7ec
MH
1168 /*
1169 * Note that we close the lockfile immediately because we
1170 * don't write new content to it, but rather to a separate
1171 * tempfile.
1172 */
67be7c5a
MH
1173 if (hold_lock_file_for_update_timeout(
1174 &refs->lock,
1175 refs->path,
c8bed835
MH
1176 flags, timeout_value) < 0) {
1177 unable_to_lock_message(refs->path, errno, err);
1178 return -1;
1179 }
1180
83a3069a 1181 if (close_lock_file_gently(&refs->lock)) {
c8bed835 1182 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
83a3069a 1183 rollback_lock_file(&refs->lock);
67be7c5a 1184 return -1;
c8bed835 1185 }
67be7c5a
MH
1186
1187 /*
a613d4f8
SC
1188 * There is a stat-validity problem might cause `update-ref -d`
1189 * lost the newly commit of a ref, because a new `packed-refs`
1190 * file might has the same on-disk file attributes such as
1191 * timestamp, file size and inode value, but has a changed
1192 * ref value.
1193 *
1194 * This could happen with a very small chance when
1195 * `update-ref -d` is called and at the same time another
1196 * `pack-refs --all` process is running.
1197 *
1198 * Now that we hold the `packed-refs` lock, it is important
1199 * to make sure we could read the latest version of
1200 * `packed-refs` file no matter we have just mmap it or not.
1201 * So what need to do is clear the snapshot if we hold it
1202 * already.
67be7c5a 1203 */
a613d4f8 1204 clear_snapshot(refs);
67be7c5a 1205
39c8df0c
MH
1206 /*
1207 * Now make sure that the packed-refs file as it exists in the
cff28ca9 1208 * locked state is loaded into the snapshot:
39c8df0c 1209 */
cff28ca9 1210 get_snapshot(refs);
67be7c5a
MH
1211 return 0;
1212}
1213
49aebcf4
MH
1214void packed_refs_unlock(struct ref_store *ref_store)
1215{
1216 struct packed_ref_store *refs = packed_downcast(
1217 ref_store,
1218 REF_STORE_READ | REF_STORE_WRITE,
1219 "packed_refs_unlock");
1220
1221 if (!is_lock_file_locked(&refs->lock))
033abf97 1222 BUG("packed_refs_unlock() called when not locked");
49aebcf4 1223 rollback_lock_file(&refs->lock);
49aebcf4
MH
1224}
1225
1226int packed_refs_is_locked(struct ref_store *ref_store)
1227{
1228 struct packed_ref_store *refs = packed_downcast(
1229 ref_store,
1230 REF_STORE_READ | REF_STORE_WRITE,
1231 "packed_refs_is_locked");
1232
1233 return is_lock_file_locked(&refs->lock);
1234}
1235
67be7c5a 1236/*
cff28ca9
MH
1237 * The packed-refs header line that we write out. Perhaps other traits
1238 * will be added later.
a8811695
MH
1239 *
1240 * Note that earlier versions of Git used to parse these traits by
1241 * looking for " trait " in the line. For this reason, the space after
1242 * the colon and the trailing space are required.
67be7c5a
MH
1243 */
1244static const char PACKED_REFS_HEADER[] =
02b920f3 1245 "# pack-refs with: peeled fully-peeled sorted \n";
67be7c5a 1246
5cf88fd8 1247static int packed_init_db(struct ref_store *ref_store UNUSED,
2e573d61 1248 int flags UNUSED,
5cf88fd8 1249 struct strbuf *err UNUSED)
e0cc8ac8
MH
1250{
1251 /* Nothing to do. */
1252 return 0;
1253}
1254
67be7c5a 1255/*
cff28ca9
MH
1256 * Write the packed refs from the current snapshot to the packed-refs
1257 * tempfile, incorporating any changes from `updates`. `updates` must
1258 * be a sorted string list whose keys are the refnames and whose util
2775d872
MH
1259 * values are `struct ref_update *`. On error, rollback the tempfile,
1260 * write an error message to `err`, and return a nonzero value.
1261 *
1262 * The packfile must be locked before calling this function and will
1263 * remain locked when it is done.
67be7c5a 1264 */
2775d872
MH
1265static int write_with_updates(struct packed_ref_store *refs,
1266 struct string_list *updates,
1267 struct strbuf *err)
67be7c5a 1268{
2775d872
MH
1269 struct ref_iterator *iter = NULL;
1270 size_t i;
3478983b 1271 int ok;
67be7c5a 1272 FILE *out;
2775d872 1273 struct strbuf sb = STRBUF_INIT;
198b808e 1274 char *packed_refs_path;
67be7c5a 1275
67be7c5a 1276 if (!is_lock_file_locked(&refs->lock))
033abf97 1277 BUG("write_with_updates() called while unlocked");
67be7c5a 1278
198b808e
MH
1279 /*
1280 * If packed-refs is a symlink, we want to overwrite the
1281 * symlinked-to file, not the symlink itself. Also, put the
1282 * staging file next to it:
1283 */
1284 packed_refs_path = get_locked_file_path(&refs->lock);
1285 strbuf_addf(&sb, "%s.new", packed_refs_path);
2775d872 1286 free(packed_refs_path);
076aa2cb
JK
1287 refs->tempfile = create_tempfile(sb.buf);
1288 if (!refs->tempfile) {
42dfa7ec
MH
1289 strbuf_addf(err, "unable to create file %s: %s",
1290 sb.buf, strerror(errno));
1291 strbuf_release(&sb);
2775d872 1292 return -1;
42dfa7ec
MH
1293 }
1294 strbuf_release(&sb);
1295
076aa2cb 1296 out = fdopen_tempfile(refs->tempfile, "w");
3478983b
MH
1297 if (!out) {
1298 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
1299 strerror(errno));
1300 goto error;
1301 }
67be7c5a 1302
2775d872
MH
1303 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
1304 goto write_error;
1305
1306 /*
1307 * We iterate in parallel through the current list of refs and
1308 * the list of updates, processing an entry from at least one
1309 * of the lists each time through the loop. When the current
1310 * list of refs is exhausted, set iter to NULL. When the list
1311 * of updates is exhausted, leave i set to updates->nr.
1312 */
b269ac53 1313 iter = packed_ref_iterator_begin(&refs->base, "", NULL,
2775d872
MH
1314 DO_FOR_EACH_INCLUDE_BROKEN);
1315 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
1316 iter = NULL;
1317
1318 i = 0;
67be7c5a 1319
2775d872
MH
1320 while (iter || i < updates->nr) {
1321 struct ref_update *update = NULL;
1322 int cmp;
1323
1324 if (i >= updates->nr) {
1325 cmp = -1;
1326 } else {
1327 update = updates->items[i].util;
1328
1329 if (!iter)
1330 cmp = +1;
1331 else
1332 cmp = strcmp(iter->refname, update->refname);
1333 }
1334
1335 if (!cmp) {
1336 /*
1337 * There is both an old value and an update
1338 * for this reference. Check the old value if
1339 * necessary:
1340 */
1341 if ((update->flags & REF_HAVE_OLD)) {
1342 if (is_null_oid(&update->old_oid)) {
1343 strbuf_addf(err, "cannot update ref '%s': "
1344 "reference already exists",
1345 update->refname);
1346 goto error;
9001dc2a 1347 } else if (!oideq(&update->old_oid, iter->oid)) {
2775d872
MH
1348 strbuf_addf(err, "cannot update ref '%s': "
1349 "is at %s but expected %s",
1350 update->refname,
1351 oid_to_hex(iter->oid),
1352 oid_to_hex(&update->old_oid));
1353 goto error;
1354 }
1355 }
1356
1357 /* Now figure out what to use for the new value: */
1358 if ((update->flags & REF_HAVE_NEW)) {
1359 /*
1360 * The update takes precedence. Skip
1361 * the iterator over the unneeded
1362 * value.
1363 */
1364 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
1365 iter = NULL;
1366 cmp = +1;
1367 } else {
1368 /*
1369 * The update doesn't actually want to
1370 * change anything. We're done with it.
1371 */
1372 i++;
1373 cmp = -1;
1374 }
1375 } else if (cmp > 0) {
1376 /*
1377 * There is no old value but there is an
1378 * update for this reference. Make sure that
1379 * the update didn't expect an existing value:
1380 */
1381 if ((update->flags & REF_HAVE_OLD) &&
1382 !is_null_oid(&update->old_oid)) {
1383 strbuf_addf(err, "cannot update ref '%s': "
1384 "reference is missing but expected %s",
1385 update->refname,
1386 oid_to_hex(&update->old_oid));
1387 goto error;
1388 }
1389 }
1390
1391 if (cmp < 0) {
1392 /* Pass the old reference through. */
1393
1394 struct object_id peeled;
1395 int peel_error = ref_iterator_peel(iter, &peeled);
1396
1397 if (write_packed_entry(out, iter->refname,
41701882
MH
1398 iter->oid,
1399 peel_error ? NULL : &peeled))
2775d872
MH
1400 goto write_error;
1401
1402 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
1403 iter = NULL;
1404 } else if (is_null_oid(&update->new_oid)) {
1405 /*
1406 * The update wants to delete the reference,
1407 * and the reference either didn't exist or we
1408 * have already skipped it. So we're done with
1409 * the update (and don't have to write
1410 * anything).
1411 */
1412 i++;
1413 } else {
1414 struct object_id peeled;
ac2ed0d7 1415 int peel_error = peel_object(&update->new_oid,
1416 &peeled);
2775d872
MH
1417
1418 if (write_packed_entry(out, update->refname,
41701882
MH
1419 &update->new_oid,
1420 peel_error ? NULL : &peeled))
2775d872
MH
1421 goto write_error;
1422
1423 i++;
3478983b 1424 }
67be7c5a
MH
1425 }
1426
3478983b 1427 if (ok != ITER_DONE) {
72d4a9a7
RS
1428 strbuf_addstr(err, "unable to write packed-refs file: "
1429 "error iterating over old contents");
3478983b
MH
1430 goto error;
1431 }
67be7c5a 1432
ce54672f
PS
1433 if (fflush(out) ||
1434 fsync_component(FSYNC_COMPONENT_REFERENCE, get_tempfile_fd(refs->tempfile)) ||
bc22d845 1435 close_tempfile_gently(refs->tempfile)) {
2775d872 1436 strbuf_addf(err, "error closing file %s: %s",
07f0542d 1437 get_tempfile_path(refs->tempfile),
2775d872
MH
1438 strerror(errno));
1439 strbuf_release(&sb);
07f0542d 1440 delete_tempfile(&refs->tempfile);
2775d872 1441 return -1;
67be7c5a 1442 }
3478983b 1443
2775d872
MH
1444 return 0;
1445
1446write_error:
1447 strbuf_addf(err, "error writing to %s: %s",
07f0542d 1448 get_tempfile_path(refs->tempfile), strerror(errno));
3478983b
MH
1449
1450error:
2775d872
MH
1451 if (iter)
1452 ref_iterator_abort(iter);
198b808e 1453
2775d872
MH
1454 delete_tempfile(&refs->tempfile);
1455 return -1;
67be7c5a
MH
1456}
1457
7c6bd25c
MH
1458int is_packed_transaction_needed(struct ref_store *ref_store,
1459 struct ref_transaction *transaction)
1460{
1461 struct packed_ref_store *refs = packed_downcast(
1462 ref_store,
1463 REF_STORE_READ,
1464 "is_packed_transaction_needed");
1465 struct strbuf referent = STRBUF_INIT;
1466 size_t i;
1467 int ret;
1468
1469 if (!is_lock_file_locked(&refs->lock))
1470 BUG("is_packed_transaction_needed() called while unlocked");
1471
1472 /*
1473 * We're only going to bother returning false for the common,
1474 * trivial case that references are only being deleted, their
1475 * old values are not being checked, and the old `packed-refs`
1476 * file doesn't contain any of those reference(s). This gives
1477 * false positives for some other cases that could
1478 * theoretically be optimized away:
1479 *
1480 * 1. It could be that the old value is being verified without
1481 * setting a new value. In this case, we could verify the
1482 * old value here and skip the update if it agrees. If it
1483 * disagrees, we could either let the update go through
1484 * (the actual commit would re-detect and report the
1485 * problem), or come up with a way of reporting such an
1486 * error to *our* caller.
1487 *
1488 * 2. It could be that a new value is being set, but that it
1489 * is identical to the current packed value of the
1490 * reference.
1491 *
1492 * Neither of these cases will come up in the current code,
1493 * because the only caller of this function passes to it a
1494 * transaction that only includes `delete` updates with no
1495 * `old_id`. Even if that ever changes, false positives only
1496 * cause an optimization to be missed; they do not affect
1497 * correctness.
1498 */
1499
1500 /*
1501 * Start with the cheap checks that don't require old
1502 * reference values to be read:
1503 */
1504 for (i = 0; i < transaction->nr; i++) {
1505 struct ref_update *update = transaction->updates[i];
1506
1507 if (update->flags & REF_HAVE_OLD)
1508 /* Have to check the old value -> needed. */
1509 return 1;
1510
1511 if ((update->flags & REF_HAVE_NEW) && !is_null_oid(&update->new_oid))
1512 /* Have to set a new value -> needed. */
1513 return 1;
1514 }
1515
1516 /*
1517 * The transaction isn't checking any old values nor is it
1518 * setting any nonzero new values, so it still might be able
1519 * to be skipped. Now do the more expensive check: the update
1520 * is needed if any of the updates is a delete, and the old
1521 * `packed-refs` file contains a value for that reference.
1522 */
1523 ret = 0;
1524 for (i = 0; i < transaction->nr; i++) {
1525 struct ref_update *update = transaction->updates[i];
8b72fea7 1526 int failure_errno;
7c6bd25c
MH
1527 unsigned int type;
1528 struct object_id oid;
1529
1530 if (!(update->flags & REF_HAVE_NEW))
1531 /*
1532 * This reference isn't being deleted -> not
1533 * needed.
1534 */
1535 continue;
1536
8b72fea7
HWN
1537 if (!refs_read_raw_ref(ref_store, update->refname, &oid,
1538 &referent, &type, &failure_errno) ||
1539 failure_errno != ENOENT) {
7c6bd25c
MH
1540 /*
1541 * We have to actually delete that reference
1542 * -> this transaction is needed.
1543 */
1544 ret = 1;
1545 break;
1546 }
1547 }
1548
1549 strbuf_release(&referent);
1550 return ret;
1551}
1552
2775d872
MH
1553struct packed_transaction_backend_data {
1554 /* True iff the transaction owns the packed-refs lock. */
1555 int own_lock;
1556
1557 struct string_list updates;
1558};
1559
1560static void packed_transaction_cleanup(struct packed_ref_store *refs,
1561 struct ref_transaction *transaction)
67be7c5a 1562{
2775d872 1563 struct packed_transaction_backend_data *data = transaction->backend_data;
67be7c5a 1564
2775d872
MH
1565 if (data) {
1566 string_list_clear(&data->updates, 0);
67be7c5a 1567
07f0542d 1568 if (is_tempfile_active(refs->tempfile))
2775d872 1569 delete_tempfile(&refs->tempfile);
e5cc7d7d 1570
2775d872
MH
1571 if (data->own_lock && is_lock_file_locked(&refs->lock)) {
1572 packed_refs_unlock(&refs->base);
1573 data->own_lock = 0;
67be7c5a 1574 }
67be7c5a 1575
2775d872
MH
1576 free(data);
1577 transaction->backend_data = NULL;
67be7c5a
MH
1578 }
1579
2775d872 1580 transaction->state = REF_TRANSACTION_CLOSED;
e0cc8ac8
MH
1581}
1582
1583static int packed_transaction_prepare(struct ref_store *ref_store,
1584 struct ref_transaction *transaction,
1585 struct strbuf *err)
1586{
2775d872
MH
1587 struct packed_ref_store *refs = packed_downcast(
1588 ref_store,
1589 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1590 "ref_transaction_prepare");
1591 struct packed_transaction_backend_data *data;
1592 size_t i;
1593 int ret = TRANSACTION_GENERIC_ERROR;
1594
1595 /*
1596 * Note that we *don't* skip transactions with zero updates,
1597 * because such a transaction might be executed for the side
cff28ca9
MH
1598 * effect of ensuring that all of the references are peeled or
1599 * ensuring that the `packed-refs` file is sorted. If the
1600 * caller wants to optimize away empty transactions, it should
1601 * do so itself.
2775d872
MH
1602 */
1603
ca56dadb 1604 CALLOC_ARRAY(data, 1);
bc40dfb1 1605 string_list_init_nodup(&data->updates);
2775d872
MH
1606
1607 transaction->backend_data = data;
1608
1609 /*
1610 * Stick the updates in a string list by refname so that we
1611 * can sort them:
1612 */
1613 for (i = 0; i < transaction->nr; i++) {
1614 struct ref_update *update = transaction->updates[i];
1615 struct string_list_item *item =
1616 string_list_append(&data->updates, update->refname);
1617
1618 /* Store a pointer to update in item->util: */
1619 item->util = update;
1620 }
1621 string_list_sort(&data->updates);
1622
1623 if (ref_update_reject_duplicates(&data->updates, err))
1624 goto failure;
1625
1626 if (!is_lock_file_locked(&refs->lock)) {
1627 if (packed_refs_lock(ref_store, 0, err))
1628 goto failure;
1629 data->own_lock = 1;
1630 }
1631
1632 if (write_with_updates(refs, &data->updates, err))
1633 goto failure;
1634
1635 transaction->state = REF_TRANSACTION_PREPARED;
1636 return 0;
1637
1638failure:
1639 packed_transaction_cleanup(refs, transaction);
1640 return ret;
e0cc8ac8
MH
1641}
1642
1643static int packed_transaction_abort(struct ref_store *ref_store,
1644 struct ref_transaction *transaction,
5cf88fd8 1645 struct strbuf *err UNUSED)
e0cc8ac8 1646{
2775d872
MH
1647 struct packed_ref_store *refs = packed_downcast(
1648 ref_store,
1649 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1650 "ref_transaction_abort");
1651
1652 packed_transaction_cleanup(refs, transaction);
1653 return 0;
e0cc8ac8
MH
1654}
1655
1656static int packed_transaction_finish(struct ref_store *ref_store,
1657 struct ref_transaction *transaction,
1658 struct strbuf *err)
1659{
2775d872
MH
1660 struct packed_ref_store *refs = packed_downcast(
1661 ref_store,
1662 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
1663 "ref_transaction_finish");
1664 int ret = TRANSACTION_GENERIC_ERROR;
1665 char *packed_refs_path;
1666
cff28ca9 1667 clear_snapshot(refs);
5b633610 1668
2775d872
MH
1669 packed_refs_path = get_locked_file_path(&refs->lock);
1670 if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
1671 strbuf_addf(err, "error replacing %s: %s",
1672 refs->path, strerror(errno));
1673 goto cleanup;
1674 }
1675
2775d872
MH
1676 ret = 0;
1677
1678cleanup:
1679 free(packed_refs_path);
1680 packed_transaction_cleanup(refs, transaction);
1681 return ret;
e0cc8ac8
MH
1682}
1683
5cf88fd8 1684static int packed_initial_transaction_commit(struct ref_store *ref_store UNUSED,
e0cc8ac8
MH
1685 struct ref_transaction *transaction,
1686 struct strbuf *err)
1687{
1688 return ref_transaction_commit(transaction, err);
1689}
1690
5cf88fd8 1691static int packed_pack_refs(struct ref_store *ref_store UNUSED,
826ae79f 1692 struct pack_refs_opts *pack_opts UNUSED)
e0cc8ac8
MH
1693{
1694 /*
1695 * Packed refs are already packed. It might be that loose refs
1696 * are packed *into* a packed refs store, but that is done by
1697 * updating the packed references via a transaction.
1698 */
1699 return 0;
1700}
1701
5cf88fd8 1702static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store UNUSED)
e0cc8ac8
MH
1703{
1704 return empty_ref_iterator_begin();
1705}
1706
e0cc8ac8 1707struct ref_storage_be refs_be_packed = {
32bff617
ÆAB
1708 .name = "packed",
1709 .init = packed_ref_store_create,
1710 .init_db = packed_init_db,
1711 .transaction_prepare = packed_transaction_prepare,
1712 .transaction_finish = packed_transaction_finish,
1713 .transaction_abort = packed_transaction_abort,
1714 .initial_transaction_commit = packed_initial_transaction_commit,
1715
1716 .pack_refs = packed_pack_refs,
ca40893a 1717 .create_symref = NULL,
ca40893a
ÆAB
1718 .rename_ref = NULL,
1719 .copy_ref = NULL,
32bff617
ÆAB
1720
1721 .iterator_begin = packed_ref_iterator_begin,
1722 .read_raw_ref = packed_read_raw_ref,
1723 .read_symbolic_ref = NULL,
1724
1725 .reflog_iterator_begin = packed_reflog_iterator_begin,
ca40893a
ÆAB
1726 .for_each_reflog_ent = NULL,
1727 .for_each_reflog_ent_reverse = NULL,
1728 .reflog_exists = NULL,
1729 .create_reflog = NULL,
1730 .delete_reflog = NULL,
1731 .reflog_expire = NULL,
e0cc8ac8 1732};