4 #include "refs-internal.h"
6 #include "packed-backend.h"
7 #include "../iterator.h"
8 #include "../lockfile.h"
10 struct packed_ref_store;
12 struct packed_ref_cache {
14 * A back-pointer to the packed_ref_store with which this
15 * cache is associated:
17 struct packed_ref_store *refs;
19 struct ref_cache *cache;
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
27 unsigned int referrers;
29 /* The metadata from when this packed-refs cache was read */
30 struct stat_validity validity;
34 * Increment the reference count of *packed_refs.
36 static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
38 packed_refs->referrers++;
42 * Decrease the reference count of *packed_refs. If it goes to zero,
43 * free *packed_refs and return true; otherwise return false.
45 static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
47 if (!--packed_refs->referrers) {
48 free_ref_cache(packed_refs->cache);
49 stat_validity_clear(&packed_refs->validity);
58 * A container for `packed-refs`-related data. It is not (yet) a
61 struct packed_ref_store {
62 struct ref_store base;
64 unsigned int store_flags;
66 /* The path of the "packed-refs" file: */
70 * A cache of the values read from the `packed-refs` file, if
71 * it might still be current; otherwise, NULL.
73 struct packed_ref_cache *cache;
76 * Lock used for the "packed-refs" file. Note that this (and
77 * thus the enclosing `packed_ref_store`) must not be freed.
79 struct lock_file lock;
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.
86 struct tempfile tempfile;
89 struct ref_store *packed_ref_store_create(const char *path,
90 unsigned int store_flags)
92 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
93 struct ref_store *ref_store = (struct ref_store *)refs;
95 base_ref_store_init(ref_store, &refs_be_packed);
96 refs->store_flags = store_flags;
98 refs->path = xstrdup(path);
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.
108 static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
109 unsigned int required_flags,
112 struct packed_ref_store *refs;
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);
118 refs = (struct packed_ref_store *)ref_store;
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);
127 static void clear_packed_ref_cache(struct packed_ref_store *refs)
130 struct packed_ref_cache *cache = refs->cache;
133 release_packed_ref_cache(cache);
137 /* The length of a peeled reference line in packed-refs, including EOL: */
138 #define PEELED_LINE_LENGTH 42
141 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
142 * Return a pointer to the refname within the line (null-terminated),
143 * or NULL if there was a problem.
145 static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
149 if (parse_oid_hex(line->buf, oid, &ref) < 0)
151 if (!isspace(*ref++))
157 if (line->buf[line->len - 1] != '\n')
159 line->buf[--line->len] = 0;
164 static NORETURN void die_unterminated_line(const char *path,
165 const char *p, size_t len)
168 die("unterminated line in %s: %.*s", path, (int)len, p);
170 die("unterminated line in %s: %.75s...", path, p);
173 static NORETURN void die_invalid_line(const char *path,
174 const char *p, size_t len)
176 const char *eol = memchr(p, '\n', len);
179 die_unterminated_line(path, p, len);
180 else if (eol - p < 80)
181 die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
183 die("unexpected line in %s: %.75s...", path, p);
188 * Read from the `packed-refs` file into a newly-allocated
189 * `packed_ref_cache` and return it. The return value will already
190 * have its reference count incremented.
192 * A comment line of the form "# pack-refs with: " may contain zero or
193 * more traits. We interpret the traits as follows:
197 * Probably no references are peeled. But if the file contains a
198 * peeled value for a reference, we will use it.
202 * References under "refs/tags/", if they *can* be peeled, *are*
203 * peeled in this file. References outside of "refs/tags/" are
204 * probably not peeled even if they could have been, but if we find
205 * a peeled value for such a reference we will use it.
209 * All references in the file that can be peeled are peeled.
210 * Inversely (and this is more important), any references in the
211 * file for which no peeled value is recorded is not peelable. This
212 * trait should typically be written alongside "peeled" for
213 * compatibility with older clients, but we do not require it
214 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
216 static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
218 struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
223 const char *pos, *eol, *eof;
224 struct ref_entry *last = NULL;
225 struct strbuf line = STRBUF_INIT;
226 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
229 packed_refs->refs = refs;
230 acquire_packed_ref_cache(packed_refs);
231 packed_refs->cache = create_ref_cache(NULL, NULL);
232 packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
234 fd = open(refs->path, O_RDONLY);
236 if (errno == ENOENT) {
238 * This is OK; it just means that no
239 * "packed-refs" file has been written yet,
240 * which is equivalent to it being empty.
244 die_errno("couldn't read %s", refs->path);
248 stat_validity_update(&packed_refs->validity, fd);
250 if (fstat(fd, &st) < 0)
251 die_errno("couldn't stat %s", refs->path);
253 size = xsize_t(st.st_size);
254 buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
258 /* If the file has a header line, process it: */
259 if (pos < eof && *pos == '#') {
261 struct string_list traits = STRING_LIST_INIT_NODUP;
263 eol = memchr(pos, '\n', eof - pos);
265 die_unterminated_line(refs->path, pos, eof - pos);
267 strbuf_add(&line, pos, eol - pos);
269 if (!skip_prefix(line.buf, "# pack-refs with:", (const char **)&p))
270 die_invalid_line(refs->path, pos, eof - pos);
272 string_list_split_in_place(&traits, p, ' ', -1);
274 if (unsorted_string_list_has_string(&traits, "fully-peeled"))
275 peeled = PEELED_FULLY;
276 else if (unsorted_string_list_has_string(&traits, "peeled"))
277 peeled = PEELED_TAGS;
278 /* perhaps other traits later as well */
280 /* The "+ 1" is for the LF character. */
283 string_list_clear(&traits, 0);
287 dir = get_ref_dir(packed_refs->cache->root);
289 struct object_id oid;
292 eol = memchr(pos, '\n', eof - pos);
294 die_unterminated_line(refs->path, pos, eof - pos);
296 strbuf_add(&line, pos, eol + 1 - pos);
298 refname = parse_ref_line(&line, &oid);
300 int flag = REF_ISPACKED;
302 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
303 if (!refname_is_safe(refname))
304 die("packed refname is dangerous: %s", refname);
306 flag |= REF_BAD_NAME | REF_ISBROKEN;
308 last = create_ref_entry(refname, &oid, flag);
309 if (peeled == PEELED_FULLY ||
310 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
311 last->flag |= REF_KNOWS_PEELED;
312 add_ref_entry(dir, last);
314 line.buf[0] == '^' &&
315 line.len == PEELED_LINE_LENGTH &&
316 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
317 !get_oid_hex(line.buf + 1, &oid)) {
318 oidcpy(&last->u.value.peeled, &oid);
320 * Regardless of what the file header said,
321 * we definitely know the value of *this*
324 last->flag |= REF_KNOWS_PEELED;
326 die_invalid_line(refs->path, line.buf, line.len);
329 /* The "+ 1" is for the LF character. */
334 if (munmap(buf, size))
335 die_errno("error ummapping packed-refs file");
338 strbuf_release(&line);
343 * Check that the packed refs cache (if any) still reflects the
344 * contents of the file. If not, clear the cache.
346 static void validate_packed_ref_cache(struct packed_ref_store *refs)
349 !stat_validity_check(&refs->cache->validity, refs->path))
350 clear_packed_ref_cache(refs);
354 * Get the packed_ref_cache for the specified packed_ref_store,
355 * creating and populating it if it hasn't been read before or if the
356 * file has been changed (according to its `validity` field) since it
357 * was last read. On the other hand, if we hold the lock, then assume
358 * that the file hasn't been changed out from under us, so skip the
359 * extra `stat()` call in `stat_validity_check()`.
361 static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
363 if (!is_lock_file_locked(&refs->lock))
364 validate_packed_ref_cache(refs);
367 refs->cache = read_packed_refs(refs);
372 static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
374 return get_ref_dir(packed_ref_cache->cache->root);
377 static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
379 return get_packed_ref_dir(get_packed_ref_cache(refs));
383 * Return the ref_entry for the given refname from the packed
384 * references. If it does not exist, return NULL.
386 static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
389 return find_ref_entry(get_packed_refs(refs), refname);
392 static int packed_read_raw_ref(struct ref_store *ref_store,
393 const char *refname, unsigned char *sha1,
394 struct strbuf *referent, unsigned int *type)
396 struct packed_ref_store *refs =
397 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
399 struct ref_entry *entry;
403 entry = get_packed_ref(refs, refname);
409 hashcpy(sha1, entry->u.value.oid.hash);
410 *type = REF_ISPACKED;
414 static int packed_peel_ref(struct ref_store *ref_store,
415 const char *refname, unsigned char *sha1)
417 struct packed_ref_store *refs =
418 packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
420 struct ref_entry *r = get_packed_ref(refs, refname);
422 if (!r || peel_entry(r, 0))
425 hashcpy(sha1, r->u.value.peeled.hash);
429 struct packed_ref_iterator {
430 struct ref_iterator base;
432 struct packed_ref_cache *cache;
433 struct ref_iterator *iter0;
437 static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
439 struct packed_ref_iterator *iter =
440 (struct packed_ref_iterator *)ref_iterator;
443 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
444 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
445 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
448 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
449 !ref_resolves_to_object(iter->iter0->refname,
454 iter->base.refname = iter->iter0->refname;
455 iter->base.oid = iter->iter0->oid;
456 iter->base.flags = iter->iter0->flags;
461 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
467 static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
468 struct object_id *peeled)
470 struct packed_ref_iterator *iter =
471 (struct packed_ref_iterator *)ref_iterator;
473 return ref_iterator_peel(iter->iter0, peeled);
476 static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
478 struct packed_ref_iterator *iter =
479 (struct packed_ref_iterator *)ref_iterator;
483 ok = ref_iterator_abort(iter->iter0);
485 release_packed_ref_cache(iter->cache);
486 base_ref_iterator_free(ref_iterator);
490 static struct ref_iterator_vtable packed_ref_iterator_vtable = {
491 packed_ref_iterator_advance,
492 packed_ref_iterator_peel,
493 packed_ref_iterator_abort
496 static struct ref_iterator *packed_ref_iterator_begin(
497 struct ref_store *ref_store,
498 const char *prefix, unsigned int flags)
500 struct packed_ref_store *refs;
501 struct packed_ref_iterator *iter;
502 struct ref_iterator *ref_iterator;
503 unsigned int required_flags = REF_STORE_READ;
505 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
506 required_flags |= REF_STORE_ODB;
507 refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
509 iter = xcalloc(1, sizeof(*iter));
510 ref_iterator = &iter->base;
511 base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
514 * Note that get_packed_ref_cache() internally checks whether
515 * the packed-ref cache is up to date with what is on disk,
516 * and re-reads it if not.
519 iter->cache = get_packed_ref_cache(refs);
520 acquire_packed_ref_cache(iter->cache);
521 iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
529 * Write an entry to the packed-refs file for the specified refname.
530 * If peeled is non-NULL, write it as the entry's peeled value. On
531 * error, return a nonzero value and leave errno set at the value left
532 * by the failing call to `fprintf()`.
534 static int write_packed_entry(FILE *fh, const char *refname,
535 const unsigned char *sha1,
536 const unsigned char *peeled)
538 if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
539 (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
545 int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
547 struct packed_ref_store *refs =
548 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
550 static int timeout_configured = 0;
551 static int timeout_value = 1000;
553 if (!timeout_configured) {
554 git_config_get_int("core.packedrefstimeout", &timeout_value);
555 timeout_configured = 1;
559 * Note that we close the lockfile immediately because we
560 * don't write new content to it, but rather to a separate
563 if (hold_lock_file_for_update_timeout(
566 flags, timeout_value) < 0) {
567 unable_to_lock_message(refs->path, errno, err);
571 if (close_lock_file(&refs->lock)) {
572 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
577 * Now that we hold the `packed-refs` lock, make sure that our
578 * cache matches the current version of the file. Normally
579 * `get_packed_ref_cache()` does that for us, but that
580 * function assumes that when the file is locked, any existing
581 * cache is still valid. We've just locked the file, but it
582 * might have changed the moment *before* we locked it.
584 validate_packed_ref_cache(refs);
587 * Now make sure that the packed-refs file as it exists in the
588 * locked state is loaded into the cache:
590 get_packed_ref_cache(refs);
594 void packed_refs_unlock(struct ref_store *ref_store)
596 struct packed_ref_store *refs = packed_downcast(
598 REF_STORE_READ | REF_STORE_WRITE,
599 "packed_refs_unlock");
601 if (!is_lock_file_locked(&refs->lock))
602 die("BUG: packed_refs_unlock() called when not locked");
603 rollback_lock_file(&refs->lock);
606 int packed_refs_is_locked(struct ref_store *ref_store)
608 struct packed_ref_store *refs = packed_downcast(
610 REF_STORE_READ | REF_STORE_WRITE,
611 "packed_refs_is_locked");
613 return is_lock_file_locked(&refs->lock);
617 * The packed-refs header line that we write out. Perhaps other
618 * traits will be added later.
620 * Note that earlier versions of Git used to parse these traits by
621 * looking for " trait " in the line. For this reason, the space after
622 * the colon and the trailing space are required.
624 static const char PACKED_REFS_HEADER[] =
625 "# pack-refs with: peeled fully-peeled \n";
627 static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
634 * Write the packed-refs from the cache to the packed-refs tempfile,
635 * incorporating any changes from `updates`. `updates` must be a
636 * sorted string list whose keys are the refnames and whose util
637 * values are `struct ref_update *`. On error, rollback the tempfile,
638 * write an error message to `err`, and return a nonzero value.
640 * The packfile must be locked before calling this function and will
641 * remain locked when it is done.
643 static int write_with_updates(struct packed_ref_store *refs,
644 struct string_list *updates,
647 struct ref_iterator *iter = NULL;
651 struct strbuf sb = STRBUF_INIT;
652 char *packed_refs_path;
654 if (!is_lock_file_locked(&refs->lock))
655 die("BUG: write_with_updates() called while unlocked");
658 * If packed-refs is a symlink, we want to overwrite the
659 * symlinked-to file, not the symlink itself. Also, put the
660 * staging file next to it:
662 packed_refs_path = get_locked_file_path(&refs->lock);
663 strbuf_addf(&sb, "%s.new", packed_refs_path);
664 free(packed_refs_path);
665 if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
666 strbuf_addf(err, "unable to create file %s: %s",
667 sb.buf, strerror(errno));
673 out = fdopen_tempfile(&refs->tempfile, "w");
675 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
680 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
684 * We iterate in parallel through the current list of refs and
685 * the list of updates, processing an entry from at least one
686 * of the lists each time through the loop. When the current
687 * list of refs is exhausted, set iter to NULL. When the list
688 * of updates is exhausted, leave i set to updates->nr.
690 iter = packed_ref_iterator_begin(&refs->base, "",
691 DO_FOR_EACH_INCLUDE_BROKEN);
692 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
697 while (iter || i < updates->nr) {
698 struct ref_update *update = NULL;
701 if (i >= updates->nr) {
704 update = updates->items[i].util;
709 cmp = strcmp(iter->refname, update->refname);
714 * There is both an old value and an update
715 * for this reference. Check the old value if
718 if ((update->flags & REF_HAVE_OLD)) {
719 if (is_null_oid(&update->old_oid)) {
720 strbuf_addf(err, "cannot update ref '%s': "
721 "reference already exists",
724 } else if (oidcmp(&update->old_oid, iter->oid)) {
725 strbuf_addf(err, "cannot update ref '%s': "
726 "is at %s but expected %s",
728 oid_to_hex(iter->oid),
729 oid_to_hex(&update->old_oid));
734 /* Now figure out what to use for the new value: */
735 if ((update->flags & REF_HAVE_NEW)) {
737 * The update takes precedence. Skip
738 * the iterator over the unneeded
741 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
746 * The update doesn't actually want to
747 * change anything. We're done with it.
752 } else if (cmp > 0) {
754 * There is no old value but there is an
755 * update for this reference. Make sure that
756 * the update didn't expect an existing value:
758 if ((update->flags & REF_HAVE_OLD) &&
759 !is_null_oid(&update->old_oid)) {
760 strbuf_addf(err, "cannot update ref '%s': "
761 "reference is missing but expected %s",
763 oid_to_hex(&update->old_oid));
769 /* Pass the old reference through. */
771 struct object_id peeled;
772 int peel_error = ref_iterator_peel(iter, &peeled);
774 if (write_packed_entry(out, iter->refname,
776 peel_error ? NULL : peeled.hash))
779 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
781 } else if (is_null_oid(&update->new_oid)) {
783 * The update wants to delete the reference,
784 * and the reference either didn't exist or we
785 * have already skipped it. So we're done with
786 * the update (and don't have to write
791 struct object_id peeled;
792 int peel_error = peel_object(update->new_oid.hash,
795 if (write_packed_entry(out, update->refname,
796 update->new_oid.hash,
797 peel_error ? NULL : peeled.hash))
804 if (ok != ITER_DONE) {
805 strbuf_addf(err, "unable to write packed-refs file: "
806 "error iterating over old contents");
810 if (close_tempfile(&refs->tempfile)) {
811 strbuf_addf(err, "error closing file %s: %s",
812 get_tempfile_path(&refs->tempfile),
821 strbuf_addf(err, "error writing to %s: %s",
822 get_tempfile_path(&refs->tempfile), strerror(errno));
826 ref_iterator_abort(iter);
828 delete_tempfile(&refs->tempfile);
832 struct packed_transaction_backend_data {
833 /* True iff the transaction owns the packed-refs lock. */
836 struct string_list updates;
839 static void packed_transaction_cleanup(struct packed_ref_store *refs,
840 struct ref_transaction *transaction)
842 struct packed_transaction_backend_data *data = transaction->backend_data;
845 string_list_clear(&data->updates, 0);
847 if (is_tempfile_active(&refs->tempfile))
848 delete_tempfile(&refs->tempfile);
850 if (data->own_lock && is_lock_file_locked(&refs->lock)) {
851 packed_refs_unlock(&refs->base);
856 transaction->backend_data = NULL;
859 transaction->state = REF_TRANSACTION_CLOSED;
862 static int packed_transaction_prepare(struct ref_store *ref_store,
863 struct ref_transaction *transaction,
866 struct packed_ref_store *refs = packed_downcast(
868 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
869 "ref_transaction_prepare");
870 struct packed_transaction_backend_data *data;
872 int ret = TRANSACTION_GENERIC_ERROR;
875 * Note that we *don't* skip transactions with zero updates,
876 * because such a transaction might be executed for the side
877 * effect of ensuring that all of the references are peeled.
878 * If the caller wants to optimize away empty transactions, it
879 * should do so itself.
882 data = xcalloc(1, sizeof(*data));
883 string_list_init(&data->updates, 0);
885 transaction->backend_data = data;
888 * Stick the updates in a string list by refname so that we
891 for (i = 0; i < transaction->nr; i++) {
892 struct ref_update *update = transaction->updates[i];
893 struct string_list_item *item =
894 string_list_append(&data->updates, update->refname);
896 /* Store a pointer to update in item->util: */
899 string_list_sort(&data->updates);
901 if (ref_update_reject_duplicates(&data->updates, err))
904 if (!is_lock_file_locked(&refs->lock)) {
905 if (packed_refs_lock(ref_store, 0, err))
910 if (write_with_updates(refs, &data->updates, err))
913 transaction->state = REF_TRANSACTION_PREPARED;
917 packed_transaction_cleanup(refs, transaction);
921 static int packed_transaction_abort(struct ref_store *ref_store,
922 struct ref_transaction *transaction,
925 struct packed_ref_store *refs = packed_downcast(
927 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
928 "ref_transaction_abort");
930 packed_transaction_cleanup(refs, transaction);
934 static int packed_transaction_finish(struct ref_store *ref_store,
935 struct ref_transaction *transaction,
938 struct packed_ref_store *refs = packed_downcast(
940 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
941 "ref_transaction_finish");
942 int ret = TRANSACTION_GENERIC_ERROR;
943 char *packed_refs_path;
945 packed_refs_path = get_locked_file_path(&refs->lock);
946 if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
947 strbuf_addf(err, "error replacing %s: %s",
948 refs->path, strerror(errno));
952 clear_packed_ref_cache(refs);
956 free(packed_refs_path);
957 packed_transaction_cleanup(refs, transaction);
961 static int packed_initial_transaction_commit(struct ref_store *ref_store,
962 struct ref_transaction *transaction,
965 return ref_transaction_commit(transaction, err);
968 static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
969 struct string_list *refnames, unsigned int flags)
971 struct packed_ref_store *refs =
972 packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
973 struct strbuf err = STRBUF_INIT;
974 struct ref_transaction *transaction;
975 struct string_list_item *item;
978 (void)refs; /* We need the check above, but don't use the variable */
984 * Since we don't check the references' old_oids, the
985 * individual updates can't fail, so we can pack all of the
986 * updates into a single transaction.
989 transaction = ref_store_transaction_begin(ref_store, &err);
993 for_each_string_list_item(item, refnames) {
994 if (ref_transaction_delete(transaction, item->string, NULL,
996 warning(_("could not delete reference %s: %s"),
997 item->string, err.buf);
1002 ret = ref_transaction_commit(transaction, &err);
1005 if (refnames->nr == 1)
1006 error(_("could not delete reference %s: %s"),
1007 refnames->items[0].string, err.buf);
1009 error(_("could not delete references: %s"), err.buf);
1012 ref_transaction_free(transaction);
1013 strbuf_release(&err);
1017 static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
1020 * Packed refs are already packed. It might be that loose refs
1021 * are packed *into* a packed refs store, but that is done by
1022 * updating the packed references via a transaction.
1027 static int packed_create_symref(struct ref_store *ref_store,
1028 const char *refname, const char *target,
1031 die("BUG: packed reference store does not support symrefs");
1034 static int packed_rename_ref(struct ref_store *ref_store,
1035 const char *oldrefname, const char *newrefname,
1038 die("BUG: packed reference store does not support renaming references");
1041 static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
1043 return empty_ref_iterator_begin();
1046 static int packed_for_each_reflog_ent(struct ref_store *ref_store,
1047 const char *refname,
1048 each_reflog_ent_fn fn, void *cb_data)
1053 static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1054 const char *refname,
1055 each_reflog_ent_fn fn,
1061 static int packed_reflog_exists(struct ref_store *ref_store,
1062 const char *refname)
1067 static int packed_create_reflog(struct ref_store *ref_store,
1068 const char *refname, int force_create,
1071 die("BUG: packed reference store does not support reflogs");
1074 static int packed_delete_reflog(struct ref_store *ref_store,
1075 const char *refname)
1080 static int packed_reflog_expire(struct ref_store *ref_store,
1081 const char *refname, const unsigned char *sha1,
1083 reflog_expiry_prepare_fn prepare_fn,
1084 reflog_expiry_should_prune_fn should_prune_fn,
1085 reflog_expiry_cleanup_fn cleanup_fn,
1086 void *policy_cb_data)
1091 struct ref_storage_be refs_be_packed = {
1094 packed_ref_store_create,
1096 packed_transaction_prepare,
1097 packed_transaction_finish,
1098 packed_transaction_abort,
1099 packed_initial_transaction_commit,
1103 packed_create_symref,
1107 packed_ref_iterator_begin,
1108 packed_read_raw_ref,
1110 packed_reflog_iterator_begin,
1111 packed_for_each_reflog_ent,
1112 packed_for_each_reflog_ent_reverse,
1113 packed_reflog_exists,
1114 packed_create_reflog,
1115 packed_delete_reflog,
1116 packed_reflog_expire