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