The eighteenth batch
[alt-git.git] / refs.c
blobceb72d4bd740b9496f284c2d654b69eb9eafe33d
1 /*
2 * The backend-independent part of the reference module.
3 */
5 #define USE_THE_REPOSITORY_VARIABLE
7 #include "git-compat-util.h"
8 #include "advice.h"
9 #include "config.h"
10 #include "environment.h"
11 #include "strmap.h"
12 #include "gettext.h"
13 #include "hex.h"
14 #include "lockfile.h"
15 #include "iterator.h"
16 #include "refs.h"
17 #include "refs/refs-internal.h"
18 #include "run-command.h"
19 #include "hook.h"
20 #include "object-name.h"
21 #include "object-store-ll.h"
22 #include "object.h"
23 #include "path.h"
24 #include "submodule.h"
25 #include "worktree.h"
26 #include "strvec.h"
27 #include "repository.h"
28 #include "setup.h"
29 #include "sigchain.h"
30 #include "date.h"
31 #include "commit.h"
32 #include "wildmatch.h"
35 * List of all available backends
37 static const struct ref_storage_be *refs_backends[] = {
38 [REF_STORAGE_FORMAT_FILES] = &refs_be_files,
39 [REF_STORAGE_FORMAT_REFTABLE] = &refs_be_reftable,
42 static const struct ref_storage_be *find_ref_storage_backend(
43 enum ref_storage_format ref_storage_format)
45 if (ref_storage_format < ARRAY_SIZE(refs_backends))
46 return refs_backends[ref_storage_format];
47 return NULL;
50 enum ref_storage_format ref_storage_format_by_name(const char *name)
52 for (unsigned int i = 0; i < ARRAY_SIZE(refs_backends); i++)
53 if (refs_backends[i] && !strcmp(refs_backends[i]->name, name))
54 return i;
55 return REF_STORAGE_FORMAT_UNKNOWN;
58 const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format)
60 const struct ref_storage_be *be = find_ref_storage_backend(ref_storage_format);
61 if (!be)
62 return "unknown";
63 return be->name;
67 * How to handle various characters in refnames:
68 * 0: An acceptable character for refs
69 * 1: End-of-component
70 * 2: ., look for a preceding . to reject .. in refs
71 * 3: {, look for a preceding @ to reject @{ in refs
72 * 4: A bad character: ASCII control characters, and
73 * ":", "?", "[", "\", "^", "~", SP, or TAB
74 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
76 static unsigned char refname_disposition[256] = {
77 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
78 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
79 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
87 struct ref_namespace_info ref_namespace[] = {
88 [NAMESPACE_HEAD] = {
89 .ref = "HEAD",
90 .decoration = DECORATION_REF_HEAD,
91 .exact = 1,
93 [NAMESPACE_BRANCHES] = {
94 .ref = "refs/heads/",
95 .decoration = DECORATION_REF_LOCAL,
97 [NAMESPACE_TAGS] = {
98 .ref = "refs/tags/",
99 .decoration = DECORATION_REF_TAG,
101 [NAMESPACE_REMOTE_REFS] = {
103 * The default refspec for new remotes copies refs from
104 * refs/heads/ on the remote into refs/remotes/<remote>/.
105 * As such, "refs/remotes/" has special handling.
107 .ref = "refs/remotes/",
108 .decoration = DECORATION_REF_REMOTE,
110 [NAMESPACE_STASH] = {
112 * The single ref "refs/stash" stores the latest stash.
113 * Older stashes can be found in the reflog.
115 .ref = "refs/stash",
116 .exact = 1,
117 .decoration = DECORATION_REF_STASH,
119 [NAMESPACE_REPLACE] = {
121 * This namespace allows Git to act as if one object ID
122 * points to the content of another. Unlike the other
123 * ref namespaces, this one can be changed by the
124 * GIT_REPLACE_REF_BASE environment variable. This
125 * .namespace value will be overwritten in setup_git_env().
127 .ref = "refs/replace/",
128 .decoration = DECORATION_GRAFTED,
130 [NAMESPACE_NOTES] = {
132 * The refs/notes/commit ref points to the tip of a
133 * parallel commit history that adds metadata to commits
134 * in the normal history. This ref can be overwritten
135 * by the core.notesRef config variable or the
136 * GIT_NOTES_REFS environment variable.
138 .ref = "refs/notes/commit",
139 .exact = 1,
141 [NAMESPACE_PREFETCH] = {
143 * Prefetch refs are written by the background 'fetch'
144 * maintenance task. It allows faster foreground fetches
145 * by advertising these previously-downloaded tips without
146 * updating refs/remotes/ without user intervention.
148 .ref = "refs/prefetch/",
150 [NAMESPACE_REWRITTEN] = {
152 * Rewritten refs are used by the 'label' command in the
153 * sequencer. These are particularly useful during an
154 * interactive rebase that uses the 'merge' command.
156 .ref = "refs/rewritten/",
160 void update_ref_namespace(enum ref_namespace namespace, char *ref)
162 struct ref_namespace_info *info = &ref_namespace[namespace];
163 if (info->ref_updated)
164 free((char *)info->ref);
165 info->ref = ref;
166 info->ref_updated = 1;
170 * Try to read one refname component from the front of refname.
171 * Return the length of the component found, or -1 if the component is
172 * not legal. It is legal if it is something reasonable to have under
173 * ".git/refs/"; We do not like it if:
175 * - it begins with ".", or
176 * - it has double dots "..", or
177 * - it has ASCII control characters, or
178 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
179 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
180 * - it ends with a "/", or
181 * - it ends with ".lock", or
182 * - it contains a "@{" portion
184 * When sanitized is not NULL, instead of rejecting the input refname
185 * as an error, try to come up with a usable replacement for the input
186 * refname in it.
188 static int check_refname_component(const char *refname, int *flags,
189 struct strbuf *sanitized)
191 const char *cp;
192 char last = '\0';
193 size_t component_start = 0; /* garbage - not a reasonable initial value */
195 if (sanitized)
196 component_start = sanitized->len;
198 for (cp = refname; ; cp++) {
199 int ch = *cp & 255;
200 unsigned char disp = refname_disposition[ch];
202 if (sanitized && disp != 1)
203 strbuf_addch(sanitized, ch);
205 switch (disp) {
206 case 1:
207 goto out;
208 case 2:
209 if (last == '.') { /* Refname contains "..". */
210 if (sanitized)
211 /* collapse ".." to single "." */
212 strbuf_setlen(sanitized, sanitized->len - 1);
213 else
214 return -1;
216 break;
217 case 3:
218 if (last == '@') { /* Refname contains "@{". */
219 if (sanitized)
220 sanitized->buf[sanitized->len-1] = '-';
221 else
222 return -1;
224 break;
225 case 4:
226 /* forbidden char */
227 if (sanitized)
228 sanitized->buf[sanitized->len-1] = '-';
229 else
230 return -1;
231 break;
232 case 5:
233 if (!(*flags & REFNAME_REFSPEC_PATTERN)) {
234 /* refspec can't be a pattern */
235 if (sanitized)
236 sanitized->buf[sanitized->len-1] = '-';
237 else
238 return -1;
242 * Unset the pattern flag so that we only accept
243 * a single asterisk for one side of refspec.
245 *flags &= ~ REFNAME_REFSPEC_PATTERN;
246 break;
248 last = ch;
250 out:
251 if (cp == refname)
252 return 0; /* Component has zero length. */
254 if (refname[0] == '.') { /* Component starts with '.'. */
255 if (sanitized)
256 sanitized->buf[component_start] = '-';
257 else
258 return -1;
260 if (cp - refname >= LOCK_SUFFIX_LEN &&
261 !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) {
262 if (!sanitized)
263 return -1;
264 /* Refname ends with ".lock". */
265 while (strbuf_strip_suffix(sanitized, LOCK_SUFFIX)) {
266 /* try again in case we have .lock.lock */
269 return cp - refname;
272 static int check_or_sanitize_refname(const char *refname, int flags,
273 struct strbuf *sanitized)
275 int component_len, component_count = 0;
277 if (!strcmp(refname, "@")) {
278 /* Refname is a single character '@'. */
279 if (sanitized)
280 strbuf_addch(sanitized, '-');
281 else
282 return -1;
285 while (1) {
286 if (sanitized && sanitized->len)
287 strbuf_complete(sanitized, '/');
289 /* We are at the start of a path component. */
290 component_len = check_refname_component(refname, &flags,
291 sanitized);
292 if (sanitized && component_len == 0)
293 ; /* OK, omit empty component */
294 else if (component_len <= 0)
295 return -1;
297 component_count++;
298 if (refname[component_len] == '\0')
299 break;
300 /* Skip to next component. */
301 refname += component_len + 1;
304 if (refname[component_len - 1] == '.') {
305 /* Refname ends with '.'. */
306 if (sanitized)
307 ; /* omit ending dot */
308 else
309 return -1;
311 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
312 return -1; /* Refname has only one component. */
313 return 0;
316 int check_refname_format(const char *refname, int flags)
318 return check_or_sanitize_refname(refname, flags, NULL);
321 int refs_fsck(struct ref_store *refs, struct fsck_options *o)
323 return refs->be->fsck(refs, o);
326 void sanitize_refname_component(const char *refname, struct strbuf *out)
328 if (check_or_sanitize_refname(refname, REFNAME_ALLOW_ONELEVEL, out))
329 BUG("sanitizing refname '%s' check returned error", refname);
332 int refname_is_safe(const char *refname)
334 const char *rest;
336 if (skip_prefix(refname, "refs/", &rest)) {
337 char *buf;
338 int result;
339 size_t restlen = strlen(rest);
341 /* rest must not be empty, or start or end with "/" */
342 if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
343 return 0;
346 * Does the refname try to escape refs/?
347 * For example: refs/foo/../bar is safe but refs/foo/../../bar
348 * is not.
350 buf = xmallocz(restlen);
351 result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
352 free(buf);
353 return result;
356 do {
357 if (!isupper(*refname) && *refname != '_')
358 return 0;
359 refname++;
360 } while (*refname);
361 return 1;
365 * Return true if refname, which has the specified oid and flags, can
366 * be resolved to an object in the database. If the referred-to object
367 * does not exist, emit a warning and return false.
369 int ref_resolves_to_object(const char *refname,
370 struct repository *repo,
371 const struct object_id *oid,
372 unsigned int flags)
374 if (flags & REF_ISBROKEN)
375 return 0;
376 if (!repo_has_object_file(repo, oid)) {
377 error(_("%s does not point to a valid object!"), refname);
378 return 0;
380 return 1;
383 char *refs_resolve_refdup(struct ref_store *refs,
384 const char *refname, int resolve_flags,
385 struct object_id *oid, int *flags)
387 const char *result;
389 result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
390 oid, flags);
391 return xstrdup_or_null(result);
394 /* The argument to for_each_filter_refs */
395 struct for_each_ref_filter {
396 const char *pattern;
397 const char *prefix;
398 each_ref_fn *fn;
399 void *cb_data;
402 int refs_read_ref_full(struct ref_store *refs, const char *refname,
403 int resolve_flags, struct object_id *oid, int *flags)
405 if (refs_resolve_ref_unsafe(refs, refname, resolve_flags,
406 oid, flags))
407 return 0;
408 return -1;
411 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid)
413 return refs_read_ref_full(refs, refname, RESOLVE_REF_READING, oid, NULL);
416 int refs_ref_exists(struct ref_store *refs, const char *refname)
418 return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING,
419 NULL, NULL);
422 static int for_each_filter_refs(const char *refname, const char *referent,
423 const struct object_id *oid,
424 int flags, void *data)
426 struct for_each_ref_filter *filter = data;
428 if (wildmatch(filter->pattern, refname, 0))
429 return 0;
430 if (filter->prefix)
431 skip_prefix(refname, filter->prefix, &refname);
432 return filter->fn(refname, referent, oid, flags, filter->cb_data);
435 struct warn_if_dangling_data {
436 struct ref_store *refs;
437 FILE *fp;
438 const char *refname;
439 const struct string_list *refnames;
440 const char *msg_fmt;
443 static int warn_if_dangling_symref(const char *refname, const char *referent UNUSED,
444 const struct object_id *oid UNUSED,
445 int flags, void *cb_data)
447 struct warn_if_dangling_data *d = cb_data;
448 const char *resolves_to;
450 if (!(flags & REF_ISSYMREF))
451 return 0;
453 resolves_to = refs_resolve_ref_unsafe(d->refs, refname, 0, NULL, NULL);
454 if (!resolves_to
455 || (d->refname
456 ? strcmp(resolves_to, d->refname)
457 : !string_list_has_string(d->refnames, resolves_to))) {
458 return 0;
461 fprintf(d->fp, d->msg_fmt, refname);
462 fputc('\n', d->fp);
463 return 0;
466 void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp,
467 const char *msg_fmt, const char *refname)
469 struct warn_if_dangling_data data = {
470 .refs = refs,
471 .fp = fp,
472 .refname = refname,
473 .msg_fmt = msg_fmt,
475 refs_for_each_rawref(refs, warn_if_dangling_symref, &data);
478 void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
479 const char *msg_fmt, const struct string_list *refnames)
481 struct warn_if_dangling_data data = {
482 .refs = refs,
483 .fp = fp,
484 .refnames = refnames,
485 .msg_fmt = msg_fmt,
487 refs_for_each_rawref(refs, warn_if_dangling_symref, &data);
490 int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
492 return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data);
495 int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
497 return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data);
500 int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
502 return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data);
505 int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data)
507 struct strbuf buf = STRBUF_INIT;
508 int ret = 0;
509 struct object_id oid;
510 int flag;
512 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
513 if (!refs_read_ref_full(refs, buf.buf, RESOLVE_REF_READING, &oid, &flag))
514 ret = fn(buf.buf, NULL, &oid, flag, cb_data);
515 strbuf_release(&buf);
517 return ret;
520 void normalize_glob_ref(struct string_list_item *item, const char *prefix,
521 const char *pattern)
523 struct strbuf normalized_pattern = STRBUF_INIT;
525 if (*pattern == '/')
526 BUG("pattern must not start with '/'");
528 if (prefix)
529 strbuf_addstr(&normalized_pattern, prefix);
530 else if (!starts_with(pattern, "refs/") &&
531 strcmp(pattern, "HEAD"))
532 strbuf_addstr(&normalized_pattern, "refs/");
534 * NEEDSWORK: Special case other symrefs such as REBASE_HEAD,
535 * MERGE_HEAD, etc.
538 strbuf_addstr(&normalized_pattern, pattern);
539 strbuf_strip_suffix(&normalized_pattern, "/");
541 item->string = strbuf_detach(&normalized_pattern, NULL);
542 item->util = has_glob_specials(pattern) ? NULL : item->string;
543 strbuf_release(&normalized_pattern);
546 int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn,
547 const char *pattern, const char *prefix, void *cb_data)
549 struct strbuf real_pattern = STRBUF_INIT;
550 struct for_each_ref_filter filter;
551 int ret;
553 if (!prefix && !starts_with(pattern, "refs/"))
554 strbuf_addstr(&real_pattern, "refs/");
555 else if (prefix)
556 strbuf_addstr(&real_pattern, prefix);
557 strbuf_addstr(&real_pattern, pattern);
559 if (!has_glob_specials(pattern)) {
560 /* Append implied '/' '*' if not present. */
561 strbuf_complete(&real_pattern, '/');
562 /* No need to check for '*', there is none. */
563 strbuf_addch(&real_pattern, '*');
566 filter.pattern = real_pattern.buf;
567 filter.prefix = prefix;
568 filter.fn = fn;
569 filter.cb_data = cb_data;
570 ret = refs_for_each_ref(refs, for_each_filter_refs, &filter);
572 strbuf_release(&real_pattern);
573 return ret;
576 int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
577 const char *pattern, void *cb_data)
579 return refs_for_each_glob_ref_in(refs, fn, pattern, NULL, cb_data);
582 const char *prettify_refname(const char *name)
584 if (skip_prefix(name, "refs/heads/", &name) ||
585 skip_prefix(name, "refs/tags/", &name) ||
586 skip_prefix(name, "refs/remotes/", &name))
587 ; /* nothing */
588 return name;
591 static const char *ref_rev_parse_rules[] = {
592 "%.*s",
593 "refs/%.*s",
594 "refs/tags/%.*s",
595 "refs/heads/%.*s",
596 "refs/remotes/%.*s",
597 "refs/remotes/%.*s/HEAD",
598 NULL
601 #define NUM_REV_PARSE_RULES (ARRAY_SIZE(ref_rev_parse_rules) - 1)
604 * Is it possible that the caller meant full_name with abbrev_name?
605 * If so return a non-zero value to signal "yes"; the magnitude of
606 * the returned value gives the precedence used for disambiguation.
608 * If abbrev_name cannot mean full_name, return 0.
610 int refname_match(const char *abbrev_name, const char *full_name)
612 const char **p;
613 const int abbrev_name_len = strlen(abbrev_name);
614 const int num_rules = NUM_REV_PARSE_RULES;
616 for (p = ref_rev_parse_rules; *p; p++)
617 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name)))
618 return &ref_rev_parse_rules[num_rules] - p;
620 return 0;
624 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
625 * the results to 'prefixes'
627 void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
629 const char **p;
630 int len = strlen(prefix);
632 for (p = ref_rev_parse_rules; *p; p++)
633 strvec_pushf(prefixes, *p, len, prefix);
636 static const char default_branch_name_advice[] = N_(
637 "Using '%s' as the name for the initial branch. This default branch name\n"
638 "is subject to change. To configure the initial branch name to use in all\n"
639 "of your new repositories, which will suppress this warning, call:\n"
640 "\n"
641 "\tgit config --global init.defaultBranch <name>\n"
642 "\n"
643 "Names commonly chosen instead of 'master' are 'main', 'trunk' and\n"
644 "'development'. The just-created branch can be renamed via this command:\n"
645 "\n"
646 "\tgit branch -m <name>\n"
649 char *repo_default_branch_name(struct repository *r, int quiet)
651 const char *config_key = "init.defaultbranch";
652 const char *config_display_key = "init.defaultBranch";
653 char *ret = NULL, *full_ref;
654 const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME");
656 if (env && *env)
657 ret = xstrdup(env);
658 else if (repo_config_get_string(r, config_key, &ret) < 0)
659 die(_("could not retrieve `%s`"), config_display_key);
661 if (!ret) {
662 ret = xstrdup("master");
663 if (!quiet)
664 advise(_(default_branch_name_advice), ret);
667 full_ref = xstrfmt("refs/heads/%s", ret);
668 if (check_refname_format(full_ref, 0))
669 die(_("invalid branch name: %s = %s"), config_display_key, ret);
670 free(full_ref);
672 return ret;
676 * *string and *len will only be substituted, and *string returned (for
677 * later free()ing) if the string passed in is a magic short-hand form
678 * to name a branch.
680 static char *substitute_branch_name(struct repository *r,
681 const char **string, int *len,
682 int nonfatal_dangling_mark)
684 struct strbuf buf = STRBUF_INIT;
685 struct interpret_branch_name_options options = {
686 .nonfatal_dangling_mark = nonfatal_dangling_mark
688 int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options);
690 if (ret == *len) {
691 size_t size;
692 *string = strbuf_detach(&buf, &size);
693 *len = size;
694 return (char *)*string;
697 return NULL;
700 int repo_dwim_ref(struct repository *r, const char *str, int len,
701 struct object_id *oid, char **ref, int nonfatal_dangling_mark)
703 char *last_branch = substitute_branch_name(r, &str, &len,
704 nonfatal_dangling_mark);
705 int refs_found = expand_ref(r, str, len, oid, ref);
706 free(last_branch);
707 return refs_found;
710 int expand_ref(struct repository *repo, const char *str, int len,
711 struct object_id *oid, char **ref)
713 const char **p, *r;
714 int refs_found = 0;
715 struct strbuf fullref = STRBUF_INIT;
717 *ref = NULL;
718 for (p = ref_rev_parse_rules; *p; p++) {
719 struct object_id oid_from_ref;
720 struct object_id *this_result;
721 int flag;
722 struct ref_store *refs = get_main_ref_store(repo);
724 this_result = refs_found ? &oid_from_ref : oid;
725 strbuf_reset(&fullref);
726 strbuf_addf(&fullref, *p, len, str);
727 r = refs_resolve_ref_unsafe(refs, fullref.buf,
728 RESOLVE_REF_READING,
729 this_result, &flag);
730 if (r) {
731 if (!refs_found++)
732 *ref = xstrdup(r);
733 if (!warn_ambiguous_refs)
734 break;
735 } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
736 warning(_("ignoring dangling symref %s"), fullref.buf);
737 } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) {
738 warning(_("ignoring broken ref %s"), fullref.buf);
741 strbuf_release(&fullref);
742 return refs_found;
745 int repo_dwim_log(struct repository *r, const char *str, int len,
746 struct object_id *oid, char **log)
748 struct ref_store *refs = get_main_ref_store(r);
749 char *last_branch = substitute_branch_name(r, &str, &len, 0);
750 const char **p;
751 int logs_found = 0;
752 struct strbuf path = STRBUF_INIT;
754 *log = NULL;
755 for (p = ref_rev_parse_rules; *p; p++) {
756 struct object_id hash;
757 const char *ref, *it;
759 strbuf_reset(&path);
760 strbuf_addf(&path, *p, len, str);
761 ref = refs_resolve_ref_unsafe(refs, path.buf,
762 RESOLVE_REF_READING,
763 oid ? &hash : NULL, NULL);
764 if (!ref)
765 continue;
766 if (refs_reflog_exists(refs, path.buf))
767 it = path.buf;
768 else if (strcmp(ref, path.buf) &&
769 refs_reflog_exists(refs, ref))
770 it = ref;
771 else
772 continue;
773 if (!logs_found++) {
774 *log = xstrdup(it);
775 if (oid)
776 oidcpy(oid, &hash);
778 if (!warn_ambiguous_refs)
779 break;
781 strbuf_release(&path);
782 free(last_branch);
783 return logs_found;
786 int is_per_worktree_ref(const char *refname)
788 return starts_with(refname, "refs/worktree/") ||
789 starts_with(refname, "refs/bisect/") ||
790 starts_with(refname, "refs/rewritten/");
793 int is_pseudo_ref(const char *refname)
795 static const char * const pseudo_refs[] = {
796 "FETCH_HEAD",
797 "MERGE_HEAD",
799 size_t i;
801 for (i = 0; i < ARRAY_SIZE(pseudo_refs); i++)
802 if (!strcmp(refname, pseudo_refs[i]))
803 return 1;
805 return 0;
808 static int is_root_ref_syntax(const char *refname)
810 const char *c;
812 for (c = refname; *c; c++) {
813 if (!isupper(*c) && *c != '-' && *c != '_')
814 return 0;
817 return 1;
820 int is_root_ref(const char *refname)
822 static const char *const irregular_root_refs[] = {
823 "HEAD",
824 "AUTO_MERGE",
825 "BISECT_EXPECTED_REV",
826 "NOTES_MERGE_PARTIAL",
827 "NOTES_MERGE_REF",
828 "MERGE_AUTOSTASH",
830 size_t i;
832 if (!is_root_ref_syntax(refname) ||
833 is_pseudo_ref(refname))
834 return 0;
836 if (ends_with(refname, "_HEAD"))
837 return 1;
839 for (i = 0; i < ARRAY_SIZE(irregular_root_refs); i++)
840 if (!strcmp(refname, irregular_root_refs[i]))
841 return 1;
843 return 0;
846 static int is_current_worktree_ref(const char *ref) {
847 return is_root_ref_syntax(ref) || is_per_worktree_ref(ref);
850 enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
851 const char **worktree_name, int *worktree_name_length,
852 const char **bare_refname)
854 const char *name_dummy;
855 int name_length_dummy;
856 const char *ref_dummy;
858 if (!worktree_name)
859 worktree_name = &name_dummy;
860 if (!worktree_name_length)
861 worktree_name_length = &name_length_dummy;
862 if (!bare_refname)
863 bare_refname = &ref_dummy;
865 if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) {
866 const char *slash = strchr(*bare_refname, '/');
868 *worktree_name = *bare_refname;
869 if (!slash) {
870 *worktree_name_length = strlen(*worktree_name);
872 /* This is an error condition, and the caller tell because the bare_refname is "" */
873 *bare_refname = *worktree_name + *worktree_name_length;
874 return REF_WORKTREE_OTHER;
877 *worktree_name_length = slash - *bare_refname;
878 *bare_refname = slash + 1;
880 if (is_current_worktree_ref(*bare_refname))
881 return REF_WORKTREE_OTHER;
884 *worktree_name = NULL;
885 *worktree_name_length = 0;
887 if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname)
888 && is_current_worktree_ref(*bare_refname))
889 return REF_WORKTREE_MAIN;
891 *bare_refname = maybe_worktree_ref;
892 if (is_current_worktree_ref(maybe_worktree_ref))
893 return REF_WORKTREE_CURRENT;
895 return REF_WORKTREE_SHARED;
898 long get_files_ref_lock_timeout_ms(void)
900 static int configured = 0;
902 /* The default timeout is 100 ms: */
903 static int timeout_ms = 100;
905 if (!configured) {
906 git_config_get_int("core.filesreflocktimeout", &timeout_ms);
907 configured = 1;
910 return timeout_ms;
913 int refs_delete_ref(struct ref_store *refs, const char *msg,
914 const char *refname,
915 const struct object_id *old_oid,
916 unsigned int flags)
918 struct ref_transaction *transaction;
919 struct strbuf err = STRBUF_INIT;
921 transaction = ref_store_transaction_begin(refs, &err);
922 if (!transaction ||
923 ref_transaction_delete(transaction, refname, old_oid,
924 NULL, flags, msg, &err) ||
925 ref_transaction_commit(transaction, &err)) {
926 error("%s", err.buf);
927 ref_transaction_free(transaction);
928 strbuf_release(&err);
929 return 1;
931 ref_transaction_free(transaction);
932 strbuf_release(&err);
933 return 0;
936 static void copy_reflog_msg(struct strbuf *sb, const char *msg)
938 char c;
939 int wasspace = 1;
941 while ((c = *msg++)) {
942 if (wasspace && isspace(c))
943 continue;
944 wasspace = isspace(c);
945 if (wasspace)
946 c = ' ';
947 strbuf_addch(sb, c);
949 strbuf_rtrim(sb);
952 static char *normalize_reflog_message(const char *msg)
954 struct strbuf sb = STRBUF_INIT;
956 if (msg && *msg)
957 copy_reflog_msg(&sb, msg);
958 return strbuf_detach(&sb, NULL);
961 int should_autocreate_reflog(const char *refname)
963 switch (log_all_ref_updates) {
964 case LOG_REFS_ALWAYS:
965 return 1;
966 case LOG_REFS_NORMAL:
967 return starts_with(refname, "refs/heads/") ||
968 starts_with(refname, "refs/remotes/") ||
969 starts_with(refname, "refs/notes/") ||
970 !strcmp(refname, "HEAD");
971 default:
972 return 0;
976 int is_branch(const char *refname)
978 return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
981 struct read_ref_at_cb {
982 const char *refname;
983 timestamp_t at_time;
984 int cnt;
985 int reccnt;
986 struct object_id *oid;
987 int found_it;
989 struct object_id ooid;
990 struct object_id noid;
991 int tz;
992 timestamp_t date;
993 char **msg;
994 timestamp_t *cutoff_time;
995 int *cutoff_tz;
996 int *cutoff_cnt;
999 static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
1000 timestamp_t timestamp, int tz, const char *message)
1002 if (cb->msg)
1003 *cb->msg = xstrdup(message);
1004 if (cb->cutoff_time)
1005 *cb->cutoff_time = timestamp;
1006 if (cb->cutoff_tz)
1007 *cb->cutoff_tz = tz;
1008 if (cb->cutoff_cnt)
1009 *cb->cutoff_cnt = cb->reccnt;
1012 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
1013 const char *email UNUSED,
1014 timestamp_t timestamp, int tz,
1015 const char *message, void *cb_data)
1017 struct read_ref_at_cb *cb = cb_data;
1019 cb->tz = tz;
1020 cb->date = timestamp;
1022 if (timestamp <= cb->at_time || cb->cnt == 0) {
1023 set_read_ref_cutoffs(cb, timestamp, tz, message);
1025 * we have not yet updated cb->[n|o]oid so they still
1026 * hold the values for the previous record.
1028 if (!is_null_oid(&cb->ooid)) {
1029 oidcpy(cb->oid, noid);
1030 if (!oideq(&cb->ooid, noid))
1031 warning(_("log for ref %s has gap after %s"),
1032 cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
1034 else if (cb->date == cb->at_time)
1035 oidcpy(cb->oid, noid);
1036 else if (!oideq(noid, cb->oid))
1037 warning(_("log for ref %s unexpectedly ended on %s"),
1038 cb->refname, show_date(cb->date, cb->tz,
1039 DATE_MODE(RFC2822)));
1040 cb->reccnt++;
1041 oidcpy(&cb->ooid, ooid);
1042 oidcpy(&cb->noid, noid);
1043 cb->found_it = 1;
1044 return 1;
1046 cb->reccnt++;
1047 oidcpy(&cb->ooid, ooid);
1048 oidcpy(&cb->noid, noid);
1049 if (cb->cnt > 0)
1050 cb->cnt--;
1051 return 0;
1054 static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
1055 const char *email UNUSED,
1056 timestamp_t timestamp, int tz,
1057 const char *message, void *cb_data)
1059 struct read_ref_at_cb *cb = cb_data;
1061 set_read_ref_cutoffs(cb, timestamp, tz, message);
1062 oidcpy(cb->oid, ooid);
1063 if (cb->at_time && is_null_oid(cb->oid))
1064 oidcpy(cb->oid, noid);
1065 /* We just want the first entry */
1066 return 1;
1069 int read_ref_at(struct ref_store *refs, const char *refname,
1070 unsigned int flags, timestamp_t at_time, int cnt,
1071 struct object_id *oid, char **msg,
1072 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
1074 struct read_ref_at_cb cb;
1076 memset(&cb, 0, sizeof(cb));
1077 cb.refname = refname;
1078 cb.at_time = at_time;
1079 cb.cnt = cnt;
1080 cb.msg = msg;
1081 cb.cutoff_time = cutoff_time;
1082 cb.cutoff_tz = cutoff_tz;
1083 cb.cutoff_cnt = cutoff_cnt;
1084 cb.oid = oid;
1086 refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb);
1088 if (!cb.reccnt) {
1089 if (cnt == 0) {
1091 * The caller asked for ref@{0}, and we had no entries.
1092 * It's a bit subtle, but in practice all callers have
1093 * prepped the "oid" field with the current value of
1094 * the ref, which is the most reasonable fallback.
1096 * We'll put dummy values into the out-parameters (so
1097 * they're not just uninitialized garbage), and the
1098 * caller can take our return value as a hint that
1099 * we did not find any such reflog.
1101 set_read_ref_cutoffs(&cb, 0, 0, "empty reflog");
1102 return 1;
1104 if (flags & GET_OID_QUIETLY)
1105 exit(128);
1106 else
1107 die(_("log for %s is empty"), refname);
1109 if (cb.found_it)
1110 return 0;
1112 refs_for_each_reflog_ent(refs, refname, read_ref_at_ent_oldest, &cb);
1114 return 1;
1117 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
1118 struct strbuf *err)
1120 struct ref_transaction *tr;
1121 assert(err);
1123 CALLOC_ARRAY(tr, 1);
1124 tr->ref_store = refs;
1125 return tr;
1128 void ref_transaction_free(struct ref_transaction *transaction)
1130 size_t i;
1132 if (!transaction)
1133 return;
1135 switch (transaction->state) {
1136 case REF_TRANSACTION_OPEN:
1137 case REF_TRANSACTION_CLOSED:
1138 /* OK */
1139 break;
1140 case REF_TRANSACTION_PREPARED:
1141 BUG("free called on a prepared reference transaction");
1142 break;
1143 default:
1144 BUG("unexpected reference transaction state");
1145 break;
1148 for (i = 0; i < transaction->nr; i++) {
1149 free(transaction->updates[i]->msg);
1150 free((char *)transaction->updates[i]->new_target);
1151 free((char *)transaction->updates[i]->old_target);
1152 free(transaction->updates[i]);
1154 free(transaction->updates);
1155 free(transaction);
1158 struct ref_update *ref_transaction_add_update(
1159 struct ref_transaction *transaction,
1160 const char *refname, unsigned int flags,
1161 const struct object_id *new_oid,
1162 const struct object_id *old_oid,
1163 const char *new_target, const char *old_target,
1164 const char *msg)
1166 struct ref_update *update;
1168 if (transaction->state != REF_TRANSACTION_OPEN)
1169 BUG("update called for transaction that is not open");
1171 if (old_oid && old_target)
1172 BUG("only one of old_oid and old_target should be non NULL");
1173 if (new_oid && new_target)
1174 BUG("only one of new_oid and new_target should be non NULL");
1176 FLEX_ALLOC_STR(update, refname, refname);
1177 ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
1178 transaction->updates[transaction->nr++] = update;
1180 update->flags = flags;
1182 update->new_target = xstrdup_or_null(new_target);
1183 update->old_target = xstrdup_or_null(old_target);
1184 if ((flags & REF_HAVE_NEW) && new_oid)
1185 oidcpy(&update->new_oid, new_oid);
1186 if ((flags & REF_HAVE_OLD) && old_oid)
1187 oidcpy(&update->old_oid, old_oid);
1189 update->msg = normalize_reflog_message(msg);
1190 return update;
1193 int ref_transaction_update(struct ref_transaction *transaction,
1194 const char *refname,
1195 const struct object_id *new_oid,
1196 const struct object_id *old_oid,
1197 const char *new_target,
1198 const char *old_target,
1199 unsigned int flags, const char *msg,
1200 struct strbuf *err)
1202 assert(err);
1204 if ((flags & REF_FORCE_CREATE_REFLOG) &&
1205 (flags & REF_SKIP_CREATE_REFLOG)) {
1206 strbuf_addstr(err, _("refusing to force and skip creation of reflog"));
1207 return -1;
1210 if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
1211 ((new_oid && !is_null_oid(new_oid)) ?
1212 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
1213 !refname_is_safe(refname))) {
1214 strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
1215 refname);
1216 return -1;
1219 if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
1220 is_pseudo_ref(refname)) {
1221 strbuf_addf(err, _("refusing to update pseudoref '%s'"),
1222 refname);
1223 return -1;
1226 if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
1227 BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
1230 * Clear flags outside the allowed set; this should be a noop because
1231 * of the BUG() check above, but it works around a -Wnonnull warning
1232 * with some versions of "gcc -O3".
1234 flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
1236 flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
1237 flags |= (new_target ? REF_HAVE_NEW : 0) | (old_target ? REF_HAVE_OLD : 0);
1239 ref_transaction_add_update(transaction, refname, flags,
1240 new_oid, old_oid, new_target,
1241 old_target, msg);
1242 return 0;
1245 int ref_transaction_create(struct ref_transaction *transaction,
1246 const char *refname,
1247 const struct object_id *new_oid,
1248 const char *new_target,
1249 unsigned int flags, const char *msg,
1250 struct strbuf *err)
1252 if (new_oid && new_target)
1253 BUG("create called with both new_oid and new_target set");
1254 if ((!new_oid || is_null_oid(new_oid)) && !new_target) {
1255 strbuf_addf(err, "'%s' has neither a valid OID nor a target", refname);
1256 return 1;
1258 return ref_transaction_update(transaction, refname, new_oid,
1259 null_oid(), new_target, NULL, flags,
1260 msg, err);
1263 int ref_transaction_delete(struct ref_transaction *transaction,
1264 const char *refname,
1265 const struct object_id *old_oid,
1266 const char *old_target,
1267 unsigned int flags,
1268 const char *msg,
1269 struct strbuf *err)
1271 if (old_oid && is_null_oid(old_oid))
1272 BUG("delete called with old_oid set to zeros");
1273 if (old_oid && old_target)
1274 BUG("delete called with both old_oid and old_target set");
1275 if (old_target && !(flags & REF_NO_DEREF))
1276 BUG("delete cannot operate on symrefs with deref mode");
1277 return ref_transaction_update(transaction, refname,
1278 null_oid(), old_oid,
1279 NULL, old_target, flags,
1280 msg, err);
1283 int ref_transaction_verify(struct ref_transaction *transaction,
1284 const char *refname,
1285 const struct object_id *old_oid,
1286 const char *old_target,
1287 unsigned int flags,
1288 struct strbuf *err)
1290 if (!old_target && !old_oid)
1291 BUG("verify called with old_oid and old_target set to NULL");
1292 if (old_oid && old_target)
1293 BUG("verify called with both old_oid and old_target set");
1294 if (old_target && !(flags & REF_NO_DEREF))
1295 BUG("verify cannot operate on symrefs with deref mode");
1296 return ref_transaction_update(transaction, refname,
1297 NULL, old_oid,
1298 NULL, old_target,
1299 flags, NULL, err);
1302 int refs_update_ref(struct ref_store *refs, const char *msg,
1303 const char *refname, const struct object_id *new_oid,
1304 const struct object_id *old_oid, unsigned int flags,
1305 enum action_on_err onerr)
1307 struct ref_transaction *t = NULL;
1308 struct strbuf err = STRBUF_INIT;
1309 int ret = 0;
1311 t = ref_store_transaction_begin(refs, &err);
1312 if (!t ||
1313 ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL,
1314 flags, msg, &err) ||
1315 ref_transaction_commit(t, &err)) {
1316 ret = 1;
1317 ref_transaction_free(t);
1319 if (ret) {
1320 const char *str = _("update_ref failed for ref '%s': %s");
1322 switch (onerr) {
1323 case UPDATE_REFS_MSG_ON_ERR:
1324 error(str, refname, err.buf);
1325 break;
1326 case UPDATE_REFS_DIE_ON_ERR:
1327 die(str, refname, err.buf);
1328 break;
1329 case UPDATE_REFS_QUIET_ON_ERR:
1330 break;
1332 strbuf_release(&err);
1333 return 1;
1335 strbuf_release(&err);
1336 if (t)
1337 ref_transaction_free(t);
1338 return 0;
1342 * Check that the string refname matches a rule of the form
1343 * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule
1344 * "foo/%.*s/baz", and return the string "bar".
1346 static const char *match_parse_rule(const char *refname, const char *rule,
1347 size_t *len)
1350 * Check that rule matches refname up to the first percent in the rule.
1351 * We can bail immediately if not, but otherwise we leave "rule" at the
1352 * %-placeholder, and "refname" at the start of the potential matched
1353 * name.
1355 while (*rule != '%') {
1356 if (!*rule)
1357 BUG("rev-parse rule did not have percent");
1358 if (*refname++ != *rule++)
1359 return NULL;
1363 * Check that our "%" is the expected placeholder. This assumes there
1364 * are no other percents (placeholder or quoted) in the string, but
1365 * that is sufficient for our rev-parse rules.
1367 if (!skip_prefix(rule, "%.*s", &rule))
1368 return NULL;
1371 * And now check that our suffix (if any) matches.
1373 if (!strip_suffix(refname, rule, len))
1374 return NULL;
1376 return refname; /* len set by strip_suffix() */
1379 char *refs_shorten_unambiguous_ref(struct ref_store *refs,
1380 const char *refname, int strict)
1382 int i;
1383 struct strbuf resolved_buf = STRBUF_INIT;
1385 /* skip first rule, it will always match */
1386 for (i = NUM_REV_PARSE_RULES - 1; i > 0 ; --i) {
1387 int j;
1388 int rules_to_fail = i;
1389 const char *short_name;
1390 size_t short_name_len;
1392 short_name = match_parse_rule(refname, ref_rev_parse_rules[i],
1393 &short_name_len);
1394 if (!short_name)
1395 continue;
1398 * in strict mode, all (except the matched one) rules
1399 * must fail to resolve to a valid non-ambiguous ref
1401 if (strict)
1402 rules_to_fail = NUM_REV_PARSE_RULES;
1405 * check if the short name resolves to a valid ref,
1406 * but use only rules prior to the matched one
1408 for (j = 0; j < rules_to_fail; j++) {
1409 const char *rule = ref_rev_parse_rules[j];
1411 /* skip matched rule */
1412 if (i == j)
1413 continue;
1416 * the short name is ambiguous, if it resolves
1417 * (with this previous rule) to a valid ref
1418 * read_ref() returns 0 on success
1420 strbuf_reset(&resolved_buf);
1421 strbuf_addf(&resolved_buf, rule,
1422 cast_size_t_to_int(short_name_len),
1423 short_name);
1424 if (refs_ref_exists(refs, resolved_buf.buf))
1425 break;
1429 * short name is non-ambiguous if all previous rules
1430 * haven't resolved to a valid ref
1432 if (j == rules_to_fail) {
1433 strbuf_release(&resolved_buf);
1434 return xmemdupz(short_name, short_name_len);
1438 strbuf_release(&resolved_buf);
1439 return xstrdup(refname);
1442 int parse_hide_refs_config(const char *var, const char *value, const char *section,
1443 struct strvec *hide_refs)
1445 const char *key;
1446 if (!strcmp("transfer.hiderefs", var) ||
1447 (!parse_config_key(var, section, NULL, NULL, &key) &&
1448 !strcmp(key, "hiderefs"))) {
1449 char *ref;
1450 int len;
1452 if (!value)
1453 return config_error_nonbool(var);
1455 /* drop const to remove trailing '/' characters */
1456 ref = (char *)strvec_push(hide_refs, value);
1457 len = strlen(ref);
1458 while (len && ref[len - 1] == '/')
1459 ref[--len] = '\0';
1461 return 0;
1464 int ref_is_hidden(const char *refname, const char *refname_full,
1465 const struct strvec *hide_refs)
1467 int i;
1469 for (i = hide_refs->nr - 1; i >= 0; i--) {
1470 const char *match = hide_refs->v[i];
1471 const char *subject;
1472 int neg = 0;
1473 const char *p;
1475 if (*match == '!') {
1476 neg = 1;
1477 match++;
1480 if (*match == '^') {
1481 subject = refname_full;
1482 match++;
1483 } else {
1484 subject = refname;
1487 /* refname can be NULL when namespaces are used. */
1488 if (subject &&
1489 skip_prefix(subject, match, &p) &&
1490 (!*p || *p == '/'))
1491 return !neg;
1493 return 0;
1496 const char **hidden_refs_to_excludes(const struct strvec *hide_refs)
1498 const char **pattern;
1499 for (pattern = hide_refs->v; *pattern; pattern++) {
1501 * We can't feed any excludes from hidden refs config
1502 * sections, since later rules may override previous
1503 * ones. For example, with rules "refs/foo" and
1504 * "!refs/foo/bar", we should show "refs/foo/bar" (and
1505 * everything underneath it), but the earlier exclusion
1506 * would cause us to skip all of "refs/foo". We
1507 * likewise don't implement the namespace stripping
1508 * required for '^' rules.
1510 * Both are possible to do, but complicated, so avoid
1511 * populating the jump list at all if we see either of
1512 * these patterns.
1514 if (**pattern == '!' || **pattern == '^')
1515 return NULL;
1517 return hide_refs->v;
1520 const char *find_descendant_ref(const char *dirname,
1521 const struct string_list *extras,
1522 const struct string_list *skip)
1524 int pos;
1526 if (!extras)
1527 return NULL;
1530 * Look at the place where dirname would be inserted into
1531 * extras. If there is an entry at that position that starts
1532 * with dirname (remember, dirname includes the trailing
1533 * slash) and is not in skip, then we have a conflict.
1535 for (pos = string_list_find_insert_index(extras, dirname, 0);
1536 pos < extras->nr; pos++) {
1537 const char *extra_refname = extras->items[pos].string;
1539 if (!starts_with(extra_refname, dirname))
1540 break;
1542 if (!skip || !string_list_has_string(skip, extra_refname))
1543 return extra_refname;
1545 return NULL;
1548 int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1550 struct object_id oid;
1551 int flag;
1553 if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING,
1554 &oid, &flag))
1555 return fn("HEAD", NULL, &oid, flag, cb_data);
1557 return 0;
1560 struct ref_iterator *refs_ref_iterator_begin(
1561 struct ref_store *refs,
1562 const char *prefix,
1563 const char **exclude_patterns,
1564 int trim,
1565 enum do_for_each_ref_flags flags)
1567 struct ref_iterator *iter;
1569 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
1570 static int ref_paranoia = -1;
1572 if (ref_paranoia < 0)
1573 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 1);
1574 if (ref_paranoia) {
1575 flags |= DO_FOR_EACH_INCLUDE_BROKEN;
1576 flags |= DO_FOR_EACH_OMIT_DANGLING_SYMREFS;
1580 iter = refs->be->iterator_begin(refs, prefix, exclude_patterns, flags);
1582 * `iterator_begin()` already takes care of prefix, but we
1583 * might need to do some trimming:
1585 if (trim)
1586 iter = prefix_ref_iterator_begin(iter, "", trim);
1588 return iter;
1591 static int do_for_each_ref(struct ref_store *refs, const char *prefix,
1592 const char **exclude_patterns,
1593 each_ref_fn fn, int trim,
1594 enum do_for_each_ref_flags flags, void *cb_data)
1596 struct ref_iterator *iter;
1598 if (!refs)
1599 return 0;
1601 iter = refs_ref_iterator_begin(refs, prefix, exclude_patterns, trim,
1602 flags);
1604 return do_for_each_ref_iterator(iter, fn, cb_data);
1607 int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1609 return do_for_each_ref(refs, "", NULL, fn, 0, 0, cb_data);
1612 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
1613 each_ref_fn fn, void *cb_data)
1615 return do_for_each_ref(refs, prefix, NULL, fn, strlen(prefix), 0, cb_data);
1618 int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
1619 const char **exclude_patterns,
1620 each_ref_fn fn, void *cb_data)
1622 return do_for_each_ref(refs, prefix, exclude_patterns, fn, 0, 0, cb_data);
1625 int refs_for_each_replace_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1627 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
1628 return do_for_each_ref(refs, git_replace_ref_base, NULL, fn,
1629 strlen(git_replace_ref_base),
1630 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1633 int refs_for_each_namespaced_ref(struct ref_store *refs,
1634 const char **exclude_patterns,
1635 each_ref_fn fn, void *cb_data)
1637 struct strbuf buf = STRBUF_INIT;
1638 int ret;
1639 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1640 ret = do_for_each_ref(refs, buf.buf, exclude_patterns, fn, 0, 0, cb_data);
1641 strbuf_release(&buf);
1642 return ret;
1645 int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1647 return do_for_each_ref(refs, "", NULL, fn, 0,
1648 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1651 int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
1652 void *cb_data)
1654 return do_for_each_ref(refs, "", NULL, fn, 0,
1655 DO_FOR_EACH_INCLUDE_ROOT_REFS, cb_data);
1658 static int qsort_strcmp(const void *va, const void *vb)
1660 const char *a = *(const char **)va;
1661 const char *b = *(const char **)vb;
1663 return strcmp(a, b);
1666 static void find_longest_prefixes_1(struct string_list *out,
1667 struct strbuf *prefix,
1668 const char **patterns, size_t nr)
1670 size_t i;
1672 for (i = 0; i < nr; i++) {
1673 char c = patterns[i][prefix->len];
1674 if (!c || is_glob_special(c)) {
1675 string_list_append(out, prefix->buf);
1676 return;
1680 i = 0;
1681 while (i < nr) {
1682 size_t end;
1685 * Set "end" to the index of the element _after_ the last one
1686 * in our group.
1688 for (end = i + 1; end < nr; end++) {
1689 if (patterns[i][prefix->len] != patterns[end][prefix->len])
1690 break;
1693 strbuf_addch(prefix, patterns[i][prefix->len]);
1694 find_longest_prefixes_1(out, prefix, patterns + i, end - i);
1695 strbuf_setlen(prefix, prefix->len - 1);
1697 i = end;
1701 static void find_longest_prefixes(struct string_list *out,
1702 const char **patterns)
1704 struct strvec sorted = STRVEC_INIT;
1705 struct strbuf prefix = STRBUF_INIT;
1707 strvec_pushv(&sorted, patterns);
1708 QSORT(sorted.v, sorted.nr, qsort_strcmp);
1710 find_longest_prefixes_1(out, &prefix, sorted.v, sorted.nr);
1712 strvec_clear(&sorted);
1713 strbuf_release(&prefix);
1716 int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store,
1717 const char *namespace,
1718 const char **patterns,
1719 const char **exclude_patterns,
1720 each_ref_fn fn, void *cb_data)
1722 struct string_list prefixes = STRING_LIST_INIT_DUP;
1723 struct string_list_item *prefix;
1724 struct strbuf buf = STRBUF_INIT;
1725 int ret = 0, namespace_len;
1727 find_longest_prefixes(&prefixes, patterns);
1729 if (namespace)
1730 strbuf_addstr(&buf, namespace);
1731 namespace_len = buf.len;
1733 for_each_string_list_item(prefix, &prefixes) {
1734 strbuf_addstr(&buf, prefix->string);
1735 ret = refs_for_each_fullref_in(ref_store, buf.buf,
1736 exclude_patterns, fn, cb_data);
1737 if (ret)
1738 break;
1739 strbuf_setlen(&buf, namespace_len);
1742 string_list_clear(&prefixes, 0);
1743 strbuf_release(&buf);
1744 return ret;
1747 static int refs_read_special_head(struct ref_store *ref_store,
1748 const char *refname, struct object_id *oid,
1749 struct strbuf *referent, unsigned int *type,
1750 int *failure_errno)
1752 struct strbuf full_path = STRBUF_INIT;
1753 struct strbuf content = STRBUF_INIT;
1754 int result = -1;
1755 strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname);
1757 if (strbuf_read_file(&content, full_path.buf, 0) < 0) {
1758 *failure_errno = errno;
1759 goto done;
1762 result = parse_loose_ref_contents(ref_store->repo->hash_algo, content.buf,
1763 oid, referent, type, failure_errno);
1765 done:
1766 strbuf_release(&full_path);
1767 strbuf_release(&content);
1768 return result;
1771 int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
1772 struct object_id *oid, struct strbuf *referent,
1773 unsigned int *type, int *failure_errno)
1775 assert(failure_errno);
1776 if (is_pseudo_ref(refname))
1777 return refs_read_special_head(ref_store, refname, oid, referent,
1778 type, failure_errno);
1780 return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
1781 type, failure_errno);
1784 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
1785 struct strbuf *referent)
1787 return ref_store->be->read_symbolic_ref(ref_store, refname, referent);
1790 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1791 const char *refname,
1792 int resolve_flags,
1793 struct object_id *oid,
1794 int *flags)
1796 static struct strbuf sb_refname = STRBUF_INIT;
1797 struct object_id unused_oid;
1798 int unused_flags;
1799 int symref_count;
1801 if (!oid)
1802 oid = &unused_oid;
1803 if (!flags)
1804 flags = &unused_flags;
1806 *flags = 0;
1808 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1809 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1810 !refname_is_safe(refname))
1811 return NULL;
1814 * repo_dwim_ref() uses REF_ISBROKEN to distinguish between
1815 * missing refs and refs that were present but invalid,
1816 * to complain about the latter to stderr.
1818 * We don't know whether the ref exists, so don't set
1819 * REF_ISBROKEN yet.
1821 *flags |= REF_BAD_NAME;
1824 for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1825 unsigned int read_flags = 0;
1826 int failure_errno;
1828 if (refs_read_raw_ref(refs, refname, oid, &sb_refname,
1829 &read_flags, &failure_errno)) {
1830 *flags |= read_flags;
1832 /* In reading mode, refs must eventually resolve */
1833 if (resolve_flags & RESOLVE_REF_READING)
1834 return NULL;
1837 * Otherwise a missing ref is OK. But the files backend
1838 * may show errors besides ENOENT if there are
1839 * similarly-named refs.
1841 if (failure_errno != ENOENT &&
1842 failure_errno != EISDIR &&
1843 failure_errno != ENOTDIR)
1844 return NULL;
1846 oidclr(oid, refs->repo->hash_algo);
1847 if (*flags & REF_BAD_NAME)
1848 *flags |= REF_ISBROKEN;
1849 return refname;
1852 *flags |= read_flags;
1854 if (!(read_flags & REF_ISSYMREF)) {
1855 if (*flags & REF_BAD_NAME) {
1856 oidclr(oid, refs->repo->hash_algo);
1857 *flags |= REF_ISBROKEN;
1859 return refname;
1862 refname = sb_refname.buf;
1863 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1864 oidclr(oid, refs->repo->hash_algo);
1865 return refname;
1867 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1868 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1869 !refname_is_safe(refname))
1870 return NULL;
1872 *flags |= REF_ISBROKEN | REF_BAD_NAME;
1876 return NULL;
1879 /* backend functions */
1880 int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err)
1882 return refs->be->create_on_disk(refs, flags, err);
1885 int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err)
1887 return refs->be->remove_on_disk(refs, err);
1890 int repo_resolve_gitlink_ref(struct repository *r,
1891 const char *submodule, const char *refname,
1892 struct object_id *oid)
1894 struct ref_store *refs;
1895 int flags;
1897 refs = repo_get_submodule_ref_store(r, submodule);
1898 if (!refs)
1899 return -1;
1901 if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) ||
1902 is_null_oid(oid))
1903 return -1;
1904 return 0;
1908 * Look up a ref store by name. If that ref_store hasn't been
1909 * registered yet, return NULL.
1911 static struct ref_store *lookup_ref_store_map(struct strmap *map,
1912 const char *name)
1914 struct strmap_entry *entry;
1916 if (!map->map.tablesize)
1917 /* It's initialized on demand in register_ref_store(). */
1918 return NULL;
1920 entry = strmap_get_entry(map, name);
1921 return entry ? entry->value : NULL;
1925 * Create, record, and return a ref_store instance for the specified
1926 * gitdir using the given ref storage format.
1928 static struct ref_store *ref_store_init(struct repository *repo,
1929 enum ref_storage_format format,
1930 const char *gitdir,
1931 unsigned int flags)
1933 const struct ref_storage_be *be;
1934 struct ref_store *refs;
1936 be = find_ref_storage_backend(format);
1937 if (!be)
1938 BUG("reference backend is unknown");
1940 refs = be->init(repo, gitdir, flags);
1941 return refs;
1944 void ref_store_release(struct ref_store *ref_store)
1946 ref_store->be->release(ref_store);
1947 free(ref_store->gitdir);
1950 struct ref_store *get_main_ref_store(struct repository *r)
1952 if (r->refs_private)
1953 return r->refs_private;
1955 if (!r->gitdir)
1956 BUG("attempting to get main_ref_store outside of repository");
1958 r->refs_private = ref_store_init(r, r->ref_storage_format,
1959 r->gitdir, REF_STORE_ALL_CAPS);
1960 r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private);
1961 return r->refs_private;
1965 * Associate a ref store with a name. It is a fatal error to call this
1966 * function twice for the same name.
1968 static void register_ref_store_map(struct strmap *map,
1969 const char *type,
1970 struct ref_store *refs,
1971 const char *name)
1973 if (!map->map.tablesize)
1974 strmap_init(map);
1975 if (strmap_put(map, name, refs))
1976 BUG("%s ref_store '%s' initialized twice", type, name);
1979 struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
1980 const char *submodule)
1982 struct strbuf submodule_sb = STRBUF_INIT;
1983 struct ref_store *refs;
1984 char *to_free = NULL;
1985 size_t len;
1986 struct repository *subrepo;
1988 if (!submodule)
1989 return NULL;
1991 len = strlen(submodule);
1992 while (len && is_dir_sep(submodule[len - 1]))
1993 len--;
1994 if (!len)
1995 return NULL;
1997 if (submodule[len])
1998 /* We need to strip off one or more trailing slashes */
1999 submodule = to_free = xmemdupz(submodule, len);
2001 refs = lookup_ref_store_map(&repo->submodule_ref_stores, submodule);
2002 if (refs)
2003 goto done;
2005 strbuf_addstr(&submodule_sb, submodule);
2006 if (!is_nonbare_repository_dir(&submodule_sb))
2007 goto done;
2009 if (submodule_to_gitdir(&submodule_sb, submodule))
2010 goto done;
2012 subrepo = xmalloc(sizeof(*subrepo));
2014 if (repo_submodule_init(subrepo, repo, submodule,
2015 null_oid())) {
2016 free(subrepo);
2017 goto done;
2019 refs = ref_store_init(subrepo, subrepo->ref_storage_format,
2020 submodule_sb.buf,
2021 REF_STORE_READ | REF_STORE_ODB);
2022 register_ref_store_map(&repo->submodule_ref_stores, "submodule",
2023 refs, submodule);
2025 done:
2026 strbuf_release(&submodule_sb);
2027 free(to_free);
2029 return refs;
2032 struct ref_store *get_worktree_ref_store(const struct worktree *wt)
2034 struct ref_store *refs;
2035 const char *id;
2037 if (wt->is_current)
2038 return get_main_ref_store(wt->repo);
2040 id = wt->id ? wt->id : "/";
2041 refs = lookup_ref_store_map(&wt->repo->worktree_ref_stores, id);
2042 if (refs)
2043 return refs;
2045 if (wt->id) {
2046 struct strbuf common_path = STRBUF_INIT;
2047 strbuf_git_common_path(&common_path, wt->repo,
2048 "worktrees/%s", wt->id);
2049 refs = ref_store_init(wt->repo, wt->repo->ref_storage_format,
2050 common_path.buf, REF_STORE_ALL_CAPS);
2051 strbuf_release(&common_path);
2052 } else {
2053 refs = ref_store_init(wt->repo, wt->repo->ref_storage_format,
2054 wt->repo->commondir, REF_STORE_ALL_CAPS);
2057 if (refs)
2058 register_ref_store_map(&wt->repo->worktree_ref_stores,
2059 "worktree", refs, id);
2061 return refs;
2064 void base_ref_store_init(struct ref_store *refs, struct repository *repo,
2065 const char *path, const struct ref_storage_be *be)
2067 refs->be = be;
2068 refs->repo = repo;
2069 refs->gitdir = xstrdup(path);
2072 /* backend functions */
2073 int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)
2075 return refs->be->pack_refs(refs, opts);
2078 int peel_iterated_oid(struct repository *r, const struct object_id *base, struct object_id *peeled)
2080 if (current_ref_iter &&
2081 (current_ref_iter->oid == base ||
2082 oideq(current_ref_iter->oid, base)))
2083 return ref_iterator_peel(current_ref_iter, peeled);
2085 return peel_object(r, base, peeled) ? -1 : 0;
2088 int refs_update_symref(struct ref_store *refs, const char *ref,
2089 const char *target, const char *logmsg)
2091 struct ref_transaction *transaction;
2092 struct strbuf err = STRBUF_INIT;
2093 int ret = 0;
2095 transaction = ref_store_transaction_begin(refs, &err);
2096 if (!transaction ||
2097 ref_transaction_update(transaction, ref, NULL, NULL,
2098 target, NULL, REF_NO_DEREF,
2099 logmsg, &err) ||
2100 ref_transaction_commit(transaction, &err)) {
2101 ret = error("%s", err.buf);
2104 strbuf_release(&err);
2105 if (transaction)
2106 ref_transaction_free(transaction);
2108 return ret;
2111 int ref_update_reject_duplicates(struct string_list *refnames,
2112 struct strbuf *err)
2114 size_t i, n = refnames->nr;
2116 assert(err);
2118 for (i = 1; i < n; i++) {
2119 int cmp = strcmp(refnames->items[i - 1].string,
2120 refnames->items[i].string);
2122 if (!cmp) {
2123 strbuf_addf(err,
2124 _("multiple updates for ref '%s' not allowed"),
2125 refnames->items[i].string);
2126 return 1;
2127 } else if (cmp > 0) {
2128 BUG("ref_update_reject_duplicates() received unsorted list");
2131 return 0;
2134 static int run_transaction_hook(struct ref_transaction *transaction,
2135 const char *state)
2137 struct child_process proc = CHILD_PROCESS_INIT;
2138 struct strbuf buf = STRBUF_INIT;
2139 const char *hook;
2140 int ret = 0, i;
2142 hook = find_hook(transaction->ref_store->repo, "reference-transaction");
2143 if (!hook)
2144 return ret;
2146 strvec_pushl(&proc.args, hook, state, NULL);
2147 proc.in = -1;
2148 proc.stdout_to_stderr = 1;
2149 proc.trace2_hook_name = "reference-transaction";
2151 ret = start_command(&proc);
2152 if (ret)
2153 return ret;
2155 sigchain_push(SIGPIPE, SIG_IGN);
2157 for (i = 0; i < transaction->nr; i++) {
2158 struct ref_update *update = transaction->updates[i];
2160 strbuf_reset(&buf);
2162 if (!(update->flags & REF_HAVE_OLD))
2163 strbuf_addf(&buf, "%s ", oid_to_hex(null_oid()));
2164 else if (update->old_target)
2165 strbuf_addf(&buf, "ref:%s ", update->old_target);
2166 else
2167 strbuf_addf(&buf, "%s ", oid_to_hex(&update->old_oid));
2169 if (!(update->flags & REF_HAVE_NEW))
2170 strbuf_addf(&buf, "%s ", oid_to_hex(null_oid()));
2171 else if (update->new_target)
2172 strbuf_addf(&buf, "ref:%s ", update->new_target);
2173 else
2174 strbuf_addf(&buf, "%s ", oid_to_hex(&update->new_oid));
2176 strbuf_addf(&buf, "%s\n", update->refname);
2178 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
2179 if (errno != EPIPE) {
2180 /* Don't leak errno outside this API */
2181 errno = 0;
2182 ret = -1;
2184 break;
2188 close(proc.in);
2189 sigchain_pop(SIGPIPE);
2190 strbuf_release(&buf);
2192 ret |= finish_command(&proc);
2193 return ret;
2196 int ref_transaction_prepare(struct ref_transaction *transaction,
2197 struct strbuf *err)
2199 struct ref_store *refs = transaction->ref_store;
2200 int ret;
2202 switch (transaction->state) {
2203 case REF_TRANSACTION_OPEN:
2204 /* Good. */
2205 break;
2206 case REF_TRANSACTION_PREPARED:
2207 BUG("prepare called twice on reference transaction");
2208 break;
2209 case REF_TRANSACTION_CLOSED:
2210 BUG("prepare called on a closed reference transaction");
2211 break;
2212 default:
2213 BUG("unexpected reference transaction state");
2214 break;
2217 if (refs->repo->objects->odb->disable_ref_updates) {
2218 strbuf_addstr(err,
2219 _("ref updates forbidden inside quarantine environment"));
2220 return -1;
2223 ret = refs->be->transaction_prepare(refs, transaction, err);
2224 if (ret)
2225 return ret;
2227 ret = run_transaction_hook(transaction, "prepared");
2228 if (ret) {
2229 ref_transaction_abort(transaction, err);
2230 die(_("ref updates aborted by hook"));
2233 return 0;
2236 int ref_transaction_abort(struct ref_transaction *transaction,
2237 struct strbuf *err)
2239 struct ref_store *refs = transaction->ref_store;
2240 int ret = 0;
2242 switch (transaction->state) {
2243 case REF_TRANSACTION_OPEN:
2244 /* No need to abort explicitly. */
2245 break;
2246 case REF_TRANSACTION_PREPARED:
2247 ret = refs->be->transaction_abort(refs, transaction, err);
2248 break;
2249 case REF_TRANSACTION_CLOSED:
2250 BUG("abort called on a closed reference transaction");
2251 break;
2252 default:
2253 BUG("unexpected reference transaction state");
2254 break;
2257 run_transaction_hook(transaction, "aborted");
2259 ref_transaction_free(transaction);
2260 return ret;
2263 int ref_transaction_commit(struct ref_transaction *transaction,
2264 struct strbuf *err)
2266 struct ref_store *refs = transaction->ref_store;
2267 int ret;
2269 switch (transaction->state) {
2270 case REF_TRANSACTION_OPEN:
2271 /* Need to prepare first. */
2272 ret = ref_transaction_prepare(transaction, err);
2273 if (ret)
2274 return ret;
2275 break;
2276 case REF_TRANSACTION_PREPARED:
2277 /* Fall through to finish. */
2278 break;
2279 case REF_TRANSACTION_CLOSED:
2280 BUG("commit called on a closed reference transaction");
2281 break;
2282 default:
2283 BUG("unexpected reference transaction state");
2284 break;
2287 ret = refs->be->transaction_finish(refs, transaction, err);
2288 if (!ret)
2289 run_transaction_hook(transaction, "committed");
2290 return ret;
2293 int refs_verify_refname_available(struct ref_store *refs,
2294 const char *refname,
2295 const struct string_list *extras,
2296 const struct string_list *skip,
2297 struct strbuf *err)
2299 const char *slash;
2300 const char *extra_refname;
2301 struct strbuf dirname = STRBUF_INIT;
2302 struct strbuf referent = STRBUF_INIT;
2303 struct object_id oid;
2304 unsigned int type;
2305 struct ref_iterator *iter;
2306 int ok;
2307 int ret = -1;
2310 * For the sake of comments in this function, suppose that
2311 * refname is "refs/foo/bar".
2314 assert(err);
2316 strbuf_grow(&dirname, strlen(refname) + 1);
2317 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
2319 * Just saying "Is a directory" when we e.g. can't
2320 * lock some multi-level ref isn't very informative,
2321 * the user won't be told *what* is a directory, so
2322 * let's not use strerror() below.
2324 int ignore_errno;
2325 /* Expand dirname to the new prefix, not including the trailing slash: */
2326 strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
2329 * We are still at a leading dir of the refname (e.g.,
2330 * "refs/foo"; if there is a reference with that name,
2331 * it is a conflict, *unless* it is in skip.
2333 if (skip && string_list_has_string(skip, dirname.buf))
2334 continue;
2336 if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
2337 &type, &ignore_errno)) {
2338 strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
2339 dirname.buf, refname);
2340 goto cleanup;
2343 if (extras && string_list_has_string(extras, dirname.buf)) {
2344 strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
2345 refname, dirname.buf);
2346 goto cleanup;
2351 * We are at the leaf of our refname (e.g., "refs/foo/bar").
2352 * There is no point in searching for a reference with that
2353 * name, because a refname isn't considered to conflict with
2354 * itself. But we still need to check for references whose
2355 * names are in the "refs/foo/bar/" namespace, because they
2356 * *do* conflict.
2358 strbuf_addstr(&dirname, refname + dirname.len);
2359 strbuf_addch(&dirname, '/');
2361 iter = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,
2362 DO_FOR_EACH_INCLUDE_BROKEN);
2363 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
2364 if (skip &&
2365 string_list_has_string(skip, iter->refname))
2366 continue;
2368 strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
2369 iter->refname, refname);
2370 ref_iterator_abort(iter);
2371 goto cleanup;
2374 if (ok != ITER_DONE)
2375 BUG("error while iterating over references");
2377 extra_refname = find_descendant_ref(dirname.buf, extras, skip);
2378 if (extra_refname)
2379 strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
2380 refname, extra_refname);
2381 else
2382 ret = 0;
2384 cleanup:
2385 strbuf_release(&referent);
2386 strbuf_release(&dirname);
2387 return ret;
2390 struct do_for_each_reflog_help {
2391 each_reflog_fn *fn;
2392 void *cb_data;
2395 static int do_for_each_reflog_helper(const char *refname,
2396 const char *referent UNUSED,
2397 const struct object_id *oid UNUSED,
2398 int flags UNUSED,
2399 void *cb_data)
2401 struct do_for_each_reflog_help *hp = cb_data;
2402 return hp->fn(refname, hp->cb_data);
2405 int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data)
2407 struct ref_iterator *iter;
2408 struct do_for_each_reflog_help hp = { fn, cb_data };
2410 iter = refs->be->reflog_iterator_begin(refs);
2412 return do_for_each_ref_iterator(iter, do_for_each_reflog_helper, &hp);
2415 int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
2416 const char *refname,
2417 each_reflog_ent_fn fn,
2418 void *cb_data)
2420 return refs->be->for_each_reflog_ent_reverse(refs, refname,
2421 fn, cb_data);
2424 int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
2425 each_reflog_ent_fn fn, void *cb_data)
2427 return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
2430 int refs_reflog_exists(struct ref_store *refs, const char *refname)
2432 return refs->be->reflog_exists(refs, refname);
2435 int refs_create_reflog(struct ref_store *refs, const char *refname,
2436 struct strbuf *err)
2438 return refs->be->create_reflog(refs, refname, err);
2441 int refs_delete_reflog(struct ref_store *refs, const char *refname)
2443 return refs->be->delete_reflog(refs, refname);
2446 int refs_reflog_expire(struct ref_store *refs,
2447 const char *refname,
2448 unsigned int flags,
2449 reflog_expiry_prepare_fn prepare_fn,
2450 reflog_expiry_should_prune_fn should_prune_fn,
2451 reflog_expiry_cleanup_fn cleanup_fn,
2452 void *policy_cb_data)
2454 return refs->be->reflog_expire(refs, refname, flags,
2455 prepare_fn, should_prune_fn,
2456 cleanup_fn, policy_cb_data);
2459 int initial_ref_transaction_commit(struct ref_transaction *transaction,
2460 struct strbuf *err)
2462 struct ref_store *refs = transaction->ref_store;
2464 return refs->be->initial_transaction_commit(refs, transaction, err);
2467 void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
2468 ref_transaction_for_each_queued_update_fn cb,
2469 void *cb_data)
2471 int i;
2473 for (i = 0; i < transaction->nr; i++) {
2474 struct ref_update *update = transaction->updates[i];
2476 cb(update->refname,
2477 (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL,
2478 (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL,
2479 cb_data);
2483 int refs_delete_refs(struct ref_store *refs, const char *logmsg,
2484 struct string_list *refnames, unsigned int flags)
2486 struct ref_transaction *transaction;
2487 struct strbuf err = STRBUF_INIT;
2488 struct string_list_item *item;
2489 int ret = 0, failures = 0;
2490 char *msg;
2492 if (!refnames->nr)
2493 return 0;
2495 msg = normalize_reflog_message(logmsg);
2498 * Since we don't check the references' old_oids, the
2499 * individual updates can't fail, so we can pack all of the
2500 * updates into a single transaction.
2502 transaction = ref_store_transaction_begin(refs, &err);
2503 if (!transaction) {
2504 ret = error("%s", err.buf);
2505 goto out;
2508 for_each_string_list_item(item, refnames) {
2509 ret = ref_transaction_delete(transaction, item->string,
2510 NULL, NULL, flags, msg, &err);
2511 if (ret) {
2512 warning(_("could not delete reference %s: %s"),
2513 item->string, err.buf);
2514 strbuf_reset(&err);
2515 failures = 1;
2519 ret = ref_transaction_commit(transaction, &err);
2520 if (ret) {
2521 if (refnames->nr == 1)
2522 error(_("could not delete reference %s: %s"),
2523 refnames->items[0].string, err.buf);
2524 else
2525 error(_("could not delete references: %s"), err.buf);
2528 out:
2529 if (!ret && failures)
2530 ret = -1;
2531 ref_transaction_free(transaction);
2532 strbuf_release(&err);
2533 free(msg);
2534 return ret;
2537 int refs_rename_ref(struct ref_store *refs, const char *oldref,
2538 const char *newref, const char *logmsg)
2540 char *msg;
2541 int retval;
2543 msg = normalize_reflog_message(logmsg);
2544 retval = refs->be->rename_ref(refs, oldref, newref, msg);
2545 free(msg);
2546 return retval;
2549 int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
2550 const char *newref, const char *logmsg)
2552 char *msg;
2553 int retval;
2555 msg = normalize_reflog_message(logmsg);
2556 retval = refs->be->copy_ref(refs, oldref, newref, msg);
2557 free(msg);
2558 return retval;
2561 const char *ref_update_original_update_refname(struct ref_update *update)
2563 while (update->parent_update)
2564 update = update->parent_update;
2566 return update->refname;
2569 int ref_update_has_null_new_value(struct ref_update *update)
2571 return !update->new_target && is_null_oid(&update->new_oid);
2574 int ref_update_check_old_target(const char *referent, struct ref_update *update,
2575 struct strbuf *err)
2577 if (!update->old_target)
2578 BUG("called without old_target set");
2580 if (!strcmp(referent, update->old_target))
2581 return 0;
2583 if (!strcmp(referent, ""))
2584 strbuf_addf(err, "verifying symref target: '%s': "
2585 "reference is missing but expected %s",
2586 ref_update_original_update_refname(update),
2587 update->old_target);
2588 else
2589 strbuf_addf(err, "verifying symref target: '%s': "
2590 "is at %s but expected %s",
2591 ref_update_original_update_refname(update),
2592 referent, update->old_target);
2593 return -1;
2596 struct migration_data {
2597 struct ref_store *old_refs;
2598 struct ref_transaction *transaction;
2599 struct strbuf *errbuf;
2602 static int migrate_one_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
2603 int flags, void *cb_data)
2605 struct migration_data *data = cb_data;
2606 struct strbuf symref_target = STRBUF_INIT;
2607 int ret;
2609 if (flags & REF_ISSYMREF) {
2610 ret = refs_read_symbolic_ref(data->old_refs, refname, &symref_target);
2611 if (ret < 0)
2612 goto done;
2614 ret = ref_transaction_update(data->transaction, refname, NULL, null_oid(),
2615 symref_target.buf, NULL,
2616 REF_SKIP_CREATE_REFLOG | REF_NO_DEREF, NULL, data->errbuf);
2617 if (ret < 0)
2618 goto done;
2619 } else {
2620 ret = ref_transaction_create(data->transaction, refname, oid, NULL,
2621 REF_SKIP_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION,
2622 NULL, data->errbuf);
2623 if (ret < 0)
2624 goto done;
2627 done:
2628 strbuf_release(&symref_target);
2629 return ret;
2632 static int move_files(const char *from_path, const char *to_path, struct strbuf *errbuf)
2634 struct strbuf from_buf = STRBUF_INIT, to_buf = STRBUF_INIT;
2635 size_t from_len, to_len;
2636 DIR *from_dir;
2637 int ret;
2639 from_dir = opendir(from_path);
2640 if (!from_dir) {
2641 strbuf_addf(errbuf, "could not open source directory '%s': %s",
2642 from_path, strerror(errno));
2643 ret = -1;
2644 goto done;
2647 strbuf_addstr(&from_buf, from_path);
2648 strbuf_complete(&from_buf, '/');
2649 from_len = from_buf.len;
2651 strbuf_addstr(&to_buf, to_path);
2652 strbuf_complete(&to_buf, '/');
2653 to_len = to_buf.len;
2655 while (1) {
2656 struct dirent *ent;
2658 errno = 0;
2659 ent = readdir(from_dir);
2660 if (!ent)
2661 break;
2663 if (!strcmp(ent->d_name, ".") ||
2664 !strcmp(ent->d_name, ".."))
2665 continue;
2667 strbuf_setlen(&from_buf, from_len);
2668 strbuf_addstr(&from_buf, ent->d_name);
2670 strbuf_setlen(&to_buf, to_len);
2671 strbuf_addstr(&to_buf, ent->d_name);
2673 ret = rename(from_buf.buf, to_buf.buf);
2674 if (ret < 0) {
2675 strbuf_addf(errbuf, "could not link file '%s' to '%s': %s",
2676 from_buf.buf, to_buf.buf, strerror(errno));
2677 goto done;
2681 if (errno) {
2682 strbuf_addf(errbuf, "could not read entry from directory '%s': %s",
2683 from_path, strerror(errno));
2684 ret = -1;
2685 goto done;
2688 ret = 0;
2690 done:
2691 strbuf_release(&from_buf);
2692 strbuf_release(&to_buf);
2693 if (from_dir)
2694 closedir(from_dir);
2695 return ret;
2698 static int count_reflogs(const char *reflog UNUSED, void *payload)
2700 size_t *reflog_count = payload;
2701 (*reflog_count)++;
2702 return 0;
2705 static int has_worktrees(void)
2707 struct worktree **worktrees = get_worktrees();
2708 int ret = 0;
2709 size_t i;
2711 for (i = 0; worktrees[i]; i++) {
2712 if (is_main_worktree(worktrees[i]))
2713 continue;
2714 ret = 1;
2717 free_worktrees(worktrees);
2718 return ret;
2721 int repo_migrate_ref_storage_format(struct repository *repo,
2722 enum ref_storage_format format,
2723 unsigned int flags,
2724 struct strbuf *errbuf)
2726 struct ref_store *old_refs = NULL, *new_refs = NULL;
2727 struct ref_transaction *transaction = NULL;
2728 struct strbuf new_gitdir = STRBUF_INIT;
2729 struct migration_data data;
2730 size_t reflog_count = 0;
2731 int did_migrate_refs = 0;
2732 int ret;
2734 if (repo->ref_storage_format == format) {
2735 strbuf_addstr(errbuf, "current and new ref storage format are equal");
2736 ret = -1;
2737 goto done;
2740 old_refs = get_main_ref_store(repo);
2743 * We do not have any interfaces that would allow us to write many
2744 * reflog entries. Once we have them we can remove this restriction.
2746 if (refs_for_each_reflog(old_refs, count_reflogs, &reflog_count) < 0) {
2747 strbuf_addstr(errbuf, "cannot count reflogs");
2748 ret = -1;
2749 goto done;
2751 if (reflog_count) {
2752 strbuf_addstr(errbuf, "migrating reflogs is not supported yet");
2753 ret = -1;
2754 goto done;
2758 * Worktrees complicate the migration because every worktree has a
2759 * separate ref storage. While it should be feasible to implement, this
2760 * is pushed out to a future iteration.
2762 * TODO: we should really be passing the caller-provided repository to
2763 * `has_worktrees()`, but our worktree subsystem doesn't yet support
2764 * that.
2766 if (has_worktrees()) {
2767 strbuf_addstr(errbuf, "migrating repositories with worktrees is not supported yet");
2768 ret = -1;
2769 goto done;
2773 * The overall logic looks like this:
2775 * 1. Set up a new temporary directory and initialize it with the new
2776 * format. This is where all refs will be migrated into.
2778 * 2. Enumerate all refs and write them into the new ref storage.
2779 * This operation is safe as we do not yet modify the main
2780 * repository.
2782 * 3. If we're in dry-run mode then we are done and can hand over the
2783 * directory to the caller for inspection. If not, we now start
2784 * with the destructive part.
2786 * 4. Delete the old ref storage from disk. As we have a copy of refs
2787 * in the new ref storage it's okay(ish) if we now get interrupted
2788 * as there is an equivalent copy of all refs available.
2790 * 5. Move the new ref storage files into place.
2792 * 6. Change the repository format to the new ref format.
2794 strbuf_addf(&new_gitdir, "%s/%s", old_refs->gitdir, "ref_migration.XXXXXX");
2795 if (!mkdtemp(new_gitdir.buf)) {
2796 strbuf_addf(errbuf, "cannot create migration directory: %s",
2797 strerror(errno));
2798 ret = -1;
2799 goto done;
2802 new_refs = ref_store_init(repo, format, new_gitdir.buf,
2803 REF_STORE_ALL_CAPS);
2804 ret = ref_store_create_on_disk(new_refs, 0, errbuf);
2805 if (ret < 0)
2806 goto done;
2808 transaction = ref_store_transaction_begin(new_refs, errbuf);
2809 if (!transaction)
2810 goto done;
2812 data.old_refs = old_refs;
2813 data.transaction = transaction;
2814 data.errbuf = errbuf;
2817 * We need to use the internal `do_for_each_ref()` here so that we can
2818 * also include broken refs and symrefs. These would otherwise be
2819 * skipped silently.
2821 * Ideally, we would do this call while locking the old ref storage
2822 * such that there cannot be any concurrent modifications. We do not
2823 * have the infra for that though, and the "files" backend does not
2824 * allow for a central lock due to its design. It's thus on the user to
2825 * ensure that there are no concurrent writes.
2827 ret = do_for_each_ref(old_refs, "", NULL, migrate_one_ref, 0,
2828 DO_FOR_EACH_INCLUDE_ROOT_REFS | DO_FOR_EACH_INCLUDE_BROKEN,
2829 &data);
2830 if (ret < 0)
2831 goto done;
2834 * TODO: we might want to migrate to `initial_ref_transaction_commit()`
2835 * here, which is more efficient for the files backend because it would
2836 * write new refs into the packed-refs file directly. At this point,
2837 * the files backend doesn't handle pseudo-refs and symrefs correctly
2838 * though, so this requires some more work.
2840 ret = ref_transaction_commit(transaction, errbuf);
2841 if (ret < 0)
2842 goto done;
2843 did_migrate_refs = 1;
2845 if (flags & REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN) {
2846 printf(_("Finished dry-run migration of refs, "
2847 "the result can be found at '%s'\n"), new_gitdir.buf);
2848 ret = 0;
2849 goto done;
2853 * Release the new ref store such that any potentially-open files will
2854 * be closed. This is required for platforms like Cygwin, where
2855 * renaming an open file results in EPERM.
2857 ref_store_release(new_refs);
2858 FREE_AND_NULL(new_refs);
2861 * Until now we were in the non-destructive phase, where we only
2862 * populated the new ref store. From hereon though we are about
2863 * to get hands by deleting the old ref store and then moving
2864 * the new one into place.
2866 * Assuming that there were no concurrent writes, the new ref
2867 * store should have all information. So if we fail from hereon
2868 * we may be in an in-between state, but it would still be able
2869 * to recover by manually moving remaining files from the
2870 * temporary migration directory into place.
2872 ret = ref_store_remove_on_disk(old_refs, errbuf);
2873 if (ret < 0)
2874 goto done;
2876 ret = move_files(new_gitdir.buf, old_refs->gitdir, errbuf);
2877 if (ret < 0)
2878 goto done;
2880 if (rmdir(new_gitdir.buf) < 0)
2881 warning_errno(_("could not remove temporary migration directory '%s'"),
2882 new_gitdir.buf);
2885 * We have migrated the repository, so we now need to adjust the
2886 * repository format so that clients will use the new ref store.
2887 * We also need to swap out the repository's main ref store.
2889 initialize_repository_version(hash_algo_by_ptr(repo->hash_algo), format, 1);
2892 * Unset the old ref store and release it. `get_main_ref_store()` will
2893 * make sure to lazily re-initialize the repository's ref store with
2894 * the new format.
2896 ref_store_release(old_refs);
2897 FREE_AND_NULL(old_refs);
2898 repo->refs_private = NULL;
2900 ret = 0;
2902 done:
2903 if (ret && did_migrate_refs) {
2904 strbuf_complete(errbuf, '\n');
2905 strbuf_addf(errbuf, _("migrated refs can be found at '%s'"),
2906 new_gitdir.buf);
2909 if (new_refs) {
2910 ref_store_release(new_refs);
2911 free(new_refs);
2913 ref_transaction_free(transaction);
2914 strbuf_release(&new_gitdir);
2915 return ret;
2918 int ref_update_expects_existing_old_ref(struct ref_update *update)
2920 return (update->flags & REF_HAVE_OLD) &&
2921 (!is_null_oid(&update->old_oid) || update->old_target);