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
)
123 if (starts_with(refname
, "refs/")) {
127 buf
= xmallocz(strlen(refname
));
129 * Does the refname try to escape refs/?
130 * For example: refs/foo/../bar is safe but refs/foo/../../bar
133 result
= !normalize_path_copy(buf
, refname
+ strlen("refs/"));
138 if (!isupper(*refname
) && *refname
!= '_')
145 char *resolve_refdup(const char *refname
, int resolve_flags
,
146 unsigned char *sha1
, int *flags
)
148 return xstrdup_or_null(resolve_ref_unsafe(refname
, resolve_flags
,
152 /* The argument to filter_refs */
159 int read_ref_full(const char *refname
, int resolve_flags
, unsigned char *sha1
, int *flags
)
161 if (resolve_ref_unsafe(refname
, resolve_flags
, sha1
, flags
))
166 int read_ref(const char *refname
, unsigned char *sha1
)
168 return read_ref_full(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
171 int ref_exists(const char *refname
)
173 unsigned char sha1
[20];
174 return !!resolve_ref_unsafe(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
177 static int filter_refs(const char *refname
, const struct object_id
*oid
,
178 int flags
, void *data
)
180 struct ref_filter
*filter
= (struct ref_filter
*)data
;
182 if (wildmatch(filter
->pattern
, refname
, 0, NULL
))
184 return filter
->fn(refname
, oid
, flags
, filter
->cb_data
);
187 enum peel_status
peel_object(const unsigned char *name
, unsigned char *sha1
)
189 struct object
*o
= lookup_unknown_object(name
);
191 if (o
->type
== OBJ_NONE
) {
192 int type
= sha1_object_info(name
, NULL
);
193 if (type
< 0 || !object_as_type(o
, type
, 0))
197 if (o
->type
!= OBJ_TAG
)
200 o
= deref_tag_noverify(o
);
204 hashcpy(sha1
, o
->oid
.hash
);
208 struct warn_if_dangling_data
{
211 const struct string_list
*refnames
;
215 static int warn_if_dangling_symref(const char *refname
, const struct object_id
*oid
,
216 int flags
, void *cb_data
)
218 struct warn_if_dangling_data
*d
= cb_data
;
219 const char *resolves_to
;
220 struct object_id junk
;
222 if (!(flags
& REF_ISSYMREF
))
225 resolves_to
= resolve_ref_unsafe(refname
, 0, junk
.hash
, NULL
);
228 ? strcmp(resolves_to
, d
->refname
)
229 : !string_list_has_string(d
->refnames
, resolves_to
))) {
233 fprintf(d
->fp
, d
->msg_fmt
, refname
);
238 void warn_dangling_symref(FILE *fp
, const char *msg_fmt
, const char *refname
)
240 struct warn_if_dangling_data data
;
243 data
.refname
= refname
;
244 data
.refnames
= NULL
;
245 data
.msg_fmt
= msg_fmt
;
246 for_each_rawref(warn_if_dangling_symref
, &data
);
249 void warn_dangling_symrefs(FILE *fp
, const char *msg_fmt
, const struct string_list
*refnames
)
251 struct warn_if_dangling_data data
;
255 data
.refnames
= refnames
;
256 data
.msg_fmt
= msg_fmt
;
257 for_each_rawref(warn_if_dangling_symref
, &data
);
260 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
262 return for_each_ref_in("refs/tags/", fn
, cb_data
);
265 int for_each_tag_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
267 return for_each_ref_in_submodule(submodule
, "refs/tags/", fn
, cb_data
);
270 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
272 return for_each_ref_in("refs/heads/", fn
, cb_data
);
275 int for_each_branch_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
277 return for_each_ref_in_submodule(submodule
, "refs/heads/", fn
, cb_data
);
280 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
282 return for_each_ref_in("refs/remotes/", fn
, cb_data
);
285 int for_each_remote_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
287 return for_each_ref_in_submodule(submodule
, "refs/remotes/", fn
, cb_data
);
290 int head_ref_namespaced(each_ref_fn fn
, void *cb_data
)
292 struct strbuf buf
= STRBUF_INIT
;
294 struct object_id oid
;
297 strbuf_addf(&buf
, "%sHEAD", get_git_namespace());
298 if (!read_ref_full(buf
.buf
, RESOLVE_REF_READING
, oid
.hash
, &flag
))
299 ret
= fn(buf
.buf
, &oid
, flag
, cb_data
);
300 strbuf_release(&buf
);
305 int for_each_glob_ref_in(each_ref_fn fn
, const char *pattern
,
306 const char *prefix
, void *cb_data
)
308 struct strbuf real_pattern
= STRBUF_INIT
;
309 struct ref_filter filter
;
312 if (!prefix
&& !starts_with(pattern
, "refs/"))
313 strbuf_addstr(&real_pattern
, "refs/");
315 strbuf_addstr(&real_pattern
, prefix
);
316 strbuf_addstr(&real_pattern
, pattern
);
318 if (!has_glob_specials(pattern
)) {
319 /* Append implied '/' '*' if not present. */
320 strbuf_complete(&real_pattern
, '/');
321 /* No need to check for '*', there is none. */
322 strbuf_addch(&real_pattern
, '*');
325 filter
.pattern
= real_pattern
.buf
;
327 filter
.cb_data
= cb_data
;
328 ret
= for_each_ref(filter_refs
, &filter
);
330 strbuf_release(&real_pattern
);
334 int for_each_glob_ref(each_ref_fn fn
, const char *pattern
, void *cb_data
)
336 return for_each_glob_ref_in(fn
, pattern
, NULL
, cb_data
);
339 const char *prettify_refname(const char *name
)
342 starts_with(name
, "refs/heads/") ? 11 :
343 starts_with(name
, "refs/tags/") ? 10 :
344 starts_with(name
, "refs/remotes/") ? 13 :
348 static const char *ref_rev_parse_rules
[] = {
354 "refs/remotes/%.*s/HEAD",
358 int refname_match(const char *abbrev_name
, const char *full_name
)
361 const int abbrev_name_len
= strlen(abbrev_name
);
363 for (p
= ref_rev_parse_rules
; *p
; p
++) {
364 if (!strcmp(full_name
, mkpath(*p
, abbrev_name_len
, abbrev_name
))) {
373 * *string and *len will only be substituted, and *string returned (for
374 * later free()ing) if the string passed in is a magic short-hand form
377 static char *substitute_branch_name(const char **string
, int *len
)
379 struct strbuf buf
= STRBUF_INIT
;
380 int ret
= interpret_branch_name(*string
, *len
, &buf
);
384 *string
= strbuf_detach(&buf
, &size
);
386 return (char *)*string
;
392 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
394 char *last_branch
= substitute_branch_name(&str
, &len
);
399 for (p
= ref_rev_parse_rules
; *p
; p
++) {
400 char fullref
[PATH_MAX
];
401 unsigned char sha1_from_ref
[20];
402 unsigned char *this_result
;
405 this_result
= refs_found
? sha1_from_ref
: sha1
;
406 mksnpath(fullref
, sizeof(fullref
), *p
, len
, str
);
407 r
= resolve_ref_unsafe(fullref
, RESOLVE_REF_READING
,
412 if (!warn_ambiguous_refs
)
414 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
, "HEAD")) {
415 warning("ignoring dangling symref %s.", fullref
);
416 } else if ((flag
& REF_ISBROKEN
) && strchr(fullref
, '/')) {
417 warning("ignoring broken ref %s.", fullref
);
424 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
426 char *last_branch
= substitute_branch_name(&str
, &len
);
431 for (p
= ref_rev_parse_rules
; *p
; p
++) {
432 unsigned char hash
[20];
434 const char *ref
, *it
;
436 mksnpath(path
, sizeof(path
), *p
, len
, str
);
437 ref
= resolve_ref_unsafe(path
, RESOLVE_REF_READING
,
441 if (reflog_exists(path
))
443 else if (strcmp(ref
, path
) && reflog_exists(ref
))
451 if (!warn_ambiguous_refs
)
458 static int is_per_worktree_ref(const char *refname
)
460 return !strcmp(refname
, "HEAD") ||
461 starts_with(refname
, "refs/bisect/");
464 static int is_pseudoref_syntax(const char *refname
)
468 for (c
= refname
; *c
; c
++) {
469 if (!isupper(*c
) && *c
!= '-' && *c
!= '_')
476 enum ref_type
ref_type(const char *refname
)
478 if (is_per_worktree_ref(refname
))
479 return REF_TYPE_PER_WORKTREE
;
480 if (is_pseudoref_syntax(refname
))
481 return REF_TYPE_PSEUDOREF
;
482 return REF_TYPE_NORMAL
;
485 static int write_pseudoref(const char *pseudoref
, const unsigned char *sha1
,
486 const unsigned char *old_sha1
, struct strbuf
*err
)
488 const char *filename
;
490 static struct lock_file lock
;
491 struct strbuf buf
= STRBUF_INIT
;
494 strbuf_addf(&buf
, "%s\n", sha1_to_hex(sha1
));
496 filename
= git_path("%s", pseudoref
);
497 fd
= hold_lock_file_for_update(&lock
, filename
, LOCK_DIE_ON_ERROR
);
499 strbuf_addf(err
, "Could not open '%s' for writing: %s",
500 filename
, strerror(errno
));
505 unsigned char actual_old_sha1
[20];
507 if (read_ref(pseudoref
, actual_old_sha1
))
508 die("could not read ref '%s'", pseudoref
);
509 if (hashcmp(actual_old_sha1
, old_sha1
)) {
510 strbuf_addf(err
, "Unexpected sha1 when writing %s", pseudoref
);
511 rollback_lock_file(&lock
);
516 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
) {
517 strbuf_addf(err
, "Could not write to '%s'", filename
);
518 rollback_lock_file(&lock
);
522 commit_lock_file(&lock
);
525 strbuf_release(&buf
);
529 static int delete_pseudoref(const char *pseudoref
, const unsigned char *old_sha1
)
531 static struct lock_file lock
;
532 const char *filename
;
534 filename
= git_path("%s", pseudoref
);
536 if (old_sha1
&& !is_null_sha1(old_sha1
)) {
538 unsigned char actual_old_sha1
[20];
540 fd
= hold_lock_file_for_update(&lock
, filename
,
543 die_errno(_("Could not open '%s' for writing"), filename
);
544 if (read_ref(pseudoref
, actual_old_sha1
))
545 die("could not read ref '%s'", pseudoref
);
546 if (hashcmp(actual_old_sha1
, old_sha1
)) {
547 warning("Unexpected sha1 when deleting %s", pseudoref
);
548 rollback_lock_file(&lock
);
553 rollback_lock_file(&lock
);
561 int delete_ref(const char *refname
, const unsigned char *old_sha1
,
564 struct ref_transaction
*transaction
;
565 struct strbuf err
= STRBUF_INIT
;
567 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
)
568 return delete_pseudoref(refname
, old_sha1
);
570 transaction
= ref_transaction_begin(&err
);
572 ref_transaction_delete(transaction
, refname
, old_sha1
,
573 flags
, NULL
, &err
) ||
574 ref_transaction_commit(transaction
, &err
)) {
575 error("%s", err
.buf
);
576 ref_transaction_free(transaction
);
577 strbuf_release(&err
);
580 ref_transaction_free(transaction
);
581 strbuf_release(&err
);
585 int copy_reflog_msg(char *buf
, const char *msg
)
592 while ((c
= *msg
++)) {
593 if (wasspace
&& isspace(c
))
595 wasspace
= isspace(c
);
600 while (buf
< cp
&& isspace(cp
[-1]))
606 int should_autocreate_reflog(const char *refname
)
608 if (!log_all_ref_updates
)
610 return starts_with(refname
, "refs/heads/") ||
611 starts_with(refname
, "refs/remotes/") ||
612 starts_with(refname
, "refs/notes/") ||
613 !strcmp(refname
, "HEAD");
616 int is_branch(const char *refname
)
618 return !strcmp(refname
, "HEAD") || starts_with(refname
, "refs/heads/");
621 struct read_ref_at_cb
{
623 unsigned long at_time
;
629 unsigned char osha1
[20];
630 unsigned char nsha1
[20];
634 unsigned long *cutoff_time
;
639 static int read_ref_at_ent(unsigned char *osha1
, unsigned char *nsha1
,
640 const char *email
, unsigned long timestamp
, int tz
,
641 const char *message
, void *cb_data
)
643 struct read_ref_at_cb
*cb
= cb_data
;
647 cb
->date
= timestamp
;
649 if (timestamp
<= cb
->at_time
|| cb
->cnt
== 0) {
651 *cb
->msg
= xstrdup(message
);
653 *cb
->cutoff_time
= timestamp
;
657 *cb
->cutoff_cnt
= cb
->reccnt
- 1;
659 * we have not yet updated cb->[n|o]sha1 so they still
660 * hold the values for the previous record.
662 if (!is_null_sha1(cb
->osha1
)) {
663 hashcpy(cb
->sha1
, nsha1
);
664 if (hashcmp(cb
->osha1
, nsha1
))
665 warning("Log for ref %s has gap after %s.",
666 cb
->refname
, show_date(cb
->date
, cb
->tz
, DATE_MODE(RFC2822
)));
668 else if (cb
->date
== cb
->at_time
)
669 hashcpy(cb
->sha1
, nsha1
);
670 else if (hashcmp(nsha1
, cb
->sha1
))
671 warning("Log for ref %s unexpectedly ended on %s.",
672 cb
->refname
, show_date(cb
->date
, cb
->tz
,
673 DATE_MODE(RFC2822
)));
674 hashcpy(cb
->osha1
, osha1
);
675 hashcpy(cb
->nsha1
, nsha1
);
679 hashcpy(cb
->osha1
, osha1
);
680 hashcpy(cb
->nsha1
, nsha1
);
686 static int read_ref_at_ent_oldest(unsigned char *osha1
, unsigned char *nsha1
,
687 const char *email
, unsigned long timestamp
,
688 int tz
, const char *message
, void *cb_data
)
690 struct read_ref_at_cb
*cb
= cb_data
;
693 *cb
->msg
= xstrdup(message
);
695 *cb
->cutoff_time
= timestamp
;
699 *cb
->cutoff_cnt
= cb
->reccnt
;
700 hashcpy(cb
->sha1
, osha1
);
701 if (is_null_sha1(cb
->sha1
))
702 hashcpy(cb
->sha1
, nsha1
);
703 /* We just want the first entry */
707 int read_ref_at(const char *refname
, unsigned int flags
, unsigned long at_time
, int cnt
,
708 unsigned char *sha1
, char **msg
,
709 unsigned long *cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
711 struct read_ref_at_cb cb
;
713 memset(&cb
, 0, sizeof(cb
));
714 cb
.refname
= refname
;
715 cb
.at_time
= at_time
;
718 cb
.cutoff_time
= cutoff_time
;
719 cb
.cutoff_tz
= cutoff_tz
;
720 cb
.cutoff_cnt
= cutoff_cnt
;
723 for_each_reflog_ent_reverse(refname
, read_ref_at_ent
, &cb
);
726 if (flags
& GET_SHA1_QUIETLY
)
729 die("Log for %s is empty.", refname
);
734 for_each_reflog_ent(refname
, read_ref_at_ent_oldest
, &cb
);
739 struct ref_transaction
*ref_transaction_begin(struct strbuf
*err
)
743 return xcalloc(1, sizeof(struct ref_transaction
));
746 void ref_transaction_free(struct ref_transaction
*transaction
)
753 for (i
= 0; i
< transaction
->nr
; i
++) {
754 free(transaction
->updates
[i
]->msg
);
755 free(transaction
->updates
[i
]);
757 free(transaction
->updates
);
761 static struct ref_update
*add_update(struct ref_transaction
*transaction
,
764 struct ref_update
*update
;
765 FLEX_ALLOC_STR(update
, refname
, refname
);
766 ALLOC_GROW(transaction
->updates
, transaction
->nr
+ 1, transaction
->alloc
);
767 transaction
->updates
[transaction
->nr
++] = update
;
771 int ref_transaction_update(struct ref_transaction
*transaction
,
773 const unsigned char *new_sha1
,
774 const unsigned char *old_sha1
,
775 unsigned int flags
, const char *msg
,
778 struct ref_update
*update
;
782 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
783 die("BUG: update called for transaction that is not open");
785 if (new_sha1
&& !is_null_sha1(new_sha1
) &&
786 check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
787 strbuf_addf(err
, "refusing to update ref with bad name %s",
792 update
= add_update(transaction
, refname
);
794 hashcpy(update
->new_sha1
, new_sha1
);
795 flags
|= REF_HAVE_NEW
;
798 hashcpy(update
->old_sha1
, old_sha1
);
799 flags
|= REF_HAVE_OLD
;
801 update
->flags
= flags
;
803 update
->msg
= xstrdup(msg
);
807 int ref_transaction_create(struct ref_transaction
*transaction
,
809 const unsigned char *new_sha1
,
810 unsigned int flags
, const char *msg
,
813 if (!new_sha1
|| is_null_sha1(new_sha1
))
814 die("BUG: create called without valid new_sha1");
815 return ref_transaction_update(transaction
, refname
, new_sha1
,
816 null_sha1
, flags
, msg
, err
);
819 int ref_transaction_delete(struct ref_transaction
*transaction
,
821 const unsigned char *old_sha1
,
822 unsigned int flags
, const char *msg
,
825 if (old_sha1
&& is_null_sha1(old_sha1
))
826 die("BUG: delete called with old_sha1 set to zeros");
827 return ref_transaction_update(transaction
, refname
,
832 int ref_transaction_verify(struct ref_transaction
*transaction
,
834 const unsigned char *old_sha1
,
839 die("BUG: verify called with old_sha1 set to NULL");
840 return ref_transaction_update(transaction
, refname
,
845 int update_ref(const char *msg
, const char *refname
,
846 const unsigned char *new_sha1
, const unsigned char *old_sha1
,
847 unsigned int flags
, enum action_on_err onerr
)
849 struct ref_transaction
*t
= NULL
;
850 struct strbuf err
= STRBUF_INIT
;
853 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
) {
854 ret
= write_pseudoref(refname
, new_sha1
, old_sha1
, &err
);
856 t
= ref_transaction_begin(&err
);
858 ref_transaction_update(t
, refname
, new_sha1
, old_sha1
,
860 ref_transaction_commit(t
, &err
)) {
862 ref_transaction_free(t
);
866 const char *str
= "update_ref failed for ref '%s': %s";
869 case UPDATE_REFS_MSG_ON_ERR
:
870 error(str
, refname
, err
.buf
);
872 case UPDATE_REFS_DIE_ON_ERR
:
873 die(str
, refname
, err
.buf
);
875 case UPDATE_REFS_QUIET_ON_ERR
:
878 strbuf_release(&err
);
881 strbuf_release(&err
);
883 ref_transaction_free(t
);
887 char *shorten_unambiguous_ref(const char *refname
, int strict
)
890 static char **scanf_fmts
;
896 * Pre-generate scanf formats from ref_rev_parse_rules[].
897 * Generate a format suitable for scanf from a
898 * ref_rev_parse_rules rule by interpolating "%s" at the
899 * location of the "%.*s".
901 size_t total_len
= 0;
904 /* the rule list is NULL terminated, count them first */
905 for (nr_rules
= 0; ref_rev_parse_rules
[nr_rules
]; nr_rules
++)
906 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
907 total_len
+= strlen(ref_rev_parse_rules
[nr_rules
]) - 2 + 1;
909 scanf_fmts
= xmalloc(st_add(st_mult(nr_rules
, sizeof(char *)), total_len
));
912 for (i
= 0; i
< nr_rules
; i
++) {
913 assert(offset
< total_len
);
914 scanf_fmts
[i
] = (char *)&scanf_fmts
[nr_rules
] + offset
;
915 offset
+= snprintf(scanf_fmts
[i
], total_len
- offset
,
916 ref_rev_parse_rules
[i
], 2, "%s") + 1;
920 /* bail out if there are no rules */
922 return xstrdup(refname
);
924 /* buffer for scanf result, at most refname must fit */
925 short_name
= xstrdup(refname
);
927 /* skip first rule, it will always match */
928 for (i
= nr_rules
- 1; i
> 0 ; --i
) {
930 int rules_to_fail
= i
;
933 if (1 != sscanf(refname
, scanf_fmts
[i
], short_name
))
936 short_name_len
= strlen(short_name
);
939 * in strict mode, all (except the matched one) rules
940 * must fail to resolve to a valid non-ambiguous ref
943 rules_to_fail
= nr_rules
;
946 * check if the short name resolves to a valid ref,
947 * but use only rules prior to the matched one
949 for (j
= 0; j
< rules_to_fail
; j
++) {
950 const char *rule
= ref_rev_parse_rules
[j
];
951 char refname
[PATH_MAX
];
953 /* skip matched rule */
958 * the short name is ambiguous, if it resolves
959 * (with this previous rule) to a valid ref
960 * read_ref() returns 0 on success
962 mksnpath(refname
, sizeof(refname
),
963 rule
, short_name_len
, short_name
);
964 if (ref_exists(refname
))
969 * short name is non-ambiguous if all previous rules
970 * haven't resolved to a valid ref
972 if (j
== rules_to_fail
)
977 return xstrdup(refname
);
980 static struct string_list
*hide_refs
;
982 int parse_hide_refs_config(const char *var
, const char *value
, const char *section
)
984 if (!strcmp("transfer.hiderefs", var
) ||
985 /* NEEDSWORK: use parse_config_key() once both are merged */
986 (starts_with(var
, section
) && var
[strlen(section
)] == '.' &&
987 !strcmp(var
+ strlen(section
), ".hiderefs"))) {
992 return config_error_nonbool(var
);
993 ref
= xstrdup(value
);
995 while (len
&& ref
[len
- 1] == '/')
998 hide_refs
= xcalloc(1, sizeof(*hide_refs
));
999 hide_refs
->strdup_strings
= 1;
1001 string_list_append(hide_refs
, ref
);
1006 int ref_is_hidden(const char *refname
, const char *refname_full
)
1012 for (i
= hide_refs
->nr
- 1; i
>= 0; i
--) {
1013 const char *match
= hide_refs
->items
[i
].string
;
1014 const char *subject
;
1018 if (*match
== '!') {
1023 if (*match
== '^') {
1024 subject
= refname_full
;
1030 /* refname can be NULL when namespaces are used. */
1031 if (!subject
|| !starts_with(subject
, match
))
1033 len
= strlen(match
);
1034 if (!subject
[len
] || subject
[len
] == '/')
1040 const char *find_descendant_ref(const char *dirname
,
1041 const struct string_list
*extras
,
1042 const struct string_list
*skip
)
1050 * Look at the place where dirname would be inserted into
1051 * extras. If there is an entry at that position that starts
1052 * with dirname (remember, dirname includes the trailing
1053 * slash) and is not in skip, then we have a conflict.
1055 for (pos
= string_list_find_insert_index(extras
, dirname
, 0);
1056 pos
< extras
->nr
; pos
++) {
1057 const char *extra_refname
= extras
->items
[pos
].string
;
1059 if (!starts_with(extra_refname
, dirname
))
1062 if (!skip
|| !string_list_has_string(skip
, extra_refname
))
1063 return extra_refname
;
1068 int rename_ref_available(const char *oldname
, const char *newname
)
1070 struct string_list skip
= STRING_LIST_INIT_NODUP
;
1071 struct strbuf err
= STRBUF_INIT
;
1074 string_list_insert(&skip
, oldname
);
1075 ret
= !verify_refname_available(newname
, NULL
, &skip
, &err
);
1077 error("%s", err
.buf
);
1079 string_list_clear(&skip
, 0);
1080 strbuf_release(&err
);