2 * The backend-independent part of the reference module.
11 #include "refs/refs-internal.h"
14 #include "submodule.h"
16 #include "argv-array.h"
19 * List of all available backends
21 static struct ref_storage_be *refs_backends = &refs_be_files;
23 static struct ref_storage_be *find_ref_storage_backend(const char *name)
25 struct ref_storage_be *be;
26 for (be = refs_backends; be; be = be->next)
27 if (!strcmp(be->name, name))
32 int ref_storage_backend_exists(const char *name)
34 return find_ref_storage_backend(name) != NULL;
38 * How to handle various characters in refnames:
39 * 0: An acceptable character for refs
41 * 2: ., look for a preceding . to reject .. in refs
42 * 3: {, look for a preceding @ to reject @{ in refs
43 * 4: A bad character: ASCII control characters, and
44 * ":", "?", "[", "\", "^", "~", SP, or TAB
45 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
47 static unsigned char refname_disposition[256] = {
48 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
49 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
50 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
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, 4, 4, 0, 4, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
59 * Try to read one refname component from the front of refname.
60 * Return the length of the component found, or -1 if the component is
61 * not legal. It is legal if it is something reasonable to have under
62 * ".git/refs/"; We do not like it if:
64 * - any path component of it begins with ".", or
65 * - it has double dots "..", or
66 * - it has ASCII control characters, or
67 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
68 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
69 * - it ends with a "/", or
70 * - it ends with ".lock", or
71 * - it contains a "@{" portion
73 static int check_refname_component(const char *refname, int *flags)
78 for (cp = refname; ; cp++) {
80 unsigned char disp = refname_disposition[ch];
86 return -1; /* Refname contains "..". */
90 return -1; /* Refname contains "@{". */
95 if (!(*flags & REFNAME_REFSPEC_PATTERN))
96 return -1; /* refspec can't be a pattern */
99 * Unset the pattern flag so that we only accept
100 * a single asterisk for one side of refspec.
102 *flags &= ~ REFNAME_REFSPEC_PATTERN;
109 return 0; /* Component has zero length. */
110 if (refname[0] == '.')
111 return -1; /* Component starts with '.'. */
112 if (cp - refname >= LOCK_SUFFIX_LEN &&
113 !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
114 return -1; /* Refname ends with ".lock". */
118 int check_refname_format(const char *refname, int flags)
120 int component_len, component_count = 0;
122 if (!strcmp(refname, "@"))
123 /* Refname is a single character '@'. */
127 /* We are at the start of a path component. */
128 component_len = check_refname_component(refname, &flags);
129 if (component_len <= 0)
133 if (refname[component_len] == '\0')
135 /* Skip to next component. */
136 refname += component_len + 1;
139 if (refname[component_len - 1] == '.')
140 return -1; /* Refname ends with '.'. */
141 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
142 return -1; /* Refname has only one component. */
146 int refname_is_safe(const char *refname)
150 if (skip_prefix(refname, "refs/", &rest)) {
153 size_t restlen = strlen(rest);
155 /* rest must not be empty, or start or end with "/" */
156 if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
160 * Does the refname try to escape refs/?
161 * For example: refs/foo/../bar is safe but refs/foo/../../bar
164 buf = xmallocz(restlen);
165 result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
171 if (!isupper(*refname) && *refname != '_')
179 * Return true if refname, which has the specified oid and flags, can
180 * be resolved to an object in the database. If the referred-to object
181 * does not exist, emit a warning and return false.
183 int ref_resolves_to_object(const char *refname,
184 const struct object_id *oid,
187 if (flags & REF_ISBROKEN)
189 if (!has_sha1_file(oid->hash)) {
190 error("%s does not point to a valid object!", refname);
196 char *refs_resolve_refdup(struct ref_store *refs,
197 const char *refname, int resolve_flags,
198 struct object_id *oid, int *flags)
202 result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
204 return xstrdup_or_null(result);
207 char *resolve_refdup(const char *refname, int resolve_flags,
208 struct object_id *oid, int *flags)
210 return refs_resolve_refdup(get_main_ref_store(),
211 refname, resolve_flags,
215 /* The argument to filter_refs */
222 int refs_read_ref_full(struct ref_store *refs, const char *refname,
223 int resolve_flags, struct object_id *oid, int *flags)
225 if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, oid, flags))
230 int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags)
232 return refs_read_ref_full(get_main_ref_store(), refname,
233 resolve_flags, oid, flags);
236 int read_ref(const char *refname, struct object_id *oid)
238 return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL);
241 int ref_exists(const char *refname)
243 return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, NULL, NULL);
246 static int match_ref_pattern(const char *refname,
247 const struct string_list_item *item)
250 if (item->util == NULL) {
251 if (!wildmatch(item->string, refname, 0))
255 if (skip_prefix(refname, item->string, &rest) &&
256 (!*rest || *rest == '/'))
262 int ref_filter_match(const char *refname,
263 const struct string_list *include_patterns,
264 const struct string_list *exclude_patterns)
266 struct string_list_item *item;
268 if (exclude_patterns && exclude_patterns->nr) {
269 for_each_string_list_item(item, exclude_patterns) {
270 if (match_ref_pattern(refname, item))
275 if (include_patterns && include_patterns->nr) {
277 for_each_string_list_item(item, include_patterns) {
278 if (match_ref_pattern(refname, item)) {
290 static int filter_refs(const char *refname, const struct object_id *oid,
291 int flags, void *data)
293 struct ref_filter *filter = (struct ref_filter *)data;
295 if (wildmatch(filter->pattern, refname, 0))
297 return filter->fn(refname, oid, flags, filter->cb_data);
300 enum peel_status peel_object(const struct object_id *name, struct object_id *oid)
302 struct object *o = lookup_unknown_object(name->hash);
304 if (o->type == OBJ_NONE) {
305 int type = oid_object_info(name, NULL);
306 if (type < 0 || !object_as_type(o, type, 0))
310 if (o->type != OBJ_TAG)
313 o = deref_tag_noverify(o);
317 oidcpy(oid, &o->oid);
321 struct warn_if_dangling_data {
324 const struct string_list *refnames;
328 static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
329 int flags, void *cb_data)
331 struct warn_if_dangling_data *d = cb_data;
332 const char *resolves_to;
334 if (!(flags & REF_ISSYMREF))
337 resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL);
340 ? strcmp(resolves_to, d->refname)
341 : !string_list_has_string(d->refnames, resolves_to))) {
345 fprintf(d->fp, d->msg_fmt, refname);
350 void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
352 struct warn_if_dangling_data data;
355 data.refname = refname;
356 data.refnames = NULL;
357 data.msg_fmt = msg_fmt;
358 for_each_rawref(warn_if_dangling_symref, &data);
361 void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
363 struct warn_if_dangling_data data;
367 data.refnames = refnames;
368 data.msg_fmt = msg_fmt;
369 for_each_rawref(warn_if_dangling_symref, &data);
372 int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
374 return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data);
377 int for_each_tag_ref(each_ref_fn fn, void *cb_data)
379 return refs_for_each_tag_ref(get_main_ref_store(), fn, cb_data);
382 int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
384 return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data);
387 int for_each_branch_ref(each_ref_fn fn, void *cb_data)
389 return refs_for_each_branch_ref(get_main_ref_store(), fn, cb_data);
392 int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
394 return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data);
397 int for_each_remote_ref(each_ref_fn fn, void *cb_data)
399 return refs_for_each_remote_ref(get_main_ref_store(), fn, cb_data);
402 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
404 struct strbuf buf = STRBUF_INIT;
406 struct object_id oid;
409 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
410 if (!read_ref_full(buf.buf, RESOLVE_REF_READING, &oid, &flag))
411 ret = fn(buf.buf, &oid, flag, cb_data);
412 strbuf_release(&buf);
417 void normalize_glob_ref(struct string_list_item *item, const char *prefix,
420 struct strbuf normalized_pattern = STRBUF_INIT;
423 BUG("pattern must not start with '/'");
426 strbuf_addstr(&normalized_pattern, prefix);
428 else if (!starts_with(pattern, "refs/"))
429 strbuf_addstr(&normalized_pattern, "refs/");
430 strbuf_addstr(&normalized_pattern, pattern);
431 strbuf_strip_suffix(&normalized_pattern, "/");
433 item->string = strbuf_detach(&normalized_pattern, NULL);
434 item->util = has_glob_specials(pattern) ? NULL : item->string;
435 strbuf_release(&normalized_pattern);
438 int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
439 const char *prefix, void *cb_data)
441 struct strbuf real_pattern = STRBUF_INIT;
442 struct ref_filter filter;
445 if (!prefix && !starts_with(pattern, "refs/"))
446 strbuf_addstr(&real_pattern, "refs/");
448 strbuf_addstr(&real_pattern, prefix);
449 strbuf_addstr(&real_pattern, pattern);
451 if (!has_glob_specials(pattern)) {
452 /* Append implied '/' '*' if not present. */
453 strbuf_complete(&real_pattern, '/');
454 /* No need to check for '*', there is none. */
455 strbuf_addch(&real_pattern, '*');
458 filter.pattern = real_pattern.buf;
460 filter.cb_data = cb_data;
461 ret = for_each_ref(filter_refs, &filter);
463 strbuf_release(&real_pattern);
467 int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
469 return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
472 const char *prettify_refname(const char *name)
474 if (skip_prefix(name, "refs/heads/", &name) ||
475 skip_prefix(name, "refs/tags/", &name) ||
476 skip_prefix(name, "refs/remotes/", &name))
481 static const char *ref_rev_parse_rules[] = {
487 "refs/remotes/%.*s/HEAD",
491 int refname_match(const char *abbrev_name, const char *full_name)
494 const int abbrev_name_len = strlen(abbrev_name);
496 for (p = ref_rev_parse_rules; *p; p++) {
497 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
506 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
507 * the results to 'prefixes'
509 void expand_ref_prefix(struct argv_array *prefixes, const char *prefix)
512 int len = strlen(prefix);
514 for (p = ref_rev_parse_rules; *p; p++)
515 argv_array_pushf(prefixes, *p, len, prefix);
519 * *string and *len will only be substituted, and *string returned (for
520 * later free()ing) if the string passed in is a magic short-hand form
523 static char *substitute_branch_name(const char **string, int *len)
525 struct strbuf buf = STRBUF_INIT;
526 int ret = interpret_branch_name(*string, *len, &buf, 0);
530 *string = strbuf_detach(&buf, &size);
532 return (char *)*string;
538 int dwim_ref(const char *str, int len, struct object_id *oid, char **ref)
540 char *last_branch = substitute_branch_name(&str, &len);
541 int refs_found = expand_ref(str, len, oid, ref);
546 int expand_ref(const char *str, int len, struct object_id *oid, char **ref)
550 struct strbuf fullref = STRBUF_INIT;
553 for (p = ref_rev_parse_rules; *p; p++) {
554 struct object_id oid_from_ref;
555 struct object_id *this_result;
558 this_result = refs_found ? &oid_from_ref : oid;
559 strbuf_reset(&fullref);
560 strbuf_addf(&fullref, *p, len, str);
561 r = resolve_ref_unsafe(fullref.buf, RESOLVE_REF_READING,
566 if (!warn_ambiguous_refs)
568 } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
569 warning("ignoring dangling symref %s.", fullref.buf);
570 } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) {
571 warning("ignoring broken ref %s.", fullref.buf);
574 strbuf_release(&fullref);
578 int dwim_log(const char *str, int len, struct object_id *oid, char **log)
580 char *last_branch = substitute_branch_name(&str, &len);
583 struct strbuf path = STRBUF_INIT;
586 for (p = ref_rev_parse_rules; *p; p++) {
587 struct object_id hash;
588 const char *ref, *it;
591 strbuf_addf(&path, *p, len, str);
592 ref = resolve_ref_unsafe(path.buf, RESOLVE_REF_READING,
596 if (reflog_exists(path.buf))
598 else if (strcmp(ref, path.buf) && reflog_exists(ref))
606 if (!warn_ambiguous_refs)
609 strbuf_release(&path);
614 static int is_per_worktree_ref(const char *refname)
616 return !strcmp(refname, "HEAD") ||
617 starts_with(refname, "refs/bisect/");
620 static int is_pseudoref_syntax(const char *refname)
624 for (c = refname; *c; c++) {
625 if (!isupper(*c) && *c != '-' && *c != '_')
632 enum ref_type ref_type(const char *refname)
634 if (is_per_worktree_ref(refname))
635 return REF_TYPE_PER_WORKTREE;
636 if (is_pseudoref_syntax(refname))
637 return REF_TYPE_PSEUDOREF;
638 return REF_TYPE_NORMAL;
641 long get_files_ref_lock_timeout_ms(void)
643 static int configured = 0;
645 /* The default timeout is 100 ms: */
646 static int timeout_ms = 100;
649 git_config_get_int("core.filesreflocktimeout", &timeout_ms);
656 static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
657 const struct object_id *old_oid, struct strbuf *err)
659 const char *filename;
661 static struct lock_file lock;
662 struct strbuf buf = STRBUF_INIT;
668 strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
670 filename = git_path("%s", pseudoref);
671 fd = hold_lock_file_for_update_timeout(&lock, filename,
673 get_files_ref_lock_timeout_ms());
675 strbuf_addf(err, "could not open '%s' for writing: %s",
676 filename, strerror(errno));
681 struct object_id actual_old_oid;
683 if (read_ref(pseudoref, &actual_old_oid))
684 die("could not read ref '%s'", pseudoref);
685 if (oidcmp(&actual_old_oid, old_oid)) {
686 strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref);
687 rollback_lock_file(&lock);
692 if (write_in_full(fd, buf.buf, buf.len) < 0) {
693 strbuf_addf(err, "could not write to '%s'", filename);
694 rollback_lock_file(&lock);
698 commit_lock_file(&lock);
701 strbuf_release(&buf);
705 static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid)
707 static struct lock_file lock;
708 const char *filename;
710 filename = git_path("%s", pseudoref);
712 if (old_oid && !is_null_oid(old_oid)) {
714 struct object_id actual_old_oid;
716 fd = hold_lock_file_for_update_timeout(
717 &lock, filename, LOCK_DIE_ON_ERROR,
718 get_files_ref_lock_timeout_ms());
720 die_errno(_("Could not open '%s' for writing"), filename);
721 if (read_ref(pseudoref, &actual_old_oid))
722 die("could not read ref '%s'", pseudoref);
723 if (oidcmp(&actual_old_oid, old_oid)) {
724 warning("Unexpected sha1 when deleting %s", pseudoref);
725 rollback_lock_file(&lock);
730 rollback_lock_file(&lock);
738 int refs_delete_ref(struct ref_store *refs, const char *msg,
740 const struct object_id *old_oid,
743 struct ref_transaction *transaction;
744 struct strbuf err = STRBUF_INIT;
746 if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
747 assert(refs == get_main_ref_store());
748 return delete_pseudoref(refname, old_oid);
751 transaction = ref_store_transaction_begin(refs, &err);
753 ref_transaction_delete(transaction, refname, old_oid,
755 ref_transaction_commit(transaction, &err)) {
756 error("%s", err.buf);
757 ref_transaction_free(transaction);
758 strbuf_release(&err);
761 ref_transaction_free(transaction);
762 strbuf_release(&err);
766 int delete_ref(const char *msg, const char *refname,
767 const struct object_id *old_oid, unsigned int flags)
769 return refs_delete_ref(get_main_ref_store(), msg, refname,
773 int copy_reflog_msg(char *buf, const char *msg)
780 while ((c = *msg++)) {
781 if (wasspace && isspace(c))
783 wasspace = isspace(c);
788 while (buf < cp && isspace(cp[-1]))
794 int should_autocreate_reflog(const char *refname)
796 switch (log_all_ref_updates) {
797 case LOG_REFS_ALWAYS:
799 case LOG_REFS_NORMAL:
800 return starts_with(refname, "refs/heads/") ||
801 starts_with(refname, "refs/remotes/") ||
802 starts_with(refname, "refs/notes/") ||
803 !strcmp(refname, "HEAD");
809 int is_branch(const char *refname)
811 return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
814 struct read_ref_at_cb {
819 struct object_id *oid;
822 struct object_id ooid;
823 struct object_id noid;
827 timestamp_t *cutoff_time;
832 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
833 const char *email, timestamp_t timestamp, int tz,
834 const char *message, void *cb_data)
836 struct read_ref_at_cb *cb = cb_data;
840 cb->date = timestamp;
842 if (timestamp <= cb->at_time || cb->cnt == 0) {
844 *cb->msg = xstrdup(message);
846 *cb->cutoff_time = timestamp;
850 *cb->cutoff_cnt = cb->reccnt - 1;
852 * we have not yet updated cb->[n|o]oid so they still
853 * hold the values for the previous record.
855 if (!is_null_oid(&cb->ooid)) {
856 oidcpy(cb->oid, noid);
857 if (oidcmp(&cb->ooid, noid))
858 warning("Log for ref %s has gap after %s.",
859 cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
861 else if (cb->date == cb->at_time)
862 oidcpy(cb->oid, noid);
863 else if (oidcmp(noid, cb->oid))
864 warning("Log for ref %s unexpectedly ended on %s.",
865 cb->refname, show_date(cb->date, cb->tz,
866 DATE_MODE(RFC2822)));
867 oidcpy(&cb->ooid, ooid);
868 oidcpy(&cb->noid, noid);
872 oidcpy(&cb->ooid, ooid);
873 oidcpy(&cb->noid, noid);
879 static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
880 const char *email, timestamp_t timestamp,
881 int tz, const char *message, void *cb_data)
883 struct read_ref_at_cb *cb = cb_data;
886 *cb->msg = xstrdup(message);
888 *cb->cutoff_time = timestamp;
892 *cb->cutoff_cnt = cb->reccnt;
893 oidcpy(cb->oid, ooid);
894 if (is_null_oid(cb->oid))
895 oidcpy(cb->oid, noid);
896 /* We just want the first entry */
900 int read_ref_at(const char *refname, unsigned int flags, timestamp_t at_time, int cnt,
901 struct object_id *oid, char **msg,
902 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
904 struct read_ref_at_cb cb;
906 memset(&cb, 0, sizeof(cb));
907 cb.refname = refname;
908 cb.at_time = at_time;
911 cb.cutoff_time = cutoff_time;
912 cb.cutoff_tz = cutoff_tz;
913 cb.cutoff_cnt = cutoff_cnt;
916 for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
919 if (flags & GET_OID_QUIETLY)
922 die("Log for %s is empty.", refname);
927 for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
932 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
935 struct ref_transaction *tr;
938 tr = xcalloc(1, sizeof(struct ref_transaction));
939 tr->ref_store = refs;
943 struct ref_transaction *ref_transaction_begin(struct strbuf *err)
945 return ref_store_transaction_begin(get_main_ref_store(), err);
948 void ref_transaction_free(struct ref_transaction *transaction)
955 switch (transaction->state) {
956 case REF_TRANSACTION_OPEN:
957 case REF_TRANSACTION_CLOSED:
960 case REF_TRANSACTION_PREPARED:
961 die("BUG: free called on a prepared reference transaction");
964 die("BUG: unexpected reference transaction state");
968 for (i = 0; i < transaction->nr; i++) {
969 free(transaction->updates[i]->msg);
970 free(transaction->updates[i]);
972 free(transaction->updates);
976 struct ref_update *ref_transaction_add_update(
977 struct ref_transaction *transaction,
978 const char *refname, unsigned int flags,
979 const struct object_id *new_oid,
980 const struct object_id *old_oid,
983 struct ref_update *update;
985 if (transaction->state != REF_TRANSACTION_OPEN)
986 die("BUG: update called for transaction that is not open");
988 FLEX_ALLOC_STR(update, refname, refname);
989 ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
990 transaction->updates[transaction->nr++] = update;
992 update->flags = flags;
994 if (flags & REF_HAVE_NEW)
995 oidcpy(&update->new_oid, new_oid);
996 if (flags & REF_HAVE_OLD)
997 oidcpy(&update->old_oid, old_oid);
998 update->msg = xstrdup_or_null(msg);
1002 int ref_transaction_update(struct ref_transaction *transaction,
1003 const char *refname,
1004 const struct object_id *new_oid,
1005 const struct object_id *old_oid,
1006 unsigned int flags, const char *msg,
1011 if ((new_oid && !is_null_oid(new_oid)) ?
1012 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
1013 !refname_is_safe(refname)) {
1014 strbuf_addf(err, "refusing to update ref with bad name '%s'",
1019 if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
1020 BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
1022 flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
1024 ref_transaction_add_update(transaction, refname, flags,
1025 new_oid, old_oid, msg);
1029 int ref_transaction_create(struct ref_transaction *transaction,
1030 const char *refname,
1031 const struct object_id *new_oid,
1032 unsigned int flags, const char *msg,
1035 if (!new_oid || is_null_oid(new_oid))
1036 die("BUG: create called without valid new_oid");
1037 return ref_transaction_update(transaction, refname, new_oid,
1038 &null_oid, flags, msg, err);
1041 int ref_transaction_delete(struct ref_transaction *transaction,
1042 const char *refname,
1043 const struct object_id *old_oid,
1044 unsigned int flags, const char *msg,
1047 if (old_oid && is_null_oid(old_oid))
1048 die("BUG: delete called with old_oid set to zeros");
1049 return ref_transaction_update(transaction, refname,
1054 int ref_transaction_verify(struct ref_transaction *transaction,
1055 const char *refname,
1056 const struct object_id *old_oid,
1061 die("BUG: verify called with old_oid set to NULL");
1062 return ref_transaction_update(transaction, refname,
1067 int refs_update_ref(struct ref_store *refs, const char *msg,
1068 const char *refname, const struct object_id *new_oid,
1069 const struct object_id *old_oid, unsigned int flags,
1070 enum action_on_err onerr)
1072 struct ref_transaction *t = NULL;
1073 struct strbuf err = STRBUF_INIT;
1076 if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
1077 assert(refs == get_main_ref_store());
1078 ret = write_pseudoref(refname, new_oid, old_oid, &err);
1080 t = ref_store_transaction_begin(refs, &err);
1082 ref_transaction_update(t, refname, new_oid, old_oid,
1083 flags, msg, &err) ||
1084 ref_transaction_commit(t, &err)) {
1086 ref_transaction_free(t);
1090 const char *str = "update_ref failed for ref '%s': %s";
1093 case UPDATE_REFS_MSG_ON_ERR:
1094 error(str, refname, err.buf);
1096 case UPDATE_REFS_DIE_ON_ERR:
1097 die(str, refname, err.buf);
1099 case UPDATE_REFS_QUIET_ON_ERR:
1102 strbuf_release(&err);
1105 strbuf_release(&err);
1107 ref_transaction_free(t);
1111 int update_ref(const char *msg, const char *refname,
1112 const struct object_id *new_oid,
1113 const struct object_id *old_oid,
1114 unsigned int flags, enum action_on_err onerr)
1116 return refs_update_ref(get_main_ref_store(), msg, refname, new_oid,
1117 old_oid, flags, onerr);
1120 char *shorten_unambiguous_ref(const char *refname, int strict)
1123 static char **scanf_fmts;
1124 static int nr_rules;
1126 struct strbuf resolved_buf = STRBUF_INIT;
1130 * Pre-generate scanf formats from ref_rev_parse_rules[].
1131 * Generate a format suitable for scanf from a
1132 * ref_rev_parse_rules rule by interpolating "%s" at the
1133 * location of the "%.*s".
1135 size_t total_len = 0;
1138 /* the rule list is NULL terminated, count them first */
1139 for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
1140 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
1141 total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
1143 scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), nr_rules), total_len));
1146 for (i = 0; i < nr_rules; i++) {
1147 assert(offset < total_len);
1148 scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
1149 offset += snprintf(scanf_fmts[i], total_len - offset,
1150 ref_rev_parse_rules[i], 2, "%s") + 1;
1154 /* bail out if there are no rules */
1156 return xstrdup(refname);
1158 /* buffer for scanf result, at most refname must fit */
1159 short_name = xstrdup(refname);
1161 /* skip first rule, it will always match */
1162 for (i = nr_rules - 1; i > 0 ; --i) {
1164 int rules_to_fail = i;
1167 if (1 != sscanf(refname, scanf_fmts[i], short_name))
1170 short_name_len = strlen(short_name);
1173 * in strict mode, all (except the matched one) rules
1174 * must fail to resolve to a valid non-ambiguous ref
1177 rules_to_fail = nr_rules;
1180 * check if the short name resolves to a valid ref,
1181 * but use only rules prior to the matched one
1183 for (j = 0; j < rules_to_fail; j++) {
1184 const char *rule = ref_rev_parse_rules[j];
1186 /* skip matched rule */
1191 * the short name is ambiguous, if it resolves
1192 * (with this previous rule) to a valid ref
1193 * read_ref() returns 0 on success
1195 strbuf_reset(&resolved_buf);
1196 strbuf_addf(&resolved_buf, rule,
1197 short_name_len, short_name);
1198 if (ref_exists(resolved_buf.buf))
1203 * short name is non-ambiguous if all previous rules
1204 * haven't resolved to a valid ref
1206 if (j == rules_to_fail) {
1207 strbuf_release(&resolved_buf);
1212 strbuf_release(&resolved_buf);
1214 return xstrdup(refname);
1217 static struct string_list *hide_refs;
1219 int parse_hide_refs_config(const char *var, const char *value, const char *section)
1222 if (!strcmp("transfer.hiderefs", var) ||
1223 (!parse_config_key(var, section, NULL, NULL, &key) &&
1224 !strcmp(key, "hiderefs"))) {
1229 return config_error_nonbool(var);
1230 ref = xstrdup(value);
1232 while (len && ref[len - 1] == '/')
1235 hide_refs = xcalloc(1, sizeof(*hide_refs));
1236 hide_refs->strdup_strings = 1;
1238 string_list_append(hide_refs, ref);
1243 int ref_is_hidden(const char *refname, const char *refname_full)
1249 for (i = hide_refs->nr - 1; i >= 0; i--) {
1250 const char *match = hide_refs->items[i].string;
1251 const char *subject;
1255 if (*match == '!') {
1260 if (*match == '^') {
1261 subject = refname_full;
1267 /* refname can be NULL when namespaces are used. */
1269 skip_prefix(subject, match, &p) &&
1276 const char *find_descendant_ref(const char *dirname,
1277 const struct string_list *extras,
1278 const struct string_list *skip)
1286 * Look at the place where dirname would be inserted into
1287 * extras. If there is an entry at that position that starts
1288 * with dirname (remember, dirname includes the trailing
1289 * slash) and is not in skip, then we have a conflict.
1291 for (pos = string_list_find_insert_index(extras, dirname, 0);
1292 pos < extras->nr; pos++) {
1293 const char *extra_refname = extras->items[pos].string;
1295 if (!starts_with(extra_refname, dirname))
1298 if (!skip || !string_list_has_string(skip, extra_refname))
1299 return extra_refname;
1304 int refs_rename_ref_available(struct ref_store *refs,
1305 const char *old_refname,
1306 const char *new_refname)
1308 struct string_list skip = STRING_LIST_INIT_NODUP;
1309 struct strbuf err = STRBUF_INIT;
1312 string_list_insert(&skip, old_refname);
1313 ok = !refs_verify_refname_available(refs, new_refname,
1316 error("%s", err.buf);
1318 string_list_clear(&skip, 0);
1319 strbuf_release(&err);
1323 int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1325 struct object_id oid;
1328 if (!refs_read_ref_full(refs, "HEAD", RESOLVE_REF_READING,
1330 return fn("HEAD", &oid, flag, cb_data);
1335 int head_ref(each_ref_fn fn, void *cb_data)
1337 return refs_head_ref(get_main_ref_store(), fn, cb_data);
1340 struct ref_iterator *refs_ref_iterator_begin(
1341 struct ref_store *refs,
1342 const char *prefix, int trim, int flags)
1344 struct ref_iterator *iter;
1346 if (ref_paranoia < 0)
1347 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
1349 flags |= DO_FOR_EACH_INCLUDE_BROKEN;
1351 iter = refs->be->iterator_begin(refs, prefix, flags);
1354 * `iterator_begin()` already takes care of prefix, but we
1355 * might need to do some trimming:
1358 iter = prefix_ref_iterator_begin(iter, "", trim);
1360 /* Sanity check for subclasses: */
1362 BUG("reference iterator is not ordered");
1368 * Call fn for each reference in the specified submodule for which the
1369 * refname begins with prefix. If trim is non-zero, then trim that
1370 * many characters off the beginning of each refname before passing
1371 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1372 * include broken references in the iteration. If fn ever returns a
1373 * non-zero value, stop the iteration and return that value;
1374 * otherwise, return 0.
1376 static int do_for_each_ref(struct ref_store *refs, const char *prefix,
1377 each_ref_fn fn, int trim, int flags, void *cb_data)
1379 struct ref_iterator *iter;
1384 iter = refs_ref_iterator_begin(refs, prefix, trim, flags);
1386 return do_for_each_ref_iterator(iter, fn, cb_data);
1389 int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1391 return do_for_each_ref(refs, "", fn, 0, 0, cb_data);
1394 int for_each_ref(each_ref_fn fn, void *cb_data)
1396 return refs_for_each_ref(get_main_ref_store(), fn, cb_data);
1399 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
1400 each_ref_fn fn, void *cb_data)
1402 return do_for_each_ref(refs, prefix, fn, strlen(prefix), 0, cb_data);
1405 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1407 return refs_for_each_ref_in(get_main_ref_store(), prefix, fn, cb_data);
1410 int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1412 unsigned int flag = 0;
1415 flag = DO_FOR_EACH_INCLUDE_BROKEN;
1416 return do_for_each_ref(get_main_ref_store(),
1417 prefix, fn, 0, flag, cb_data);
1420 int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
1421 each_ref_fn fn, void *cb_data,
1422 unsigned int broken)
1424 unsigned int flag = 0;
1427 flag = DO_FOR_EACH_INCLUDE_BROKEN;
1428 return do_for_each_ref(refs, prefix, fn, 0, flag, cb_data);
1431 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1433 return do_for_each_ref(get_main_ref_store(),
1434 git_replace_ref_base, fn,
1435 strlen(git_replace_ref_base),
1436 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1439 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1441 struct strbuf buf = STRBUF_INIT;
1443 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1444 ret = do_for_each_ref(get_main_ref_store(),
1445 buf.buf, fn, 0, 0, cb_data);
1446 strbuf_release(&buf);
1450 int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1452 return do_for_each_ref(refs, "", fn, 0,
1453 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1456 int for_each_rawref(each_ref_fn fn, void *cb_data)
1458 return refs_for_each_rawref(get_main_ref_store(), fn, cb_data);
1461 int refs_read_raw_ref(struct ref_store *ref_store,
1462 const char *refname, struct object_id *oid,
1463 struct strbuf *referent, unsigned int *type)
1465 return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, type);
1468 /* This function needs to return a meaningful errno on failure */
1469 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1470 const char *refname,
1472 struct object_id *oid, int *flags)
1474 static struct strbuf sb_refname = STRBUF_INIT;
1475 struct object_id unused_oid;
1482 flags = &unused_flags;
1486 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1487 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1488 !refname_is_safe(refname)) {
1494 * dwim_ref() uses REF_ISBROKEN to distinguish between
1495 * missing refs and refs that were present but invalid,
1496 * to complain about the latter to stderr.
1498 * We don't know whether the ref exists, so don't set
1501 *flags |= REF_BAD_NAME;
1504 for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1505 unsigned int read_flags = 0;
1507 if (refs_read_raw_ref(refs, refname,
1508 oid, &sb_refname, &read_flags)) {
1509 *flags |= read_flags;
1511 /* In reading mode, refs must eventually resolve */
1512 if (resolve_flags & RESOLVE_REF_READING)
1516 * Otherwise a missing ref is OK. But the files backend
1517 * may show errors besides ENOENT if there are
1518 * similarly-named refs.
1520 if (errno != ENOENT &&
1526 if (*flags & REF_BAD_NAME)
1527 *flags |= REF_ISBROKEN;
1531 *flags |= read_flags;
1533 if (!(read_flags & REF_ISSYMREF)) {
1534 if (*flags & REF_BAD_NAME) {
1536 *flags |= REF_ISBROKEN;
1541 refname = sb_refname.buf;
1542 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1546 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1547 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1548 !refname_is_safe(refname)) {
1553 *flags |= REF_ISBROKEN | REF_BAD_NAME;
1561 /* backend functions */
1562 int refs_init_db(struct strbuf *err)
1564 struct ref_store *refs = get_main_ref_store();
1566 return refs->be->init_db(refs, err);
1569 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1570 struct object_id *oid, int *flags)
1572 return refs_resolve_ref_unsafe(get_main_ref_store(), refname,
1573 resolve_flags, oid, flags);
1576 int resolve_gitlink_ref(const char *submodule, const char *refname,
1577 struct object_id *oid)
1579 struct ref_store *refs;
1582 refs = get_submodule_ref_store(submodule);
1587 if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) ||
1593 struct ref_store_hash_entry
1595 struct hashmap_entry ent; /* must be the first member! */
1597 struct ref_store *refs;
1599 /* NUL-terminated identifier of the ref store: */
1600 char name[FLEX_ARRAY];
1603 static int ref_store_hash_cmp(const void *unused_cmp_data,
1604 const void *entry, const void *entry_or_key,
1605 const void *keydata)
1607 const struct ref_store_hash_entry *e1 = entry, *e2 = entry_or_key;
1608 const char *name = keydata ? keydata : e2->name;
1610 return strcmp(e1->name, name);
1613 static struct ref_store_hash_entry *alloc_ref_store_hash_entry(
1614 const char *name, struct ref_store *refs)
1616 struct ref_store_hash_entry *entry;
1618 FLEX_ALLOC_STR(entry, name, name);
1619 hashmap_entry_init(entry, strhash(name));
1624 /* A pointer to the ref_store for the main repository: */
1625 static struct ref_store *main_ref_store;
1627 /* A hashmap of ref_stores, stored by submodule name: */
1628 static struct hashmap submodule_ref_stores;
1630 /* A hashmap of ref_stores, stored by worktree id: */
1631 static struct hashmap worktree_ref_stores;
1634 * Look up a ref store by name. If that ref_store hasn't been
1635 * registered yet, return NULL.
1637 static struct ref_store *lookup_ref_store_map(struct hashmap *map,
1640 struct ref_store_hash_entry *entry;
1642 if (!map->tablesize)
1643 /* It's initialized on demand in register_ref_store(). */
1646 entry = hashmap_get_from_hash(map, strhash(name), name);
1647 return entry ? entry->refs : NULL;
1651 * Create, record, and return a ref_store instance for the specified
1654 static struct ref_store *ref_store_init(const char *gitdir,
1657 const char *be_name = "files";
1658 struct ref_storage_be *be = find_ref_storage_backend(be_name);
1659 struct ref_store *refs;
1662 die("BUG: reference backend %s is unknown", be_name);
1664 refs = be->init(gitdir, flags);
1668 struct ref_store *get_main_ref_store(void)
1671 return main_ref_store;
1673 main_ref_store = ref_store_init(get_git_dir(), REF_STORE_ALL_CAPS);
1674 return main_ref_store;
1678 * Associate a ref store with a name. It is a fatal error to call this
1679 * function twice for the same name.
1681 static void register_ref_store_map(struct hashmap *map,
1683 struct ref_store *refs,
1686 if (!map->tablesize)
1687 hashmap_init(map, ref_store_hash_cmp, NULL, 0);
1689 if (hashmap_put(map, alloc_ref_store_hash_entry(name, refs)))
1690 die("BUG: %s ref_store '%s' initialized twice", type, name);
1693 struct ref_store *get_submodule_ref_store(const char *submodule)
1695 struct strbuf submodule_sb = STRBUF_INIT;
1696 struct ref_store *refs;
1697 char *to_free = NULL;
1703 len = strlen(submodule);
1704 while (len && is_dir_sep(submodule[len - 1]))
1710 /* We need to strip off one or more trailing slashes */
1711 submodule = to_free = xmemdupz(submodule, len);
1713 refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
1717 strbuf_addstr(&submodule_sb, submodule);
1718 if (!is_nonbare_repository_dir(&submodule_sb))
1721 if (submodule_to_gitdir(&submodule_sb, submodule))
1724 /* assume that add_submodule_odb() has been called */
1725 refs = ref_store_init(submodule_sb.buf,
1726 REF_STORE_READ | REF_STORE_ODB);
1727 register_ref_store_map(&submodule_ref_stores, "submodule",
1731 strbuf_release(&submodule_sb);
1737 struct ref_store *get_worktree_ref_store(const struct worktree *wt)
1739 struct ref_store *refs;
1743 return get_main_ref_store();
1745 id = wt->id ? wt->id : "/";
1746 refs = lookup_ref_store_map(&worktree_ref_stores, id);
1751 refs = ref_store_init(git_common_path("worktrees/%s", wt->id),
1752 REF_STORE_ALL_CAPS);
1754 refs = ref_store_init(get_git_common_dir(),
1755 REF_STORE_ALL_CAPS);
1758 register_ref_store_map(&worktree_ref_stores, "worktree",
1763 void base_ref_store_init(struct ref_store *refs,
1764 const struct ref_storage_be *be)
1769 /* backend functions */
1770 int refs_pack_refs(struct ref_store *refs, unsigned int flags)
1772 return refs->be->pack_refs(refs, flags);
1775 int refs_peel_ref(struct ref_store *refs, const char *refname,
1776 struct object_id *oid)
1779 struct object_id base;
1781 if (current_ref_iter && current_ref_iter->refname == refname) {
1782 struct object_id peeled;
1784 if (ref_iterator_peel(current_ref_iter, &peeled))
1786 oidcpy(oid, &peeled);
1790 if (refs_read_ref_full(refs, refname,
1791 RESOLVE_REF_READING, &base, &flag))
1794 return peel_object(&base, oid);
1797 int peel_ref(const char *refname, struct object_id *oid)
1799 return refs_peel_ref(get_main_ref_store(), refname, oid);
1802 int refs_create_symref(struct ref_store *refs,
1803 const char *ref_target,
1804 const char *refs_heads_master,
1807 return refs->be->create_symref(refs, ref_target,
1812 int create_symref(const char *ref_target, const char *refs_heads_master,
1815 return refs_create_symref(get_main_ref_store(), ref_target,
1816 refs_heads_master, logmsg);
1819 int ref_update_reject_duplicates(struct string_list *refnames,
1822 size_t i, n = refnames->nr;
1826 for (i = 1; i < n; i++) {
1827 int cmp = strcmp(refnames->items[i - 1].string,
1828 refnames->items[i].string);
1832 "multiple updates for ref '%s' not allowed.",
1833 refnames->items[i].string);
1835 } else if (cmp > 0) {
1836 die("BUG: ref_update_reject_duplicates() received unsorted list");
1842 int ref_transaction_prepare(struct ref_transaction *transaction,
1845 struct ref_store *refs = transaction->ref_store;
1847 switch (transaction->state) {
1848 case REF_TRANSACTION_OPEN:
1851 case REF_TRANSACTION_PREPARED:
1852 die("BUG: prepare called twice on reference transaction");
1854 case REF_TRANSACTION_CLOSED:
1855 die("BUG: prepare called on a closed reference transaction");
1858 die("BUG: unexpected reference transaction state");
1862 if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
1864 _("ref updates forbidden inside quarantine environment"));
1868 return refs->be->transaction_prepare(refs, transaction, err);
1871 int ref_transaction_abort(struct ref_transaction *transaction,
1874 struct ref_store *refs = transaction->ref_store;
1877 switch (transaction->state) {
1878 case REF_TRANSACTION_OPEN:
1879 /* No need to abort explicitly. */
1881 case REF_TRANSACTION_PREPARED:
1882 ret = refs->be->transaction_abort(refs, transaction, err);
1884 case REF_TRANSACTION_CLOSED:
1885 die("BUG: abort called on a closed reference transaction");
1888 die("BUG: unexpected reference transaction state");
1892 ref_transaction_free(transaction);
1896 int ref_transaction_commit(struct ref_transaction *transaction,
1899 struct ref_store *refs = transaction->ref_store;
1902 switch (transaction->state) {
1903 case REF_TRANSACTION_OPEN:
1904 /* Need to prepare first. */
1905 ret = ref_transaction_prepare(transaction, err);
1909 case REF_TRANSACTION_PREPARED:
1910 /* Fall through to finish. */
1912 case REF_TRANSACTION_CLOSED:
1913 die("BUG: commit called on a closed reference transaction");
1916 die("BUG: unexpected reference transaction state");
1920 return refs->be->transaction_finish(refs, transaction, err);
1923 int refs_verify_refname_available(struct ref_store *refs,
1924 const char *refname,
1925 const struct string_list *extras,
1926 const struct string_list *skip,
1930 const char *extra_refname;
1931 struct strbuf dirname = STRBUF_INIT;
1932 struct strbuf referent = STRBUF_INIT;
1933 struct object_id oid;
1935 struct ref_iterator *iter;
1940 * For the sake of comments in this function, suppose that
1941 * refname is "refs/foo/bar".
1946 strbuf_grow(&dirname, strlen(refname) + 1);
1947 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
1948 /* Expand dirname to the new prefix, not including the trailing slash: */
1949 strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
1952 * We are still at a leading dir of the refname (e.g.,
1953 * "refs/foo"; if there is a reference with that name,
1954 * it is a conflict, *unless* it is in skip.
1956 if (skip && string_list_has_string(skip, dirname.buf))
1959 if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, &type)) {
1960 strbuf_addf(err, "'%s' exists; cannot create '%s'",
1961 dirname.buf, refname);
1965 if (extras && string_list_has_string(extras, dirname.buf)) {
1966 strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
1967 refname, dirname.buf);
1973 * We are at the leaf of our refname (e.g., "refs/foo/bar").
1974 * There is no point in searching for a reference with that
1975 * name, because a refname isn't considered to conflict with
1976 * itself. But we still need to check for references whose
1977 * names are in the "refs/foo/bar/" namespace, because they
1980 strbuf_addstr(&dirname, refname + dirname.len);
1981 strbuf_addch(&dirname, '/');
1983 iter = refs_ref_iterator_begin(refs, dirname.buf, 0,
1984 DO_FOR_EACH_INCLUDE_BROKEN);
1985 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1987 string_list_has_string(skip, iter->refname))
1990 strbuf_addf(err, "'%s' exists; cannot create '%s'",
1991 iter->refname, refname);
1992 ref_iterator_abort(iter);
1996 if (ok != ITER_DONE)
1997 die("BUG: error while iterating over references");
1999 extra_refname = find_descendant_ref(dirname.buf, extras, skip);
2001 strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
2002 refname, extra_refname);
2007 strbuf_release(&referent);
2008 strbuf_release(&dirname);
2012 int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2014 struct ref_iterator *iter;
2016 iter = refs->be->reflog_iterator_begin(refs);
2018 return do_for_each_ref_iterator(iter, fn, cb_data);
2021 int for_each_reflog(each_ref_fn fn, void *cb_data)
2023 return refs_for_each_reflog(get_main_ref_store(), fn, cb_data);
2026 int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
2027 const char *refname,
2028 each_reflog_ent_fn fn,
2031 return refs->be->for_each_reflog_ent_reverse(refs, refname,
2035 int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
2038 return refs_for_each_reflog_ent_reverse(get_main_ref_store(),
2039 refname, fn, cb_data);
2042 int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
2043 each_reflog_ent_fn fn, void *cb_data)
2045 return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
2048 int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,
2051 return refs_for_each_reflog_ent(get_main_ref_store(), refname,
2055 int refs_reflog_exists(struct ref_store *refs, const char *refname)
2057 return refs->be->reflog_exists(refs, refname);
2060 int reflog_exists(const char *refname)
2062 return refs_reflog_exists(get_main_ref_store(), refname);
2065 int refs_create_reflog(struct ref_store *refs, const char *refname,
2066 int force_create, struct strbuf *err)
2068 return refs->be->create_reflog(refs, refname, force_create, err);
2071 int safe_create_reflog(const char *refname, int force_create,
2074 return refs_create_reflog(get_main_ref_store(), refname,
2078 int refs_delete_reflog(struct ref_store *refs, const char *refname)
2080 return refs->be->delete_reflog(refs, refname);
2083 int delete_reflog(const char *refname)
2085 return refs_delete_reflog(get_main_ref_store(), refname);
2088 int refs_reflog_expire(struct ref_store *refs,
2089 const char *refname, const struct object_id *oid,
2091 reflog_expiry_prepare_fn prepare_fn,
2092 reflog_expiry_should_prune_fn should_prune_fn,
2093 reflog_expiry_cleanup_fn cleanup_fn,
2094 void *policy_cb_data)
2096 return refs->be->reflog_expire(refs, refname, oid, flags,
2097 prepare_fn, should_prune_fn,
2098 cleanup_fn, policy_cb_data);
2101 int reflog_expire(const char *refname, const struct object_id *oid,
2103 reflog_expiry_prepare_fn prepare_fn,
2104 reflog_expiry_should_prune_fn should_prune_fn,
2105 reflog_expiry_cleanup_fn cleanup_fn,
2106 void *policy_cb_data)
2108 return refs_reflog_expire(get_main_ref_store(),
2109 refname, oid, flags,
2110 prepare_fn, should_prune_fn,
2111 cleanup_fn, policy_cb_data);
2114 int initial_ref_transaction_commit(struct ref_transaction *transaction,
2117 struct ref_store *refs = transaction->ref_store;
2119 return refs->be->initial_transaction_commit(refs, transaction, err);
2122 int refs_delete_refs(struct ref_store *refs, const char *msg,
2123 struct string_list *refnames, unsigned int flags)
2125 return refs->be->delete_refs(refs, msg, refnames, flags);
2128 int delete_refs(const char *msg, struct string_list *refnames,
2131 return refs_delete_refs(get_main_ref_store(), msg, refnames, flags);
2134 int refs_rename_ref(struct ref_store *refs, const char *oldref,
2135 const char *newref, const char *logmsg)
2137 return refs->be->rename_ref(refs, oldref, newref, logmsg);
2140 int rename_ref(const char *oldref, const char *newref, const char *logmsg)
2142 return refs_rename_ref(get_main_ref_store(), oldref, newref, logmsg);
2145 int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
2146 const char *newref, const char *logmsg)
2148 return refs->be->copy_ref(refs, oldref, newref, logmsg);
2151 int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg)
2153 return refs_copy_existing_ref(get_main_ref_store(), oldref, newref, logmsg);