3 #include "refs-internal.h"
5 #include "../iterator.h"
6 #include "../dir-iterator.h"
7 #include "../lockfile.h"
14 struct object_id old_oid;
18 * Return true if refname, which has the specified oid and flags, can
19 * be resolved to an object in the database. If the referred-to object
20 * does not exist, emit a warning and return false.
22 static int ref_resolves_to_object(const char *refname,
23 const struct object_id *oid,
26 if (flags & REF_ISBROKEN)
28 if (!has_sha1_file(oid->hash)) {
29 error("%s does not point to a valid object!", refname);
36 * Return true if the reference described by entry can be resolved to
37 * an object in the database; otherwise, emit a warning and return
40 static int entry_resolves_to_object(struct ref_entry *entry)
42 return ref_resolves_to_object(entry->name,
43 &entry->u.value.oid, entry->flag);
46 struct packed_ref_cache {
47 struct ref_cache *cache;
50 * Count of references to the data structure in this instance,
51 * including the pointer from files_ref_store::packed if any.
52 * The data will not be freed as long as the reference count
55 unsigned int referrers;
58 * Iff the packed-refs file associated with this instance is
59 * currently locked for writing, this points at the associated
60 * lock (which is owned by somebody else). The referrer count
61 * is also incremented when the file is locked and decremented
62 * when it is unlocked.
64 struct lock_file *lock;
66 /* The metadata from when this packed-refs cache was read */
67 struct stat_validity validity;
71 * Future: need to be in "struct repository"
72 * when doing a full libification.
74 struct files_ref_store {
75 struct ref_store base;
76 unsigned int store_flags;
80 char *packed_refs_path;
82 struct ref_cache *loose;
83 struct packed_ref_cache *packed;
86 /* Lock used for the main packed-refs file: */
87 static struct lock_file packlock;
90 * Increment the reference count of *packed_refs.
92 static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
94 packed_refs->referrers++;
98 * Decrease the reference count of *packed_refs. If it goes to zero,
99 * free *packed_refs and return true; otherwise return false.
101 static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
103 if (!--packed_refs->referrers) {
104 free_ref_cache(packed_refs->cache);
105 stat_validity_clear(&packed_refs->validity);
113 static void clear_packed_ref_cache(struct files_ref_store *refs)
116 struct packed_ref_cache *packed_refs = refs->packed;
118 if (packed_refs->lock)
119 die("internal error: packed-ref cache cleared while locked");
121 release_packed_ref_cache(packed_refs);
125 static void clear_loose_ref_cache(struct files_ref_store *refs)
128 free_ref_cache(refs->loose);
134 * Create a new submodule ref cache and add it to the internal
137 static struct ref_store *files_ref_store_create(const char *gitdir,
140 struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
141 struct ref_store *ref_store = (struct ref_store *)refs;
142 struct strbuf sb = STRBUF_INIT;
144 base_ref_store_init(ref_store, &refs_be_files);
145 refs->store_flags = flags;
147 refs->gitdir = xstrdup(gitdir);
148 get_common_dir_noenv(&sb, gitdir);
149 refs->gitcommondir = strbuf_detach(&sb, NULL);
150 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
151 refs->packed_refs_path = strbuf_detach(&sb, NULL);
157 * Die if refs is not the main ref store. caller is used in any
158 * necessary error messages.
160 static void files_assert_main_repository(struct files_ref_store *refs,
163 if (refs->store_flags & REF_STORE_MAIN)
166 die("BUG: operation %s only allowed for main ref store", caller);
170 * Downcast ref_store to files_ref_store. Die if ref_store is not a
171 * files_ref_store. required_flags is compared with ref_store's
172 * store_flags to ensure the ref_store has all required capabilities.
173 * "caller" is used in any necessary error messages.
175 static struct files_ref_store *files_downcast(struct ref_store *ref_store,
176 unsigned int required_flags,
179 struct files_ref_store *refs;
181 if (ref_store->be != &refs_be_files)
182 die("BUG: ref_store is type \"%s\" not \"files\" in %s",
183 ref_store->be->name, caller);
185 refs = (struct files_ref_store *)ref_store;
187 if ((refs->store_flags & required_flags) != required_flags)
188 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
189 caller, required_flags, refs->store_flags);
194 /* The length of a peeled reference line in packed-refs, including EOL: */
195 #define PEELED_LINE_LENGTH 42
198 * The packed-refs header line that we write out. Perhaps other
199 * traits will be added later. The trailing space is required.
201 static const char PACKED_REFS_HEADER[] =
202 "# pack-refs with: peeled fully-peeled \n";
205 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
206 * Return a pointer to the refname within the line (null-terminated),
207 * or NULL if there was a problem.
209 static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
214 * 42: the answer to everything.
216 * In this case, it happens to be the answer to
217 * 40 (length of sha1 hex representation)
218 * +1 (space in between hex and name)
219 * +1 (newline at the end of the line)
224 if (get_sha1_hex(line->buf, sha1) < 0)
226 if (!isspace(line->buf[40]))
229 ref = line->buf + 41;
233 if (line->buf[line->len - 1] != '\n')
235 line->buf[--line->len] = 0;
241 * Read f, which is a packed-refs file, into dir.
243 * A comment line of the form "# pack-refs with: " may contain zero or
244 * more traits. We interpret the traits as follows:
248 * Probably no references are peeled. But if the file contains a
249 * peeled value for a reference, we will use it.
253 * References under "refs/tags/", if they *can* be peeled, *are*
254 * peeled in this file. References outside of "refs/tags/" are
255 * probably not peeled even if they could have been, but if we find
256 * a peeled value for such a reference we will use it.
260 * All references in the file that can be peeled are peeled.
261 * Inversely (and this is more important), any references in the
262 * file for which no peeled value is recorded is not peelable. This
263 * trait should typically be written alongside "peeled" for
264 * compatibility with older clients, but we do not require it
265 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
267 static void read_packed_refs(FILE *f, struct ref_dir *dir)
269 struct ref_entry *last = NULL;
270 struct strbuf line = STRBUF_INIT;
271 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
273 while (strbuf_getwholeline(&line, f, '\n') != EOF) {
274 unsigned char sha1[20];
278 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
279 if (strstr(traits, " fully-peeled "))
280 peeled = PEELED_FULLY;
281 else if (strstr(traits, " peeled "))
282 peeled = PEELED_TAGS;
283 /* perhaps other traits later as well */
287 refname = parse_ref_line(&line, sha1);
289 int flag = REF_ISPACKED;
291 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
292 if (!refname_is_safe(refname))
293 die("packed refname is dangerous: %s", refname);
295 flag |= REF_BAD_NAME | REF_ISBROKEN;
297 last = create_ref_entry(refname, sha1, flag, 0);
298 if (peeled == PEELED_FULLY ||
299 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
300 last->flag |= REF_KNOWS_PEELED;
301 add_ref_entry(dir, last);
305 line.buf[0] == '^' &&
306 line.len == PEELED_LINE_LENGTH &&
307 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
308 !get_sha1_hex(line.buf + 1, sha1)) {
309 hashcpy(last->u.value.peeled.hash, sha1);
311 * Regardless of what the file header said,
312 * we definitely know the value of *this*
315 last->flag |= REF_KNOWS_PEELED;
319 strbuf_release(&line);
322 static const char *files_packed_refs_path(struct files_ref_store *refs)
324 return refs->packed_refs_path;
327 static void files_reflog_path(struct files_ref_store *refs,
333 * FIXME: of course this is wrong in multi worktree
334 * setting. To be fixed real soon.
336 strbuf_addf(sb, "%s/logs", refs->gitcommondir);
340 switch (ref_type(refname)) {
341 case REF_TYPE_PER_WORKTREE:
342 case REF_TYPE_PSEUDOREF:
343 strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname);
345 case REF_TYPE_NORMAL:
346 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
349 die("BUG: unknown ref type %d of ref %s",
350 ref_type(refname), refname);
354 static void files_ref_path(struct files_ref_store *refs,
358 switch (ref_type(refname)) {
359 case REF_TYPE_PER_WORKTREE:
360 case REF_TYPE_PSEUDOREF:
361 strbuf_addf(sb, "%s/%s", refs->gitdir, refname);
363 case REF_TYPE_NORMAL:
364 strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
367 die("BUG: unknown ref type %d of ref %s",
368 ref_type(refname), refname);
373 * Get the packed_ref_cache for the specified files_ref_store,
374 * creating it if necessary.
376 static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
378 const char *packed_refs_file = files_packed_refs_path(refs);
381 !stat_validity_check(&refs->packed->validity, packed_refs_file))
382 clear_packed_ref_cache(refs);
387 refs->packed = xcalloc(1, sizeof(*refs->packed));
388 acquire_packed_ref_cache(refs->packed);
389 refs->packed->cache = create_ref_cache(&refs->base, NULL);
390 refs->packed->cache->root->flag &= ~REF_INCOMPLETE;
391 f = fopen(packed_refs_file, "r");
393 stat_validity_update(&refs->packed->validity, fileno(f));
394 read_packed_refs(f, get_ref_dir(refs->packed->cache->root));
401 static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
403 return get_ref_dir(packed_ref_cache->cache->root);
406 static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
408 return get_packed_ref_dir(get_packed_ref_cache(refs));
412 * Add a reference to the in-memory packed reference cache. This may
413 * only be called while the packed-refs file is locked (see
414 * lock_packed_refs()). To actually write the packed-refs file, call
415 * commit_packed_refs().
417 static void add_packed_ref(struct files_ref_store *refs,
418 const char *refname, const unsigned char *sha1)
420 struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
422 if (!packed_ref_cache->lock)
423 die("internal error: packed refs not locked");
424 add_ref_entry(get_packed_ref_dir(packed_ref_cache),
425 create_ref_entry(refname, sha1, REF_ISPACKED, 1));
429 * Read the loose references from the namespace dirname into dir
430 * (without recursing). dirname must end with '/'. dir must be the
431 * directory entry corresponding to dirname.
433 static void loose_fill_ref_dir(struct ref_store *ref_store,
434 struct ref_dir *dir, const char *dirname)
436 struct files_ref_store *refs =
437 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
440 int dirnamelen = strlen(dirname);
441 struct strbuf refname;
442 struct strbuf path = STRBUF_INIT;
445 files_ref_path(refs, &path, dirname);
446 path_baselen = path.len;
448 d = opendir(path.buf);
450 strbuf_release(&path);
454 strbuf_init(&refname, dirnamelen + 257);
455 strbuf_add(&refname, dirname, dirnamelen);
457 while ((de = readdir(d)) != NULL) {
458 unsigned char sha1[20];
462 if (de->d_name[0] == '.')
464 if (ends_with(de->d_name, ".lock"))
466 strbuf_addstr(&refname, de->d_name);
467 strbuf_addstr(&path, de->d_name);
468 if (stat(path.buf, &st) < 0) {
469 ; /* silently ignore */
470 } else if (S_ISDIR(st.st_mode)) {
471 strbuf_addch(&refname, '/');
472 add_entry_to_dir(dir,
473 create_dir_entry(dir->cache, refname.buf,
476 if (!refs_resolve_ref_unsafe(&refs->base,
481 flag |= REF_ISBROKEN;
482 } else if (is_null_sha1(sha1)) {
484 * It is so astronomically unlikely
485 * that NULL_SHA1 is the SHA-1 of an
486 * actual object that we consider its
487 * appearance in a loose reference
488 * file to be repo corruption
489 * (probably due to a software bug).
491 flag |= REF_ISBROKEN;
494 if (check_refname_format(refname.buf,
495 REFNAME_ALLOW_ONELEVEL)) {
496 if (!refname_is_safe(refname.buf))
497 die("loose refname is dangerous: %s", refname.buf);
499 flag |= REF_BAD_NAME | REF_ISBROKEN;
501 add_entry_to_dir(dir,
502 create_ref_entry(refname.buf, sha1, flag, 0));
504 strbuf_setlen(&refname, dirnamelen);
505 strbuf_setlen(&path, path_baselen);
507 strbuf_release(&refname);
508 strbuf_release(&path);
512 static struct ref_dir *get_loose_refs(struct files_ref_store *refs)
516 * Mark the top-level directory complete because we
517 * are about to read the only subdirectory that can
520 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
522 /* We're going to fill the top level ourselves: */
523 refs->loose->root->flag &= ~REF_INCOMPLETE;
526 * Add an incomplete entry for "refs/" (to be filled
529 add_entry_to_dir(get_ref_dir(refs->loose->root),
530 create_dir_entry(refs->loose, "refs/", 5, 1));
532 return get_ref_dir(refs->loose->root);
536 * Return the ref_entry for the given refname from the packed
537 * references. If it does not exist, return NULL.
539 static struct ref_entry *get_packed_ref(struct files_ref_store *refs,
542 return find_ref_entry(get_packed_refs(refs), refname);
546 * A loose ref file doesn't exist; check for a packed ref.
548 static int resolve_packed_ref(struct files_ref_store *refs,
550 unsigned char *sha1, unsigned int *flags)
552 struct ref_entry *entry;
555 * The loose reference file does not exist; check for a packed
558 entry = get_packed_ref(refs, refname);
560 hashcpy(sha1, entry->u.value.oid.hash);
561 *flags |= REF_ISPACKED;
564 /* refname is not a packed reference. */
568 static int files_read_raw_ref(struct ref_store *ref_store,
569 const char *refname, unsigned char *sha1,
570 struct strbuf *referent, unsigned int *type)
572 struct files_ref_store *refs =
573 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
574 struct strbuf sb_contents = STRBUF_INIT;
575 struct strbuf sb_path = STRBUF_INIT;
582 int remaining_retries = 3;
585 strbuf_reset(&sb_path);
587 files_ref_path(refs, &sb_path, refname);
593 * We might have to loop back here to avoid a race
594 * condition: first we lstat() the file, then we try
595 * to read it as a link or as a file. But if somebody
596 * changes the type of the file (file <-> directory
597 * <-> symlink) between the lstat() and reading, then
598 * we don't want to report that as an error but rather
599 * try again starting with the lstat().
601 * We'll keep a count of the retries, though, just to avoid
602 * any confusing situation sending us into an infinite loop.
605 if (remaining_retries-- <= 0)
608 if (lstat(path, &st) < 0) {
611 if (resolve_packed_ref(refs, refname, sha1, type)) {
619 /* Follow "normalized" - ie "refs/.." symlinks by hand */
620 if (S_ISLNK(st.st_mode)) {
621 strbuf_reset(&sb_contents);
622 if (strbuf_readlink(&sb_contents, path, 0) < 0) {
623 if (errno == ENOENT || errno == EINVAL)
624 /* inconsistent with lstat; retry */
629 if (starts_with(sb_contents.buf, "refs/") &&
630 !check_refname_format(sb_contents.buf, 0)) {
631 strbuf_swap(&sb_contents, referent);
632 *type |= REF_ISSYMREF;
637 * It doesn't look like a refname; fall through to just
638 * treating it like a non-symlink, and reading whatever it
643 /* Is it a directory? */
644 if (S_ISDIR(st.st_mode)) {
646 * Even though there is a directory where the loose
647 * ref is supposed to be, there could still be a
650 if (resolve_packed_ref(refs, refname, sha1, type)) {
659 * Anything else, just open it and try to use it as
662 fd = open(path, O_RDONLY);
664 if (errno == ENOENT && !S_ISLNK(st.st_mode))
665 /* inconsistent with lstat; retry */
670 strbuf_reset(&sb_contents);
671 if (strbuf_read(&sb_contents, fd, 256) < 0) {
672 int save_errno = errno;
678 strbuf_rtrim(&sb_contents);
679 buf = sb_contents.buf;
680 if (starts_with(buf, "ref:")) {
682 while (isspace(*buf))
685 strbuf_reset(referent);
686 strbuf_addstr(referent, buf);
687 *type |= REF_ISSYMREF;
693 * Please note that FETCH_HEAD has additional
694 * data after the sha.
696 if (get_sha1_hex(buf, sha1) ||
697 (buf[40] != '\0' && !isspace(buf[40]))) {
698 *type |= REF_ISBROKEN;
707 strbuf_release(&sb_path);
708 strbuf_release(&sb_contents);
713 static void unlock_ref(struct ref_lock *lock)
715 /* Do not free lock->lk -- atexit() still looks at them */
717 rollback_lock_file(lock->lk);
718 free(lock->ref_name);
723 * Lock refname, without following symrefs, and set *lock_p to point
724 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
725 * and type similarly to read_raw_ref().
727 * The caller must verify that refname is a "safe" reference name (in
728 * the sense of refname_is_safe()) before calling this function.
730 * If the reference doesn't already exist, verify that refname doesn't
731 * have a D/F conflict with any existing references. extras and skip
732 * are passed to refs_verify_refname_available() for this check.
734 * If mustexist is not set and the reference is not found or is
735 * broken, lock the reference anyway but clear sha1.
737 * Return 0 on success. On failure, write an error message to err and
738 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
740 * Implementation note: This function is basically
745 * but it includes a lot more code to
746 * - Deal with possible races with other processes
747 * - Avoid calling refs_verify_refname_available() when it can be
748 * avoided, namely if we were successfully able to read the ref
749 * - Generate informative error messages in the case of failure
751 static int lock_raw_ref(struct files_ref_store *refs,
752 const char *refname, int mustexist,
753 const struct string_list *extras,
754 const struct string_list *skip,
755 struct ref_lock **lock_p,
756 struct strbuf *referent,
760 struct ref_lock *lock;
761 struct strbuf ref_file = STRBUF_INIT;
762 int attempts_remaining = 3;
763 int ret = TRANSACTION_GENERIC_ERROR;
766 files_assert_main_repository(refs, "lock_raw_ref");
770 /* First lock the file so it can't change out from under us. */
772 *lock_p = lock = xcalloc(1, sizeof(*lock));
774 lock->ref_name = xstrdup(refname);
775 files_ref_path(refs, &ref_file, refname);
778 switch (safe_create_leading_directories(ref_file.buf)) {
783 * Suppose refname is "refs/foo/bar". We just failed
784 * to create the containing directory, "refs/foo",
785 * because there was a non-directory in the way. This
786 * indicates a D/F conflict, probably because of
787 * another reference such as "refs/foo". There is no
788 * reason to expect this error to be transitory.
790 if (refs_verify_refname_available(&refs->base, refname,
791 extras, skip, err)) {
794 * To the user the relevant error is
795 * that the "mustexist" reference is
799 strbuf_addf(err, "unable to resolve reference '%s'",
803 * The error message set by
804 * refs_verify_refname_available() is
807 ret = TRANSACTION_NAME_CONFLICT;
811 * The file that is in the way isn't a loose
812 * reference. Report it as a low-level
815 strbuf_addf(err, "unable to create lock file %s.lock; "
816 "non-directory in the way",
821 /* Maybe another process was tidying up. Try again. */
822 if (--attempts_remaining > 0)
826 strbuf_addf(err, "unable to create directory for %s",
832 lock->lk = xcalloc(1, sizeof(struct lock_file));
834 if (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {
835 if (errno == ENOENT && --attempts_remaining > 0) {
837 * Maybe somebody just deleted one of the
838 * directories leading to ref_file. Try
843 unable_to_lock_message(ref_file.buf, errno, err);
849 * Now we hold the lock and can read the reference without
850 * fear that its value will change.
853 if (files_read_raw_ref(&refs->base, refname,
854 lock->old_oid.hash, referent, type)) {
855 if (errno == ENOENT) {
857 /* Garden variety missing reference. */
858 strbuf_addf(err, "unable to resolve reference '%s'",
863 * Reference is missing, but that's OK. We
864 * know that there is not a conflict with
865 * another loose reference because
866 * (supposing that we are trying to lock
867 * reference "refs/foo/bar"):
869 * - We were successfully able to create
870 * the lockfile refs/foo/bar.lock, so we
871 * know there cannot be a loose reference
874 * - We got ENOENT and not EISDIR, so we
875 * know that there cannot be a loose
876 * reference named "refs/foo/bar/baz".
879 } else if (errno == EISDIR) {
881 * There is a directory in the way. It might have
882 * contained references that have been deleted. If
883 * we don't require that the reference already
884 * exists, try to remove the directory so that it
885 * doesn't cause trouble when we want to rename the
886 * lockfile into place later.
889 /* Garden variety missing reference. */
890 strbuf_addf(err, "unable to resolve reference '%s'",
893 } else if (remove_dir_recursively(&ref_file,
894 REMOVE_DIR_EMPTY_ONLY)) {
895 if (refs_verify_refname_available(
896 &refs->base, refname,
897 extras, skip, err)) {
899 * The error message set by
900 * verify_refname_available() is OK.
902 ret = TRANSACTION_NAME_CONFLICT;
906 * We can't delete the directory,
907 * but we also don't know of any
908 * references that it should
911 strbuf_addf(err, "there is a non-empty directory '%s' "
912 "blocking reference '%s'",
913 ref_file.buf, refname);
917 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) {
918 strbuf_addf(err, "unable to resolve reference '%s': "
919 "reference broken", refname);
922 strbuf_addf(err, "unable to resolve reference '%s': %s",
923 refname, strerror(errno));
928 * If the ref did not exist and we are creating it,
929 * make sure there is no existing ref that conflicts
932 if (refs_verify_refname_available(
933 &refs->base, refname,
946 strbuf_release(&ref_file);
950 static int files_peel_ref(struct ref_store *ref_store,
951 const char *refname, unsigned char *sha1)
953 struct files_ref_store *refs =
954 files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
957 unsigned char base[20];
959 if (current_ref_iter && current_ref_iter->refname == refname) {
960 struct object_id peeled;
962 if (ref_iterator_peel(current_ref_iter, &peeled))
964 hashcpy(sha1, peeled.hash);
968 if (refs_read_ref_full(ref_store, refname,
969 RESOLVE_REF_READING, base, &flag))
973 * If the reference is packed, read its ref_entry from the
974 * cache in the hope that we already know its peeled value.
975 * We only try this optimization on packed references because
976 * (a) forcing the filling of the loose reference cache could
977 * be expensive and (b) loose references anyway usually do not
978 * have REF_KNOWS_PEELED.
980 if (flag & REF_ISPACKED) {
981 struct ref_entry *r = get_packed_ref(refs, refname);
983 if (peel_entry(r, 0))
985 hashcpy(sha1, r->u.value.peeled.hash);
990 return peel_object(base, sha1);
993 struct files_ref_iterator {
994 struct ref_iterator base;
996 struct packed_ref_cache *packed_ref_cache;
997 struct ref_iterator *iter0;
1001 static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
1003 struct files_ref_iterator *iter =
1004 (struct files_ref_iterator *)ref_iterator;
1007 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
1008 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
1009 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
1012 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
1013 !ref_resolves_to_object(iter->iter0->refname,
1015 iter->iter0->flags))
1018 iter->base.refname = iter->iter0->refname;
1019 iter->base.oid = iter->iter0->oid;
1020 iter->base.flags = iter->iter0->flags;
1025 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
1031 static int files_ref_iterator_peel(struct ref_iterator *ref_iterator,
1032 struct object_id *peeled)
1034 struct files_ref_iterator *iter =
1035 (struct files_ref_iterator *)ref_iterator;
1037 return ref_iterator_peel(iter->iter0, peeled);
1040 static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
1042 struct files_ref_iterator *iter =
1043 (struct files_ref_iterator *)ref_iterator;
1047 ok = ref_iterator_abort(iter->iter0);
1049 release_packed_ref_cache(iter->packed_ref_cache);
1050 base_ref_iterator_free(ref_iterator);
1054 static struct ref_iterator_vtable files_ref_iterator_vtable = {
1055 files_ref_iterator_advance,
1056 files_ref_iterator_peel,
1057 files_ref_iterator_abort
1060 static struct ref_iterator *files_ref_iterator_begin(
1061 struct ref_store *ref_store,
1062 const char *prefix, unsigned int flags)
1064 struct files_ref_store *refs;
1065 struct ref_dir *loose_dir, *packed_dir;
1066 struct ref_iterator *loose_iter, *packed_iter;
1067 struct files_ref_iterator *iter;
1068 struct ref_iterator *ref_iterator;
1070 if (ref_paranoia < 0)
1071 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
1073 flags |= DO_FOR_EACH_INCLUDE_BROKEN;
1075 refs = files_downcast(ref_store,
1076 REF_STORE_READ | (ref_paranoia ? 0 : REF_STORE_ODB),
1077 "ref_iterator_begin");
1079 iter = xcalloc(1, sizeof(*iter));
1080 ref_iterator = &iter->base;
1081 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
1084 * We must make sure that all loose refs are read before
1085 * accessing the packed-refs file; this avoids a race
1086 * condition if loose refs are migrated to the packed-refs
1087 * file by a simultaneous process, but our in-memory view is
1088 * from before the migration. We ensure this as follows:
1089 * First, we call prime_ref_dir(), which pre-reads the loose
1090 * references for the subtree into the cache. (If they've
1091 * already been read, that's OK; we only need to guarantee
1092 * that they're read before the packed refs, not *how much*
1093 * before.) After that, we call get_packed_ref_cache(), which
1094 * internally checks whether the packed-ref cache is up to
1095 * date with what is on disk, and re-reads it if not.
1098 loose_dir = get_loose_refs(refs);
1100 if (prefix && *prefix)
1101 loose_dir = find_containing_dir(loose_dir, prefix, 0);
1104 prime_ref_dir(loose_dir);
1105 loose_iter = cache_ref_iterator_begin(loose_dir);
1107 /* There's nothing to iterate over. */
1108 loose_iter = empty_ref_iterator_begin();
1111 iter->packed_ref_cache = get_packed_ref_cache(refs);
1112 acquire_packed_ref_cache(iter->packed_ref_cache);
1113 packed_dir = get_packed_ref_dir(iter->packed_ref_cache);
1115 if (prefix && *prefix)
1116 packed_dir = find_containing_dir(packed_dir, prefix, 0);
1119 packed_iter = cache_ref_iterator_begin(packed_dir);
1121 /* There's nothing to iterate over. */
1122 packed_iter = empty_ref_iterator_begin();
1125 iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);
1126 iter->flags = flags;
1128 return ref_iterator;
1132 * Verify that the reference locked by lock has the value old_sha1.
1133 * Fail if the reference doesn't exist and mustexist is set. Return 0
1134 * on success. On error, write an error message to err, set errno, and
1135 * return a negative value.
1137 static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
1138 const unsigned char *old_sha1, int mustexist,
1143 if (refs_read_ref_full(ref_store, lock->ref_name,
1144 mustexist ? RESOLVE_REF_READING : 0,
1145 lock->old_oid.hash, NULL)) {
1147 int save_errno = errno;
1148 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
1152 oidclr(&lock->old_oid);
1156 if (old_sha1 && hashcmp(lock->old_oid.hash, old_sha1)) {
1157 strbuf_addf(err, "ref '%s' is at %s but expected %s",
1159 oid_to_hex(&lock->old_oid),
1160 sha1_to_hex(old_sha1));
1167 static int remove_empty_directories(struct strbuf *path)
1170 * we want to create a file but there is a directory there;
1171 * if that is an empty directory (or a directory that contains
1172 * only empty directories), remove them.
1174 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
1177 static int create_reflock(const char *path, void *cb)
1179 struct lock_file *lk = cb;
1181 return hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;
1185 * Locks a ref returning the lock on success and NULL on failure.
1186 * On failure errno is set to something meaningful.
1188 static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
1189 const char *refname,
1190 const unsigned char *old_sha1,
1191 const struct string_list *extras,
1192 const struct string_list *skip,
1193 unsigned int flags, int *type,
1196 struct strbuf ref_file = STRBUF_INIT;
1197 struct ref_lock *lock;
1199 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
1200 int resolve_flags = RESOLVE_REF_NO_RECURSE;
1203 files_assert_main_repository(refs, "lock_ref_sha1_basic");
1206 lock = xcalloc(1, sizeof(struct ref_lock));
1209 resolve_flags |= RESOLVE_REF_READING;
1210 if (flags & REF_DELETING)
1211 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
1213 files_ref_path(refs, &ref_file, refname);
1214 resolved = !!refs_resolve_ref_unsafe(&refs->base,
1215 refname, resolve_flags,
1216 lock->old_oid.hash, type);
1217 if (!resolved && errno == EISDIR) {
1219 * we are trying to lock foo but we used to
1220 * have foo/bar which now does not exist;
1221 * it is normal for the empty directory 'foo'
1224 if (remove_empty_directories(&ref_file)) {
1226 if (!refs_verify_refname_available(
1228 refname, extras, skip, err))
1229 strbuf_addf(err, "there are still refs under '%s'",
1233 resolved = !!refs_resolve_ref_unsafe(&refs->base,
1234 refname, resolve_flags,
1235 lock->old_oid.hash, type);
1239 if (last_errno != ENOTDIR ||
1240 !refs_verify_refname_available(&refs->base, refname,
1242 strbuf_addf(err, "unable to resolve reference '%s': %s",
1243 refname, strerror(last_errno));
1249 * If the ref did not exist and we are creating it, make sure
1250 * there is no existing packed ref whose name begins with our
1251 * refname, nor a packed ref whose name is a proper prefix of
1254 if (is_null_oid(&lock->old_oid) &&
1255 refs_verify_refname_available(&refs->base, refname,
1256 extras, skip, err)) {
1257 last_errno = ENOTDIR;
1261 lock->lk = xcalloc(1, sizeof(struct lock_file));
1263 lock->ref_name = xstrdup(refname);
1265 if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {
1267 unable_to_lock_message(ref_file.buf, errno, err);
1271 if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {
1282 strbuf_release(&ref_file);
1288 * Write an entry to the packed-refs file for the specified refname.
1289 * If peeled is non-NULL, write it as the entry's peeled value.
1291 static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1,
1292 unsigned char *peeled)
1294 fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
1296 fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
1300 * An each_ref_entry_fn that writes the entry to a packed-refs file.
1302 static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
1304 enum peel_status peel_status = peel_entry(entry, 0);
1306 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
1307 error("internal error: %s is not a valid packed reference!",
1309 write_packed_entry(cb_data, entry->name, entry->u.value.oid.hash,
1310 peel_status == PEEL_PEELED ?
1311 entry->u.value.peeled.hash : NULL);
1316 * Lock the packed-refs file for writing. Flags is passed to
1317 * hold_lock_file_for_update(). Return 0 on success. On errors, set
1318 * errno appropriately and return a nonzero value.
1320 static int lock_packed_refs(struct files_ref_store *refs, int flags)
1322 static int timeout_configured = 0;
1323 static int timeout_value = 1000;
1324 struct packed_ref_cache *packed_ref_cache;
1326 files_assert_main_repository(refs, "lock_packed_refs");
1328 if (!timeout_configured) {
1329 git_config_get_int("core.packedrefstimeout", &timeout_value);
1330 timeout_configured = 1;
1333 if (hold_lock_file_for_update_timeout(
1334 &packlock, files_packed_refs_path(refs),
1335 flags, timeout_value) < 0)
1338 * Get the current packed-refs while holding the lock. If the
1339 * packed-refs file has been modified since we last read it,
1340 * this will automatically invalidate the cache and re-read
1341 * the packed-refs file.
1343 packed_ref_cache = get_packed_ref_cache(refs);
1344 packed_ref_cache->lock = &packlock;
1345 /* Increment the reference count to prevent it from being freed: */
1346 acquire_packed_ref_cache(packed_ref_cache);
1351 * Write the current version of the packed refs cache from memory to
1352 * disk. The packed-refs file must already be locked for writing (see
1353 * lock_packed_refs()). Return zero on success. On errors, set errno
1354 * and return a nonzero value
1356 static int commit_packed_refs(struct files_ref_store *refs)
1358 struct packed_ref_cache *packed_ref_cache =
1359 get_packed_ref_cache(refs);
1364 files_assert_main_repository(refs, "commit_packed_refs");
1366 if (!packed_ref_cache->lock)
1367 die("internal error: packed-refs not locked");
1369 out = fdopen_lock_file(packed_ref_cache->lock, "w");
1371 die_errno("unable to fdopen packed-refs descriptor");
1373 fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
1374 do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
1375 0, write_packed_entry_fn, out);
1377 if (commit_lock_file(packed_ref_cache->lock)) {
1381 packed_ref_cache->lock = NULL;
1382 release_packed_ref_cache(packed_ref_cache);
1388 * Rollback the lockfile for the packed-refs file, and discard the
1389 * in-memory packed reference cache. (The packed-refs file will be
1390 * read anew if it is needed again after this function is called.)
1392 static void rollback_packed_refs(struct files_ref_store *refs)
1394 struct packed_ref_cache *packed_ref_cache =
1395 get_packed_ref_cache(refs);
1397 files_assert_main_repository(refs, "rollback_packed_refs");
1399 if (!packed_ref_cache->lock)
1400 die("internal error: packed-refs not locked");
1401 rollback_lock_file(packed_ref_cache->lock);
1402 packed_ref_cache->lock = NULL;
1403 release_packed_ref_cache(packed_ref_cache);
1404 clear_packed_ref_cache(refs);
1407 struct ref_to_prune {
1408 struct ref_to_prune *next;
1409 unsigned char sha1[20];
1410 char name[FLEX_ARRAY];
1413 struct pack_refs_cb_data {
1415 struct ref_dir *packed_refs;
1416 struct ref_to_prune *ref_to_prune;
1420 * An each_ref_entry_fn that is run over loose references only. If
1421 * the loose reference can be packed, add an entry in the packed ref
1422 * cache. If the reference should be pruned, also add it to
1423 * ref_to_prune in the pack_refs_cb_data.
1425 static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
1427 struct pack_refs_cb_data *cb = cb_data;
1428 enum peel_status peel_status;
1429 struct ref_entry *packed_entry;
1430 int is_tag_ref = starts_with(entry->name, "refs/tags/");
1432 /* Do not pack per-worktree refs: */
1433 if (ref_type(entry->name) != REF_TYPE_NORMAL)
1436 /* ALWAYS pack tags */
1437 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)
1440 /* Do not pack symbolic or broken refs: */
1441 if ((entry->flag & REF_ISSYMREF) || !entry_resolves_to_object(entry))
1444 /* Add a packed ref cache entry equivalent to the loose entry. */
1445 peel_status = peel_entry(entry, 1);
1446 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
1447 die("internal error peeling reference %s (%s)",
1448 entry->name, oid_to_hex(&entry->u.value.oid));
1449 packed_entry = find_ref_entry(cb->packed_refs, entry->name);
1451 /* Overwrite existing packed entry with info from loose entry */
1452 packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED;
1453 oidcpy(&packed_entry->u.value.oid, &entry->u.value.oid);
1455 packed_entry = create_ref_entry(entry->name, entry->u.value.oid.hash,
1456 REF_ISPACKED | REF_KNOWS_PEELED, 0);
1457 add_ref_entry(cb->packed_refs, packed_entry);
1459 oidcpy(&packed_entry->u.value.peeled, &entry->u.value.peeled);
1461 /* Schedule the loose reference for pruning if requested. */
1462 if ((cb->flags & PACK_REFS_PRUNE)) {
1463 struct ref_to_prune *n;
1464 FLEX_ALLOC_STR(n, name, entry->name);
1465 hashcpy(n->sha1, entry->u.value.oid.hash);
1466 n->next = cb->ref_to_prune;
1467 cb->ref_to_prune = n;
1473 REMOVE_EMPTY_PARENTS_REF = 0x01,
1474 REMOVE_EMPTY_PARENTS_REFLOG = 0x02
1478 * Remove empty parent directories associated with the specified
1479 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1480 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1481 * REMOVE_EMPTY_PARENTS_REFLOG.
1483 static void try_remove_empty_parents(struct files_ref_store *refs,
1484 const char *refname,
1487 struct strbuf buf = STRBUF_INIT;
1488 struct strbuf sb = STRBUF_INIT;
1492 strbuf_addstr(&buf, refname);
1494 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
1495 while (*p && *p != '/')
1497 /* tolerate duplicate slashes; see check_refname_format() */
1501 q = buf.buf + buf.len;
1502 while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
1503 while (q > p && *q != '/')
1505 while (q > p && *(q-1) == '/')
1509 strbuf_setlen(&buf, q - buf.buf);
1512 files_ref_path(refs, &sb, buf.buf);
1513 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
1514 flags &= ~REMOVE_EMPTY_PARENTS_REF;
1517 files_reflog_path(refs, &sb, buf.buf);
1518 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
1519 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
1521 strbuf_release(&buf);
1522 strbuf_release(&sb);
1525 /* make sure nobody touched the ref, and unlink */
1526 static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
1528 struct ref_transaction *transaction;
1529 struct strbuf err = STRBUF_INIT;
1531 if (check_refname_format(r->name, 0))
1534 transaction = ref_store_transaction_begin(&refs->base, &err);
1536 ref_transaction_delete(transaction, r->name, r->sha1,
1537 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
1538 ref_transaction_commit(transaction, &err)) {
1539 ref_transaction_free(transaction);
1540 error("%s", err.buf);
1541 strbuf_release(&err);
1544 ref_transaction_free(transaction);
1545 strbuf_release(&err);
1548 static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r)
1556 static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1558 struct files_ref_store *refs =
1559 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1561 struct pack_refs_cb_data cbdata;
1563 memset(&cbdata, 0, sizeof(cbdata));
1564 cbdata.flags = flags;
1566 lock_packed_refs(refs, LOCK_DIE_ON_ERROR);
1567 cbdata.packed_refs = get_packed_refs(refs);
1569 do_for_each_entry_in_dir(get_loose_refs(refs), 0,
1570 pack_if_possible_fn, &cbdata);
1572 if (commit_packed_refs(refs))
1573 die_errno("unable to overwrite old ref-pack file");
1575 prune_refs(refs, cbdata.ref_to_prune);
1580 * Rewrite the packed-refs file, omitting any refs listed in
1581 * 'refnames'. On error, leave packed-refs unchanged, write an error
1582 * message to 'err', and return a nonzero value.
1584 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
1586 static int repack_without_refs(struct files_ref_store *refs,
1587 struct string_list *refnames, struct strbuf *err)
1589 struct ref_dir *packed;
1590 struct string_list_item *refname;
1591 int ret, needs_repacking = 0, removed = 0;
1593 files_assert_main_repository(refs, "repack_without_refs");
1596 /* Look for a packed ref */
1597 for_each_string_list_item(refname, refnames) {
1598 if (get_packed_ref(refs, refname->string)) {
1599 needs_repacking = 1;
1604 /* Avoid locking if we have nothing to do */
1605 if (!needs_repacking)
1606 return 0; /* no refname exists in packed refs */
1608 if (lock_packed_refs(refs, 0)) {
1609 unable_to_lock_message(files_packed_refs_path(refs), errno, err);
1612 packed = get_packed_refs(refs);
1614 /* Remove refnames from the cache */
1615 for_each_string_list_item(refname, refnames)
1616 if (remove_entry_from_dir(packed, refname->string) != -1)
1620 * All packed entries disappeared while we were
1621 * acquiring the lock.
1623 rollback_packed_refs(refs);
1627 /* Write what remains */
1628 ret = commit_packed_refs(refs);
1630 strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
1635 static int files_delete_refs(struct ref_store *ref_store,
1636 struct string_list *refnames, unsigned int flags)
1638 struct files_ref_store *refs =
1639 files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1640 struct strbuf err = STRBUF_INIT;
1646 result = repack_without_refs(refs, refnames, &err);
1649 * If we failed to rewrite the packed-refs file, then
1650 * it is unsafe to try to remove loose refs, because
1651 * doing so might expose an obsolete packed value for
1652 * a reference that might even point at an object that
1653 * has been garbage collected.
1655 if (refnames->nr == 1)
1656 error(_("could not delete reference %s: %s"),
1657 refnames->items[0].string, err.buf);
1659 error(_("could not delete references: %s"), err.buf);
1664 for (i = 0; i < refnames->nr; i++) {
1665 const char *refname = refnames->items[i].string;
1667 if (refs_delete_ref(&refs->base, NULL, refname, NULL, flags))
1668 result |= error(_("could not remove reference %s"), refname);
1672 strbuf_release(&err);
1677 * People using contrib's git-new-workdir have .git/logs/refs ->
1678 * /some/other/path/.git/logs/refs, and that may live on another device.
1680 * IOW, to avoid cross device rename errors, the temporary renamed log must
1681 * live into logs/refs.
1683 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1686 const char *tmp_renamed_log;
1690 static int rename_tmp_log_callback(const char *path, void *cb_data)
1692 struct rename_cb *cb = cb_data;
1694 if (rename(cb->tmp_renamed_log, path)) {
1696 * rename(a, b) when b is an existing directory ought
1697 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1698 * Sheesh. Record the true errno for error reporting,
1699 * but report EISDIR to raceproof_create_file() so
1700 * that it knows to retry.
1702 cb->true_errno = errno;
1703 if (errno == ENOTDIR)
1711 static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
1713 struct strbuf path = STRBUF_INIT;
1714 struct strbuf tmp = STRBUF_INIT;
1715 struct rename_cb cb;
1718 files_reflog_path(refs, &path, newrefname);
1719 files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
1720 cb.tmp_renamed_log = tmp.buf;
1721 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
1723 if (errno == EISDIR)
1724 error("directory not empty: %s", path.buf);
1726 error("unable to move logfile %s to %s: %s",
1728 strerror(cb.true_errno));
1731 strbuf_release(&path);
1732 strbuf_release(&tmp);
1736 static int write_ref_to_lockfile(struct ref_lock *lock,
1737 const unsigned char *sha1, struct strbuf *err);
1738 static int commit_ref_update(struct files_ref_store *refs,
1739 struct ref_lock *lock,
1740 const unsigned char *sha1, const char *logmsg,
1741 struct strbuf *err);
1743 static int files_rename_ref(struct ref_store *ref_store,
1744 const char *oldrefname, const char *newrefname,
1747 struct files_ref_store *refs =
1748 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
1749 unsigned char sha1[20], orig_sha1[20];
1750 int flag = 0, logmoved = 0;
1751 struct ref_lock *lock;
1752 struct stat loginfo;
1753 struct strbuf sb_oldref = STRBUF_INIT;
1754 struct strbuf sb_newref = STRBUF_INIT;
1755 struct strbuf tmp_renamed_log = STRBUF_INIT;
1757 struct strbuf err = STRBUF_INIT;
1759 files_reflog_path(refs, &sb_oldref, oldrefname);
1760 files_reflog_path(refs, &sb_newref, newrefname);
1761 files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
1763 log = !lstat(sb_oldref.buf, &loginfo);
1764 if (log && S_ISLNK(loginfo.st_mode)) {
1765 ret = error("reflog for %s is a symlink", oldrefname);
1769 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1770 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1771 orig_sha1, &flag)) {
1772 ret = error("refname %s not found", oldrefname);
1776 if (flag & REF_ISSYMREF) {
1777 ret = error("refname %s is a symbolic ref, renaming it is not supported",
1781 if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
1786 if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
1787 ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1788 oldrefname, strerror(errno));
1792 if (refs_delete_ref(&refs->base, logmsg, oldrefname,
1793 orig_sha1, REF_NODEREF)) {
1794 error("unable to delete old %s", oldrefname);
1799 * Since we are doing a shallow lookup, sha1 is not the
1800 * correct value to pass to delete_ref as old_sha1. But that
1801 * doesn't matter, because an old_sha1 check wouldn't add to
1802 * the safety anyway; we want to delete the reference whatever
1803 * its current value.
1805 if (!refs_read_ref_full(&refs->base, newrefname,
1806 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1808 refs_delete_ref(&refs->base, NULL, newrefname,
1809 NULL, REF_NODEREF)) {
1810 if (errno == EISDIR) {
1811 struct strbuf path = STRBUF_INIT;
1814 files_ref_path(refs, &path, newrefname);
1815 result = remove_empty_directories(&path);
1816 strbuf_release(&path);
1819 error("Directory not empty: %s", newrefname);
1823 error("unable to delete existing %s", newrefname);
1828 if (log && rename_tmp_log(refs, newrefname))
1833 lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
1834 REF_NODEREF, NULL, &err);
1836 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1837 strbuf_release(&err);
1840 hashcpy(lock->old_oid.hash, orig_sha1);
1842 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
1843 commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {
1844 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1845 strbuf_release(&err);
1853 lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,
1854 REF_NODEREF, NULL, &err);
1856 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1857 strbuf_release(&err);
1861 flag = log_all_ref_updates;
1862 log_all_ref_updates = LOG_REFS_NONE;
1863 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
1864 commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {
1865 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1866 strbuf_release(&err);
1868 log_all_ref_updates = flag;
1871 if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
1872 error("unable to restore logfile %s from %s: %s",
1873 oldrefname, newrefname, strerror(errno));
1874 if (!logmoved && log &&
1875 rename(tmp_renamed_log.buf, sb_oldref.buf))
1876 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
1877 oldrefname, strerror(errno));
1880 strbuf_release(&sb_newref);
1881 strbuf_release(&sb_oldref);
1882 strbuf_release(&tmp_renamed_log);
1887 static int close_ref(struct ref_lock *lock)
1889 if (close_lock_file(lock->lk))
1894 static int commit_ref(struct ref_lock *lock)
1896 char *path = get_locked_file_path(lock->lk);
1899 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1901 * There is a directory at the path we want to rename
1902 * the lockfile to. Hopefully it is empty; try to
1905 size_t len = strlen(path);
1906 struct strbuf sb_path = STRBUF_INIT;
1908 strbuf_attach(&sb_path, path, len, len);
1911 * If this fails, commit_lock_file() will also fail
1912 * and will report the problem.
1914 remove_empty_directories(&sb_path);
1915 strbuf_release(&sb_path);
1920 if (commit_lock_file(lock->lk))
1925 static int open_or_create_logfile(const char *path, void *cb)
1929 *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1930 return (*fd < 0) ? -1 : 0;
1934 * Create a reflog for a ref. If force_create = 0, only create the
1935 * reflog for certain refs (those for which should_autocreate_reflog
1936 * returns non-zero). Otherwise, create it regardless of the reference
1937 * name. If the logfile already existed or was created, return 0 and
1938 * set *logfd to the file descriptor opened for appending to the file.
1939 * If no logfile exists and we decided not to create one, return 0 and
1940 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1943 static int log_ref_setup(struct files_ref_store *refs,
1944 const char *refname, int force_create,
1945 int *logfd, struct strbuf *err)
1947 struct strbuf logfile_sb = STRBUF_INIT;
1950 files_reflog_path(refs, &logfile_sb, refname);
1951 logfile = strbuf_detach(&logfile_sb, NULL);
1953 if (force_create || should_autocreate_reflog(refname)) {
1954 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
1955 if (errno == ENOENT)
1956 strbuf_addf(err, "unable to create directory for '%s': "
1957 "%s", logfile, strerror(errno));
1958 else if (errno == EISDIR)
1959 strbuf_addf(err, "there are still logs under '%s'",
1962 strbuf_addf(err, "unable to append to '%s': %s",
1963 logfile, strerror(errno));
1968 *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
1970 if (errno == ENOENT || errno == EISDIR) {
1972 * The logfile doesn't already exist,
1973 * but that is not an error; it only
1974 * means that we won't write log
1979 strbuf_addf(err, "unable to append to '%s': %s",
1980 logfile, strerror(errno));
1987 adjust_shared_perm(logfile);
1997 static int files_create_reflog(struct ref_store *ref_store,
1998 const char *refname, int force_create,
2001 struct files_ref_store *refs =
2002 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
2005 if (log_ref_setup(refs, refname, force_create, &fd, err))
2014 static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
2015 const unsigned char *new_sha1,
2016 const char *committer, const char *msg)
2018 int msglen, written;
2019 unsigned maxlen, len;
2022 msglen = msg ? strlen(msg) : 0;
2023 maxlen = strlen(committer) + msglen + 100;
2024 logrec = xmalloc(maxlen);
2025 len = xsnprintf(logrec, maxlen, "%s %s %s\n",
2026 sha1_to_hex(old_sha1),
2027 sha1_to_hex(new_sha1),
2030 len += copy_reflog_msg(logrec + len - 1, msg) - 1;
2032 written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
2040 static int files_log_ref_write(struct files_ref_store *refs,
2041 const char *refname, const unsigned char *old_sha1,
2042 const unsigned char *new_sha1, const char *msg,
2043 int flags, struct strbuf *err)
2047 if (log_all_ref_updates == LOG_REFS_UNSET)
2048 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
2050 result = log_ref_setup(refs, refname,
2051 flags & REF_FORCE_CREATE_REFLOG,
2059 result = log_ref_write_fd(logfd, old_sha1, new_sha1,
2060 git_committer_info(0), msg);
2062 struct strbuf sb = STRBUF_INIT;
2063 int save_errno = errno;
2065 files_reflog_path(refs, &sb, refname);
2066 strbuf_addf(err, "unable to append to '%s': %s",
2067 sb.buf, strerror(save_errno));
2068 strbuf_release(&sb);
2073 struct strbuf sb = STRBUF_INIT;
2074 int save_errno = errno;
2076 files_reflog_path(refs, &sb, refname);
2077 strbuf_addf(err, "unable to append to '%s': %s",
2078 sb.buf, strerror(save_errno));
2079 strbuf_release(&sb);
2086 * Write sha1 into the open lockfile, then close the lockfile. On
2087 * errors, rollback the lockfile, fill in *err and
2090 static int write_ref_to_lockfile(struct ref_lock *lock,
2091 const unsigned char *sha1, struct strbuf *err)
2093 static char term = '\n';
2097 o = parse_object(sha1);
2100 "trying to write ref '%s' with nonexistent object %s",
2101 lock->ref_name, sha1_to_hex(sha1));
2105 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2107 "trying to write non-commit object %s to branch '%s'",
2108 sha1_to_hex(sha1), lock->ref_name);
2112 fd = get_lock_file_fd(lock->lk);
2113 if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
2114 write_in_full(fd, &term, 1) != 1 ||
2115 close_ref(lock) < 0) {
2117 "couldn't write '%s'", get_lock_file_path(lock->lk));
2125 * Commit a change to a loose reference that has already been written
2126 * to the loose reference lockfile. Also update the reflogs if
2127 * necessary, using the specified lockmsg (which can be NULL).
2129 static int commit_ref_update(struct files_ref_store *refs,
2130 struct ref_lock *lock,
2131 const unsigned char *sha1, const char *logmsg,
2134 files_assert_main_repository(refs, "commit_ref_update");
2136 clear_loose_ref_cache(refs);
2137 if (files_log_ref_write(refs, lock->ref_name,
2138 lock->old_oid.hash, sha1,
2140 char *old_msg = strbuf_detach(err, NULL);
2141 strbuf_addf(err, "cannot update the ref '%s': %s",
2142 lock->ref_name, old_msg);
2148 if (strcmp(lock->ref_name, "HEAD") != 0) {
2150 * Special hack: If a branch is updated directly and HEAD
2151 * points to it (may happen on the remote side of a push
2152 * for example) then logically the HEAD reflog should be
2154 * A generic solution implies reverse symref information,
2155 * but finding all symrefs pointing to the given branch
2156 * would be rather costly for this rare event (the direct
2157 * update of a branch) to be worth it. So let's cheat and
2158 * check with HEAD only which should cover 99% of all usage
2159 * scenarios (even 100% of the default ones).
2161 unsigned char head_sha1[20];
2163 const char *head_ref;
2165 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
2166 RESOLVE_REF_READING,
2167 head_sha1, &head_flag);
2168 if (head_ref && (head_flag & REF_ISSYMREF) &&
2169 !strcmp(head_ref, lock->ref_name)) {
2170 struct strbuf log_err = STRBUF_INIT;
2171 if (files_log_ref_write(refs, "HEAD",
2172 lock->old_oid.hash, sha1,
2173 logmsg, 0, &log_err)) {
2174 error("%s", log_err.buf);
2175 strbuf_release(&log_err);
2180 if (commit_ref(lock)) {
2181 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
2190 static int create_ref_symlink(struct ref_lock *lock, const char *target)
2193 #ifndef NO_SYMLINK_HEAD
2194 char *ref_path = get_locked_file_path(lock->lk);
2196 ret = symlink(target, ref_path);
2200 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
2205 static void update_symref_reflog(struct files_ref_store *refs,
2206 struct ref_lock *lock, const char *refname,
2207 const char *target, const char *logmsg)
2209 struct strbuf err = STRBUF_INIT;
2210 unsigned char new_sha1[20];
2212 !refs_read_ref_full(&refs->base, target,
2213 RESOLVE_REF_READING, new_sha1, NULL) &&
2214 files_log_ref_write(refs, refname, lock->old_oid.hash,
2215 new_sha1, logmsg, 0, &err)) {
2216 error("%s", err.buf);
2217 strbuf_release(&err);
2221 static int create_symref_locked(struct files_ref_store *refs,
2222 struct ref_lock *lock, const char *refname,
2223 const char *target, const char *logmsg)
2225 if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
2226 update_symref_reflog(refs, lock, refname, target, logmsg);
2230 if (!fdopen_lock_file(lock->lk, "w"))
2231 return error("unable to fdopen %s: %s",
2232 lock->lk->tempfile.filename.buf, strerror(errno));
2234 update_symref_reflog(refs, lock, refname, target, logmsg);
2236 /* no error check; commit_ref will check ferror */
2237 fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
2238 if (commit_ref(lock) < 0)
2239 return error("unable to write symref for %s: %s", refname,
2244 static int files_create_symref(struct ref_store *ref_store,
2245 const char *refname, const char *target,
2248 struct files_ref_store *refs =
2249 files_downcast(ref_store, REF_STORE_WRITE, "create_symref");
2250 struct strbuf err = STRBUF_INIT;
2251 struct ref_lock *lock;
2254 lock = lock_ref_sha1_basic(refs, refname, NULL,
2255 NULL, NULL, REF_NODEREF, NULL,
2258 error("%s", err.buf);
2259 strbuf_release(&err);
2263 ret = create_symref_locked(refs, lock, refname, target, logmsg);
2268 int set_worktree_head_symref(const char *gitdir, const char *target, const char *logmsg)
2271 * FIXME: this obviously will not work well for future refs
2272 * backends. This function needs to die.
2274 struct files_ref_store *refs =
2275 files_downcast(get_main_ref_store(),
2279 static struct lock_file head_lock;
2280 struct ref_lock *lock;
2281 struct strbuf head_path = STRBUF_INIT;
2282 const char *head_rel;
2285 strbuf_addf(&head_path, "%s/HEAD", absolute_path(gitdir));
2286 if (hold_lock_file_for_update(&head_lock, head_path.buf,
2287 LOCK_NO_DEREF) < 0) {
2288 struct strbuf err = STRBUF_INIT;
2289 unable_to_lock_message(head_path.buf, errno, &err);
2290 error("%s", err.buf);
2291 strbuf_release(&err);
2292 strbuf_release(&head_path);
2296 /* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for
2298 head_rel = remove_leading_path(head_path.buf,
2299 absolute_path(get_git_common_dir()));
2300 /* to make use of create_symref_locked(), initialize ref_lock */
2301 lock = xcalloc(1, sizeof(struct ref_lock));
2302 lock->lk = &head_lock;
2303 lock->ref_name = xstrdup(head_rel);
2305 ret = create_symref_locked(refs, lock, head_rel, target, logmsg);
2307 unlock_ref(lock); /* will free lock */
2308 strbuf_release(&head_path);
2312 static int files_reflog_exists(struct ref_store *ref_store,
2313 const char *refname)
2315 struct files_ref_store *refs =
2316 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
2317 struct strbuf sb = STRBUF_INIT;
2321 files_reflog_path(refs, &sb, refname);
2322 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
2323 strbuf_release(&sb);
2327 static int files_delete_reflog(struct ref_store *ref_store,
2328 const char *refname)
2330 struct files_ref_store *refs =
2331 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
2332 struct strbuf sb = STRBUF_INIT;
2335 files_reflog_path(refs, &sb, refname);
2336 ret = remove_path(sb.buf);
2337 strbuf_release(&sb);
2341 static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
2343 struct object_id ooid, noid;
2344 char *email_end, *message;
2345 unsigned long timestamp;
2347 const char *p = sb->buf;
2349 /* old SP new SP name <email> SP time TAB msg LF */
2350 if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
2351 parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||
2352 parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||
2353 !(email_end = strchr(p, '>')) ||
2354 email_end[1] != ' ' ||
2355 !(timestamp = strtoul(email_end + 2, &message, 10)) ||
2356 !message || message[0] != ' ' ||
2357 (message[1] != '+' && message[1] != '-') ||
2358 !isdigit(message[2]) || !isdigit(message[3]) ||
2359 !isdigit(message[4]) || !isdigit(message[5]))
2360 return 0; /* corrupt? */
2361 email_end[1] = '\0';
2362 tz = strtol(message + 1, NULL, 10);
2363 if (message[6] != '\t')
2367 return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
2370 static char *find_beginning_of_line(char *bob, char *scan)
2372 while (bob < scan && *(--scan) != '\n')
2373 ; /* keep scanning backwards */
2375 * Return either beginning of the buffer, or LF at the end of
2376 * the previous line.
2381 static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
2382 const char *refname,
2383 each_reflog_ent_fn fn,
2386 struct files_ref_store *refs =
2387 files_downcast(ref_store, REF_STORE_READ,
2388 "for_each_reflog_ent_reverse");
2389 struct strbuf sb = STRBUF_INIT;
2392 int ret = 0, at_tail = 1;
2394 files_reflog_path(refs, &sb, refname);
2395 logfp = fopen(sb.buf, "r");
2396 strbuf_release(&sb);
2400 /* Jump to the end */
2401 if (fseek(logfp, 0, SEEK_END) < 0)
2402 return error("cannot seek back reflog for %s: %s",
2403 refname, strerror(errno));
2405 while (!ret && 0 < pos) {
2411 /* Fill next block from the end */
2412 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
2413 if (fseek(logfp, pos - cnt, SEEK_SET))
2414 return error("cannot seek back reflog for %s: %s",
2415 refname, strerror(errno));
2416 nread = fread(buf, cnt, 1, logfp);
2418 return error("cannot read %d bytes from reflog for %s: %s",
2419 cnt, refname, strerror(errno));
2422 scanp = endp = buf + cnt;
2423 if (at_tail && scanp[-1] == '\n')
2424 /* Looking at the final LF at the end of the file */
2428 while (buf < scanp) {
2430 * terminating LF of the previous line, or the beginning
2435 bp = find_beginning_of_line(buf, scanp);
2439 * The newline is the end of the previous line,
2440 * so we know we have complete line starting
2441 * at (bp + 1). Prefix it onto any prior data
2442 * we collected for the line and process it.
2444 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
2447 ret = show_one_reflog_ent(&sb, fn, cb_data);
2453 * We are at the start of the buffer, and the
2454 * start of the file; there is no previous
2455 * line, and we have everything for this one.
2456 * Process it, and we can end the loop.
2458 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2459 ret = show_one_reflog_ent(&sb, fn, cb_data);
2466 * We are at the start of the buffer, and there
2467 * is more file to read backwards. Which means
2468 * we are in the middle of a line. Note that we
2469 * may get here even if *bp was a newline; that
2470 * just means we are at the exact end of the
2471 * previous line, rather than some spot in the
2474 * Save away what we have to be combined with
2475 * the data from the next read.
2477 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2484 die("BUG: reverse reflog parser had leftover data");
2487 strbuf_release(&sb);
2491 static int files_for_each_reflog_ent(struct ref_store *ref_store,
2492 const char *refname,
2493 each_reflog_ent_fn fn, void *cb_data)
2495 struct files_ref_store *refs =
2496 files_downcast(ref_store, REF_STORE_READ,
2497 "for_each_reflog_ent");
2499 struct strbuf sb = STRBUF_INIT;
2502 files_reflog_path(refs, &sb, refname);
2503 logfp = fopen(sb.buf, "r");
2504 strbuf_release(&sb);
2508 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
2509 ret = show_one_reflog_ent(&sb, fn, cb_data);
2511 strbuf_release(&sb);
2515 struct files_reflog_iterator {
2516 struct ref_iterator base;
2518 struct ref_store *ref_store;
2519 struct dir_iterator *dir_iterator;
2520 struct object_id oid;
2523 static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
2525 struct files_reflog_iterator *iter =
2526 (struct files_reflog_iterator *)ref_iterator;
2527 struct dir_iterator *diter = iter->dir_iterator;
2530 while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2533 if (!S_ISREG(diter->st.st_mode))
2535 if (diter->basename[0] == '.')
2537 if (ends_with(diter->basename, ".lock"))
2540 if (refs_read_ref_full(iter->ref_store,
2541 diter->relative_path, 0,
2542 iter->oid.hash, &flags)) {
2543 error("bad ref for %s", diter->path.buf);
2547 iter->base.refname = diter->relative_path;
2548 iter->base.oid = &iter->oid;
2549 iter->base.flags = flags;
2553 iter->dir_iterator = NULL;
2554 if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
2559 static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2560 struct object_id *peeled)
2562 die("BUG: ref_iterator_peel() called for reflog_iterator");
2565 static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
2567 struct files_reflog_iterator *iter =
2568 (struct files_reflog_iterator *)ref_iterator;
2571 if (iter->dir_iterator)
2572 ok = dir_iterator_abort(iter->dir_iterator);
2574 base_ref_iterator_free(ref_iterator);
2578 static struct ref_iterator_vtable files_reflog_iterator_vtable = {
2579 files_reflog_iterator_advance,
2580 files_reflog_iterator_peel,
2581 files_reflog_iterator_abort
2584 static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
2586 struct files_ref_store *refs =
2587 files_downcast(ref_store, REF_STORE_READ,
2588 "reflog_iterator_begin");
2589 struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
2590 struct ref_iterator *ref_iterator = &iter->base;
2591 struct strbuf sb = STRBUF_INIT;
2593 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
2594 files_reflog_path(refs, &sb, NULL);
2595 iter->dir_iterator = dir_iterator_begin(sb.buf);
2596 iter->ref_store = ref_store;
2597 strbuf_release(&sb);
2598 return ref_iterator;
2601 static int ref_update_reject_duplicates(struct string_list *refnames,
2604 int i, n = refnames->nr;
2608 for (i = 1; i < n; i++)
2609 if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) {
2611 "multiple updates for ref '%s' not allowed.",
2612 refnames->items[i].string);
2619 * If update is a direct update of head_ref (the reference pointed to
2620 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2622 static int split_head_update(struct ref_update *update,
2623 struct ref_transaction *transaction,
2624 const char *head_ref,
2625 struct string_list *affected_refnames,
2628 struct string_list_item *item;
2629 struct ref_update *new_update;
2631 if ((update->flags & REF_LOG_ONLY) ||
2632 (update->flags & REF_ISPRUNING) ||
2633 (update->flags & REF_UPDATE_VIA_HEAD))
2636 if (strcmp(update->refname, head_ref))
2640 * First make sure that HEAD is not already in the
2641 * transaction. This insertion is O(N) in the transaction
2642 * size, but it happens at most once per transaction.
2644 item = string_list_insert(affected_refnames, "HEAD");
2646 /* An entry already existed */
2648 "multiple updates for 'HEAD' (including one "
2649 "via its referent '%s') are not allowed",
2651 return TRANSACTION_NAME_CONFLICT;
2654 new_update = ref_transaction_add_update(
2655 transaction, "HEAD",
2656 update->flags | REF_LOG_ONLY | REF_NODEREF,
2657 update->new_sha1, update->old_sha1,
2660 item->util = new_update;
2666 * update is for a symref that points at referent and doesn't have
2667 * REF_NODEREF set. Split it into two updates:
2668 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2669 * - A new, separate update for the referent reference
2670 * Note that the new update will itself be subject to splitting when
2671 * the iteration gets to it.
2673 static int split_symref_update(struct files_ref_store *refs,
2674 struct ref_update *update,
2675 const char *referent,
2676 struct ref_transaction *transaction,
2677 struct string_list *affected_refnames,
2680 struct string_list_item *item;
2681 struct ref_update *new_update;
2682 unsigned int new_flags;
2685 * First make sure that referent is not already in the
2686 * transaction. This insertion is O(N) in the transaction
2687 * size, but it happens at most once per symref in a
2690 item = string_list_insert(affected_refnames, referent);
2692 /* An entry already existed */
2694 "multiple updates for '%s' (including one "
2695 "via symref '%s') are not allowed",
2696 referent, update->refname);
2697 return TRANSACTION_NAME_CONFLICT;
2700 new_flags = update->flags;
2701 if (!strcmp(update->refname, "HEAD")) {
2703 * Record that the new update came via HEAD, so that
2704 * when we process it, split_head_update() doesn't try
2705 * to add another reflog update for HEAD. Note that
2706 * this bit will be propagated if the new_update
2707 * itself needs to be split.
2709 new_flags |= REF_UPDATE_VIA_HEAD;
2712 new_update = ref_transaction_add_update(
2713 transaction, referent, new_flags,
2714 update->new_sha1, update->old_sha1,
2717 new_update->parent_update = update;
2720 * Change the symbolic ref update to log only. Also, it
2721 * doesn't need to check its old SHA-1 value, as that will be
2722 * done when new_update is processed.
2724 update->flags |= REF_LOG_ONLY | REF_NODEREF;
2725 update->flags &= ~REF_HAVE_OLD;
2727 item->util = new_update;
2733 * Return the refname under which update was originally requested.
2735 static const char *original_update_refname(struct ref_update *update)
2737 while (update->parent_update)
2738 update = update->parent_update;
2740 return update->refname;
2744 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2745 * are consistent with oid, which is the reference's current value. If
2746 * everything is OK, return 0; otherwise, write an error message to
2747 * err and return -1.
2749 static int check_old_oid(struct ref_update *update, struct object_id *oid,
2752 if (!(update->flags & REF_HAVE_OLD) ||
2753 !hashcmp(oid->hash, update->old_sha1))
2756 if (is_null_sha1(update->old_sha1))
2757 strbuf_addf(err, "cannot lock ref '%s': "
2758 "reference already exists",
2759 original_update_refname(update));
2760 else if (is_null_oid(oid))
2761 strbuf_addf(err, "cannot lock ref '%s': "
2762 "reference is missing but expected %s",
2763 original_update_refname(update),
2764 sha1_to_hex(update->old_sha1));
2766 strbuf_addf(err, "cannot lock ref '%s': "
2767 "is at %s but expected %s",
2768 original_update_refname(update),
2770 sha1_to_hex(update->old_sha1));
2776 * Prepare for carrying out update:
2777 * - Lock the reference referred to by update.
2778 * - Read the reference under lock.
2779 * - Check that its old SHA-1 value (if specified) is correct, and in
2780 * any case record it in update->lock->old_oid for later use when
2781 * writing the reflog.
2782 * - If it is a symref update without REF_NODEREF, split it up into a
2783 * REF_LOG_ONLY update of the symref and add a separate update for
2784 * the referent to transaction.
2785 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2788 static int lock_ref_for_update(struct files_ref_store *refs,
2789 struct ref_update *update,
2790 struct ref_transaction *transaction,
2791 const char *head_ref,
2792 struct string_list *affected_refnames,
2795 struct strbuf referent = STRBUF_INIT;
2796 int mustexist = (update->flags & REF_HAVE_OLD) &&
2797 !is_null_sha1(update->old_sha1);
2799 struct ref_lock *lock;
2801 files_assert_main_repository(refs, "lock_ref_for_update");
2803 if ((update->flags & REF_HAVE_NEW) && is_null_sha1(update->new_sha1))
2804 update->flags |= REF_DELETING;
2807 ret = split_head_update(update, transaction, head_ref,
2808 affected_refnames, err);
2813 ret = lock_raw_ref(refs, update->refname, mustexist,
2814 affected_refnames, NULL,
2816 &update->type, err);
2820 reason = strbuf_detach(err, NULL);
2821 strbuf_addf(err, "cannot lock ref '%s': %s",
2822 original_update_refname(update), reason);
2827 update->backend_data = lock;
2829 if (update->type & REF_ISSYMREF) {
2830 if (update->flags & REF_NODEREF) {
2832 * We won't be reading the referent as part of
2833 * the transaction, so we have to read it here
2834 * to record and possibly check old_sha1:
2836 if (refs_read_ref_full(&refs->base,
2838 lock->old_oid.hash, NULL)) {
2839 if (update->flags & REF_HAVE_OLD) {
2840 strbuf_addf(err, "cannot lock ref '%s': "
2841 "error reading reference",
2842 original_update_refname(update));
2845 } else if (check_old_oid(update, &lock->old_oid, err)) {
2846 return TRANSACTION_GENERIC_ERROR;
2850 * Create a new update for the reference this
2851 * symref is pointing at. Also, we will record
2852 * and verify old_sha1 for this update as part
2853 * of processing the split-off update, so we
2854 * don't have to do it here.
2856 ret = split_symref_update(refs, update,
2857 referent.buf, transaction,
2858 affected_refnames, err);
2863 struct ref_update *parent_update;
2865 if (check_old_oid(update, &lock->old_oid, err))
2866 return TRANSACTION_GENERIC_ERROR;
2869 * If this update is happening indirectly because of a
2870 * symref update, record the old SHA-1 in the parent
2873 for (parent_update = update->parent_update;
2875 parent_update = parent_update->parent_update) {
2876 struct ref_lock *parent_lock = parent_update->backend_data;
2877 oidcpy(&parent_lock->old_oid, &lock->old_oid);
2881 if ((update->flags & REF_HAVE_NEW) &&
2882 !(update->flags & REF_DELETING) &&
2883 !(update->flags & REF_LOG_ONLY)) {
2884 if (!(update->type & REF_ISSYMREF) &&
2885 !hashcmp(lock->old_oid.hash, update->new_sha1)) {
2887 * The reference already has the desired
2888 * value, so we don't need to write it.
2890 } else if (write_ref_to_lockfile(lock, update->new_sha1,
2892 char *write_err = strbuf_detach(err, NULL);
2895 * The lock was freed upon failure of
2896 * write_ref_to_lockfile():
2898 update->backend_data = NULL;
2900 "cannot update ref '%s': %s",
2901 update->refname, write_err);
2903 return TRANSACTION_GENERIC_ERROR;
2905 update->flags |= REF_NEEDS_COMMIT;
2908 if (!(update->flags & REF_NEEDS_COMMIT)) {
2910 * We didn't call write_ref_to_lockfile(), so
2911 * the lockfile is still open. Close it to
2912 * free up the file descriptor:
2914 if (close_ref(lock)) {
2915 strbuf_addf(err, "couldn't close '%s.lock'",
2917 return TRANSACTION_GENERIC_ERROR;
2923 static int files_transaction_commit(struct ref_store *ref_store,
2924 struct ref_transaction *transaction,
2927 struct files_ref_store *refs =
2928 files_downcast(ref_store, REF_STORE_WRITE,
2929 "ref_transaction_commit");
2931 struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
2932 struct string_list_item *ref_to_delete;
2933 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
2934 char *head_ref = NULL;
2936 struct object_id head_oid;
2937 struct strbuf sb = STRBUF_INIT;
2941 if (transaction->state != REF_TRANSACTION_OPEN)
2942 die("BUG: commit called for transaction that is not open");
2944 if (!transaction->nr) {
2945 transaction->state = REF_TRANSACTION_CLOSED;
2950 * Fail if a refname appears more than once in the
2951 * transaction. (If we end up splitting up any updates using
2952 * split_symref_update() or split_head_update(), those
2953 * functions will check that the new updates don't have the
2954 * same refname as any existing ones.)
2956 for (i = 0; i < transaction->nr; i++) {
2957 struct ref_update *update = transaction->updates[i];
2958 struct string_list_item *item =
2959 string_list_append(&affected_refnames, update->refname);
2962 * We store a pointer to update in item->util, but at
2963 * the moment we never use the value of this field
2964 * except to check whether it is non-NULL.
2966 item->util = update;
2968 string_list_sort(&affected_refnames);
2969 if (ref_update_reject_duplicates(&affected_refnames, err)) {
2970 ret = TRANSACTION_GENERIC_ERROR;
2975 * Special hack: If a branch is updated directly and HEAD
2976 * points to it (may happen on the remote side of a push
2977 * for example) then logically the HEAD reflog should be
2980 * A generic solution would require reverse symref lookups,
2981 * but finding all symrefs pointing to a given branch would be
2982 * rather costly for this rare event (the direct update of a
2983 * branch) to be worth it. So let's cheat and check with HEAD
2984 * only, which should cover 99% of all usage scenarios (even
2985 * 100% of the default ones).
2987 * So if HEAD is a symbolic reference, then record the name of
2988 * the reference that it points to. If we see an update of
2989 * head_ref within the transaction, then split_head_update()
2990 * arranges for the reflog of HEAD to be updated, too.
2992 head_ref = refs_resolve_refdup(ref_store, "HEAD",
2993 RESOLVE_REF_NO_RECURSE,
2994 head_oid.hash, &head_type);
2996 if (head_ref && !(head_type & REF_ISSYMREF)) {
3002 * Acquire all locks, verify old values if provided, check
3003 * that new values are valid, and write new values to the
3004 * lockfiles, ready to be activated. Only keep one lockfile
3005 * open at a time to avoid running out of file descriptors.
3007 for (i = 0; i < transaction->nr; i++) {
3008 struct ref_update *update = transaction->updates[i];
3010 ret = lock_ref_for_update(refs, update, transaction,
3011 head_ref, &affected_refnames, err);
3016 /* Perform updates first so live commits remain referenced */
3017 for (i = 0; i < transaction->nr; i++) {
3018 struct ref_update *update = transaction->updates[i];
3019 struct ref_lock *lock = update->backend_data;
3021 if (update->flags & REF_NEEDS_COMMIT ||
3022 update->flags & REF_LOG_ONLY) {
3023 if (files_log_ref_write(refs,
3027 update->msg, update->flags,
3029 char *old_msg = strbuf_detach(err, NULL);
3031 strbuf_addf(err, "cannot update the ref '%s': %s",
3032 lock->ref_name, old_msg);
3035 update->backend_data = NULL;
3036 ret = TRANSACTION_GENERIC_ERROR;
3040 if (update->flags & REF_NEEDS_COMMIT) {
3041 clear_loose_ref_cache(refs);
3042 if (commit_ref(lock)) {
3043 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
3045 update->backend_data = NULL;
3046 ret = TRANSACTION_GENERIC_ERROR;
3051 /* Perform deletes now that updates are safely completed */
3052 for (i = 0; i < transaction->nr; i++) {
3053 struct ref_update *update = transaction->updates[i];
3054 struct ref_lock *lock = update->backend_data;
3056 if (update->flags & REF_DELETING &&
3057 !(update->flags & REF_LOG_ONLY)) {
3058 if (!(update->type & REF_ISPACKED) ||
3059 update->type & REF_ISSYMREF) {
3060 /* It is a loose reference. */
3062 files_ref_path(refs, &sb, lock->ref_name);
3063 if (unlink_or_msg(sb.buf, err)) {
3064 ret = TRANSACTION_GENERIC_ERROR;
3067 update->flags |= REF_DELETED_LOOSE;
3070 if (!(update->flags & REF_ISPRUNING))
3071 string_list_append(&refs_to_delete,
3076 if (repack_without_refs(refs, &refs_to_delete, err)) {
3077 ret = TRANSACTION_GENERIC_ERROR;
3081 /* Delete the reflogs of any references that were deleted: */
3082 for_each_string_list_item(ref_to_delete, &refs_to_delete) {
3084 files_reflog_path(refs, &sb, ref_to_delete->string);
3085 if (!unlink_or_warn(sb.buf))
3086 try_remove_empty_parents(refs, ref_to_delete->string,
3087 REMOVE_EMPTY_PARENTS_REFLOG);
3090 clear_loose_ref_cache(refs);
3093 strbuf_release(&sb);
3094 transaction->state = REF_TRANSACTION_CLOSED;
3096 for (i = 0; i < transaction->nr; i++) {
3097 struct ref_update *update = transaction->updates[i];
3098 struct ref_lock *lock = update->backend_data;
3103 if (update->flags & REF_DELETED_LOOSE) {
3105 * The loose reference was deleted. Delete any
3106 * empty parent directories. (Note that this
3107 * can only work because we have already
3108 * removed the lockfile.)
3110 try_remove_empty_parents(refs, update->refname,
3111 REMOVE_EMPTY_PARENTS_REF);
3115 string_list_clear(&refs_to_delete, 0);
3117 string_list_clear(&affected_refnames, 0);
3122 static int ref_present(const char *refname,
3123 const struct object_id *oid, int flags, void *cb_data)
3125 struct string_list *affected_refnames = cb_data;
3127 return string_list_has_string(affected_refnames, refname);
3130 static int files_initial_transaction_commit(struct ref_store *ref_store,
3131 struct ref_transaction *transaction,
3134 struct files_ref_store *refs =
3135 files_downcast(ref_store, REF_STORE_WRITE,
3136 "initial_ref_transaction_commit");
3138 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
3142 if (transaction->state != REF_TRANSACTION_OPEN)
3143 die("BUG: commit called for transaction that is not open");
3145 /* Fail if a refname appears more than once in the transaction: */
3146 for (i = 0; i < transaction->nr; i++)
3147 string_list_append(&affected_refnames,
3148 transaction->updates[i]->refname);
3149 string_list_sort(&affected_refnames);
3150 if (ref_update_reject_duplicates(&affected_refnames, err)) {
3151 ret = TRANSACTION_GENERIC_ERROR;
3156 * It's really undefined to call this function in an active
3157 * repository or when there are existing references: we are
3158 * only locking and changing packed-refs, so (1) any
3159 * simultaneous processes might try to change a reference at
3160 * the same time we do, and (2) any existing loose versions of
3161 * the references that we are setting would have precedence
3162 * over our values. But some remote helpers create the remote
3163 * "HEAD" and "master" branches before calling this function,
3164 * so here we really only check that none of the references
3165 * that we are creating already exists.
3167 if (refs_for_each_rawref(&refs->base, ref_present,
3168 &affected_refnames))
3169 die("BUG: initial ref transaction called with existing refs");
3171 for (i = 0; i < transaction->nr; i++) {
3172 struct ref_update *update = transaction->updates[i];
3174 if ((update->flags & REF_HAVE_OLD) &&
3175 !is_null_sha1(update->old_sha1))
3176 die("BUG: initial ref transaction with old_sha1 set");
3177 if (refs_verify_refname_available(&refs->base, update->refname,
3178 &affected_refnames, NULL,
3180 ret = TRANSACTION_NAME_CONFLICT;
3185 if (lock_packed_refs(refs, 0)) {
3186 strbuf_addf(err, "unable to lock packed-refs file: %s",
3188 ret = TRANSACTION_GENERIC_ERROR;
3192 for (i = 0; i < transaction->nr; i++) {
3193 struct ref_update *update = transaction->updates[i];
3195 if ((update->flags & REF_HAVE_NEW) &&
3196 !is_null_sha1(update->new_sha1))
3197 add_packed_ref(refs, update->refname, update->new_sha1);
3200 if (commit_packed_refs(refs)) {
3201 strbuf_addf(err, "unable to commit packed-refs file: %s",
3203 ret = TRANSACTION_GENERIC_ERROR;
3208 transaction->state = REF_TRANSACTION_CLOSED;
3209 string_list_clear(&affected_refnames, 0);
3213 struct expire_reflog_cb {
3215 reflog_expiry_should_prune_fn *should_prune_fn;
3218 struct object_id last_kept_oid;
3221 static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
3222 const char *email, unsigned long timestamp, int tz,
3223 const char *message, void *cb_data)
3225 struct expire_reflog_cb *cb = cb_data;
3226 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
3228 if (cb->flags & EXPIRE_REFLOGS_REWRITE)
3229 ooid = &cb->last_kept_oid;
3231 if ((*cb->should_prune_fn)(ooid->hash, noid->hash, email, timestamp, tz,
3232 message, policy_cb)) {
3234 printf("would prune %s", message);
3235 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3236 printf("prune %s", message);
3239 fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s",
3240 oid_to_hex(ooid), oid_to_hex(noid),
3241 email, timestamp, tz, message);
3242 oidcpy(&cb->last_kept_oid, noid);
3244 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3245 printf("keep %s", message);
3250 static int files_reflog_expire(struct ref_store *ref_store,
3251 const char *refname, const unsigned char *sha1,
3253 reflog_expiry_prepare_fn prepare_fn,
3254 reflog_expiry_should_prune_fn should_prune_fn,
3255 reflog_expiry_cleanup_fn cleanup_fn,
3256 void *policy_cb_data)
3258 struct files_ref_store *refs =
3259 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
3260 static struct lock_file reflog_lock;
3261 struct expire_reflog_cb cb;
3262 struct ref_lock *lock;
3263 struct strbuf log_file_sb = STRBUF_INIT;
3267 struct strbuf err = STRBUF_INIT;
3269 memset(&cb, 0, sizeof(cb));
3271 cb.policy_cb = policy_cb_data;
3272 cb.should_prune_fn = should_prune_fn;
3275 * The reflog file is locked by holding the lock on the
3276 * reference itself, plus we might need to update the
3277 * reference if --updateref was specified:
3279 lock = lock_ref_sha1_basic(refs, refname, sha1,
3280 NULL, NULL, REF_NODEREF,
3283 error("cannot lock ref '%s': %s", refname, err.buf);
3284 strbuf_release(&err);
3287 if (!refs_reflog_exists(ref_store, refname)) {
3292 files_reflog_path(refs, &log_file_sb, refname);
3293 log_file = strbuf_detach(&log_file_sb, NULL);
3294 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3296 * Even though holding $GIT_DIR/logs/$reflog.lock has
3297 * no locking implications, we use the lock_file
3298 * machinery here anyway because it does a lot of the
3299 * work we need, including cleaning up if the program
3300 * exits unexpectedly.
3302 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3303 struct strbuf err = STRBUF_INIT;
3304 unable_to_lock_message(log_file, errno, &err);
3305 error("%s", err.buf);
3306 strbuf_release(&err);
3309 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3311 error("cannot fdopen %s (%s)",
3312 get_lock_file_path(&reflog_lock), strerror(errno));
3317 (*prepare_fn)(refname, sha1, cb.policy_cb);
3318 refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
3319 (*cleanup_fn)(cb.policy_cb);
3321 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3323 * It doesn't make sense to adjust a reference pointed
3324 * to by a symbolic ref based on expiring entries in
3325 * the symbolic reference's reflog. Nor can we update
3326 * a reference if there are no remaining reflog
3329 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3330 !(type & REF_ISSYMREF) &&
3331 !is_null_oid(&cb.last_kept_oid);
3333 if (close_lock_file(&reflog_lock)) {
3334 status |= error("couldn't write %s: %s", log_file,
3336 } else if (update &&
3337 (write_in_full(get_lock_file_fd(lock->lk),
3338 oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
3339 write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
3340 close_ref(lock) < 0)) {
3341 status |= error("couldn't write %s",
3342 get_lock_file_path(lock->lk));
3343 rollback_lock_file(&reflog_lock);
3344 } else if (commit_lock_file(&reflog_lock)) {
3345 status |= error("unable to write reflog '%s' (%s)",
3346 log_file, strerror(errno));
3347 } else if (update && commit_ref(lock)) {
3348 status |= error("couldn't set %s", lock->ref_name);
3356 rollback_lock_file(&reflog_lock);
3362 static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
3364 struct files_ref_store *refs =
3365 files_downcast(ref_store, REF_STORE_WRITE, "init_db");
3366 struct strbuf sb = STRBUF_INIT;
3369 * Create .git/refs/{heads,tags}
3371 files_ref_path(refs, &sb, "refs/heads");
3372 safe_create_dir(sb.buf, 1);
3375 files_ref_path(refs, &sb, "refs/tags");
3376 safe_create_dir(sb.buf, 1);
3378 strbuf_release(&sb);
3382 struct ref_storage_be refs_be_files = {
3385 files_ref_store_create,
3387 files_transaction_commit,
3388 files_initial_transaction_commit,
3392 files_create_symref,
3396 files_ref_iterator_begin,
3399 files_reflog_iterator_begin,
3400 files_for_each_reflog_ent,
3401 files_for_each_reflog_ent_reverse,
3402 files_reflog_exists,
3403 files_create_reflog,
3404 files_delete_reflog,