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
)) {
128 size_t restlen
= strlen(rest
);
130 /* rest must not be empty, or start or end with "/" */
131 if (!restlen
|| *rest
== '/' || rest
[restlen
- 1] == '/')
135 * Does the refname try to escape refs/?
136 * For example: refs/foo/../bar is safe but refs/foo/../../bar
139 buf
= xmallocz(restlen
);
140 result
= !normalize_path_copy(buf
, rest
) && !strcmp(buf
, rest
);
146 if (!isupper(*refname
) && *refname
!= '_')
153 char *resolve_refdup(const char *refname
, int resolve_flags
,
154 unsigned char *sha1
, int *flags
)
156 return xstrdup_or_null(resolve_ref_unsafe(refname
, resolve_flags
,
160 /* The argument to filter_refs */
167 int read_ref_full(const char *refname
, int resolve_flags
, unsigned char *sha1
, int *flags
)
169 if (resolve_ref_unsafe(refname
, resolve_flags
, sha1
, flags
))
174 int read_ref(const char *refname
, unsigned char *sha1
)
176 return read_ref_full(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
179 int ref_exists(const char *refname
)
181 unsigned char sha1
[20];
182 return !!resolve_ref_unsafe(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
185 static int filter_refs(const char *refname
, const struct object_id
*oid
,
186 int flags
, void *data
)
188 struct ref_filter
*filter
= (struct ref_filter
*)data
;
190 if (wildmatch(filter
->pattern
, refname
, 0, NULL
))
192 return filter
->fn(refname
, oid
, flags
, filter
->cb_data
);
195 enum peel_status
peel_object(const unsigned char *name
, unsigned char *sha1
)
197 struct object
*o
= lookup_unknown_object(name
);
199 if (o
->type
== OBJ_NONE
) {
200 int type
= sha1_object_info(name
, NULL
);
201 if (type
< 0 || !object_as_type(o
, type
, 0))
205 if (o
->type
!= OBJ_TAG
)
208 o
= deref_tag_noverify(o
);
212 hashcpy(sha1
, o
->oid
.hash
);
216 struct warn_if_dangling_data
{
219 const struct string_list
*refnames
;
223 static int warn_if_dangling_symref(const char *refname
, const struct object_id
*oid
,
224 int flags
, void *cb_data
)
226 struct warn_if_dangling_data
*d
= cb_data
;
227 const char *resolves_to
;
228 struct object_id junk
;
230 if (!(flags
& REF_ISSYMREF
))
233 resolves_to
= resolve_ref_unsafe(refname
, 0, junk
.hash
, NULL
);
236 ? strcmp(resolves_to
, d
->refname
)
237 : !string_list_has_string(d
->refnames
, resolves_to
))) {
241 fprintf(d
->fp
, d
->msg_fmt
, refname
);
246 void warn_dangling_symref(FILE *fp
, const char *msg_fmt
, const char *refname
)
248 struct warn_if_dangling_data data
;
251 data
.refname
= refname
;
252 data
.refnames
= NULL
;
253 data
.msg_fmt
= msg_fmt
;
254 for_each_rawref(warn_if_dangling_symref
, &data
);
257 void warn_dangling_symrefs(FILE *fp
, const char *msg_fmt
, const struct string_list
*refnames
)
259 struct warn_if_dangling_data data
;
263 data
.refnames
= refnames
;
264 data
.msg_fmt
= msg_fmt
;
265 for_each_rawref(warn_if_dangling_symref
, &data
);
268 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
270 return for_each_ref_in("refs/tags/", fn
, cb_data
);
273 int for_each_tag_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
275 return for_each_ref_in_submodule(submodule
, "refs/tags/", fn
, cb_data
);
278 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
280 return for_each_ref_in("refs/heads/", fn
, cb_data
);
283 int for_each_branch_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
285 return for_each_ref_in_submodule(submodule
, "refs/heads/", fn
, cb_data
);
288 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
290 return for_each_ref_in("refs/remotes/", fn
, cb_data
);
293 int for_each_remote_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
295 return for_each_ref_in_submodule(submodule
, "refs/remotes/", fn
, cb_data
);
298 int head_ref_namespaced(each_ref_fn fn
, void *cb_data
)
300 struct strbuf buf
= STRBUF_INIT
;
302 struct object_id oid
;
305 strbuf_addf(&buf
, "%sHEAD", get_git_namespace());
306 if (!read_ref_full(buf
.buf
, RESOLVE_REF_READING
, oid
.hash
, &flag
))
307 ret
= fn(buf
.buf
, &oid
, flag
, cb_data
);
308 strbuf_release(&buf
);
313 int for_each_glob_ref_in(each_ref_fn fn
, const char *pattern
,
314 const char *prefix
, void *cb_data
)
316 struct strbuf real_pattern
= STRBUF_INIT
;
317 struct ref_filter filter
;
320 if (!prefix
&& !starts_with(pattern
, "refs/"))
321 strbuf_addstr(&real_pattern
, "refs/");
323 strbuf_addstr(&real_pattern
, prefix
);
324 strbuf_addstr(&real_pattern
, pattern
);
326 if (!has_glob_specials(pattern
)) {
327 /* Append implied '/' '*' if not present. */
328 strbuf_complete(&real_pattern
, '/');
329 /* No need to check for '*', there is none. */
330 strbuf_addch(&real_pattern
, '*');
333 filter
.pattern
= real_pattern
.buf
;
335 filter
.cb_data
= cb_data
;
336 ret
= for_each_ref(filter_refs
, &filter
);
338 strbuf_release(&real_pattern
);
342 int for_each_glob_ref(each_ref_fn fn
, const char *pattern
, void *cb_data
)
344 return for_each_glob_ref_in(fn
, pattern
, NULL
, cb_data
);
347 const char *prettify_refname(const char *name
)
350 starts_with(name
, "refs/heads/") ? 11 :
351 starts_with(name
, "refs/tags/") ? 10 :
352 starts_with(name
, "refs/remotes/") ? 13 :
356 static const char *ref_rev_parse_rules
[] = {
362 "refs/remotes/%.*s/HEAD",
366 int refname_match(const char *abbrev_name
, const char *full_name
)
369 const int abbrev_name_len
= strlen(abbrev_name
);
371 for (p
= ref_rev_parse_rules
; *p
; p
++) {
372 if (!strcmp(full_name
, mkpath(*p
, abbrev_name_len
, abbrev_name
))) {
381 * *string and *len will only be substituted, and *string returned (for
382 * later free()ing) if the string passed in is a magic short-hand form
385 static char *substitute_branch_name(const char **string
, int *len
)
387 struct strbuf buf
= STRBUF_INIT
;
388 int ret
= interpret_branch_name(*string
, *len
, &buf
);
392 *string
= strbuf_detach(&buf
, &size
);
394 return (char *)*string
;
400 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
402 char *last_branch
= substitute_branch_name(&str
, &len
);
407 for (p
= ref_rev_parse_rules
; *p
; p
++) {
408 char fullref
[PATH_MAX
];
409 unsigned char sha1_from_ref
[20];
410 unsigned char *this_result
;
413 this_result
= refs_found
? sha1_from_ref
: sha1
;
414 mksnpath(fullref
, sizeof(fullref
), *p
, len
, str
);
415 r
= resolve_ref_unsafe(fullref
, RESOLVE_REF_READING
,
420 if (!warn_ambiguous_refs
)
422 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
, "HEAD")) {
423 warning("ignoring dangling symref %s.", fullref
);
424 } else if ((flag
& REF_ISBROKEN
) && strchr(fullref
, '/')) {
425 warning("ignoring broken ref %s.", fullref
);
432 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
434 char *last_branch
= substitute_branch_name(&str
, &len
);
439 for (p
= ref_rev_parse_rules
; *p
; p
++) {
440 unsigned char hash
[20];
442 const char *ref
, *it
;
444 mksnpath(path
, sizeof(path
), *p
, len
, str
);
445 ref
= resolve_ref_unsafe(path
, RESOLVE_REF_READING
,
449 if (reflog_exists(path
))
451 else if (strcmp(ref
, path
) && reflog_exists(ref
))
459 if (!warn_ambiguous_refs
)
466 static int is_per_worktree_ref(const char *refname
)
468 return !strcmp(refname
, "HEAD") ||
469 starts_with(refname
, "refs/bisect/");
472 static int is_pseudoref_syntax(const char *refname
)
476 for (c
= refname
; *c
; c
++) {
477 if (!isupper(*c
) && *c
!= '-' && *c
!= '_')
484 enum ref_type
ref_type(const char *refname
)
486 if (is_per_worktree_ref(refname
))
487 return REF_TYPE_PER_WORKTREE
;
488 if (is_pseudoref_syntax(refname
))
489 return REF_TYPE_PSEUDOREF
;
490 return REF_TYPE_NORMAL
;
493 static int write_pseudoref(const char *pseudoref
, const unsigned char *sha1
,
494 const unsigned char *old_sha1
, struct strbuf
*err
)
496 const char *filename
;
498 static struct lock_file lock
;
499 struct strbuf buf
= STRBUF_INIT
;
502 strbuf_addf(&buf
, "%s\n", sha1_to_hex(sha1
));
504 filename
= git_path("%s", pseudoref
);
505 fd
= hold_lock_file_for_update(&lock
, filename
, LOCK_DIE_ON_ERROR
);
507 strbuf_addf(err
, "could not open '%s' for writing: %s",
508 filename
, strerror(errno
));
513 unsigned char actual_old_sha1
[20];
515 if (read_ref(pseudoref
, actual_old_sha1
))
516 die("could not read ref '%s'", pseudoref
);
517 if (hashcmp(actual_old_sha1
, old_sha1
)) {
518 strbuf_addf(err
, "unexpected sha1 when writing '%s'", pseudoref
);
519 rollback_lock_file(&lock
);
524 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
) {
525 strbuf_addf(err
, "could not write to '%s'", filename
);
526 rollback_lock_file(&lock
);
530 commit_lock_file(&lock
);
533 strbuf_release(&buf
);
537 static int delete_pseudoref(const char *pseudoref
, const unsigned char *old_sha1
)
539 static struct lock_file lock
;
540 const char *filename
;
542 filename
= git_path("%s", pseudoref
);
544 if (old_sha1
&& !is_null_sha1(old_sha1
)) {
546 unsigned char actual_old_sha1
[20];
548 fd
= hold_lock_file_for_update(&lock
, filename
,
551 die_errno(_("Could not open '%s' for writing"), filename
);
552 if (read_ref(pseudoref
, actual_old_sha1
))
553 die("could not read ref '%s'", pseudoref
);
554 if (hashcmp(actual_old_sha1
, old_sha1
)) {
555 warning("Unexpected sha1 when deleting %s", pseudoref
);
556 rollback_lock_file(&lock
);
561 rollback_lock_file(&lock
);
569 int delete_ref(const char *refname
, const unsigned char *old_sha1
,
572 struct ref_transaction
*transaction
;
573 struct strbuf err
= STRBUF_INIT
;
575 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
)
576 return delete_pseudoref(refname
, old_sha1
);
578 transaction
= ref_transaction_begin(&err
);
580 ref_transaction_delete(transaction
, refname
, old_sha1
,
581 flags
, NULL
, &err
) ||
582 ref_transaction_commit(transaction
, &err
)) {
583 error("%s", err
.buf
);
584 ref_transaction_free(transaction
);
585 strbuf_release(&err
);
588 ref_transaction_free(transaction
);
589 strbuf_release(&err
);
593 int copy_reflog_msg(char *buf
, const char *msg
)
600 while ((c
= *msg
++)) {
601 if (wasspace
&& isspace(c
))
603 wasspace
= isspace(c
);
608 while (buf
< cp
&& isspace(cp
[-1]))
614 int should_autocreate_reflog(const char *refname
)
616 if (!log_all_ref_updates
)
618 return starts_with(refname
, "refs/heads/") ||
619 starts_with(refname
, "refs/remotes/") ||
620 starts_with(refname
, "refs/notes/") ||
621 !strcmp(refname
, "HEAD");
624 int is_branch(const char *refname
)
626 return !strcmp(refname
, "HEAD") || starts_with(refname
, "refs/heads/");
629 struct read_ref_at_cb
{
631 unsigned long at_time
;
637 unsigned char osha1
[20];
638 unsigned char nsha1
[20];
642 unsigned long *cutoff_time
;
647 static int read_ref_at_ent(unsigned char *osha1
, unsigned char *nsha1
,
648 const char *email
, unsigned long timestamp
, int tz
,
649 const char *message
, void *cb_data
)
651 struct read_ref_at_cb
*cb
= cb_data
;
655 cb
->date
= timestamp
;
657 if (timestamp
<= cb
->at_time
|| cb
->cnt
== 0) {
659 *cb
->msg
= xstrdup(message
);
661 *cb
->cutoff_time
= timestamp
;
665 *cb
->cutoff_cnt
= cb
->reccnt
- 1;
667 * we have not yet updated cb->[n|o]sha1 so they still
668 * hold the values for the previous record.
670 if (!is_null_sha1(cb
->osha1
)) {
671 hashcpy(cb
->sha1
, nsha1
);
672 if (hashcmp(cb
->osha1
, nsha1
))
673 warning("Log for ref %s has gap after %s.",
674 cb
->refname
, show_date(cb
->date
, cb
->tz
, DATE_MODE(RFC2822
)));
676 else if (cb
->date
== cb
->at_time
)
677 hashcpy(cb
->sha1
, nsha1
);
678 else if (hashcmp(nsha1
, cb
->sha1
))
679 warning("Log for ref %s unexpectedly ended on %s.",
680 cb
->refname
, show_date(cb
->date
, cb
->tz
,
681 DATE_MODE(RFC2822
)));
682 hashcpy(cb
->osha1
, osha1
);
683 hashcpy(cb
->nsha1
, nsha1
);
687 hashcpy(cb
->osha1
, osha1
);
688 hashcpy(cb
->nsha1
, nsha1
);
694 static int read_ref_at_ent_oldest(unsigned char *osha1
, unsigned char *nsha1
,
695 const char *email
, unsigned long timestamp
,
696 int tz
, const char *message
, void *cb_data
)
698 struct read_ref_at_cb
*cb
= cb_data
;
701 *cb
->msg
= xstrdup(message
);
703 *cb
->cutoff_time
= timestamp
;
707 *cb
->cutoff_cnt
= cb
->reccnt
;
708 hashcpy(cb
->sha1
, osha1
);
709 if (is_null_sha1(cb
->sha1
))
710 hashcpy(cb
->sha1
, nsha1
);
711 /* We just want the first entry */
715 int read_ref_at(const char *refname
, unsigned int flags
, unsigned long at_time
, int cnt
,
716 unsigned char *sha1
, char **msg
,
717 unsigned long *cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
719 struct read_ref_at_cb cb
;
721 memset(&cb
, 0, sizeof(cb
));
722 cb
.refname
= refname
;
723 cb
.at_time
= at_time
;
726 cb
.cutoff_time
= cutoff_time
;
727 cb
.cutoff_tz
= cutoff_tz
;
728 cb
.cutoff_cnt
= cutoff_cnt
;
731 for_each_reflog_ent_reverse(refname
, read_ref_at_ent
, &cb
);
734 if (flags
& GET_SHA1_QUIETLY
)
737 die("Log for %s is empty.", refname
);
742 for_each_reflog_ent(refname
, read_ref_at_ent_oldest
, &cb
);
747 struct ref_transaction
*ref_transaction_begin(struct strbuf
*err
)
751 return xcalloc(1, sizeof(struct ref_transaction
));
754 void ref_transaction_free(struct ref_transaction
*transaction
)
761 for (i
= 0; i
< transaction
->nr
; i
++) {
762 free(transaction
->updates
[i
]->msg
);
763 free(transaction
->updates
[i
]);
765 free(transaction
->updates
);
769 struct ref_update
*ref_transaction_add_update(
770 struct ref_transaction
*transaction
,
771 const char *refname
, unsigned int flags
,
772 const unsigned char *new_sha1
,
773 const unsigned char *old_sha1
,
776 struct ref_update
*update
;
778 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
779 die("BUG: update called for transaction that is not open");
781 if ((flags
& REF_ISPRUNING
) && !(flags
& REF_NODEREF
))
782 die("BUG: REF_ISPRUNING set without REF_NODEREF");
784 FLEX_ALLOC_STR(update
, refname
, refname
);
785 ALLOC_GROW(transaction
->updates
, transaction
->nr
+ 1, transaction
->alloc
);
786 transaction
->updates
[transaction
->nr
++] = update
;
788 update
->flags
= flags
;
790 if (flags
& REF_HAVE_NEW
)
791 hashcpy(update
->new_sha1
, new_sha1
);
792 if (flags
& REF_HAVE_OLD
)
793 hashcpy(update
->old_sha1
, old_sha1
);
795 update
->msg
= xstrdup(msg
);
799 int ref_transaction_update(struct ref_transaction
*transaction
,
801 const unsigned char *new_sha1
,
802 const unsigned char *old_sha1
,
803 unsigned int flags
, const char *msg
,
808 if ((new_sha1
&& !is_null_sha1(new_sha1
)) ?
809 check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
) :
810 !refname_is_safe(refname
)) {
811 strbuf_addf(err
, "refusing to update ref with bad name '%s'",
816 flags
|= (new_sha1
? REF_HAVE_NEW
: 0) | (old_sha1
? REF_HAVE_OLD
: 0);
818 ref_transaction_add_update(transaction
, refname
, flags
,
819 new_sha1
, old_sha1
, msg
);
823 int ref_transaction_create(struct ref_transaction
*transaction
,
825 const unsigned char *new_sha1
,
826 unsigned int flags
, const char *msg
,
829 if (!new_sha1
|| is_null_sha1(new_sha1
))
830 die("BUG: create called without valid new_sha1");
831 return ref_transaction_update(transaction
, refname
, new_sha1
,
832 null_sha1
, flags
, msg
, err
);
835 int ref_transaction_delete(struct ref_transaction
*transaction
,
837 const unsigned char *old_sha1
,
838 unsigned int flags
, const char *msg
,
841 if (old_sha1
&& is_null_sha1(old_sha1
))
842 die("BUG: delete called with old_sha1 set to zeros");
843 return ref_transaction_update(transaction
, refname
,
848 int ref_transaction_verify(struct ref_transaction
*transaction
,
850 const unsigned char *old_sha1
,
855 die("BUG: verify called with old_sha1 set to NULL");
856 return ref_transaction_update(transaction
, refname
,
861 int update_ref(const char *msg
, const char *refname
,
862 const unsigned char *new_sha1
, const unsigned char *old_sha1
,
863 unsigned int flags
, enum action_on_err onerr
)
865 struct ref_transaction
*t
= NULL
;
866 struct strbuf err
= STRBUF_INIT
;
869 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
) {
870 ret
= write_pseudoref(refname
, new_sha1
, old_sha1
, &err
);
872 t
= ref_transaction_begin(&err
);
874 ref_transaction_update(t
, refname
, new_sha1
, old_sha1
,
876 ref_transaction_commit(t
, &err
)) {
878 ref_transaction_free(t
);
882 const char *str
= "update_ref failed for ref '%s': %s";
885 case UPDATE_REFS_MSG_ON_ERR
:
886 error(str
, refname
, err
.buf
);
888 case UPDATE_REFS_DIE_ON_ERR
:
889 die(str
, refname
, err
.buf
);
891 case UPDATE_REFS_QUIET_ON_ERR
:
894 strbuf_release(&err
);
897 strbuf_release(&err
);
899 ref_transaction_free(t
);
903 char *shorten_unambiguous_ref(const char *refname
, int strict
)
906 static char **scanf_fmts
;
912 * Pre-generate scanf formats from ref_rev_parse_rules[].
913 * Generate a format suitable for scanf from a
914 * ref_rev_parse_rules rule by interpolating "%s" at the
915 * location of the "%.*s".
917 size_t total_len
= 0;
920 /* the rule list is NULL terminated, count them first */
921 for (nr_rules
= 0; ref_rev_parse_rules
[nr_rules
]; nr_rules
++)
922 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
923 total_len
+= strlen(ref_rev_parse_rules
[nr_rules
]) - 2 + 1;
925 scanf_fmts
= xmalloc(st_add(st_mult(nr_rules
, sizeof(char *)), total_len
));
928 for (i
= 0; i
< nr_rules
; i
++) {
929 assert(offset
< total_len
);
930 scanf_fmts
[i
] = (char *)&scanf_fmts
[nr_rules
] + offset
;
931 offset
+= snprintf(scanf_fmts
[i
], total_len
- offset
,
932 ref_rev_parse_rules
[i
], 2, "%s") + 1;
936 /* bail out if there are no rules */
938 return xstrdup(refname
);
940 /* buffer for scanf result, at most refname must fit */
941 short_name
= xstrdup(refname
);
943 /* skip first rule, it will always match */
944 for (i
= nr_rules
- 1; i
> 0 ; --i
) {
946 int rules_to_fail
= i
;
949 if (1 != sscanf(refname
, scanf_fmts
[i
], short_name
))
952 short_name_len
= strlen(short_name
);
955 * in strict mode, all (except the matched one) rules
956 * must fail to resolve to a valid non-ambiguous ref
959 rules_to_fail
= nr_rules
;
962 * check if the short name resolves to a valid ref,
963 * but use only rules prior to the matched one
965 for (j
= 0; j
< rules_to_fail
; j
++) {
966 const char *rule
= ref_rev_parse_rules
[j
];
967 char refname
[PATH_MAX
];
969 /* skip matched rule */
974 * the short name is ambiguous, if it resolves
975 * (with this previous rule) to a valid ref
976 * read_ref() returns 0 on success
978 mksnpath(refname
, sizeof(refname
),
979 rule
, short_name_len
, short_name
);
980 if (ref_exists(refname
))
985 * short name is non-ambiguous if all previous rules
986 * haven't resolved to a valid ref
988 if (j
== rules_to_fail
)
993 return xstrdup(refname
);
996 static struct string_list
*hide_refs
;
998 int parse_hide_refs_config(const char *var
, const char *value
, const char *section
)
1000 if (!strcmp("transfer.hiderefs", var
) ||
1001 /* NEEDSWORK: use parse_config_key() once both are merged */
1002 (starts_with(var
, section
) && var
[strlen(section
)] == '.' &&
1003 !strcmp(var
+ strlen(section
), ".hiderefs"))) {
1008 return config_error_nonbool(var
);
1009 ref
= xstrdup(value
);
1011 while (len
&& ref
[len
- 1] == '/')
1014 hide_refs
= xcalloc(1, sizeof(*hide_refs
));
1015 hide_refs
->strdup_strings
= 1;
1017 string_list_append(hide_refs
, ref
);
1022 int ref_is_hidden(const char *refname
, const char *refname_full
)
1028 for (i
= hide_refs
->nr
- 1; i
>= 0; i
--) {
1029 const char *match
= hide_refs
->items
[i
].string
;
1030 const char *subject
;
1034 if (*match
== '!') {
1039 if (*match
== '^') {
1040 subject
= refname_full
;
1046 /* refname can be NULL when namespaces are used. */
1047 if (!subject
|| !starts_with(subject
, match
))
1049 len
= strlen(match
);
1050 if (!subject
[len
] || subject
[len
] == '/')
1056 const char *find_descendant_ref(const char *dirname
,
1057 const struct string_list
*extras
,
1058 const struct string_list
*skip
)
1066 * Look at the place where dirname would be inserted into
1067 * extras. If there is an entry at that position that starts
1068 * with dirname (remember, dirname includes the trailing
1069 * slash) and is not in skip, then we have a conflict.
1071 for (pos
= string_list_find_insert_index(extras
, dirname
, 0);
1072 pos
< extras
->nr
; pos
++) {
1073 const char *extra_refname
= extras
->items
[pos
].string
;
1075 if (!starts_with(extra_refname
, dirname
))
1078 if (!skip
|| !string_list_has_string(skip
, extra_refname
))
1079 return extra_refname
;
1084 int rename_ref_available(const char *oldname
, const char *newname
)
1086 struct string_list skip
= STRING_LIST_INIT_NODUP
;
1087 struct strbuf err
= STRBUF_INIT
;
1090 string_list_insert(&skip
, oldname
);
1091 ret
= !verify_refname_available(newname
, NULL
, &skip
, &err
);
1093 error("%s", err
.buf
);
1095 string_list_clear(&skip
, 0);
1096 strbuf_release(&err
);
1100 int head_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1102 struct object_id oid
;
1106 if (resolve_gitlink_ref(submodule
, "HEAD", oid
.hash
) == 0)
1107 return fn("HEAD", &oid
, 0, cb_data
);
1112 if (!read_ref_full("HEAD", RESOLVE_REF_READING
, oid
.hash
, &flag
))
1113 return fn("HEAD", &oid
, flag
, cb_data
);
1118 int head_ref(each_ref_fn fn
, void *cb_data
)
1120 return head_ref_submodule(NULL
, fn
, cb_data
);
1123 int for_each_ref(each_ref_fn fn
, void *cb_data
)
1125 return do_for_each_ref(NULL
, "", fn
, 0, 0, cb_data
);
1128 int for_each_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1130 return do_for_each_ref(submodule
, "", fn
, 0, 0, cb_data
);
1133 int for_each_ref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
)
1135 return do_for_each_ref(NULL
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1138 int for_each_fullref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
, unsigned int broken
)
1140 unsigned int flag
= 0;
1143 flag
= DO_FOR_EACH_INCLUDE_BROKEN
;
1144 return do_for_each_ref(NULL
, prefix
, fn
, 0, flag
, cb_data
);
1147 int for_each_ref_in_submodule(const char *submodule
, const char *prefix
,
1148 each_ref_fn fn
, void *cb_data
)
1150 return do_for_each_ref(submodule
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1153 int for_each_replace_ref(each_ref_fn fn
, void *cb_data
)
1155 return do_for_each_ref(NULL
, git_replace_ref_base
, fn
,
1156 strlen(git_replace_ref_base
), 0, cb_data
);
1159 int for_each_namespaced_ref(each_ref_fn fn
, void *cb_data
)
1161 struct strbuf buf
= STRBUF_INIT
;
1163 strbuf_addf(&buf
, "%srefs/", get_git_namespace());
1164 ret
= do_for_each_ref(NULL
, buf
.buf
, fn
, 0, 0, cb_data
);
1165 strbuf_release(&buf
);
1169 int for_each_rawref(each_ref_fn fn
, void *cb_data
)
1171 return do_for_each_ref(NULL
, "", fn
, 0,
1172 DO_FOR_EACH_INCLUDE_BROKEN
, cb_data
);
1175 /* This function needs to return a meaningful errno on failure */
1176 const char *resolve_ref_unsafe(const char *refname
, int resolve_flags
,
1177 unsigned char *sha1
, int *flags
)
1179 static struct strbuf sb_refname
= STRBUF_INIT
;
1184 flags
= &unused_flags
;
1188 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1189 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1190 !refname_is_safe(refname
)) {
1196 * dwim_ref() uses REF_ISBROKEN to distinguish between
1197 * missing refs and refs that were present but invalid,
1198 * to complain about the latter to stderr.
1200 * We don't know whether the ref exists, so don't set
1203 *flags
|= REF_BAD_NAME
;
1206 for (symref_count
= 0; symref_count
< SYMREF_MAXDEPTH
; symref_count
++) {
1207 unsigned int read_flags
= 0;
1209 if (read_raw_ref(refname
, sha1
, &sb_refname
, &read_flags
)) {
1210 *flags
|= read_flags
;
1211 if (errno
!= ENOENT
|| (resolve_flags
& RESOLVE_REF_READING
))
1214 if (*flags
& REF_BAD_NAME
)
1215 *flags
|= REF_ISBROKEN
;
1219 *flags
|= read_flags
;
1221 if (!(read_flags
& REF_ISSYMREF
)) {
1222 if (*flags
& REF_BAD_NAME
) {
1224 *flags
|= REF_ISBROKEN
;
1229 refname
= sb_refname
.buf
;
1230 if (resolve_flags
& RESOLVE_REF_NO_RECURSE
) {
1234 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1235 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1236 !refname_is_safe(refname
)) {
1241 *flags
|= REF_ISBROKEN
| REF_BAD_NAME
;