2 * The backend-independent part of the reference module.
9 #include "refs/refs-internal.h"
14 * List of all available backends
16 static struct ref_storage_be
*refs_backends
= &refs_be_files
;
18 static struct ref_storage_be
*find_ref_storage_backend(const char *name
)
20 struct ref_storage_be
*be
;
21 for (be
= refs_backends
; be
; be
= be
->next
)
22 if (!strcmp(be
->name
, name
))
27 int ref_storage_backend_exists(const char *name
)
29 return find_ref_storage_backend(name
) != NULL
;
33 * How to handle various characters in refnames:
34 * 0: An acceptable character for refs
36 * 2: ., look for a preceding . to reject .. in refs
37 * 3: {, look for a preceding @ to reject @{ in refs
38 * 4: A bad character: ASCII control characters, and
39 * ":", "?", "[", "\", "^", "~", SP, or TAB
40 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
42 static unsigned char refname_disposition
[256] = {
43 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
44 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
45 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
54 * Try to read one refname component from the front of refname.
55 * Return the length of the component found, or -1 if the component is
56 * not legal. It is legal if it is something reasonable to have under
57 * ".git/refs/"; We do not like it if:
59 * - any path component of it begins with ".", or
60 * - it has double dots "..", or
61 * - it has ASCII control characters, or
62 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
63 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
64 * - it ends with a "/", or
65 * - it ends with ".lock", or
66 * - it contains a "@{" portion
68 static int check_refname_component(const char *refname
, int *flags
)
73 for (cp
= refname
; ; cp
++) {
75 unsigned char disp
= refname_disposition
[ch
];
81 return -1; /* Refname contains "..". */
85 return -1; /* Refname contains "@{". */
90 if (!(*flags
& REFNAME_REFSPEC_PATTERN
))
91 return -1; /* refspec can't be a pattern */
94 * Unset the pattern flag so that we only accept
95 * a single asterisk for one side of refspec.
97 *flags
&= ~ REFNAME_REFSPEC_PATTERN
;
104 return 0; /* Component has zero length. */
105 if (refname
[0] == '.')
106 return -1; /* Component starts with '.'. */
107 if (cp
- refname
>= LOCK_SUFFIX_LEN
&&
108 !memcmp(cp
- LOCK_SUFFIX_LEN
, LOCK_SUFFIX
, LOCK_SUFFIX_LEN
))
109 return -1; /* Refname ends with ".lock". */
113 int check_refname_format(const char *refname
, int flags
)
115 int component_len
, component_count
= 0;
117 if (!strcmp(refname
, "@"))
118 /* Refname is a single character '@'. */
122 /* We are at the start of a path component. */
123 component_len
= check_refname_component(refname
, &flags
);
124 if (component_len
<= 0)
128 if (refname
[component_len
] == '\0')
130 /* Skip to next component. */
131 refname
+= component_len
+ 1;
134 if (refname
[component_len
- 1] == '.')
135 return -1; /* Refname ends with '.'. */
136 if (!(flags
& REFNAME_ALLOW_ONELEVEL
) && component_count
< 2)
137 return -1; /* Refname has only one component. */
141 int refname_is_safe(const char *refname
)
145 if (skip_prefix(refname
, "refs/", &rest
)) {
148 size_t restlen
= strlen(rest
);
150 /* rest must not be empty, or start or end with "/" */
151 if (!restlen
|| *rest
== '/' || rest
[restlen
- 1] == '/')
155 * Does the refname try to escape refs/?
156 * For example: refs/foo/../bar is safe but refs/foo/../../bar
159 buf
= xmallocz(restlen
);
160 result
= !normalize_path_copy(buf
, rest
) && !strcmp(buf
, rest
);
166 if (!isupper(*refname
) && *refname
!= '_')
173 char *resolve_refdup(const char *refname
, int resolve_flags
,
174 unsigned char *sha1
, int *flags
)
176 return xstrdup_or_null(resolve_ref_unsafe(refname
, resolve_flags
,
180 /* The argument to filter_refs */
187 int read_ref_full(const char *refname
, int resolve_flags
, unsigned char *sha1
, int *flags
)
189 if (resolve_ref_unsafe(refname
, resolve_flags
, sha1
, flags
))
194 int read_ref(const char *refname
, unsigned char *sha1
)
196 return read_ref_full(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
199 int ref_exists(const char *refname
)
201 unsigned char sha1
[20];
202 return !!resolve_ref_unsafe(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
205 static int filter_refs(const char *refname
, const struct object_id
*oid
,
206 int flags
, void *data
)
208 struct ref_filter
*filter
= (struct ref_filter
*)data
;
210 if (wildmatch(filter
->pattern
, refname
, 0, NULL
))
212 return filter
->fn(refname
, oid
, flags
, filter
->cb_data
);
215 enum peel_status
peel_object(const unsigned char *name
, unsigned char *sha1
)
217 struct object
*o
= lookup_unknown_object(name
);
219 if (o
->type
== OBJ_NONE
) {
220 int type
= sha1_object_info(name
, NULL
);
221 if (type
< 0 || !object_as_type(o
, type
, 0))
225 if (o
->type
!= OBJ_TAG
)
228 o
= deref_tag_noverify(o
);
232 hashcpy(sha1
, o
->oid
.hash
);
236 struct warn_if_dangling_data
{
239 const struct string_list
*refnames
;
243 static int warn_if_dangling_symref(const char *refname
, const struct object_id
*oid
,
244 int flags
, void *cb_data
)
246 struct warn_if_dangling_data
*d
= cb_data
;
247 const char *resolves_to
;
248 struct object_id junk
;
250 if (!(flags
& REF_ISSYMREF
))
253 resolves_to
= resolve_ref_unsafe(refname
, 0, junk
.hash
, NULL
);
256 ? strcmp(resolves_to
, d
->refname
)
257 : !string_list_has_string(d
->refnames
, resolves_to
))) {
261 fprintf(d
->fp
, d
->msg_fmt
, refname
);
266 void warn_dangling_symref(FILE *fp
, const char *msg_fmt
, const char *refname
)
268 struct warn_if_dangling_data data
;
271 data
.refname
= refname
;
272 data
.refnames
= NULL
;
273 data
.msg_fmt
= msg_fmt
;
274 for_each_rawref(warn_if_dangling_symref
, &data
);
277 void warn_dangling_symrefs(FILE *fp
, const char *msg_fmt
, const struct string_list
*refnames
)
279 struct warn_if_dangling_data data
;
283 data
.refnames
= refnames
;
284 data
.msg_fmt
= msg_fmt
;
285 for_each_rawref(warn_if_dangling_symref
, &data
);
288 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
290 return for_each_ref_in("refs/tags/", fn
, cb_data
);
293 int for_each_tag_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
295 return for_each_ref_in_submodule(submodule
, "refs/tags/", fn
, cb_data
);
298 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
300 return for_each_ref_in("refs/heads/", fn
, cb_data
);
303 int for_each_branch_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
305 return for_each_ref_in_submodule(submodule
, "refs/heads/", fn
, cb_data
);
308 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
310 return for_each_ref_in("refs/remotes/", fn
, cb_data
);
313 int for_each_remote_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
315 return for_each_ref_in_submodule(submodule
, "refs/remotes/", fn
, cb_data
);
318 int head_ref_namespaced(each_ref_fn fn
, void *cb_data
)
320 struct strbuf buf
= STRBUF_INIT
;
322 struct object_id oid
;
325 strbuf_addf(&buf
, "%sHEAD", get_git_namespace());
326 if (!read_ref_full(buf
.buf
, RESOLVE_REF_READING
, oid
.hash
, &flag
))
327 ret
= fn(buf
.buf
, &oid
, flag
, cb_data
);
328 strbuf_release(&buf
);
333 int for_each_glob_ref_in(each_ref_fn fn
, const char *pattern
,
334 const char *prefix
, void *cb_data
)
336 struct strbuf real_pattern
= STRBUF_INIT
;
337 struct ref_filter filter
;
340 if (!prefix
&& !starts_with(pattern
, "refs/"))
341 strbuf_addstr(&real_pattern
, "refs/");
343 strbuf_addstr(&real_pattern
, prefix
);
344 strbuf_addstr(&real_pattern
, pattern
);
346 if (!has_glob_specials(pattern
)) {
347 /* Append implied '/' '*' if not present. */
348 strbuf_complete(&real_pattern
, '/');
349 /* No need to check for '*', there is none. */
350 strbuf_addch(&real_pattern
, '*');
353 filter
.pattern
= real_pattern
.buf
;
355 filter
.cb_data
= cb_data
;
356 ret
= for_each_ref(filter_refs
, &filter
);
358 strbuf_release(&real_pattern
);
362 int for_each_glob_ref(each_ref_fn fn
, const char *pattern
, void *cb_data
)
364 return for_each_glob_ref_in(fn
, pattern
, NULL
, cb_data
);
367 const char *prettify_refname(const char *name
)
370 starts_with(name
, "refs/heads/") ? 11 :
371 starts_with(name
, "refs/tags/") ? 10 :
372 starts_with(name
, "refs/remotes/") ? 13 :
376 static const char *ref_rev_parse_rules
[] = {
382 "refs/remotes/%.*s/HEAD",
386 int refname_match(const char *abbrev_name
, const char *full_name
)
389 const int abbrev_name_len
= strlen(abbrev_name
);
391 for (p
= ref_rev_parse_rules
; *p
; p
++) {
392 if (!strcmp(full_name
, mkpath(*p
, abbrev_name_len
, abbrev_name
))) {
401 * *string and *len will only be substituted, and *string returned (for
402 * later free()ing) if the string passed in is a magic short-hand form
405 static char *substitute_branch_name(const char **string
, int *len
)
407 struct strbuf buf
= STRBUF_INIT
;
408 int ret
= interpret_branch_name(*string
, *len
, &buf
);
412 *string
= strbuf_detach(&buf
, &size
);
414 return (char *)*string
;
420 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
422 char *last_branch
= substitute_branch_name(&str
, &len
);
423 int refs_found
= expand_ref(str
, len
, sha1
, ref
);
428 int expand_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
434 for (p
= ref_rev_parse_rules
; *p
; p
++) {
435 char fullref
[PATH_MAX
];
436 unsigned char sha1_from_ref
[20];
437 unsigned char *this_result
;
440 this_result
= refs_found
? sha1_from_ref
: sha1
;
441 mksnpath(fullref
, sizeof(fullref
), *p
, len
, str
);
442 r
= resolve_ref_unsafe(fullref
, RESOLVE_REF_READING
,
447 if (!warn_ambiguous_refs
)
449 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
, "HEAD")) {
450 warning("ignoring dangling symref %s.", fullref
);
451 } else if ((flag
& REF_ISBROKEN
) && strchr(fullref
, '/')) {
452 warning("ignoring broken ref %s.", fullref
);
458 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
460 char *last_branch
= substitute_branch_name(&str
, &len
);
465 for (p
= ref_rev_parse_rules
; *p
; p
++) {
466 unsigned char hash
[20];
468 const char *ref
, *it
;
470 mksnpath(path
, sizeof(path
), *p
, len
, str
);
471 ref
= resolve_ref_unsafe(path
, RESOLVE_REF_READING
,
475 if (reflog_exists(path
))
477 else if (strcmp(ref
, path
) && reflog_exists(ref
))
485 if (!warn_ambiguous_refs
)
492 static int is_per_worktree_ref(const char *refname
)
494 return !strcmp(refname
, "HEAD") ||
495 starts_with(refname
, "refs/bisect/");
498 static int is_pseudoref_syntax(const char *refname
)
502 for (c
= refname
; *c
; c
++) {
503 if (!isupper(*c
) && *c
!= '-' && *c
!= '_')
510 enum ref_type
ref_type(const char *refname
)
512 if (is_per_worktree_ref(refname
))
513 return REF_TYPE_PER_WORKTREE
;
514 if (is_pseudoref_syntax(refname
))
515 return REF_TYPE_PSEUDOREF
;
516 return REF_TYPE_NORMAL
;
519 static int write_pseudoref(const char *pseudoref
, const unsigned char *sha1
,
520 const unsigned char *old_sha1
, struct strbuf
*err
)
522 const char *filename
;
524 static struct lock_file lock
;
525 struct strbuf buf
= STRBUF_INIT
;
528 strbuf_addf(&buf
, "%s\n", sha1_to_hex(sha1
));
530 filename
= git_path("%s", pseudoref
);
531 fd
= hold_lock_file_for_update(&lock
, filename
, LOCK_DIE_ON_ERROR
);
533 strbuf_addf(err
, "could not open '%s' for writing: %s",
534 filename
, strerror(errno
));
539 unsigned char actual_old_sha1
[20];
541 if (read_ref(pseudoref
, actual_old_sha1
))
542 die("could not read ref '%s'", pseudoref
);
543 if (hashcmp(actual_old_sha1
, old_sha1
)) {
544 strbuf_addf(err
, "unexpected sha1 when writing '%s'", pseudoref
);
545 rollback_lock_file(&lock
);
550 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
) {
551 strbuf_addf(err
, "could not write to '%s'", filename
);
552 rollback_lock_file(&lock
);
556 commit_lock_file(&lock
);
559 strbuf_release(&buf
);
563 static int delete_pseudoref(const char *pseudoref
, const unsigned char *old_sha1
)
565 static struct lock_file lock
;
566 const char *filename
;
568 filename
= git_path("%s", pseudoref
);
570 if (old_sha1
&& !is_null_sha1(old_sha1
)) {
572 unsigned char actual_old_sha1
[20];
574 fd
= hold_lock_file_for_update(&lock
, filename
,
577 die_errno(_("Could not open '%s' for writing"), filename
);
578 if (read_ref(pseudoref
, actual_old_sha1
))
579 die("could not read ref '%s'", pseudoref
);
580 if (hashcmp(actual_old_sha1
, old_sha1
)) {
581 warning("Unexpected sha1 when deleting %s", pseudoref
);
582 rollback_lock_file(&lock
);
587 rollback_lock_file(&lock
);
595 int delete_ref(const char *refname
, const unsigned char *old_sha1
,
598 struct ref_transaction
*transaction
;
599 struct strbuf err
= STRBUF_INIT
;
601 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
)
602 return delete_pseudoref(refname
, old_sha1
);
604 transaction
= ref_transaction_begin(&err
);
606 ref_transaction_delete(transaction
, refname
, old_sha1
,
607 flags
, NULL
, &err
) ||
608 ref_transaction_commit(transaction
, &err
)) {
609 error("%s", err
.buf
);
610 ref_transaction_free(transaction
);
611 strbuf_release(&err
);
614 ref_transaction_free(transaction
);
615 strbuf_release(&err
);
619 int copy_reflog_msg(char *buf
, const char *msg
)
626 while ((c
= *msg
++)) {
627 if (wasspace
&& isspace(c
))
629 wasspace
= isspace(c
);
634 while (buf
< cp
&& isspace(cp
[-1]))
640 int should_autocreate_reflog(const char *refname
)
642 if (!log_all_ref_updates
)
644 return starts_with(refname
, "refs/heads/") ||
645 starts_with(refname
, "refs/remotes/") ||
646 starts_with(refname
, "refs/notes/") ||
647 !strcmp(refname
, "HEAD");
650 int is_branch(const char *refname
)
652 return !strcmp(refname
, "HEAD") || starts_with(refname
, "refs/heads/");
655 struct read_ref_at_cb
{
657 unsigned long at_time
;
663 unsigned char osha1
[20];
664 unsigned char nsha1
[20];
668 unsigned long *cutoff_time
;
673 static int read_ref_at_ent(unsigned char *osha1
, unsigned char *nsha1
,
674 const char *email
, unsigned long timestamp
, int tz
,
675 const char *message
, void *cb_data
)
677 struct read_ref_at_cb
*cb
= cb_data
;
681 cb
->date
= timestamp
;
683 if (timestamp
<= cb
->at_time
|| cb
->cnt
== 0) {
685 *cb
->msg
= xstrdup(message
);
687 *cb
->cutoff_time
= timestamp
;
691 *cb
->cutoff_cnt
= cb
->reccnt
- 1;
693 * we have not yet updated cb->[n|o]sha1 so they still
694 * hold the values for the previous record.
696 if (!is_null_sha1(cb
->osha1
)) {
697 hashcpy(cb
->sha1
, nsha1
);
698 if (hashcmp(cb
->osha1
, nsha1
))
699 warning("Log for ref %s has gap after %s.",
700 cb
->refname
, show_date(cb
->date
, cb
->tz
, DATE_MODE(RFC2822
)));
702 else if (cb
->date
== cb
->at_time
)
703 hashcpy(cb
->sha1
, nsha1
);
704 else if (hashcmp(nsha1
, cb
->sha1
))
705 warning("Log for ref %s unexpectedly ended on %s.",
706 cb
->refname
, show_date(cb
->date
, cb
->tz
,
707 DATE_MODE(RFC2822
)));
708 hashcpy(cb
->osha1
, osha1
);
709 hashcpy(cb
->nsha1
, nsha1
);
713 hashcpy(cb
->osha1
, osha1
);
714 hashcpy(cb
->nsha1
, nsha1
);
720 static int read_ref_at_ent_oldest(unsigned char *osha1
, unsigned char *nsha1
,
721 const char *email
, unsigned long timestamp
,
722 int tz
, const char *message
, void *cb_data
)
724 struct read_ref_at_cb
*cb
= cb_data
;
727 *cb
->msg
= xstrdup(message
);
729 *cb
->cutoff_time
= timestamp
;
733 *cb
->cutoff_cnt
= cb
->reccnt
;
734 hashcpy(cb
->sha1
, osha1
);
735 if (is_null_sha1(cb
->sha1
))
736 hashcpy(cb
->sha1
, nsha1
);
737 /* We just want the first entry */
741 int read_ref_at(const char *refname
, unsigned int flags
, unsigned long at_time
, int cnt
,
742 unsigned char *sha1
, char **msg
,
743 unsigned long *cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
745 struct read_ref_at_cb cb
;
747 memset(&cb
, 0, sizeof(cb
));
748 cb
.refname
= refname
;
749 cb
.at_time
= at_time
;
752 cb
.cutoff_time
= cutoff_time
;
753 cb
.cutoff_tz
= cutoff_tz
;
754 cb
.cutoff_cnt
= cutoff_cnt
;
757 for_each_reflog_ent_reverse(refname
, read_ref_at_ent
, &cb
);
760 if (flags
& GET_SHA1_QUIETLY
)
763 die("Log for %s is empty.", refname
);
768 for_each_reflog_ent(refname
, read_ref_at_ent_oldest
, &cb
);
773 struct ref_transaction
*ref_transaction_begin(struct strbuf
*err
)
777 return xcalloc(1, sizeof(struct ref_transaction
));
780 void ref_transaction_free(struct ref_transaction
*transaction
)
787 for (i
= 0; i
< transaction
->nr
; i
++) {
788 free(transaction
->updates
[i
]->msg
);
789 free(transaction
->updates
[i
]);
791 free(transaction
->updates
);
795 struct ref_update
*ref_transaction_add_update(
796 struct ref_transaction
*transaction
,
797 const char *refname
, unsigned int flags
,
798 const unsigned char *new_sha1
,
799 const unsigned char *old_sha1
,
802 struct ref_update
*update
;
804 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
805 die("BUG: update called for transaction that is not open");
807 if ((flags
& REF_ISPRUNING
) && !(flags
& REF_NODEREF
))
808 die("BUG: REF_ISPRUNING set without REF_NODEREF");
810 FLEX_ALLOC_STR(update
, refname
, refname
);
811 ALLOC_GROW(transaction
->updates
, transaction
->nr
+ 1, transaction
->alloc
);
812 transaction
->updates
[transaction
->nr
++] = update
;
814 update
->flags
= flags
;
816 if (flags
& REF_HAVE_NEW
)
817 hashcpy(update
->new_sha1
, new_sha1
);
818 if (flags
& REF_HAVE_OLD
)
819 hashcpy(update
->old_sha1
, old_sha1
);
820 update
->msg
= xstrdup_or_null(msg
);
824 int ref_transaction_update(struct ref_transaction
*transaction
,
826 const unsigned char *new_sha1
,
827 const unsigned char *old_sha1
,
828 unsigned int flags
, const char *msg
,
833 if ((new_sha1
&& !is_null_sha1(new_sha1
)) ?
834 check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
) :
835 !refname_is_safe(refname
)) {
836 strbuf_addf(err
, "refusing to update ref with bad name '%s'",
841 flags
|= (new_sha1
? REF_HAVE_NEW
: 0) | (old_sha1
? REF_HAVE_OLD
: 0);
843 ref_transaction_add_update(transaction
, refname
, flags
,
844 new_sha1
, old_sha1
, msg
);
848 int ref_transaction_create(struct ref_transaction
*transaction
,
850 const unsigned char *new_sha1
,
851 unsigned int flags
, const char *msg
,
854 if (!new_sha1
|| is_null_sha1(new_sha1
))
855 die("BUG: create called without valid new_sha1");
856 return ref_transaction_update(transaction
, refname
, new_sha1
,
857 null_sha1
, flags
, msg
, err
);
860 int ref_transaction_delete(struct ref_transaction
*transaction
,
862 const unsigned char *old_sha1
,
863 unsigned int flags
, const char *msg
,
866 if (old_sha1
&& is_null_sha1(old_sha1
))
867 die("BUG: delete called with old_sha1 set to zeros");
868 return ref_transaction_update(transaction
, refname
,
873 int ref_transaction_verify(struct ref_transaction
*transaction
,
875 const unsigned char *old_sha1
,
880 die("BUG: verify called with old_sha1 set to NULL");
881 return ref_transaction_update(transaction
, refname
,
886 int update_ref_oid(const char *msg
, const char *refname
,
887 const struct object_id
*new_oid
, const struct object_id
*old_oid
,
888 unsigned int flags
, enum action_on_err onerr
)
890 return update_ref(msg
, refname
, new_oid
? new_oid
->hash
: NULL
,
891 old_oid
? old_oid
->hash
: NULL
, flags
, onerr
);
894 int update_ref(const char *msg
, const char *refname
,
895 const unsigned char *new_sha1
, const unsigned char *old_sha1
,
896 unsigned int flags
, enum action_on_err onerr
)
898 struct ref_transaction
*t
= NULL
;
899 struct strbuf err
= STRBUF_INIT
;
902 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
) {
903 ret
= write_pseudoref(refname
, new_sha1
, old_sha1
, &err
);
905 t
= ref_transaction_begin(&err
);
907 ref_transaction_update(t
, refname
, new_sha1
, old_sha1
,
909 ref_transaction_commit(t
, &err
)) {
911 ref_transaction_free(t
);
915 const char *str
= "update_ref failed for ref '%s': %s";
918 case UPDATE_REFS_MSG_ON_ERR
:
919 error(str
, refname
, err
.buf
);
921 case UPDATE_REFS_DIE_ON_ERR
:
922 die(str
, refname
, err
.buf
);
924 case UPDATE_REFS_QUIET_ON_ERR
:
927 strbuf_release(&err
);
930 strbuf_release(&err
);
932 ref_transaction_free(t
);
936 char *shorten_unambiguous_ref(const char *refname
, int strict
)
939 static char **scanf_fmts
;
945 * Pre-generate scanf formats from ref_rev_parse_rules[].
946 * Generate a format suitable for scanf from a
947 * ref_rev_parse_rules rule by interpolating "%s" at the
948 * location of the "%.*s".
950 size_t total_len
= 0;
953 /* the rule list is NULL terminated, count them first */
954 for (nr_rules
= 0; ref_rev_parse_rules
[nr_rules
]; nr_rules
++)
955 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
956 total_len
+= strlen(ref_rev_parse_rules
[nr_rules
]) - 2 + 1;
958 scanf_fmts
= xmalloc(st_add(st_mult(sizeof(char *), nr_rules
), total_len
));
961 for (i
= 0; i
< nr_rules
; i
++) {
962 assert(offset
< total_len
);
963 scanf_fmts
[i
] = (char *)&scanf_fmts
[nr_rules
] + offset
;
964 offset
+= snprintf(scanf_fmts
[i
], total_len
- offset
,
965 ref_rev_parse_rules
[i
], 2, "%s") + 1;
969 /* bail out if there are no rules */
971 return xstrdup(refname
);
973 /* buffer for scanf result, at most refname must fit */
974 short_name
= xstrdup(refname
);
976 /* skip first rule, it will always match */
977 for (i
= nr_rules
- 1; i
> 0 ; --i
) {
979 int rules_to_fail
= i
;
982 if (1 != sscanf(refname
, scanf_fmts
[i
], short_name
))
985 short_name_len
= strlen(short_name
);
988 * in strict mode, all (except the matched one) rules
989 * must fail to resolve to a valid non-ambiguous ref
992 rules_to_fail
= nr_rules
;
995 * check if the short name resolves to a valid ref,
996 * but use only rules prior to the matched one
998 for (j
= 0; j
< rules_to_fail
; j
++) {
999 const char *rule
= ref_rev_parse_rules
[j
];
1000 char refname
[PATH_MAX
];
1002 /* skip matched rule */
1007 * the short name is ambiguous, if it resolves
1008 * (with this previous rule) to a valid ref
1009 * read_ref() returns 0 on success
1011 mksnpath(refname
, sizeof(refname
),
1012 rule
, short_name_len
, short_name
);
1013 if (ref_exists(refname
))
1018 * short name is non-ambiguous if all previous rules
1019 * haven't resolved to a valid ref
1021 if (j
== rules_to_fail
)
1026 return xstrdup(refname
);
1029 static struct string_list
*hide_refs
;
1031 int parse_hide_refs_config(const char *var
, const char *value
, const char *section
)
1033 if (!strcmp("transfer.hiderefs", var
) ||
1034 /* NEEDSWORK: use parse_config_key() once both are merged */
1035 (starts_with(var
, section
) && var
[strlen(section
)] == '.' &&
1036 !strcmp(var
+ strlen(section
), ".hiderefs"))) {
1041 return config_error_nonbool(var
);
1042 ref
= xstrdup(value
);
1044 while (len
&& ref
[len
- 1] == '/')
1047 hide_refs
= xcalloc(1, sizeof(*hide_refs
));
1048 hide_refs
->strdup_strings
= 1;
1050 string_list_append(hide_refs
, ref
);
1055 int ref_is_hidden(const char *refname
, const char *refname_full
)
1061 for (i
= hide_refs
->nr
- 1; i
>= 0; i
--) {
1062 const char *match
= hide_refs
->items
[i
].string
;
1063 const char *subject
;
1067 if (*match
== '!') {
1072 if (*match
== '^') {
1073 subject
= refname_full
;
1079 /* refname can be NULL when namespaces are used. */
1080 if (!subject
|| !starts_with(subject
, match
))
1082 len
= strlen(match
);
1083 if (!subject
[len
] || subject
[len
] == '/')
1089 const char *find_descendant_ref(const char *dirname
,
1090 const struct string_list
*extras
,
1091 const struct string_list
*skip
)
1099 * Look at the place where dirname would be inserted into
1100 * extras. If there is an entry at that position that starts
1101 * with dirname (remember, dirname includes the trailing
1102 * slash) and is not in skip, then we have a conflict.
1104 for (pos
= string_list_find_insert_index(extras
, dirname
, 0);
1105 pos
< extras
->nr
; pos
++) {
1106 const char *extra_refname
= extras
->items
[pos
].string
;
1108 if (!starts_with(extra_refname
, dirname
))
1111 if (!skip
|| !string_list_has_string(skip
, extra_refname
))
1112 return extra_refname
;
1117 int rename_ref_available(const char *old_refname
, const char *new_refname
)
1119 struct string_list skip
= STRING_LIST_INIT_NODUP
;
1120 struct strbuf err
= STRBUF_INIT
;
1123 string_list_insert(&skip
, old_refname
);
1124 ok
= !verify_refname_available(new_refname
, NULL
, &skip
, &err
);
1126 error("%s", err
.buf
);
1128 string_list_clear(&skip
, 0);
1129 strbuf_release(&err
);
1133 int head_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1135 struct object_id oid
;
1139 if (resolve_gitlink_ref(submodule
, "HEAD", oid
.hash
) == 0)
1140 return fn("HEAD", &oid
, 0, cb_data
);
1145 if (!read_ref_full("HEAD", RESOLVE_REF_READING
, oid
.hash
, &flag
))
1146 return fn("HEAD", &oid
, flag
, cb_data
);
1151 int head_ref(each_ref_fn fn
, void *cb_data
)
1153 return head_ref_submodule(NULL
, fn
, cb_data
);
1157 * Call fn for each reference in the specified submodule for which the
1158 * refname begins with prefix. If trim is non-zero, then trim that
1159 * many characters off the beginning of each refname before passing
1160 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1161 * include broken references in the iteration. If fn ever returns a
1162 * non-zero value, stop the iteration and return that value;
1163 * otherwise, return 0.
1165 static int do_for_each_ref(const char *submodule
, const char *prefix
,
1166 each_ref_fn fn
, int trim
, int flags
, void *cb_data
)
1168 struct ref_store
*refs
= get_ref_store(submodule
);
1169 struct ref_iterator
*iter
;
1174 iter
= refs
->be
->iterator_begin(refs
, prefix
, flags
);
1175 iter
= prefix_ref_iterator_begin(iter
, prefix
, trim
);
1177 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1180 int for_each_ref(each_ref_fn fn
, void *cb_data
)
1182 return do_for_each_ref(NULL
, "", fn
, 0, 0, cb_data
);
1185 int for_each_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1187 return do_for_each_ref(submodule
, "", fn
, 0, 0, cb_data
);
1190 int for_each_ref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
)
1192 return do_for_each_ref(NULL
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1195 int for_each_fullref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
, unsigned int broken
)
1197 unsigned int flag
= 0;
1200 flag
= DO_FOR_EACH_INCLUDE_BROKEN
;
1201 return do_for_each_ref(NULL
, prefix
, fn
, 0, flag
, cb_data
);
1204 int for_each_ref_in_submodule(const char *submodule
, const char *prefix
,
1205 each_ref_fn fn
, void *cb_data
)
1207 return do_for_each_ref(submodule
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1210 int for_each_replace_ref(each_ref_fn fn
, void *cb_data
)
1212 return do_for_each_ref(NULL
, git_replace_ref_base
, fn
,
1213 strlen(git_replace_ref_base
), 0, cb_data
);
1216 int for_each_namespaced_ref(each_ref_fn fn
, void *cb_data
)
1218 struct strbuf buf
= STRBUF_INIT
;
1220 strbuf_addf(&buf
, "%srefs/", get_git_namespace());
1221 ret
= do_for_each_ref(NULL
, buf
.buf
, fn
, 0, 0, cb_data
);
1222 strbuf_release(&buf
);
1226 int for_each_rawref(each_ref_fn fn
, void *cb_data
)
1228 return do_for_each_ref(NULL
, "", fn
, 0,
1229 DO_FOR_EACH_INCLUDE_BROKEN
, cb_data
);
1232 /* This function needs to return a meaningful errno on failure */
1233 static const char *resolve_ref_recursively(struct ref_store
*refs
,
1234 const char *refname
,
1236 unsigned char *sha1
, int *flags
)
1238 static struct strbuf sb_refname
= STRBUF_INIT
;
1243 flags
= &unused_flags
;
1247 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1248 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1249 !refname_is_safe(refname
)) {
1255 * dwim_ref() uses REF_ISBROKEN to distinguish between
1256 * missing refs and refs that were present but invalid,
1257 * to complain about the latter to stderr.
1259 * We don't know whether the ref exists, so don't set
1262 *flags
|= REF_BAD_NAME
;
1265 for (symref_count
= 0; symref_count
< SYMREF_MAXDEPTH
; symref_count
++) {
1266 unsigned int read_flags
= 0;
1268 if (refs
->be
->read_raw_ref(refs
, refname
,
1269 sha1
, &sb_refname
, &read_flags
)) {
1270 *flags
|= read_flags
;
1271 if (errno
!= ENOENT
|| (resolve_flags
& RESOLVE_REF_READING
))
1274 if (*flags
& REF_BAD_NAME
)
1275 *flags
|= REF_ISBROKEN
;
1279 *flags
|= read_flags
;
1281 if (!(read_flags
& REF_ISSYMREF
)) {
1282 if (*flags
& REF_BAD_NAME
) {
1284 *flags
|= REF_ISBROKEN
;
1289 refname
= sb_refname
.buf
;
1290 if (resolve_flags
& RESOLVE_REF_NO_RECURSE
) {
1294 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1295 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1296 !refname_is_safe(refname
)) {
1301 *flags
|= REF_ISBROKEN
| REF_BAD_NAME
;
1309 /* backend functions */
1310 int refs_init_db(struct strbuf
*err
)
1312 struct ref_store
*refs
= get_ref_store(NULL
);
1314 return refs
->be
->init_db(refs
, err
);
1317 const char *resolve_ref_unsafe(const char *refname
, int resolve_flags
,
1318 unsigned char *sha1
, int *flags
)
1320 return resolve_ref_recursively(get_ref_store(NULL
), refname
,
1321 resolve_flags
, sha1
, flags
);
1324 int resolve_gitlink_ref(const char *submodule
, const char *refname
,
1325 unsigned char *sha1
)
1327 size_t len
= strlen(submodule
);
1328 struct ref_store
*refs
;
1331 while (len
&& submodule
[len
- 1] == '/')
1337 if (submodule
[len
]) {
1338 /* We need to strip off one or more trailing slashes */
1339 char *stripped
= xmemdupz(submodule
, len
);
1341 refs
= get_ref_store(stripped
);
1344 refs
= get_ref_store(submodule
);
1350 if (!resolve_ref_recursively(refs
, refname
, 0, sha1
, &flags
) ||
1356 struct submodule_hash_entry
1358 struct hashmap_entry ent
; /* must be the first member! */
1360 struct ref_store
*refs
;
1362 /* NUL-terminated name of submodule: */
1363 char submodule
[FLEX_ARRAY
];
1366 static int submodule_hash_cmp(const void *entry
, const void *entry_or_key
,
1367 const void *keydata
)
1369 const struct submodule_hash_entry
*e1
= entry
, *e2
= entry_or_key
;
1370 const char *submodule
= keydata
? keydata
: e2
->submodule
;
1372 return strcmp(e1
->submodule
, submodule
);
1375 static struct submodule_hash_entry
*alloc_submodule_hash_entry(
1376 const char *submodule
, struct ref_store
*refs
)
1378 struct submodule_hash_entry
*entry
;
1380 FLEX_ALLOC_STR(entry
, submodule
, submodule
);
1381 hashmap_entry_init(entry
, strhash(submodule
));
1386 /* A pointer to the ref_store for the main repository: */
1387 static struct ref_store
*main_ref_store
;
1389 /* A hashmap of ref_stores, stored by submodule name: */
1390 static struct hashmap submodule_ref_stores
;
1393 * Return the ref_store instance for the specified submodule (or the
1394 * main repository if submodule is NULL). If that ref_store hasn't
1395 * been initialized yet, return NULL.
1397 static struct ref_store
*lookup_ref_store(const char *submodule
)
1399 struct submodule_hash_entry
*entry
;
1402 return main_ref_store
;
1404 if (!submodule_ref_stores
.tablesize
)
1405 /* It's initialized on demand in register_ref_store(). */
1408 entry
= hashmap_get_from_hash(&submodule_ref_stores
,
1409 strhash(submodule
), submodule
);
1410 return entry
? entry
->refs
: NULL
;
1414 * Register the specified ref_store to be the one that should be used
1415 * for submodule (or the main repository if submodule is NULL). It is
1416 * a fatal error to call this function twice for the same submodule.
1418 static void register_ref_store(struct ref_store
*refs
, const char *submodule
)
1422 die("BUG: main_ref_store initialized twice");
1424 main_ref_store
= refs
;
1426 if (!submodule_ref_stores
.tablesize
)
1427 hashmap_init(&submodule_ref_stores
, submodule_hash_cmp
, 0);
1429 if (hashmap_put(&submodule_ref_stores
,
1430 alloc_submodule_hash_entry(submodule
, refs
)))
1431 die("BUG: ref_store for submodule '%s' initialized twice",
1437 * Create, record, and return a ref_store instance for the specified
1438 * submodule (or the main repository if submodule is NULL).
1440 static struct ref_store
*ref_store_init(const char *submodule
)
1442 const char *be_name
= "files";
1443 struct ref_storage_be
*be
= find_ref_storage_backend(be_name
);
1444 struct ref_store
*refs
;
1447 die("BUG: reference backend %s is unknown", be_name
);
1449 refs
= be
->init(submodule
);
1450 register_ref_store(refs
, submodule
);
1454 struct ref_store
*get_ref_store(const char *submodule
)
1456 struct ref_store
*refs
;
1458 if (!submodule
|| !*submodule
) {
1459 refs
= lookup_ref_store(NULL
);
1462 refs
= ref_store_init(NULL
);
1464 refs
= lookup_ref_store(submodule
);
1467 struct strbuf submodule_sb
= STRBUF_INIT
;
1469 strbuf_addstr(&submodule_sb
, submodule
);
1470 if (is_nonbare_repository_dir(&submodule_sb
))
1471 refs
= ref_store_init(submodule
);
1472 strbuf_release(&submodule_sb
);
1479 void base_ref_store_init(struct ref_store
*refs
,
1480 const struct ref_storage_be
*be
,
1481 const char *submodule
)
1486 /* backend functions */
1487 int pack_refs(unsigned int flags
)
1489 struct ref_store
*refs
= get_ref_store(NULL
);
1491 return refs
->be
->pack_refs(refs
, flags
);
1494 int peel_ref(const char *refname
, unsigned char *sha1
)
1496 struct ref_store
*refs
= get_ref_store(NULL
);
1498 return refs
->be
->peel_ref(refs
, refname
, sha1
);
1501 int create_symref(const char *ref_target
, const char *refs_heads_master
,
1504 struct ref_store
*refs
= get_ref_store(NULL
);
1506 return refs
->be
->create_symref(refs
, ref_target
, refs_heads_master
,
1510 int ref_transaction_commit(struct ref_transaction
*transaction
,
1513 struct ref_store
*refs
= get_ref_store(NULL
);
1515 return refs
->be
->transaction_commit(refs
, transaction
, err
);
1518 int verify_refname_available(const char *refname
,
1519 const struct string_list
*extra
,
1520 const struct string_list
*skip
,
1523 struct ref_store
*refs
= get_ref_store(NULL
);
1525 return refs
->be
->verify_refname_available(refs
, refname
, extra
, skip
, err
);
1528 int for_each_reflog(each_ref_fn fn
, void *cb_data
)
1530 struct ref_store
*refs
= get_ref_store(NULL
);
1531 struct ref_iterator
*iter
;
1533 iter
= refs
->be
->reflog_iterator_begin(refs
);
1535 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1538 int for_each_reflog_ent_reverse(const char *refname
, each_reflog_ent_fn fn
,
1541 struct ref_store
*refs
= get_ref_store(NULL
);
1543 return refs
->be
->for_each_reflog_ent_reverse(refs
, refname
,
1547 int for_each_reflog_ent(const char *refname
, each_reflog_ent_fn fn
,
1550 struct ref_store
*refs
= get_ref_store(NULL
);
1552 return refs
->be
->for_each_reflog_ent(refs
, refname
, fn
, cb_data
);
1555 int reflog_exists(const char *refname
)
1557 struct ref_store
*refs
= get_ref_store(NULL
);
1559 return refs
->be
->reflog_exists(refs
, refname
);
1562 int safe_create_reflog(const char *refname
, int force_create
,
1565 struct ref_store
*refs
= get_ref_store(NULL
);
1567 return refs
->be
->create_reflog(refs
, refname
, force_create
, err
);
1570 int delete_reflog(const char *refname
)
1572 struct ref_store
*refs
= get_ref_store(NULL
);
1574 return refs
->be
->delete_reflog(refs
, refname
);
1577 int reflog_expire(const char *refname
, const unsigned char *sha1
,
1579 reflog_expiry_prepare_fn prepare_fn
,
1580 reflog_expiry_should_prune_fn should_prune_fn
,
1581 reflog_expiry_cleanup_fn cleanup_fn
,
1582 void *policy_cb_data
)
1584 struct ref_store
*refs
= get_ref_store(NULL
);
1586 return refs
->be
->reflog_expire(refs
, refname
, sha1
, flags
,
1587 prepare_fn
, should_prune_fn
,
1588 cleanup_fn
, policy_cb_data
);
1591 int initial_ref_transaction_commit(struct ref_transaction
*transaction
,
1594 struct ref_store
*refs
= get_ref_store(NULL
);
1596 return refs
->be
->initial_transaction_commit(refs
, transaction
, err
);
1599 int delete_refs(struct string_list
*refnames
, unsigned int flags
)
1601 struct ref_store
*refs
= get_ref_store(NULL
);
1603 return refs
->be
->delete_refs(refs
, refnames
, flags
);
1606 int rename_ref(const char *oldref
, const char *newref
, const char *logmsg
)
1608 struct ref_store
*refs
= get_ref_store(NULL
);
1610 return refs
->be
->rename_ref(refs
, oldref
, newref
, logmsg
);