2 * The backend-independent part of the reference module.
8 #include "refs/refs-internal.h"
13 * How to handle various characters in refnames:
14 * 0: An acceptable character for refs
16 * 2: ., look for a preceding . to reject .. in refs
17 * 3: {, look for a preceding @ to reject @{ in refs
18 * 4: A bad character: ASCII control characters, and
19 * ":", "?", "[", "\", "^", "~", SP, or TAB
20 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
22 static unsigned char refname_disposition[256] = {
23 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
24 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
25 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
26 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
34 * Try to read one refname component from the front of refname.
35 * Return the length of the component found, or -1 if the component is
36 * not legal. It is legal if it is something reasonable to have under
37 * ".git/refs/"; We do not like it if:
39 * - any path component of it begins with ".", or
40 * - it has double dots "..", or
41 * - it has ASCII control characters, or
42 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
43 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
44 * - it ends with a "/", or
45 * - it ends with ".lock", or
46 * - it contains a "@{" portion
48 static int check_refname_component(const char *refname, int *flags)
53 for (cp = refname; ; cp++) {
55 unsigned char disp = refname_disposition[ch];
61 return -1; /* Refname contains "..". */
65 return -1; /* Refname contains "@{". */
70 if (!(*flags & REFNAME_REFSPEC_PATTERN))
71 return -1; /* refspec can't be a pattern */
74 * Unset the pattern flag so that we only accept
75 * a single asterisk for one side of refspec.
77 *flags &= ~ REFNAME_REFSPEC_PATTERN;
84 return 0; /* Component has zero length. */
85 if (refname[0] == '.')
86 return -1; /* Component starts with '.'. */
87 if (cp - refname >= LOCK_SUFFIX_LEN &&
88 !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
89 return -1; /* Refname ends with ".lock". */
93 int check_refname_format(const char *refname, int flags)
95 int component_len, component_count = 0;
97 if (!strcmp(refname, "@"))
98 /* Refname is a single character '@'. */
102 /* We are at the start of a path component. */
103 component_len = check_refname_component(refname, &flags);
104 if (component_len <= 0)
108 if (refname[component_len] == '\0')
110 /* Skip to next component. */
111 refname += component_len + 1;
114 if (refname[component_len - 1] == '.')
115 return -1; /* Refname ends with '.'. */
116 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
117 return -1; /* Refname has only one component. */
121 int refname_is_safe(const char *refname)
125 if (skip_prefix(refname, "refs/", &rest)) {
130 * Does the refname try to escape refs/?
131 * For example: refs/foo/../bar is safe but refs/foo/../../bar
134 buf = xmallocz(strlen(rest));
135 result = !normalize_path_copy(buf, rest);
140 if (!isupper(*refname) && *refname != '_')
147 char *resolve_refdup(const char *refname, int resolve_flags,
148 unsigned char *sha1, int *flags)
150 return xstrdup_or_null(resolve_ref_unsafe(refname, resolve_flags,
154 /* The argument to filter_refs */
161 int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
163 if (resolve_ref_unsafe(refname, resolve_flags, sha1, flags))
168 int read_ref(const char *refname, unsigned char *sha1)
170 return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL);
173 int ref_exists(const char *refname)
175 unsigned char sha1[20];
176 return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
179 static int filter_refs(const char *refname, const struct object_id *oid,
180 int flags, void *data)
182 struct ref_filter *filter = (struct ref_filter *)data;
184 if (wildmatch(filter->pattern, refname, 0, NULL))
186 return filter->fn(refname, oid, flags, filter->cb_data);
189 enum peel_status peel_object(const unsigned char *name, unsigned char *sha1)
191 struct object *o = lookup_unknown_object(name);
193 if (o->type == OBJ_NONE) {
194 int type = sha1_object_info(name, NULL);
195 if (type < 0 || !object_as_type(o, type, 0))
199 if (o->type != OBJ_TAG)
202 o = deref_tag_noverify(o);
206 hashcpy(sha1, o->oid.hash);
210 struct warn_if_dangling_data {
213 const struct string_list *refnames;
217 static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
218 int flags, void *cb_data)
220 struct warn_if_dangling_data *d = cb_data;
221 const char *resolves_to;
222 struct object_id junk;
224 if (!(flags & REF_ISSYMREF))
227 resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
230 ? strcmp(resolves_to, d->refname)
231 : !string_list_has_string(d->refnames, resolves_to))) {
235 fprintf(d->fp, d->msg_fmt, refname);
240 void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
242 struct warn_if_dangling_data data;
245 data.refname = refname;
246 data.refnames = NULL;
247 data.msg_fmt = msg_fmt;
248 for_each_rawref(warn_if_dangling_symref, &data);
251 void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
253 struct warn_if_dangling_data data;
257 data.refnames = refnames;
258 data.msg_fmt = msg_fmt;
259 for_each_rawref(warn_if_dangling_symref, &data);
262 int for_each_tag_ref(each_ref_fn fn, void *cb_data)
264 return for_each_ref_in("refs/tags/", fn, cb_data);
267 int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
269 return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
272 int for_each_branch_ref(each_ref_fn fn, void *cb_data)
274 return for_each_ref_in("refs/heads/", fn, cb_data);
277 int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
279 return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data);
282 int for_each_remote_ref(each_ref_fn fn, void *cb_data)
284 return for_each_ref_in("refs/remotes/", fn, cb_data);
287 int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
289 return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data);
292 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
294 struct strbuf buf = STRBUF_INIT;
296 struct object_id oid;
299 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
300 if (!read_ref_full(buf.buf, RESOLVE_REF_READING, oid.hash, &flag))
301 ret = fn(buf.buf, &oid, flag, cb_data);
302 strbuf_release(&buf);
307 int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
308 const char *prefix, void *cb_data)
310 struct strbuf real_pattern = STRBUF_INIT;
311 struct ref_filter filter;
314 if (!prefix && !starts_with(pattern, "refs/"))
315 strbuf_addstr(&real_pattern, "refs/");
317 strbuf_addstr(&real_pattern, prefix);
318 strbuf_addstr(&real_pattern, pattern);
320 if (!has_glob_specials(pattern)) {
321 /* Append implied '/' '*' if not present. */
322 strbuf_complete(&real_pattern, '/');
323 /* No need to check for '*', there is none. */
324 strbuf_addch(&real_pattern, '*');
327 filter.pattern = real_pattern.buf;
329 filter.cb_data = cb_data;
330 ret = for_each_ref(filter_refs, &filter);
332 strbuf_release(&real_pattern);
336 int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
338 return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
341 const char *prettify_refname(const char *name)
344 starts_with(name, "refs/heads/") ? 11 :
345 starts_with(name, "refs/tags/") ? 10 :
346 starts_with(name, "refs/remotes/") ? 13 :
350 static const char *ref_rev_parse_rules[] = {
356 "refs/remotes/%.*s/HEAD",
360 int refname_match(const char *abbrev_name, const char *full_name)
363 const int abbrev_name_len = strlen(abbrev_name);
365 for (p = ref_rev_parse_rules; *p; p++) {
366 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
375 * *string and *len will only be substituted, and *string returned (for
376 * later free()ing) if the string passed in is a magic short-hand form
379 static char *substitute_branch_name(const char **string, int *len)
381 struct strbuf buf = STRBUF_INIT;
382 int ret = interpret_branch_name(*string, *len, &buf);
386 *string = strbuf_detach(&buf, &size);
388 return (char *)*string;
394 int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
396 char *last_branch = substitute_branch_name(&str, &len);
401 for (p = ref_rev_parse_rules; *p; p++) {
402 char fullref[PATH_MAX];
403 unsigned char sha1_from_ref[20];
404 unsigned char *this_result;
407 this_result = refs_found ? sha1_from_ref : sha1;
408 mksnpath(fullref, sizeof(fullref), *p, len, str);
409 r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING,
414 if (!warn_ambiguous_refs)
416 } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) {
417 warning("ignoring dangling symref %s.", fullref);
418 } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) {
419 warning("ignoring broken ref %s.", fullref);
426 int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
428 char *last_branch = substitute_branch_name(&str, &len);
433 for (p = ref_rev_parse_rules; *p; p++) {
434 unsigned char hash[20];
436 const char *ref, *it;
438 mksnpath(path, sizeof(path), *p, len, str);
439 ref = resolve_ref_unsafe(path, RESOLVE_REF_READING,
443 if (reflog_exists(path))
445 else if (strcmp(ref, path) && reflog_exists(ref))
453 if (!warn_ambiguous_refs)
460 static int is_per_worktree_ref(const char *refname)
462 return !strcmp(refname, "HEAD") ||
463 starts_with(refname, "refs/bisect/");
466 static int is_pseudoref_syntax(const char *refname)
470 for (c = refname; *c; c++) {
471 if (!isupper(*c) && *c != '-' && *c != '_')
478 enum ref_type ref_type(const char *refname)
480 if (is_per_worktree_ref(refname))
481 return REF_TYPE_PER_WORKTREE;
482 if (is_pseudoref_syntax(refname))
483 return REF_TYPE_PSEUDOREF;
484 return REF_TYPE_NORMAL;
487 static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
488 const unsigned char *old_sha1, struct strbuf *err)
490 const char *filename;
492 static struct lock_file lock;
493 struct strbuf buf = STRBUF_INIT;
496 strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
498 filename = git_path("%s", pseudoref);
499 fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
501 strbuf_addf(err, "Could not open '%s' for writing: %s",
502 filename, strerror(errno));
507 unsigned char actual_old_sha1[20];
509 if (read_ref(pseudoref, actual_old_sha1))
510 die("could not read ref '%s'", pseudoref);
511 if (hashcmp(actual_old_sha1, old_sha1)) {
512 strbuf_addf(err, "Unexpected sha1 when writing %s", pseudoref);
513 rollback_lock_file(&lock);
518 if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
519 strbuf_addf(err, "Could not write to '%s'", filename);
520 rollback_lock_file(&lock);
524 commit_lock_file(&lock);
527 strbuf_release(&buf);
531 static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1)
533 static struct lock_file lock;
534 const char *filename;
536 filename = git_path("%s", pseudoref);
538 if (old_sha1 && !is_null_sha1(old_sha1)) {
540 unsigned char actual_old_sha1[20];
542 fd = hold_lock_file_for_update(&lock, filename,
545 die_errno(_("Could not open '%s' for writing"), filename);
546 if (read_ref(pseudoref, actual_old_sha1))
547 die("could not read ref '%s'", pseudoref);
548 if (hashcmp(actual_old_sha1, old_sha1)) {
549 warning("Unexpected sha1 when deleting %s", pseudoref);
550 rollback_lock_file(&lock);
555 rollback_lock_file(&lock);
563 int delete_ref(const char *refname, const unsigned char *old_sha1,
566 struct ref_transaction *transaction;
567 struct strbuf err = STRBUF_INIT;
569 if (ref_type(refname) == REF_TYPE_PSEUDOREF)
570 return delete_pseudoref(refname, old_sha1);
572 transaction = ref_transaction_begin(&err);
574 ref_transaction_delete(transaction, refname, old_sha1,
575 flags, NULL, &err) ||
576 ref_transaction_commit(transaction, &err)) {
577 error("%s", err.buf);
578 ref_transaction_free(transaction);
579 strbuf_release(&err);
582 ref_transaction_free(transaction);
583 strbuf_release(&err);
587 int copy_reflog_msg(char *buf, const char *msg)
594 while ((c = *msg++)) {
595 if (wasspace && isspace(c))
597 wasspace = isspace(c);
602 while (buf < cp && isspace(cp[-1]))
608 int should_autocreate_reflog(const char *refname)
610 if (!log_all_ref_updates)
612 return starts_with(refname, "refs/heads/") ||
613 starts_with(refname, "refs/remotes/") ||
614 starts_with(refname, "refs/notes/") ||
615 !strcmp(refname, "HEAD");
618 int is_branch(const char *refname)
620 return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
623 struct read_ref_at_cb {
625 unsigned long at_time;
631 unsigned char osha1[20];
632 unsigned char nsha1[20];
636 unsigned long *cutoff_time;
641 static int read_ref_at_ent(unsigned char *osha1, unsigned char *nsha1,
642 const char *email, unsigned long timestamp, int tz,
643 const char *message, void *cb_data)
645 struct read_ref_at_cb *cb = cb_data;
649 cb->date = timestamp;
651 if (timestamp <= cb->at_time || cb->cnt == 0) {
653 *cb->msg = xstrdup(message);
655 *cb->cutoff_time = timestamp;
659 *cb->cutoff_cnt = cb->reccnt - 1;
661 * we have not yet updated cb->[n|o]sha1 so they still
662 * hold the values for the previous record.
664 if (!is_null_sha1(cb->osha1)) {
665 hashcpy(cb->sha1, nsha1);
666 if (hashcmp(cb->osha1, nsha1))
667 warning("Log for ref %s has gap after %s.",
668 cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
670 else if (cb->date == cb->at_time)
671 hashcpy(cb->sha1, nsha1);
672 else if (hashcmp(nsha1, cb->sha1))
673 warning("Log for ref %s unexpectedly ended on %s.",
674 cb->refname, show_date(cb->date, cb->tz,
675 DATE_MODE(RFC2822)));
676 hashcpy(cb->osha1, osha1);
677 hashcpy(cb->nsha1, nsha1);
681 hashcpy(cb->osha1, osha1);
682 hashcpy(cb->nsha1, nsha1);
688 static int read_ref_at_ent_oldest(unsigned char *osha1, unsigned char *nsha1,
689 const char *email, unsigned long timestamp,
690 int tz, const char *message, void *cb_data)
692 struct read_ref_at_cb *cb = cb_data;
695 *cb->msg = xstrdup(message);
697 *cb->cutoff_time = timestamp;
701 *cb->cutoff_cnt = cb->reccnt;
702 hashcpy(cb->sha1, osha1);
703 if (is_null_sha1(cb->sha1))
704 hashcpy(cb->sha1, nsha1);
705 /* We just want the first entry */
709 int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt,
710 unsigned char *sha1, char **msg,
711 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
713 struct read_ref_at_cb cb;
715 memset(&cb, 0, sizeof(cb));
716 cb.refname = refname;
717 cb.at_time = at_time;
720 cb.cutoff_time = cutoff_time;
721 cb.cutoff_tz = cutoff_tz;
722 cb.cutoff_cnt = cutoff_cnt;
725 for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
728 if (flags & GET_SHA1_QUIETLY)
731 die("Log for %s is empty.", refname);
736 for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
741 struct ref_transaction *ref_transaction_begin(struct strbuf *err)
745 return xcalloc(1, sizeof(struct ref_transaction));
748 void ref_transaction_free(struct ref_transaction *transaction)
755 for (i = 0; i < transaction->nr; i++) {
756 free(transaction->updates[i]->msg);
757 free(transaction->updates[i]);
759 free(transaction->updates);
763 static struct ref_update *add_update(struct ref_transaction *transaction,
766 struct ref_update *update;
767 FLEX_ALLOC_STR(update, refname, refname);
768 ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
769 transaction->updates[transaction->nr++] = update;
773 int ref_transaction_update(struct ref_transaction *transaction,
775 const unsigned char *new_sha1,
776 const unsigned char *old_sha1,
777 unsigned int flags, const char *msg,
780 struct ref_update *update;
784 if (transaction->state != REF_TRANSACTION_OPEN)
785 die("BUG: update called for transaction that is not open");
787 if (new_sha1 && !is_null_sha1(new_sha1) &&
788 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
789 strbuf_addf(err, "refusing to update ref with bad name %s",
794 update = add_update(transaction, refname);
796 hashcpy(update->new_sha1, new_sha1);
797 flags |= REF_HAVE_NEW;
800 hashcpy(update->old_sha1, old_sha1);
801 flags |= REF_HAVE_OLD;
803 update->flags = flags;
805 update->msg = xstrdup(msg);
809 int ref_transaction_create(struct ref_transaction *transaction,
811 const unsigned char *new_sha1,
812 unsigned int flags, const char *msg,
815 if (!new_sha1 || is_null_sha1(new_sha1))
816 die("BUG: create called without valid new_sha1");
817 return ref_transaction_update(transaction, refname, new_sha1,
818 null_sha1, flags, msg, err);
821 int ref_transaction_delete(struct ref_transaction *transaction,
823 const unsigned char *old_sha1,
824 unsigned int flags, const char *msg,
827 if (old_sha1 && is_null_sha1(old_sha1))
828 die("BUG: delete called with old_sha1 set to zeros");
829 return ref_transaction_update(transaction, refname,
834 int ref_transaction_verify(struct ref_transaction *transaction,
836 const unsigned char *old_sha1,
841 die("BUG: verify called with old_sha1 set to NULL");
842 return ref_transaction_update(transaction, refname,
847 int update_ref(const char *msg, const char *refname,
848 const unsigned char *new_sha1, const unsigned char *old_sha1,
849 unsigned int flags, enum action_on_err onerr)
851 struct ref_transaction *t = NULL;
852 struct strbuf err = STRBUF_INIT;
855 if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
856 ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
858 t = ref_transaction_begin(&err);
860 ref_transaction_update(t, refname, new_sha1, old_sha1,
862 ref_transaction_commit(t, &err)) {
864 ref_transaction_free(t);
868 const char *str = "update_ref failed for ref '%s': %s";
871 case UPDATE_REFS_MSG_ON_ERR:
872 error(str, refname, err.buf);
874 case UPDATE_REFS_DIE_ON_ERR:
875 die(str, refname, err.buf);
877 case UPDATE_REFS_QUIET_ON_ERR:
880 strbuf_release(&err);
883 strbuf_release(&err);
885 ref_transaction_free(t);
889 char *shorten_unambiguous_ref(const char *refname, int strict)
892 static char **scanf_fmts;
898 * Pre-generate scanf formats from ref_rev_parse_rules[].
899 * Generate a format suitable for scanf from a
900 * ref_rev_parse_rules rule by interpolating "%s" at the
901 * location of the "%.*s".
903 size_t total_len = 0;
906 /* the rule list is NULL terminated, count them first */
907 for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
908 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
909 total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
911 scanf_fmts = xmalloc(st_add(st_mult(nr_rules, sizeof(char *)), total_len));
914 for (i = 0; i < nr_rules; i++) {
915 assert(offset < total_len);
916 scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
917 offset += snprintf(scanf_fmts[i], total_len - offset,
918 ref_rev_parse_rules[i], 2, "%s") + 1;
922 /* bail out if there are no rules */
924 return xstrdup(refname);
926 /* buffer for scanf result, at most refname must fit */
927 short_name = xstrdup(refname);
929 /* skip first rule, it will always match */
930 for (i = nr_rules - 1; i > 0 ; --i) {
932 int rules_to_fail = i;
935 if (1 != sscanf(refname, scanf_fmts[i], short_name))
938 short_name_len = strlen(short_name);
941 * in strict mode, all (except the matched one) rules
942 * must fail to resolve to a valid non-ambiguous ref
945 rules_to_fail = nr_rules;
948 * check if the short name resolves to a valid ref,
949 * but use only rules prior to the matched one
951 for (j = 0; j < rules_to_fail; j++) {
952 const char *rule = ref_rev_parse_rules[j];
953 char refname[PATH_MAX];
955 /* skip matched rule */
960 * the short name is ambiguous, if it resolves
961 * (with this previous rule) to a valid ref
962 * read_ref() returns 0 on success
964 mksnpath(refname, sizeof(refname),
965 rule, short_name_len, short_name);
966 if (ref_exists(refname))
971 * short name is non-ambiguous if all previous rules
972 * haven't resolved to a valid ref
974 if (j == rules_to_fail)
979 return xstrdup(refname);
982 static struct string_list *hide_refs;
984 int parse_hide_refs_config(const char *var, const char *value, const char *section)
986 if (!strcmp("transfer.hiderefs", var) ||
987 /* NEEDSWORK: use parse_config_key() once both are merged */
988 (starts_with(var, section) && var[strlen(section)] == '.' &&
989 !strcmp(var + strlen(section), ".hiderefs"))) {
994 return config_error_nonbool(var);
995 ref = xstrdup(value);
997 while (len && ref[len - 1] == '/')
1000 hide_refs = xcalloc(1, sizeof(*hide_refs));
1001 hide_refs->strdup_strings = 1;
1003 string_list_append(hide_refs, ref);
1008 int ref_is_hidden(const char *refname, const char *refname_full)
1014 for (i = hide_refs->nr - 1; i >= 0; i--) {
1015 const char *match = hide_refs->items[i].string;
1016 const char *subject;
1020 if (*match == '!') {
1025 if (*match == '^') {
1026 subject = refname_full;
1032 /* refname can be NULL when namespaces are used. */
1033 if (!subject || !starts_with(subject, match))
1035 len = strlen(match);
1036 if (!subject[len] || subject[len] == '/')
1042 const char *find_descendant_ref(const char *dirname,
1043 const struct string_list *extras,
1044 const struct string_list *skip)
1052 * Look at the place where dirname would be inserted into
1053 * extras. If there is an entry at that position that starts
1054 * with dirname (remember, dirname includes the trailing
1055 * slash) and is not in skip, then we have a conflict.
1057 for (pos = string_list_find_insert_index(extras, dirname, 0);
1058 pos < extras->nr; pos++) {
1059 const char *extra_refname = extras->items[pos].string;
1061 if (!starts_with(extra_refname, dirname))
1064 if (!skip || !string_list_has_string(skip, extra_refname))
1065 return extra_refname;
1070 int rename_ref_available(const char *oldname, const char *newname)
1072 struct string_list skip = STRING_LIST_INIT_NODUP;
1073 struct strbuf err = STRBUF_INIT;
1076 string_list_insert(&skip, oldname);
1077 ret = !verify_refname_available(newname, NULL, &skip, &err);
1079 error("%s", err.buf);
1081 string_list_clear(&skip, 0);
1082 strbuf_release(&err);
1086 int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1088 struct object_id oid;
1092 if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
1093 return fn("HEAD", &oid, 0, cb_data);
1098 if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
1099 return fn("HEAD", &oid, flag, cb_data);
1104 int head_ref(each_ref_fn fn, void *cb_data)
1106 return head_ref_submodule(NULL, fn, cb_data);
1109 int for_each_ref(each_ref_fn fn, void *cb_data)
1111 return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
1114 int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1116 return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
1119 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1121 return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
1124 int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1126 unsigned int flag = 0;
1129 flag = DO_FOR_EACH_INCLUDE_BROKEN;
1130 return do_for_each_ref(NULL, prefix, fn, 0, flag, cb_data);
1133 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1134 each_ref_fn fn, void *cb_data)
1136 return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
1139 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1141 return do_for_each_ref(NULL, git_replace_ref_base, fn,
1142 strlen(git_replace_ref_base), 0, cb_data);
1145 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1147 struct strbuf buf = STRBUF_INIT;
1149 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1150 ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
1151 strbuf_release(&buf);
1155 int for_each_rawref(each_ref_fn fn, void *cb_data)
1157 return do_for_each_ref(NULL, "", fn, 0,
1158 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1161 /* This function needs to return a meaningful errno on failure */
1162 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1163 unsigned char *sha1, int *flags)
1165 static struct strbuf sb_refname = STRBUF_INIT;
1170 flags = &unused_flags;
1174 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1175 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1176 !refname_is_safe(refname)) {
1182 * dwim_ref() uses REF_ISBROKEN to distinguish between
1183 * missing refs and refs that were present but invalid,
1184 * to complain about the latter to stderr.
1186 * We don't know whether the ref exists, so don't set
1189 *flags |= REF_BAD_NAME;
1192 for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1193 unsigned int read_flags = 0;
1195 if (read_raw_ref(refname, sha1, &sb_refname, &read_flags)) {
1196 *flags |= read_flags;
1197 if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1200 if (*flags & REF_BAD_NAME)
1201 *flags |= REF_ISBROKEN;
1205 *flags |= read_flags;
1207 if (!(read_flags & REF_ISSYMREF)) {
1208 if (*flags & REF_BAD_NAME) {
1210 *flags |= REF_ISBROKEN;
1215 refname = sb_refname.buf;
1216 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1220 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1221 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1222 !refname_is_safe(refname)) {
1227 *flags |= REF_ISBROKEN | REF_BAD_NAME;