2 * The backend-independent part of the reference module.
8 #include "refs/refs-internal.h"
13 * List of all available backends
15 static struct ref_storage_be
*refs_backends
= &refs_be_files
;
17 static struct ref_storage_be
*find_ref_storage_backend(const char *name
)
19 struct ref_storage_be
*be
;
20 for (be
= refs_backends
; be
; be
= be
->next
)
21 if (!strcmp(be
->name
, name
))
26 int ref_storage_backend_exists(const char *name
)
28 return find_ref_storage_backend(name
) != NULL
;
32 * How to handle various characters in refnames:
33 * 0: An acceptable character for refs
35 * 2: ., look for a preceding . to reject .. in refs
36 * 3: {, look for a preceding @ to reject @{ in refs
37 * 4: A bad character: ASCII control characters, and
38 * ":", "?", "[", "\", "^", "~", SP, or TAB
39 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
41 static unsigned char refname_disposition
[256] = {
42 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
43 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
44 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
53 * Try to read one refname component from the front of refname.
54 * Return the length of the component found, or -1 if the component is
55 * not legal. It is legal if it is something reasonable to have under
56 * ".git/refs/"; We do not like it if:
58 * - any path component of it begins with ".", or
59 * - it has double dots "..", or
60 * - it has ASCII control characters, or
61 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
62 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
63 * - it ends with a "/", or
64 * - it ends with ".lock", or
65 * - it contains a "@{" portion
67 static int check_refname_component(const char *refname
, int *flags
)
72 for (cp
= refname
; ; cp
++) {
74 unsigned char disp
= refname_disposition
[ch
];
80 return -1; /* Refname contains "..". */
84 return -1; /* Refname contains "@{". */
89 if (!(*flags
& REFNAME_REFSPEC_PATTERN
))
90 return -1; /* refspec can't be a pattern */
93 * Unset the pattern flag so that we only accept
94 * a single asterisk for one side of refspec.
96 *flags
&= ~ REFNAME_REFSPEC_PATTERN
;
103 return 0; /* Component has zero length. */
104 if (refname
[0] == '.')
105 return -1; /* Component starts with '.'. */
106 if (cp
- refname
>= LOCK_SUFFIX_LEN
&&
107 !memcmp(cp
- LOCK_SUFFIX_LEN
, LOCK_SUFFIX
, LOCK_SUFFIX_LEN
))
108 return -1; /* Refname ends with ".lock". */
112 int check_refname_format(const char *refname
, int flags
)
114 int component_len
, component_count
= 0;
116 if (!strcmp(refname
, "@"))
117 /* Refname is a single character '@'. */
121 /* We are at the start of a path component. */
122 component_len
= check_refname_component(refname
, &flags
);
123 if (component_len
<= 0)
127 if (refname
[component_len
] == '\0')
129 /* Skip to next component. */
130 refname
+= component_len
+ 1;
133 if (refname
[component_len
- 1] == '.')
134 return -1; /* Refname ends with '.'. */
135 if (!(flags
& REFNAME_ALLOW_ONELEVEL
) && component_count
< 2)
136 return -1; /* Refname has only one component. */
140 int refname_is_safe(const char *refname
)
144 if (skip_prefix(refname
, "refs/", &rest
)) {
147 size_t restlen
= strlen(rest
);
149 /* rest must not be empty, or start or end with "/" */
150 if (!restlen
|| *rest
== '/' || rest
[restlen
- 1] == '/')
154 * Does the refname try to escape refs/?
155 * For example: refs/foo/../bar is safe but refs/foo/../../bar
158 buf
= xmallocz(restlen
);
159 result
= !normalize_path_copy(buf
, rest
) && !strcmp(buf
, rest
);
165 if (!isupper(*refname
) && *refname
!= '_')
172 char *resolve_refdup(const char *refname
, int resolve_flags
,
173 unsigned char *sha1
, int *flags
)
175 return xstrdup_or_null(resolve_ref_unsafe(refname
, resolve_flags
,
179 /* The argument to filter_refs */
186 int read_ref_full(const char *refname
, int resolve_flags
, unsigned char *sha1
, int *flags
)
188 if (resolve_ref_unsafe(refname
, resolve_flags
, sha1
, flags
))
193 int read_ref(const char *refname
, unsigned char *sha1
)
195 return read_ref_full(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
198 int ref_exists(const char *refname
)
200 unsigned char sha1
[20];
201 return !!resolve_ref_unsafe(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
204 static int filter_refs(const char *refname
, const struct object_id
*oid
,
205 int flags
, void *data
)
207 struct ref_filter
*filter
= (struct ref_filter
*)data
;
209 if (wildmatch(filter
->pattern
, refname
, 0, NULL
))
211 return filter
->fn(refname
, oid
, flags
, filter
->cb_data
);
214 enum peel_status
peel_object(const unsigned char *name
, unsigned char *sha1
)
216 struct object
*o
= lookup_unknown_object(name
);
218 if (o
->type
== OBJ_NONE
) {
219 int type
= sha1_object_info(name
, NULL
);
220 if (type
< 0 || !object_as_type(o
, type
, 0))
224 if (o
->type
!= OBJ_TAG
)
227 o
= deref_tag_noverify(o
);
231 hashcpy(sha1
, o
->oid
.hash
);
235 struct warn_if_dangling_data
{
238 const struct string_list
*refnames
;
242 static int warn_if_dangling_symref(const char *refname
, const struct object_id
*oid
,
243 int flags
, void *cb_data
)
245 struct warn_if_dangling_data
*d
= cb_data
;
246 const char *resolves_to
;
247 struct object_id junk
;
249 if (!(flags
& REF_ISSYMREF
))
252 resolves_to
= resolve_ref_unsafe(refname
, 0, junk
.hash
, NULL
);
255 ? strcmp(resolves_to
, d
->refname
)
256 : !string_list_has_string(d
->refnames
, resolves_to
))) {
260 fprintf(d
->fp
, d
->msg_fmt
, refname
);
265 void warn_dangling_symref(FILE *fp
, const char *msg_fmt
, const char *refname
)
267 struct warn_if_dangling_data data
;
270 data
.refname
= refname
;
271 data
.refnames
= NULL
;
272 data
.msg_fmt
= msg_fmt
;
273 for_each_rawref(warn_if_dangling_symref
, &data
);
276 void warn_dangling_symrefs(FILE *fp
, const char *msg_fmt
, const struct string_list
*refnames
)
278 struct warn_if_dangling_data data
;
282 data
.refnames
= refnames
;
283 data
.msg_fmt
= msg_fmt
;
284 for_each_rawref(warn_if_dangling_symref
, &data
);
287 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
289 return for_each_ref_in("refs/tags/", fn
, cb_data
);
292 int for_each_tag_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
294 return for_each_ref_in_submodule(submodule
, "refs/tags/", fn
, cb_data
);
297 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
299 return for_each_ref_in("refs/heads/", fn
, cb_data
);
302 int for_each_branch_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
304 return for_each_ref_in_submodule(submodule
, "refs/heads/", fn
, cb_data
);
307 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
309 return for_each_ref_in("refs/remotes/", fn
, cb_data
);
312 int for_each_remote_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
314 return for_each_ref_in_submodule(submodule
, "refs/remotes/", fn
, cb_data
);
317 int head_ref_namespaced(each_ref_fn fn
, void *cb_data
)
319 struct strbuf buf
= STRBUF_INIT
;
321 struct object_id oid
;
324 strbuf_addf(&buf
, "%sHEAD", get_git_namespace());
325 if (!read_ref_full(buf
.buf
, RESOLVE_REF_READING
, oid
.hash
, &flag
))
326 ret
= fn(buf
.buf
, &oid
, flag
, cb_data
);
327 strbuf_release(&buf
);
332 int for_each_glob_ref_in(each_ref_fn fn
, const char *pattern
,
333 const char *prefix
, void *cb_data
)
335 struct strbuf real_pattern
= STRBUF_INIT
;
336 struct ref_filter filter
;
339 if (!prefix
&& !starts_with(pattern
, "refs/"))
340 strbuf_addstr(&real_pattern
, "refs/");
342 strbuf_addstr(&real_pattern
, prefix
);
343 strbuf_addstr(&real_pattern
, pattern
);
345 if (!has_glob_specials(pattern
)) {
346 /* Append implied '/' '*' if not present. */
347 strbuf_complete(&real_pattern
, '/');
348 /* No need to check for '*', there is none. */
349 strbuf_addch(&real_pattern
, '*');
352 filter
.pattern
= real_pattern
.buf
;
354 filter
.cb_data
= cb_data
;
355 ret
= for_each_ref(filter_refs
, &filter
);
357 strbuf_release(&real_pattern
);
361 int for_each_glob_ref(each_ref_fn fn
, const char *pattern
, void *cb_data
)
363 return for_each_glob_ref_in(fn
, pattern
, NULL
, cb_data
);
366 const char *prettify_refname(const char *name
)
369 starts_with(name
, "refs/heads/") ? 11 :
370 starts_with(name
, "refs/tags/") ? 10 :
371 starts_with(name
, "refs/remotes/") ? 13 :
375 static const char *ref_rev_parse_rules
[] = {
381 "refs/remotes/%.*s/HEAD",
385 int refname_match(const char *abbrev_name
, const char *full_name
)
388 const int abbrev_name_len
= strlen(abbrev_name
);
390 for (p
= ref_rev_parse_rules
; *p
; p
++) {
391 if (!strcmp(full_name
, mkpath(*p
, abbrev_name_len
, abbrev_name
))) {
400 * *string and *len will only be substituted, and *string returned (for
401 * later free()ing) if the string passed in is a magic short-hand form
404 static char *substitute_branch_name(const char **string
, int *len
)
406 struct strbuf buf
= STRBUF_INIT
;
407 int ret
= interpret_branch_name(*string
, *len
, &buf
);
411 *string
= strbuf_detach(&buf
, &size
);
413 return (char *)*string
;
419 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
421 char *last_branch
= substitute_branch_name(&str
, &len
);
422 int refs_found
= expand_ref(str
, len
, sha1
, ref
);
427 int expand_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
433 for (p
= ref_rev_parse_rules
; *p
; p
++) {
434 char fullref
[PATH_MAX
];
435 unsigned char sha1_from_ref
[20];
436 unsigned char *this_result
;
439 this_result
= refs_found
? sha1_from_ref
: sha1
;
440 mksnpath(fullref
, sizeof(fullref
), *p
, len
, str
);
441 r
= resolve_ref_unsafe(fullref
, RESOLVE_REF_READING
,
446 if (!warn_ambiguous_refs
)
448 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
, "HEAD")) {
449 warning("ignoring dangling symref %s.", fullref
);
450 } else if ((flag
& REF_ISBROKEN
) && strchr(fullref
, '/')) {
451 warning("ignoring broken ref %s.", fullref
);
457 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
459 char *last_branch
= substitute_branch_name(&str
, &len
);
464 for (p
= ref_rev_parse_rules
; *p
; p
++) {
465 unsigned char hash
[20];
467 const char *ref
, *it
;
469 mksnpath(path
, sizeof(path
), *p
, len
, str
);
470 ref
= resolve_ref_unsafe(path
, RESOLVE_REF_READING
,
474 if (reflog_exists(path
))
476 else if (strcmp(ref
, path
) && reflog_exists(ref
))
484 if (!warn_ambiguous_refs
)
491 static int is_per_worktree_ref(const char *refname
)
493 return !strcmp(refname
, "HEAD") ||
494 starts_with(refname
, "refs/bisect/");
497 static int is_pseudoref_syntax(const char *refname
)
501 for (c
= refname
; *c
; c
++) {
502 if (!isupper(*c
) && *c
!= '-' && *c
!= '_')
509 enum ref_type
ref_type(const char *refname
)
511 if (is_per_worktree_ref(refname
))
512 return REF_TYPE_PER_WORKTREE
;
513 if (is_pseudoref_syntax(refname
))
514 return REF_TYPE_PSEUDOREF
;
515 return REF_TYPE_NORMAL
;
518 static int write_pseudoref(const char *pseudoref
, const unsigned char *sha1
,
519 const unsigned char *old_sha1
, struct strbuf
*err
)
521 const char *filename
;
523 static struct lock_file lock
;
524 struct strbuf buf
= STRBUF_INIT
;
527 strbuf_addf(&buf
, "%s\n", sha1_to_hex(sha1
));
529 filename
= git_path("%s", pseudoref
);
530 fd
= hold_lock_file_for_update(&lock
, filename
, LOCK_DIE_ON_ERROR
);
532 strbuf_addf(err
, "could not open '%s' for writing: %s",
533 filename
, strerror(errno
));
538 unsigned char actual_old_sha1
[20];
540 if (read_ref(pseudoref
, actual_old_sha1
))
541 die("could not read ref '%s'", pseudoref
);
542 if (hashcmp(actual_old_sha1
, old_sha1
)) {
543 strbuf_addf(err
, "unexpected sha1 when writing '%s'", pseudoref
);
544 rollback_lock_file(&lock
);
549 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
) {
550 strbuf_addf(err
, "could not write to '%s'", filename
);
551 rollback_lock_file(&lock
);
555 commit_lock_file(&lock
);
558 strbuf_release(&buf
);
562 static int delete_pseudoref(const char *pseudoref
, const unsigned char *old_sha1
)
564 static struct lock_file lock
;
565 const char *filename
;
567 filename
= git_path("%s", pseudoref
);
569 if (old_sha1
&& !is_null_sha1(old_sha1
)) {
571 unsigned char actual_old_sha1
[20];
573 fd
= hold_lock_file_for_update(&lock
, filename
,
576 die_errno(_("Could not open '%s' for writing"), filename
);
577 if (read_ref(pseudoref
, actual_old_sha1
))
578 die("could not read ref '%s'", pseudoref
);
579 if (hashcmp(actual_old_sha1
, old_sha1
)) {
580 warning("Unexpected sha1 when deleting %s", pseudoref
);
581 rollback_lock_file(&lock
);
586 rollback_lock_file(&lock
);
594 int delete_ref(const char *msg
, const char *refname
,
595 const unsigned char *old_sha1
, unsigned int flags
)
597 struct ref_transaction
*transaction
;
598 struct strbuf err
= STRBUF_INIT
;
600 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
)
601 return delete_pseudoref(refname
, old_sha1
);
603 transaction
= ref_transaction_begin(&err
);
605 ref_transaction_delete(transaction
, refname
, old_sha1
,
607 ref_transaction_commit(transaction
, &err
)) {
608 error("%s", err
.buf
);
609 ref_transaction_free(transaction
);
610 strbuf_release(&err
);
613 ref_transaction_free(transaction
);
614 strbuf_release(&err
);
618 int copy_reflog_msg(char *buf
, const char *msg
)
625 while ((c
= *msg
++)) {
626 if (wasspace
&& isspace(c
))
628 wasspace
= isspace(c
);
633 while (buf
< cp
&& isspace(cp
[-1]))
639 int should_autocreate_reflog(const char *refname
)
641 switch (log_all_ref_updates
) {
642 case LOG_REFS_ALWAYS
:
644 case LOG_REFS_NORMAL
:
645 return starts_with(refname
, "refs/heads/") ||
646 starts_with(refname
, "refs/remotes/") ||
647 starts_with(refname
, "refs/notes/") ||
648 !strcmp(refname
, "HEAD");
654 int is_branch(const char *refname
)
656 return !strcmp(refname
, "HEAD") || starts_with(refname
, "refs/heads/");
659 struct read_ref_at_cb
{
661 unsigned long at_time
;
667 unsigned char osha1
[20];
668 unsigned char nsha1
[20];
672 unsigned long *cutoff_time
;
677 static int read_ref_at_ent(unsigned char *osha1
, unsigned char *nsha1
,
678 const char *email
, unsigned long timestamp
, int tz
,
679 const char *message
, void *cb_data
)
681 struct read_ref_at_cb
*cb
= cb_data
;
685 cb
->date
= timestamp
;
687 if (timestamp
<= cb
->at_time
|| cb
->cnt
== 0) {
689 *cb
->msg
= xstrdup(message
);
691 *cb
->cutoff_time
= timestamp
;
695 *cb
->cutoff_cnt
= cb
->reccnt
- 1;
697 * we have not yet updated cb->[n|o]sha1 so they still
698 * hold the values for the previous record.
700 if (!is_null_sha1(cb
->osha1
)) {
701 hashcpy(cb
->sha1
, nsha1
);
702 if (hashcmp(cb
->osha1
, nsha1
))
703 warning("Log for ref %s has gap after %s.",
704 cb
->refname
, show_date(cb
->date
, cb
->tz
, DATE_MODE(RFC2822
)));
706 else if (cb
->date
== cb
->at_time
)
707 hashcpy(cb
->sha1
, nsha1
);
708 else if (hashcmp(nsha1
, cb
->sha1
))
709 warning("Log for ref %s unexpectedly ended on %s.",
710 cb
->refname
, show_date(cb
->date
, cb
->tz
,
711 DATE_MODE(RFC2822
)));
712 hashcpy(cb
->osha1
, osha1
);
713 hashcpy(cb
->nsha1
, nsha1
);
717 hashcpy(cb
->osha1
, osha1
);
718 hashcpy(cb
->nsha1
, nsha1
);
724 static int read_ref_at_ent_oldest(unsigned char *osha1
, unsigned char *nsha1
,
725 const char *email
, unsigned long timestamp
,
726 int tz
, const char *message
, void *cb_data
)
728 struct read_ref_at_cb
*cb
= cb_data
;
731 *cb
->msg
= xstrdup(message
);
733 *cb
->cutoff_time
= timestamp
;
737 *cb
->cutoff_cnt
= cb
->reccnt
;
738 hashcpy(cb
->sha1
, osha1
);
739 if (is_null_sha1(cb
->sha1
))
740 hashcpy(cb
->sha1
, nsha1
);
741 /* We just want the first entry */
745 int read_ref_at(const char *refname
, unsigned int flags
, unsigned long at_time
, int cnt
,
746 unsigned char *sha1
, char **msg
,
747 unsigned long *cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
749 struct read_ref_at_cb cb
;
751 memset(&cb
, 0, sizeof(cb
));
752 cb
.refname
= refname
;
753 cb
.at_time
= at_time
;
756 cb
.cutoff_time
= cutoff_time
;
757 cb
.cutoff_tz
= cutoff_tz
;
758 cb
.cutoff_cnt
= cutoff_cnt
;
761 for_each_reflog_ent_reverse(refname
, read_ref_at_ent
, &cb
);
764 if (flags
& GET_SHA1_QUIETLY
)
767 die("Log for %s is empty.", refname
);
772 for_each_reflog_ent(refname
, read_ref_at_ent_oldest
, &cb
);
777 struct ref_transaction
*ref_transaction_begin(struct strbuf
*err
)
781 return xcalloc(1, sizeof(struct ref_transaction
));
784 void ref_transaction_free(struct ref_transaction
*transaction
)
791 for (i
= 0; i
< transaction
->nr
; i
++) {
792 free(transaction
->updates
[i
]->msg
);
793 free(transaction
->updates
[i
]);
795 free(transaction
->updates
);
799 struct ref_update
*ref_transaction_add_update(
800 struct ref_transaction
*transaction
,
801 const char *refname
, unsigned int flags
,
802 const unsigned char *new_sha1
,
803 const unsigned char *old_sha1
,
806 struct ref_update
*update
;
808 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
809 die("BUG: update called for transaction that is not open");
811 if ((flags
& REF_ISPRUNING
) && !(flags
& REF_NODEREF
))
812 die("BUG: REF_ISPRUNING set without REF_NODEREF");
814 FLEX_ALLOC_STR(update
, refname
, refname
);
815 ALLOC_GROW(transaction
->updates
, transaction
->nr
+ 1, transaction
->alloc
);
816 transaction
->updates
[transaction
->nr
++] = update
;
818 update
->flags
= flags
;
820 if (flags
& REF_HAVE_NEW
)
821 hashcpy(update
->new_sha1
, new_sha1
);
822 if (flags
& REF_HAVE_OLD
)
823 hashcpy(update
->old_sha1
, old_sha1
);
824 update
->msg
= xstrdup_or_null(msg
);
828 int ref_transaction_update(struct ref_transaction
*transaction
,
830 const unsigned char *new_sha1
,
831 const unsigned char *old_sha1
,
832 unsigned int flags
, const char *msg
,
837 if ((new_sha1
&& !is_null_sha1(new_sha1
)) ?
838 check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
) :
839 !refname_is_safe(refname
)) {
840 strbuf_addf(err
, "refusing to update ref with bad name '%s'",
845 flags
|= (new_sha1
? REF_HAVE_NEW
: 0) | (old_sha1
? REF_HAVE_OLD
: 0);
847 ref_transaction_add_update(transaction
, refname
, flags
,
848 new_sha1
, old_sha1
, msg
);
852 int ref_transaction_create(struct ref_transaction
*transaction
,
854 const unsigned char *new_sha1
,
855 unsigned int flags
, const char *msg
,
858 if (!new_sha1
|| is_null_sha1(new_sha1
))
859 die("BUG: create called without valid new_sha1");
860 return ref_transaction_update(transaction
, refname
, new_sha1
,
861 null_sha1
, flags
, msg
, err
);
864 int ref_transaction_delete(struct ref_transaction
*transaction
,
866 const unsigned char *old_sha1
,
867 unsigned int flags
, const char *msg
,
870 if (old_sha1
&& is_null_sha1(old_sha1
))
871 die("BUG: delete called with old_sha1 set to zeros");
872 return ref_transaction_update(transaction
, refname
,
877 int ref_transaction_verify(struct ref_transaction
*transaction
,
879 const unsigned char *old_sha1
,
884 die("BUG: verify called with old_sha1 set to NULL");
885 return ref_transaction_update(transaction
, refname
,
890 int update_ref_oid(const char *msg
, const char *refname
,
891 const struct object_id
*new_oid
, const struct object_id
*old_oid
,
892 unsigned int flags
, enum action_on_err onerr
)
894 return update_ref(msg
, refname
, new_oid
? new_oid
->hash
: NULL
,
895 old_oid
? old_oid
->hash
: NULL
, flags
, onerr
);
898 int update_ref(const char *msg
, const char *refname
,
899 const unsigned char *new_sha1
, const unsigned char *old_sha1
,
900 unsigned int flags
, enum action_on_err onerr
)
902 struct ref_transaction
*t
= NULL
;
903 struct strbuf err
= STRBUF_INIT
;
906 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
) {
907 ret
= write_pseudoref(refname
, new_sha1
, old_sha1
, &err
);
909 t
= ref_transaction_begin(&err
);
911 ref_transaction_update(t
, refname
, new_sha1
, old_sha1
,
913 ref_transaction_commit(t
, &err
)) {
915 ref_transaction_free(t
);
919 const char *str
= "update_ref failed for ref '%s': %s";
922 case UPDATE_REFS_MSG_ON_ERR
:
923 error(str
, refname
, err
.buf
);
925 case UPDATE_REFS_DIE_ON_ERR
:
926 die(str
, refname
, err
.buf
);
928 case UPDATE_REFS_QUIET_ON_ERR
:
931 strbuf_release(&err
);
934 strbuf_release(&err
);
936 ref_transaction_free(t
);
940 char *shorten_unambiguous_ref(const char *refname
, int strict
)
943 static char **scanf_fmts
;
949 * Pre-generate scanf formats from ref_rev_parse_rules[].
950 * Generate a format suitable for scanf from a
951 * ref_rev_parse_rules rule by interpolating "%s" at the
952 * location of the "%.*s".
954 size_t total_len
= 0;
957 /* the rule list is NULL terminated, count them first */
958 for (nr_rules
= 0; ref_rev_parse_rules
[nr_rules
]; nr_rules
++)
959 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
960 total_len
+= strlen(ref_rev_parse_rules
[nr_rules
]) - 2 + 1;
962 scanf_fmts
= xmalloc(st_add(st_mult(sizeof(char *), nr_rules
), total_len
));
965 for (i
= 0; i
< nr_rules
; i
++) {
966 assert(offset
< total_len
);
967 scanf_fmts
[i
] = (char *)&scanf_fmts
[nr_rules
] + offset
;
968 offset
+= snprintf(scanf_fmts
[i
], total_len
- offset
,
969 ref_rev_parse_rules
[i
], 2, "%s") + 1;
973 /* bail out if there are no rules */
975 return xstrdup(refname
);
977 /* buffer for scanf result, at most refname must fit */
978 short_name
= xstrdup(refname
);
980 /* skip first rule, it will always match */
981 for (i
= nr_rules
- 1; i
> 0 ; --i
) {
983 int rules_to_fail
= i
;
986 if (1 != sscanf(refname
, scanf_fmts
[i
], short_name
))
989 short_name_len
= strlen(short_name
);
992 * in strict mode, all (except the matched one) rules
993 * must fail to resolve to a valid non-ambiguous ref
996 rules_to_fail
= nr_rules
;
999 * check if the short name resolves to a valid ref,
1000 * but use only rules prior to the matched one
1002 for (j
= 0; j
< rules_to_fail
; j
++) {
1003 const char *rule
= ref_rev_parse_rules
[j
];
1004 char refname
[PATH_MAX
];
1006 /* skip matched rule */
1011 * the short name is ambiguous, if it resolves
1012 * (with this previous rule) to a valid ref
1013 * read_ref() returns 0 on success
1015 mksnpath(refname
, sizeof(refname
),
1016 rule
, short_name_len
, short_name
);
1017 if (ref_exists(refname
))
1022 * short name is non-ambiguous if all previous rules
1023 * haven't resolved to a valid ref
1025 if (j
== rules_to_fail
)
1030 return xstrdup(refname
);
1033 static struct string_list
*hide_refs
;
1035 int parse_hide_refs_config(const char *var
, const char *value
, const char *section
)
1037 if (!strcmp("transfer.hiderefs", var
) ||
1038 /* NEEDSWORK: use parse_config_key() once both are merged */
1039 (starts_with(var
, section
) && var
[strlen(section
)] == '.' &&
1040 !strcmp(var
+ strlen(section
), ".hiderefs"))) {
1045 return config_error_nonbool(var
);
1046 ref
= xstrdup(value
);
1048 while (len
&& ref
[len
- 1] == '/')
1051 hide_refs
= xcalloc(1, sizeof(*hide_refs
));
1052 hide_refs
->strdup_strings
= 1;
1054 string_list_append(hide_refs
, ref
);
1059 int ref_is_hidden(const char *refname
, const char *refname_full
)
1065 for (i
= hide_refs
->nr
- 1; i
>= 0; i
--) {
1066 const char *match
= hide_refs
->items
[i
].string
;
1067 const char *subject
;
1071 if (*match
== '!') {
1076 if (*match
== '^') {
1077 subject
= refname_full
;
1083 /* refname can be NULL when namespaces are used. */
1084 if (!subject
|| !starts_with(subject
, match
))
1086 len
= strlen(match
);
1087 if (!subject
[len
] || subject
[len
] == '/')
1093 const char *find_descendant_ref(const char *dirname
,
1094 const struct string_list
*extras
,
1095 const struct string_list
*skip
)
1103 * Look at the place where dirname would be inserted into
1104 * extras. If there is an entry at that position that starts
1105 * with dirname (remember, dirname includes the trailing
1106 * slash) and is not in skip, then we have a conflict.
1108 for (pos
= string_list_find_insert_index(extras
, dirname
, 0);
1109 pos
< extras
->nr
; pos
++) {
1110 const char *extra_refname
= extras
->items
[pos
].string
;
1112 if (!starts_with(extra_refname
, dirname
))
1115 if (!skip
|| !string_list_has_string(skip
, extra_refname
))
1116 return extra_refname
;
1121 int rename_ref_available(const char *old_refname
, const char *new_refname
)
1123 struct string_list skip
= STRING_LIST_INIT_NODUP
;
1124 struct strbuf err
= STRBUF_INIT
;
1127 string_list_insert(&skip
, old_refname
);
1128 ok
= !verify_refname_available(new_refname
, NULL
, &skip
, &err
);
1130 error("%s", err
.buf
);
1132 string_list_clear(&skip
, 0);
1133 strbuf_release(&err
);
1137 int head_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1139 struct object_id oid
;
1143 if (resolve_gitlink_ref(submodule
, "HEAD", oid
.hash
) == 0)
1144 return fn("HEAD", &oid
, 0, cb_data
);
1149 if (!read_ref_full("HEAD", RESOLVE_REF_READING
, oid
.hash
, &flag
))
1150 return fn("HEAD", &oid
, flag
, cb_data
);
1155 int head_ref(each_ref_fn fn
, void *cb_data
)
1157 return head_ref_submodule(NULL
, fn
, cb_data
);
1161 * Call fn for each reference in the specified submodule for which the
1162 * refname begins with prefix. If trim is non-zero, then trim that
1163 * many characters off the beginning of each refname before passing
1164 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1165 * include broken references in the iteration. If fn ever returns a
1166 * non-zero value, stop the iteration and return that value;
1167 * otherwise, return 0.
1169 static int do_for_each_ref(const char *submodule
, const char *prefix
,
1170 each_ref_fn fn
, int trim
, int flags
, void *cb_data
)
1172 struct ref_store
*refs
= get_ref_store(submodule
);
1173 struct ref_iterator
*iter
;
1178 iter
= refs
->be
->iterator_begin(refs
, prefix
, flags
);
1179 iter
= prefix_ref_iterator_begin(iter
, prefix
, trim
);
1181 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1184 int for_each_ref(each_ref_fn fn
, void *cb_data
)
1186 return do_for_each_ref(NULL
, "", fn
, 0, 0, cb_data
);
1189 int for_each_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1191 return do_for_each_ref(submodule
, "", fn
, 0, 0, cb_data
);
1194 int for_each_ref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
)
1196 return do_for_each_ref(NULL
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1199 int for_each_fullref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
, unsigned int broken
)
1201 unsigned int flag
= 0;
1204 flag
= DO_FOR_EACH_INCLUDE_BROKEN
;
1205 return do_for_each_ref(NULL
, prefix
, fn
, 0, flag
, cb_data
);
1208 int for_each_ref_in_submodule(const char *submodule
, const char *prefix
,
1209 each_ref_fn fn
, void *cb_data
)
1211 return do_for_each_ref(submodule
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1214 int for_each_replace_ref(each_ref_fn fn
, void *cb_data
)
1216 return do_for_each_ref(NULL
, git_replace_ref_base
, fn
,
1217 strlen(git_replace_ref_base
), 0, cb_data
);
1220 int for_each_namespaced_ref(each_ref_fn fn
, void *cb_data
)
1222 struct strbuf buf
= STRBUF_INIT
;
1224 strbuf_addf(&buf
, "%srefs/", get_git_namespace());
1225 ret
= do_for_each_ref(NULL
, buf
.buf
, fn
, 0, 0, cb_data
);
1226 strbuf_release(&buf
);
1230 int for_each_rawref(each_ref_fn fn
, void *cb_data
)
1232 return do_for_each_ref(NULL
, "", fn
, 0,
1233 DO_FOR_EACH_INCLUDE_BROKEN
, cb_data
);
1236 /* This function needs to return a meaningful errno on failure */
1237 static const char *resolve_ref_recursively(struct ref_store
*refs
,
1238 const char *refname
,
1240 unsigned char *sha1
, int *flags
)
1242 static struct strbuf sb_refname
= STRBUF_INIT
;
1247 flags
= &unused_flags
;
1251 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1252 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1253 !refname_is_safe(refname
)) {
1259 * dwim_ref() uses REF_ISBROKEN to distinguish between
1260 * missing refs and refs that were present but invalid,
1261 * to complain about the latter to stderr.
1263 * We don't know whether the ref exists, so don't set
1266 *flags
|= REF_BAD_NAME
;
1269 for (symref_count
= 0; symref_count
< SYMREF_MAXDEPTH
; symref_count
++) {
1270 unsigned int read_flags
= 0;
1272 if (refs
->be
->read_raw_ref(refs
, refname
,
1273 sha1
, &sb_refname
, &read_flags
)) {
1274 *flags
|= read_flags
;
1275 if (errno
!= ENOENT
|| (resolve_flags
& RESOLVE_REF_READING
))
1278 if (*flags
& REF_BAD_NAME
)
1279 *flags
|= REF_ISBROKEN
;
1283 *flags
|= read_flags
;
1285 if (!(read_flags
& REF_ISSYMREF
)) {
1286 if (*flags
& REF_BAD_NAME
) {
1288 *flags
|= REF_ISBROKEN
;
1293 refname
= sb_refname
.buf
;
1294 if (resolve_flags
& RESOLVE_REF_NO_RECURSE
) {
1298 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1299 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1300 !refname_is_safe(refname
)) {
1305 *flags
|= REF_ISBROKEN
| REF_BAD_NAME
;
1313 /* backend functions */
1314 int refs_init_db(struct strbuf
*err
)
1316 struct ref_store
*refs
= get_ref_store(NULL
);
1318 return refs
->be
->init_db(refs
, err
);
1321 const char *resolve_ref_unsafe(const char *refname
, int resolve_flags
,
1322 unsigned char *sha1
, int *flags
)
1324 return resolve_ref_recursively(get_ref_store(NULL
), refname
,
1325 resolve_flags
, sha1
, flags
);
1328 int resolve_gitlink_ref(const char *submodule
, const char *refname
,
1329 unsigned char *sha1
)
1331 size_t len
= strlen(submodule
);
1332 struct ref_store
*refs
;
1335 while (len
&& submodule
[len
- 1] == '/')
1341 if (submodule
[len
]) {
1342 /* We need to strip off one or more trailing slashes */
1343 char *stripped
= xmemdupz(submodule
, len
);
1345 refs
= get_ref_store(stripped
);
1348 refs
= get_ref_store(submodule
);
1354 if (!resolve_ref_recursively(refs
, refname
, 0, sha1
, &flags
) ||
1360 /* A pointer to the ref_store for the main repository: */
1361 static struct ref_store
*main_ref_store
;
1363 /* A linked list of ref_stores for submodules: */
1364 static struct ref_store
*submodule_ref_stores
;
1366 void base_ref_store_init(struct ref_store
*refs
,
1367 const struct ref_storage_be
*be
,
1368 const char *submodule
)
1373 die("BUG: main_ref_store initialized twice");
1375 refs
->submodule
= "";
1377 main_ref_store
= refs
;
1379 if (lookup_ref_store(submodule
))
1380 die("BUG: ref_store for submodule '%s' initialized twice",
1383 refs
->submodule
= xstrdup(submodule
);
1384 refs
->next
= submodule_ref_stores
;
1385 submodule_ref_stores
= refs
;
1389 struct ref_store
*ref_store_init(const char *submodule
)
1391 const char *be_name
= "files";
1392 struct ref_storage_be
*be
= find_ref_storage_backend(be_name
);
1395 die("BUG: reference backend %s is unknown", be_name
);
1397 if (!submodule
|| !*submodule
)
1398 return be
->init(NULL
);
1400 return be
->init(submodule
);
1403 struct ref_store
*lookup_ref_store(const char *submodule
)
1405 struct ref_store
*refs
;
1407 if (!submodule
|| !*submodule
)
1408 return main_ref_store
;
1410 for (refs
= submodule_ref_stores
; refs
; refs
= refs
->next
) {
1411 if (!strcmp(submodule
, refs
->submodule
))
1418 struct ref_store
*get_ref_store(const char *submodule
)
1420 struct ref_store
*refs
;
1422 if (!submodule
|| !*submodule
) {
1423 refs
= lookup_ref_store(NULL
);
1426 refs
= ref_store_init(NULL
);
1428 refs
= lookup_ref_store(submodule
);
1431 struct strbuf submodule_sb
= STRBUF_INIT
;
1433 strbuf_addstr(&submodule_sb
, submodule
);
1434 if (is_nonbare_repository_dir(&submodule_sb
))
1435 refs
= ref_store_init(submodule
);
1436 strbuf_release(&submodule_sb
);
1443 void assert_main_repository(struct ref_store
*refs
, const char *caller
)
1445 if (*refs
->submodule
)
1446 die("BUG: %s called for a submodule", caller
);
1449 /* backend functions */
1450 int pack_refs(unsigned int flags
)
1452 struct ref_store
*refs
= get_ref_store(NULL
);
1454 return refs
->be
->pack_refs(refs
, flags
);
1457 int peel_ref(const char *refname
, unsigned char *sha1
)
1459 struct ref_store
*refs
= get_ref_store(NULL
);
1461 return refs
->be
->peel_ref(refs
, refname
, sha1
);
1464 int create_symref(const char *ref_target
, const char *refs_heads_master
,
1467 struct ref_store
*refs
= get_ref_store(NULL
);
1469 return refs
->be
->create_symref(refs
, ref_target
, refs_heads_master
,
1473 int ref_transaction_commit(struct ref_transaction
*transaction
,
1476 struct ref_store
*refs
= get_ref_store(NULL
);
1478 return refs
->be
->transaction_commit(refs
, transaction
, err
);
1481 int verify_refname_available(const char *refname
,
1482 const struct string_list
*extra
,
1483 const struct string_list
*skip
,
1486 struct ref_store
*refs
= get_ref_store(NULL
);
1488 return refs
->be
->verify_refname_available(refs
, refname
, extra
, skip
, err
);
1491 int for_each_reflog(each_ref_fn fn
, void *cb_data
)
1493 struct ref_store
*refs
= get_ref_store(NULL
);
1494 struct ref_iterator
*iter
;
1496 iter
= refs
->be
->reflog_iterator_begin(refs
);
1498 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1501 int for_each_reflog_ent_reverse(const char *refname
, each_reflog_ent_fn fn
,
1504 struct ref_store
*refs
= get_ref_store(NULL
);
1506 return refs
->be
->for_each_reflog_ent_reverse(refs
, refname
,
1510 int for_each_reflog_ent(const char *refname
, each_reflog_ent_fn fn
,
1513 struct ref_store
*refs
= get_ref_store(NULL
);
1515 return refs
->be
->for_each_reflog_ent(refs
, refname
, fn
, cb_data
);
1518 int reflog_exists(const char *refname
)
1520 struct ref_store
*refs
= get_ref_store(NULL
);
1522 return refs
->be
->reflog_exists(refs
, refname
);
1525 int safe_create_reflog(const char *refname
, int force_create
,
1528 struct ref_store
*refs
= get_ref_store(NULL
);
1530 return refs
->be
->create_reflog(refs
, refname
, force_create
, err
);
1533 int delete_reflog(const char *refname
)
1535 struct ref_store
*refs
= get_ref_store(NULL
);
1537 return refs
->be
->delete_reflog(refs
, refname
);
1540 int reflog_expire(const char *refname
, const unsigned char *sha1
,
1542 reflog_expiry_prepare_fn prepare_fn
,
1543 reflog_expiry_should_prune_fn should_prune_fn
,
1544 reflog_expiry_cleanup_fn cleanup_fn
,
1545 void *policy_cb_data
)
1547 struct ref_store
*refs
= get_ref_store(NULL
);
1549 return refs
->be
->reflog_expire(refs
, refname
, sha1
, flags
,
1550 prepare_fn
, should_prune_fn
,
1551 cleanup_fn
, policy_cb_data
);
1554 int initial_ref_transaction_commit(struct ref_transaction
*transaction
,
1557 struct ref_store
*refs
= get_ref_store(NULL
);
1559 return refs
->be
->initial_transaction_commit(refs
, transaction
, err
);
1562 int delete_refs(struct string_list
*refnames
, unsigned int flags
)
1564 struct ref_store
*refs
= get_ref_store(NULL
);
1566 return refs
->be
->delete_refs(refs
, refnames
, flags
);
1569 int rename_ref(const char *oldref
, const char *newref
, const char *logmsg
)
1571 struct ref_store
*refs
= get_ref_store(NULL
);
1573 return refs
->be
->rename_ref(refs
, oldref
, newref
, logmsg
);