4 #include "refs-internal.h"
6 #include "packed-backend.h"
7 #include "../iterator.h"
8 #include "../dir-iterator.h"
9 #include "../lockfile.h"
10 #include "../object.h"
16 struct object_id old_oid;
20 * Future: need to be in "struct repository"
21 * when doing a full libification.
23 struct files_ref_store {
24 struct ref_store base;
25 unsigned int store_flags;
30 struct ref_cache *loose;
32 struct ref_store *packed_ref_store;
35 static void clear_loose_ref_cache(struct files_ref_store *refs)
38 free_ref_cache(refs->loose);
44 * Create a new submodule ref cache and add it to the internal
47 static struct ref_store *files_ref_store_create(const char *gitdir,
50 struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
51 struct ref_store *ref_store = (struct ref_store *)refs;
52 struct strbuf sb = STRBUF_INIT;
54 base_ref_store_init(ref_store, &refs_be_files);
55 refs->store_flags = flags;
57 refs->gitdir = xstrdup(gitdir);
58 get_common_dir_noenv(&sb, gitdir);
59 refs->gitcommondir = strbuf_detach(&sb, NULL);
60 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
61 refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
68 * Die if refs is not the main ref store. caller is used in any
69 * necessary error messages.
71 static void files_assert_main_repository(struct files_ref_store *refs,
74 if (refs->store_flags & REF_STORE_MAIN)
77 die("BUG: operation %s only allowed for main ref store", caller);
81 * Downcast ref_store to files_ref_store. Die if ref_store is not a
82 * files_ref_store. required_flags is compared with ref_store's
83 * store_flags to ensure the ref_store has all required capabilities.
84 * "caller" is used in any necessary error messages.
86 static struct files_ref_store *files_downcast(struct ref_store *ref_store,
87 unsigned int required_flags,
90 struct files_ref_store *refs;
92 if (ref_store->be != &refs_be_files)
93 die("BUG: ref_store is type \"%s\" not \"files\" in %s",
94 ref_store->be->name, caller);
96 refs = (struct files_ref_store *)ref_store;
98 if ((refs->store_flags & required_flags) != required_flags)
99 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
100 caller, required_flags, refs->store_flags);
105 static void files_reflog_path(struct files_ref_store *refs,
109 switch (ref_type(refname)) {
110 case REF_TYPE_PER_WORKTREE:
111 case REF_TYPE_PSEUDOREF:
112 strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname);
114 case REF_TYPE_NORMAL:
115 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
118 die("BUG: unknown ref type %d of ref %s",
119 ref_type(refname), refname);
123 static void files_ref_path(struct files_ref_store *refs,
127 switch (ref_type(refname)) {
128 case REF_TYPE_PER_WORKTREE:
129 case REF_TYPE_PSEUDOREF:
130 strbuf_addf(sb, "%s/%s", refs->gitdir, refname);
132 case REF_TYPE_NORMAL:
133 strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
136 die("BUG: unknown ref type %d of ref %s",
137 ref_type(refname), refname);
142 * Read the loose references from the namespace dirname into dir
143 * (without recursing). dirname must end with '/'. dir must be the
144 * directory entry corresponding to dirname.
146 static void loose_fill_ref_dir(struct ref_store *ref_store,
147 struct ref_dir *dir, const char *dirname)
149 struct files_ref_store *refs =
150 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
153 int dirnamelen = strlen(dirname);
154 struct strbuf refname;
155 struct strbuf path = STRBUF_INIT;
158 files_ref_path(refs, &path, dirname);
159 path_baselen = path.len;
161 d = opendir(path.buf);
163 strbuf_release(&path);
167 strbuf_init(&refname, dirnamelen + 257);
168 strbuf_add(&refname, dirname, dirnamelen);
170 while ((de = readdir(d)) != NULL) {
171 struct object_id oid;
175 if (de->d_name[0] == '.')
177 if (ends_with(de->d_name, ".lock"))
179 strbuf_addstr(&refname, de->d_name);
180 strbuf_addstr(&path, de->d_name);
181 if (stat(path.buf, &st) < 0) {
182 ; /* silently ignore */
183 } else if (S_ISDIR(st.st_mode)) {
184 strbuf_addch(&refname, '/');
185 add_entry_to_dir(dir,
186 create_dir_entry(dir->cache, refname.buf,
189 if (!refs_resolve_ref_unsafe(&refs->base,
194 flag |= REF_ISBROKEN;
195 } else if (is_null_oid(&oid)) {
197 * It is so astronomically unlikely
198 * that NULL_SHA1 is the SHA-1 of an
199 * actual object that we consider its
200 * appearance in a loose reference
201 * file to be repo corruption
202 * (probably due to a software bug).
204 flag |= REF_ISBROKEN;
207 if (check_refname_format(refname.buf,
208 REFNAME_ALLOW_ONELEVEL)) {
209 if (!refname_is_safe(refname.buf))
210 die("loose refname is dangerous: %s", refname.buf);
212 flag |= REF_BAD_NAME | REF_ISBROKEN;
214 add_entry_to_dir(dir,
215 create_ref_entry(refname.buf, &oid, flag));
217 strbuf_setlen(&refname, dirnamelen);
218 strbuf_setlen(&path, path_baselen);
220 strbuf_release(&refname);
221 strbuf_release(&path);
225 * Manually add refs/bisect, which, being per-worktree, might
226 * not appear in the directory listing for refs/ in the main
229 if (!strcmp(dirname, "refs/")) {
230 int pos = search_ref_dir(dir, "refs/bisect/", 12);
233 struct ref_entry *child_entry = create_dir_entry(
234 dir->cache, "refs/bisect/", 12, 1);
235 add_entry_to_dir(dir, child_entry);
240 static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
244 * Mark the top-level directory complete because we
245 * are about to read the only subdirectory that can
248 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
250 /* We're going to fill the top level ourselves: */
251 refs->loose->root->flag &= ~REF_INCOMPLETE;
254 * Add an incomplete entry for "refs/" (to be filled
257 add_entry_to_dir(get_ref_dir(refs->loose->root),
258 create_dir_entry(refs->loose, "refs/", 5, 1));
263 static int files_read_raw_ref(struct ref_store *ref_store,
264 const char *refname, struct object_id *oid,
265 struct strbuf *referent, unsigned int *type)
267 struct files_ref_store *refs =
268 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
269 struct strbuf sb_contents = STRBUF_INIT;
270 struct strbuf sb_path = STRBUF_INIT;
278 int remaining_retries = 3;
281 strbuf_reset(&sb_path);
283 files_ref_path(refs, &sb_path, refname);
289 * We might have to loop back here to avoid a race
290 * condition: first we lstat() the file, then we try
291 * to read it as a link or as a file. But if somebody
292 * changes the type of the file (file <-> directory
293 * <-> symlink) between the lstat() and reading, then
294 * we don't want to report that as an error but rather
295 * try again starting with the lstat().
297 * We'll keep a count of the retries, though, just to avoid
298 * any confusing situation sending us into an infinite loop.
301 if (remaining_retries-- <= 0)
304 if (lstat(path, &st) < 0) {
307 if (refs_read_raw_ref(refs->packed_ref_store, refname,
308 oid, referent, type)) {
316 /* Follow "normalized" - ie "refs/.." symlinks by hand */
317 if (S_ISLNK(st.st_mode)) {
318 strbuf_reset(&sb_contents);
319 if (strbuf_readlink(&sb_contents, path, 0) < 0) {
320 if (errno == ENOENT || errno == EINVAL)
321 /* inconsistent with lstat; retry */
326 if (starts_with(sb_contents.buf, "refs/") &&
327 !check_refname_format(sb_contents.buf, 0)) {
328 strbuf_swap(&sb_contents, referent);
329 *type |= REF_ISSYMREF;
334 * It doesn't look like a refname; fall through to just
335 * treating it like a non-symlink, and reading whatever it
340 /* Is it a directory? */
341 if (S_ISDIR(st.st_mode)) {
343 * Even though there is a directory where the loose
344 * ref is supposed to be, there could still be a
347 if (refs_read_raw_ref(refs->packed_ref_store, refname,
348 oid, referent, type)) {
357 * Anything else, just open it and try to use it as
360 fd = open(path, O_RDONLY);
362 if (errno == ENOENT && !S_ISLNK(st.st_mode))
363 /* inconsistent with lstat; retry */
368 strbuf_reset(&sb_contents);
369 if (strbuf_read(&sb_contents, fd, 256) < 0) {
370 int save_errno = errno;
376 strbuf_rtrim(&sb_contents);
377 buf = sb_contents.buf;
378 if (starts_with(buf, "ref:")) {
380 while (isspace(*buf))
383 strbuf_reset(referent);
384 strbuf_addstr(referent, buf);
385 *type |= REF_ISSYMREF;
391 * Please note that FETCH_HEAD has additional
392 * data after the sha.
394 if (parse_oid_hex(buf, oid, &p) ||
395 (*p != '\0' && !isspace(*p))) {
396 *type |= REF_ISBROKEN;
405 strbuf_release(&sb_path);
406 strbuf_release(&sb_contents);
411 static void unlock_ref(struct ref_lock *lock)
413 rollback_lock_file(&lock->lk);
414 free(lock->ref_name);
419 * Lock refname, without following symrefs, and set *lock_p to point
420 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
421 * and type similarly to read_raw_ref().
423 * The caller must verify that refname is a "safe" reference name (in
424 * the sense of refname_is_safe()) before calling this function.
426 * If the reference doesn't already exist, verify that refname doesn't
427 * have a D/F conflict with any existing references. extras and skip
428 * are passed to refs_verify_refname_available() for this check.
430 * If mustexist is not set and the reference is not found or is
431 * broken, lock the reference anyway but clear sha1.
433 * Return 0 on success. On failure, write an error message to err and
434 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
436 * Implementation note: This function is basically
441 * but it includes a lot more code to
442 * - Deal with possible races with other processes
443 * - Avoid calling refs_verify_refname_available() when it can be
444 * avoided, namely if we were successfully able to read the ref
445 * - Generate informative error messages in the case of failure
447 static int lock_raw_ref(struct files_ref_store *refs,
448 const char *refname, int mustexist,
449 const struct string_list *extras,
450 const struct string_list *skip,
451 struct ref_lock **lock_p,
452 struct strbuf *referent,
456 struct ref_lock *lock;
457 struct strbuf ref_file = STRBUF_INIT;
458 int attempts_remaining = 3;
459 int ret = TRANSACTION_GENERIC_ERROR;
462 files_assert_main_repository(refs, "lock_raw_ref");
466 /* First lock the file so it can't change out from under us. */
468 *lock_p = lock = xcalloc(1, sizeof(*lock));
470 lock->ref_name = xstrdup(refname);
471 files_ref_path(refs, &ref_file, refname);
474 switch (safe_create_leading_directories(ref_file.buf)) {
479 * Suppose refname is "refs/foo/bar". We just failed
480 * to create the containing directory, "refs/foo",
481 * because there was a non-directory in the way. This
482 * indicates a D/F conflict, probably because of
483 * another reference such as "refs/foo". There is no
484 * reason to expect this error to be transitory.
486 if (refs_verify_refname_available(&refs->base, refname,
487 extras, skip, err)) {
490 * To the user the relevant error is
491 * that the "mustexist" reference is
495 strbuf_addf(err, "unable to resolve reference '%s'",
499 * The error message set by
500 * refs_verify_refname_available() is
503 ret = TRANSACTION_NAME_CONFLICT;
507 * The file that is in the way isn't a loose
508 * reference. Report it as a low-level
511 strbuf_addf(err, "unable to create lock file %s.lock; "
512 "non-directory in the way",
517 /* Maybe another process was tidying up. Try again. */
518 if (--attempts_remaining > 0)
522 strbuf_addf(err, "unable to create directory for %s",
527 if (hold_lock_file_for_update_timeout(
528 &lock->lk, ref_file.buf, LOCK_NO_DEREF,
529 get_files_ref_lock_timeout_ms()) < 0) {
530 if (errno == ENOENT && --attempts_remaining > 0) {
532 * Maybe somebody just deleted one of the
533 * directories leading to ref_file. Try
538 unable_to_lock_message(ref_file.buf, errno, err);
544 * Now we hold the lock and can read the reference without
545 * fear that its value will change.
548 if (files_read_raw_ref(&refs->base, refname,
549 &lock->old_oid, referent, type)) {
550 if (errno == ENOENT) {
552 /* Garden variety missing reference. */
553 strbuf_addf(err, "unable to resolve reference '%s'",
558 * Reference is missing, but that's OK. We
559 * know that there is not a conflict with
560 * another loose reference because
561 * (supposing that we are trying to lock
562 * reference "refs/foo/bar"):
564 * - We were successfully able to create
565 * the lockfile refs/foo/bar.lock, so we
566 * know there cannot be a loose reference
569 * - We got ENOENT and not EISDIR, so we
570 * know that there cannot be a loose
571 * reference named "refs/foo/bar/baz".
574 } else if (errno == EISDIR) {
576 * There is a directory in the way. It might have
577 * contained references that have been deleted. If
578 * we don't require that the reference already
579 * exists, try to remove the directory so that it
580 * doesn't cause trouble when we want to rename the
581 * lockfile into place later.
584 /* Garden variety missing reference. */
585 strbuf_addf(err, "unable to resolve reference '%s'",
588 } else if (remove_dir_recursively(&ref_file,
589 REMOVE_DIR_EMPTY_ONLY)) {
590 if (refs_verify_refname_available(
591 &refs->base, refname,
592 extras, skip, err)) {
594 * The error message set by
595 * verify_refname_available() is OK.
597 ret = TRANSACTION_NAME_CONFLICT;
601 * We can't delete the directory,
602 * but we also don't know of any
603 * references that it should
606 strbuf_addf(err, "there is a non-empty directory '%s' "
607 "blocking reference '%s'",
608 ref_file.buf, refname);
612 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) {
613 strbuf_addf(err, "unable to resolve reference '%s': "
614 "reference broken", refname);
617 strbuf_addf(err, "unable to resolve reference '%s': %s",
618 refname, strerror(errno));
623 * If the ref did not exist and we are creating it,
624 * make sure there is no existing packed ref that
625 * conflicts with refname:
627 if (refs_verify_refname_available(
628 refs->packed_ref_store, refname,
641 strbuf_release(&ref_file);
645 struct files_ref_iterator {
646 struct ref_iterator base;
648 struct ref_iterator *iter0;
652 static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
654 struct files_ref_iterator *iter =
655 (struct files_ref_iterator *)ref_iterator;
658 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
659 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
660 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
663 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
664 !ref_resolves_to_object(iter->iter0->refname,
669 iter->base.refname = iter->iter0->refname;
670 iter->base.oid = iter->iter0->oid;
671 iter->base.flags = iter->iter0->flags;
676 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
682 static int files_ref_iterator_peel(struct ref_iterator *ref_iterator,
683 struct object_id *peeled)
685 struct files_ref_iterator *iter =
686 (struct files_ref_iterator *)ref_iterator;
688 return ref_iterator_peel(iter->iter0, peeled);
691 static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
693 struct files_ref_iterator *iter =
694 (struct files_ref_iterator *)ref_iterator;
698 ok = ref_iterator_abort(iter->iter0);
700 base_ref_iterator_free(ref_iterator);
704 static struct ref_iterator_vtable files_ref_iterator_vtable = {
705 files_ref_iterator_advance,
706 files_ref_iterator_peel,
707 files_ref_iterator_abort
710 static struct ref_iterator *files_ref_iterator_begin(
711 struct ref_store *ref_store,
712 const char *prefix, unsigned int flags)
714 struct files_ref_store *refs;
715 struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
716 struct files_ref_iterator *iter;
717 struct ref_iterator *ref_iterator;
718 unsigned int required_flags = REF_STORE_READ;
720 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
721 required_flags |= REF_STORE_ODB;
723 refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
726 * We must make sure that all loose refs are read before
727 * accessing the packed-refs file; this avoids a race
728 * condition if loose refs are migrated to the packed-refs
729 * file by a simultaneous process, but our in-memory view is
730 * from before the migration. We ensure this as follows:
731 * First, we call start the loose refs iteration with its
732 * `prime_ref` argument set to true. This causes the loose
733 * references in the subtree to be pre-read into the cache.
734 * (If they've already been read, that's OK; we only need to
735 * guarantee that they're read before the packed refs, not
736 * *how much* before.) After that, we call
737 * packed_ref_iterator_begin(), which internally checks
738 * whether the packed-ref cache is up to date with what is on
739 * disk, and re-reads it if not.
742 loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
746 * The packed-refs file might contain broken references, for
747 * example an old version of a reference that points at an
748 * object that has since been garbage-collected. This is OK as
749 * long as there is a corresponding loose reference that
750 * overrides it, and we don't want to emit an error message in
751 * this case. So ask the packed_ref_store for all of its
752 * references, and (if needed) do our own check for broken
753 * ones in files_ref_iterator_advance(), after we have merged
754 * the packed and loose references.
756 packed_iter = refs_ref_iterator_begin(
757 refs->packed_ref_store, prefix, 0,
758 DO_FOR_EACH_INCLUDE_BROKEN);
760 overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter);
762 iter = xcalloc(1, sizeof(*iter));
763 ref_iterator = &iter->base;
764 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable,
765 overlay_iter->ordered);
766 iter->iter0 = overlay_iter;
773 * Verify that the reference locked by lock has the value old_oid
774 * (unless it is NULL). Fail if the reference doesn't exist and
775 * mustexist is set. Return 0 on success. On error, write an error
776 * message to err, set errno, and return a negative value.
778 static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
779 const struct object_id *old_oid, int mustexist,
784 if (refs_read_ref_full(ref_store, lock->ref_name,
785 mustexist ? RESOLVE_REF_READING : 0,
786 &lock->old_oid, NULL)) {
788 int save_errno = errno;
789 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
793 oidclr(&lock->old_oid);
797 if (old_oid && oidcmp(&lock->old_oid, old_oid)) {
798 strbuf_addf(err, "ref '%s' is at %s but expected %s",
800 oid_to_hex(&lock->old_oid),
801 oid_to_hex(old_oid));
808 static int remove_empty_directories(struct strbuf *path)
811 * we want to create a file but there is a directory there;
812 * if that is an empty directory (or a directory that contains
813 * only empty directories), remove them.
815 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
818 static int create_reflock(const char *path, void *cb)
820 struct lock_file *lk = cb;
822 return hold_lock_file_for_update_timeout(
823 lk, path, LOCK_NO_DEREF,
824 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
828 * Locks a ref returning the lock on success and NULL on failure.
829 * On failure errno is set to something meaningful.
831 static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
833 const struct object_id *old_oid,
834 const struct string_list *extras,
835 const struct string_list *skip,
836 unsigned int flags, int *type,
839 struct strbuf ref_file = STRBUF_INIT;
840 struct ref_lock *lock;
842 int mustexist = (old_oid && !is_null_oid(old_oid));
843 int resolve_flags = RESOLVE_REF_NO_RECURSE;
846 files_assert_main_repository(refs, "lock_ref_oid_basic");
849 lock = xcalloc(1, sizeof(struct ref_lock));
852 resolve_flags |= RESOLVE_REF_READING;
853 if (flags & REF_DELETING)
854 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
856 files_ref_path(refs, &ref_file, refname);
857 resolved = !!refs_resolve_ref_unsafe(&refs->base,
858 refname, resolve_flags,
859 &lock->old_oid, type);
860 if (!resolved && errno == EISDIR) {
862 * we are trying to lock foo but we used to
863 * have foo/bar which now does not exist;
864 * it is normal for the empty directory 'foo'
867 if (remove_empty_directories(&ref_file)) {
869 if (!refs_verify_refname_available(
871 refname, extras, skip, err))
872 strbuf_addf(err, "there are still refs under '%s'",
876 resolved = !!refs_resolve_ref_unsafe(&refs->base,
877 refname, resolve_flags,
878 &lock->old_oid, type);
882 if (last_errno != ENOTDIR ||
883 !refs_verify_refname_available(&refs->base, refname,
885 strbuf_addf(err, "unable to resolve reference '%s': %s",
886 refname, strerror(last_errno));
892 * If the ref did not exist and we are creating it, make sure
893 * there is no existing packed ref whose name begins with our
894 * refname, nor a packed ref whose name is a proper prefix of
897 if (is_null_oid(&lock->old_oid) &&
898 refs_verify_refname_available(refs->packed_ref_store, refname,
899 extras, skip, err)) {
900 last_errno = ENOTDIR;
904 lock->ref_name = xstrdup(refname);
906 if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) {
908 unable_to_lock_message(ref_file.buf, errno, err);
912 if (verify_lock(&refs->base, lock, old_oid, mustexist, err)) {
923 strbuf_release(&ref_file);
928 struct ref_to_prune {
929 struct ref_to_prune *next;
930 struct object_id oid;
931 char name[FLEX_ARRAY];
935 REMOVE_EMPTY_PARENTS_REF = 0x01,
936 REMOVE_EMPTY_PARENTS_REFLOG = 0x02
940 * Remove empty parent directories associated with the specified
941 * reference and/or its reflog, but spare [logs/]refs/ and immediate
942 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
943 * REMOVE_EMPTY_PARENTS_REFLOG.
945 static void try_remove_empty_parents(struct files_ref_store *refs,
949 struct strbuf buf = STRBUF_INIT;
950 struct strbuf sb = STRBUF_INIT;
954 strbuf_addstr(&buf, refname);
956 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
957 while (*p && *p != '/')
959 /* tolerate duplicate slashes; see check_refname_format() */
963 q = buf.buf + buf.len;
964 while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
965 while (q > p && *q != '/')
967 while (q > p && *(q-1) == '/')
971 strbuf_setlen(&buf, q - buf.buf);
974 files_ref_path(refs, &sb, buf.buf);
975 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
976 flags &= ~REMOVE_EMPTY_PARENTS_REF;
979 files_reflog_path(refs, &sb, buf.buf);
980 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
981 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
983 strbuf_release(&buf);
987 /* make sure nobody touched the ref, and unlink */
988 static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
990 struct ref_transaction *transaction;
991 struct strbuf err = STRBUF_INIT;
993 if (check_refname_format(r->name, 0))
996 transaction = ref_store_transaction_begin(&refs->base, &err);
998 ref_transaction_delete(transaction, r->name, &r->oid,
999 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
1000 ref_transaction_commit(transaction, &err)) {
1001 ref_transaction_free(transaction);
1002 error("%s", err.buf);
1003 strbuf_release(&err);
1006 ref_transaction_free(transaction);
1007 strbuf_release(&err);
1011 * Prune the loose versions of the references in the linked list
1012 * `*refs_to_prune`, freeing the entries in the list as we go.
1014 static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_to_prune)
1016 while (*refs_to_prune) {
1017 struct ref_to_prune *r = *refs_to_prune;
1018 *refs_to_prune = r->next;
1025 * Return true if the specified reference should be packed.
1027 static int should_pack_ref(const char *refname,
1028 const struct object_id *oid, unsigned int ref_flags,
1029 unsigned int pack_flags)
1031 /* Do not pack per-worktree refs: */
1032 if (ref_type(refname) != REF_TYPE_NORMAL)
1035 /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1036 if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/"))
1039 /* Do not pack symbolic refs: */
1040 if (ref_flags & REF_ISSYMREF)
1043 /* Do not pack broken refs: */
1044 if (!ref_resolves_to_object(refname, oid, ref_flags))
1050 static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1052 struct files_ref_store *refs =
1053 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1055 struct ref_iterator *iter;
1057 struct ref_to_prune *refs_to_prune = NULL;
1058 struct strbuf err = STRBUF_INIT;
1059 struct ref_transaction *transaction;
1061 transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);
1065 packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
1067 iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);
1068 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1070 * If the loose reference can be packed, add an entry
1071 * in the packed ref cache. If the reference should be
1072 * pruned, also add it to refs_to_prune.
1074 if (!should_pack_ref(iter->refname, iter->oid, iter->flags,
1079 * Add a reference creation for this reference to the
1080 * packed-refs transaction:
1082 if (ref_transaction_update(transaction, iter->refname,
1084 REF_NODEREF, NULL, &err))
1085 die("failure preparing to create packed reference %s: %s",
1086 iter->refname, err.buf);
1088 /* Schedule the loose reference for pruning if requested. */
1089 if ((flags & PACK_REFS_PRUNE)) {
1090 struct ref_to_prune *n;
1091 FLEX_ALLOC_STR(n, name, iter->refname);
1092 oidcpy(&n->oid, iter->oid);
1093 n->next = refs_to_prune;
1097 if (ok != ITER_DONE)
1098 die("error while iterating over references");
1100 if (ref_transaction_commit(transaction, &err))
1101 die("unable to write new packed-refs: %s", err.buf);
1103 ref_transaction_free(transaction);
1105 packed_refs_unlock(refs->packed_ref_store);
1107 prune_refs(refs, &refs_to_prune);
1108 strbuf_release(&err);
1112 static int files_delete_refs(struct ref_store *ref_store, const char *msg,
1113 struct string_list *refnames, unsigned int flags)
1115 struct files_ref_store *refs =
1116 files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1117 struct strbuf err = STRBUF_INIT;
1123 if (packed_refs_lock(refs->packed_ref_store, 0, &err))
1126 if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) {
1127 packed_refs_unlock(refs->packed_ref_store);
1131 packed_refs_unlock(refs->packed_ref_store);
1133 for (i = 0; i < refnames->nr; i++) {
1134 const char *refname = refnames->items[i].string;
1136 if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))
1137 result |= error(_("could not remove reference %s"), refname);
1140 strbuf_release(&err);
1145 * If we failed to rewrite the packed-refs file, then it is
1146 * unsafe to try to remove loose refs, because doing so might
1147 * expose an obsolete packed value for a reference that might
1148 * even point at an object that has been garbage collected.
1150 if (refnames->nr == 1)
1151 error(_("could not delete reference %s: %s"),
1152 refnames->items[0].string, err.buf);
1154 error(_("could not delete references: %s"), err.buf);
1156 strbuf_release(&err);
1161 * People using contrib's git-new-workdir have .git/logs/refs ->
1162 * /some/other/path/.git/logs/refs, and that may live on another device.
1164 * IOW, to avoid cross device rename errors, the temporary renamed log must
1165 * live into logs/refs.
1167 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1170 const char *tmp_renamed_log;
1174 static int rename_tmp_log_callback(const char *path, void *cb_data)
1176 struct rename_cb *cb = cb_data;
1178 if (rename(cb->tmp_renamed_log, path)) {
1180 * rename(a, b) when b is an existing directory ought
1181 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1182 * Sheesh. Record the true errno for error reporting,
1183 * but report EISDIR to raceproof_create_file() so
1184 * that it knows to retry.
1186 cb->true_errno = errno;
1187 if (errno == ENOTDIR)
1195 static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
1197 struct strbuf path = STRBUF_INIT;
1198 struct strbuf tmp = STRBUF_INIT;
1199 struct rename_cb cb;
1202 files_reflog_path(refs, &path, newrefname);
1203 files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
1204 cb.tmp_renamed_log = tmp.buf;
1205 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
1207 if (errno == EISDIR)
1208 error("directory not empty: %s", path.buf);
1210 error("unable to move logfile %s to %s: %s",
1212 strerror(cb.true_errno));
1215 strbuf_release(&path);
1216 strbuf_release(&tmp);
1220 static int write_ref_to_lockfile(struct ref_lock *lock,
1221 const struct object_id *oid, struct strbuf *err);
1222 static int commit_ref_update(struct files_ref_store *refs,
1223 struct ref_lock *lock,
1224 const struct object_id *oid, const char *logmsg,
1225 struct strbuf *err);
1227 static int files_copy_or_rename_ref(struct ref_store *ref_store,
1228 const char *oldrefname, const char *newrefname,
1229 const char *logmsg, int copy)
1231 struct files_ref_store *refs =
1232 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
1233 struct object_id oid, orig_oid;
1234 int flag = 0, logmoved = 0;
1235 struct ref_lock *lock;
1236 struct stat loginfo;
1237 struct strbuf sb_oldref = STRBUF_INIT;
1238 struct strbuf sb_newref = STRBUF_INIT;
1239 struct strbuf tmp_renamed_log = STRBUF_INIT;
1241 struct strbuf err = STRBUF_INIT;
1243 files_reflog_path(refs, &sb_oldref, oldrefname);
1244 files_reflog_path(refs, &sb_newref, newrefname);
1245 files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
1247 log = !lstat(sb_oldref.buf, &loginfo);
1248 if (log && S_ISLNK(loginfo.st_mode)) {
1249 ret = error("reflog for %s is a symlink", oldrefname);
1253 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1254 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1255 &orig_oid, &flag)) {
1256 ret = error("refname %s not found", oldrefname);
1260 if (flag & REF_ISSYMREF) {
1262 ret = error("refname %s is a symbolic ref, copying it is not supported",
1265 ret = error("refname %s is a symbolic ref, renaming it is not supported",
1269 if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
1274 if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
1275 ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1276 oldrefname, strerror(errno));
1280 if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
1281 ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1282 oldrefname, strerror(errno));
1286 if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,
1287 &orig_oid, REF_NODEREF)) {
1288 error("unable to delete old %s", oldrefname);
1293 * Since we are doing a shallow lookup, oid is not the
1294 * correct value to pass to delete_ref as old_oid. But that
1295 * doesn't matter, because an old_oid check wouldn't add to
1296 * the safety anyway; we want to delete the reference whatever
1297 * its current value.
1299 if (!copy && !refs_read_ref_full(&refs->base, newrefname,
1300 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1302 refs_delete_ref(&refs->base, NULL, newrefname,
1303 NULL, REF_NODEREF)) {
1304 if (errno == EISDIR) {
1305 struct strbuf path = STRBUF_INIT;
1308 files_ref_path(refs, &path, newrefname);
1309 result = remove_empty_directories(&path);
1310 strbuf_release(&path);
1313 error("Directory not empty: %s", newrefname);
1317 error("unable to delete existing %s", newrefname);
1322 if (log && rename_tmp_log(refs, newrefname))
1327 lock = lock_ref_oid_basic(refs, newrefname, NULL, NULL, NULL,
1328 REF_NODEREF, NULL, &err);
1331 error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1333 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1334 strbuf_release(&err);
1337 oidcpy(&lock->old_oid, &orig_oid);
1339 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1340 commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
1341 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1342 strbuf_release(&err);
1350 lock = lock_ref_oid_basic(refs, oldrefname, NULL, NULL, NULL,
1351 REF_NODEREF, NULL, &err);
1353 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1354 strbuf_release(&err);
1358 flag = log_all_ref_updates;
1359 log_all_ref_updates = LOG_REFS_NONE;
1360 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1361 commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
1362 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1363 strbuf_release(&err);
1365 log_all_ref_updates = flag;
1368 if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
1369 error("unable to restore logfile %s from %s: %s",
1370 oldrefname, newrefname, strerror(errno));
1371 if (!logmoved && log &&
1372 rename(tmp_renamed_log.buf, sb_oldref.buf))
1373 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
1374 oldrefname, strerror(errno));
1377 strbuf_release(&sb_newref);
1378 strbuf_release(&sb_oldref);
1379 strbuf_release(&tmp_renamed_log);
1384 static int files_rename_ref(struct ref_store *ref_store,
1385 const char *oldrefname, const char *newrefname,
1388 return files_copy_or_rename_ref(ref_store, oldrefname,
1389 newrefname, logmsg, 0);
1392 static int files_copy_ref(struct ref_store *ref_store,
1393 const char *oldrefname, const char *newrefname,
1396 return files_copy_or_rename_ref(ref_store, oldrefname,
1397 newrefname, logmsg, 1);
1400 static int close_ref_gently(struct ref_lock *lock)
1402 if (close_lock_file_gently(&lock->lk))
1407 static int commit_ref(struct ref_lock *lock)
1409 char *path = get_locked_file_path(&lock->lk);
1412 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1414 * There is a directory at the path we want to rename
1415 * the lockfile to. Hopefully it is empty; try to
1418 size_t len = strlen(path);
1419 struct strbuf sb_path = STRBUF_INIT;
1421 strbuf_attach(&sb_path, path, len, len);
1424 * If this fails, commit_lock_file() will also fail
1425 * and will report the problem.
1427 remove_empty_directories(&sb_path);
1428 strbuf_release(&sb_path);
1433 if (commit_lock_file(&lock->lk))
1438 static int open_or_create_logfile(const char *path, void *cb)
1442 *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1443 return (*fd < 0) ? -1 : 0;
1447 * Create a reflog for a ref. If force_create = 0, only create the
1448 * reflog for certain refs (those for which should_autocreate_reflog
1449 * returns non-zero). Otherwise, create it regardless of the reference
1450 * name. If the logfile already existed or was created, return 0 and
1451 * set *logfd to the file descriptor opened for appending to the file.
1452 * If no logfile exists and we decided not to create one, return 0 and
1453 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1456 static int log_ref_setup(struct files_ref_store *refs,
1457 const char *refname, int force_create,
1458 int *logfd, struct strbuf *err)
1460 struct strbuf logfile_sb = STRBUF_INIT;
1463 files_reflog_path(refs, &logfile_sb, refname);
1464 logfile = strbuf_detach(&logfile_sb, NULL);
1466 if (force_create || should_autocreate_reflog(refname)) {
1467 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
1468 if (errno == ENOENT)
1469 strbuf_addf(err, "unable to create directory for '%s': "
1470 "%s", logfile, strerror(errno));
1471 else if (errno == EISDIR)
1472 strbuf_addf(err, "there are still logs under '%s'",
1475 strbuf_addf(err, "unable to append to '%s': %s",
1476 logfile, strerror(errno));
1481 *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
1483 if (errno == ENOENT || errno == EISDIR) {
1485 * The logfile doesn't already exist,
1486 * but that is not an error; it only
1487 * means that we won't write log
1492 strbuf_addf(err, "unable to append to '%s': %s",
1493 logfile, strerror(errno));
1500 adjust_shared_perm(logfile);
1510 static int files_create_reflog(struct ref_store *ref_store,
1511 const char *refname, int force_create,
1514 struct files_ref_store *refs =
1515 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
1518 if (log_ref_setup(refs, refname, force_create, &fd, err))
1527 static int log_ref_write_fd(int fd, const struct object_id *old_oid,
1528 const struct object_id *new_oid,
1529 const char *committer, const char *msg)
1531 int msglen, written;
1532 unsigned maxlen, len;
1535 msglen = msg ? strlen(msg) : 0;
1536 maxlen = strlen(committer) + msglen + 100;
1537 logrec = xmalloc(maxlen);
1538 len = xsnprintf(logrec, maxlen, "%s %s %s\n",
1539 oid_to_hex(old_oid),
1540 oid_to_hex(new_oid),
1543 len += copy_reflog_msg(logrec + len - 1, msg) - 1;
1545 written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
1553 static int files_log_ref_write(struct files_ref_store *refs,
1554 const char *refname, const struct object_id *old_oid,
1555 const struct object_id *new_oid, const char *msg,
1556 int flags, struct strbuf *err)
1560 if (log_all_ref_updates == LOG_REFS_UNSET)
1561 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1563 result = log_ref_setup(refs, refname,
1564 flags & REF_FORCE_CREATE_REFLOG,
1572 result = log_ref_write_fd(logfd, old_oid, new_oid,
1573 git_committer_info(0), msg);
1575 struct strbuf sb = STRBUF_INIT;
1576 int save_errno = errno;
1578 files_reflog_path(refs, &sb, refname);
1579 strbuf_addf(err, "unable to append to '%s': %s",
1580 sb.buf, strerror(save_errno));
1581 strbuf_release(&sb);
1586 struct strbuf sb = STRBUF_INIT;
1587 int save_errno = errno;
1589 files_reflog_path(refs, &sb, refname);
1590 strbuf_addf(err, "unable to append to '%s': %s",
1591 sb.buf, strerror(save_errno));
1592 strbuf_release(&sb);
1599 * Write sha1 into the open lockfile, then close the lockfile. On
1600 * errors, rollback the lockfile, fill in *err and
1603 static int write_ref_to_lockfile(struct ref_lock *lock,
1604 const struct object_id *oid, struct strbuf *err)
1606 static char term = '\n';
1610 o = parse_object(oid);
1613 "trying to write ref '%s' with nonexistent object %s",
1614 lock->ref_name, oid_to_hex(oid));
1618 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
1620 "trying to write non-commit object %s to branch '%s'",
1621 oid_to_hex(oid), lock->ref_name);
1625 fd = get_lock_file_fd(&lock->lk);
1626 if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 ||
1627 write_in_full(fd, &term, 1) < 0 ||
1628 close_ref_gently(lock) < 0) {
1630 "couldn't write '%s'", get_lock_file_path(&lock->lk));
1638 * Commit a change to a loose reference that has already been written
1639 * to the loose reference lockfile. Also update the reflogs if
1640 * necessary, using the specified lockmsg (which can be NULL).
1642 static int commit_ref_update(struct files_ref_store *refs,
1643 struct ref_lock *lock,
1644 const struct object_id *oid, const char *logmsg,
1647 files_assert_main_repository(refs, "commit_ref_update");
1649 clear_loose_ref_cache(refs);
1650 if (files_log_ref_write(refs, lock->ref_name,
1651 &lock->old_oid, oid,
1653 char *old_msg = strbuf_detach(err, NULL);
1654 strbuf_addf(err, "cannot update the ref '%s': %s",
1655 lock->ref_name, old_msg);
1661 if (strcmp(lock->ref_name, "HEAD") != 0) {
1663 * Special hack: If a branch is updated directly and HEAD
1664 * points to it (may happen on the remote side of a push
1665 * for example) then logically the HEAD reflog should be
1667 * A generic solution implies reverse symref information,
1668 * but finding all symrefs pointing to the given branch
1669 * would be rather costly for this rare event (the direct
1670 * update of a branch) to be worth it. So let's cheat and
1671 * check with HEAD only which should cover 99% of all usage
1672 * scenarios (even 100% of the default ones).
1675 const char *head_ref;
1677 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
1678 RESOLVE_REF_READING,
1680 if (head_ref && (head_flag & REF_ISSYMREF) &&
1681 !strcmp(head_ref, lock->ref_name)) {
1682 struct strbuf log_err = STRBUF_INIT;
1683 if (files_log_ref_write(refs, "HEAD",
1684 &lock->old_oid, oid,
1685 logmsg, 0, &log_err)) {
1686 error("%s", log_err.buf);
1687 strbuf_release(&log_err);
1692 if (commit_ref(lock)) {
1693 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
1702 static int create_ref_symlink(struct ref_lock *lock, const char *target)
1705 #ifndef NO_SYMLINK_HEAD
1706 char *ref_path = get_locked_file_path(&lock->lk);
1708 ret = symlink(target, ref_path);
1712 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
1717 static void update_symref_reflog(struct files_ref_store *refs,
1718 struct ref_lock *lock, const char *refname,
1719 const char *target, const char *logmsg)
1721 struct strbuf err = STRBUF_INIT;
1722 struct object_id new_oid;
1724 !refs_read_ref_full(&refs->base, target,
1725 RESOLVE_REF_READING, &new_oid, NULL) &&
1726 files_log_ref_write(refs, refname, &lock->old_oid,
1727 &new_oid, logmsg, 0, &err)) {
1728 error("%s", err.buf);
1729 strbuf_release(&err);
1733 static int create_symref_locked(struct files_ref_store *refs,
1734 struct ref_lock *lock, const char *refname,
1735 const char *target, const char *logmsg)
1737 if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
1738 update_symref_reflog(refs, lock, refname, target, logmsg);
1742 if (!fdopen_lock_file(&lock->lk, "w"))
1743 return error("unable to fdopen %s: %s",
1744 lock->lk.tempfile->filename.buf, strerror(errno));
1746 update_symref_reflog(refs, lock, refname, target, logmsg);
1748 /* no error check; commit_ref will check ferror */
1749 fprintf(lock->lk.tempfile->fp, "ref: %s\n", target);
1750 if (commit_ref(lock) < 0)
1751 return error("unable to write symref for %s: %s", refname,
1756 static int files_create_symref(struct ref_store *ref_store,
1757 const char *refname, const char *target,
1760 struct files_ref_store *refs =
1761 files_downcast(ref_store, REF_STORE_WRITE, "create_symref");
1762 struct strbuf err = STRBUF_INIT;
1763 struct ref_lock *lock;
1766 lock = lock_ref_oid_basic(refs, refname, NULL,
1767 NULL, NULL, REF_NODEREF, NULL,
1770 error("%s", err.buf);
1771 strbuf_release(&err);
1775 ret = create_symref_locked(refs, lock, refname, target, logmsg);
1780 static int files_reflog_exists(struct ref_store *ref_store,
1781 const char *refname)
1783 struct files_ref_store *refs =
1784 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
1785 struct strbuf sb = STRBUF_INIT;
1789 files_reflog_path(refs, &sb, refname);
1790 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
1791 strbuf_release(&sb);
1795 static int files_delete_reflog(struct ref_store *ref_store,
1796 const char *refname)
1798 struct files_ref_store *refs =
1799 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
1800 struct strbuf sb = STRBUF_INIT;
1803 files_reflog_path(refs, &sb, refname);
1804 ret = remove_path(sb.buf);
1805 strbuf_release(&sb);
1809 static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
1811 struct object_id ooid, noid;
1812 char *email_end, *message;
1813 timestamp_t timestamp;
1815 const char *p = sb->buf;
1817 /* old SP new SP name <email> SP time TAB msg LF */
1818 if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
1819 parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||
1820 parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||
1821 !(email_end = strchr(p, '>')) ||
1822 email_end[1] != ' ' ||
1823 !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||
1824 !message || message[0] != ' ' ||
1825 (message[1] != '+' && message[1] != '-') ||
1826 !isdigit(message[2]) || !isdigit(message[3]) ||
1827 !isdigit(message[4]) || !isdigit(message[5]))
1828 return 0; /* corrupt? */
1829 email_end[1] = '\0';
1830 tz = strtol(message + 1, NULL, 10);
1831 if (message[6] != '\t')
1835 return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
1838 static char *find_beginning_of_line(char *bob, char *scan)
1840 while (bob < scan && *(--scan) != '\n')
1841 ; /* keep scanning backwards */
1843 * Return either beginning of the buffer, or LF at the end of
1844 * the previous line.
1849 static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1850 const char *refname,
1851 each_reflog_ent_fn fn,
1854 struct files_ref_store *refs =
1855 files_downcast(ref_store, REF_STORE_READ,
1856 "for_each_reflog_ent_reverse");
1857 struct strbuf sb = STRBUF_INIT;
1860 int ret = 0, at_tail = 1;
1862 files_reflog_path(refs, &sb, refname);
1863 logfp = fopen(sb.buf, "r");
1864 strbuf_release(&sb);
1868 /* Jump to the end */
1869 if (fseek(logfp, 0, SEEK_END) < 0)
1870 ret = error("cannot seek back reflog for %s: %s",
1871 refname, strerror(errno));
1873 while (!ret && 0 < pos) {
1879 /* Fill next block from the end */
1880 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
1881 if (fseek(logfp, pos - cnt, SEEK_SET)) {
1882 ret = error("cannot seek back reflog for %s: %s",
1883 refname, strerror(errno));
1886 nread = fread(buf, cnt, 1, logfp);
1888 ret = error("cannot read %d bytes from reflog for %s: %s",
1889 cnt, refname, strerror(errno));
1894 scanp = endp = buf + cnt;
1895 if (at_tail && scanp[-1] == '\n')
1896 /* Looking at the final LF at the end of the file */
1900 while (buf < scanp) {
1902 * terminating LF of the previous line, or the beginning
1907 bp = find_beginning_of_line(buf, scanp);
1911 * The newline is the end of the previous line,
1912 * so we know we have complete line starting
1913 * at (bp + 1). Prefix it onto any prior data
1914 * we collected for the line and process it.
1916 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
1919 ret = show_one_reflog_ent(&sb, fn, cb_data);
1925 * We are at the start of the buffer, and the
1926 * start of the file; there is no previous
1927 * line, and we have everything for this one.
1928 * Process it, and we can end the loop.
1930 strbuf_splice(&sb, 0, 0, buf, endp - buf);
1931 ret = show_one_reflog_ent(&sb, fn, cb_data);
1938 * We are at the start of the buffer, and there
1939 * is more file to read backwards. Which means
1940 * we are in the middle of a line. Note that we
1941 * may get here even if *bp was a newline; that
1942 * just means we are at the exact end of the
1943 * previous line, rather than some spot in the
1946 * Save away what we have to be combined with
1947 * the data from the next read.
1949 strbuf_splice(&sb, 0, 0, buf, endp - buf);
1956 die("BUG: reverse reflog parser had leftover data");
1959 strbuf_release(&sb);
1963 static int files_for_each_reflog_ent(struct ref_store *ref_store,
1964 const char *refname,
1965 each_reflog_ent_fn fn, void *cb_data)
1967 struct files_ref_store *refs =
1968 files_downcast(ref_store, REF_STORE_READ,
1969 "for_each_reflog_ent");
1971 struct strbuf sb = STRBUF_INIT;
1974 files_reflog_path(refs, &sb, refname);
1975 logfp = fopen(sb.buf, "r");
1976 strbuf_release(&sb);
1980 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
1981 ret = show_one_reflog_ent(&sb, fn, cb_data);
1983 strbuf_release(&sb);
1987 struct files_reflog_iterator {
1988 struct ref_iterator base;
1990 struct ref_store *ref_store;
1991 struct dir_iterator *dir_iterator;
1992 struct object_id oid;
1995 static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
1997 struct files_reflog_iterator *iter =
1998 (struct files_reflog_iterator *)ref_iterator;
1999 struct dir_iterator *diter = iter->dir_iterator;
2002 while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2005 if (!S_ISREG(diter->st.st_mode))
2007 if (diter->basename[0] == '.')
2009 if (ends_with(diter->basename, ".lock"))
2012 if (refs_read_ref_full(iter->ref_store,
2013 diter->relative_path, 0,
2014 &iter->oid, &flags)) {
2015 error("bad ref for %s", diter->path.buf);
2019 iter->base.refname = diter->relative_path;
2020 iter->base.oid = &iter->oid;
2021 iter->base.flags = flags;
2025 iter->dir_iterator = NULL;
2026 if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
2031 static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2032 struct object_id *peeled)
2034 die("BUG: ref_iterator_peel() called for reflog_iterator");
2037 static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
2039 struct files_reflog_iterator *iter =
2040 (struct files_reflog_iterator *)ref_iterator;
2043 if (iter->dir_iterator)
2044 ok = dir_iterator_abort(iter->dir_iterator);
2046 base_ref_iterator_free(ref_iterator);
2050 static struct ref_iterator_vtable files_reflog_iterator_vtable = {
2051 files_reflog_iterator_advance,
2052 files_reflog_iterator_peel,
2053 files_reflog_iterator_abort
2056 static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
2059 struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
2060 struct ref_iterator *ref_iterator = &iter->base;
2061 struct strbuf sb = STRBUF_INIT;
2063 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);
2064 strbuf_addf(&sb, "%s/logs", gitdir);
2065 iter->dir_iterator = dir_iterator_begin(sb.buf);
2066 iter->ref_store = ref_store;
2067 strbuf_release(&sb);
2069 return ref_iterator;
2072 static enum iterator_selection reflog_iterator_select(
2073 struct ref_iterator *iter_worktree,
2074 struct ref_iterator *iter_common,
2077 if (iter_worktree) {
2079 * We're a bit loose here. We probably should ignore
2080 * common refs if they are accidentally added as
2081 * per-worktree refs.
2083 return ITER_SELECT_0;
2084 } else if (iter_common) {
2085 if (ref_type(iter_common->refname) == REF_TYPE_NORMAL)
2086 return ITER_SELECT_1;
2089 * The main ref store may contain main worktree's
2090 * per-worktree refs, which should be ignored
2097 static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
2099 struct files_ref_store *refs =
2100 files_downcast(ref_store, REF_STORE_READ,
2101 "reflog_iterator_begin");
2103 if (!strcmp(refs->gitdir, refs->gitcommondir)) {
2104 return reflog_iterator_begin(ref_store, refs->gitcommondir);
2106 return merge_ref_iterator_begin(
2108 reflog_iterator_begin(ref_store, refs->gitdir),
2109 reflog_iterator_begin(ref_store, refs->gitcommondir),
2110 reflog_iterator_select, refs);
2115 * If update is a direct update of head_ref (the reference pointed to
2116 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2118 static int split_head_update(struct ref_update *update,
2119 struct ref_transaction *transaction,
2120 const char *head_ref,
2121 struct string_list *affected_refnames,
2124 struct string_list_item *item;
2125 struct ref_update *new_update;
2127 if ((update->flags & REF_LOG_ONLY) ||
2128 (update->flags & REF_ISPRUNING) ||
2129 (update->flags & REF_UPDATE_VIA_HEAD))
2132 if (strcmp(update->refname, head_ref))
2136 * First make sure that HEAD is not already in the
2137 * transaction. This check is O(lg N) in the transaction
2138 * size, but it happens at most once per transaction.
2140 if (string_list_has_string(affected_refnames, "HEAD")) {
2141 /* An entry already existed */
2143 "multiple updates for 'HEAD' (including one "
2144 "via its referent '%s') are not allowed",
2146 return TRANSACTION_NAME_CONFLICT;
2149 new_update = ref_transaction_add_update(
2150 transaction, "HEAD",
2151 update->flags | REF_LOG_ONLY | REF_NODEREF,
2152 &update->new_oid, &update->old_oid,
2156 * Add "HEAD". This insertion is O(N) in the transaction
2157 * size, but it happens at most once per transaction.
2158 * Add new_update->refname instead of a literal "HEAD".
2160 if (strcmp(new_update->refname, "HEAD"))
2161 BUG("%s unexpectedly not 'HEAD'", new_update->refname);
2162 item = string_list_insert(affected_refnames, new_update->refname);
2163 item->util = new_update;
2169 * update is for a symref that points at referent and doesn't have
2170 * REF_NODEREF set. Split it into two updates:
2171 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2172 * - A new, separate update for the referent reference
2173 * Note that the new update will itself be subject to splitting when
2174 * the iteration gets to it.
2176 static int split_symref_update(struct files_ref_store *refs,
2177 struct ref_update *update,
2178 const char *referent,
2179 struct ref_transaction *transaction,
2180 struct string_list *affected_refnames,
2183 struct string_list_item *item;
2184 struct ref_update *new_update;
2185 unsigned int new_flags;
2188 * First make sure that referent is not already in the
2189 * transaction. This check is O(lg N) in the transaction
2190 * size, but it happens at most once per symref in a
2193 if (string_list_has_string(affected_refnames, referent)) {
2194 /* An entry already exists */
2196 "multiple updates for '%s' (including one "
2197 "via symref '%s') are not allowed",
2198 referent, update->refname);
2199 return TRANSACTION_NAME_CONFLICT;
2202 new_flags = update->flags;
2203 if (!strcmp(update->refname, "HEAD")) {
2205 * Record that the new update came via HEAD, so that
2206 * when we process it, split_head_update() doesn't try
2207 * to add another reflog update for HEAD. Note that
2208 * this bit will be propagated if the new_update
2209 * itself needs to be split.
2211 new_flags |= REF_UPDATE_VIA_HEAD;
2214 new_update = ref_transaction_add_update(
2215 transaction, referent, new_flags,
2216 &update->new_oid, &update->old_oid,
2219 new_update->parent_update = update;
2222 * Change the symbolic ref update to log only. Also, it
2223 * doesn't need to check its old SHA-1 value, as that will be
2224 * done when new_update is processed.
2226 update->flags |= REF_LOG_ONLY | REF_NODEREF;
2227 update->flags &= ~REF_HAVE_OLD;
2230 * Add the referent. This insertion is O(N) in the transaction
2231 * size, but it happens at most once per symref in a
2232 * transaction. Make sure to add new_update->refname, which will
2233 * be valid as long as affected_refnames is in use, and NOT
2234 * referent, which might soon be freed by our caller.
2236 item = string_list_insert(affected_refnames, new_update->refname);
2238 BUG("%s unexpectedly found in affected_refnames",
2239 new_update->refname);
2240 item->util = new_update;
2246 * Return the refname under which update was originally requested.
2248 static const char *original_update_refname(struct ref_update *update)
2250 while (update->parent_update)
2251 update = update->parent_update;
2253 return update->refname;
2257 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2258 * are consistent with oid, which is the reference's current value. If
2259 * everything is OK, return 0; otherwise, write an error message to
2260 * err and return -1.
2262 static int check_old_oid(struct ref_update *update, struct object_id *oid,
2265 if (!(update->flags & REF_HAVE_OLD) ||
2266 !oidcmp(oid, &update->old_oid))
2269 if (is_null_oid(&update->old_oid))
2270 strbuf_addf(err, "cannot lock ref '%s': "
2271 "reference already exists",
2272 original_update_refname(update));
2273 else if (is_null_oid(oid))
2274 strbuf_addf(err, "cannot lock ref '%s': "
2275 "reference is missing but expected %s",
2276 original_update_refname(update),
2277 oid_to_hex(&update->old_oid));
2279 strbuf_addf(err, "cannot lock ref '%s': "
2280 "is at %s but expected %s",
2281 original_update_refname(update),
2283 oid_to_hex(&update->old_oid));
2289 * Prepare for carrying out update:
2290 * - Lock the reference referred to by update.
2291 * - Read the reference under lock.
2292 * - Check that its old SHA-1 value (if specified) is correct, and in
2293 * any case record it in update->lock->old_oid for later use when
2294 * writing the reflog.
2295 * - If it is a symref update without REF_NODEREF, split it up into a
2296 * REF_LOG_ONLY update of the symref and add a separate update for
2297 * the referent to transaction.
2298 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2301 static int lock_ref_for_update(struct files_ref_store *refs,
2302 struct ref_update *update,
2303 struct ref_transaction *transaction,
2304 const char *head_ref,
2305 struct string_list *affected_refnames,
2308 struct strbuf referent = STRBUF_INIT;
2309 int mustexist = (update->flags & REF_HAVE_OLD) &&
2310 !is_null_oid(&update->old_oid);
2312 struct ref_lock *lock;
2314 files_assert_main_repository(refs, "lock_ref_for_update");
2316 if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid))
2317 update->flags |= REF_DELETING;
2320 ret = split_head_update(update, transaction, head_ref,
2321 affected_refnames, err);
2326 ret = lock_raw_ref(refs, update->refname, mustexist,
2327 affected_refnames, NULL,
2329 &update->type, err);
2333 reason = strbuf_detach(err, NULL);
2334 strbuf_addf(err, "cannot lock ref '%s': %s",
2335 original_update_refname(update), reason);
2340 update->backend_data = lock;
2342 if (update->type & REF_ISSYMREF) {
2343 if (update->flags & REF_NODEREF) {
2345 * We won't be reading the referent as part of
2346 * the transaction, so we have to read it here
2347 * to record and possibly check old_sha1:
2349 if (refs_read_ref_full(&refs->base,
2351 &lock->old_oid, NULL)) {
2352 if (update->flags & REF_HAVE_OLD) {
2353 strbuf_addf(err, "cannot lock ref '%s': "
2354 "error reading reference",
2355 original_update_refname(update));
2356 ret = TRANSACTION_GENERIC_ERROR;
2359 } else if (check_old_oid(update, &lock->old_oid, err)) {
2360 ret = TRANSACTION_GENERIC_ERROR;
2365 * Create a new update for the reference this
2366 * symref is pointing at. Also, we will record
2367 * and verify old_sha1 for this update as part
2368 * of processing the split-off update, so we
2369 * don't have to do it here.
2371 ret = split_symref_update(refs, update,
2372 referent.buf, transaction,
2373 affected_refnames, err);
2378 struct ref_update *parent_update;
2380 if (check_old_oid(update, &lock->old_oid, err)) {
2381 ret = TRANSACTION_GENERIC_ERROR;
2386 * If this update is happening indirectly because of a
2387 * symref update, record the old SHA-1 in the parent
2390 for (parent_update = update->parent_update;
2392 parent_update = parent_update->parent_update) {
2393 struct ref_lock *parent_lock = parent_update->backend_data;
2394 oidcpy(&parent_lock->old_oid, &lock->old_oid);
2398 if ((update->flags & REF_HAVE_NEW) &&
2399 !(update->flags & REF_DELETING) &&
2400 !(update->flags & REF_LOG_ONLY)) {
2401 if (!(update->type & REF_ISSYMREF) &&
2402 !oidcmp(&lock->old_oid, &update->new_oid)) {
2404 * The reference already has the desired
2405 * value, so we don't need to write it.
2407 } else if (write_ref_to_lockfile(lock, &update->new_oid,
2409 char *write_err = strbuf_detach(err, NULL);
2412 * The lock was freed upon failure of
2413 * write_ref_to_lockfile():
2415 update->backend_data = NULL;
2417 "cannot update ref '%s': %s",
2418 update->refname, write_err);
2420 ret = TRANSACTION_GENERIC_ERROR;
2423 update->flags |= REF_NEEDS_COMMIT;
2426 if (!(update->flags & REF_NEEDS_COMMIT)) {
2428 * We didn't call write_ref_to_lockfile(), so
2429 * the lockfile is still open. Close it to
2430 * free up the file descriptor:
2432 if (close_ref_gently(lock)) {
2433 strbuf_addf(err, "couldn't close '%s.lock'",
2435 ret = TRANSACTION_GENERIC_ERROR;
2441 strbuf_release(&referent);
2445 struct files_transaction_backend_data {
2446 struct ref_transaction *packed_transaction;
2447 int packed_refs_locked;
2451 * Unlock any references in `transaction` that are still locked, and
2452 * mark the transaction closed.
2454 static void files_transaction_cleanup(struct files_ref_store *refs,
2455 struct ref_transaction *transaction)
2458 struct files_transaction_backend_data *backend_data =
2459 transaction->backend_data;
2460 struct strbuf err = STRBUF_INIT;
2462 for (i = 0; i < transaction->nr; i++) {
2463 struct ref_update *update = transaction->updates[i];
2464 struct ref_lock *lock = update->backend_data;
2468 update->backend_data = NULL;
2472 if (backend_data->packed_transaction &&
2473 ref_transaction_abort(backend_data->packed_transaction, &err)) {
2474 error("error aborting transaction: %s", err.buf);
2475 strbuf_release(&err);
2478 if (backend_data->packed_refs_locked)
2479 packed_refs_unlock(refs->packed_ref_store);
2483 transaction->state = REF_TRANSACTION_CLOSED;
2486 static int files_transaction_prepare(struct ref_store *ref_store,
2487 struct ref_transaction *transaction,
2490 struct files_ref_store *refs =
2491 files_downcast(ref_store, REF_STORE_WRITE,
2492 "ref_transaction_prepare");
2495 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
2496 char *head_ref = NULL;
2498 struct files_transaction_backend_data *backend_data;
2499 struct ref_transaction *packed_transaction = NULL;
2503 if (!transaction->nr)
2506 backend_data = xcalloc(1, sizeof(*backend_data));
2507 transaction->backend_data = backend_data;
2510 * Fail if a refname appears more than once in the
2511 * transaction. (If we end up splitting up any updates using
2512 * split_symref_update() or split_head_update(), those
2513 * functions will check that the new updates don't have the
2514 * same refname as any existing ones.)
2516 for (i = 0; i < transaction->nr; i++) {
2517 struct ref_update *update = transaction->updates[i];
2518 struct string_list_item *item =
2519 string_list_append(&affected_refnames, update->refname);
2522 * We store a pointer to update in item->util, but at
2523 * the moment we never use the value of this field
2524 * except to check whether it is non-NULL.
2526 item->util = update;
2528 string_list_sort(&affected_refnames);
2529 if (ref_update_reject_duplicates(&affected_refnames, err)) {
2530 ret = TRANSACTION_GENERIC_ERROR;
2535 * Special hack: If a branch is updated directly and HEAD
2536 * points to it (may happen on the remote side of a push
2537 * for example) then logically the HEAD reflog should be
2540 * A generic solution would require reverse symref lookups,
2541 * but finding all symrefs pointing to a given branch would be
2542 * rather costly for this rare event (the direct update of a
2543 * branch) to be worth it. So let's cheat and check with HEAD
2544 * only, which should cover 99% of all usage scenarios (even
2545 * 100% of the default ones).
2547 * So if HEAD is a symbolic reference, then record the name of
2548 * the reference that it points to. If we see an update of
2549 * head_ref within the transaction, then split_head_update()
2550 * arranges for the reflog of HEAD to be updated, too.
2552 head_ref = refs_resolve_refdup(ref_store, "HEAD",
2553 RESOLVE_REF_NO_RECURSE,
2556 if (head_ref && !(head_type & REF_ISSYMREF)) {
2557 FREE_AND_NULL(head_ref);
2561 * Acquire all locks, verify old values if provided, check
2562 * that new values are valid, and write new values to the
2563 * lockfiles, ready to be activated. Only keep one lockfile
2564 * open at a time to avoid running out of file descriptors.
2565 * Note that lock_ref_for_update() might append more updates
2566 * to the transaction.
2568 for (i = 0; i < transaction->nr; i++) {
2569 struct ref_update *update = transaction->updates[i];
2571 ret = lock_ref_for_update(refs, update, transaction,
2572 head_ref, &affected_refnames, err);
2576 if (update->flags & REF_DELETING &&
2577 !(update->flags & REF_LOG_ONLY) &&
2578 !(update->flags & REF_ISPRUNING)) {
2580 * This reference has to be deleted from
2581 * packed-refs if it exists there.
2583 if (!packed_transaction) {
2584 packed_transaction = ref_store_transaction_begin(
2585 refs->packed_ref_store, err);
2586 if (!packed_transaction) {
2587 ret = TRANSACTION_GENERIC_ERROR;
2591 backend_data->packed_transaction =
2595 ref_transaction_add_update(
2596 packed_transaction, update->refname,
2597 update->flags & ~REF_HAVE_OLD,
2598 &update->new_oid, &update->old_oid,
2603 if (packed_transaction) {
2604 if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
2605 ret = TRANSACTION_GENERIC_ERROR;
2608 backend_data->packed_refs_locked = 1;
2609 ret = ref_transaction_prepare(packed_transaction, err);
2614 string_list_clear(&affected_refnames, 0);
2617 files_transaction_cleanup(refs, transaction);
2619 transaction->state = REF_TRANSACTION_PREPARED;
2624 static int files_transaction_finish(struct ref_store *ref_store,
2625 struct ref_transaction *transaction,
2628 struct files_ref_store *refs =
2629 files_downcast(ref_store, 0, "ref_transaction_finish");
2632 struct strbuf sb = STRBUF_INIT;
2633 struct files_transaction_backend_data *backend_data;
2634 struct ref_transaction *packed_transaction;
2639 if (!transaction->nr) {
2640 transaction->state = REF_TRANSACTION_CLOSED;
2644 backend_data = transaction->backend_data;
2645 packed_transaction = backend_data->packed_transaction;
2647 /* Perform updates first so live commits remain referenced */
2648 for (i = 0; i < transaction->nr; i++) {
2649 struct ref_update *update = transaction->updates[i];
2650 struct ref_lock *lock = update->backend_data;
2652 if (update->flags & REF_NEEDS_COMMIT ||
2653 update->flags & REF_LOG_ONLY) {
2654 if (files_log_ref_write(refs,
2658 update->msg, update->flags,
2660 char *old_msg = strbuf_detach(err, NULL);
2662 strbuf_addf(err, "cannot update the ref '%s': %s",
2663 lock->ref_name, old_msg);
2666 update->backend_data = NULL;
2667 ret = TRANSACTION_GENERIC_ERROR;
2671 if (update->flags & REF_NEEDS_COMMIT) {
2672 clear_loose_ref_cache(refs);
2673 if (commit_ref(lock)) {
2674 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
2676 update->backend_data = NULL;
2677 ret = TRANSACTION_GENERIC_ERROR;
2684 * Now that updates are safely completed, we can perform
2685 * deletes. First delete the reflogs of any references that
2686 * will be deleted, since (in the unexpected event of an
2687 * error) leaving a reference without a reflog is less bad
2688 * than leaving a reflog without a reference (the latter is a
2689 * mildly invalid repository state):
2691 for (i = 0; i < transaction->nr; i++) {
2692 struct ref_update *update = transaction->updates[i];
2693 if (update->flags & REF_DELETING &&
2694 !(update->flags & REF_LOG_ONLY) &&
2695 !(update->flags & REF_ISPRUNING)) {
2697 files_reflog_path(refs, &sb, update->refname);
2698 if (!unlink_or_warn(sb.buf))
2699 try_remove_empty_parents(refs, update->refname,
2700 REMOVE_EMPTY_PARENTS_REFLOG);
2705 * Perform deletes now that updates are safely completed.
2707 * First delete any packed versions of the references, while
2708 * retaining the packed-refs lock:
2710 if (packed_transaction) {
2711 ret = ref_transaction_commit(packed_transaction, err);
2712 ref_transaction_free(packed_transaction);
2713 packed_transaction = NULL;
2714 backend_data->packed_transaction = NULL;
2719 /* Now delete the loose versions of the references: */
2720 for (i = 0; i < transaction->nr; i++) {
2721 struct ref_update *update = transaction->updates[i];
2722 struct ref_lock *lock = update->backend_data;
2724 if (update->flags & REF_DELETING &&
2725 !(update->flags & REF_LOG_ONLY)) {
2726 if (!(update->type & REF_ISPACKED) ||
2727 update->type & REF_ISSYMREF) {
2728 /* It is a loose reference. */
2730 files_ref_path(refs, &sb, lock->ref_name);
2731 if (unlink_or_msg(sb.buf, err)) {
2732 ret = TRANSACTION_GENERIC_ERROR;
2735 update->flags |= REF_DELETED_LOOSE;
2740 clear_loose_ref_cache(refs);
2743 files_transaction_cleanup(refs, transaction);
2745 for (i = 0; i < transaction->nr; i++) {
2746 struct ref_update *update = transaction->updates[i];
2748 if (update->flags & REF_DELETED_LOOSE) {
2750 * The loose reference was deleted. Delete any
2751 * empty parent directories. (Note that this
2752 * can only work because we have already
2753 * removed the lockfile.)
2755 try_remove_empty_parents(refs, update->refname,
2756 REMOVE_EMPTY_PARENTS_REF);
2760 strbuf_release(&sb);
2764 static int files_transaction_abort(struct ref_store *ref_store,
2765 struct ref_transaction *transaction,
2768 struct files_ref_store *refs =
2769 files_downcast(ref_store, 0, "ref_transaction_abort");
2771 files_transaction_cleanup(refs, transaction);
2775 static int ref_present(const char *refname,
2776 const struct object_id *oid, int flags, void *cb_data)
2778 struct string_list *affected_refnames = cb_data;
2780 return string_list_has_string(affected_refnames, refname);
2783 static int files_initial_transaction_commit(struct ref_store *ref_store,
2784 struct ref_transaction *transaction,
2787 struct files_ref_store *refs =
2788 files_downcast(ref_store, REF_STORE_WRITE,
2789 "initial_ref_transaction_commit");
2792 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
2793 struct ref_transaction *packed_transaction = NULL;
2797 if (transaction->state != REF_TRANSACTION_OPEN)
2798 die("BUG: commit called for transaction that is not open");
2800 /* Fail if a refname appears more than once in the transaction: */
2801 for (i = 0; i < transaction->nr; i++)
2802 string_list_append(&affected_refnames,
2803 transaction->updates[i]->refname);
2804 string_list_sort(&affected_refnames);
2805 if (ref_update_reject_duplicates(&affected_refnames, err)) {
2806 ret = TRANSACTION_GENERIC_ERROR;
2811 * It's really undefined to call this function in an active
2812 * repository or when there are existing references: we are
2813 * only locking and changing packed-refs, so (1) any
2814 * simultaneous processes might try to change a reference at
2815 * the same time we do, and (2) any existing loose versions of
2816 * the references that we are setting would have precedence
2817 * over our values. But some remote helpers create the remote
2818 * "HEAD" and "master" branches before calling this function,
2819 * so here we really only check that none of the references
2820 * that we are creating already exists.
2822 if (refs_for_each_rawref(&refs->base, ref_present,
2823 &affected_refnames))
2824 die("BUG: initial ref transaction called with existing refs");
2826 packed_transaction = ref_store_transaction_begin(refs->packed_ref_store, err);
2827 if (!packed_transaction) {
2828 ret = TRANSACTION_GENERIC_ERROR;
2832 for (i = 0; i < transaction->nr; i++) {
2833 struct ref_update *update = transaction->updates[i];
2835 if ((update->flags & REF_HAVE_OLD) &&
2836 !is_null_oid(&update->old_oid))
2837 die("BUG: initial ref transaction with old_sha1 set");
2838 if (refs_verify_refname_available(&refs->base, update->refname,
2839 &affected_refnames, NULL,
2841 ret = TRANSACTION_NAME_CONFLICT;
2846 * Add a reference creation for this reference to the
2847 * packed-refs transaction:
2849 ref_transaction_add_update(packed_transaction, update->refname,
2850 update->flags & ~REF_HAVE_OLD,
2851 &update->new_oid, &update->old_oid,
2855 if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
2856 ret = TRANSACTION_GENERIC_ERROR;
2860 if (initial_ref_transaction_commit(packed_transaction, err)) {
2861 ret = TRANSACTION_GENERIC_ERROR;
2866 if (packed_transaction)
2867 ref_transaction_free(packed_transaction);
2868 packed_refs_unlock(refs->packed_ref_store);
2869 transaction->state = REF_TRANSACTION_CLOSED;
2870 string_list_clear(&affected_refnames, 0);
2874 struct expire_reflog_cb {
2876 reflog_expiry_should_prune_fn *should_prune_fn;
2879 struct object_id last_kept_oid;
2882 static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
2883 const char *email, timestamp_t timestamp, int tz,
2884 const char *message, void *cb_data)
2886 struct expire_reflog_cb *cb = cb_data;
2887 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
2889 if (cb->flags & EXPIRE_REFLOGS_REWRITE)
2890 ooid = &cb->last_kept_oid;
2892 if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,
2893 message, policy_cb)) {
2895 printf("would prune %s", message);
2896 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
2897 printf("prune %s", message);
2900 fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",
2901 oid_to_hex(ooid), oid_to_hex(noid),
2902 email, timestamp, tz, message);
2903 oidcpy(&cb->last_kept_oid, noid);
2905 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
2906 printf("keep %s", message);
2911 static int files_reflog_expire(struct ref_store *ref_store,
2912 const char *refname, const struct object_id *oid,
2914 reflog_expiry_prepare_fn prepare_fn,
2915 reflog_expiry_should_prune_fn should_prune_fn,
2916 reflog_expiry_cleanup_fn cleanup_fn,
2917 void *policy_cb_data)
2919 struct files_ref_store *refs =
2920 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
2921 static struct lock_file reflog_lock;
2922 struct expire_reflog_cb cb;
2923 struct ref_lock *lock;
2924 struct strbuf log_file_sb = STRBUF_INIT;
2928 struct strbuf err = STRBUF_INIT;
2930 memset(&cb, 0, sizeof(cb));
2932 cb.policy_cb = policy_cb_data;
2933 cb.should_prune_fn = should_prune_fn;
2936 * The reflog file is locked by holding the lock on the
2937 * reference itself, plus we might need to update the
2938 * reference if --updateref was specified:
2940 lock = lock_ref_oid_basic(refs, refname, oid,
2941 NULL, NULL, REF_NODEREF,
2944 error("cannot lock ref '%s': %s", refname, err.buf);
2945 strbuf_release(&err);
2948 if (!refs_reflog_exists(ref_store, refname)) {
2953 files_reflog_path(refs, &log_file_sb, refname);
2954 log_file = strbuf_detach(&log_file_sb, NULL);
2955 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
2957 * Even though holding $GIT_DIR/logs/$reflog.lock has
2958 * no locking implications, we use the lock_file
2959 * machinery here anyway because it does a lot of the
2960 * work we need, including cleaning up if the program
2961 * exits unexpectedly.
2963 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
2964 struct strbuf err = STRBUF_INIT;
2965 unable_to_lock_message(log_file, errno, &err);
2966 error("%s", err.buf);
2967 strbuf_release(&err);
2970 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
2972 error("cannot fdopen %s (%s)",
2973 get_lock_file_path(&reflog_lock), strerror(errno));
2978 (*prepare_fn)(refname, oid, cb.policy_cb);
2979 refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
2980 (*cleanup_fn)(cb.policy_cb);
2982 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
2984 * It doesn't make sense to adjust a reference pointed
2985 * to by a symbolic ref based on expiring entries in
2986 * the symbolic reference's reflog. Nor can we update
2987 * a reference if there are no remaining reflog
2990 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
2991 !(type & REF_ISSYMREF) &&
2992 !is_null_oid(&cb.last_kept_oid);
2994 if (close_lock_file_gently(&reflog_lock)) {
2995 status |= error("couldn't write %s: %s", log_file,
2997 rollback_lock_file(&reflog_lock);
2998 } else if (update &&
2999 (write_in_full(get_lock_file_fd(&lock->lk),
3000 oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 ||
3001 write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
3002 close_ref_gently(lock) < 0)) {
3003 status |= error("couldn't write %s",
3004 get_lock_file_path(&lock->lk));
3005 rollback_lock_file(&reflog_lock);
3006 } else if (commit_lock_file(&reflog_lock)) {
3007 status |= error("unable to write reflog '%s' (%s)",
3008 log_file, strerror(errno));
3009 } else if (update && commit_ref(lock)) {
3010 status |= error("couldn't set %s", lock->ref_name);
3018 rollback_lock_file(&reflog_lock);
3024 static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
3026 struct files_ref_store *refs =
3027 files_downcast(ref_store, REF_STORE_WRITE, "init_db");
3028 struct strbuf sb = STRBUF_INIT;
3031 * Create .git/refs/{heads,tags}
3033 files_ref_path(refs, &sb, "refs/heads");
3034 safe_create_dir(sb.buf, 1);
3037 files_ref_path(refs, &sb, "refs/tags");
3038 safe_create_dir(sb.buf, 1);
3040 strbuf_release(&sb);
3044 struct ref_storage_be refs_be_files = {
3047 files_ref_store_create,
3049 files_transaction_prepare,
3050 files_transaction_finish,
3051 files_transaction_abort,
3052 files_initial_transaction_commit,
3055 files_create_symref,
3060 files_ref_iterator_begin,
3063 files_reflog_iterator_begin,
3064 files_for_each_reflog_ent,
3065 files_for_each_reflog_ent_reverse,
3066 files_reflog_exists,
3067 files_create_reflog,
3068 files_delete_reflog,