2 * The backend-independent part of the reference module.
5 #define USE_THE_REPOSITORY_VARIABLE
6 #define DISABLE_SIGN_COMPARE_WARNINGS
8 #include "git-compat-util.h"
11 #include "environment.h"
18 #include "refs/refs-internal.h"
19 #include "run-command.h"
21 #include "object-name.h"
22 #include "object-store.h"
25 #include "submodule.h"
28 #include "repo-settings.h"
33 #include "wildmatch.h"
37 * List of all available backends
39 static const struct ref_storage_be
*refs_backends
[] = {
40 [REF_STORAGE_FORMAT_FILES
] = &refs_be_files
,
41 [REF_STORAGE_FORMAT_REFTABLE
] = &refs_be_reftable
,
44 static const struct ref_storage_be
*find_ref_storage_backend(
45 enum ref_storage_format ref_storage_format
)
47 if (ref_storage_format
< ARRAY_SIZE(refs_backends
))
48 return refs_backends
[ref_storage_format
];
52 enum ref_storage_format
ref_storage_format_by_name(const char *name
)
54 for (unsigned int i
= 0; i
< ARRAY_SIZE(refs_backends
); i
++)
55 if (refs_backends
[i
] && !strcmp(refs_backends
[i
]->name
, name
))
57 return REF_STORAGE_FORMAT_UNKNOWN
;
60 const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format
)
62 const struct ref_storage_be
*be
= find_ref_storage_backend(ref_storage_format
);
69 * How to handle various characters in refnames:
70 * 0: An acceptable character for refs
72 * 2: ., look for a preceding . to reject .. in refs
73 * 3: {, look for a preceding @ to reject @{ in refs
74 * 4: A bad character: ASCII control characters, and
75 * ":", "?", "[", "\", "^", "~", SP, or TAB
76 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
78 static unsigned char refname_disposition
[256] = {
79 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
80 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
81 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
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, 4, 4, 0, 4, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
89 struct ref_namespace_info ref_namespace
[] = {
92 .decoration
= DECORATION_REF_HEAD
,
95 [NAMESPACE_BRANCHES
] = {
97 .decoration
= DECORATION_REF_LOCAL
,
101 .decoration
= DECORATION_REF_TAG
,
103 [NAMESPACE_REMOTE_REFS
] = {
105 * The default refspec for new remotes copies refs from
106 * refs/heads/ on the remote into refs/remotes/<remote>/.
107 * As such, "refs/remotes/" has special handling.
109 .ref
= "refs/remotes/",
110 .decoration
= DECORATION_REF_REMOTE
,
112 [NAMESPACE_STASH
] = {
114 * The single ref "refs/stash" stores the latest stash.
115 * Older stashes can be found in the reflog.
119 .decoration
= DECORATION_REF_STASH
,
121 [NAMESPACE_REPLACE
] = {
123 * This namespace allows Git to act as if one object ID
124 * points to the content of another. Unlike the other
125 * ref namespaces, this one can be changed by the
126 * GIT_REPLACE_REF_BASE environment variable. This
127 * .namespace value will be overwritten in setup_git_env().
129 .ref
= "refs/replace/",
130 .decoration
= DECORATION_GRAFTED
,
132 [NAMESPACE_NOTES
] = {
134 * The refs/notes/commit ref points to the tip of a
135 * parallel commit history that adds metadata to commits
136 * in the normal history. This ref can be overwritten
137 * by the core.notesRef config variable or the
138 * GIT_NOTES_REFS environment variable.
140 .ref
= "refs/notes/commit",
143 [NAMESPACE_PREFETCH
] = {
145 * Prefetch refs are written by the background 'fetch'
146 * maintenance task. It allows faster foreground fetches
147 * by advertising these previously-downloaded tips without
148 * updating refs/remotes/ without user intervention.
150 .ref
= "refs/prefetch/",
152 [NAMESPACE_REWRITTEN
] = {
154 * Rewritten refs are used by the 'label' command in the
155 * sequencer. These are particularly useful during an
156 * interactive rebase that uses the 'merge' command.
158 .ref
= "refs/rewritten/",
162 void update_ref_namespace(enum ref_namespace
namespace, char *ref
)
164 struct ref_namespace_info
*info
= &ref_namespace
[namespace];
165 if (info
->ref_updated
)
166 free((char *)info
->ref
);
168 info
->ref_updated
= 1;
172 * Try to read one refname component from the front of refname.
173 * Return the length of the component found, or -1 if the component is
174 * not legal. It is legal if it is something reasonable to have under
175 * ".git/refs/"; We do not like it if:
177 * - it begins with ".", or
178 * - it has double dots "..", or
179 * - it has ASCII control characters, or
180 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
181 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
182 * - it ends with a "/", or
183 * - it ends with ".lock", or
184 * - it contains a "@{" portion
186 * When sanitized is not NULL, instead of rejecting the input refname
187 * as an error, try to come up with a usable replacement for the input
190 static int check_refname_component(const char *refname
, int *flags
,
191 struct strbuf
*sanitized
)
195 size_t component_start
= 0; /* garbage - not a reasonable initial value */
198 component_start
= sanitized
->len
;
200 for (cp
= refname
; ; cp
++) {
202 unsigned char disp
= refname_disposition
[ch
];
204 if (sanitized
&& disp
!= 1)
205 strbuf_addch(sanitized
, ch
);
211 if (last
== '.') { /* Refname contains "..". */
213 /* collapse ".." to single "." */
214 strbuf_setlen(sanitized
, sanitized
->len
- 1);
220 if (last
== '@') { /* Refname contains "@{". */
222 sanitized
->buf
[sanitized
->len
-1] = '-';
230 sanitized
->buf
[sanitized
->len
-1] = '-';
235 if (!(*flags
& REFNAME_REFSPEC_PATTERN
)) {
236 /* refspec can't be a pattern */
238 sanitized
->buf
[sanitized
->len
-1] = '-';
244 * Unset the pattern flag so that we only accept
245 * a single asterisk for one side of refspec.
247 *flags
&= ~ REFNAME_REFSPEC_PATTERN
;
254 return 0; /* Component has zero length. */
256 if (refname
[0] == '.') { /* Component starts with '.'. */
258 sanitized
->buf
[component_start
] = '-';
262 if (cp
- refname
>= LOCK_SUFFIX_LEN
&&
263 !memcmp(cp
- LOCK_SUFFIX_LEN
, LOCK_SUFFIX
, LOCK_SUFFIX_LEN
)) {
266 /* Refname ends with ".lock". */
267 while (strbuf_strip_suffix(sanitized
, LOCK_SUFFIX
)) {
268 /* try again in case we have .lock.lock */
274 static int check_or_sanitize_refname(const char *refname
, int flags
,
275 struct strbuf
*sanitized
)
277 int component_len
, component_count
= 0;
279 if (!strcmp(refname
, "@")) {
280 /* Refname is a single character '@'. */
282 strbuf_addch(sanitized
, '-');
288 if (sanitized
&& sanitized
->len
)
289 strbuf_complete(sanitized
, '/');
291 /* We are at the start of a path component. */
292 component_len
= check_refname_component(refname
, &flags
,
294 if (sanitized
&& component_len
== 0)
295 ; /* OK, omit empty component */
296 else if (component_len
<= 0)
300 if (refname
[component_len
] == '\0')
302 /* Skip to next component. */
303 refname
+= component_len
+ 1;
306 if (refname
[component_len
- 1] == '.') {
307 /* Refname ends with '.'. */
309 ; /* omit ending dot */
313 if (!(flags
& REFNAME_ALLOW_ONELEVEL
) && component_count
< 2)
314 return -1; /* Refname has only one component. */
318 int check_refname_format(const char *refname
, int flags
)
320 return check_or_sanitize_refname(refname
, flags
, NULL
);
323 int refs_fsck(struct ref_store
*refs
, struct fsck_options
*o
,
326 return refs
->be
->fsck(refs
, o
, wt
);
329 void sanitize_refname_component(const char *refname
, struct strbuf
*out
)
331 if (check_or_sanitize_refname(refname
, REFNAME_ALLOW_ONELEVEL
, out
))
332 BUG("sanitizing refname '%s' check returned error", refname
);
335 int refname_is_safe(const char *refname
)
339 if (skip_prefix(refname
, "refs/", &rest
)) {
342 size_t restlen
= strlen(rest
);
344 /* rest must not be empty, or start or end with "/" */
345 if (!restlen
|| *rest
== '/' || rest
[restlen
- 1] == '/')
349 * Does the refname try to escape refs/?
350 * For example: refs/foo/../bar is safe but refs/foo/../../bar
353 buf
= xmallocz(restlen
);
354 result
= !normalize_path_copy(buf
, rest
) && !strcmp(buf
, rest
);
360 if (!isupper(*refname
) && *refname
!= '_')
368 * Return true if refname, which has the specified oid and flags, can
369 * be resolved to an object in the database. If the referred-to object
370 * does not exist, emit a warning and return false.
372 int ref_resolves_to_object(const char *refname
,
373 struct repository
*repo
,
374 const struct object_id
*oid
,
377 if (flags
& REF_ISBROKEN
)
379 if (!has_object(repo
, oid
, HAS_OBJECT_RECHECK_PACKED
| HAS_OBJECT_FETCH_PROMISOR
)) {
380 error(_("%s does not point to a valid object!"), refname
);
386 char *refs_resolve_refdup(struct ref_store
*refs
,
387 const char *refname
, int resolve_flags
,
388 struct object_id
*oid
, int *flags
)
392 result
= refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
,
394 return xstrdup_or_null(result
);
397 /* The argument to for_each_filter_refs */
398 struct for_each_ref_filter
{
405 int refs_read_ref_full(struct ref_store
*refs
, const char *refname
,
406 int resolve_flags
, struct object_id
*oid
, int *flags
)
408 if (refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
,
414 int refs_read_ref(struct ref_store
*refs
, const char *refname
, struct object_id
*oid
)
416 return refs_read_ref_full(refs
, refname
, RESOLVE_REF_READING
, oid
, NULL
);
419 int refs_ref_exists(struct ref_store
*refs
, const char *refname
)
421 return !!refs_resolve_ref_unsafe(refs
, refname
, RESOLVE_REF_READING
,
425 static int for_each_filter_refs(const char *refname
, const char *referent
,
426 const struct object_id
*oid
,
427 int flags
, void *data
)
429 struct for_each_ref_filter
*filter
= data
;
431 if (wildmatch(filter
->pattern
, refname
, 0))
434 skip_prefix(refname
, filter
->prefix
, &refname
);
435 return filter
->fn(refname
, referent
, oid
, flags
, filter
->cb_data
);
438 struct warn_if_dangling_data
{
439 struct ref_store
*refs
;
442 const struct string_list
*refnames
;
446 static int warn_if_dangling_symref(const char *refname
, const char *referent UNUSED
,
447 const struct object_id
*oid UNUSED
,
448 int flags
, void *cb_data
)
450 struct warn_if_dangling_data
*d
= cb_data
;
451 const char *resolves_to
;
453 if (!(flags
& REF_ISSYMREF
))
456 resolves_to
= refs_resolve_ref_unsafe(d
->refs
, refname
, 0, NULL
, NULL
);
459 ? strcmp(resolves_to
, d
->refname
)
460 : !string_list_has_string(d
->refnames
, resolves_to
))) {
464 fprintf(d
->fp
, d
->msg_fmt
, refname
);
469 void refs_warn_dangling_symref(struct ref_store
*refs
, FILE *fp
,
470 const char *msg_fmt
, const char *refname
)
472 struct warn_if_dangling_data data
= {
478 refs_for_each_rawref(refs
, warn_if_dangling_symref
, &data
);
481 void refs_warn_dangling_symrefs(struct ref_store
*refs
, FILE *fp
,
482 const char *msg_fmt
, const struct string_list
*refnames
)
484 struct warn_if_dangling_data data
= {
487 .refnames
= refnames
,
490 refs_for_each_rawref(refs
, warn_if_dangling_symref
, &data
);
493 int refs_for_each_tag_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
495 return refs_for_each_ref_in(refs
, "refs/tags/", fn
, cb_data
);
498 int refs_for_each_branch_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
500 return refs_for_each_ref_in(refs
, "refs/heads/", fn
, cb_data
);
503 int refs_for_each_remote_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
505 return refs_for_each_ref_in(refs
, "refs/remotes/", fn
, cb_data
);
508 int refs_head_ref_namespaced(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
510 struct strbuf buf
= STRBUF_INIT
;
512 struct object_id oid
;
515 strbuf_addf(&buf
, "%sHEAD", get_git_namespace());
516 if (!refs_read_ref_full(refs
, buf
.buf
, RESOLVE_REF_READING
, &oid
, &flag
))
517 ret
= fn(buf
.buf
, NULL
, &oid
, flag
, cb_data
);
518 strbuf_release(&buf
);
523 void normalize_glob_ref(struct string_list_item
*item
, const char *prefix
,
526 struct strbuf normalized_pattern
= STRBUF_INIT
;
529 BUG("pattern must not start with '/'");
532 strbuf_addstr(&normalized_pattern
, prefix
);
533 else if (!starts_with(pattern
, "refs/") &&
534 strcmp(pattern
, "HEAD"))
535 strbuf_addstr(&normalized_pattern
, "refs/");
537 * NEEDSWORK: Special case other symrefs such as REBASE_HEAD,
541 strbuf_addstr(&normalized_pattern
, pattern
);
542 strbuf_strip_suffix(&normalized_pattern
, "/");
544 item
->string
= strbuf_detach(&normalized_pattern
, NULL
);
545 item
->util
= has_glob_specials(pattern
) ? NULL
: item
->string
;
546 strbuf_release(&normalized_pattern
);
549 int refs_for_each_glob_ref_in(struct ref_store
*refs
, each_ref_fn fn
,
550 const char *pattern
, const char *prefix
, void *cb_data
)
552 struct strbuf real_pattern
= STRBUF_INIT
;
553 struct for_each_ref_filter filter
;
556 if (!prefix
&& !starts_with(pattern
, "refs/"))
557 strbuf_addstr(&real_pattern
, "refs/");
559 strbuf_addstr(&real_pattern
, prefix
);
560 strbuf_addstr(&real_pattern
, pattern
);
562 if (!has_glob_specials(pattern
)) {
563 /* Append implied '/' '*' if not present. */
564 strbuf_complete(&real_pattern
, '/');
565 /* No need to check for '*', there is none. */
566 strbuf_addch(&real_pattern
, '*');
569 filter
.pattern
= real_pattern
.buf
;
570 filter
.prefix
= prefix
;
572 filter
.cb_data
= cb_data
;
573 ret
= refs_for_each_ref(refs
, for_each_filter_refs
, &filter
);
575 strbuf_release(&real_pattern
);
579 int refs_for_each_glob_ref(struct ref_store
*refs
, each_ref_fn fn
,
580 const char *pattern
, void *cb_data
)
582 return refs_for_each_glob_ref_in(refs
, fn
, pattern
, NULL
, cb_data
);
585 const char *prettify_refname(const char *name
)
587 if (skip_prefix(name
, "refs/heads/", &name
) ||
588 skip_prefix(name
, "refs/tags/", &name
) ||
589 skip_prefix(name
, "refs/remotes/", &name
))
594 static const char *ref_rev_parse_rules
[] = {
600 "refs/remotes/%.*s/HEAD",
604 #define NUM_REV_PARSE_RULES (ARRAY_SIZE(ref_rev_parse_rules) - 1)
607 * Is it possible that the caller meant full_name with abbrev_name?
608 * If so return a non-zero value to signal "yes"; the magnitude of
609 * the returned value gives the precedence used for disambiguation.
611 * If abbrev_name cannot mean full_name, return 0.
613 int refname_match(const char *abbrev_name
, const char *full_name
)
616 const int abbrev_name_len
= strlen(abbrev_name
);
617 const int num_rules
= NUM_REV_PARSE_RULES
;
619 for (p
= ref_rev_parse_rules
; *p
; p
++)
620 if (!strcmp(full_name
, mkpath(*p
, abbrev_name_len
, abbrev_name
)))
621 return &ref_rev_parse_rules
[num_rules
] - p
;
627 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
628 * the results to 'prefixes'
630 void expand_ref_prefix(struct strvec
*prefixes
, const char *prefix
)
633 int len
= strlen(prefix
);
635 for (p
= ref_rev_parse_rules
; *p
; p
++)
636 strvec_pushf(prefixes
, *p
, len
, prefix
);
639 static const char default_branch_name_advice
[] = N_(
640 "Using '%s' as the name for the initial branch. This default branch name\n"
641 "is subject to change. To configure the initial branch name to use in all\n"
642 "of your new repositories, which will suppress this warning, call:\n"
644 "\tgit config --global init.defaultBranch <name>\n"
646 "Names commonly chosen instead of 'master' are 'main', 'trunk' and\n"
647 "'development'. The just-created branch can be renamed via this command:\n"
649 "\tgit branch -m <name>\n"
652 char *repo_default_branch_name(struct repository
*r
, int quiet
)
654 const char *config_key
= "init.defaultbranch";
655 const char *config_display_key
= "init.defaultBranch";
656 char *ret
= NULL
, *full_ref
;
657 const char *env
= getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME");
661 else if (repo_config_get_string(r
, config_key
, &ret
) < 0)
662 die(_("could not retrieve `%s`"), config_display_key
);
665 ret
= xstrdup("master");
667 advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME
,
668 _(default_branch_name_advice
), ret
);
671 full_ref
= xstrfmt("refs/heads/%s", ret
);
672 if (check_refname_format(full_ref
, 0))
673 die(_("invalid branch name: %s = %s"), config_display_key
, ret
);
680 * *string and *len will only be substituted, and *string returned (for
681 * later free()ing) if the string passed in is a magic short-hand form
684 static char *substitute_branch_name(struct repository
*r
,
685 const char **string
, int *len
,
686 int nonfatal_dangling_mark
)
688 struct strbuf buf
= STRBUF_INIT
;
689 struct interpret_branch_name_options options
= {
690 .nonfatal_dangling_mark
= nonfatal_dangling_mark
692 int ret
= repo_interpret_branch_name(r
, *string
, *len
, &buf
, &options
);
696 *string
= strbuf_detach(&buf
, &size
);
698 return (char *)*string
;
704 void copy_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
706 int len
= strlen(name
);
707 struct interpret_branch_name_options options
= {
710 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
715 strbuf_add(sb
, name
+ used
, len
- used
);
718 int check_branch_ref(struct strbuf
*sb
, const char *name
)
720 if (startup_info
->have_repository
)
721 copy_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
723 strbuf_addstr(sb
, name
);
726 * This splice must be done even if we end up rejecting the
727 * name; builtin/branch.c::copy_or_rename_branch() still wants
728 * to see what the name expanded to so that "branch -m" can be
729 * used as a tool to correct earlier mistakes.
731 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
734 !strcmp(sb
->buf
, "refs/heads/HEAD"))
737 return check_refname_format(sb
->buf
, 0);
740 int check_tag_ref(struct strbuf
*sb
, const char *name
)
742 if (name
[0] == '-' || !strcmp(name
, "HEAD"))
746 strbuf_addf(sb
, "refs/tags/%s", name
);
748 return check_refname_format(sb
->buf
, 0);
751 int repo_dwim_ref(struct repository
*r
, const char *str
, int len
,
752 struct object_id
*oid
, char **ref
, int nonfatal_dangling_mark
)
754 char *last_branch
= substitute_branch_name(r
, &str
, &len
,
755 nonfatal_dangling_mark
);
756 int refs_found
= expand_ref(r
, str
, len
, oid
, ref
);
761 int expand_ref(struct repository
*repo
, const char *str
, int len
,
762 struct object_id
*oid
, char **ref
)
766 struct strbuf fullref
= STRBUF_INIT
;
769 for (p
= ref_rev_parse_rules
; *p
; p
++) {
770 struct object_id oid_from_ref
;
771 struct object_id
*this_result
;
773 struct ref_store
*refs
= get_main_ref_store(repo
);
775 this_result
= refs_found
? &oid_from_ref
: oid
;
776 strbuf_reset(&fullref
);
777 strbuf_addf(&fullref
, *p
, len
, str
);
778 r
= refs_resolve_ref_unsafe(refs
, fullref
.buf
,
784 if (!repo_settings_get_warn_ambiguous_refs(repo
))
786 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
.buf
, "HEAD")) {
787 warning(_("ignoring dangling symref %s"), fullref
.buf
);
788 } else if ((flag
& REF_ISBROKEN
) && strchr(fullref
.buf
, '/')) {
789 warning(_("ignoring broken ref %s"), fullref
.buf
);
792 strbuf_release(&fullref
);
796 int repo_dwim_log(struct repository
*r
, const char *str
, int len
,
797 struct object_id
*oid
, char **log
)
799 struct ref_store
*refs
= get_main_ref_store(r
);
800 char *last_branch
= substitute_branch_name(r
, &str
, &len
, 0);
803 struct strbuf path
= STRBUF_INIT
;
806 for (p
= ref_rev_parse_rules
; *p
; p
++) {
807 struct object_id hash
;
808 const char *ref
, *it
;
811 strbuf_addf(&path
, *p
, len
, str
);
812 ref
= refs_resolve_ref_unsafe(refs
, path
.buf
,
814 oid
? &hash
: NULL
, NULL
);
817 if (refs_reflog_exists(refs
, path
.buf
))
819 else if (strcmp(ref
, path
.buf
) &&
820 refs_reflog_exists(refs
, ref
))
829 if (!repo_settings_get_warn_ambiguous_refs(r
))
832 strbuf_release(&path
);
837 int is_per_worktree_ref(const char *refname
)
839 return starts_with(refname
, "refs/worktree/") ||
840 starts_with(refname
, "refs/bisect/") ||
841 starts_with(refname
, "refs/rewritten/");
844 int is_pseudo_ref(const char *refname
)
846 static const char * const pseudo_refs
[] = {
852 for (i
= 0; i
< ARRAY_SIZE(pseudo_refs
); i
++)
853 if (!strcmp(refname
, pseudo_refs
[i
]))
859 static int is_root_ref_syntax(const char *refname
)
863 for (c
= refname
; *c
; c
++) {
864 if (!isupper(*c
) && *c
!= '-' && *c
!= '_')
871 int is_root_ref(const char *refname
)
873 static const char *const irregular_root_refs
[] = {
876 "BISECT_EXPECTED_REV",
877 "NOTES_MERGE_PARTIAL",
883 if (!is_root_ref_syntax(refname
) ||
884 is_pseudo_ref(refname
))
887 if (ends_with(refname
, "_HEAD"))
890 for (i
= 0; i
< ARRAY_SIZE(irregular_root_refs
); i
++)
891 if (!strcmp(refname
, irregular_root_refs
[i
]))
897 static int is_current_worktree_ref(const char *ref
) {
898 return is_root_ref_syntax(ref
) || is_per_worktree_ref(ref
);
901 enum ref_worktree_type
parse_worktree_ref(const char *maybe_worktree_ref
,
902 const char **worktree_name
, int *worktree_name_length
,
903 const char **bare_refname
)
905 const char *name_dummy
;
906 int name_length_dummy
;
907 const char *ref_dummy
;
910 worktree_name
= &name_dummy
;
911 if (!worktree_name_length
)
912 worktree_name_length
= &name_length_dummy
;
914 bare_refname
= &ref_dummy
;
916 if (skip_prefix(maybe_worktree_ref
, "worktrees/", bare_refname
)) {
917 const char *slash
= strchr(*bare_refname
, '/');
919 *worktree_name
= *bare_refname
;
921 *worktree_name_length
= strlen(*worktree_name
);
923 /* This is an error condition, and the caller tell because the bare_refname is "" */
924 *bare_refname
= *worktree_name
+ *worktree_name_length
;
925 return REF_WORKTREE_OTHER
;
928 *worktree_name_length
= slash
- *bare_refname
;
929 *bare_refname
= slash
+ 1;
931 if (is_current_worktree_ref(*bare_refname
))
932 return REF_WORKTREE_OTHER
;
935 *worktree_name
= NULL
;
936 *worktree_name_length
= 0;
938 if (skip_prefix(maybe_worktree_ref
, "main-worktree/", bare_refname
)
939 && is_current_worktree_ref(*bare_refname
))
940 return REF_WORKTREE_MAIN
;
942 *bare_refname
= maybe_worktree_ref
;
943 if (is_current_worktree_ref(maybe_worktree_ref
))
944 return REF_WORKTREE_CURRENT
;
946 return REF_WORKTREE_SHARED
;
949 long get_files_ref_lock_timeout_ms(void)
951 static int configured
= 0;
953 /* The default timeout is 100 ms: */
954 static int timeout_ms
= 100;
957 git_config_get_int("core.filesreflocktimeout", &timeout_ms
);
964 int refs_delete_ref(struct ref_store
*refs
, const char *msg
,
966 const struct object_id
*old_oid
,
969 struct ref_transaction
*transaction
;
970 struct strbuf err
= STRBUF_INIT
;
972 transaction
= ref_store_transaction_begin(refs
, 0, &err
);
974 ref_transaction_delete(transaction
, refname
, old_oid
,
975 NULL
, flags
, msg
, &err
) ||
976 ref_transaction_commit(transaction
, &err
)) {
977 error("%s", err
.buf
);
978 ref_transaction_free(transaction
);
979 strbuf_release(&err
);
982 ref_transaction_free(transaction
);
983 strbuf_release(&err
);
987 static void copy_reflog_msg(struct strbuf
*sb
, const char *msg
)
992 while ((c
= *msg
++)) {
993 if (wasspace
&& isspace(c
))
995 wasspace
= isspace(c
);
1003 static char *normalize_reflog_message(const char *msg
)
1005 struct strbuf sb
= STRBUF_INIT
;
1008 copy_reflog_msg(&sb
, msg
);
1009 return strbuf_detach(&sb
, NULL
);
1012 int should_autocreate_reflog(enum log_refs_config log_all_ref_updates
,
1013 const char *refname
)
1015 switch (log_all_ref_updates
) {
1016 case LOG_REFS_ALWAYS
:
1018 case LOG_REFS_NORMAL
:
1019 return starts_with(refname
, "refs/heads/") ||
1020 starts_with(refname
, "refs/remotes/") ||
1021 starts_with(refname
, "refs/notes/") ||
1022 !strcmp(refname
, "HEAD");
1028 int is_branch(const char *refname
)
1030 return !strcmp(refname
, "HEAD") || starts_with(refname
, "refs/heads/");
1033 struct read_ref_at_cb
{
1034 const char *refname
;
1035 timestamp_t at_time
;
1038 struct object_id
*oid
;
1041 struct object_id ooid
;
1042 struct object_id noid
;
1046 timestamp_t
*cutoff_time
;
1051 static void set_read_ref_cutoffs(struct read_ref_at_cb
*cb
,
1052 timestamp_t timestamp
, int tz
, const char *message
)
1055 *cb
->msg
= xstrdup(message
);
1056 if (cb
->cutoff_time
)
1057 *cb
->cutoff_time
= timestamp
;
1059 *cb
->cutoff_tz
= tz
;
1061 *cb
->cutoff_cnt
= cb
->reccnt
;
1064 static int read_ref_at_ent(struct object_id
*ooid
, struct object_id
*noid
,
1065 const char *email UNUSED
,
1066 timestamp_t timestamp
, int tz
,
1067 const char *message
, void *cb_data
)
1069 struct read_ref_at_cb
*cb
= cb_data
;
1072 cb
->date
= timestamp
;
1074 if (timestamp
<= cb
->at_time
|| cb
->cnt
== 0) {
1075 set_read_ref_cutoffs(cb
, timestamp
, tz
, message
);
1077 * we have not yet updated cb->[n|o]oid so they still
1078 * hold the values for the previous record.
1080 if (!is_null_oid(&cb
->ooid
)) {
1081 oidcpy(cb
->oid
, noid
);
1082 if (!oideq(&cb
->ooid
, noid
))
1083 warning(_("log for ref %s has gap after %s"),
1084 cb
->refname
, show_date(cb
->date
, cb
->tz
, DATE_MODE(RFC2822
)));
1086 else if (cb
->date
== cb
->at_time
)
1087 oidcpy(cb
->oid
, noid
);
1088 else if (!oideq(noid
, cb
->oid
))
1089 warning(_("log for ref %s unexpectedly ended on %s"),
1090 cb
->refname
, show_date(cb
->date
, cb
->tz
,
1091 DATE_MODE(RFC2822
)));
1093 oidcpy(&cb
->ooid
, ooid
);
1094 oidcpy(&cb
->noid
, noid
);
1099 oidcpy(&cb
->ooid
, ooid
);
1100 oidcpy(&cb
->noid
, noid
);
1106 static int read_ref_at_ent_oldest(struct object_id
*ooid
, struct object_id
*noid
,
1107 const char *email UNUSED
,
1108 timestamp_t timestamp
, int tz
,
1109 const char *message
, void *cb_data
)
1111 struct read_ref_at_cb
*cb
= cb_data
;
1113 set_read_ref_cutoffs(cb
, timestamp
, tz
, message
);
1114 oidcpy(cb
->oid
, ooid
);
1115 if (cb
->at_time
&& is_null_oid(cb
->oid
))
1116 oidcpy(cb
->oid
, noid
);
1117 /* We just want the first entry */
1121 int read_ref_at(struct ref_store
*refs
, const char *refname
,
1122 unsigned int flags
, timestamp_t at_time
, int cnt
,
1123 struct object_id
*oid
, char **msg
,
1124 timestamp_t
*cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
1126 struct read_ref_at_cb cb
;
1128 memset(&cb
, 0, sizeof(cb
));
1129 cb
.refname
= refname
;
1130 cb
.at_time
= at_time
;
1133 cb
.cutoff_time
= cutoff_time
;
1134 cb
.cutoff_tz
= cutoff_tz
;
1135 cb
.cutoff_cnt
= cutoff_cnt
;
1138 refs_for_each_reflog_ent_reverse(refs
, refname
, read_ref_at_ent
, &cb
);
1143 * The caller asked for ref@{0}, and we had no entries.
1144 * It's a bit subtle, but in practice all callers have
1145 * prepped the "oid" field with the current value of
1146 * the ref, which is the most reasonable fallback.
1148 * We'll put dummy values into the out-parameters (so
1149 * they're not just uninitialized garbage), and the
1150 * caller can take our return value as a hint that
1151 * we did not find any such reflog.
1153 set_read_ref_cutoffs(&cb
, 0, 0, "empty reflog");
1156 if (flags
& GET_OID_QUIETLY
)
1159 die(_("log for %s is empty"), refname
);
1164 refs_for_each_reflog_ent(refs
, refname
, read_ref_at_ent_oldest
, &cb
);
1169 struct ref_transaction
*ref_store_transaction_begin(struct ref_store
*refs
,
1173 struct ref_transaction
*tr
;
1176 CALLOC_ARRAY(tr
, 1);
1177 tr
->ref_store
= refs
;
1179 string_list_init_dup(&tr
->refnames
);
1181 if (flags
& REF_TRANSACTION_ALLOW_FAILURE
)
1182 CALLOC_ARRAY(tr
->rejections
, 1);
1187 void ref_transaction_free(struct ref_transaction
*transaction
)
1194 switch (transaction
->state
) {
1195 case REF_TRANSACTION_OPEN
:
1196 case REF_TRANSACTION_CLOSED
:
1199 case REF_TRANSACTION_PREPARED
:
1200 BUG("free called on a prepared reference transaction");
1203 BUG("unexpected reference transaction state");
1207 for (i
= 0; i
< transaction
->nr
; i
++) {
1208 free(transaction
->updates
[i
]->msg
);
1209 free(transaction
->updates
[i
]->committer_info
);
1210 free((char *)transaction
->updates
[i
]->new_target
);
1211 free((char *)transaction
->updates
[i
]->old_target
);
1212 free(transaction
->updates
[i
]);
1215 if (transaction
->rejections
)
1216 free(transaction
->rejections
->update_indices
);
1217 free(transaction
->rejections
);
1219 string_list_clear(&transaction
->refnames
, 0);
1220 free(transaction
->updates
);
1224 int ref_transaction_maybe_set_rejected(struct ref_transaction
*transaction
,
1226 enum ref_transaction_error err
)
1228 if (update_idx
>= transaction
->nr
)
1229 BUG("trying to set rejection on invalid update index");
1231 if (!(transaction
->flags
& REF_TRANSACTION_ALLOW_FAILURE
))
1234 if (!transaction
->rejections
)
1235 BUG("transaction not inititalized with failure support");
1238 * Don't accept generic errors, since these errors are not user
1241 if (err
== REF_TRANSACTION_ERROR_GENERIC
)
1244 transaction
->updates
[update_idx
]->rejection_err
= err
;
1245 ALLOC_GROW(transaction
->rejections
->update_indices
,
1246 transaction
->rejections
->nr
+ 1,
1247 transaction
->rejections
->alloc
);
1248 transaction
->rejections
->update_indices
[transaction
->rejections
->nr
++] = update_idx
;
1253 struct ref_update
*ref_transaction_add_update(
1254 struct ref_transaction
*transaction
,
1255 const char *refname
, unsigned int flags
,
1256 const struct object_id
*new_oid
,
1257 const struct object_id
*old_oid
,
1258 const char *new_target
, const char *old_target
,
1259 const char *committer_info
,
1262 struct string_list_item
*item
;
1263 struct ref_update
*update
;
1265 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
1266 BUG("update called for transaction that is not open");
1268 if (old_oid
&& old_target
)
1269 BUG("only one of old_oid and old_target should be non NULL");
1270 if (new_oid
&& new_target
)
1271 BUG("only one of new_oid and new_target should be non NULL");
1273 FLEX_ALLOC_STR(update
, refname
, refname
);
1274 ALLOC_GROW(transaction
->updates
, transaction
->nr
+ 1, transaction
->alloc
);
1275 transaction
->updates
[transaction
->nr
++] = update
;
1277 update
->flags
= flags
;
1278 update
->rejection_err
= 0;
1280 update
->new_target
= xstrdup_or_null(new_target
);
1281 update
->old_target
= xstrdup_or_null(old_target
);
1282 if ((flags
& REF_HAVE_NEW
) && new_oid
)
1283 oidcpy(&update
->new_oid
, new_oid
);
1284 if ((flags
& REF_HAVE_OLD
) && old_oid
)
1285 oidcpy(&update
->old_oid
, old_oid
);
1286 if (!(flags
& REF_SKIP_CREATE_REFLOG
)) {
1287 update
->committer_info
= xstrdup_or_null(committer_info
);
1288 update
->msg
= normalize_reflog_message(msg
);
1292 * This list is generally used by the backends to avoid duplicates.
1293 * But we do support multiple log updates for a given refname within
1294 * a single transaction.
1296 if (!(update
->flags
& REF_LOG_ONLY
)) {
1297 item
= string_list_append(&transaction
->refnames
, refname
);
1298 item
->util
= update
;
1304 static int transaction_refname_valid(const char *refname
,
1305 const struct object_id
*new_oid
,
1306 unsigned int flags
, struct strbuf
*err
)
1308 if (flags
& REF_SKIP_REFNAME_VERIFICATION
)
1311 if (is_pseudo_ref(refname
)) {
1312 const char *refusal_msg
;
1313 if (flags
& REF_LOG_ONLY
)
1314 refusal_msg
= _("refusing to update reflog for pseudoref '%s'");
1316 refusal_msg
= _("refusing to update pseudoref '%s'");
1317 strbuf_addf(err
, refusal_msg
, refname
);
1319 } else if ((new_oid
&& !is_null_oid(new_oid
)) ?
1320 check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
) :
1321 !refname_is_safe(refname
)) {
1322 const char *refusal_msg
;
1323 if (flags
& REF_LOG_ONLY
)
1324 refusal_msg
= _("refusing to update reflog with bad name '%s'");
1326 refusal_msg
= _("refusing to update ref with bad name '%s'");
1327 strbuf_addf(err
, refusal_msg
, refname
);
1334 int ref_transaction_update(struct ref_transaction
*transaction
,
1335 const char *refname
,
1336 const struct object_id
*new_oid
,
1337 const struct object_id
*old_oid
,
1338 const char *new_target
,
1339 const char *old_target
,
1340 unsigned int flags
, const char *msg
,
1345 if ((flags
& REF_FORCE_CREATE_REFLOG
) &&
1346 (flags
& REF_SKIP_CREATE_REFLOG
)) {
1347 strbuf_addstr(err
, _("refusing to force and skip creation of reflog"));
1351 if (!transaction_refname_valid(refname
, new_oid
, flags
, err
))
1354 if (flags
& ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS
)
1355 BUG("illegal flags 0x%x passed to ref_transaction_update()", flags
);
1358 * Clear flags outside the allowed set; this should be a noop because
1359 * of the BUG() check above, but it works around a -Wnonnull warning
1360 * with some versions of "gcc -O3".
1362 flags
&= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS
;
1364 flags
|= (new_oid
? REF_HAVE_NEW
: 0) | (old_oid
? REF_HAVE_OLD
: 0);
1365 flags
|= (new_target
? REF_HAVE_NEW
: 0) | (old_target
? REF_HAVE_OLD
: 0);
1367 ref_transaction_add_update(transaction
, refname
, flags
,
1368 new_oid
, old_oid
, new_target
,
1369 old_target
, NULL
, msg
);
1375 * Similar to`ref_transaction_update`, but this function is only for adding
1376 * a reflog update. Supports providing custom committer information. The index
1377 * field can be utiltized to order updates as desired. When not used, the
1378 * updates default to being ordered by refname.
1380 static int ref_transaction_update_reflog(struct ref_transaction
*transaction
,
1381 const char *refname
,
1382 const struct object_id
*new_oid
,
1383 const struct object_id
*old_oid
,
1384 const char *committer_info
,
1390 struct ref_update
*update
;
1394 flags
|= REF_LOG_ONLY
| REF_FORCE_CREATE_REFLOG
| REF_NO_DEREF
;
1396 if (!transaction_refname_valid(refname
, new_oid
, flags
, err
))
1399 update
= ref_transaction_add_update(transaction
, refname
, flags
,
1400 new_oid
, old_oid
, NULL
, NULL
,
1401 committer_info
, msg
);
1403 * While we do set the old_oid value, we unset the flag to skip
1404 * old_oid verification which only makes sense for refs.
1406 update
->flags
&= ~REF_HAVE_OLD
;
1407 update
->index
= index
;
1410 * Reference backends may need to know the max index to optimize
1411 * their writes. So we store the max_index on the transaction level.
1413 if (index
> transaction
->max_index
)
1414 transaction
->max_index
= index
;
1419 int ref_transaction_create(struct ref_transaction
*transaction
,
1420 const char *refname
,
1421 const struct object_id
*new_oid
,
1422 const char *new_target
,
1423 unsigned int flags
, const char *msg
,
1426 if (new_oid
&& new_target
)
1427 BUG("create called with both new_oid and new_target set");
1428 if ((!new_oid
|| is_null_oid(new_oid
)) && !new_target
) {
1429 strbuf_addf(err
, "'%s' has neither a valid OID nor a target", refname
);
1432 return ref_transaction_update(transaction
, refname
, new_oid
,
1433 null_oid(the_hash_algo
), new_target
, NULL
, flags
,
1437 int ref_transaction_delete(struct ref_transaction
*transaction
,
1438 const char *refname
,
1439 const struct object_id
*old_oid
,
1440 const char *old_target
,
1445 if (old_oid
&& is_null_oid(old_oid
))
1446 BUG("delete called with old_oid set to zeros");
1447 if (old_oid
&& old_target
)
1448 BUG("delete called with both old_oid and old_target set");
1449 if (old_target
&& !(flags
& REF_NO_DEREF
))
1450 BUG("delete cannot operate on symrefs with deref mode");
1451 return ref_transaction_update(transaction
, refname
,
1452 null_oid(the_hash_algo
), old_oid
,
1453 NULL
, old_target
, flags
,
1457 int ref_transaction_verify(struct ref_transaction
*transaction
,
1458 const char *refname
,
1459 const struct object_id
*old_oid
,
1460 const char *old_target
,
1464 if (!old_target
&& !old_oid
)
1465 BUG("verify called with old_oid and old_target set to NULL");
1466 if (old_oid
&& old_target
)
1467 BUG("verify called with both old_oid and old_target set");
1468 if (old_target
&& !(flags
& REF_NO_DEREF
))
1469 BUG("verify cannot operate on symrefs with deref mode");
1470 return ref_transaction_update(transaction
, refname
,
1476 int refs_update_ref(struct ref_store
*refs
, const char *msg
,
1477 const char *refname
, const struct object_id
*new_oid
,
1478 const struct object_id
*old_oid
, unsigned int flags
,
1479 enum action_on_err onerr
)
1481 struct ref_transaction
*t
= NULL
;
1482 struct strbuf err
= STRBUF_INIT
;
1485 t
= ref_store_transaction_begin(refs
, 0, &err
);
1487 ref_transaction_update(t
, refname
, new_oid
, old_oid
, NULL
, NULL
,
1488 flags
, msg
, &err
) ||
1489 ref_transaction_commit(t
, &err
)) {
1491 ref_transaction_free(t
);
1494 const char *str
= _("update_ref failed for ref '%s': %s");
1497 case UPDATE_REFS_MSG_ON_ERR
:
1498 error(str
, refname
, err
.buf
);
1500 case UPDATE_REFS_DIE_ON_ERR
:
1501 die(str
, refname
, err
.buf
);
1503 case UPDATE_REFS_QUIET_ON_ERR
:
1506 strbuf_release(&err
);
1509 strbuf_release(&err
);
1511 ref_transaction_free(t
);
1516 * Check that the string refname matches a rule of the form
1517 * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule
1518 * "foo/%.*s/baz", and return the string "bar".
1520 static const char *match_parse_rule(const char *refname
, const char *rule
,
1524 * Check that rule matches refname up to the first percent in the rule.
1525 * We can bail immediately if not, but otherwise we leave "rule" at the
1526 * %-placeholder, and "refname" at the start of the potential matched
1529 while (*rule
!= '%') {
1531 BUG("rev-parse rule did not have percent");
1532 if (*refname
++ != *rule
++)
1537 * Check that our "%" is the expected placeholder. This assumes there
1538 * are no other percents (placeholder or quoted) in the string, but
1539 * that is sufficient for our rev-parse rules.
1541 if (!skip_prefix(rule
, "%.*s", &rule
))
1545 * And now check that our suffix (if any) matches.
1547 if (!strip_suffix(refname
, rule
, len
))
1550 return refname
; /* len set by strip_suffix() */
1553 char *refs_shorten_unambiguous_ref(struct ref_store
*refs
,
1554 const char *refname
, int strict
)
1557 struct strbuf resolved_buf
= STRBUF_INIT
;
1559 /* skip first rule, it will always match */
1560 for (i
= NUM_REV_PARSE_RULES
- 1; i
> 0 ; --i
) {
1562 int rules_to_fail
= i
;
1563 const char *short_name
;
1564 size_t short_name_len
;
1566 short_name
= match_parse_rule(refname
, ref_rev_parse_rules
[i
],
1572 * in strict mode, all (except the matched one) rules
1573 * must fail to resolve to a valid non-ambiguous ref
1576 rules_to_fail
= NUM_REV_PARSE_RULES
;
1579 * check if the short name resolves to a valid ref,
1580 * but use only rules prior to the matched one
1582 for (j
= 0; j
< rules_to_fail
; j
++) {
1583 const char *rule
= ref_rev_parse_rules
[j
];
1585 /* skip matched rule */
1590 * the short name is ambiguous, if it resolves
1591 * (with this previous rule) to a valid ref
1592 * read_ref() returns 0 on success
1594 strbuf_reset(&resolved_buf
);
1595 strbuf_addf(&resolved_buf
, rule
,
1596 cast_size_t_to_int(short_name_len
),
1598 if (refs_ref_exists(refs
, resolved_buf
.buf
))
1603 * short name is non-ambiguous if all previous rules
1604 * haven't resolved to a valid ref
1606 if (j
== rules_to_fail
) {
1607 strbuf_release(&resolved_buf
);
1608 return xmemdupz(short_name
, short_name_len
);
1612 strbuf_release(&resolved_buf
);
1613 return xstrdup(refname
);
1616 int parse_hide_refs_config(const char *var
, const char *value
, const char *section
,
1617 struct strvec
*hide_refs
)
1620 if (!strcmp("transfer.hiderefs", var
) ||
1621 (!parse_config_key(var
, section
, NULL
, NULL
, &key
) &&
1622 !strcmp(key
, "hiderefs"))) {
1627 return config_error_nonbool(var
);
1629 /* drop const to remove trailing '/' characters */
1630 ref
= (char *)strvec_push(hide_refs
, value
);
1632 while (len
&& ref
[len
- 1] == '/')
1638 int ref_is_hidden(const char *refname
, const char *refname_full
,
1639 const struct strvec
*hide_refs
)
1643 for (i
= hide_refs
->nr
- 1; i
>= 0; i
--) {
1644 const char *match
= hide_refs
->v
[i
];
1645 const char *subject
;
1649 if (*match
== '!') {
1654 if (*match
== '^') {
1655 subject
= refname_full
;
1661 /* refname can be NULL when namespaces are used. */
1663 skip_prefix(subject
, match
, &p
) &&
1670 const char **hidden_refs_to_excludes(const struct strvec
*hide_refs
)
1672 const char **pattern
;
1673 for (pattern
= hide_refs
->v
; *pattern
; pattern
++) {
1675 * We can't feed any excludes from hidden refs config
1676 * sections, since later rules may override previous
1677 * ones. For example, with rules "refs/foo" and
1678 * "!refs/foo/bar", we should show "refs/foo/bar" (and
1679 * everything underneath it), but the earlier exclusion
1680 * would cause us to skip all of "refs/foo". We
1681 * likewise don't implement the namespace stripping
1682 * required for '^' rules.
1684 * Both are possible to do, but complicated, so avoid
1685 * populating the jump list at all if we see either of
1688 if (**pattern
== '!' || **pattern
== '^')
1691 return hide_refs
->v
;
1694 const char **get_namespaced_exclude_patterns(const char **exclude_patterns
,
1695 const char *namespace,
1698 if (!namespace || !*namespace || !exclude_patterns
|| !*exclude_patterns
)
1699 return exclude_patterns
;
1701 for (size_t i
= 0; exclude_patterns
[i
]; i
++)
1702 strvec_pushf(out
, "%s%s", namespace, exclude_patterns
[i
]);
1707 const char *find_descendant_ref(const char *dirname
,
1708 const struct string_list
*extras
,
1709 const struct string_list
*skip
)
1717 * Look at the place where dirname would be inserted into
1718 * extras. If there is an entry at that position that starts
1719 * with dirname (remember, dirname includes the trailing
1720 * slash) and is not in skip, then we have a conflict.
1722 for (pos
= string_list_find_insert_index(extras
, dirname
, 0);
1723 pos
< extras
->nr
; pos
++) {
1724 const char *extra_refname
= extras
->items
[pos
].string
;
1726 if (!starts_with(extra_refname
, dirname
))
1729 if (!skip
|| !string_list_has_string(skip
, extra_refname
))
1730 return extra_refname
;
1735 int refs_head_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1737 struct object_id oid
;
1740 if (refs_resolve_ref_unsafe(refs
, "HEAD", RESOLVE_REF_READING
,
1742 return fn("HEAD", NULL
, &oid
, flag
, cb_data
);
1747 struct ref_iterator
*refs_ref_iterator_begin(
1748 struct ref_store
*refs
,
1750 const char **exclude_patterns
,
1752 enum do_for_each_ref_flags flags
)
1754 struct ref_iterator
*iter
;
1755 struct strvec normalized_exclude_patterns
= STRVEC_INIT
;
1757 if (exclude_patterns
) {
1758 for (size_t i
= 0; exclude_patterns
[i
]; i
++) {
1759 const char *pattern
= exclude_patterns
[i
];
1760 size_t len
= strlen(pattern
);
1764 if (pattern
[len
- 1] == '/')
1765 strvec_push(&normalized_exclude_patterns
, pattern
);
1767 strvec_pushf(&normalized_exclude_patterns
, "%s/",
1771 exclude_patterns
= normalized_exclude_patterns
.v
;
1774 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
)) {
1775 static int ref_paranoia
= -1;
1777 if (ref_paranoia
< 0)
1778 ref_paranoia
= git_env_bool("GIT_REF_PARANOIA", 1);
1780 flags
|= DO_FOR_EACH_INCLUDE_BROKEN
;
1781 flags
|= DO_FOR_EACH_OMIT_DANGLING_SYMREFS
;
1785 iter
= refs
->be
->iterator_begin(refs
, prefix
, exclude_patterns
, flags
);
1787 * `iterator_begin()` already takes care of prefix, but we
1788 * might need to do some trimming:
1791 iter
= prefix_ref_iterator_begin(iter
, "", trim
);
1793 strvec_clear(&normalized_exclude_patterns
);
1798 static int do_for_each_ref(struct ref_store
*refs
, const char *prefix
,
1799 const char **exclude_patterns
,
1800 each_ref_fn fn
, int trim
,
1801 enum do_for_each_ref_flags flags
, void *cb_data
)
1803 struct ref_iterator
*iter
;
1808 iter
= refs_ref_iterator_begin(refs
, prefix
, exclude_patterns
, trim
,
1811 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1814 int refs_for_each_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1816 return do_for_each_ref(refs
, "", NULL
, fn
, 0, 0, cb_data
);
1819 int refs_for_each_ref_in(struct ref_store
*refs
, const char *prefix
,
1820 each_ref_fn fn
, void *cb_data
)
1822 return do_for_each_ref(refs
, prefix
, NULL
, fn
, strlen(prefix
), 0, cb_data
);
1825 int refs_for_each_fullref_in(struct ref_store
*refs
, const char *prefix
,
1826 const char **exclude_patterns
,
1827 each_ref_fn fn
, void *cb_data
)
1829 return do_for_each_ref(refs
, prefix
, exclude_patterns
, fn
, 0, 0, cb_data
);
1832 int refs_for_each_replace_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1834 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
1835 return do_for_each_ref(refs
, git_replace_ref_base
, NULL
, fn
,
1836 strlen(git_replace_ref_base
),
1837 DO_FOR_EACH_INCLUDE_BROKEN
, cb_data
);
1840 int refs_for_each_namespaced_ref(struct ref_store
*refs
,
1841 const char **exclude_patterns
,
1842 each_ref_fn fn
, void *cb_data
)
1844 struct strvec namespaced_exclude_patterns
= STRVEC_INIT
;
1845 struct strbuf prefix
= STRBUF_INIT
;
1848 exclude_patterns
= get_namespaced_exclude_patterns(exclude_patterns
,
1849 get_git_namespace(),
1850 &namespaced_exclude_patterns
);
1852 strbuf_addf(&prefix
, "%srefs/", get_git_namespace());
1853 ret
= do_for_each_ref(refs
, prefix
.buf
, exclude_patterns
, fn
, 0, 0, cb_data
);
1855 strvec_clear(&namespaced_exclude_patterns
);
1856 strbuf_release(&prefix
);
1860 int refs_for_each_rawref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1862 return do_for_each_ref(refs
, "", NULL
, fn
, 0,
1863 DO_FOR_EACH_INCLUDE_BROKEN
, cb_data
);
1866 int refs_for_each_include_root_refs(struct ref_store
*refs
, each_ref_fn fn
,
1869 return do_for_each_ref(refs
, "", NULL
, fn
, 0,
1870 DO_FOR_EACH_INCLUDE_ROOT_REFS
, cb_data
);
1873 static int qsort_strcmp(const void *va
, const void *vb
)
1875 const char *a
= *(const char **)va
;
1876 const char *b
= *(const char **)vb
;
1878 return strcmp(a
, b
);
1881 static void find_longest_prefixes_1(struct string_list
*out
,
1882 struct strbuf
*prefix
,
1883 const char **patterns
, size_t nr
)
1887 for (i
= 0; i
< nr
; i
++) {
1888 char c
= patterns
[i
][prefix
->len
];
1889 if (!c
|| is_glob_special(c
)) {
1890 string_list_append(out
, prefix
->buf
);
1900 * Set "end" to the index of the element _after_ the last one
1903 for (end
= i
+ 1; end
< nr
; end
++) {
1904 if (patterns
[i
][prefix
->len
] != patterns
[end
][prefix
->len
])
1908 strbuf_addch(prefix
, patterns
[i
][prefix
->len
]);
1909 find_longest_prefixes_1(out
, prefix
, patterns
+ i
, end
- i
);
1910 strbuf_setlen(prefix
, prefix
->len
- 1);
1916 static void find_longest_prefixes(struct string_list
*out
,
1917 const char **patterns
)
1919 struct strvec sorted
= STRVEC_INIT
;
1920 struct strbuf prefix
= STRBUF_INIT
;
1922 strvec_pushv(&sorted
, patterns
);
1923 QSORT(sorted
.v
, sorted
.nr
, qsort_strcmp
);
1925 find_longest_prefixes_1(out
, &prefix
, sorted
.v
, sorted
.nr
);
1927 strvec_clear(&sorted
);
1928 strbuf_release(&prefix
);
1931 int refs_for_each_fullref_in_prefixes(struct ref_store
*ref_store
,
1932 const char *namespace,
1933 const char **patterns
,
1934 const char **exclude_patterns
,
1935 each_ref_fn fn
, void *cb_data
)
1937 struct strvec namespaced_exclude_patterns
= STRVEC_INIT
;
1938 struct string_list prefixes
= STRING_LIST_INIT_DUP
;
1939 struct string_list_item
*prefix
;
1940 struct strbuf buf
= STRBUF_INIT
;
1941 int ret
= 0, namespace_len
;
1943 find_longest_prefixes(&prefixes
, patterns
);
1946 strbuf_addstr(&buf
, namespace);
1947 namespace_len
= buf
.len
;
1949 exclude_patterns
= get_namespaced_exclude_patterns(exclude_patterns
,
1951 &namespaced_exclude_patterns
);
1953 for_each_string_list_item(prefix
, &prefixes
) {
1954 strbuf_addstr(&buf
, prefix
->string
);
1955 ret
= refs_for_each_fullref_in(ref_store
, buf
.buf
,
1956 exclude_patterns
, fn
, cb_data
);
1959 strbuf_setlen(&buf
, namespace_len
);
1962 strvec_clear(&namespaced_exclude_patterns
);
1963 string_list_clear(&prefixes
, 0);
1964 strbuf_release(&buf
);
1968 static int refs_read_special_head(struct ref_store
*ref_store
,
1969 const char *refname
, struct object_id
*oid
,
1970 struct strbuf
*referent
, unsigned int *type
,
1973 struct strbuf full_path
= STRBUF_INIT
;
1974 struct strbuf content
= STRBUF_INIT
;
1976 strbuf_addf(&full_path
, "%s/%s", ref_store
->gitdir
, refname
);
1978 if (strbuf_read_file(&content
, full_path
.buf
, 0) < 0) {
1979 *failure_errno
= errno
;
1983 result
= parse_loose_ref_contents(ref_store
->repo
->hash_algo
, content
.buf
,
1984 oid
, referent
, type
, NULL
, failure_errno
);
1987 strbuf_release(&full_path
);
1988 strbuf_release(&content
);
1992 int refs_read_raw_ref(struct ref_store
*ref_store
, const char *refname
,
1993 struct object_id
*oid
, struct strbuf
*referent
,
1994 unsigned int *type
, int *failure_errno
)
1996 assert(failure_errno
);
1997 if (is_pseudo_ref(refname
))
1998 return refs_read_special_head(ref_store
, refname
, oid
, referent
,
1999 type
, failure_errno
);
2001 return ref_store
->be
->read_raw_ref(ref_store
, refname
, oid
, referent
,
2002 type
, failure_errno
);
2005 int refs_read_symbolic_ref(struct ref_store
*ref_store
, const char *refname
,
2006 struct strbuf
*referent
)
2008 return ref_store
->be
->read_symbolic_ref(ref_store
, refname
, referent
);
2011 const char *refs_resolve_ref_unsafe(struct ref_store
*refs
,
2012 const char *refname
,
2014 struct object_id
*oid
,
2017 static struct strbuf sb_refname
= STRBUF_INIT
;
2018 struct object_id unused_oid
;
2025 flags
= &unused_flags
;
2029 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
2030 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
2031 !refname_is_safe(refname
))
2035 * repo_dwim_ref() uses REF_ISBROKEN to distinguish between
2036 * missing refs and refs that were present but invalid,
2037 * to complain about the latter to stderr.
2039 * We don't know whether the ref exists, so don't set
2042 *flags
|= REF_BAD_NAME
;
2045 for (symref_count
= 0; symref_count
< SYMREF_MAXDEPTH
; symref_count
++) {
2046 unsigned int read_flags
= 0;
2049 if (refs_read_raw_ref(refs
, refname
, oid
, &sb_refname
,
2050 &read_flags
, &failure_errno
)) {
2051 *flags
|= read_flags
;
2053 /* In reading mode, refs must eventually resolve */
2054 if (resolve_flags
& RESOLVE_REF_READING
)
2058 * Otherwise a missing ref is OK. But the files backend
2059 * may show errors besides ENOENT if there are
2060 * similarly-named refs.
2062 if (failure_errno
!= ENOENT
&&
2063 failure_errno
!= EISDIR
&&
2064 failure_errno
!= ENOTDIR
)
2067 oidclr(oid
, refs
->repo
->hash_algo
);
2068 if (*flags
& REF_BAD_NAME
)
2069 *flags
|= REF_ISBROKEN
;
2073 *flags
|= read_flags
;
2075 if (!(read_flags
& REF_ISSYMREF
)) {
2076 if (*flags
& REF_BAD_NAME
) {
2077 oidclr(oid
, refs
->repo
->hash_algo
);
2078 *flags
|= REF_ISBROKEN
;
2083 refname
= sb_refname
.buf
;
2084 if (resolve_flags
& RESOLVE_REF_NO_RECURSE
) {
2085 oidclr(oid
, refs
->repo
->hash_algo
);
2088 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
2089 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
2090 !refname_is_safe(refname
))
2093 *flags
|= REF_ISBROKEN
| REF_BAD_NAME
;
2100 /* backend functions */
2101 int ref_store_create_on_disk(struct ref_store
*refs
, int flags
, struct strbuf
*err
)
2103 return refs
->be
->create_on_disk(refs
, flags
, err
);
2106 int ref_store_remove_on_disk(struct ref_store
*refs
, struct strbuf
*err
)
2108 return refs
->be
->remove_on_disk(refs
, err
);
2111 int repo_resolve_gitlink_ref(struct repository
*r
,
2112 const char *submodule
, const char *refname
,
2113 struct object_id
*oid
)
2115 struct ref_store
*refs
;
2118 refs
= repo_get_submodule_ref_store(r
, submodule
);
2122 if (!refs_resolve_ref_unsafe(refs
, refname
, 0, oid
, &flags
) ||
2129 * Look up a ref store by name. If that ref_store hasn't been
2130 * registered yet, return NULL.
2132 static struct ref_store
*lookup_ref_store_map(struct strmap
*map
,
2135 struct strmap_entry
*entry
;
2137 if (!map
->map
.tablesize
)
2138 /* It's initialized on demand in register_ref_store(). */
2141 entry
= strmap_get_entry(map
, name
);
2142 return entry
? entry
->value
: NULL
;
2146 * Create, record, and return a ref_store instance for the specified
2147 * gitdir using the given ref storage format.
2149 static struct ref_store
*ref_store_init(struct repository
*repo
,
2150 enum ref_storage_format format
,
2154 const struct ref_storage_be
*be
;
2155 struct ref_store
*refs
;
2157 be
= find_ref_storage_backend(format
);
2159 BUG("reference backend is unknown");
2161 refs
= be
->init(repo
, gitdir
, flags
);
2165 void ref_store_release(struct ref_store
*ref_store
)
2167 ref_store
->be
->release(ref_store
);
2168 free(ref_store
->gitdir
);
2171 struct ref_store
*get_main_ref_store(struct repository
*r
)
2173 if (r
->refs_private
)
2174 return r
->refs_private
;
2177 BUG("attempting to get main_ref_store outside of repository");
2179 r
->refs_private
= ref_store_init(r
, r
->ref_storage_format
,
2180 r
->gitdir
, REF_STORE_ALL_CAPS
);
2181 r
->refs_private
= maybe_debug_wrap_ref_store(r
->gitdir
, r
->refs_private
);
2182 return r
->refs_private
;
2186 * Associate a ref store with a name. It is a fatal error to call this
2187 * function twice for the same name.
2189 static void register_ref_store_map(struct strmap
*map
,
2191 struct ref_store
*refs
,
2194 if (!map
->map
.tablesize
)
2196 if (strmap_put(map
, name
, refs
))
2197 BUG("%s ref_store '%s' initialized twice", type
, name
);
2200 struct ref_store
*repo_get_submodule_ref_store(struct repository
*repo
,
2201 const char *submodule
)
2203 struct strbuf submodule_sb
= STRBUF_INIT
;
2204 struct ref_store
*refs
;
2205 char *to_free
= NULL
;
2207 struct repository
*subrepo
;
2212 len
= strlen(submodule
);
2213 while (len
&& is_dir_sep(submodule
[len
- 1]))
2219 /* We need to strip off one or more trailing slashes */
2220 submodule
= to_free
= xmemdupz(submodule
, len
);
2222 refs
= lookup_ref_store_map(&repo
->submodule_ref_stores
, submodule
);
2226 strbuf_addstr(&submodule_sb
, submodule
);
2227 if (!is_nonbare_repository_dir(&submodule_sb
))
2230 if (submodule_to_gitdir(repo
, &submodule_sb
, submodule
))
2233 subrepo
= xmalloc(sizeof(*subrepo
));
2235 if (repo_submodule_init(subrepo
, repo
, submodule
,
2236 null_oid(the_hash_algo
))) {
2240 refs
= ref_store_init(subrepo
, subrepo
->ref_storage_format
,
2242 REF_STORE_READ
| REF_STORE_ODB
);
2243 register_ref_store_map(&repo
->submodule_ref_stores
, "submodule",
2247 strbuf_release(&submodule_sb
);
2253 struct ref_store
*get_worktree_ref_store(const struct worktree
*wt
)
2255 struct ref_store
*refs
;
2259 return get_main_ref_store(wt
->repo
);
2261 id
= wt
->id
? wt
->id
: "/";
2262 refs
= lookup_ref_store_map(&wt
->repo
->worktree_ref_stores
, id
);
2267 struct strbuf common_path
= STRBUF_INIT
;
2268 repo_common_path_append(wt
->repo
, &common_path
,
2269 "worktrees/%s", wt
->id
);
2270 refs
= ref_store_init(wt
->repo
, wt
->repo
->ref_storage_format
,
2271 common_path
.buf
, REF_STORE_ALL_CAPS
);
2272 strbuf_release(&common_path
);
2274 refs
= ref_store_init(wt
->repo
, wt
->repo
->ref_storage_format
,
2275 wt
->repo
->commondir
, REF_STORE_ALL_CAPS
);
2279 register_ref_store_map(&wt
->repo
->worktree_ref_stores
,
2280 "worktree", refs
, id
);
2285 void base_ref_store_init(struct ref_store
*refs
, struct repository
*repo
,
2286 const char *path
, const struct ref_storage_be
*be
)
2290 refs
->gitdir
= xstrdup(path
);
2293 /* backend functions */
2294 int refs_pack_refs(struct ref_store
*refs
, struct pack_refs_opts
*opts
)
2296 return refs
->be
->pack_refs(refs
, opts
);
2299 int peel_iterated_oid(struct repository
*r
, const struct object_id
*base
, struct object_id
*peeled
)
2301 if (current_ref_iter
&&
2302 (current_ref_iter
->oid
== base
||
2303 oideq(current_ref_iter
->oid
, base
)))
2304 return ref_iterator_peel(current_ref_iter
, peeled
);
2306 return peel_object(r
, base
, peeled
) ? -1 : 0;
2309 int refs_update_symref(struct ref_store
*refs
, const char *ref
,
2310 const char *target
, const char *logmsg
)
2312 return refs_update_symref_extended(refs
, ref
, target
, logmsg
, NULL
, 0);
2315 int refs_update_symref_extended(struct ref_store
*refs
, const char *ref
,
2316 const char *target
, const char *logmsg
,
2317 struct strbuf
*referent
, int create_only
)
2319 struct ref_transaction
*transaction
;
2320 struct strbuf err
= STRBUF_INIT
;
2321 int ret
= 0, prepret
= 0;
2323 transaction
= ref_store_transaction_begin(refs
, 0, &err
);
2326 ret
= error("%s", err
.buf
);
2330 if (ref_transaction_create(transaction
, ref
, NULL
, target
,
2331 REF_NO_DEREF
, logmsg
, &err
))
2333 prepret
= ref_transaction_prepare(transaction
, &err
);
2334 if (prepret
&& prepret
!= REF_TRANSACTION_ERROR_CREATE_EXISTS
)
2337 if (ref_transaction_update(transaction
, ref
, NULL
, NULL
,
2338 target
, NULL
, REF_NO_DEREF
,
2340 ref_transaction_prepare(transaction
, &err
))
2344 if (referent
&& refs_read_symbolic_ref(refs
, ref
, referent
) == NOT_A_SYMREF
) {
2345 struct object_id oid
;
2346 if (!refs_read_ref(refs
, ref
, &oid
)) {
2347 strbuf_addstr(referent
, oid_to_hex(&oid
));
2352 if (prepret
== REF_TRANSACTION_ERROR_CREATE_EXISTS
)
2355 if (ref_transaction_commit(transaction
, &err
))
2359 strbuf_release(&err
);
2361 ref_transaction_free(transaction
);
2367 * Write an error to `err` and return a nonzero value iff the same
2368 * refname appears multiple times in `refnames`. `refnames` must be
2369 * sorted on entry to this function.
2371 static int ref_update_reject_duplicates(struct string_list
*refnames
,
2374 size_t i
, n
= refnames
->nr
;
2378 for (i
= 1; i
< n
; i
++) {
2379 int cmp
= strcmp(refnames
->items
[i
- 1].string
,
2380 refnames
->items
[i
].string
);
2384 _("multiple updates for ref '%s' not allowed"),
2385 refnames
->items
[i
].string
);
2387 } else if (cmp
> 0) {
2388 BUG("ref_update_reject_duplicates() received unsorted list");
2394 static int run_transaction_hook(struct ref_transaction
*transaction
,
2397 struct child_process proc
= CHILD_PROCESS_INIT
;
2398 struct strbuf buf
= STRBUF_INIT
;
2402 hook
= find_hook(transaction
->ref_store
->repo
, "reference-transaction");
2406 strvec_pushl(&proc
.args
, hook
, state
, NULL
);
2408 proc
.stdout_to_stderr
= 1;
2409 proc
.trace2_hook_name
= "reference-transaction";
2411 ret
= start_command(&proc
);
2415 sigchain_push(SIGPIPE
, SIG_IGN
);
2417 for (i
= 0; i
< transaction
->nr
; i
++) {
2418 struct ref_update
*update
= transaction
->updates
[i
];
2420 if (update
->flags
& REF_LOG_ONLY
)
2425 if (!(update
->flags
& REF_HAVE_OLD
))
2426 strbuf_addf(&buf
, "%s ", oid_to_hex(null_oid(the_hash_algo
)));
2427 else if (update
->old_target
)
2428 strbuf_addf(&buf
, "ref:%s ", update
->old_target
);
2430 strbuf_addf(&buf
, "%s ", oid_to_hex(&update
->old_oid
));
2432 if (!(update
->flags
& REF_HAVE_NEW
))
2433 strbuf_addf(&buf
, "%s ", oid_to_hex(null_oid(the_hash_algo
)));
2434 else if (update
->new_target
)
2435 strbuf_addf(&buf
, "ref:%s ", update
->new_target
);
2437 strbuf_addf(&buf
, "%s ", oid_to_hex(&update
->new_oid
));
2439 strbuf_addf(&buf
, "%s\n", update
->refname
);
2441 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
2442 if (errno
!= EPIPE
) {
2443 /* Don't leak errno outside this API */
2452 sigchain_pop(SIGPIPE
);
2453 strbuf_release(&buf
);
2455 ret
|= finish_command(&proc
);
2459 int ref_transaction_prepare(struct ref_transaction
*transaction
,
2462 struct ref_store
*refs
= transaction
->ref_store
;
2465 switch (transaction
->state
) {
2466 case REF_TRANSACTION_OPEN
:
2469 case REF_TRANSACTION_PREPARED
:
2470 BUG("prepare called twice on reference transaction");
2472 case REF_TRANSACTION_CLOSED
:
2473 BUG("prepare called on a closed reference transaction");
2476 BUG("unexpected reference transaction state");
2480 if (refs
->repo
->objects
->odb
->disable_ref_updates
) {
2482 _("ref updates forbidden inside quarantine environment"));
2486 string_list_sort(&transaction
->refnames
);
2487 if (ref_update_reject_duplicates(&transaction
->refnames
, err
))
2488 return REF_TRANSACTION_ERROR_GENERIC
;
2490 ret
= refs
->be
->transaction_prepare(refs
, transaction
, err
);
2494 ret
= run_transaction_hook(transaction
, "prepared");
2496 ref_transaction_abort(transaction
, err
);
2497 die(_("ref updates aborted by hook"));
2503 int ref_transaction_abort(struct ref_transaction
*transaction
,
2506 struct ref_store
*refs
= transaction
->ref_store
;
2509 switch (transaction
->state
) {
2510 case REF_TRANSACTION_OPEN
:
2511 /* No need to abort explicitly. */
2513 case REF_TRANSACTION_PREPARED
:
2514 ret
= refs
->be
->transaction_abort(refs
, transaction
, err
);
2516 case REF_TRANSACTION_CLOSED
:
2517 BUG("abort called on a closed reference transaction");
2520 BUG("unexpected reference transaction state");
2524 run_transaction_hook(transaction
, "aborted");
2526 ref_transaction_free(transaction
);
2530 int ref_transaction_commit(struct ref_transaction
*transaction
,
2533 struct ref_store
*refs
= transaction
->ref_store
;
2536 switch (transaction
->state
) {
2537 case REF_TRANSACTION_OPEN
:
2538 /* Need to prepare first. */
2539 ret
= ref_transaction_prepare(transaction
, err
);
2543 case REF_TRANSACTION_PREPARED
:
2544 /* Fall through to finish. */
2546 case REF_TRANSACTION_CLOSED
:
2547 BUG("commit called on a closed reference transaction");
2550 BUG("unexpected reference transaction state");
2554 ret
= refs
->be
->transaction_finish(refs
, transaction
, err
);
2555 if (!ret
&& !(transaction
->flags
& REF_TRANSACTION_FLAG_INITIAL
))
2556 run_transaction_hook(transaction
, "committed");
2560 enum ref_transaction_error
refs_verify_refnames_available(struct ref_store
*refs
,
2561 const struct string_list
*refnames
,
2562 const struct string_list
*extras
,
2563 const struct string_list
*skip
,
2564 struct ref_transaction
*transaction
,
2565 unsigned int initial_transaction
,
2568 struct strbuf dirname
= STRBUF_INIT
;
2569 struct strbuf referent
= STRBUF_INIT
;
2570 struct string_list_item
*item
;
2571 struct ref_iterator
*iter
= NULL
;
2572 struct strset conflicting_dirnames
;
2573 struct strset dirnames
;
2574 int ret
= REF_TRANSACTION_ERROR_NAME_CONFLICT
;
2577 * For the sake of comments in this function, suppose that
2578 * refname is "refs/foo/bar".
2583 strset_init(&conflicting_dirnames
);
2584 strset_init(&dirnames
);
2586 for_each_string_list_item(item
, refnames
) {
2587 const size_t *update_idx
= (size_t *)item
->util
;
2588 const char *refname
= item
->string
;
2589 const char *extra_refname
;
2590 struct object_id oid
;
2594 strbuf_reset(&dirname
);
2596 for (slash
= strchr(refname
, '/'); slash
; slash
= strchr(slash
+ 1, '/')) {
2598 * Just saying "Is a directory" when we e.g. can't
2599 * lock some multi-level ref isn't very informative,
2600 * the user won't be told *what* is a directory, so
2601 * let's not use strerror() below.
2605 /* Expand dirname to the new prefix, not including the trailing slash: */
2606 strbuf_add(&dirname
, refname
+ dirname
.len
, slash
- refname
- dirname
.len
);
2609 * We are still at a leading dir of the refname (e.g.,
2610 * "refs/foo"; if there is a reference with that name,
2611 * it is a conflict, *unless* it is in skip.
2613 if (skip
&& string_list_has_string(skip
, dirname
.buf
))
2617 * If we've already seen the directory we don't need to
2618 * process it again. Skip it to avoid checking common
2619 * prefixes like "refs/heads/" repeatedly.
2621 if (!strset_add(&dirnames
, dirname
.buf
))
2624 if (!initial_transaction
&&
2625 (strset_contains(&conflicting_dirnames
, dirname
.buf
) ||
2626 !refs_read_raw_ref(refs
, dirname
.buf
, &oid
, &referent
,
2627 &type
, &ignore_errno
))) {
2628 if (transaction
&& ref_transaction_maybe_set_rejected(
2629 transaction
, *update_idx
,
2630 REF_TRANSACTION_ERROR_NAME_CONFLICT
)) {
2631 strset_remove(&dirnames
, dirname
.buf
);
2632 strset_add(&conflicting_dirnames
, dirname
.buf
);
2636 strbuf_addf(err
, _("'%s' exists; cannot create '%s'"),
2637 dirname
.buf
, refname
);
2641 if (extras
&& string_list_has_string(extras
, dirname
.buf
)) {
2642 if (transaction
&& ref_transaction_maybe_set_rejected(
2643 transaction
, *update_idx
,
2644 REF_TRANSACTION_ERROR_NAME_CONFLICT
)) {
2645 strset_remove(&dirnames
, dirname
.buf
);
2649 strbuf_addf(err
, _("cannot process '%s' and '%s' at the same time"),
2650 refname
, dirname
.buf
);
2656 * We are at the leaf of our refname (e.g., "refs/foo/bar").
2657 * There is no point in searching for a reference with that
2658 * name, because a refname isn't considered to conflict with
2659 * itself. But we still need to check for references whose
2660 * names are in the "refs/foo/bar/" namespace, because they
2663 strbuf_addstr(&dirname
, refname
+ dirname
.len
);
2664 strbuf_addch(&dirname
, '/');
2666 if (!initial_transaction
) {
2670 iter
= refs_ref_iterator_begin(refs
, dirname
.buf
, NULL
, 0,
2671 DO_FOR_EACH_INCLUDE_BROKEN
);
2672 } else if (ref_iterator_seek(iter
, dirname
.buf
) < 0) {
2676 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
2678 string_list_has_string(skip
, iter
->refname
))
2681 if (transaction
&& ref_transaction_maybe_set_rejected(
2682 transaction
, *update_idx
,
2683 REF_TRANSACTION_ERROR_NAME_CONFLICT
))
2686 strbuf_addf(err
, _("'%s' exists; cannot create '%s'"),
2687 iter
->refname
, refname
);
2691 if (ok
!= ITER_DONE
)
2692 BUG("error while iterating over references");
2695 extra_refname
= find_descendant_ref(dirname
.buf
, extras
, skip
);
2696 if (extra_refname
) {
2697 if (transaction
&& ref_transaction_maybe_set_rejected(
2698 transaction
, *update_idx
,
2699 REF_TRANSACTION_ERROR_NAME_CONFLICT
))
2702 strbuf_addf(err
, _("cannot process '%s' and '%s' at the same time"),
2703 refname
, extra_refname
);
2711 strbuf_release(&referent
);
2712 strbuf_release(&dirname
);
2713 strset_clear(&conflicting_dirnames
);
2714 strset_clear(&dirnames
);
2715 ref_iterator_free(iter
);
2719 enum ref_transaction_error
refs_verify_refname_available(
2720 struct ref_store
*refs
,
2721 const char *refname
,
2722 const struct string_list
*extras
,
2723 const struct string_list
*skip
,
2724 unsigned int initial_transaction
,
2727 struct string_list_item item
= { .string
= (char *) refname
};
2728 struct string_list refnames
= {
2733 return refs_verify_refnames_available(refs
, &refnames
, extras
, skip
,
2734 NULL
, initial_transaction
, err
);
2737 struct do_for_each_reflog_help
{
2742 static int do_for_each_reflog_helper(const char *refname
,
2743 const char *referent UNUSED
,
2744 const struct object_id
*oid UNUSED
,
2748 struct do_for_each_reflog_help
*hp
= cb_data
;
2749 return hp
->fn(refname
, hp
->cb_data
);
2752 int refs_for_each_reflog(struct ref_store
*refs
, each_reflog_fn fn
, void *cb_data
)
2754 struct ref_iterator
*iter
;
2755 struct do_for_each_reflog_help hp
= { fn
, cb_data
};
2757 iter
= refs
->be
->reflog_iterator_begin(refs
);
2759 return do_for_each_ref_iterator(iter
, do_for_each_reflog_helper
, &hp
);
2762 int refs_for_each_reflog_ent_reverse(struct ref_store
*refs
,
2763 const char *refname
,
2764 each_reflog_ent_fn fn
,
2767 return refs
->be
->for_each_reflog_ent_reverse(refs
, refname
,
2771 int refs_for_each_reflog_ent(struct ref_store
*refs
, const char *refname
,
2772 each_reflog_ent_fn fn
, void *cb_data
)
2774 return refs
->be
->for_each_reflog_ent(refs
, refname
, fn
, cb_data
);
2777 int refs_reflog_exists(struct ref_store
*refs
, const char *refname
)
2779 return refs
->be
->reflog_exists(refs
, refname
);
2782 int refs_create_reflog(struct ref_store
*refs
, const char *refname
,
2785 return refs
->be
->create_reflog(refs
, refname
, err
);
2788 int refs_delete_reflog(struct ref_store
*refs
, const char *refname
)
2790 return refs
->be
->delete_reflog(refs
, refname
);
2793 int refs_reflog_expire(struct ref_store
*refs
,
2794 const char *refname
,
2796 reflog_expiry_prepare_fn prepare_fn
,
2797 reflog_expiry_should_prune_fn should_prune_fn
,
2798 reflog_expiry_cleanup_fn cleanup_fn
,
2799 void *policy_cb_data
)
2801 return refs
->be
->reflog_expire(refs
, refname
, flags
,
2802 prepare_fn
, should_prune_fn
,
2803 cleanup_fn
, policy_cb_data
);
2806 void ref_transaction_for_each_queued_update(struct ref_transaction
*transaction
,
2807 ref_transaction_for_each_queued_update_fn cb
,
2812 for (i
= 0; i
< transaction
->nr
; i
++) {
2813 struct ref_update
*update
= transaction
->updates
[i
];
2816 (update
->flags
& REF_HAVE_OLD
) ? &update
->old_oid
: NULL
,
2817 (update
->flags
& REF_HAVE_NEW
) ? &update
->new_oid
: NULL
,
2822 void ref_transaction_for_each_rejected_update(struct ref_transaction
*transaction
,
2823 ref_transaction_for_each_rejected_update_fn cb
,
2826 if (!transaction
->rejections
)
2829 for (size_t i
= 0; i
< transaction
->rejections
->nr
; i
++) {
2830 size_t update_index
= transaction
->rejections
->update_indices
[i
];
2831 struct ref_update
*update
= transaction
->updates
[update_index
];
2833 if (!update
->rejection_err
)
2837 (update
->flags
& REF_HAVE_OLD
) ? &update
->old_oid
: NULL
,
2838 (update
->flags
& REF_HAVE_NEW
) ? &update
->new_oid
: NULL
,
2839 update
->old_target
, update
->new_target
,
2840 update
->rejection_err
, cb_data
);
2844 int refs_delete_refs(struct ref_store
*refs
, const char *logmsg
,
2845 struct string_list
*refnames
, unsigned int flags
)
2847 struct ref_transaction
*transaction
;
2848 struct strbuf err
= STRBUF_INIT
;
2849 struct string_list_item
*item
;
2850 int ret
= 0, failures
= 0;
2856 msg
= normalize_reflog_message(logmsg
);
2859 * Since we don't check the references' old_oids, the
2860 * individual updates can't fail, so we can pack all of the
2861 * updates into a single transaction.
2863 transaction
= ref_store_transaction_begin(refs
, 0, &err
);
2865 ret
= error("%s", err
.buf
);
2869 for_each_string_list_item(item
, refnames
) {
2870 ret
= ref_transaction_delete(transaction
, item
->string
,
2871 NULL
, NULL
, flags
, msg
, &err
);
2873 warning(_("could not delete reference %s: %s"),
2874 item
->string
, err
.buf
);
2880 ret
= ref_transaction_commit(transaction
, &err
);
2882 if (refnames
->nr
== 1)
2883 error(_("could not delete reference %s: %s"),
2884 refnames
->items
[0].string
, err
.buf
);
2886 error(_("could not delete references: %s"), err
.buf
);
2890 if (!ret
&& failures
)
2892 ref_transaction_free(transaction
);
2893 strbuf_release(&err
);
2898 int refs_rename_ref(struct ref_store
*refs
, const char *oldref
,
2899 const char *newref
, const char *logmsg
)
2904 msg
= normalize_reflog_message(logmsg
);
2905 retval
= refs
->be
->rename_ref(refs
, oldref
, newref
, msg
);
2910 int refs_copy_existing_ref(struct ref_store
*refs
, const char *oldref
,
2911 const char *newref
, const char *logmsg
)
2916 msg
= normalize_reflog_message(logmsg
);
2917 retval
= refs
->be
->copy_ref(refs
, oldref
, newref
, msg
);
2922 const char *ref_update_original_update_refname(struct ref_update
*update
)
2924 while (update
->parent_update
)
2925 update
= update
->parent_update
;
2927 return update
->refname
;
2930 int ref_update_has_null_new_value(struct ref_update
*update
)
2932 return !update
->new_target
&& is_null_oid(&update
->new_oid
);
2935 enum ref_transaction_error
ref_update_check_old_target(const char *referent
,
2936 struct ref_update
*update
,
2939 if (!update
->old_target
)
2940 BUG("called without old_target set");
2942 if (!strcmp(referent
, update
->old_target
))
2945 if (!strcmp(referent
, "")) {
2946 strbuf_addf(err
, "verifying symref target: '%s': "
2947 "reference is missing but expected %s",
2948 ref_update_original_update_refname(update
),
2949 update
->old_target
);
2950 return REF_TRANSACTION_ERROR_NONEXISTENT_REF
;
2953 strbuf_addf(err
, "verifying symref target: '%s': is at %s but expected %s",
2954 ref_update_original_update_refname(update
),
2955 referent
, update
->old_target
);
2956 return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE
;
2959 struct migration_data
{
2960 struct ref_store
*old_refs
;
2961 struct ref_transaction
*transaction
;
2962 struct strbuf
*errbuf
;
2966 static int migrate_one_ref(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
2967 int flags
, void *cb_data
)
2969 struct migration_data
*data
= cb_data
;
2970 struct strbuf symref_target
= STRBUF_INIT
;
2973 if (flags
& REF_ISSYMREF
) {
2974 ret
= refs_read_symbolic_ref(data
->old_refs
, refname
, &symref_target
);
2978 ret
= ref_transaction_update(data
->transaction
, refname
, NULL
, null_oid(the_hash_algo
),
2979 symref_target
.buf
, NULL
,
2980 REF_SKIP_CREATE_REFLOG
| REF_NO_DEREF
, NULL
, data
->errbuf
);
2984 ret
= ref_transaction_create(data
->transaction
, refname
, oid
, NULL
,
2985 REF_SKIP_CREATE_REFLOG
| REF_SKIP_OID_VERIFICATION
,
2986 NULL
, data
->errbuf
);
2992 strbuf_release(&symref_target
);
2996 struct reflog_migration_data
{
2998 const char *refname
;
2999 struct ref_store
*old_refs
;
3000 struct ref_transaction
*transaction
;
3001 struct strbuf
*errbuf
;
3005 static int migrate_one_reflog_entry(struct object_id
*old_oid
,
3006 struct object_id
*new_oid
,
3007 const char *committer
,
3008 timestamp_t timestamp
, int tz
,
3009 const char *msg
, void *cb_data
)
3011 struct reflog_migration_data
*data
= cb_data
;
3015 date
= show_date(timestamp
, tz
, DATE_MODE(NORMAL
));
3016 strbuf_reset(data
->sb
);
3017 /* committer contains name and email */
3018 strbuf_addstr(data
->sb
, fmt_ident("", committer
, WANT_BLANK_IDENT
, date
, 0));
3020 ret
= ref_transaction_update_reflog(data
->transaction
, data
->refname
,
3021 new_oid
, old_oid
, data
->sb
->buf
,
3022 REF_HAVE_NEW
| REF_HAVE_OLD
, msg
,
3023 data
->index
++, data
->errbuf
);
3027 static int migrate_one_reflog(const char *refname
, void *cb_data
)
3029 struct migration_data
*migration_data
= cb_data
;
3030 struct reflog_migration_data data
= {
3032 .old_refs
= migration_data
->old_refs
,
3033 .transaction
= migration_data
->transaction
,
3034 .errbuf
= migration_data
->errbuf
,
3035 .sb
= &migration_data
->sb
,
3038 return refs_for_each_reflog_ent(migration_data
->old_refs
, refname
,
3039 migrate_one_reflog_entry
, &data
);
3042 static int move_files(const char *from_path
, const char *to_path
, struct strbuf
*errbuf
)
3044 struct strbuf from_buf
= STRBUF_INIT
, to_buf
= STRBUF_INIT
;
3045 size_t from_len
, to_len
;
3049 from_dir
= opendir(from_path
);
3051 strbuf_addf(errbuf
, "could not open source directory '%s': %s",
3052 from_path
, strerror(errno
));
3057 strbuf_addstr(&from_buf
, from_path
);
3058 strbuf_complete(&from_buf
, '/');
3059 from_len
= from_buf
.len
;
3061 strbuf_addstr(&to_buf
, to_path
);
3062 strbuf_complete(&to_buf
, '/');
3063 to_len
= to_buf
.len
;
3069 ent
= readdir(from_dir
);
3073 if (!strcmp(ent
->d_name
, ".") ||
3074 !strcmp(ent
->d_name
, ".."))
3077 strbuf_setlen(&from_buf
, from_len
);
3078 strbuf_addstr(&from_buf
, ent
->d_name
);
3080 strbuf_setlen(&to_buf
, to_len
);
3081 strbuf_addstr(&to_buf
, ent
->d_name
);
3083 ret
= rename(from_buf
.buf
, to_buf
.buf
);
3085 strbuf_addf(errbuf
, "could not link file '%s' to '%s': %s",
3086 from_buf
.buf
, to_buf
.buf
, strerror(errno
));
3092 strbuf_addf(errbuf
, "could not read entry from directory '%s': %s",
3093 from_path
, strerror(errno
));
3101 strbuf_release(&from_buf
);
3102 strbuf_release(&to_buf
);
3108 static int has_worktrees(void)
3110 struct worktree
**worktrees
= get_worktrees();
3114 for (i
= 0; worktrees
[i
]; i
++) {
3115 if (is_main_worktree(worktrees
[i
]))
3120 free_worktrees(worktrees
);
3124 int repo_migrate_ref_storage_format(struct repository
*repo
,
3125 enum ref_storage_format format
,
3127 struct strbuf
*errbuf
)
3129 struct ref_store
*old_refs
= NULL
, *new_refs
= NULL
;
3130 struct ref_transaction
*transaction
= NULL
;
3131 struct strbuf new_gitdir
= STRBUF_INIT
;
3132 struct migration_data data
= {
3135 int did_migrate_refs
= 0;
3138 if (repo
->ref_storage_format
== format
) {
3139 strbuf_addstr(errbuf
, "current and new ref storage format are equal");
3144 old_refs
= get_main_ref_store(repo
);
3147 * Worktrees complicate the migration because every worktree has a
3148 * separate ref storage. While it should be feasible to implement, this
3149 * is pushed out to a future iteration.
3151 * TODO: we should really be passing the caller-provided repository to
3152 * `has_worktrees()`, but our worktree subsystem doesn't yet support
3155 if (has_worktrees()) {
3156 strbuf_addstr(errbuf
, "migrating repositories with worktrees is not supported yet");
3162 * The overall logic looks like this:
3164 * 1. Set up a new temporary directory and initialize it with the new
3165 * format. This is where all refs will be migrated into.
3167 * 2. Enumerate all refs and write them into the new ref storage.
3168 * This operation is safe as we do not yet modify the main
3171 * 3. Enumerate all reflogs and write them into the new ref storage.
3172 * This operation is safe as we do not yet modify the main
3175 * 4. If we're in dry-run mode then we are done and can hand over the
3176 * directory to the caller for inspection. If not, we now start
3177 * with the destructive part.
3179 * 5. Delete the old ref storage from disk. As we have a copy of refs
3180 * in the new ref storage it's okay(ish) if we now get interrupted
3181 * as there is an equivalent copy of all refs available.
3183 * 6. Move the new ref storage files into place.
3185 * 7. Change the repository format to the new ref format.
3187 strbuf_addf(&new_gitdir
, "%s/%s", old_refs
->gitdir
, "ref_migration.XXXXXX");
3188 if (!mkdtemp(new_gitdir
.buf
)) {
3189 strbuf_addf(errbuf
, "cannot create migration directory: %s",
3195 new_refs
= ref_store_init(repo
, format
, new_gitdir
.buf
,
3196 REF_STORE_ALL_CAPS
);
3197 ret
= ref_store_create_on_disk(new_refs
, 0, errbuf
);
3201 transaction
= ref_store_transaction_begin(new_refs
, REF_TRANSACTION_FLAG_INITIAL
,
3206 data
.old_refs
= old_refs
;
3207 data
.transaction
= transaction
;
3208 data
.errbuf
= errbuf
;
3211 * We need to use the internal `do_for_each_ref()` here so that we can
3212 * also include broken refs and symrefs. These would otherwise be
3215 * Ideally, we would do this call while locking the old ref storage
3216 * such that there cannot be any concurrent modifications. We do not
3217 * have the infra for that though, and the "files" backend does not
3218 * allow for a central lock due to its design. It's thus on the user to
3219 * ensure that there are no concurrent writes.
3221 ret
= do_for_each_ref(old_refs
, "", NULL
, migrate_one_ref
, 0,
3222 DO_FOR_EACH_INCLUDE_ROOT_REFS
| DO_FOR_EACH_INCLUDE_BROKEN
,
3227 if (!(flags
& REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG
)) {
3228 ret
= refs_for_each_reflog(old_refs
, migrate_one_reflog
, &data
);
3233 ret
= ref_transaction_commit(transaction
, errbuf
);
3236 did_migrate_refs
= 1;
3238 if (flags
& REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN
) {
3239 printf(_("Finished dry-run migration of refs, "
3240 "the result can be found at '%s'\n"), new_gitdir
.buf
);
3246 * Release the new ref store such that any potentially-open files will
3247 * be closed. This is required for platforms like Cygwin, where
3248 * renaming an open file results in EPERM.
3250 ref_store_release(new_refs
);
3251 FREE_AND_NULL(new_refs
);
3254 * Until now we were in the non-destructive phase, where we only
3255 * populated the new ref store. From hereon though we are about
3256 * to get hands by deleting the old ref store and then moving
3257 * the new one into place.
3259 * Assuming that there were no concurrent writes, the new ref
3260 * store should have all information. So if we fail from hereon
3261 * we may be in an in-between state, but it would still be able
3262 * to recover by manually moving remaining files from the
3263 * temporary migration directory into place.
3265 ret
= ref_store_remove_on_disk(old_refs
, errbuf
);
3269 ret
= move_files(new_gitdir
.buf
, old_refs
->gitdir
, errbuf
);
3273 if (rmdir(new_gitdir
.buf
) < 0)
3274 warning_errno(_("could not remove temporary migration directory '%s'"),
3278 * We have migrated the repository, so we now need to adjust the
3279 * repository format so that clients will use the new ref store.
3280 * We also need to swap out the repository's main ref store.
3282 initialize_repository_version(hash_algo_by_ptr(repo
->hash_algo
), format
, 1);
3285 * Unset the old ref store and release it. `get_main_ref_store()` will
3286 * make sure to lazily re-initialize the repository's ref store with
3289 ref_store_release(old_refs
);
3290 FREE_AND_NULL(old_refs
);
3291 repo
->refs_private
= NULL
;
3296 if (ret
&& did_migrate_refs
) {
3297 strbuf_complete(errbuf
, '\n');
3298 strbuf_addf(errbuf
, _("migrated refs can be found at '%s'"),
3303 ref_store_release(new_refs
);
3306 ref_transaction_free(transaction
);
3307 strbuf_release(&new_gitdir
);
3308 strbuf_release(&data
.sb
);
3312 int ref_update_expects_existing_old_ref(struct ref_update
*update
)
3314 return (update
->flags
& REF_HAVE_OLD
) &&
3315 (!is_null_oid(&update
->old_oid
) || update
->old_target
);