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