2 * The backend-independent part of the reference module.
10 #include "refs/refs-internal.h"
13 #include "submodule.h"
17 * List of all available backends
19 static struct ref_storage_be
*refs_backends
= &refs_be_files
;
21 static struct ref_storage_be
*find_ref_storage_backend(const char *name
)
23 struct ref_storage_be
*be
;
24 for (be
= refs_backends
; be
; be
= be
->next
)
25 if (!strcmp(be
->name
, name
))
30 int ref_storage_backend_exists(const char *name
)
32 return find_ref_storage_backend(name
) != NULL
;
36 * How to handle various characters in refnames:
37 * 0: An acceptable character for refs
39 * 2: ., look for a preceding . to reject .. in refs
40 * 3: {, look for a preceding @ to reject @{ in refs
41 * 4: A bad character: ASCII control characters, and
42 * ":", "?", "[", "\", "^", "~", SP, or TAB
43 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
45 static unsigned char refname_disposition
[256] = {
46 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
47 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
48 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
57 * Try to read one refname component from the front of refname.
58 * Return the length of the component found, or -1 if the component is
59 * not legal. It is legal if it is something reasonable to have under
60 * ".git/refs/"; We do not like it if:
62 * - any path component of it begins with ".", or
63 * - it has double dots "..", or
64 * - it has ASCII control characters, or
65 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
66 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
67 * - it ends with a "/", or
68 * - it ends with ".lock", or
69 * - it contains a "@{" portion
71 static int check_refname_component(const char *refname
, int *flags
)
76 for (cp
= refname
; ; cp
++) {
78 unsigned char disp
= refname_disposition
[ch
];
84 return -1; /* Refname contains "..". */
88 return -1; /* Refname contains "@{". */
93 if (!(*flags
& REFNAME_REFSPEC_PATTERN
))
94 return -1; /* refspec can't be a pattern */
97 * Unset the pattern flag so that we only accept
98 * a single asterisk for one side of refspec.
100 *flags
&= ~ REFNAME_REFSPEC_PATTERN
;
107 return 0; /* Component has zero length. */
108 if (refname
[0] == '.')
109 return -1; /* Component starts with '.'. */
110 if (cp
- refname
>= LOCK_SUFFIX_LEN
&&
111 !memcmp(cp
- LOCK_SUFFIX_LEN
, LOCK_SUFFIX
, LOCK_SUFFIX_LEN
))
112 return -1; /* Refname ends with ".lock". */
116 int check_refname_format(const char *refname
, int flags
)
118 int component_len
, component_count
= 0;
120 if (!strcmp(refname
, "@"))
121 /* Refname is a single character '@'. */
125 /* We are at the start of a path component. */
126 component_len
= check_refname_component(refname
, &flags
);
127 if (component_len
<= 0)
131 if (refname
[component_len
] == '\0')
133 /* Skip to next component. */
134 refname
+= component_len
+ 1;
137 if (refname
[component_len
- 1] == '.')
138 return -1; /* Refname ends with '.'. */
139 if (!(flags
& REFNAME_ALLOW_ONELEVEL
) && component_count
< 2)
140 return -1; /* Refname has only one component. */
144 int refname_is_safe(const char *refname
)
148 if (skip_prefix(refname
, "refs/", &rest
)) {
151 size_t restlen
= strlen(rest
);
153 /* rest must not be empty, or start or end with "/" */
154 if (!restlen
|| *rest
== '/' || rest
[restlen
- 1] == '/')
158 * Does the refname try to escape refs/?
159 * For example: refs/foo/../bar is safe but refs/foo/../../bar
162 buf
= xmallocz(restlen
);
163 result
= !normalize_path_copy(buf
, rest
) && !strcmp(buf
, rest
);
169 if (!isupper(*refname
) && *refname
!= '_')
176 char *refs_resolve_refdup(struct ref_store
*refs
,
177 const char *refname
, int resolve_flags
,
178 unsigned char *sha1
, int *flags
)
182 result
= refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
,
184 return xstrdup_or_null(result
);
187 char *resolve_refdup(const char *refname
, int resolve_flags
,
188 unsigned char *sha1
, int *flags
)
190 return refs_resolve_refdup(get_main_ref_store(),
191 refname
, resolve_flags
,
195 /* The argument to filter_refs */
202 int refs_read_ref_full(struct ref_store
*refs
, const char *refname
,
203 int resolve_flags
, unsigned char *sha1
, int *flags
)
205 if (refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
, sha1
, flags
))
210 int read_ref_full(const char *refname
, int resolve_flags
, unsigned char *sha1
, int *flags
)
212 return refs_read_ref_full(get_main_ref_store(), refname
,
213 resolve_flags
, sha1
, flags
);
216 int read_ref(const char *refname
, unsigned char *sha1
)
218 return read_ref_full(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
221 int ref_exists(const char *refname
)
223 unsigned char sha1
[20];
224 return !!resolve_ref_unsafe(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
227 static int filter_refs(const char *refname
, const struct object_id
*oid
,
228 int flags
, void *data
)
230 struct ref_filter
*filter
= (struct ref_filter
*)data
;
232 if (wildmatch(filter
->pattern
, refname
, 0, NULL
))
234 return filter
->fn(refname
, oid
, flags
, filter
->cb_data
);
237 enum peel_status
peel_object(const unsigned char *name
, unsigned char *sha1
)
239 struct object
*o
= lookup_unknown_object(name
);
241 if (o
->type
== OBJ_NONE
) {
242 int type
= sha1_object_info(name
, NULL
);
243 if (type
< 0 || !object_as_type(o
, type
, 0))
247 if (o
->type
!= OBJ_TAG
)
250 o
= deref_tag_noverify(o
);
254 hashcpy(sha1
, o
->oid
.hash
);
258 struct warn_if_dangling_data
{
261 const struct string_list
*refnames
;
265 static int warn_if_dangling_symref(const char *refname
, const struct object_id
*oid
,
266 int flags
, void *cb_data
)
268 struct warn_if_dangling_data
*d
= cb_data
;
269 const char *resolves_to
;
270 struct object_id junk
;
272 if (!(flags
& REF_ISSYMREF
))
275 resolves_to
= resolve_ref_unsafe(refname
, 0, junk
.hash
, NULL
);
278 ? strcmp(resolves_to
, d
->refname
)
279 : !string_list_has_string(d
->refnames
, resolves_to
))) {
283 fprintf(d
->fp
, d
->msg_fmt
, refname
);
288 void warn_dangling_symref(FILE *fp
, const char *msg_fmt
, const char *refname
)
290 struct warn_if_dangling_data data
;
293 data
.refname
= refname
;
294 data
.refnames
= NULL
;
295 data
.msg_fmt
= msg_fmt
;
296 for_each_rawref(warn_if_dangling_symref
, &data
);
299 void warn_dangling_symrefs(FILE *fp
, const char *msg_fmt
, const struct string_list
*refnames
)
301 struct warn_if_dangling_data data
;
305 data
.refnames
= refnames
;
306 data
.msg_fmt
= msg_fmt
;
307 for_each_rawref(warn_if_dangling_symref
, &data
);
310 int refs_for_each_tag_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
312 return refs_for_each_ref_in(refs
, "refs/tags/", fn
, cb_data
);
315 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
317 return refs_for_each_tag_ref(get_main_ref_store(), fn
, cb_data
);
320 int for_each_tag_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
322 return refs_for_each_tag_ref(get_submodule_ref_store(submodule
),
326 int refs_for_each_branch_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
328 return refs_for_each_ref_in(refs
, "refs/heads/", fn
, cb_data
);
331 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
333 return refs_for_each_branch_ref(get_main_ref_store(), fn
, cb_data
);
336 int for_each_branch_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
338 return refs_for_each_branch_ref(get_submodule_ref_store(submodule
),
342 int refs_for_each_remote_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
344 return refs_for_each_ref_in(refs
, "refs/remotes/", fn
, cb_data
);
347 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
349 return refs_for_each_remote_ref(get_main_ref_store(), fn
, cb_data
);
352 int for_each_remote_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
354 return refs_for_each_remote_ref(get_submodule_ref_store(submodule
),
358 int head_ref_namespaced(each_ref_fn fn
, void *cb_data
)
360 struct strbuf buf
= STRBUF_INIT
;
362 struct object_id oid
;
365 strbuf_addf(&buf
, "%sHEAD", get_git_namespace());
366 if (!read_ref_full(buf
.buf
, RESOLVE_REF_READING
, oid
.hash
, &flag
))
367 ret
= fn(buf
.buf
, &oid
, flag
, cb_data
);
368 strbuf_release(&buf
);
373 int for_each_glob_ref_in(each_ref_fn fn
, const char *pattern
,
374 const char *prefix
, void *cb_data
)
376 struct strbuf real_pattern
= STRBUF_INIT
;
377 struct ref_filter filter
;
380 if (!prefix
&& !starts_with(pattern
, "refs/"))
381 strbuf_addstr(&real_pattern
, "refs/");
383 strbuf_addstr(&real_pattern
, prefix
);
384 strbuf_addstr(&real_pattern
, pattern
);
386 if (!has_glob_specials(pattern
)) {
387 /* Append implied '/' '*' if not present. */
388 strbuf_complete(&real_pattern
, '/');
389 /* No need to check for '*', there is none. */
390 strbuf_addch(&real_pattern
, '*');
393 filter
.pattern
= real_pattern
.buf
;
395 filter
.cb_data
= cb_data
;
396 ret
= for_each_ref(filter_refs
, &filter
);
398 strbuf_release(&real_pattern
);
402 int for_each_glob_ref(each_ref_fn fn
, const char *pattern
, void *cb_data
)
404 return for_each_glob_ref_in(fn
, pattern
, NULL
, cb_data
);
407 const char *prettify_refname(const char *name
)
409 if (skip_prefix(name
, "refs/heads/", &name
) ||
410 skip_prefix(name
, "refs/tags/", &name
) ||
411 skip_prefix(name
, "refs/remotes/", &name
))
416 static const char *ref_rev_parse_rules
[] = {
422 "refs/remotes/%.*s/HEAD",
426 int refname_match(const char *abbrev_name
, const char *full_name
)
429 const int abbrev_name_len
= strlen(abbrev_name
);
431 for (p
= ref_rev_parse_rules
; *p
; p
++) {
432 if (!strcmp(full_name
, mkpath(*p
, abbrev_name_len
, abbrev_name
))) {
441 * *string and *len will only be substituted, and *string returned (for
442 * later free()ing) if the string passed in is a magic short-hand form
445 static char *substitute_branch_name(const char **string
, int *len
)
447 struct strbuf buf
= STRBUF_INIT
;
448 int ret
= interpret_branch_name(*string
, *len
, &buf
, 0);
452 *string
= strbuf_detach(&buf
, &size
);
454 return (char *)*string
;
460 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
462 char *last_branch
= substitute_branch_name(&str
, &len
);
463 int refs_found
= expand_ref(str
, len
, sha1
, ref
);
468 int expand_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
472 struct strbuf fullref
= STRBUF_INIT
;
475 for (p
= ref_rev_parse_rules
; *p
; p
++) {
476 unsigned char sha1_from_ref
[20];
477 unsigned char *this_result
;
480 this_result
= refs_found
? sha1_from_ref
: sha1
;
481 strbuf_reset(&fullref
);
482 strbuf_addf(&fullref
, *p
, len
, str
);
483 r
= resolve_ref_unsafe(fullref
.buf
, RESOLVE_REF_READING
,
488 if (!warn_ambiguous_refs
)
490 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
.buf
, "HEAD")) {
491 warning("ignoring dangling symref %s.", fullref
.buf
);
492 } else if ((flag
& REF_ISBROKEN
) && strchr(fullref
.buf
, '/')) {
493 warning("ignoring broken ref %s.", fullref
.buf
);
496 strbuf_release(&fullref
);
500 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
502 char *last_branch
= substitute_branch_name(&str
, &len
);
505 struct strbuf path
= STRBUF_INIT
;
508 for (p
= ref_rev_parse_rules
; *p
; p
++) {
509 unsigned char hash
[20];
510 const char *ref
, *it
;
513 strbuf_addf(&path
, *p
, len
, str
);
514 ref
= resolve_ref_unsafe(path
.buf
, RESOLVE_REF_READING
,
518 if (reflog_exists(path
.buf
))
520 else if (strcmp(ref
, path
.buf
) && reflog_exists(ref
))
528 if (!warn_ambiguous_refs
)
531 strbuf_release(&path
);
536 static int is_per_worktree_ref(const char *refname
)
538 return !strcmp(refname
, "HEAD") ||
539 starts_with(refname
, "refs/bisect/");
542 static int is_pseudoref_syntax(const char *refname
)
546 for (c
= refname
; *c
; c
++) {
547 if (!isupper(*c
) && *c
!= '-' && *c
!= '_')
554 enum ref_type
ref_type(const char *refname
)
556 if (is_per_worktree_ref(refname
))
557 return REF_TYPE_PER_WORKTREE
;
558 if (is_pseudoref_syntax(refname
))
559 return REF_TYPE_PSEUDOREF
;
560 return REF_TYPE_NORMAL
;
563 static int write_pseudoref(const char *pseudoref
, const unsigned char *sha1
,
564 const unsigned char *old_sha1
, struct strbuf
*err
)
566 const char *filename
;
568 static struct lock_file lock
;
569 struct strbuf buf
= STRBUF_INIT
;
572 strbuf_addf(&buf
, "%s\n", sha1_to_hex(sha1
));
574 filename
= git_path("%s", pseudoref
);
575 fd
= hold_lock_file_for_update(&lock
, filename
, LOCK_DIE_ON_ERROR
);
577 strbuf_addf(err
, "could not open '%s' for writing: %s",
578 filename
, strerror(errno
));
583 unsigned char actual_old_sha1
[20];
585 if (read_ref(pseudoref
, actual_old_sha1
))
586 die("could not read ref '%s'", pseudoref
);
587 if (hashcmp(actual_old_sha1
, old_sha1
)) {
588 strbuf_addf(err
, "unexpected sha1 when writing '%s'", pseudoref
);
589 rollback_lock_file(&lock
);
594 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
) {
595 strbuf_addf(err
, "could not write to '%s'", filename
);
596 rollback_lock_file(&lock
);
600 commit_lock_file(&lock
);
603 strbuf_release(&buf
);
607 static int delete_pseudoref(const char *pseudoref
, const unsigned char *old_sha1
)
609 static struct lock_file lock
;
610 const char *filename
;
612 filename
= git_path("%s", pseudoref
);
614 if (old_sha1
&& !is_null_sha1(old_sha1
)) {
616 unsigned char actual_old_sha1
[20];
618 fd
= hold_lock_file_for_update(&lock
, filename
,
621 die_errno(_("Could not open '%s' for writing"), filename
);
622 if (read_ref(pseudoref
, actual_old_sha1
))
623 die("could not read ref '%s'", pseudoref
);
624 if (hashcmp(actual_old_sha1
, old_sha1
)) {
625 warning("Unexpected sha1 when deleting %s", pseudoref
);
626 rollback_lock_file(&lock
);
631 rollback_lock_file(&lock
);
639 int refs_delete_ref(struct ref_store
*refs
, const char *msg
,
641 const unsigned char *old_sha1
,
644 struct ref_transaction
*transaction
;
645 struct strbuf err
= STRBUF_INIT
;
647 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
) {
648 assert(refs
== get_main_ref_store());
649 return delete_pseudoref(refname
, old_sha1
);
652 transaction
= ref_store_transaction_begin(refs
, &err
);
654 ref_transaction_delete(transaction
, refname
, old_sha1
,
656 ref_transaction_commit(transaction
, &err
)) {
657 error("%s", err
.buf
);
658 ref_transaction_free(transaction
);
659 strbuf_release(&err
);
662 ref_transaction_free(transaction
);
663 strbuf_release(&err
);
667 int delete_ref(const char *msg
, const char *refname
,
668 const unsigned char *old_sha1
, unsigned int flags
)
670 return refs_delete_ref(get_main_ref_store(), msg
, refname
,
674 int copy_reflog_msg(char *buf
, const char *msg
)
681 while ((c
= *msg
++)) {
682 if (wasspace
&& isspace(c
))
684 wasspace
= isspace(c
);
689 while (buf
< cp
&& isspace(cp
[-1]))
695 int should_autocreate_reflog(const char *refname
)
697 switch (log_all_ref_updates
) {
698 case LOG_REFS_ALWAYS
:
700 case LOG_REFS_NORMAL
:
701 return starts_with(refname
, "refs/heads/") ||
702 starts_with(refname
, "refs/remotes/") ||
703 starts_with(refname
, "refs/notes/") ||
704 !strcmp(refname
, "HEAD");
710 int is_branch(const char *refname
)
712 return !strcmp(refname
, "HEAD") || starts_with(refname
, "refs/heads/");
715 struct read_ref_at_cb
{
723 unsigned char osha1
[20];
724 unsigned char nsha1
[20];
728 timestamp_t
*cutoff_time
;
733 static int read_ref_at_ent(struct object_id
*ooid
, struct object_id
*noid
,
734 const char *email
, timestamp_t timestamp
, int tz
,
735 const char *message
, void *cb_data
)
737 struct read_ref_at_cb
*cb
= cb_data
;
741 cb
->date
= timestamp
;
743 if (timestamp
<= cb
->at_time
|| cb
->cnt
== 0) {
745 *cb
->msg
= xstrdup(message
);
747 *cb
->cutoff_time
= timestamp
;
751 *cb
->cutoff_cnt
= cb
->reccnt
- 1;
753 * we have not yet updated cb->[n|o]sha1 so they still
754 * hold the values for the previous record.
756 if (!is_null_sha1(cb
->osha1
)) {
757 hashcpy(cb
->sha1
, noid
->hash
);
758 if (hashcmp(cb
->osha1
, noid
->hash
))
759 warning("Log for ref %s has gap after %s.",
760 cb
->refname
, show_date(cb
->date
, cb
->tz
, DATE_MODE(RFC2822
)));
762 else if (cb
->date
== cb
->at_time
)
763 hashcpy(cb
->sha1
, noid
->hash
);
764 else if (hashcmp(noid
->hash
, cb
->sha1
))
765 warning("Log for ref %s unexpectedly ended on %s.",
766 cb
->refname
, show_date(cb
->date
, cb
->tz
,
767 DATE_MODE(RFC2822
)));
768 hashcpy(cb
->osha1
, ooid
->hash
);
769 hashcpy(cb
->nsha1
, noid
->hash
);
773 hashcpy(cb
->osha1
, ooid
->hash
);
774 hashcpy(cb
->nsha1
, noid
->hash
);
780 static int read_ref_at_ent_oldest(struct object_id
*ooid
, struct object_id
*noid
,
781 const char *email
, timestamp_t timestamp
,
782 int tz
, const char *message
, void *cb_data
)
784 struct read_ref_at_cb
*cb
= cb_data
;
787 *cb
->msg
= xstrdup(message
);
789 *cb
->cutoff_time
= timestamp
;
793 *cb
->cutoff_cnt
= cb
->reccnt
;
794 hashcpy(cb
->sha1
, ooid
->hash
);
795 if (is_null_sha1(cb
->sha1
))
796 hashcpy(cb
->sha1
, noid
->hash
);
797 /* We just want the first entry */
801 int read_ref_at(const char *refname
, unsigned int flags
, timestamp_t at_time
, int cnt
,
802 unsigned char *sha1
, char **msg
,
803 timestamp_t
*cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
805 struct read_ref_at_cb cb
;
807 memset(&cb
, 0, sizeof(cb
));
808 cb
.refname
= refname
;
809 cb
.at_time
= at_time
;
812 cb
.cutoff_time
= cutoff_time
;
813 cb
.cutoff_tz
= cutoff_tz
;
814 cb
.cutoff_cnt
= cutoff_cnt
;
817 for_each_reflog_ent_reverse(refname
, read_ref_at_ent
, &cb
);
820 if (flags
& GET_SHA1_QUIETLY
)
823 die("Log for %s is empty.", refname
);
828 for_each_reflog_ent(refname
, read_ref_at_ent_oldest
, &cb
);
833 struct ref_transaction
*ref_store_transaction_begin(struct ref_store
*refs
,
836 struct ref_transaction
*tr
;
839 tr
= xcalloc(1, sizeof(struct ref_transaction
));
840 tr
->ref_store
= refs
;
844 struct ref_transaction
*ref_transaction_begin(struct strbuf
*err
)
846 return ref_store_transaction_begin(get_main_ref_store(), err
);
849 void ref_transaction_free(struct ref_transaction
*transaction
)
856 switch (transaction
->state
) {
857 case REF_TRANSACTION_OPEN
:
858 case REF_TRANSACTION_CLOSED
:
861 case REF_TRANSACTION_PREPARED
:
862 die("BUG: free called on a prepared reference transaction");
865 die("BUG: unexpected reference transaction state");
869 for (i
= 0; i
< transaction
->nr
; i
++) {
870 free(transaction
->updates
[i
]->msg
);
871 free(transaction
->updates
[i
]);
873 free(transaction
->updates
);
877 struct ref_update
*ref_transaction_add_update(
878 struct ref_transaction
*transaction
,
879 const char *refname
, unsigned int flags
,
880 const unsigned char *new_sha1
,
881 const unsigned char *old_sha1
,
884 struct ref_update
*update
;
886 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
887 die("BUG: update called for transaction that is not open");
889 if ((flags
& REF_ISPRUNING
) && !(flags
& REF_NODEREF
))
890 die("BUG: REF_ISPRUNING set without REF_NODEREF");
892 FLEX_ALLOC_STR(update
, refname
, refname
);
893 ALLOC_GROW(transaction
->updates
, transaction
->nr
+ 1, transaction
->alloc
);
894 transaction
->updates
[transaction
->nr
++] = update
;
896 update
->flags
= flags
;
898 if (flags
& REF_HAVE_NEW
)
899 hashcpy(update
->new_oid
.hash
, new_sha1
);
900 if (flags
& REF_HAVE_OLD
)
901 hashcpy(update
->old_oid
.hash
, old_sha1
);
902 update
->msg
= xstrdup_or_null(msg
);
906 int ref_transaction_update(struct ref_transaction
*transaction
,
908 const unsigned char *new_sha1
,
909 const unsigned char *old_sha1
,
910 unsigned int flags
, const char *msg
,
915 if ((new_sha1
&& !is_null_sha1(new_sha1
)) ?
916 check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
) :
917 !refname_is_safe(refname
)) {
918 strbuf_addf(err
, "refusing to update ref with bad name '%s'",
923 flags
|= (new_sha1
? REF_HAVE_NEW
: 0) | (old_sha1
? REF_HAVE_OLD
: 0);
925 ref_transaction_add_update(transaction
, refname
, flags
,
926 new_sha1
, old_sha1
, msg
);
930 int ref_transaction_create(struct ref_transaction
*transaction
,
932 const unsigned char *new_sha1
,
933 unsigned int flags
, const char *msg
,
936 if (!new_sha1
|| is_null_sha1(new_sha1
))
937 die("BUG: create called without valid new_sha1");
938 return ref_transaction_update(transaction
, refname
, new_sha1
,
939 null_sha1
, flags
, msg
, err
);
942 int ref_transaction_delete(struct ref_transaction
*transaction
,
944 const unsigned char *old_sha1
,
945 unsigned int flags
, const char *msg
,
948 if (old_sha1
&& is_null_sha1(old_sha1
))
949 die("BUG: delete called with old_sha1 set to zeros");
950 return ref_transaction_update(transaction
, refname
,
955 int ref_transaction_verify(struct ref_transaction
*transaction
,
957 const unsigned char *old_sha1
,
962 die("BUG: verify called with old_sha1 set to NULL");
963 return ref_transaction_update(transaction
, refname
,
968 int update_ref_oid(const char *msg
, const char *refname
,
969 const struct object_id
*new_oid
, const struct object_id
*old_oid
,
970 unsigned int flags
, enum action_on_err onerr
)
972 return update_ref(msg
, refname
, new_oid
? new_oid
->hash
: NULL
,
973 old_oid
? old_oid
->hash
: NULL
, flags
, onerr
);
976 int refs_update_ref(struct ref_store
*refs
, const char *msg
,
977 const char *refname
, const unsigned char *new_sha1
,
978 const unsigned char *old_sha1
, unsigned int flags
,
979 enum action_on_err onerr
)
981 struct ref_transaction
*t
= NULL
;
982 struct strbuf err
= STRBUF_INIT
;
985 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
) {
986 assert(refs
== get_main_ref_store());
987 ret
= write_pseudoref(refname
, new_sha1
, old_sha1
, &err
);
989 t
= ref_store_transaction_begin(refs
, &err
);
991 ref_transaction_update(t
, refname
, new_sha1
, old_sha1
,
993 ref_transaction_commit(t
, &err
)) {
995 ref_transaction_free(t
);
999 const char *str
= "update_ref failed for ref '%s': %s";
1002 case UPDATE_REFS_MSG_ON_ERR
:
1003 error(str
, refname
, err
.buf
);
1005 case UPDATE_REFS_DIE_ON_ERR
:
1006 die(str
, refname
, err
.buf
);
1008 case UPDATE_REFS_QUIET_ON_ERR
:
1011 strbuf_release(&err
);
1014 strbuf_release(&err
);
1016 ref_transaction_free(t
);
1020 int update_ref(const char *msg
, const char *refname
,
1021 const unsigned char *new_sha1
,
1022 const unsigned char *old_sha1
,
1023 unsigned int flags
, enum action_on_err onerr
)
1025 return refs_update_ref(get_main_ref_store(), msg
, refname
, new_sha1
,
1026 old_sha1
, flags
, onerr
);
1029 char *shorten_unambiguous_ref(const char *refname
, int strict
)
1032 static char **scanf_fmts
;
1033 static int nr_rules
;
1035 struct strbuf resolved_buf
= STRBUF_INIT
;
1039 * Pre-generate scanf formats from ref_rev_parse_rules[].
1040 * Generate a format suitable for scanf from a
1041 * ref_rev_parse_rules rule by interpolating "%s" at the
1042 * location of the "%.*s".
1044 size_t total_len
= 0;
1047 /* the rule list is NULL terminated, count them first */
1048 for (nr_rules
= 0; ref_rev_parse_rules
[nr_rules
]; nr_rules
++)
1049 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
1050 total_len
+= strlen(ref_rev_parse_rules
[nr_rules
]) - 2 + 1;
1052 scanf_fmts
= xmalloc(st_add(st_mult(sizeof(char *), nr_rules
), total_len
));
1055 for (i
= 0; i
< nr_rules
; i
++) {
1056 assert(offset
< total_len
);
1057 scanf_fmts
[i
] = (char *)&scanf_fmts
[nr_rules
] + offset
;
1058 offset
+= snprintf(scanf_fmts
[i
], total_len
- offset
,
1059 ref_rev_parse_rules
[i
], 2, "%s") + 1;
1063 /* bail out if there are no rules */
1065 return xstrdup(refname
);
1067 /* buffer for scanf result, at most refname must fit */
1068 short_name
= xstrdup(refname
);
1070 /* skip first rule, it will always match */
1071 for (i
= nr_rules
- 1; i
> 0 ; --i
) {
1073 int rules_to_fail
= i
;
1076 if (1 != sscanf(refname
, scanf_fmts
[i
], short_name
))
1079 short_name_len
= strlen(short_name
);
1082 * in strict mode, all (except the matched one) rules
1083 * must fail to resolve to a valid non-ambiguous ref
1086 rules_to_fail
= nr_rules
;
1089 * check if the short name resolves to a valid ref,
1090 * but use only rules prior to the matched one
1092 for (j
= 0; j
< rules_to_fail
; j
++) {
1093 const char *rule
= ref_rev_parse_rules
[j
];
1095 /* skip matched rule */
1100 * the short name is ambiguous, if it resolves
1101 * (with this previous rule) to a valid ref
1102 * read_ref() returns 0 on success
1104 strbuf_reset(&resolved_buf
);
1105 strbuf_addf(&resolved_buf
, rule
,
1106 short_name_len
, short_name
);
1107 if (ref_exists(resolved_buf
.buf
))
1112 * short name is non-ambiguous if all previous rules
1113 * haven't resolved to a valid ref
1115 if (j
== rules_to_fail
) {
1116 strbuf_release(&resolved_buf
);
1121 strbuf_release(&resolved_buf
);
1123 return xstrdup(refname
);
1126 static struct string_list
*hide_refs
;
1128 int parse_hide_refs_config(const char *var
, const char *value
, const char *section
)
1131 if (!strcmp("transfer.hiderefs", var
) ||
1132 (!parse_config_key(var
, section
, NULL
, NULL
, &key
) &&
1133 !strcmp(key
, "hiderefs"))) {
1138 return config_error_nonbool(var
);
1139 ref
= xstrdup(value
);
1141 while (len
&& ref
[len
- 1] == '/')
1144 hide_refs
= xcalloc(1, sizeof(*hide_refs
));
1145 hide_refs
->strdup_strings
= 1;
1147 string_list_append(hide_refs
, ref
);
1152 int ref_is_hidden(const char *refname
, const char *refname_full
)
1158 for (i
= hide_refs
->nr
- 1; i
>= 0; i
--) {
1159 const char *match
= hide_refs
->items
[i
].string
;
1160 const char *subject
;
1164 if (*match
== '!') {
1169 if (*match
== '^') {
1170 subject
= refname_full
;
1176 /* refname can be NULL when namespaces are used. */
1177 if (!subject
|| !starts_with(subject
, match
))
1179 len
= strlen(match
);
1180 if (!subject
[len
] || subject
[len
] == '/')
1186 const char *find_descendant_ref(const char *dirname
,
1187 const struct string_list
*extras
,
1188 const struct string_list
*skip
)
1196 * Look at the place where dirname would be inserted into
1197 * extras. If there is an entry at that position that starts
1198 * with dirname (remember, dirname includes the trailing
1199 * slash) and is not in skip, then we have a conflict.
1201 for (pos
= string_list_find_insert_index(extras
, dirname
, 0);
1202 pos
< extras
->nr
; pos
++) {
1203 const char *extra_refname
= extras
->items
[pos
].string
;
1205 if (!starts_with(extra_refname
, dirname
))
1208 if (!skip
|| !string_list_has_string(skip
, extra_refname
))
1209 return extra_refname
;
1214 int refs_rename_ref_available(struct ref_store
*refs
,
1215 const char *old_refname
,
1216 const char *new_refname
)
1218 struct string_list skip
= STRING_LIST_INIT_NODUP
;
1219 struct strbuf err
= STRBUF_INIT
;
1222 string_list_insert(&skip
, old_refname
);
1223 ok
= !refs_verify_refname_available(refs
, new_refname
,
1226 error("%s", err
.buf
);
1228 string_list_clear(&skip
, 0);
1229 strbuf_release(&err
);
1233 int head_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1235 struct object_id oid
;
1239 if (resolve_gitlink_ref(submodule
, "HEAD", oid
.hash
) == 0)
1240 return fn("HEAD", &oid
, 0, cb_data
);
1245 if (!read_ref_full("HEAD", RESOLVE_REF_READING
, oid
.hash
, &flag
))
1246 return fn("HEAD", &oid
, flag
, cb_data
);
1251 int head_ref(each_ref_fn fn
, void *cb_data
)
1253 return head_ref_submodule(NULL
, fn
, cb_data
);
1256 struct ref_iterator
*refs_ref_iterator_begin(
1257 struct ref_store
*refs
,
1258 const char *prefix
, int trim
, int flags
)
1260 struct ref_iterator
*iter
;
1262 if (ref_paranoia
< 0)
1263 ref_paranoia
= git_env_bool("GIT_REF_PARANOIA", 0);
1265 flags
|= DO_FOR_EACH_INCLUDE_BROKEN
;
1267 iter
= refs
->be
->iterator_begin(refs
, prefix
, flags
);
1270 * `iterator_begin()` already takes care of prefix, but we
1271 * might need to do some trimming:
1274 iter
= prefix_ref_iterator_begin(iter
, "", trim
);
1280 * Call fn for each reference in the specified submodule for which the
1281 * refname begins with prefix. If trim is non-zero, then trim that
1282 * many characters off the beginning of each refname before passing
1283 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1284 * include broken references in the iteration. If fn ever returns a
1285 * non-zero value, stop the iteration and return that value;
1286 * otherwise, return 0.
1288 static int do_for_each_ref(struct ref_store
*refs
, const char *prefix
,
1289 each_ref_fn fn
, int trim
, int flags
, void *cb_data
)
1291 struct ref_iterator
*iter
;
1296 iter
= refs_ref_iterator_begin(refs
, prefix
, trim
, flags
);
1298 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1301 int refs_for_each_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1303 return do_for_each_ref(refs
, "", fn
, 0, 0, cb_data
);
1306 int for_each_ref(each_ref_fn fn
, void *cb_data
)
1308 return refs_for_each_ref(get_main_ref_store(), fn
, cb_data
);
1311 int for_each_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1313 return refs_for_each_ref(get_submodule_ref_store(submodule
), fn
, cb_data
);
1316 int refs_for_each_ref_in(struct ref_store
*refs
, const char *prefix
,
1317 each_ref_fn fn
, void *cb_data
)
1319 return do_for_each_ref(refs
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1322 int for_each_ref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
)
1324 return refs_for_each_ref_in(get_main_ref_store(), prefix
, fn
, cb_data
);
1327 int for_each_fullref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
, unsigned int broken
)
1329 unsigned int flag
= 0;
1332 flag
= DO_FOR_EACH_INCLUDE_BROKEN
;
1333 return do_for_each_ref(get_main_ref_store(),
1334 prefix
, fn
, 0, flag
, cb_data
);
1337 int for_each_ref_in_submodule(const char *submodule
, const char *prefix
,
1338 each_ref_fn fn
, void *cb_data
)
1340 return refs_for_each_ref_in(get_submodule_ref_store(submodule
),
1341 prefix
, fn
, cb_data
);
1344 int for_each_fullref_in_submodule(const char *submodule
, const char *prefix
,
1345 each_ref_fn fn
, void *cb_data
,
1346 unsigned int broken
)
1348 unsigned int flag
= 0;
1351 flag
= DO_FOR_EACH_INCLUDE_BROKEN
;
1352 return do_for_each_ref(get_submodule_ref_store(submodule
),
1353 prefix
, fn
, 0, flag
, cb_data
);
1356 int for_each_replace_ref(each_ref_fn fn
, void *cb_data
)
1358 return do_for_each_ref(get_main_ref_store(),
1359 git_replace_ref_base
, fn
,
1360 strlen(git_replace_ref_base
),
1364 int for_each_namespaced_ref(each_ref_fn fn
, void *cb_data
)
1366 struct strbuf buf
= STRBUF_INIT
;
1368 strbuf_addf(&buf
, "%srefs/", get_git_namespace());
1369 ret
= do_for_each_ref(get_main_ref_store(),
1370 buf
.buf
, fn
, 0, 0, cb_data
);
1371 strbuf_release(&buf
);
1375 int refs_for_each_rawref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1377 return do_for_each_ref(refs
, "", fn
, 0,
1378 DO_FOR_EACH_INCLUDE_BROKEN
, cb_data
);
1381 int for_each_rawref(each_ref_fn fn
, void *cb_data
)
1383 return refs_for_each_rawref(get_main_ref_store(), fn
, cb_data
);
1386 int refs_read_raw_ref(struct ref_store
*ref_store
,
1387 const char *refname
, unsigned char *sha1
,
1388 struct strbuf
*referent
, unsigned int *type
)
1390 return ref_store
->be
->read_raw_ref(ref_store
, refname
, sha1
, referent
, type
);
1393 /* This function needs to return a meaningful errno on failure */
1394 const char *refs_resolve_ref_unsafe(struct ref_store
*refs
,
1395 const char *refname
,
1397 unsigned char *sha1
, int *flags
)
1399 static struct strbuf sb_refname
= STRBUF_INIT
;
1404 flags
= &unused_flags
;
1408 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1409 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1410 !refname_is_safe(refname
)) {
1416 * dwim_ref() uses REF_ISBROKEN to distinguish between
1417 * missing refs and refs that were present but invalid,
1418 * to complain about the latter to stderr.
1420 * We don't know whether the ref exists, so don't set
1423 *flags
|= REF_BAD_NAME
;
1426 for (symref_count
= 0; symref_count
< SYMREF_MAXDEPTH
; symref_count
++) {
1427 unsigned int read_flags
= 0;
1429 if (refs_read_raw_ref(refs
, refname
,
1430 sha1
, &sb_refname
, &read_flags
)) {
1431 *flags
|= read_flags
;
1432 if (errno
!= ENOENT
|| (resolve_flags
& RESOLVE_REF_READING
))
1435 if (*flags
& REF_BAD_NAME
)
1436 *flags
|= REF_ISBROKEN
;
1440 *flags
|= read_flags
;
1442 if (!(read_flags
& REF_ISSYMREF
)) {
1443 if (*flags
& REF_BAD_NAME
) {
1445 *flags
|= REF_ISBROKEN
;
1450 refname
= sb_refname
.buf
;
1451 if (resolve_flags
& RESOLVE_REF_NO_RECURSE
) {
1455 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1456 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1457 !refname_is_safe(refname
)) {
1462 *flags
|= REF_ISBROKEN
| REF_BAD_NAME
;
1470 /* backend functions */
1471 int refs_init_db(struct strbuf
*err
)
1473 struct ref_store
*refs
= get_main_ref_store();
1475 return refs
->be
->init_db(refs
, err
);
1478 const char *resolve_ref_unsafe(const char *refname
, int resolve_flags
,
1479 unsigned char *sha1
, int *flags
)
1481 return refs_resolve_ref_unsafe(get_main_ref_store(), refname
,
1482 resolve_flags
, sha1
, flags
);
1485 int resolve_gitlink_ref(const char *submodule
, const char *refname
,
1486 unsigned char *sha1
)
1488 size_t len
= strlen(submodule
);
1489 struct ref_store
*refs
;
1492 while (len
&& submodule
[len
- 1] == '/')
1498 if (submodule
[len
]) {
1499 /* We need to strip off one or more trailing slashes */
1500 char *stripped
= xmemdupz(submodule
, len
);
1502 refs
= get_submodule_ref_store(stripped
);
1505 refs
= get_submodule_ref_store(submodule
);
1511 if (!refs_resolve_ref_unsafe(refs
, refname
, 0, sha1
, &flags
) ||
1517 struct ref_store_hash_entry
1519 struct hashmap_entry ent
; /* must be the first member! */
1521 struct ref_store
*refs
;
1523 /* NUL-terminated identifier of the ref store: */
1524 char name
[FLEX_ARRAY
];
1527 static int ref_store_hash_cmp(const void *entry
, const void *entry_or_key
,
1528 const void *keydata
)
1530 const struct ref_store_hash_entry
*e1
= entry
, *e2
= entry_or_key
;
1531 const char *name
= keydata
? keydata
: e2
->name
;
1533 return strcmp(e1
->name
, name
);
1536 static struct ref_store_hash_entry
*alloc_ref_store_hash_entry(
1537 const char *name
, struct ref_store
*refs
)
1539 struct ref_store_hash_entry
*entry
;
1541 FLEX_ALLOC_STR(entry
, name
, name
);
1542 hashmap_entry_init(entry
, strhash(name
));
1547 /* A pointer to the ref_store for the main repository: */
1548 static struct ref_store
*main_ref_store
;
1550 /* A hashmap of ref_stores, stored by submodule name: */
1551 static struct hashmap submodule_ref_stores
;
1553 /* A hashmap of ref_stores, stored by worktree id: */
1554 static struct hashmap worktree_ref_stores
;
1557 * Look up a ref store by name. If that ref_store hasn't been
1558 * registered yet, return NULL.
1560 static struct ref_store
*lookup_ref_store_map(struct hashmap
*map
,
1563 struct ref_store_hash_entry
*entry
;
1565 if (!map
->tablesize
)
1566 /* It's initialized on demand in register_ref_store(). */
1569 entry
= hashmap_get_from_hash(map
, strhash(name
), name
);
1570 return entry
? entry
->refs
: NULL
;
1574 * Create, record, and return a ref_store instance for the specified
1577 static struct ref_store
*ref_store_init(const char *gitdir
,
1580 const char *be_name
= "files";
1581 struct ref_storage_be
*be
= find_ref_storage_backend(be_name
);
1582 struct ref_store
*refs
;
1585 die("BUG: reference backend %s is unknown", be_name
);
1587 refs
= be
->init(gitdir
, flags
);
1591 struct ref_store
*get_main_ref_store(void)
1594 return main_ref_store
;
1596 main_ref_store
= ref_store_init(get_git_dir(), REF_STORE_ALL_CAPS
);
1597 return main_ref_store
;
1601 * Associate a ref store with a name. It is a fatal error to call this
1602 * function twice for the same name.
1604 static void register_ref_store_map(struct hashmap
*map
,
1606 struct ref_store
*refs
,
1609 if (!map
->tablesize
)
1610 hashmap_init(map
, ref_store_hash_cmp
, 0);
1612 if (hashmap_put(map
, alloc_ref_store_hash_entry(name
, refs
)))
1613 die("BUG: %s ref_store '%s' initialized twice", type
, name
);
1616 struct ref_store
*get_submodule_ref_store(const char *submodule
)
1618 struct strbuf submodule_sb
= STRBUF_INIT
;
1619 struct ref_store
*refs
;
1622 if (!submodule
|| !*submodule
) {
1624 * FIXME: This case is ideally not allowed. But that
1625 * can't happen until we clean up all the callers.
1627 return get_main_ref_store();
1630 refs
= lookup_ref_store_map(&submodule_ref_stores
, submodule
);
1634 strbuf_addstr(&submodule_sb
, submodule
);
1635 ret
= is_nonbare_repository_dir(&submodule_sb
);
1636 strbuf_release(&submodule_sb
);
1640 ret
= submodule_to_gitdir(&submodule_sb
, submodule
);
1642 strbuf_release(&submodule_sb
);
1646 /* assume that add_submodule_odb() has been called */
1647 refs
= ref_store_init(submodule_sb
.buf
,
1648 REF_STORE_READ
| REF_STORE_ODB
);
1649 register_ref_store_map(&submodule_ref_stores
, "submodule",
1652 strbuf_release(&submodule_sb
);
1656 struct ref_store
*get_worktree_ref_store(const struct worktree
*wt
)
1658 struct ref_store
*refs
;
1662 return get_main_ref_store();
1664 id
= wt
->id
? wt
->id
: "/";
1665 refs
= lookup_ref_store_map(&worktree_ref_stores
, id
);
1670 refs
= ref_store_init(git_common_path("worktrees/%s", wt
->id
),
1671 REF_STORE_ALL_CAPS
);
1673 refs
= ref_store_init(get_git_common_dir(),
1674 REF_STORE_ALL_CAPS
);
1677 register_ref_store_map(&worktree_ref_stores
, "worktree",
1682 void base_ref_store_init(struct ref_store
*refs
,
1683 const struct ref_storage_be
*be
)
1688 /* backend functions */
1689 int refs_pack_refs(struct ref_store
*refs
, unsigned int flags
)
1691 return refs
->be
->pack_refs(refs
, flags
);
1694 int refs_peel_ref(struct ref_store
*refs
, const char *refname
,
1695 unsigned char *sha1
)
1697 return refs
->be
->peel_ref(refs
, refname
, sha1
);
1700 int peel_ref(const char *refname
, unsigned char *sha1
)
1702 return refs_peel_ref(get_main_ref_store(), refname
, sha1
);
1705 int refs_create_symref(struct ref_store
*refs
,
1706 const char *ref_target
,
1707 const char *refs_heads_master
,
1710 return refs
->be
->create_symref(refs
, ref_target
,
1715 int create_symref(const char *ref_target
, const char *refs_heads_master
,
1718 return refs_create_symref(get_main_ref_store(), ref_target
,
1719 refs_heads_master
, logmsg
);
1722 int ref_update_reject_duplicates(struct string_list
*refnames
,
1725 size_t i
, n
= refnames
->nr
;
1729 for (i
= 1; i
< n
; i
++) {
1730 int cmp
= strcmp(refnames
->items
[i
- 1].string
,
1731 refnames
->items
[i
].string
);
1735 "multiple updates for ref '%s' not allowed.",
1736 refnames
->items
[i
].string
);
1738 } else if (cmp
> 0) {
1739 die("BUG: ref_update_reject_duplicates() received unsorted list");
1745 int ref_transaction_prepare(struct ref_transaction
*transaction
,
1748 struct ref_store
*refs
= transaction
->ref_store
;
1750 switch (transaction
->state
) {
1751 case REF_TRANSACTION_OPEN
:
1754 case REF_TRANSACTION_PREPARED
:
1755 die("BUG: prepare called twice on reference transaction");
1757 case REF_TRANSACTION_CLOSED
:
1758 die("BUG: prepare called on a closed reference transaction");
1761 die("BUG: unexpected reference transaction state");
1765 if (getenv(GIT_QUARANTINE_ENVIRONMENT
)) {
1767 _("ref updates forbidden inside quarantine environment"));
1771 return refs
->be
->transaction_prepare(refs
, transaction
, err
);
1774 int ref_transaction_abort(struct ref_transaction
*transaction
,
1777 struct ref_store
*refs
= transaction
->ref_store
;
1780 switch (transaction
->state
) {
1781 case REF_TRANSACTION_OPEN
:
1782 /* No need to abort explicitly. */
1784 case REF_TRANSACTION_PREPARED
:
1785 ret
= refs
->be
->transaction_abort(refs
, transaction
, err
);
1787 case REF_TRANSACTION_CLOSED
:
1788 die("BUG: abort called on a closed reference transaction");
1791 die("BUG: unexpected reference transaction state");
1795 ref_transaction_free(transaction
);
1799 int ref_transaction_commit(struct ref_transaction
*transaction
,
1802 struct ref_store
*refs
= transaction
->ref_store
;
1805 switch (transaction
->state
) {
1806 case REF_TRANSACTION_OPEN
:
1807 /* Need to prepare first. */
1808 ret
= ref_transaction_prepare(transaction
, err
);
1812 case REF_TRANSACTION_PREPARED
:
1813 /* Fall through to finish. */
1815 case REF_TRANSACTION_CLOSED
:
1816 die("BUG: commit called on a closed reference transaction");
1819 die("BUG: unexpected reference transaction state");
1823 return refs
->be
->transaction_finish(refs
, transaction
, err
);
1826 int refs_verify_refname_available(struct ref_store
*refs
,
1827 const char *refname
,
1828 const struct string_list
*extras
,
1829 const struct string_list
*skip
,
1833 const char *extra_refname
;
1834 struct strbuf dirname
= STRBUF_INIT
;
1835 struct strbuf referent
= STRBUF_INIT
;
1836 struct object_id oid
;
1838 struct ref_iterator
*iter
;
1843 * For the sake of comments in this function, suppose that
1844 * refname is "refs/foo/bar".
1849 strbuf_grow(&dirname
, strlen(refname
) + 1);
1850 for (slash
= strchr(refname
, '/'); slash
; slash
= strchr(slash
+ 1, '/')) {
1851 /* Expand dirname to the new prefix, not including the trailing slash: */
1852 strbuf_add(&dirname
, refname
+ dirname
.len
, slash
- refname
- dirname
.len
);
1855 * We are still at a leading dir of the refname (e.g.,
1856 * "refs/foo"; if there is a reference with that name,
1857 * it is a conflict, *unless* it is in skip.
1859 if (skip
&& string_list_has_string(skip
, dirname
.buf
))
1862 if (!refs_read_raw_ref(refs
, dirname
.buf
, oid
.hash
, &referent
, &type
)) {
1863 strbuf_addf(err
, "'%s' exists; cannot create '%s'",
1864 dirname
.buf
, refname
);
1868 if (extras
&& string_list_has_string(extras
, dirname
.buf
)) {
1869 strbuf_addf(err
, "cannot process '%s' and '%s' at the same time",
1870 refname
, dirname
.buf
);
1876 * We are at the leaf of our refname (e.g., "refs/foo/bar").
1877 * There is no point in searching for a reference with that
1878 * name, because a refname isn't considered to conflict with
1879 * itself. But we still need to check for references whose
1880 * names are in the "refs/foo/bar/" namespace, because they
1883 strbuf_addstr(&dirname
, refname
+ dirname
.len
);
1884 strbuf_addch(&dirname
, '/');
1886 iter
= refs_ref_iterator_begin(refs
, dirname
.buf
, 0,
1887 DO_FOR_EACH_INCLUDE_BROKEN
);
1888 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
1890 string_list_has_string(skip
, iter
->refname
))
1893 strbuf_addf(err
, "'%s' exists; cannot create '%s'",
1894 iter
->refname
, refname
);
1895 ref_iterator_abort(iter
);
1899 if (ok
!= ITER_DONE
)
1900 die("BUG: error while iterating over references");
1902 extra_refname
= find_descendant_ref(dirname
.buf
, extras
, skip
);
1904 strbuf_addf(err
, "cannot process '%s' and '%s' at the same time",
1905 refname
, extra_refname
);
1910 strbuf_release(&referent
);
1911 strbuf_release(&dirname
);
1915 int refs_for_each_reflog(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1917 struct ref_iterator
*iter
;
1919 iter
= refs
->be
->reflog_iterator_begin(refs
);
1921 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1924 int for_each_reflog(each_ref_fn fn
, void *cb_data
)
1926 return refs_for_each_reflog(get_main_ref_store(), fn
, cb_data
);
1929 int refs_for_each_reflog_ent_reverse(struct ref_store
*refs
,
1930 const char *refname
,
1931 each_reflog_ent_fn fn
,
1934 return refs
->be
->for_each_reflog_ent_reverse(refs
, refname
,
1938 int for_each_reflog_ent_reverse(const char *refname
, each_reflog_ent_fn fn
,
1941 return refs_for_each_reflog_ent_reverse(get_main_ref_store(),
1942 refname
, fn
, cb_data
);
1945 int refs_for_each_reflog_ent(struct ref_store
*refs
, const char *refname
,
1946 each_reflog_ent_fn fn
, void *cb_data
)
1948 return refs
->be
->for_each_reflog_ent(refs
, refname
, fn
, cb_data
);
1951 int for_each_reflog_ent(const char *refname
, each_reflog_ent_fn fn
,
1954 return refs_for_each_reflog_ent(get_main_ref_store(), refname
,
1958 int refs_reflog_exists(struct ref_store
*refs
, const char *refname
)
1960 return refs
->be
->reflog_exists(refs
, refname
);
1963 int reflog_exists(const char *refname
)
1965 return refs_reflog_exists(get_main_ref_store(), refname
);
1968 int refs_create_reflog(struct ref_store
*refs
, const char *refname
,
1969 int force_create
, struct strbuf
*err
)
1971 return refs
->be
->create_reflog(refs
, refname
, force_create
, err
);
1974 int safe_create_reflog(const char *refname
, int force_create
,
1977 return refs_create_reflog(get_main_ref_store(), refname
,
1981 int refs_delete_reflog(struct ref_store
*refs
, const char *refname
)
1983 return refs
->be
->delete_reflog(refs
, refname
);
1986 int delete_reflog(const char *refname
)
1988 return refs_delete_reflog(get_main_ref_store(), refname
);
1991 int refs_reflog_expire(struct ref_store
*refs
,
1992 const char *refname
, const unsigned char *sha1
,
1994 reflog_expiry_prepare_fn prepare_fn
,
1995 reflog_expiry_should_prune_fn should_prune_fn
,
1996 reflog_expiry_cleanup_fn cleanup_fn
,
1997 void *policy_cb_data
)
1999 return refs
->be
->reflog_expire(refs
, refname
, sha1
, flags
,
2000 prepare_fn
, should_prune_fn
,
2001 cleanup_fn
, policy_cb_data
);
2004 int reflog_expire(const char *refname
, const unsigned char *sha1
,
2006 reflog_expiry_prepare_fn prepare_fn
,
2007 reflog_expiry_should_prune_fn should_prune_fn
,
2008 reflog_expiry_cleanup_fn cleanup_fn
,
2009 void *policy_cb_data
)
2011 return refs_reflog_expire(get_main_ref_store(),
2012 refname
, sha1
, flags
,
2013 prepare_fn
, should_prune_fn
,
2014 cleanup_fn
, policy_cb_data
);
2017 int initial_ref_transaction_commit(struct ref_transaction
*transaction
,
2020 struct ref_store
*refs
= transaction
->ref_store
;
2022 return refs
->be
->initial_transaction_commit(refs
, transaction
, err
);
2025 int refs_delete_refs(struct ref_store
*refs
, const char *msg
,
2026 struct string_list
*refnames
, unsigned int flags
)
2028 return refs
->be
->delete_refs(refs
, msg
, refnames
, flags
);
2031 int delete_refs(const char *msg
, struct string_list
*refnames
,
2034 return refs_delete_refs(get_main_ref_store(), msg
, refnames
, flags
);
2037 int refs_rename_ref(struct ref_store
*refs
, const char *oldref
,
2038 const char *newref
, const char *logmsg
)
2040 return refs
->be
->rename_ref(refs
, oldref
, newref
, logmsg
);
2043 int rename_ref(const char *oldref
, const char *newref
, const char *logmsg
)
2045 return refs_rename_ref(get_main_ref_store(), oldref
, newref
, logmsg
);