ae276f3445f6533452ddc3ce4808e89ecf912086
1#include "../cache.h"
2#include "../config.h"
3#include "../refs.h"
4#include "refs-internal.h"
5#include "ref-cache.h"
6#include "packed-backend.h"
7#include "../iterator.h"
8#include "../lockfile.h"
9
10struct packed_ref_store;
11
12struct packed_ref_cache {
13 /*
14 * A back-pointer to the packed_ref_store with which this
15 * cache is associated:
16 */
17 struct packed_ref_store *refs;
18
19 struct ref_cache *cache;
20
21 /*
22 * What is the peeled state of this cache? (This is usually
23 * determined from the header of the "packed-refs" file.)
24 */
25 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled;
26
27 /*
28 * Count of references to the data structure in this instance,
29 * including the pointer from files_ref_store::packed if any.
30 * The data will not be freed as long as the reference count
31 * is nonzero.
32 */
33 unsigned int referrers;
34
35 /* The metadata from when this packed-refs cache was read */
36 struct stat_validity validity;
37};
38
39/*
40 * Increment the reference count of *packed_refs.
41 */
42static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
43{
44 packed_refs->referrers++;
45}
46
47/*
48 * Decrease the reference count of *packed_refs. If it goes to zero,
49 * free *packed_refs and return true; otherwise return false.
50 */
51static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
52{
53 if (!--packed_refs->referrers) {
54 free_ref_cache(packed_refs->cache);
55 stat_validity_clear(&packed_refs->validity);
56 free(packed_refs);
57 return 1;
58 } else {
59 return 0;
60 }
61}
62
63/*
64 * A container for `packed-refs`-related data. It is not (yet) a
65 * `ref_store`.
66 */
67struct packed_ref_store {
68 struct ref_store base;
69
70 unsigned int store_flags;
71
72 /* The path of the "packed-refs" file: */
73 char *path;
74
75 /*
76 * A cache of the values read from the `packed-refs` file, if
77 * it might still be current; otherwise, NULL.
78 */
79 struct packed_ref_cache *cache;
80
81 /*
82 * Lock used for the "packed-refs" file. Note that this (and
83 * thus the enclosing `packed_ref_store`) must not be freed.
84 */
85 struct lock_file lock;
86
87 /*
88 * Temporary file used when rewriting new contents to the
89 * "packed-refs" file. Note that this (and thus the enclosing
90 * `packed_ref_store`) must not be freed.
91 */
92 struct tempfile tempfile;
93};
94
95struct ref_store *packed_ref_store_create(const char *path,
96 unsigned int store_flags)
97{
98 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
99 struct ref_store *ref_store = (struct ref_store *)refs;
100
101 base_ref_store_init(ref_store, &refs_be_packed);
102 refs->store_flags = store_flags;
103
104 refs->path = xstrdup(path);
105 return ref_store;
106}
107
108/*
109 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
110 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
111 * support at least the flags specified in `required_flags`. `caller`
112 * is used in any necessary error messages.
113 */
114static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
115 unsigned int required_flags,
116 const char *caller)
117{
118 struct packed_ref_store *refs;
119
120 if (ref_store->be != &refs_be_packed)
121 die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
122 ref_store->be->name, caller);
123
124 refs = (struct packed_ref_store *)ref_store;
125
126 if ((refs->store_flags & required_flags) != required_flags)
127 die("BUG: unallowed operation (%s), requires %x, has %x\n",
128 caller, required_flags, refs->store_flags);
129
130 return refs;
131}
132
133static void clear_packed_ref_cache(struct packed_ref_store *refs)
134{
135 if (refs->cache) {
136 struct packed_ref_cache *cache = refs->cache;
137
138 refs->cache = NULL;
139 release_packed_ref_cache(cache);
140 }
141}
142
143static NORETURN void die_unterminated_line(const char *path,
144 const char *p, size_t len)
145{
146 if (len < 80)
147 die("unterminated line in %s: %.*s", path, (int)len, p);
148 else
149 die("unterminated line in %s: %.75s...", path, p);
150}
151
152static NORETURN void die_invalid_line(const char *path,
153 const char *p, size_t len)
154{
155 const char *eol = memchr(p, '\n', len);
156
157 if (!eol)
158 die_unterminated_line(path, p, len);
159 else if (eol - p < 80)
160 die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
161 else
162 die("unexpected line in %s: %.75s...", path, p);
163
164}
165
166/*
167 * Read from the `packed-refs` file into a newly-allocated
168 * `packed_ref_cache` and return it. The return value will already
169 * have its reference count incremented.
170 *
171 * A comment line of the form "# pack-refs with: " may contain zero or
172 * more traits. We interpret the traits as follows:
173 *
174 * No traits:
175 *
176 * Probably no references are peeled. But if the file contains a
177 * peeled value for a reference, we will use it.
178 *
179 * peeled:
180 *
181 * References under "refs/tags/", if they *can* be peeled, *are*
182 * peeled in this file. References outside of "refs/tags/" are
183 * probably not peeled even if they could have been, but if we find
184 * a peeled value for such a reference we will use it.
185 *
186 * fully-peeled:
187 *
188 * All references in the file that can be peeled are peeled.
189 * Inversely (and this is more important), any references in the
190 * file for which no peeled value is recorded is not peelable. This
191 * trait should typically be written alongside "peeled" for
192 * compatibility with older clients, but we do not require it
193 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
194 */
195static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
196{
197 struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
198 int fd;
199 struct stat st;
200 size_t size;
201 char *buf;
202 const char *pos, *eol, *eof;
203 struct strbuf tmp = STRBUF_INIT;
204 struct ref_dir *dir;
205
206 packed_refs->refs = refs;
207 acquire_packed_ref_cache(packed_refs);
208 packed_refs->cache = create_ref_cache(NULL, NULL);
209 packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
210 packed_refs->peeled = PEELED_NONE;
211
212 fd = open(refs->path, O_RDONLY);
213 if (fd < 0) {
214 if (errno == ENOENT) {
215 /*
216 * This is OK; it just means that no
217 * "packed-refs" file has been written yet,
218 * which is equivalent to it being empty.
219 */
220 return packed_refs;
221 } else {
222 die_errno("couldn't read %s", refs->path);
223 }
224 }
225
226 stat_validity_update(&packed_refs->validity, fd);
227
228 if (fstat(fd, &st) < 0)
229 die_errno("couldn't stat %s", refs->path);
230
231 size = xsize_t(st.st_size);
232 buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
233 pos = buf;
234 eof = buf + size;
235
236 /* If the file has a header line, process it: */
237 if (pos < eof && *pos == '#') {
238 char *p;
239 struct string_list traits = STRING_LIST_INIT_NODUP;
240
241 eol = memchr(pos, '\n', eof - pos);
242 if (!eol)
243 die_unterminated_line(refs->path, pos, eof - pos);
244
245 strbuf_add(&tmp, pos, eol - pos);
246
247 if (!skip_prefix(tmp.buf, "# pack-refs with:", (const char **)&p))
248 die_invalid_line(refs->path, pos, eof - pos);
249
250 string_list_split_in_place(&traits, p, ' ', -1);
251
252 if (unsorted_string_list_has_string(&traits, "fully-peeled"))
253 packed_refs->peeled = PEELED_FULLY;
254 else if (unsorted_string_list_has_string(&traits, "peeled"))
255 packed_refs->peeled = PEELED_TAGS;
256 /* perhaps other traits later as well */
257
258 /* The "+ 1" is for the LF character. */
259 pos = eol + 1;
260
261 string_list_clear(&traits, 0);
262 strbuf_reset(&tmp);
263 }
264
265 dir = get_ref_dir(packed_refs->cache->root);
266 while (pos < eof) {
267 const char *p = pos;
268 struct object_id oid;
269 const char *refname;
270 int flag = REF_ISPACKED;
271 struct ref_entry *entry = NULL;
272
273 if (eof - pos < GIT_SHA1_HEXSZ + 2 ||
274 parse_oid_hex(p, &oid, &p) ||
275 !isspace(*p++))
276 die_invalid_line(refs->path, pos, eof - pos);
277
278 eol = memchr(p, '\n', eof - p);
279 if (!eol)
280 die_unterminated_line(refs->path, pos, eof - pos);
281
282 strbuf_add(&tmp, p, eol - p);
283 refname = tmp.buf;
284
285 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
286 if (!refname_is_safe(refname))
287 die("packed refname is dangerous: %s", refname);
288 oidclr(&oid);
289 flag |= REF_BAD_NAME | REF_ISBROKEN;
290 }
291 if (packed_refs->peeled == PEELED_FULLY ||
292 (packed_refs->peeled == PEELED_TAGS &&
293 starts_with(refname, "refs/tags/")))
294 flag |= REF_KNOWS_PEELED;
295 entry = create_ref_entry(refname, &oid, flag);
296 add_ref_entry(dir, entry);
297
298 pos = eol + 1;
299
300 if (pos < eof && *pos == '^') {
301 p = pos + 1;
302 if (eof - p < GIT_SHA1_HEXSZ + 1 ||
303 parse_oid_hex(p, &entry->u.value.peeled, &p) ||
304 *p++ != '\n')
305 die_invalid_line(refs->path, pos, eof - pos);
306
307 /*
308 * Regardless of what the file header said,
309 * we definitely know the value of *this*
310 * reference:
311 */
312 entry->flag |= REF_KNOWS_PEELED;
313
314 pos = p;
315 }
316
317 strbuf_reset(&tmp);
318 }
319
320 if (munmap(buf, size))
321 die_errno("error ummapping packed-refs file");
322 close(fd);
323
324 strbuf_release(&tmp);
325 return packed_refs;
326}
327
328/*
329 * Check that the packed refs cache (if any) still reflects the
330 * contents of the file. If not, clear the cache.
331 */
332static void validate_packed_ref_cache(struct packed_ref_store *refs)
333{
334 if (refs->cache &&
335 !stat_validity_check(&refs->cache->validity, refs->path))
336 clear_packed_ref_cache(refs);
337}
338
339/*
340 * Get the packed_ref_cache for the specified packed_ref_store,
341 * creating and populating it if it hasn't been read before or if the
342 * file has been changed (according to its `validity` field) since it
343 * was last read. On the other hand, if we hold the lock, then assume
344 * that the file hasn't been changed out from under us, so skip the
345 * extra `stat()` call in `stat_validity_check()`.
346 */
347static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
348{
349 if (!is_lock_file_locked(&refs->lock))
350 validate_packed_ref_cache(refs);
351
352 if (!refs->cache)
353 refs->cache = read_packed_refs(refs);
354
355 return refs->cache;
356}
357
358static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
359{
360 return get_ref_dir(packed_ref_cache->cache->root);
361}
362
363static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
364{
365 return get_packed_ref_dir(get_packed_ref_cache(refs));
366}
367
368/*
369 * Return the ref_entry for the given refname from the packed
370 * references. If it does not exist, return NULL.
371 */
372static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
373 const char *refname)
374{
375 return find_ref_entry(get_packed_refs(refs), refname);
376}
377
378static int packed_read_raw_ref(struct ref_store *ref_store,
379 const char *refname, unsigned char *sha1,
380 struct strbuf *referent, unsigned int *type)
381{
382 struct packed_ref_store *refs =
383 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
384
385 struct ref_entry *entry;
386
387 *type = 0;
388
389 entry = get_packed_ref(refs, refname);
390 if (!entry) {
391 errno = ENOENT;
392 return -1;
393 }
394
395 hashcpy(sha1, entry->u.value.oid.hash);
396 *type = REF_ISPACKED;
397 return 0;
398}
399
400static int packed_peel_ref(struct ref_store *ref_store,
401 const char *refname, unsigned char *sha1)
402{
403 struct packed_ref_store *refs =
404 packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
405 "peel_ref");
406 struct ref_entry *r = get_packed_ref(refs, refname);
407
408 if (!r || peel_entry(r, 0))
409 return -1;
410
411 hashcpy(sha1, r->u.value.peeled.hash);
412 return 0;
413}
414
415struct packed_ref_iterator {
416 struct ref_iterator base;
417
418 struct packed_ref_cache *cache;
419 struct ref_iterator *iter0;
420 unsigned int flags;
421};
422
423static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
424{
425 struct packed_ref_iterator *iter =
426 (struct packed_ref_iterator *)ref_iterator;
427 int ok;
428
429 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
430 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
431 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
432 continue;
433
434 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
435 !ref_resolves_to_object(iter->iter0->refname,
436 iter->iter0->oid,
437 iter->iter0->flags))
438 continue;
439
440 iter->base.refname = iter->iter0->refname;
441 iter->base.oid = iter->iter0->oid;
442 iter->base.flags = iter->iter0->flags;
443 return ITER_OK;
444 }
445
446 iter->iter0 = NULL;
447 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
448 ok = ITER_ERROR;
449
450 return ok;
451}
452
453static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
454 struct object_id *peeled)
455{
456 struct packed_ref_iterator *iter =
457 (struct packed_ref_iterator *)ref_iterator;
458
459 return ref_iterator_peel(iter->iter0, peeled);
460}
461
462static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
463{
464 struct packed_ref_iterator *iter =
465 (struct packed_ref_iterator *)ref_iterator;
466 int ok = ITER_DONE;
467
468 if (iter->iter0)
469 ok = ref_iterator_abort(iter->iter0);
470
471 release_packed_ref_cache(iter->cache);
472 base_ref_iterator_free(ref_iterator);
473 return ok;
474}
475
476static struct ref_iterator_vtable packed_ref_iterator_vtable = {
477 packed_ref_iterator_advance,
478 packed_ref_iterator_peel,
479 packed_ref_iterator_abort
480};
481
482static struct ref_iterator *packed_ref_iterator_begin(
483 struct ref_store *ref_store,
484 const char *prefix, unsigned int flags)
485{
486 struct packed_ref_store *refs;
487 struct packed_ref_iterator *iter;
488 struct ref_iterator *ref_iterator;
489 unsigned int required_flags = REF_STORE_READ;
490
491 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
492 required_flags |= REF_STORE_ODB;
493 refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
494
495 iter = xcalloc(1, sizeof(*iter));
496 ref_iterator = &iter->base;
497 base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
498
499 /*
500 * Note that get_packed_ref_cache() internally checks whether
501 * the packed-ref cache is up to date with what is on disk,
502 * and re-reads it if not.
503 */
504
505 iter->cache = get_packed_ref_cache(refs);
506 acquire_packed_ref_cache(iter->cache);
507 iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
508
509 iter->flags = flags;
510
511 return ref_iterator;
512}
513
514/*
515 * Write an entry to the packed-refs file for the specified refname.
516 * If peeled is non-NULL, write it as the entry's peeled value. On
517 * error, return a nonzero value and leave errno set at the value left
518 * by the failing call to `fprintf()`.
519 */
520static int write_packed_entry(FILE *fh, const char *refname,
521 const unsigned char *sha1,
522 const unsigned char *peeled)
523{
524 if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
525 (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
526 return -1;
527
528 return 0;
529}
530
531int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
532{
533 struct packed_ref_store *refs =
534 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
535 "packed_refs_lock");
536 static int timeout_configured = 0;
537 static int timeout_value = 1000;
538
539 if (!timeout_configured) {
540 git_config_get_int("core.packedrefstimeout", &timeout_value);
541 timeout_configured = 1;
542 }
543
544 /*
545 * Note that we close the lockfile immediately because we
546 * don't write new content to it, but rather to a separate
547 * tempfile.
548 */
549 if (hold_lock_file_for_update_timeout(
550 &refs->lock,
551 refs->path,
552 flags, timeout_value) < 0) {
553 unable_to_lock_message(refs->path, errno, err);
554 return -1;
555 }
556
557 if (close_lock_file(&refs->lock)) {
558 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
559 return -1;
560 }
561
562 /*
563 * Now that we hold the `packed-refs` lock, make sure that our
564 * cache matches the current version of the file. Normally
565 * `get_packed_ref_cache()` does that for us, but that
566 * function assumes that when the file is locked, any existing
567 * cache is still valid. We've just locked the file, but it
568 * might have changed the moment *before* we locked it.
569 */
570 validate_packed_ref_cache(refs);
571
572 /*
573 * Now make sure that the packed-refs file as it exists in the
574 * locked state is loaded into the cache:
575 */
576 get_packed_ref_cache(refs);
577 return 0;
578}
579
580void packed_refs_unlock(struct ref_store *ref_store)
581{
582 struct packed_ref_store *refs = packed_downcast(
583 ref_store,
584 REF_STORE_READ | REF_STORE_WRITE,
585 "packed_refs_unlock");
586
587 if (!is_lock_file_locked(&refs->lock))
588 die("BUG: packed_refs_unlock() called when not locked");
589 rollback_lock_file(&refs->lock);
590}
591
592int packed_refs_is_locked(struct ref_store *ref_store)
593{
594 struct packed_ref_store *refs = packed_downcast(
595 ref_store,
596 REF_STORE_READ | REF_STORE_WRITE,
597 "packed_refs_is_locked");
598
599 return is_lock_file_locked(&refs->lock);
600}
601
602/*
603 * The packed-refs header line that we write out. Perhaps other
604 * traits will be added later.
605 *
606 * Note that earlier versions of Git used to parse these traits by
607 * looking for " trait " in the line. For this reason, the space after
608 * the colon and the trailing space are required.
609 */
610static const char PACKED_REFS_HEADER[] =
611 "# pack-refs with: peeled fully-peeled \n";
612
613static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
614{
615 /* Nothing to do. */
616 return 0;
617}
618
619/*
620 * Write the packed-refs from the cache to the packed-refs tempfile,
621 * incorporating any changes from `updates`. `updates` must be a
622 * sorted string list whose keys are the refnames and whose util
623 * values are `struct ref_update *`. On error, rollback the tempfile,
624 * write an error message to `err`, and return a nonzero value.
625 *
626 * The packfile must be locked before calling this function and will
627 * remain locked when it is done.
628 */
629static int write_with_updates(struct packed_ref_store *refs,
630 struct string_list *updates,
631 struct strbuf *err)
632{
633 struct ref_iterator *iter = NULL;
634 size_t i;
635 int ok;
636 FILE *out;
637 struct strbuf sb = STRBUF_INIT;
638 char *packed_refs_path;
639
640 if (!is_lock_file_locked(&refs->lock))
641 die("BUG: write_with_updates() called while unlocked");
642
643 /*
644 * If packed-refs is a symlink, we want to overwrite the
645 * symlinked-to file, not the symlink itself. Also, put the
646 * staging file next to it:
647 */
648 packed_refs_path = get_locked_file_path(&refs->lock);
649 strbuf_addf(&sb, "%s.new", packed_refs_path);
650 free(packed_refs_path);
651 if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
652 strbuf_addf(err, "unable to create file %s: %s",
653 sb.buf, strerror(errno));
654 strbuf_release(&sb);
655 return -1;
656 }
657 strbuf_release(&sb);
658
659 out = fdopen_tempfile(&refs->tempfile, "w");
660 if (!out) {
661 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
662 strerror(errno));
663 goto error;
664 }
665
666 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
667 goto write_error;
668
669 /*
670 * We iterate in parallel through the current list of refs and
671 * the list of updates, processing an entry from at least one
672 * of the lists each time through the loop. When the current
673 * list of refs is exhausted, set iter to NULL. When the list
674 * of updates is exhausted, leave i set to updates->nr.
675 */
676 iter = packed_ref_iterator_begin(&refs->base, "",
677 DO_FOR_EACH_INCLUDE_BROKEN);
678 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
679 iter = NULL;
680
681 i = 0;
682
683 while (iter || i < updates->nr) {
684 struct ref_update *update = NULL;
685 int cmp;
686
687 if (i >= updates->nr) {
688 cmp = -1;
689 } else {
690 update = updates->items[i].util;
691
692 if (!iter)
693 cmp = +1;
694 else
695 cmp = strcmp(iter->refname, update->refname);
696 }
697
698 if (!cmp) {
699 /*
700 * There is both an old value and an update
701 * for this reference. Check the old value if
702 * necessary:
703 */
704 if ((update->flags & REF_HAVE_OLD)) {
705 if (is_null_oid(&update->old_oid)) {
706 strbuf_addf(err, "cannot update ref '%s': "
707 "reference already exists",
708 update->refname);
709 goto error;
710 } else if (oidcmp(&update->old_oid, iter->oid)) {
711 strbuf_addf(err, "cannot update ref '%s': "
712 "is at %s but expected %s",
713 update->refname,
714 oid_to_hex(iter->oid),
715 oid_to_hex(&update->old_oid));
716 goto error;
717 }
718 }
719
720 /* Now figure out what to use for the new value: */
721 if ((update->flags & REF_HAVE_NEW)) {
722 /*
723 * The update takes precedence. Skip
724 * the iterator over the unneeded
725 * value.
726 */
727 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
728 iter = NULL;
729 cmp = +1;
730 } else {
731 /*
732 * The update doesn't actually want to
733 * change anything. We're done with it.
734 */
735 i++;
736 cmp = -1;
737 }
738 } else if (cmp > 0) {
739 /*
740 * There is no old value but there is an
741 * update for this reference. Make sure that
742 * the update didn't expect an existing value:
743 */
744 if ((update->flags & REF_HAVE_OLD) &&
745 !is_null_oid(&update->old_oid)) {
746 strbuf_addf(err, "cannot update ref '%s': "
747 "reference is missing but expected %s",
748 update->refname,
749 oid_to_hex(&update->old_oid));
750 goto error;
751 }
752 }
753
754 if (cmp < 0) {
755 /* Pass the old reference through. */
756
757 struct object_id peeled;
758 int peel_error = ref_iterator_peel(iter, &peeled);
759
760 if (write_packed_entry(out, iter->refname,
761 iter->oid->hash,
762 peel_error ? NULL : peeled.hash))
763 goto write_error;
764
765 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
766 iter = NULL;
767 } else if (is_null_oid(&update->new_oid)) {
768 /*
769 * The update wants to delete the reference,
770 * and the reference either didn't exist or we
771 * have already skipped it. So we're done with
772 * the update (and don't have to write
773 * anything).
774 */
775 i++;
776 } else {
777 struct object_id peeled;
778 int peel_error = peel_object(update->new_oid.hash,
779 peeled.hash);
780
781 if (write_packed_entry(out, update->refname,
782 update->new_oid.hash,
783 peel_error ? NULL : peeled.hash))
784 goto write_error;
785
786 i++;
787 }
788 }
789
790 if (ok != ITER_DONE) {
791 strbuf_addf(err, "unable to write packed-refs file: "
792 "error iterating over old contents");
793 goto error;
794 }
795
796 if (close_tempfile(&refs->tempfile)) {
797 strbuf_addf(err, "error closing file %s: %s",
798 get_tempfile_path(&refs->tempfile),
799 strerror(errno));
800 strbuf_release(&sb);
801 return -1;
802 }
803
804 return 0;
805
806write_error:
807 strbuf_addf(err, "error writing to %s: %s",
808 get_tempfile_path(&refs->tempfile), strerror(errno));
809
810error:
811 if (iter)
812 ref_iterator_abort(iter);
813
814 delete_tempfile(&refs->tempfile);
815 return -1;
816}
817
818struct packed_transaction_backend_data {
819 /* True iff the transaction owns the packed-refs lock. */
820 int own_lock;
821
822 struct string_list updates;
823};
824
825static void packed_transaction_cleanup(struct packed_ref_store *refs,
826 struct ref_transaction *transaction)
827{
828 struct packed_transaction_backend_data *data = transaction->backend_data;
829
830 if (data) {
831 string_list_clear(&data->updates, 0);
832
833 if (is_tempfile_active(&refs->tempfile))
834 delete_tempfile(&refs->tempfile);
835
836 if (data->own_lock && is_lock_file_locked(&refs->lock)) {
837 packed_refs_unlock(&refs->base);
838 data->own_lock = 0;
839 }
840
841 free(data);
842 transaction->backend_data = NULL;
843 }
844
845 transaction->state = REF_TRANSACTION_CLOSED;
846}
847
848static int packed_transaction_prepare(struct ref_store *ref_store,
849 struct ref_transaction *transaction,
850 struct strbuf *err)
851{
852 struct packed_ref_store *refs = packed_downcast(
853 ref_store,
854 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
855 "ref_transaction_prepare");
856 struct packed_transaction_backend_data *data;
857 size_t i;
858 int ret = TRANSACTION_GENERIC_ERROR;
859
860 /*
861 * Note that we *don't* skip transactions with zero updates,
862 * because such a transaction might be executed for the side
863 * effect of ensuring that all of the references are peeled.
864 * If the caller wants to optimize away empty transactions, it
865 * should do so itself.
866 */
867
868 data = xcalloc(1, sizeof(*data));
869 string_list_init(&data->updates, 0);
870
871 transaction->backend_data = data;
872
873 /*
874 * Stick the updates in a string list by refname so that we
875 * can sort them:
876 */
877 for (i = 0; i < transaction->nr; i++) {
878 struct ref_update *update = transaction->updates[i];
879 struct string_list_item *item =
880 string_list_append(&data->updates, update->refname);
881
882 /* Store a pointer to update in item->util: */
883 item->util = update;
884 }
885 string_list_sort(&data->updates);
886
887 if (ref_update_reject_duplicates(&data->updates, err))
888 goto failure;
889
890 if (!is_lock_file_locked(&refs->lock)) {
891 if (packed_refs_lock(ref_store, 0, err))
892 goto failure;
893 data->own_lock = 1;
894 }
895
896 if (write_with_updates(refs, &data->updates, err))
897 goto failure;
898
899 transaction->state = REF_TRANSACTION_PREPARED;
900 return 0;
901
902failure:
903 packed_transaction_cleanup(refs, transaction);
904 return ret;
905}
906
907static int packed_transaction_abort(struct ref_store *ref_store,
908 struct ref_transaction *transaction,
909 struct strbuf *err)
910{
911 struct packed_ref_store *refs = packed_downcast(
912 ref_store,
913 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
914 "ref_transaction_abort");
915
916 packed_transaction_cleanup(refs, transaction);
917 return 0;
918}
919
920static int packed_transaction_finish(struct ref_store *ref_store,
921 struct ref_transaction *transaction,
922 struct strbuf *err)
923{
924 struct packed_ref_store *refs = packed_downcast(
925 ref_store,
926 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
927 "ref_transaction_finish");
928 int ret = TRANSACTION_GENERIC_ERROR;
929 char *packed_refs_path;
930
931 packed_refs_path = get_locked_file_path(&refs->lock);
932 if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
933 strbuf_addf(err, "error replacing %s: %s",
934 refs->path, strerror(errno));
935 goto cleanup;
936 }
937
938 clear_packed_ref_cache(refs);
939 ret = 0;
940
941cleanup:
942 free(packed_refs_path);
943 packed_transaction_cleanup(refs, transaction);
944 return ret;
945}
946
947static int packed_initial_transaction_commit(struct ref_store *ref_store,
948 struct ref_transaction *transaction,
949 struct strbuf *err)
950{
951 return ref_transaction_commit(transaction, err);
952}
953
954static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
955 struct string_list *refnames, unsigned int flags)
956{
957 struct packed_ref_store *refs =
958 packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
959 struct strbuf err = STRBUF_INIT;
960 struct ref_transaction *transaction;
961 struct string_list_item *item;
962 int ret;
963
964 (void)refs; /* We need the check above, but don't use the variable */
965
966 if (!refnames->nr)
967 return 0;
968
969 /*
970 * Since we don't check the references' old_oids, the
971 * individual updates can't fail, so we can pack all of the
972 * updates into a single transaction.
973 */
974
975 transaction = ref_store_transaction_begin(ref_store, &err);
976 if (!transaction)
977 return -1;
978
979 for_each_string_list_item(item, refnames) {
980 if (ref_transaction_delete(transaction, item->string, NULL,
981 flags, msg, &err)) {
982 warning(_("could not delete reference %s: %s"),
983 item->string, err.buf);
984 strbuf_reset(&err);
985 }
986 }
987
988 ret = ref_transaction_commit(transaction, &err);
989
990 if (ret) {
991 if (refnames->nr == 1)
992 error(_("could not delete reference %s: %s"),
993 refnames->items[0].string, err.buf);
994 else
995 error(_("could not delete references: %s"), err.buf);
996 }
997
998 ref_transaction_free(transaction);
999 strbuf_release(&err);
1000 return ret;
1001}
1002
1003static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
1004{
1005 /*
1006 * Packed refs are already packed. It might be that loose refs
1007 * are packed *into* a packed refs store, but that is done by
1008 * updating the packed references via a transaction.
1009 */
1010 return 0;
1011}
1012
1013static int packed_create_symref(struct ref_store *ref_store,
1014 const char *refname, const char *target,
1015 const char *logmsg)
1016{
1017 die("BUG: packed reference store does not support symrefs");
1018}
1019
1020static int packed_rename_ref(struct ref_store *ref_store,
1021 const char *oldrefname, const char *newrefname,
1022 const char *logmsg)
1023{
1024 die("BUG: packed reference store does not support renaming references");
1025}
1026
1027static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
1028{
1029 return empty_ref_iterator_begin();
1030}
1031
1032static int packed_for_each_reflog_ent(struct ref_store *ref_store,
1033 const char *refname,
1034 each_reflog_ent_fn fn, void *cb_data)
1035{
1036 return 0;
1037}
1038
1039static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1040 const char *refname,
1041 each_reflog_ent_fn fn,
1042 void *cb_data)
1043{
1044 return 0;
1045}
1046
1047static int packed_reflog_exists(struct ref_store *ref_store,
1048 const char *refname)
1049{
1050 return 0;
1051}
1052
1053static int packed_create_reflog(struct ref_store *ref_store,
1054 const char *refname, int force_create,
1055 struct strbuf *err)
1056{
1057 die("BUG: packed reference store does not support reflogs");
1058}
1059
1060static int packed_delete_reflog(struct ref_store *ref_store,
1061 const char *refname)
1062{
1063 return 0;
1064}
1065
1066static int packed_reflog_expire(struct ref_store *ref_store,
1067 const char *refname, const unsigned char *sha1,
1068 unsigned int flags,
1069 reflog_expiry_prepare_fn prepare_fn,
1070 reflog_expiry_should_prune_fn should_prune_fn,
1071 reflog_expiry_cleanup_fn cleanup_fn,
1072 void *policy_cb_data)
1073{
1074 return 0;
1075}
1076
1077struct ref_storage_be refs_be_packed = {
1078 NULL,
1079 "packed",
1080 packed_ref_store_create,
1081 packed_init_db,
1082 packed_transaction_prepare,
1083 packed_transaction_finish,
1084 packed_transaction_abort,
1085 packed_initial_transaction_commit,
1086
1087 packed_pack_refs,
1088 packed_peel_ref,
1089 packed_create_symref,
1090 packed_delete_refs,
1091 packed_rename_ref,
1092
1093 packed_ref_iterator_begin,
1094 packed_read_raw_ref,
1095
1096 packed_reflog_iterator_begin,
1097 packed_for_each_reflog_ent,
1098 packed_for_each_reflog_ent_reverse,
1099 packed_reflog_exists,
1100 packed_create_reflog,
1101 packed_delete_reflog,
1102 packed_reflog_expire
1103};