3 #include "refs-internal.h"
5 #include "packed-backend.h"
6 #include "../iterator.h"
7 #include "../lockfile.h"
9 struct packed_ref_cache {
10 struct ref_cache *cache;
13 * Count of references to the data structure in this instance,
14 * including the pointer from files_ref_store::packed if any.
15 * The data will not be freed as long as the reference count
18 unsigned int referrers;
20 /* The metadata from when this packed-refs cache was read */
21 struct stat_validity validity;
25 * Increment the reference count of *packed_refs.
27 static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
29 packed_refs->referrers++;
33 * Decrease the reference count of *packed_refs. If it goes to zero,
34 * free *packed_refs and return true; otherwise return false.
36 static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
38 if (!--packed_refs->referrers) {
39 free_ref_cache(packed_refs->cache);
40 stat_validity_clear(&packed_refs->validity);
49 * A container for `packed-refs`-related data. It is not (yet) a
52 struct packed_ref_store {
53 struct ref_store base;
55 unsigned int store_flags;
57 /* The path of the "packed-refs" file: */
61 * A cache of the values read from the `packed-refs` file, if
62 * it might still be current; otherwise, NULL.
64 struct packed_ref_cache *cache;
67 * Lock used for the "packed-refs" file. Note that this (and
68 * thus the enclosing `packed_ref_store`) must not be freed.
70 struct lock_file lock;
73 * Temporary file used when rewriting new contents to the
74 * "packed-refs" file. Note that this (and thus the enclosing
75 * `packed_ref_store`) must not be freed.
77 struct tempfile tempfile;
80 struct ref_store *packed_ref_store_create(const char *path,
81 unsigned int store_flags)
83 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
84 struct ref_store *ref_store = (struct ref_store *)refs;
86 base_ref_store_init(ref_store, &refs_be_packed);
87 refs->store_flags = store_flags;
89 refs->path = xstrdup(path);
94 * Die if refs is not the main ref store. caller is used in any
95 * necessary error messages.
97 static void packed_assert_main_repository(struct packed_ref_store *refs,
100 if (refs->store_flags & REF_STORE_MAIN)
103 die("BUG: operation %s only allowed for main ref store", caller);
107 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
108 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
109 * support at least the flags specified in `required_flags`. `caller`
110 * is used in any necessary error messages.
112 static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
113 unsigned int required_flags,
116 struct packed_ref_store *refs;
118 if (ref_store->be != &refs_be_packed)
119 die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
120 ref_store->be->name, caller);
122 refs = (struct packed_ref_store *)ref_store;
124 if ((refs->store_flags & required_flags) != required_flags)
125 die("BUG: unallowed operation (%s), requires %x, has %x\n",
126 caller, required_flags, refs->store_flags);
131 static void clear_packed_ref_cache(struct packed_ref_store *refs)
134 struct packed_ref_cache *cache = refs->cache;
136 if (is_lock_file_locked(&refs->lock))
137 die("BUG: packed-ref cache cleared while locked");
139 release_packed_ref_cache(cache);
143 /* The length of a peeled reference line in packed-refs, including EOL: */
144 #define PEELED_LINE_LENGTH 42
147 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
148 * Return a pointer to the refname within the line (null-terminated),
149 * or NULL if there was a problem.
151 static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
155 if (parse_oid_hex(line->buf, oid, &ref) < 0)
157 if (!isspace(*ref++))
163 if (line->buf[line->len - 1] != '\n')
165 line->buf[--line->len] = 0;
171 * Read from `packed_refs_file` into a newly-allocated
172 * `packed_ref_cache` and return it. The return value will already
173 * have its reference count incremented.
175 * A comment line of the form "# pack-refs with: " may contain zero or
176 * more traits. We interpret the traits as follows:
180 * Probably no references are peeled. But if the file contains a
181 * peeled value for a reference, we will use it.
185 * References under "refs/tags/", if they *can* be peeled, *are*
186 * peeled in this file. References outside of "refs/tags/" are
187 * probably not peeled even if they could have been, but if we find
188 * a peeled value for such a reference we will use it.
192 * All references in the file that can be peeled are peeled.
193 * Inversely (and this is more important), any references in the
194 * file for which no peeled value is recorded is not peelable. This
195 * trait should typically be written alongside "peeled" for
196 * compatibility with older clients, but we do not require it
197 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
199 static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
202 struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
203 struct ref_entry *last = NULL;
204 struct strbuf line = STRBUF_INIT;
205 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
208 acquire_packed_ref_cache(packed_refs);
209 packed_refs->cache = create_ref_cache(NULL, NULL);
210 packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
212 f = fopen(packed_refs_file, "r");
214 if (errno == ENOENT) {
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.
222 die_errno("couldn't read %s", packed_refs_file);
226 stat_validity_update(&packed_refs->validity, fileno(f));
228 dir = get_ref_dir(packed_refs->cache->root);
229 while (strbuf_getwholeline(&line, f, '\n') != EOF) {
230 struct object_id oid;
234 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
235 if (strstr(traits, " fully-peeled "))
236 peeled = PEELED_FULLY;
237 else if (strstr(traits, " peeled "))
238 peeled = PEELED_TAGS;
239 /* perhaps other traits later as well */
243 refname = parse_ref_line(&line, &oid);
245 int flag = REF_ISPACKED;
247 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
248 if (!refname_is_safe(refname))
249 die("packed refname is dangerous: %s", refname);
251 flag |= REF_BAD_NAME | REF_ISBROKEN;
253 last = create_ref_entry(refname, &oid, flag);
254 if (peeled == PEELED_FULLY ||
255 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
256 last->flag |= REF_KNOWS_PEELED;
257 add_ref_entry(dir, last);
261 line.buf[0] == '^' &&
262 line.len == PEELED_LINE_LENGTH &&
263 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
264 !get_oid_hex(line.buf + 1, &oid)) {
265 oidcpy(&last->u.value.peeled, &oid);
267 * Regardless of what the file header said,
268 * we definitely know the value of *this*
271 last->flag |= REF_KNOWS_PEELED;
276 strbuf_release(&line);
282 * Check that the packed refs cache (if any) still reflects the
283 * contents of the file. If not, clear the cache.
285 static void validate_packed_ref_cache(struct packed_ref_store *refs)
288 !stat_validity_check(&refs->cache->validity, refs->path))
289 clear_packed_ref_cache(refs);
293 * Get the packed_ref_cache for the specified packed_ref_store,
294 * creating and populating it if it hasn't been read before or if the
295 * file has been changed (according to its `validity` field) since it
296 * was last read. On the other hand, if we hold the lock, then assume
297 * that the file hasn't been changed out from under us, so skip the
298 * extra `stat()` call in `stat_validity_check()`.
300 static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
302 if (!is_lock_file_locked(&refs->lock))
303 validate_packed_ref_cache(refs);
306 refs->cache = read_packed_refs(refs->path);
311 static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
313 return get_ref_dir(packed_ref_cache->cache->root);
316 static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
318 return get_packed_ref_dir(get_packed_ref_cache(refs));
322 * Add or overwrite a reference in the in-memory packed reference
323 * cache. This may only be called while the packed-refs file is locked
324 * (see packed_refs_lock()). To actually write the packed-refs file,
325 * call commit_packed_refs().
327 void add_packed_ref(struct ref_store *ref_store,
328 const char *refname, const struct object_id *oid)
330 struct packed_ref_store *refs =
331 packed_downcast(ref_store, REF_STORE_WRITE,
333 struct ref_dir *packed_refs;
334 struct ref_entry *packed_entry;
336 if (!is_lock_file_locked(&refs->lock))
337 die("BUG: packed refs not locked");
339 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
340 die("Reference has invalid format: '%s'", refname);
342 packed_refs = get_packed_refs(refs);
343 packed_entry = find_ref_entry(packed_refs, refname);
345 /* Overwrite the existing entry: */
346 oidcpy(&packed_entry->u.value.oid, oid);
347 packed_entry->flag = REF_ISPACKED;
348 oidclr(&packed_entry->u.value.peeled);
350 packed_entry = create_ref_entry(refname, oid, REF_ISPACKED);
351 add_ref_entry(packed_refs, packed_entry);
356 * Return the ref_entry for the given refname from the packed
357 * references. If it does not exist, return NULL.
359 static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
362 return find_ref_entry(get_packed_refs(refs), refname);
365 static int packed_read_raw_ref(struct ref_store *ref_store,
366 const char *refname, unsigned char *sha1,
367 struct strbuf *referent, unsigned int *type)
369 struct packed_ref_store *refs =
370 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
372 struct ref_entry *entry;
376 entry = get_packed_ref(refs, refname);
382 hashcpy(sha1, entry->u.value.oid.hash);
383 *type = REF_ISPACKED;
387 static int packed_peel_ref(struct ref_store *ref_store,
388 const char *refname, unsigned char *sha1)
390 struct packed_ref_store *refs =
391 packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
393 struct ref_entry *r = get_packed_ref(refs, refname);
395 if (!r || peel_entry(r, 0))
398 hashcpy(sha1, r->u.value.peeled.hash);
402 struct packed_ref_iterator {
403 struct ref_iterator base;
405 struct packed_ref_cache *cache;
406 struct ref_iterator *iter0;
410 static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
412 struct packed_ref_iterator *iter =
413 (struct packed_ref_iterator *)ref_iterator;
416 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
417 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
418 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
421 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
422 !ref_resolves_to_object(iter->iter0->refname,
427 iter->base.refname = iter->iter0->refname;
428 iter->base.oid = iter->iter0->oid;
429 iter->base.flags = iter->iter0->flags;
434 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
440 static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
441 struct object_id *peeled)
443 struct packed_ref_iterator *iter =
444 (struct packed_ref_iterator *)ref_iterator;
446 return ref_iterator_peel(iter->iter0, peeled);
449 static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
451 struct packed_ref_iterator *iter =
452 (struct packed_ref_iterator *)ref_iterator;
456 ok = ref_iterator_abort(iter->iter0);
458 release_packed_ref_cache(iter->cache);
459 base_ref_iterator_free(ref_iterator);
463 static struct ref_iterator_vtable packed_ref_iterator_vtable = {
464 packed_ref_iterator_advance,
465 packed_ref_iterator_peel,
466 packed_ref_iterator_abort
469 static struct ref_iterator *packed_ref_iterator_begin(
470 struct ref_store *ref_store,
471 const char *prefix, unsigned int flags)
473 struct packed_ref_store *refs;
474 struct packed_ref_iterator *iter;
475 struct ref_iterator *ref_iterator;
476 unsigned int required_flags = REF_STORE_READ;
478 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
479 required_flags |= REF_STORE_ODB;
480 refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
482 iter = xcalloc(1, sizeof(*iter));
483 ref_iterator = &iter->base;
484 base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable);
487 * Note that get_packed_ref_cache() internally checks whether
488 * the packed-ref cache is up to date with what is on disk,
489 * and re-reads it if not.
492 iter->cache = get_packed_ref_cache(refs);
493 acquire_packed_ref_cache(iter->cache);
494 iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
502 * Write an entry to the packed-refs file for the specified refname.
503 * If peeled is non-NULL, write it as the entry's peeled value. On
504 * error, return a nonzero value and leave errno set at the value left
505 * by the failing call to `fprintf()`.
507 static int write_packed_entry(FILE *fh, const char *refname,
508 const unsigned char *sha1,
509 const unsigned char *peeled)
511 if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
512 (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
518 int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
520 struct packed_ref_store *refs =
521 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
523 static int timeout_configured = 0;
524 static int timeout_value = 1000;
525 struct packed_ref_cache *packed_ref_cache;
527 if (!timeout_configured) {
528 git_config_get_int("core.packedrefstimeout", &timeout_value);
529 timeout_configured = 1;
533 * Note that we close the lockfile immediately because we
534 * don't write new content to it, but rather to a separate
537 if (hold_lock_file_for_update_timeout(
540 flags, timeout_value) < 0) {
541 unable_to_lock_message(refs->path, errno, err);
545 if (close_lock_file(&refs->lock)) {
546 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
551 * Now that we hold the `packed-refs` lock, make sure that our
552 * cache matches the current version of the file. Normally
553 * `get_packed_ref_cache()` does that for us, but that
554 * function assumes that when the file is locked, any existing
555 * cache is still valid. We've just locked the file, but it
556 * might have changed the moment *before* we locked it.
558 validate_packed_ref_cache(refs);
560 packed_ref_cache = get_packed_ref_cache(refs);
561 /* Increment the reference count to prevent it from being freed: */
562 acquire_packed_ref_cache(packed_ref_cache);
567 * The packed-refs header line that we write out. Perhaps other
568 * traits will be added later. The trailing space is required.
570 static const char PACKED_REFS_HEADER[] =
571 "# pack-refs with: peeled fully-peeled \n";
574 * Write the current version of the packed refs cache from memory to
575 * disk. The packed-refs file must already be locked for writing (see
576 * packed_refs_lock()). Return zero on success. On errors, rollback
577 * the lockfile, write an error message to `err`, and return a nonzero
580 int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
582 struct packed_ref_store *refs =
583 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
584 "commit_packed_refs");
585 struct packed_ref_cache *packed_ref_cache =
586 get_packed_ref_cache(refs);
589 struct strbuf sb = STRBUF_INIT;
591 struct ref_iterator *iter;
593 if (!is_lock_file_locked(&refs->lock))
594 die("BUG: commit_packed_refs() called when unlocked");
596 strbuf_addf(&sb, "%s.new", refs->path);
597 if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
598 strbuf_addf(err, "unable to create file %s: %s",
599 sb.buf, strerror(errno));
605 out = fdopen_tempfile(&refs->tempfile, "w");
607 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
612 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) {
613 strbuf_addf(err, "error writing to %s: %s",
614 get_tempfile_path(&refs->tempfile), strerror(errno));
618 iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
619 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
620 struct object_id peeled;
621 int peel_error = ref_iterator_peel(iter, &peeled);
623 if (write_packed_entry(out, iter->refname, iter->oid->hash,
624 peel_error ? NULL : peeled.hash)) {
625 strbuf_addf(err, "error writing to %s: %s",
626 get_tempfile_path(&refs->tempfile),
628 ref_iterator_abort(iter);
633 if (ok != ITER_DONE) {
634 strbuf_addf(err, "unable to rewrite packed-refs file: "
635 "error iterating over old contents");
639 if (rename_tempfile(&refs->tempfile, refs->path)) {
640 strbuf_addf(err, "error replacing %s: %s",
641 refs->path, strerror(errno));
649 delete_tempfile(&refs->tempfile);
652 rollback_lock_file(&refs->lock);
653 release_packed_ref_cache(packed_ref_cache);
658 * Rollback the lockfile for the packed-refs file, and discard the
659 * in-memory packed reference cache. (The packed-refs file will be
660 * read anew if it is needed again after this function is called.)
662 static void rollback_packed_refs(struct packed_ref_store *refs)
664 struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
666 packed_assert_main_repository(refs, "rollback_packed_refs");
668 if (!is_lock_file_locked(&refs->lock))
669 die("BUG: packed-refs not locked");
670 rollback_lock_file(&refs->lock);
671 release_packed_ref_cache(packed_ref_cache);
672 clear_packed_ref_cache(refs);
676 * Rewrite the packed-refs file, omitting any refs listed in
677 * 'refnames'. On error, leave packed-refs unchanged, write an error
678 * message to 'err', and return a nonzero value.
680 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
682 int repack_without_refs(struct ref_store *ref_store,
683 struct string_list *refnames, struct strbuf *err)
685 struct packed_ref_store *refs =
686 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
687 "repack_without_refs");
688 struct ref_dir *packed;
689 struct string_list_item *refname;
690 int needs_repacking = 0, removed = 0;
692 packed_assert_main_repository(refs, "repack_without_refs");
695 /* Look for a packed ref */
696 for_each_string_list_item(refname, refnames) {
697 if (get_packed_ref(refs, refname->string)) {
703 /* Avoid locking if we have nothing to do */
704 if (!needs_repacking)
705 return 0; /* no refname exists in packed refs */
707 if (packed_refs_lock(&refs->base, 0, err))
710 packed = get_packed_refs(refs);
712 /* Remove refnames from the cache */
713 for_each_string_list_item(refname, refnames)
714 if (remove_entry_from_dir(packed, refname->string) != -1)
718 * All packed entries disappeared while we were
719 * acquiring the lock.
721 rollback_packed_refs(refs);
725 /* Write what remains */
726 return commit_packed_refs(&refs->base, err);
729 static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
735 static int packed_transaction_prepare(struct ref_store *ref_store,
736 struct ref_transaction *transaction,
739 die("BUG: not implemented yet");
742 static int packed_transaction_abort(struct ref_store *ref_store,
743 struct ref_transaction *transaction,
746 die("BUG: not implemented yet");
749 static int packed_transaction_finish(struct ref_store *ref_store,
750 struct ref_transaction *transaction,
753 die("BUG: not implemented yet");
756 static int packed_initial_transaction_commit(struct ref_store *ref_store,
757 struct ref_transaction *transaction,
760 return ref_transaction_commit(transaction, err);
763 static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
764 struct string_list *refnames, unsigned int flags)
766 die("BUG: not implemented yet");
769 static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
772 * Packed refs are already packed. It might be that loose refs
773 * are packed *into* a packed refs store, but that is done by
774 * updating the packed references via a transaction.
779 static int packed_create_symref(struct ref_store *ref_store,
780 const char *refname, const char *target,
783 die("BUG: packed reference store does not support symrefs");
786 static int packed_rename_ref(struct ref_store *ref_store,
787 const char *oldrefname, const char *newrefname,
790 die("BUG: packed reference store does not support renaming references");
793 static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
795 return empty_ref_iterator_begin();
798 static int packed_for_each_reflog_ent(struct ref_store *ref_store,
800 each_reflog_ent_fn fn, void *cb_data)
805 static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
807 each_reflog_ent_fn fn,
813 static int packed_reflog_exists(struct ref_store *ref_store,
819 static int packed_create_reflog(struct ref_store *ref_store,
820 const char *refname, int force_create,
823 die("BUG: packed reference store does not support reflogs");
826 static int packed_delete_reflog(struct ref_store *ref_store,
832 static int packed_reflog_expire(struct ref_store *ref_store,
833 const char *refname, const unsigned char *sha1,
835 reflog_expiry_prepare_fn prepare_fn,
836 reflog_expiry_should_prune_fn should_prune_fn,
837 reflog_expiry_cleanup_fn cleanup_fn,
838 void *policy_cb_data)
843 struct ref_storage_be refs_be_packed = {
846 packed_ref_store_create,
848 packed_transaction_prepare,
849 packed_transaction_finish,
850 packed_transaction_abort,
851 packed_initial_transaction_commit,
855 packed_create_symref,
859 packed_ref_iterator_begin,
862 packed_reflog_iterator_begin,
863 packed_for_each_reflog_ent,
864 packed_for_each_reflog_ent_reverse,
865 packed_reflog_exists,
866 packed_create_reflog,
867 packed_delete_reflog,