4 #include "refs-internal.h"
6 #include "packed-backend.h"
7 #include "../iterator.h"
8 #include "../lockfile.h"
10 struct packed_ref_cache
{
11 struct ref_cache
*cache
;
14 * Count of references to the data structure in this instance,
15 * including the pointer from files_ref_store::packed if any.
16 * The data will not be freed as long as the reference count
19 unsigned int referrers
;
21 /* The metadata from when this packed-refs cache was read */
22 struct stat_validity validity
;
26 * Increment the reference count of *packed_refs.
28 static void acquire_packed_ref_cache(struct packed_ref_cache
*packed_refs
)
30 packed_refs
->referrers
++;
34 * Decrease the reference count of *packed_refs. If it goes to zero,
35 * free *packed_refs and return true; otherwise return false.
37 static int release_packed_ref_cache(struct packed_ref_cache
*packed_refs
)
39 if (!--packed_refs
->referrers
) {
40 free_ref_cache(packed_refs
->cache
);
41 stat_validity_clear(&packed_refs
->validity
);
50 * A container for `packed-refs`-related data. It is not (yet) a
53 struct packed_ref_store
{
54 struct ref_store base
;
56 unsigned int store_flags
;
58 /* The path of the "packed-refs" file: */
62 * A cache of the values read from the `packed-refs` file, if
63 * it might still be current; otherwise, NULL.
65 struct packed_ref_cache
*cache
;
68 * Lock used for the "packed-refs" file. Note that this (and
69 * thus the enclosing `packed_ref_store`) must not be freed.
71 struct lock_file lock
;
74 * Temporary file used when rewriting new contents to the
75 * "packed-refs" file. Note that this (and thus the enclosing
76 * `packed_ref_store`) must not be freed.
78 struct tempfile tempfile
;
81 struct ref_store
*packed_ref_store_create(const char *path
,
82 unsigned int store_flags
)
84 struct packed_ref_store
*refs
= xcalloc(1, sizeof(*refs
));
85 struct ref_store
*ref_store
= (struct ref_store
*)refs
;
87 base_ref_store_init(ref_store
, &refs_be_packed
);
88 refs
->store_flags
= store_flags
;
90 refs
->path
= xstrdup(path
);
95 * Die if refs is not the main ref store. caller is used in any
96 * necessary error messages.
98 static void packed_assert_main_repository(struct packed_ref_store
*refs
,
101 if (refs
->store_flags
& REF_STORE_MAIN
)
104 die("BUG: operation %s only allowed for main ref store", caller
);
108 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
109 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
110 * support at least the flags specified in `required_flags`. `caller`
111 * is used in any necessary error messages.
113 static struct packed_ref_store
*packed_downcast(struct ref_store
*ref_store
,
114 unsigned int required_flags
,
117 struct packed_ref_store
*refs
;
119 if (ref_store
->be
!= &refs_be_packed
)
120 die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
121 ref_store
->be
->name
, caller
);
123 refs
= (struct packed_ref_store
*)ref_store
;
125 if ((refs
->store_flags
& required_flags
) != required_flags
)
126 die("BUG: unallowed operation (%s), requires %x, has %x\n",
127 caller
, required_flags
, refs
->store_flags
);
132 static void clear_packed_ref_cache(struct packed_ref_store
*refs
)
135 struct packed_ref_cache
*cache
= refs
->cache
;
138 release_packed_ref_cache(cache
);
142 /* The length of a peeled reference line in packed-refs, including EOL: */
143 #define PEELED_LINE_LENGTH 42
146 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
147 * Return a pointer to the refname within the line (null-terminated),
148 * or NULL if there was a problem.
150 static const char *parse_ref_line(struct strbuf
*line
, struct object_id
*oid
)
154 if (parse_oid_hex(line
->buf
, oid
, &ref
) < 0)
156 if (!isspace(*ref
++))
162 if (line
->buf
[line
->len
- 1] != '\n')
164 line
->buf
[--line
->len
] = 0;
170 * Read from `packed_refs_file` into a newly-allocated
171 * `packed_ref_cache` and return it. The return value will already
172 * have its reference count incremented.
174 * A comment line of the form "# pack-refs with: " may contain zero or
175 * more traits. We interpret the traits as follows:
179 * Probably no references are peeled. But if the file contains a
180 * peeled value for a reference, we will use it.
184 * References under "refs/tags/", if they *can* be peeled, *are*
185 * peeled in this file. References outside of "refs/tags/" are
186 * probably not peeled even if they could have been, but if we find
187 * a peeled value for such a reference we will use it.
191 * All references in the file that can be peeled are peeled.
192 * Inversely (and this is more important), any references in the
193 * file for which no peeled value is recorded is not peelable. This
194 * trait should typically be written alongside "peeled" for
195 * compatibility with older clients, but we do not require it
196 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
198 static struct packed_ref_cache
*read_packed_refs(const char *packed_refs_file
)
201 struct packed_ref_cache
*packed_refs
= xcalloc(1, sizeof(*packed_refs
));
202 struct ref_entry
*last
= NULL
;
203 struct strbuf line
= STRBUF_INIT
;
204 enum { PEELED_NONE
, PEELED_TAGS
, PEELED_FULLY
} peeled
= PEELED_NONE
;
207 acquire_packed_ref_cache(packed_refs
);
208 packed_refs
->cache
= create_ref_cache(NULL
, NULL
);
209 packed_refs
->cache
->root
->flag
&= ~REF_INCOMPLETE
;
211 f
= fopen(packed_refs_file
, "r");
213 if (errno
== ENOENT
) {
215 * This is OK; it just means that no
216 * "packed-refs" file has been written yet,
217 * which is equivalent to it being empty.
221 die_errno("couldn't read %s", packed_refs_file
);
225 stat_validity_update(&packed_refs
->validity
, fileno(f
));
227 dir
= get_ref_dir(packed_refs
->cache
->root
);
228 while (strbuf_getwholeline(&line
, f
, '\n') != EOF
) {
229 struct object_id oid
;
233 if (!line
.len
|| line
.buf
[line
.len
- 1] != '\n')
234 die("unterminated line in %s: %s", packed_refs_file
, line
.buf
);
236 if (skip_prefix(line
.buf
, "# pack-refs with:", &traits
)) {
237 if (strstr(traits
, " fully-peeled "))
238 peeled
= PEELED_FULLY
;
239 else if (strstr(traits
, " peeled "))
240 peeled
= PEELED_TAGS
;
241 /* perhaps other traits later as well */
245 refname
= parse_ref_line(&line
, &oid
);
247 int flag
= REF_ISPACKED
;
249 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
250 if (!refname_is_safe(refname
))
251 die("packed refname is dangerous: %s", refname
);
253 flag
|= REF_BAD_NAME
| REF_ISBROKEN
;
255 last
= create_ref_entry(refname
, &oid
, flag
);
256 if (peeled
== PEELED_FULLY
||
257 (peeled
== PEELED_TAGS
&& starts_with(refname
, "refs/tags/")))
258 last
->flag
|= REF_KNOWS_PEELED
;
259 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
;
273 strbuf_setlen(&line
, line
.len
- 1);
274 die("unexpected line in %s: %s", packed_refs_file
, line
.buf
);
279 strbuf_release(&line
);
285 * Check that the packed refs cache (if any) still reflects the
286 * contents of the file. If not, clear the cache.
288 static void validate_packed_ref_cache(struct packed_ref_store
*refs
)
291 !stat_validity_check(&refs
->cache
->validity
, refs
->path
))
292 clear_packed_ref_cache(refs
);
296 * Get the packed_ref_cache for the specified packed_ref_store,
297 * creating and populating it if it hasn't been read before or if the
298 * file has been changed (according to its `validity` field) since it
299 * was last read. On the other hand, if we hold the lock, then assume
300 * that the file hasn't been changed out from under us, so skip the
301 * extra `stat()` call in `stat_validity_check()`.
303 static struct packed_ref_cache
*get_packed_ref_cache(struct packed_ref_store
*refs
)
305 if (!is_lock_file_locked(&refs
->lock
))
306 validate_packed_ref_cache(refs
);
309 refs
->cache
= read_packed_refs(refs
->path
);
314 static struct ref_dir
*get_packed_ref_dir(struct packed_ref_cache
*packed_ref_cache
)
316 return get_ref_dir(packed_ref_cache
->cache
->root
);
319 static struct ref_dir
*get_packed_refs(struct packed_ref_store
*refs
)
321 return get_packed_ref_dir(get_packed_ref_cache(refs
));
325 * Add or overwrite a reference in the in-memory packed reference
326 * cache. This may only be called while the packed-refs file is locked
327 * (see packed_refs_lock()). To actually write the packed-refs file,
328 * call commit_packed_refs().
330 void add_packed_ref(struct ref_store
*ref_store
,
331 const char *refname
, const struct object_id
*oid
)
333 struct packed_ref_store
*refs
=
334 packed_downcast(ref_store
, REF_STORE_WRITE
,
336 struct ref_dir
*packed_refs
;
337 struct ref_entry
*packed_entry
;
339 if (!is_lock_file_locked(&refs
->lock
))
340 die("BUG: packed refs not locked");
342 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
))
343 die("Reference has invalid format: '%s'", refname
);
345 packed_refs
= get_packed_refs(refs
);
346 packed_entry
= find_ref_entry(packed_refs
, refname
);
348 /* Overwrite the existing entry: */
349 oidcpy(&packed_entry
->u
.value
.oid
, oid
);
350 packed_entry
->flag
= REF_ISPACKED
;
351 oidclr(&packed_entry
->u
.value
.peeled
);
353 packed_entry
= create_ref_entry(refname
, oid
, REF_ISPACKED
);
354 add_ref_entry(packed_refs
, packed_entry
);
359 * Return the ref_entry for the given refname from the packed
360 * references. If it does not exist, return NULL.
362 static struct ref_entry
*get_packed_ref(struct packed_ref_store
*refs
,
365 return find_ref_entry(get_packed_refs(refs
), refname
);
368 static int packed_read_raw_ref(struct ref_store
*ref_store
,
369 const char *refname
, unsigned char *sha1
,
370 struct strbuf
*referent
, unsigned int *type
)
372 struct packed_ref_store
*refs
=
373 packed_downcast(ref_store
, REF_STORE_READ
, "read_raw_ref");
375 struct ref_entry
*entry
;
379 entry
= get_packed_ref(refs
, refname
);
385 hashcpy(sha1
, entry
->u
.value
.oid
.hash
);
386 *type
= REF_ISPACKED
;
390 static int packed_peel_ref(struct ref_store
*ref_store
,
391 const char *refname
, unsigned char *sha1
)
393 struct packed_ref_store
*refs
=
394 packed_downcast(ref_store
, REF_STORE_READ
| REF_STORE_ODB
,
396 struct ref_entry
*r
= get_packed_ref(refs
, refname
);
398 if (!r
|| peel_entry(r
, 0))
401 hashcpy(sha1
, r
->u
.value
.peeled
.hash
);
405 struct packed_ref_iterator
{
406 struct ref_iterator base
;
408 struct packed_ref_cache
*cache
;
409 struct ref_iterator
*iter0
;
413 static int packed_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
415 struct packed_ref_iterator
*iter
=
416 (struct packed_ref_iterator
*)ref_iterator
;
419 while ((ok
= ref_iterator_advance(iter
->iter0
)) == ITER_OK
) {
420 if (iter
->flags
& DO_FOR_EACH_PER_WORKTREE_ONLY
&&
421 ref_type(iter
->iter0
->refname
) != REF_TYPE_PER_WORKTREE
)
424 if (!(iter
->flags
& DO_FOR_EACH_INCLUDE_BROKEN
) &&
425 !ref_resolves_to_object(iter
->iter0
->refname
,
430 iter
->base
.refname
= iter
->iter0
->refname
;
431 iter
->base
.oid
= iter
->iter0
->oid
;
432 iter
->base
.flags
= iter
->iter0
->flags
;
437 if (ref_iterator_abort(ref_iterator
) != ITER_DONE
)
443 static int packed_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
444 struct object_id
*peeled
)
446 struct packed_ref_iterator
*iter
=
447 (struct packed_ref_iterator
*)ref_iterator
;
449 return ref_iterator_peel(iter
->iter0
, peeled
);
452 static int packed_ref_iterator_abort(struct ref_iterator
*ref_iterator
)
454 struct packed_ref_iterator
*iter
=
455 (struct packed_ref_iterator
*)ref_iterator
;
459 ok
= ref_iterator_abort(iter
->iter0
);
461 release_packed_ref_cache(iter
->cache
);
462 base_ref_iterator_free(ref_iterator
);
466 static struct ref_iterator_vtable packed_ref_iterator_vtable
= {
467 packed_ref_iterator_advance
,
468 packed_ref_iterator_peel
,
469 packed_ref_iterator_abort
472 static struct ref_iterator
*packed_ref_iterator_begin(
473 struct ref_store
*ref_store
,
474 const char *prefix
, unsigned int flags
)
476 struct packed_ref_store
*refs
;
477 struct packed_ref_iterator
*iter
;
478 struct ref_iterator
*ref_iterator
;
479 unsigned int required_flags
= REF_STORE_READ
;
481 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
))
482 required_flags
|= REF_STORE_ODB
;
483 refs
= packed_downcast(ref_store
, required_flags
, "ref_iterator_begin");
485 iter
= xcalloc(1, sizeof(*iter
));
486 ref_iterator
= &iter
->base
;
487 base_ref_iterator_init(ref_iterator
, &packed_ref_iterator_vtable
);
490 * Note that get_packed_ref_cache() internally checks whether
491 * the packed-ref cache is up to date with what is on disk,
492 * and re-reads it if not.
495 iter
->cache
= get_packed_ref_cache(refs
);
496 acquire_packed_ref_cache(iter
->cache
);
497 iter
->iter0
= cache_ref_iterator_begin(iter
->cache
->cache
, prefix
, 0);
505 * Write an entry to the packed-refs file for the specified refname.
506 * If peeled is non-NULL, write it as the entry's peeled value. On
507 * error, return a nonzero value and leave errno set at the value left
508 * by the failing call to `fprintf()`.
510 static int write_packed_entry(FILE *fh
, const char *refname
,
511 const unsigned char *sha1
,
512 const unsigned char *peeled
)
514 if (fprintf(fh
, "%s %s\n", sha1_to_hex(sha1
), refname
) < 0 ||
515 (peeled
&& fprintf(fh
, "^%s\n", sha1_to_hex(peeled
)) < 0))
521 int packed_refs_lock(struct ref_store
*ref_store
, int flags
, struct strbuf
*err
)
523 struct packed_ref_store
*refs
=
524 packed_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_MAIN
,
526 static int timeout_configured
= 0;
527 static int timeout_value
= 1000;
529 if (!timeout_configured
) {
530 git_config_get_int("core.packedrefstimeout", &timeout_value
);
531 timeout_configured
= 1;
535 * Note that we close the lockfile immediately because we
536 * don't write new content to it, but rather to a separate
539 if (hold_lock_file_for_update_timeout(
542 flags
, timeout_value
) < 0) {
543 unable_to_lock_message(refs
->path
, errno
, err
);
547 if (close_lock_file(&refs
->lock
)) {
548 strbuf_addf(err
, "unable to close %s: %s", refs
->path
, strerror(errno
));
553 * Now that we hold the `packed-refs` lock, make sure that our
554 * cache matches the current version of the file. Normally
555 * `get_packed_ref_cache()` does that for us, but that
556 * function assumes that when the file is locked, any existing
557 * cache is still valid. We've just locked the file, but it
558 * might have changed the moment *before* we locked it.
560 validate_packed_ref_cache(refs
);
563 * Now make sure that the packed-refs file as it exists in the
564 * locked state is loaded into the cache:
566 get_packed_ref_cache(refs
);
570 void packed_refs_unlock(struct ref_store
*ref_store
)
572 struct packed_ref_store
*refs
= packed_downcast(
574 REF_STORE_READ
| REF_STORE_WRITE
,
575 "packed_refs_unlock");
577 if (!is_lock_file_locked(&refs
->lock
))
578 die("BUG: packed_refs_unlock() called when not locked");
579 rollback_lock_file(&refs
->lock
);
582 int packed_refs_is_locked(struct ref_store
*ref_store
)
584 struct packed_ref_store
*refs
= packed_downcast(
586 REF_STORE_READ
| REF_STORE_WRITE
,
587 "packed_refs_is_locked");
589 return is_lock_file_locked(&refs
->lock
);
593 * The packed-refs header line that we write out. Perhaps other
594 * traits will be added later. The trailing space is required.
596 static const char PACKED_REFS_HEADER
[] =
597 "# pack-refs with: peeled fully-peeled \n";
600 * Write the current version of the packed refs cache from memory to
601 * disk. The packed-refs file must already be locked for writing (see
602 * packed_refs_lock()). Return zero on success. On errors, rollback
603 * the lockfile, write an error message to `err`, and return a nonzero
606 int commit_packed_refs(struct ref_store
*ref_store
, struct strbuf
*err
)
608 struct packed_ref_store
*refs
=
609 packed_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_MAIN
,
610 "commit_packed_refs");
611 struct packed_ref_cache
*packed_ref_cache
=
612 get_packed_ref_cache(refs
);
615 struct strbuf sb
= STRBUF_INIT
;
617 struct ref_iterator
*iter
;
618 char *packed_refs_path
;
620 if (!is_lock_file_locked(&refs
->lock
))
621 die("BUG: commit_packed_refs() called when unlocked");
624 * If packed-refs is a symlink, we want to overwrite the
625 * symlinked-to file, not the symlink itself. Also, put the
626 * staging file next to it:
628 packed_refs_path
= get_locked_file_path(&refs
->lock
);
629 strbuf_addf(&sb
, "%s.new", packed_refs_path
);
630 if (create_tempfile(&refs
->tempfile
, sb
.buf
) < 0) {
631 strbuf_addf(err
, "unable to create file %s: %s",
632 sb
.buf
, strerror(errno
));
638 out
= fdopen_tempfile(&refs
->tempfile
, "w");
640 strbuf_addf(err
, "unable to fdopen packed-refs tempfile: %s",
645 if (fprintf(out
, "%s", PACKED_REFS_HEADER
) < 0) {
646 strbuf_addf(err
, "error writing to %s: %s",
647 get_tempfile_path(&refs
->tempfile
), strerror(errno
));
651 iter
= cache_ref_iterator_begin(packed_ref_cache
->cache
, NULL
, 0);
652 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
653 struct object_id peeled
;
654 int peel_error
= ref_iterator_peel(iter
, &peeled
);
656 if (write_packed_entry(out
, iter
->refname
, iter
->oid
->hash
,
657 peel_error
? NULL
: peeled
.hash
)) {
658 strbuf_addf(err
, "error writing to %s: %s",
659 get_tempfile_path(&refs
->tempfile
),
661 ref_iterator_abort(iter
);
666 if (ok
!= ITER_DONE
) {
667 strbuf_addf(err
, "unable to rewrite packed-refs file: "
668 "error iterating over old contents");
672 if (rename_tempfile(&refs
->tempfile
, packed_refs_path
)) {
673 strbuf_addf(err
, "error replacing %s: %s",
674 refs
->path
, strerror(errno
));
682 delete_tempfile(&refs
->tempfile
);
685 free(packed_refs_path
);
690 * Rewrite the packed-refs file, omitting any refs listed in
691 * 'refnames'. On error, leave packed-refs unchanged, write an error
692 * message to 'err', and return a nonzero value. The packed refs lock
693 * must be held when calling this function; it will still be held when
694 * the function returns.
696 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
698 int repack_without_refs(struct ref_store
*ref_store
,
699 struct string_list
*refnames
, struct strbuf
*err
)
701 struct packed_ref_store
*refs
=
702 packed_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_MAIN
,
703 "repack_without_refs");
704 struct ref_dir
*packed
;
705 struct string_list_item
*refname
;
706 int needs_repacking
= 0, removed
= 0;
708 packed_assert_main_repository(refs
, "repack_without_refs");
711 if (!is_lock_file_locked(&refs
->lock
))
712 die("BUG: repack_without_refs called without holding lock");
714 /* Look for a packed ref */
715 for_each_string_list_item(refname
, refnames
) {
716 if (get_packed_ref(refs
, refname
->string
)) {
722 /* Avoid locking if we have nothing to do */
723 if (!needs_repacking
)
724 return 0; /* no refname exists in packed refs */
726 packed
= get_packed_refs(refs
);
728 /* Remove refnames from the cache */
729 for_each_string_list_item(refname
, refnames
)
730 if (remove_entry_from_dir(packed
, refname
->string
) != -1)
734 * All packed entries disappeared while we were
735 * acquiring the lock.
737 clear_packed_ref_cache(refs
);
741 /* Write what remains */
742 return commit_packed_refs(&refs
->base
, err
);
745 static int packed_init_db(struct ref_store
*ref_store
, struct strbuf
*err
)
752 * Write the packed-refs from the cache to the packed-refs tempfile,
753 * incorporating any changes from `updates`. `updates` must be a
754 * sorted string list whose keys are the refnames and whose util
755 * values are `struct ref_update *`. On error, rollback the tempfile,
756 * write an error message to `err`, and return a nonzero value.
758 * The packfile must be locked before calling this function and will
759 * remain locked when it is done.
761 static int write_with_updates(struct packed_ref_store
*refs
,
762 struct string_list
*updates
,
765 struct ref_iterator
*iter
= NULL
;
769 struct strbuf sb
= STRBUF_INIT
;
770 char *packed_refs_path
;
772 if (!is_lock_file_locked(&refs
->lock
))
773 die("BUG: write_with_updates() called while unlocked");
776 * If packed-refs is a symlink, we want to overwrite the
777 * symlinked-to file, not the symlink itself. Also, put the
778 * staging file next to it:
780 packed_refs_path
= get_locked_file_path(&refs
->lock
);
781 strbuf_addf(&sb
, "%s.new", packed_refs_path
);
782 free(packed_refs_path
);
783 if (create_tempfile(&refs
->tempfile
, sb
.buf
) < 0) {
784 strbuf_addf(err
, "unable to create file %s: %s",
785 sb
.buf
, strerror(errno
));
791 out
= fdopen_tempfile(&refs
->tempfile
, "w");
793 strbuf_addf(err
, "unable to fdopen packed-refs tempfile: %s",
798 if (fprintf(out
, "%s", PACKED_REFS_HEADER
) < 0)
802 * We iterate in parallel through the current list of refs and
803 * the list of updates, processing an entry from at least one
804 * of the lists each time through the loop. When the current
805 * list of refs is exhausted, set iter to NULL. When the list
806 * of updates is exhausted, leave i set to updates->nr.
808 iter
= packed_ref_iterator_begin(&refs
->base
, "",
809 DO_FOR_EACH_INCLUDE_BROKEN
);
810 if ((ok
= ref_iterator_advance(iter
)) != ITER_OK
)
815 while (iter
|| i
< updates
->nr
) {
816 struct ref_update
*update
= NULL
;
819 if (i
>= updates
->nr
) {
822 update
= updates
->items
[i
].util
;
827 cmp
= strcmp(iter
->refname
, update
->refname
);
832 * There is both an old value and an update
833 * for this reference. Check the old value if
836 if ((update
->flags
& REF_HAVE_OLD
)) {
837 if (is_null_oid(&update
->old_oid
)) {
838 strbuf_addf(err
, "cannot update ref '%s': "
839 "reference already exists",
842 } else if (oidcmp(&update
->old_oid
, iter
->oid
)) {
843 strbuf_addf(err
, "cannot update ref '%s': "
844 "is at %s but expected %s",
846 oid_to_hex(iter
->oid
),
847 oid_to_hex(&update
->old_oid
));
852 /* Now figure out what to use for the new value: */
853 if ((update
->flags
& REF_HAVE_NEW
)) {
855 * The update takes precedence. Skip
856 * the iterator over the unneeded
859 if ((ok
= ref_iterator_advance(iter
)) != ITER_OK
)
864 * The update doesn't actually want to
865 * change anything. We're done with it.
870 } else if (cmp
> 0) {
872 * There is no old value but there is an
873 * update for this reference. Make sure that
874 * the update didn't expect an existing value:
876 if ((update
->flags
& REF_HAVE_OLD
) &&
877 !is_null_oid(&update
->old_oid
)) {
878 strbuf_addf(err
, "cannot update ref '%s': "
879 "reference is missing but expected %s",
881 oid_to_hex(&update
->old_oid
));
887 /* Pass the old reference through. */
889 struct object_id peeled
;
890 int peel_error
= ref_iterator_peel(iter
, &peeled
);
892 if (write_packed_entry(out
, iter
->refname
,
894 peel_error
? NULL
: peeled
.hash
))
897 if ((ok
= ref_iterator_advance(iter
)) != ITER_OK
)
899 } else if (is_null_oid(&update
->new_oid
)) {
901 * The update wants to delete the reference,
902 * and the reference either didn't exist or we
903 * have already skipped it. So we're done with
904 * the update (and don't have to write
909 struct object_id peeled
;
910 int peel_error
= peel_object(update
->new_oid
.hash
,
913 if (write_packed_entry(out
, update
->refname
,
914 update
->new_oid
.hash
,
915 peel_error
? NULL
: peeled
.hash
))
922 if (ok
!= ITER_DONE
) {
923 strbuf_addf(err
, "unable to write packed-refs file: "
924 "error iterating over old contents");
928 if (close_tempfile(&refs
->tempfile
)) {
929 strbuf_addf(err
, "error closing file %s: %s",
930 get_tempfile_path(&refs
->tempfile
),
939 strbuf_addf(err
, "error writing to %s: %s",
940 get_tempfile_path(&refs
->tempfile
), strerror(errno
));
944 ref_iterator_abort(iter
);
946 delete_tempfile(&refs
->tempfile
);
950 struct packed_transaction_backend_data
{
951 /* True iff the transaction owns the packed-refs lock. */
954 struct string_list updates
;
957 static void packed_transaction_cleanup(struct packed_ref_store
*refs
,
958 struct ref_transaction
*transaction
)
960 struct packed_transaction_backend_data
*data
= transaction
->backend_data
;
963 string_list_clear(&data
->updates
, 0);
965 if (is_tempfile_active(&refs
->tempfile
))
966 delete_tempfile(&refs
->tempfile
);
968 if (data
->own_lock
&& is_lock_file_locked(&refs
->lock
)) {
969 packed_refs_unlock(&refs
->base
);
974 transaction
->backend_data
= NULL
;
977 transaction
->state
= REF_TRANSACTION_CLOSED
;
980 static int packed_transaction_prepare(struct ref_store
*ref_store
,
981 struct ref_transaction
*transaction
,
984 struct packed_ref_store
*refs
= packed_downcast(
986 REF_STORE_READ
| REF_STORE_WRITE
| REF_STORE_ODB
,
987 "ref_transaction_prepare");
988 struct packed_transaction_backend_data
*data
;
990 int ret
= TRANSACTION_GENERIC_ERROR
;
993 * Note that we *don't* skip transactions with zero updates,
994 * because such a transaction might be executed for the side
995 * effect of ensuring that all of the references are peeled.
996 * If the caller wants to optimize away empty transactions, it
997 * should do so itself.
1000 data
= xcalloc(1, sizeof(*data
));
1001 string_list_init(&data
->updates
, 0);
1003 transaction
->backend_data
= data
;
1006 * Stick the updates in a string list by refname so that we
1009 for (i
= 0; i
< transaction
->nr
; i
++) {
1010 struct ref_update
*update
= transaction
->updates
[i
];
1011 struct string_list_item
*item
=
1012 string_list_append(&data
->updates
, update
->refname
);
1014 /* Store a pointer to update in item->util: */
1015 item
->util
= update
;
1017 string_list_sort(&data
->updates
);
1019 if (ref_update_reject_duplicates(&data
->updates
, err
))
1022 if (!is_lock_file_locked(&refs
->lock
)) {
1023 if (packed_refs_lock(ref_store
, 0, err
))
1028 if (write_with_updates(refs
, &data
->updates
, err
))
1031 transaction
->state
= REF_TRANSACTION_PREPARED
;
1035 packed_transaction_cleanup(refs
, transaction
);
1039 static int packed_transaction_abort(struct ref_store
*ref_store
,
1040 struct ref_transaction
*transaction
,
1043 struct packed_ref_store
*refs
= packed_downcast(
1045 REF_STORE_READ
| REF_STORE_WRITE
| REF_STORE_ODB
,
1046 "ref_transaction_abort");
1048 packed_transaction_cleanup(refs
, transaction
);
1052 static int packed_transaction_finish(struct ref_store
*ref_store
,
1053 struct ref_transaction
*transaction
,
1056 struct packed_ref_store
*refs
= packed_downcast(
1058 REF_STORE_READ
| REF_STORE_WRITE
| REF_STORE_ODB
,
1059 "ref_transaction_finish");
1060 int ret
= TRANSACTION_GENERIC_ERROR
;
1061 char *packed_refs_path
;
1063 packed_refs_path
= get_locked_file_path(&refs
->lock
);
1064 if (rename_tempfile(&refs
->tempfile
, packed_refs_path
)) {
1065 strbuf_addf(err
, "error replacing %s: %s",
1066 refs
->path
, strerror(errno
));
1070 clear_packed_ref_cache(refs
);
1074 free(packed_refs_path
);
1075 packed_transaction_cleanup(refs
, transaction
);
1079 static int packed_initial_transaction_commit(struct ref_store
*ref_store
,
1080 struct ref_transaction
*transaction
,
1083 return ref_transaction_commit(transaction
, err
);
1086 static int packed_delete_refs(struct ref_store
*ref_store
, const char *msg
,
1087 struct string_list
*refnames
, unsigned int flags
)
1089 die("BUG: not implemented yet");
1092 static int packed_pack_refs(struct ref_store
*ref_store
, unsigned int flags
)
1095 * Packed refs are already packed. It might be that loose refs
1096 * are packed *into* a packed refs store, but that is done by
1097 * updating the packed references via a transaction.
1102 static int packed_create_symref(struct ref_store
*ref_store
,
1103 const char *refname
, const char *target
,
1106 die("BUG: packed reference store does not support symrefs");
1109 static int packed_rename_ref(struct ref_store
*ref_store
,
1110 const char *oldrefname
, const char *newrefname
,
1113 die("BUG: packed reference store does not support renaming references");
1116 static struct ref_iterator
*packed_reflog_iterator_begin(struct ref_store
*ref_store
)
1118 return empty_ref_iterator_begin();
1121 static int packed_for_each_reflog_ent(struct ref_store
*ref_store
,
1122 const char *refname
,
1123 each_reflog_ent_fn fn
, void *cb_data
)
1128 static int packed_for_each_reflog_ent_reverse(struct ref_store
*ref_store
,
1129 const char *refname
,
1130 each_reflog_ent_fn fn
,
1136 static int packed_reflog_exists(struct ref_store
*ref_store
,
1137 const char *refname
)
1142 static int packed_create_reflog(struct ref_store
*ref_store
,
1143 const char *refname
, int force_create
,
1146 die("BUG: packed reference store does not support reflogs");
1149 static int packed_delete_reflog(struct ref_store
*ref_store
,
1150 const char *refname
)
1155 static int packed_reflog_expire(struct ref_store
*ref_store
,
1156 const char *refname
, const unsigned char *sha1
,
1158 reflog_expiry_prepare_fn prepare_fn
,
1159 reflog_expiry_should_prune_fn should_prune_fn
,
1160 reflog_expiry_cleanup_fn cleanup_fn
,
1161 void *policy_cb_data
)
1166 struct ref_storage_be refs_be_packed
= {
1169 packed_ref_store_create
,
1171 packed_transaction_prepare
,
1172 packed_transaction_finish
,
1173 packed_transaction_abort
,
1174 packed_initial_transaction_commit
,
1178 packed_create_symref
,
1182 packed_ref_iterator_begin
,
1183 packed_read_raw_ref
,
1185 packed_reflog_iterator_begin
,
1186 packed_for_each_reflog_ent
,
1187 packed_for_each_reflog_ent_reverse
,
1188 packed_reflog_exists
,
1189 packed_create_reflog
,
1190 packed_delete_reflog
,
1191 packed_reflog_expire