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
!= '_')
177 * Return true if refname, which has the specified oid and flags, can
178 * be resolved to an object in the database. If the referred-to object
179 * does not exist, emit a warning and return false.
181 int ref_resolves_to_object(const char *refname
,
182 const struct object_id
*oid
,
185 if (flags
& REF_ISBROKEN
)
187 if (!has_sha1_file(oid
->hash
)) {
188 error("%s does not point to a valid object!", refname
);
194 char *refs_resolve_refdup(struct ref_store
*refs
,
195 const char *refname
, int resolve_flags
,
196 unsigned char *sha1
, int *flags
)
200 result
= refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
,
202 return xstrdup_or_null(result
);
205 char *resolve_refdup(const char *refname
, int resolve_flags
,
206 unsigned char *sha1
, int *flags
)
208 return refs_resolve_refdup(get_main_ref_store(),
209 refname
, resolve_flags
,
213 /* The argument to filter_refs */
220 int refs_read_ref_full(struct ref_store
*refs
, const char *refname
,
221 int resolve_flags
, unsigned char *sha1
, int *flags
)
223 if (refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
, sha1
, flags
))
228 int read_ref_full(const char *refname
, int resolve_flags
, unsigned char *sha1
, int *flags
)
230 return refs_read_ref_full(get_main_ref_store(), refname
,
231 resolve_flags
, sha1
, flags
);
234 int read_ref(const char *refname
, unsigned char *sha1
)
236 return read_ref_full(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
239 int ref_exists(const char *refname
)
241 unsigned char sha1
[20];
242 return !!resolve_ref_unsafe(refname
, RESOLVE_REF_READING
, sha1
, NULL
);
245 static int filter_refs(const char *refname
, const struct object_id
*oid
,
246 int flags
, void *data
)
248 struct ref_filter
*filter
= (struct ref_filter
*)data
;
250 if (wildmatch(filter
->pattern
, refname
, 0, NULL
))
252 return filter
->fn(refname
, oid
, flags
, filter
->cb_data
);
255 enum peel_status
peel_object(const unsigned char *name
, unsigned char *sha1
)
257 struct object
*o
= lookup_unknown_object(name
);
259 if (o
->type
== OBJ_NONE
) {
260 int type
= sha1_object_info(name
, NULL
);
261 if (type
< 0 || !object_as_type(o
, type
, 0))
265 if (o
->type
!= OBJ_TAG
)
268 o
= deref_tag_noverify(o
);
272 hashcpy(sha1
, o
->oid
.hash
);
276 struct warn_if_dangling_data
{
279 const struct string_list
*refnames
;
283 static int warn_if_dangling_symref(const char *refname
, const struct object_id
*oid
,
284 int flags
, void *cb_data
)
286 struct warn_if_dangling_data
*d
= cb_data
;
287 const char *resolves_to
;
288 struct object_id junk
;
290 if (!(flags
& REF_ISSYMREF
))
293 resolves_to
= resolve_ref_unsafe(refname
, 0, junk
.hash
, NULL
);
296 ? strcmp(resolves_to
, d
->refname
)
297 : !string_list_has_string(d
->refnames
, resolves_to
))) {
301 fprintf(d
->fp
, d
->msg_fmt
, refname
);
306 void warn_dangling_symref(FILE *fp
, const char *msg_fmt
, const char *refname
)
308 struct warn_if_dangling_data data
;
311 data
.refname
= refname
;
312 data
.refnames
= NULL
;
313 data
.msg_fmt
= msg_fmt
;
314 for_each_rawref(warn_if_dangling_symref
, &data
);
317 void warn_dangling_symrefs(FILE *fp
, const char *msg_fmt
, const struct string_list
*refnames
)
319 struct warn_if_dangling_data data
;
323 data
.refnames
= refnames
;
324 data
.msg_fmt
= msg_fmt
;
325 for_each_rawref(warn_if_dangling_symref
, &data
);
328 int refs_for_each_tag_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
330 return refs_for_each_ref_in(refs
, "refs/tags/", fn
, cb_data
);
333 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
335 return refs_for_each_tag_ref(get_main_ref_store(), fn
, cb_data
);
338 int for_each_tag_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
340 return refs_for_each_tag_ref(get_submodule_ref_store(submodule
),
344 int refs_for_each_branch_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
346 return refs_for_each_ref_in(refs
, "refs/heads/", fn
, cb_data
);
349 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
351 return refs_for_each_branch_ref(get_main_ref_store(), fn
, cb_data
);
354 int for_each_branch_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
356 return refs_for_each_branch_ref(get_submodule_ref_store(submodule
),
360 int refs_for_each_remote_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
362 return refs_for_each_ref_in(refs
, "refs/remotes/", fn
, cb_data
);
365 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
367 return refs_for_each_remote_ref(get_main_ref_store(), fn
, cb_data
);
370 int for_each_remote_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
372 return refs_for_each_remote_ref(get_submodule_ref_store(submodule
),
376 int head_ref_namespaced(each_ref_fn fn
, void *cb_data
)
378 struct strbuf buf
= STRBUF_INIT
;
380 struct object_id oid
;
383 strbuf_addf(&buf
, "%sHEAD", get_git_namespace());
384 if (!read_ref_full(buf
.buf
, RESOLVE_REF_READING
, oid
.hash
, &flag
))
385 ret
= fn(buf
.buf
, &oid
, flag
, cb_data
);
386 strbuf_release(&buf
);
391 int for_each_glob_ref_in(each_ref_fn fn
, const char *pattern
,
392 const char *prefix
, void *cb_data
)
394 struct strbuf real_pattern
= STRBUF_INIT
;
395 struct ref_filter filter
;
398 if (!prefix
&& !starts_with(pattern
, "refs/"))
399 strbuf_addstr(&real_pattern
, "refs/");
401 strbuf_addstr(&real_pattern
, prefix
);
402 strbuf_addstr(&real_pattern
, pattern
);
404 if (!has_glob_specials(pattern
)) {
405 /* Append implied '/' '*' if not present. */
406 strbuf_complete(&real_pattern
, '/');
407 /* No need to check for '*', there is none. */
408 strbuf_addch(&real_pattern
, '*');
411 filter
.pattern
= real_pattern
.buf
;
413 filter
.cb_data
= cb_data
;
414 ret
= for_each_ref(filter_refs
, &filter
);
416 strbuf_release(&real_pattern
);
420 int for_each_glob_ref(each_ref_fn fn
, const char *pattern
, void *cb_data
)
422 return for_each_glob_ref_in(fn
, pattern
, NULL
, cb_data
);
425 const char *prettify_refname(const char *name
)
427 if (skip_prefix(name
, "refs/heads/", &name
) ||
428 skip_prefix(name
, "refs/tags/", &name
) ||
429 skip_prefix(name
, "refs/remotes/", &name
))
434 static const char *ref_rev_parse_rules
[] = {
440 "refs/remotes/%.*s/HEAD",
444 int refname_match(const char *abbrev_name
, const char *full_name
)
447 const int abbrev_name_len
= strlen(abbrev_name
);
449 for (p
= ref_rev_parse_rules
; *p
; p
++) {
450 if (!strcmp(full_name
, mkpath(*p
, abbrev_name_len
, abbrev_name
))) {
459 * *string and *len will only be substituted, and *string returned (for
460 * later free()ing) if the string passed in is a magic short-hand form
463 static char *substitute_branch_name(const char **string
, int *len
)
465 struct strbuf buf
= STRBUF_INIT
;
466 int ret
= interpret_branch_name(*string
, *len
, &buf
, 0);
470 *string
= strbuf_detach(&buf
, &size
);
472 return (char *)*string
;
478 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
480 char *last_branch
= substitute_branch_name(&str
, &len
);
481 int refs_found
= expand_ref(str
, len
, sha1
, ref
);
486 int expand_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
490 struct strbuf fullref
= STRBUF_INIT
;
493 for (p
= ref_rev_parse_rules
; *p
; p
++) {
494 unsigned char sha1_from_ref
[20];
495 unsigned char *this_result
;
498 this_result
= refs_found
? sha1_from_ref
: sha1
;
499 strbuf_reset(&fullref
);
500 strbuf_addf(&fullref
, *p
, len
, str
);
501 r
= resolve_ref_unsafe(fullref
.buf
, RESOLVE_REF_READING
,
506 if (!warn_ambiguous_refs
)
508 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
.buf
, "HEAD")) {
509 warning("ignoring dangling symref %s.", fullref
.buf
);
510 } else if ((flag
& REF_ISBROKEN
) && strchr(fullref
.buf
, '/')) {
511 warning("ignoring broken ref %s.", fullref
.buf
);
514 strbuf_release(&fullref
);
518 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
520 char *last_branch
= substitute_branch_name(&str
, &len
);
523 struct strbuf path
= STRBUF_INIT
;
526 for (p
= ref_rev_parse_rules
; *p
; p
++) {
527 unsigned char hash
[20];
528 const char *ref
, *it
;
531 strbuf_addf(&path
, *p
, len
, str
);
532 ref
= resolve_ref_unsafe(path
.buf
, RESOLVE_REF_READING
,
536 if (reflog_exists(path
.buf
))
538 else if (strcmp(ref
, path
.buf
) && reflog_exists(ref
))
546 if (!warn_ambiguous_refs
)
549 strbuf_release(&path
);
554 static int is_per_worktree_ref(const char *refname
)
556 return !strcmp(refname
, "HEAD") ||
557 starts_with(refname
, "refs/bisect/");
560 static int is_pseudoref_syntax(const char *refname
)
564 for (c
= refname
; *c
; c
++) {
565 if (!isupper(*c
) && *c
!= '-' && *c
!= '_')
572 enum ref_type
ref_type(const char *refname
)
574 if (is_per_worktree_ref(refname
))
575 return REF_TYPE_PER_WORKTREE
;
576 if (is_pseudoref_syntax(refname
))
577 return REF_TYPE_PSEUDOREF
;
578 return REF_TYPE_NORMAL
;
581 static int write_pseudoref(const char *pseudoref
, const unsigned char *sha1
,
582 const unsigned char *old_sha1
, struct strbuf
*err
)
584 const char *filename
;
586 static struct lock_file lock
;
587 struct strbuf buf
= STRBUF_INIT
;
590 strbuf_addf(&buf
, "%s\n", sha1_to_hex(sha1
));
592 filename
= git_path("%s", pseudoref
);
593 fd
= hold_lock_file_for_update(&lock
, filename
, LOCK_DIE_ON_ERROR
);
595 strbuf_addf(err
, "could not open '%s' for writing: %s",
596 filename
, strerror(errno
));
601 unsigned char actual_old_sha1
[20];
603 if (read_ref(pseudoref
, actual_old_sha1
))
604 die("could not read ref '%s'", pseudoref
);
605 if (hashcmp(actual_old_sha1
, old_sha1
)) {
606 strbuf_addf(err
, "unexpected sha1 when writing '%s'", pseudoref
);
607 rollback_lock_file(&lock
);
612 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
) {
613 strbuf_addf(err
, "could not write to '%s'", filename
);
614 rollback_lock_file(&lock
);
618 commit_lock_file(&lock
);
621 strbuf_release(&buf
);
625 static int delete_pseudoref(const char *pseudoref
, const unsigned char *old_sha1
)
627 static struct lock_file lock
;
628 const char *filename
;
630 filename
= git_path("%s", pseudoref
);
632 if (old_sha1
&& !is_null_sha1(old_sha1
)) {
634 unsigned char actual_old_sha1
[20];
636 fd
= hold_lock_file_for_update(&lock
, filename
,
639 die_errno(_("Could not open '%s' for writing"), filename
);
640 if (read_ref(pseudoref
, actual_old_sha1
))
641 die("could not read ref '%s'", pseudoref
);
642 if (hashcmp(actual_old_sha1
, old_sha1
)) {
643 warning("Unexpected sha1 when deleting %s", pseudoref
);
644 rollback_lock_file(&lock
);
649 rollback_lock_file(&lock
);
657 int refs_delete_ref(struct ref_store
*refs
, const char *msg
,
659 const unsigned char *old_sha1
,
662 struct ref_transaction
*transaction
;
663 struct strbuf err
= STRBUF_INIT
;
665 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
) {
666 assert(refs
== get_main_ref_store());
667 return delete_pseudoref(refname
, old_sha1
);
670 transaction
= ref_store_transaction_begin(refs
, &err
);
672 ref_transaction_delete(transaction
, refname
, old_sha1
,
674 ref_transaction_commit(transaction
, &err
)) {
675 error("%s", err
.buf
);
676 ref_transaction_free(transaction
);
677 strbuf_release(&err
);
680 ref_transaction_free(transaction
);
681 strbuf_release(&err
);
685 int delete_ref(const char *msg
, const char *refname
,
686 const unsigned char *old_sha1
, unsigned int flags
)
688 return refs_delete_ref(get_main_ref_store(), msg
, refname
,
692 int copy_reflog_msg(char *buf
, const char *msg
)
699 while ((c
= *msg
++)) {
700 if (wasspace
&& isspace(c
))
702 wasspace
= isspace(c
);
707 while (buf
< cp
&& isspace(cp
[-1]))
713 int should_autocreate_reflog(const char *refname
)
715 switch (log_all_ref_updates
) {
716 case LOG_REFS_ALWAYS
:
718 case LOG_REFS_NORMAL
:
719 return starts_with(refname
, "refs/heads/") ||
720 starts_with(refname
, "refs/remotes/") ||
721 starts_with(refname
, "refs/notes/") ||
722 !strcmp(refname
, "HEAD");
728 int is_branch(const char *refname
)
730 return !strcmp(refname
, "HEAD") || starts_with(refname
, "refs/heads/");
733 struct read_ref_at_cb
{
741 unsigned char osha1
[20];
742 unsigned char nsha1
[20];
746 timestamp_t
*cutoff_time
;
751 static int read_ref_at_ent(struct object_id
*ooid
, struct object_id
*noid
,
752 const char *email
, timestamp_t timestamp
, int tz
,
753 const char *message
, void *cb_data
)
755 struct read_ref_at_cb
*cb
= cb_data
;
759 cb
->date
= timestamp
;
761 if (timestamp
<= cb
->at_time
|| cb
->cnt
== 0) {
763 *cb
->msg
= xstrdup(message
);
765 *cb
->cutoff_time
= timestamp
;
769 *cb
->cutoff_cnt
= cb
->reccnt
- 1;
771 * we have not yet updated cb->[n|o]sha1 so they still
772 * hold the values for the previous record.
774 if (!is_null_sha1(cb
->osha1
)) {
775 hashcpy(cb
->sha1
, noid
->hash
);
776 if (hashcmp(cb
->osha1
, noid
->hash
))
777 warning("Log for ref %s has gap after %s.",
778 cb
->refname
, show_date(cb
->date
, cb
->tz
, DATE_MODE(RFC2822
)));
780 else if (cb
->date
== cb
->at_time
)
781 hashcpy(cb
->sha1
, noid
->hash
);
782 else if (hashcmp(noid
->hash
, cb
->sha1
))
783 warning("Log for ref %s unexpectedly ended on %s.",
784 cb
->refname
, show_date(cb
->date
, cb
->tz
,
785 DATE_MODE(RFC2822
)));
786 hashcpy(cb
->osha1
, ooid
->hash
);
787 hashcpy(cb
->nsha1
, noid
->hash
);
791 hashcpy(cb
->osha1
, ooid
->hash
);
792 hashcpy(cb
->nsha1
, noid
->hash
);
798 static int read_ref_at_ent_oldest(struct object_id
*ooid
, struct object_id
*noid
,
799 const char *email
, timestamp_t timestamp
,
800 int tz
, const char *message
, void *cb_data
)
802 struct read_ref_at_cb
*cb
= cb_data
;
805 *cb
->msg
= xstrdup(message
);
807 *cb
->cutoff_time
= timestamp
;
811 *cb
->cutoff_cnt
= cb
->reccnt
;
812 hashcpy(cb
->sha1
, ooid
->hash
);
813 if (is_null_sha1(cb
->sha1
))
814 hashcpy(cb
->sha1
, noid
->hash
);
815 /* We just want the first entry */
819 int read_ref_at(const char *refname
, unsigned int flags
, timestamp_t at_time
, int cnt
,
820 unsigned char *sha1
, char **msg
,
821 timestamp_t
*cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
823 struct read_ref_at_cb cb
;
825 memset(&cb
, 0, sizeof(cb
));
826 cb
.refname
= refname
;
827 cb
.at_time
= at_time
;
830 cb
.cutoff_time
= cutoff_time
;
831 cb
.cutoff_tz
= cutoff_tz
;
832 cb
.cutoff_cnt
= cutoff_cnt
;
835 for_each_reflog_ent_reverse(refname
, read_ref_at_ent
, &cb
);
838 if (flags
& GET_SHA1_QUIETLY
)
841 die("Log for %s is empty.", refname
);
846 for_each_reflog_ent(refname
, read_ref_at_ent_oldest
, &cb
);
851 struct ref_transaction
*ref_store_transaction_begin(struct ref_store
*refs
,
854 struct ref_transaction
*tr
;
857 tr
= xcalloc(1, sizeof(struct ref_transaction
));
858 tr
->ref_store
= refs
;
862 struct ref_transaction
*ref_transaction_begin(struct strbuf
*err
)
864 return ref_store_transaction_begin(get_main_ref_store(), err
);
867 void ref_transaction_free(struct ref_transaction
*transaction
)
874 switch (transaction
->state
) {
875 case REF_TRANSACTION_OPEN
:
876 case REF_TRANSACTION_CLOSED
:
879 case REF_TRANSACTION_PREPARED
:
880 die("BUG: free called on a prepared reference transaction");
883 die("BUG: unexpected reference transaction state");
887 for (i
= 0; i
< transaction
->nr
; i
++) {
888 free(transaction
->updates
[i
]->msg
);
889 free(transaction
->updates
[i
]);
891 free(transaction
->updates
);
895 struct ref_update
*ref_transaction_add_update(
896 struct ref_transaction
*transaction
,
897 const char *refname
, unsigned int flags
,
898 const unsigned char *new_sha1
,
899 const unsigned char *old_sha1
,
902 struct ref_update
*update
;
904 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
905 die("BUG: update called for transaction that is not open");
907 if ((flags
& REF_ISPRUNING
) && !(flags
& REF_NODEREF
))
908 die("BUG: REF_ISPRUNING set without REF_NODEREF");
910 FLEX_ALLOC_STR(update
, refname
, refname
);
911 ALLOC_GROW(transaction
->updates
, transaction
->nr
+ 1, transaction
->alloc
);
912 transaction
->updates
[transaction
->nr
++] = update
;
914 update
->flags
= flags
;
916 if (flags
& REF_HAVE_NEW
)
917 hashcpy(update
->new_oid
.hash
, new_sha1
);
918 if (flags
& REF_HAVE_OLD
)
919 hashcpy(update
->old_oid
.hash
, old_sha1
);
920 update
->msg
= xstrdup_or_null(msg
);
924 int ref_transaction_update(struct ref_transaction
*transaction
,
926 const unsigned char *new_sha1
,
927 const unsigned char *old_sha1
,
928 unsigned int flags
, const char *msg
,
933 if ((new_sha1
&& !is_null_sha1(new_sha1
)) ?
934 check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
) :
935 !refname_is_safe(refname
)) {
936 strbuf_addf(err
, "refusing to update ref with bad name '%s'",
941 flags
|= (new_sha1
? REF_HAVE_NEW
: 0) | (old_sha1
? REF_HAVE_OLD
: 0);
943 ref_transaction_add_update(transaction
, refname
, flags
,
944 new_sha1
, old_sha1
, msg
);
948 int ref_transaction_create(struct ref_transaction
*transaction
,
950 const unsigned char *new_sha1
,
951 unsigned int flags
, const char *msg
,
954 if (!new_sha1
|| is_null_sha1(new_sha1
))
955 die("BUG: create called without valid new_sha1");
956 return ref_transaction_update(transaction
, refname
, new_sha1
,
957 null_sha1
, flags
, msg
, err
);
960 int ref_transaction_delete(struct ref_transaction
*transaction
,
962 const unsigned char *old_sha1
,
963 unsigned int flags
, const char *msg
,
966 if (old_sha1
&& is_null_sha1(old_sha1
))
967 die("BUG: delete called with old_sha1 set to zeros");
968 return ref_transaction_update(transaction
, refname
,
973 int ref_transaction_verify(struct ref_transaction
*transaction
,
975 const unsigned char *old_sha1
,
980 die("BUG: verify called with old_sha1 set to NULL");
981 return ref_transaction_update(transaction
, refname
,
986 int update_ref_oid(const char *msg
, const char *refname
,
987 const struct object_id
*new_oid
, const struct object_id
*old_oid
,
988 unsigned int flags
, enum action_on_err onerr
)
990 return update_ref(msg
, refname
, new_oid
? new_oid
->hash
: NULL
,
991 old_oid
? old_oid
->hash
: NULL
, flags
, onerr
);
994 int refs_update_ref(struct ref_store
*refs
, const char *msg
,
995 const char *refname
, const unsigned char *new_sha1
,
996 const unsigned char *old_sha1
, unsigned int flags
,
997 enum action_on_err onerr
)
999 struct ref_transaction
*t
= NULL
;
1000 struct strbuf err
= STRBUF_INIT
;
1003 if (ref_type(refname
) == REF_TYPE_PSEUDOREF
) {
1004 assert(refs
== get_main_ref_store());
1005 ret
= write_pseudoref(refname
, new_sha1
, old_sha1
, &err
);
1007 t
= ref_store_transaction_begin(refs
, &err
);
1009 ref_transaction_update(t
, refname
, new_sha1
, old_sha1
,
1010 flags
, msg
, &err
) ||
1011 ref_transaction_commit(t
, &err
)) {
1013 ref_transaction_free(t
);
1017 const char *str
= "update_ref failed for ref '%s': %s";
1020 case UPDATE_REFS_MSG_ON_ERR
:
1021 error(str
, refname
, err
.buf
);
1023 case UPDATE_REFS_DIE_ON_ERR
:
1024 die(str
, refname
, err
.buf
);
1026 case UPDATE_REFS_QUIET_ON_ERR
:
1029 strbuf_release(&err
);
1032 strbuf_release(&err
);
1034 ref_transaction_free(t
);
1038 int update_ref(const char *msg
, const char *refname
,
1039 const unsigned char *new_sha1
,
1040 const unsigned char *old_sha1
,
1041 unsigned int flags
, enum action_on_err onerr
)
1043 return refs_update_ref(get_main_ref_store(), msg
, refname
, new_sha1
,
1044 old_sha1
, flags
, onerr
);
1047 char *shorten_unambiguous_ref(const char *refname
, int strict
)
1050 static char **scanf_fmts
;
1051 static int nr_rules
;
1053 struct strbuf resolved_buf
= STRBUF_INIT
;
1057 * Pre-generate scanf formats from ref_rev_parse_rules[].
1058 * Generate a format suitable for scanf from a
1059 * ref_rev_parse_rules rule by interpolating "%s" at the
1060 * location of the "%.*s".
1062 size_t total_len
= 0;
1065 /* the rule list is NULL terminated, count them first */
1066 for (nr_rules
= 0; ref_rev_parse_rules
[nr_rules
]; nr_rules
++)
1067 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
1068 total_len
+= strlen(ref_rev_parse_rules
[nr_rules
]) - 2 + 1;
1070 scanf_fmts
= xmalloc(st_add(st_mult(sizeof(char *), nr_rules
), total_len
));
1073 for (i
= 0; i
< nr_rules
; i
++) {
1074 assert(offset
< total_len
);
1075 scanf_fmts
[i
] = (char *)&scanf_fmts
[nr_rules
] + offset
;
1076 offset
+= snprintf(scanf_fmts
[i
], total_len
- offset
,
1077 ref_rev_parse_rules
[i
], 2, "%s") + 1;
1081 /* bail out if there are no rules */
1083 return xstrdup(refname
);
1085 /* buffer for scanf result, at most refname must fit */
1086 short_name
= xstrdup(refname
);
1088 /* skip first rule, it will always match */
1089 for (i
= nr_rules
- 1; i
> 0 ; --i
) {
1091 int rules_to_fail
= i
;
1094 if (1 != sscanf(refname
, scanf_fmts
[i
], short_name
))
1097 short_name_len
= strlen(short_name
);
1100 * in strict mode, all (except the matched one) rules
1101 * must fail to resolve to a valid non-ambiguous ref
1104 rules_to_fail
= nr_rules
;
1107 * check if the short name resolves to a valid ref,
1108 * but use only rules prior to the matched one
1110 for (j
= 0; j
< rules_to_fail
; j
++) {
1111 const char *rule
= ref_rev_parse_rules
[j
];
1113 /* skip matched rule */
1118 * the short name is ambiguous, if it resolves
1119 * (with this previous rule) to a valid ref
1120 * read_ref() returns 0 on success
1122 strbuf_reset(&resolved_buf
);
1123 strbuf_addf(&resolved_buf
, rule
,
1124 short_name_len
, short_name
);
1125 if (ref_exists(resolved_buf
.buf
))
1130 * short name is non-ambiguous if all previous rules
1131 * haven't resolved to a valid ref
1133 if (j
== rules_to_fail
) {
1134 strbuf_release(&resolved_buf
);
1139 strbuf_release(&resolved_buf
);
1141 return xstrdup(refname
);
1144 static struct string_list
*hide_refs
;
1146 int parse_hide_refs_config(const char *var
, const char *value
, const char *section
)
1149 if (!strcmp("transfer.hiderefs", var
) ||
1150 (!parse_config_key(var
, section
, NULL
, NULL
, &key
) &&
1151 !strcmp(key
, "hiderefs"))) {
1156 return config_error_nonbool(var
);
1157 ref
= xstrdup(value
);
1159 while (len
&& ref
[len
- 1] == '/')
1162 hide_refs
= xcalloc(1, sizeof(*hide_refs
));
1163 hide_refs
->strdup_strings
= 1;
1165 string_list_append(hide_refs
, ref
);
1170 int ref_is_hidden(const char *refname
, const char *refname_full
)
1176 for (i
= hide_refs
->nr
- 1; i
>= 0; i
--) {
1177 const char *match
= hide_refs
->items
[i
].string
;
1178 const char *subject
;
1182 if (*match
== '!') {
1187 if (*match
== '^') {
1188 subject
= refname_full
;
1194 /* refname can be NULL when namespaces are used. */
1195 if (!subject
|| !starts_with(subject
, match
))
1197 len
= strlen(match
);
1198 if (!subject
[len
] || subject
[len
] == '/')
1204 const char *find_descendant_ref(const char *dirname
,
1205 const struct string_list
*extras
,
1206 const struct string_list
*skip
)
1214 * Look at the place where dirname would be inserted into
1215 * extras. If there is an entry at that position that starts
1216 * with dirname (remember, dirname includes the trailing
1217 * slash) and is not in skip, then we have a conflict.
1219 for (pos
= string_list_find_insert_index(extras
, dirname
, 0);
1220 pos
< extras
->nr
; pos
++) {
1221 const char *extra_refname
= extras
->items
[pos
].string
;
1223 if (!starts_with(extra_refname
, dirname
))
1226 if (!skip
|| !string_list_has_string(skip
, extra_refname
))
1227 return extra_refname
;
1232 int refs_rename_ref_available(struct ref_store
*refs
,
1233 const char *old_refname
,
1234 const char *new_refname
)
1236 struct string_list skip
= STRING_LIST_INIT_NODUP
;
1237 struct strbuf err
= STRBUF_INIT
;
1240 string_list_insert(&skip
, old_refname
);
1241 ok
= !refs_verify_refname_available(refs
, new_refname
,
1244 error("%s", err
.buf
);
1246 string_list_clear(&skip
, 0);
1247 strbuf_release(&err
);
1251 int head_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1253 struct object_id oid
;
1257 if (resolve_gitlink_ref(submodule
, "HEAD", oid
.hash
) == 0)
1258 return fn("HEAD", &oid
, 0, cb_data
);
1263 if (!read_ref_full("HEAD", RESOLVE_REF_READING
, oid
.hash
, &flag
))
1264 return fn("HEAD", &oid
, flag
, cb_data
);
1269 int head_ref(each_ref_fn fn
, void *cb_data
)
1271 return head_ref_submodule(NULL
, fn
, cb_data
);
1274 struct ref_iterator
*refs_ref_iterator_begin(
1275 struct ref_store
*refs
,
1276 const char *prefix
, int trim
, int flags
)
1278 struct ref_iterator
*iter
;
1280 if (ref_paranoia
< 0)
1281 ref_paranoia
= git_env_bool("GIT_REF_PARANOIA", 0);
1283 flags
|= DO_FOR_EACH_INCLUDE_BROKEN
;
1285 iter
= refs
->be
->iterator_begin(refs
, prefix
, flags
);
1288 * `iterator_begin()` already takes care of prefix, but we
1289 * might need to do some trimming:
1292 iter
= prefix_ref_iterator_begin(iter
, "", trim
);
1298 * Call fn for each reference in the specified submodule for which the
1299 * refname begins with prefix. If trim is non-zero, then trim that
1300 * many characters off the beginning of each refname before passing
1301 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1302 * include broken references in the iteration. If fn ever returns a
1303 * non-zero value, stop the iteration and return that value;
1304 * otherwise, return 0.
1306 static int do_for_each_ref(struct ref_store
*refs
, const char *prefix
,
1307 each_ref_fn fn
, int trim
, int flags
, void *cb_data
)
1309 struct ref_iterator
*iter
;
1314 iter
= refs_ref_iterator_begin(refs
, prefix
, trim
, flags
);
1316 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1319 int refs_for_each_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1321 return do_for_each_ref(refs
, "", fn
, 0, 0, cb_data
);
1324 int for_each_ref(each_ref_fn fn
, void *cb_data
)
1326 return refs_for_each_ref(get_main_ref_store(), fn
, cb_data
);
1329 int for_each_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1331 return refs_for_each_ref(get_submodule_ref_store(submodule
), fn
, cb_data
);
1334 int refs_for_each_ref_in(struct ref_store
*refs
, const char *prefix
,
1335 each_ref_fn fn
, void *cb_data
)
1337 return do_for_each_ref(refs
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1340 int for_each_ref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
)
1342 return refs_for_each_ref_in(get_main_ref_store(), prefix
, fn
, cb_data
);
1345 int for_each_fullref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
, unsigned int broken
)
1347 unsigned int flag
= 0;
1350 flag
= DO_FOR_EACH_INCLUDE_BROKEN
;
1351 return do_for_each_ref(get_main_ref_store(),
1352 prefix
, fn
, 0, flag
, cb_data
);
1355 int for_each_ref_in_submodule(const char *submodule
, const char *prefix
,
1356 each_ref_fn fn
, void *cb_data
)
1358 return refs_for_each_ref_in(get_submodule_ref_store(submodule
),
1359 prefix
, fn
, cb_data
);
1362 int for_each_fullref_in_submodule(const char *submodule
, const char *prefix
,
1363 each_ref_fn fn
, void *cb_data
,
1364 unsigned int broken
)
1366 unsigned int flag
= 0;
1369 flag
= DO_FOR_EACH_INCLUDE_BROKEN
;
1370 return do_for_each_ref(get_submodule_ref_store(submodule
),
1371 prefix
, fn
, 0, flag
, cb_data
);
1374 int for_each_replace_ref(each_ref_fn fn
, void *cb_data
)
1376 return do_for_each_ref(get_main_ref_store(),
1377 git_replace_ref_base
, fn
,
1378 strlen(git_replace_ref_base
),
1382 int for_each_namespaced_ref(each_ref_fn fn
, void *cb_data
)
1384 struct strbuf buf
= STRBUF_INIT
;
1386 strbuf_addf(&buf
, "%srefs/", get_git_namespace());
1387 ret
= do_for_each_ref(get_main_ref_store(),
1388 buf
.buf
, fn
, 0, 0, cb_data
);
1389 strbuf_release(&buf
);
1393 int refs_for_each_rawref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1395 return do_for_each_ref(refs
, "", fn
, 0,
1396 DO_FOR_EACH_INCLUDE_BROKEN
, cb_data
);
1399 int for_each_rawref(each_ref_fn fn
, void *cb_data
)
1401 return refs_for_each_rawref(get_main_ref_store(), fn
, cb_data
);
1404 int refs_read_raw_ref(struct ref_store
*ref_store
,
1405 const char *refname
, unsigned char *sha1
,
1406 struct strbuf
*referent
, unsigned int *type
)
1408 return ref_store
->be
->read_raw_ref(ref_store
, refname
, sha1
, referent
, type
);
1411 /* This function needs to return a meaningful errno on failure */
1412 const char *refs_resolve_ref_unsafe(struct ref_store
*refs
,
1413 const char *refname
,
1415 unsigned char *sha1
, int *flags
)
1417 static struct strbuf sb_refname
= STRBUF_INIT
;
1422 flags
= &unused_flags
;
1426 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1427 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1428 !refname_is_safe(refname
)) {
1434 * dwim_ref() uses REF_ISBROKEN to distinguish between
1435 * missing refs and refs that were present but invalid,
1436 * to complain about the latter to stderr.
1438 * We don't know whether the ref exists, so don't set
1441 *flags
|= REF_BAD_NAME
;
1444 for (symref_count
= 0; symref_count
< SYMREF_MAXDEPTH
; symref_count
++) {
1445 unsigned int read_flags
= 0;
1447 if (refs_read_raw_ref(refs
, refname
,
1448 sha1
, &sb_refname
, &read_flags
)) {
1449 *flags
|= read_flags
;
1450 if (errno
!= ENOENT
|| (resolve_flags
& RESOLVE_REF_READING
))
1453 if (*flags
& REF_BAD_NAME
)
1454 *flags
|= REF_ISBROKEN
;
1458 *flags
|= read_flags
;
1460 if (!(read_flags
& REF_ISSYMREF
)) {
1461 if (*flags
& REF_BAD_NAME
) {
1463 *flags
|= REF_ISBROKEN
;
1468 refname
= sb_refname
.buf
;
1469 if (resolve_flags
& RESOLVE_REF_NO_RECURSE
) {
1473 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
1474 if (!(resolve_flags
& RESOLVE_REF_ALLOW_BAD_NAME
) ||
1475 !refname_is_safe(refname
)) {
1480 *flags
|= REF_ISBROKEN
| REF_BAD_NAME
;
1488 /* backend functions */
1489 int refs_init_db(struct strbuf
*err
)
1491 struct ref_store
*refs
= get_main_ref_store();
1493 return refs
->be
->init_db(refs
, err
);
1496 const char *resolve_ref_unsafe(const char *refname
, int resolve_flags
,
1497 unsigned char *sha1
, int *flags
)
1499 return refs_resolve_ref_unsafe(get_main_ref_store(), refname
,
1500 resolve_flags
, sha1
, flags
);
1503 int resolve_gitlink_ref(const char *submodule
, const char *refname
,
1504 unsigned char *sha1
)
1506 size_t len
= strlen(submodule
);
1507 struct ref_store
*refs
;
1510 while (len
&& submodule
[len
- 1] == '/')
1516 if (submodule
[len
]) {
1517 /* We need to strip off one or more trailing slashes */
1518 char *stripped
= xmemdupz(submodule
, len
);
1520 refs
= get_submodule_ref_store(stripped
);
1523 refs
= get_submodule_ref_store(submodule
);
1529 if (!refs_resolve_ref_unsafe(refs
, refname
, 0, sha1
, &flags
) ||
1535 struct ref_store_hash_entry
1537 struct hashmap_entry ent
; /* must be the first member! */
1539 struct ref_store
*refs
;
1541 /* NUL-terminated identifier of the ref store: */
1542 char name
[FLEX_ARRAY
];
1545 static int ref_store_hash_cmp(const void *entry
, const void *entry_or_key
,
1546 const void *keydata
)
1548 const struct ref_store_hash_entry
*e1
= entry
, *e2
= entry_or_key
;
1549 const char *name
= keydata
? keydata
: e2
->name
;
1551 return strcmp(e1
->name
, name
);
1554 static struct ref_store_hash_entry
*alloc_ref_store_hash_entry(
1555 const char *name
, struct ref_store
*refs
)
1557 struct ref_store_hash_entry
*entry
;
1559 FLEX_ALLOC_STR(entry
, name
, name
);
1560 hashmap_entry_init(entry
, strhash(name
));
1565 /* A pointer to the ref_store for the main repository: */
1566 static struct ref_store
*main_ref_store
;
1568 /* A hashmap of ref_stores, stored by submodule name: */
1569 static struct hashmap submodule_ref_stores
;
1571 /* A hashmap of ref_stores, stored by worktree id: */
1572 static struct hashmap worktree_ref_stores
;
1575 * Look up a ref store by name. If that ref_store hasn't been
1576 * registered yet, return NULL.
1578 static struct ref_store
*lookup_ref_store_map(struct hashmap
*map
,
1581 struct ref_store_hash_entry
*entry
;
1583 if (!map
->tablesize
)
1584 /* It's initialized on demand in register_ref_store(). */
1587 entry
= hashmap_get_from_hash(map
, strhash(name
), name
);
1588 return entry
? entry
->refs
: NULL
;
1592 * Create, record, and return a ref_store instance for the specified
1595 static struct ref_store
*ref_store_init(const char *gitdir
,
1598 const char *be_name
= "files";
1599 struct ref_storage_be
*be
= find_ref_storage_backend(be_name
);
1600 struct ref_store
*refs
;
1603 die("BUG: reference backend %s is unknown", be_name
);
1605 refs
= be
->init(gitdir
, flags
);
1609 struct ref_store
*get_main_ref_store(void)
1612 return main_ref_store
;
1614 main_ref_store
= ref_store_init(get_git_dir(), REF_STORE_ALL_CAPS
);
1615 return main_ref_store
;
1619 * Associate a ref store with a name. It is a fatal error to call this
1620 * function twice for the same name.
1622 static void register_ref_store_map(struct hashmap
*map
,
1624 struct ref_store
*refs
,
1627 if (!map
->tablesize
)
1628 hashmap_init(map
, ref_store_hash_cmp
, 0);
1630 if (hashmap_put(map
, alloc_ref_store_hash_entry(name
, refs
)))
1631 die("BUG: %s ref_store '%s' initialized twice", type
, name
);
1634 struct ref_store
*get_submodule_ref_store(const char *submodule
)
1636 struct strbuf submodule_sb
= STRBUF_INIT
;
1637 struct ref_store
*refs
;
1640 if (!submodule
|| !*submodule
) {
1642 * FIXME: This case is ideally not allowed. But that
1643 * can't happen until we clean up all the callers.
1645 return get_main_ref_store();
1648 refs
= lookup_ref_store_map(&submodule_ref_stores
, submodule
);
1652 strbuf_addstr(&submodule_sb
, submodule
);
1653 ret
= is_nonbare_repository_dir(&submodule_sb
);
1654 strbuf_release(&submodule_sb
);
1658 ret
= submodule_to_gitdir(&submodule_sb
, submodule
);
1660 strbuf_release(&submodule_sb
);
1664 /* assume that add_submodule_odb() has been called */
1665 refs
= ref_store_init(submodule_sb
.buf
,
1666 REF_STORE_READ
| REF_STORE_ODB
);
1667 register_ref_store_map(&submodule_ref_stores
, "submodule",
1670 strbuf_release(&submodule_sb
);
1674 struct ref_store
*get_worktree_ref_store(const struct worktree
*wt
)
1676 struct ref_store
*refs
;
1680 return get_main_ref_store();
1682 id
= wt
->id
? wt
->id
: "/";
1683 refs
= lookup_ref_store_map(&worktree_ref_stores
, id
);
1688 refs
= ref_store_init(git_common_path("worktrees/%s", wt
->id
),
1689 REF_STORE_ALL_CAPS
);
1691 refs
= ref_store_init(get_git_common_dir(),
1692 REF_STORE_ALL_CAPS
);
1695 register_ref_store_map(&worktree_ref_stores
, "worktree",
1700 void base_ref_store_init(struct ref_store
*refs
,
1701 const struct ref_storage_be
*be
)
1706 /* backend functions */
1707 int refs_pack_refs(struct ref_store
*refs
, unsigned int flags
)
1709 return refs
->be
->pack_refs(refs
, flags
);
1712 int refs_peel_ref(struct ref_store
*refs
, const char *refname
,
1713 unsigned char *sha1
)
1715 return refs
->be
->peel_ref(refs
, refname
, sha1
);
1718 int peel_ref(const char *refname
, unsigned char *sha1
)
1720 return refs_peel_ref(get_main_ref_store(), refname
, sha1
);
1723 int refs_create_symref(struct ref_store
*refs
,
1724 const char *ref_target
,
1725 const char *refs_heads_master
,
1728 return refs
->be
->create_symref(refs
, ref_target
,
1733 int create_symref(const char *ref_target
, const char *refs_heads_master
,
1736 return refs_create_symref(get_main_ref_store(), ref_target
,
1737 refs_heads_master
, logmsg
);
1740 int ref_update_reject_duplicates(struct string_list
*refnames
,
1743 size_t i
, n
= refnames
->nr
;
1747 for (i
= 1; i
< n
; i
++) {
1748 int cmp
= strcmp(refnames
->items
[i
- 1].string
,
1749 refnames
->items
[i
].string
);
1753 "multiple updates for ref '%s' not allowed.",
1754 refnames
->items
[i
].string
);
1756 } else if (cmp
> 0) {
1757 die("BUG: ref_update_reject_duplicates() received unsorted list");
1763 int ref_transaction_prepare(struct ref_transaction
*transaction
,
1766 struct ref_store
*refs
= transaction
->ref_store
;
1768 switch (transaction
->state
) {
1769 case REF_TRANSACTION_OPEN
:
1772 case REF_TRANSACTION_PREPARED
:
1773 die("BUG: prepare called twice on reference transaction");
1775 case REF_TRANSACTION_CLOSED
:
1776 die("BUG: prepare called on a closed reference transaction");
1779 die("BUG: unexpected reference transaction state");
1783 if (getenv(GIT_QUARANTINE_ENVIRONMENT
)) {
1785 _("ref updates forbidden inside quarantine environment"));
1789 return refs
->be
->transaction_prepare(refs
, transaction
, err
);
1792 int ref_transaction_abort(struct ref_transaction
*transaction
,
1795 struct ref_store
*refs
= transaction
->ref_store
;
1798 switch (transaction
->state
) {
1799 case REF_TRANSACTION_OPEN
:
1800 /* No need to abort explicitly. */
1802 case REF_TRANSACTION_PREPARED
:
1803 ret
= refs
->be
->transaction_abort(refs
, transaction
, err
);
1805 case REF_TRANSACTION_CLOSED
:
1806 die("BUG: abort called on a closed reference transaction");
1809 die("BUG: unexpected reference transaction state");
1813 ref_transaction_free(transaction
);
1817 int ref_transaction_commit(struct ref_transaction
*transaction
,
1820 struct ref_store
*refs
= transaction
->ref_store
;
1823 switch (transaction
->state
) {
1824 case REF_TRANSACTION_OPEN
:
1825 /* Need to prepare first. */
1826 ret
= ref_transaction_prepare(transaction
, err
);
1830 case REF_TRANSACTION_PREPARED
:
1831 /* Fall through to finish. */
1833 case REF_TRANSACTION_CLOSED
:
1834 die("BUG: commit called on a closed reference transaction");
1837 die("BUG: unexpected reference transaction state");
1841 return refs
->be
->transaction_finish(refs
, transaction
, err
);
1844 int refs_verify_refname_available(struct ref_store
*refs
,
1845 const char *refname
,
1846 const struct string_list
*extras
,
1847 const struct string_list
*skip
,
1851 const char *extra_refname
;
1852 struct strbuf dirname
= STRBUF_INIT
;
1853 struct strbuf referent
= STRBUF_INIT
;
1854 struct object_id oid
;
1856 struct ref_iterator
*iter
;
1861 * For the sake of comments in this function, suppose that
1862 * refname is "refs/foo/bar".
1867 strbuf_grow(&dirname
, strlen(refname
) + 1);
1868 for (slash
= strchr(refname
, '/'); slash
; slash
= strchr(slash
+ 1, '/')) {
1869 /* Expand dirname to the new prefix, not including the trailing slash: */
1870 strbuf_add(&dirname
, refname
+ dirname
.len
, slash
- refname
- dirname
.len
);
1873 * We are still at a leading dir of the refname (e.g.,
1874 * "refs/foo"; if there is a reference with that name,
1875 * it is a conflict, *unless* it is in skip.
1877 if (skip
&& string_list_has_string(skip
, dirname
.buf
))
1880 if (!refs_read_raw_ref(refs
, dirname
.buf
, oid
.hash
, &referent
, &type
)) {
1881 strbuf_addf(err
, "'%s' exists; cannot create '%s'",
1882 dirname
.buf
, refname
);
1886 if (extras
&& string_list_has_string(extras
, dirname
.buf
)) {
1887 strbuf_addf(err
, "cannot process '%s' and '%s' at the same time",
1888 refname
, dirname
.buf
);
1894 * We are at the leaf of our refname (e.g., "refs/foo/bar").
1895 * There is no point in searching for a reference with that
1896 * name, because a refname isn't considered to conflict with
1897 * itself. But we still need to check for references whose
1898 * names are in the "refs/foo/bar/" namespace, because they
1901 strbuf_addstr(&dirname
, refname
+ dirname
.len
);
1902 strbuf_addch(&dirname
, '/');
1904 iter
= refs_ref_iterator_begin(refs
, dirname
.buf
, 0,
1905 DO_FOR_EACH_INCLUDE_BROKEN
);
1906 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
1908 string_list_has_string(skip
, iter
->refname
))
1911 strbuf_addf(err
, "'%s' exists; cannot create '%s'",
1912 iter
->refname
, refname
);
1913 ref_iterator_abort(iter
);
1917 if (ok
!= ITER_DONE
)
1918 die("BUG: error while iterating over references");
1920 extra_refname
= find_descendant_ref(dirname
.buf
, extras
, skip
);
1922 strbuf_addf(err
, "cannot process '%s' and '%s' at the same time",
1923 refname
, extra_refname
);
1928 strbuf_release(&referent
);
1929 strbuf_release(&dirname
);
1933 int refs_for_each_reflog(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
1935 struct ref_iterator
*iter
;
1937 iter
= refs
->be
->reflog_iterator_begin(refs
);
1939 return do_for_each_ref_iterator(iter
, fn
, cb_data
);
1942 int for_each_reflog(each_ref_fn fn
, void *cb_data
)
1944 return refs_for_each_reflog(get_main_ref_store(), fn
, cb_data
);
1947 int refs_for_each_reflog_ent_reverse(struct ref_store
*refs
,
1948 const char *refname
,
1949 each_reflog_ent_fn fn
,
1952 return refs
->be
->for_each_reflog_ent_reverse(refs
, refname
,
1956 int for_each_reflog_ent_reverse(const char *refname
, each_reflog_ent_fn fn
,
1959 return refs_for_each_reflog_ent_reverse(get_main_ref_store(),
1960 refname
, fn
, cb_data
);
1963 int refs_for_each_reflog_ent(struct ref_store
*refs
, const char *refname
,
1964 each_reflog_ent_fn fn
, void *cb_data
)
1966 return refs
->be
->for_each_reflog_ent(refs
, refname
, fn
, cb_data
);
1969 int for_each_reflog_ent(const char *refname
, each_reflog_ent_fn fn
,
1972 return refs_for_each_reflog_ent(get_main_ref_store(), refname
,
1976 int refs_reflog_exists(struct ref_store
*refs
, const char *refname
)
1978 return refs
->be
->reflog_exists(refs
, refname
);
1981 int reflog_exists(const char *refname
)
1983 return refs_reflog_exists(get_main_ref_store(), refname
);
1986 int refs_create_reflog(struct ref_store
*refs
, const char *refname
,
1987 int force_create
, struct strbuf
*err
)
1989 return refs
->be
->create_reflog(refs
, refname
, force_create
, err
);
1992 int safe_create_reflog(const char *refname
, int force_create
,
1995 return refs_create_reflog(get_main_ref_store(), refname
,
1999 int refs_delete_reflog(struct ref_store
*refs
, const char *refname
)
2001 return refs
->be
->delete_reflog(refs
, refname
);
2004 int delete_reflog(const char *refname
)
2006 return refs_delete_reflog(get_main_ref_store(), refname
);
2009 int refs_reflog_expire(struct ref_store
*refs
,
2010 const char *refname
, const unsigned char *sha1
,
2012 reflog_expiry_prepare_fn prepare_fn
,
2013 reflog_expiry_should_prune_fn should_prune_fn
,
2014 reflog_expiry_cleanup_fn cleanup_fn
,
2015 void *policy_cb_data
)
2017 return refs
->be
->reflog_expire(refs
, refname
, sha1
, flags
,
2018 prepare_fn
, should_prune_fn
,
2019 cleanup_fn
, policy_cb_data
);
2022 int reflog_expire(const char *refname
, const unsigned char *sha1
,
2024 reflog_expiry_prepare_fn prepare_fn
,
2025 reflog_expiry_should_prune_fn should_prune_fn
,
2026 reflog_expiry_cleanup_fn cleanup_fn
,
2027 void *policy_cb_data
)
2029 return refs_reflog_expire(get_main_ref_store(),
2030 refname
, sha1
, flags
,
2031 prepare_fn
, should_prune_fn
,
2032 cleanup_fn
, policy_cb_data
);
2035 int initial_ref_transaction_commit(struct ref_transaction
*transaction
,
2038 struct ref_store
*refs
= transaction
->ref_store
;
2040 return refs
->be
->initial_transaction_commit(refs
, transaction
, err
);
2043 int refs_delete_refs(struct ref_store
*refs
, const char *msg
,
2044 struct string_list
*refnames
, unsigned int flags
)
2046 return refs
->be
->delete_refs(refs
, msg
, refnames
, flags
);
2049 int delete_refs(const char *msg
, struct string_list
*refnames
,
2052 return refs_delete_refs(get_main_ref_store(), msg
, refnames
, flags
);
2055 int refs_rename_ref(struct ref_store
*refs
, const char *oldref
,
2056 const char *newref
, const char *logmsg
)
2058 return refs
->be
->rename_ref(refs
, oldref
, newref
, logmsg
);
2061 int rename_ref(const char *oldref
, const char *newref
, const char *logmsg
)
2063 return refs_rename_ref(get_main_ref_store(), oldref
, newref
, logmsg
);