packed_ref_store: handle a packed-refs file that is a symlink
[git.git] / refs.c
blob5880c1237240c2f3bc86d9c937efc53a7605d9ec
1 /*
2 * The backend-independent part of the reference module.
3 */
5 #include "cache.h"
6 #include "hashmap.h"
7 #include "lockfile.h"
8 #include "iterator.h"
9 #include "refs.h"
10 #include "refs/refs-internal.h"
11 #include "object.h"
12 #include "tag.h"
13 #include "submodule.h"
14 #include "worktree.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))
26 return be;
27 return NULL;
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
38 * 1: End-of-component
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)
73 const char *cp;
74 char last = '\0';
76 for (cp = refname; ; cp++) {
77 int ch = *cp & 255;
78 unsigned char disp = refname_disposition[ch];
79 switch (disp) {
80 case 1:
81 goto out;
82 case 2:
83 if (last == '.')
84 return -1; /* Refname contains "..". */
85 break;
86 case 3:
87 if (last == '@')
88 return -1; /* Refname contains "@{". */
89 break;
90 case 4:
91 return -1;
92 case 5:
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;
101 break;
103 last = ch;
105 out:
106 if (cp == refname)
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". */
113 return cp - refname;
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 '@'. */
122 return -1;
124 while (1) {
125 /* We are at the start of a path component. */
126 component_len = check_refname_component(refname, &flags);
127 if (component_len <= 0)
128 return -1;
130 component_count++;
131 if (refname[component_len] == '\0')
132 break;
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. */
141 return 0;
144 int refname_is_safe(const char *refname)
146 const char *rest;
148 if (skip_prefix(refname, "refs/", &rest)) {
149 char *buf;
150 int result;
151 size_t restlen = strlen(rest);
153 /* rest must not be empty, or start or end with "/" */
154 if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
155 return 0;
158 * Does the refname try to escape refs/?
159 * For example: refs/foo/../bar is safe but refs/foo/../../bar
160 * is not.
162 buf = xmallocz(restlen);
163 result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
164 free(buf);
165 return result;
168 do {
169 if (!isupper(*refname) && *refname != '_')
170 return 0;
171 refname++;
172 } while (*refname);
173 return 1;
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,
183 unsigned int flags)
185 if (flags & REF_ISBROKEN)
186 return 0;
187 if (!has_sha1_file(oid->hash)) {
188 error("%s does not point to a valid object!", refname);
189 return 0;
191 return 1;
194 char *refs_resolve_refdup(struct ref_store *refs,
195 const char *refname, int resolve_flags,
196 unsigned char *sha1, int *flags)
198 const char *result;
200 result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
201 sha1, 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,
210 sha1, flags);
213 /* The argument to filter_refs */
214 struct ref_filter {
215 const char *pattern;
216 each_ref_fn *fn;
217 void *cb_data;
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))
224 return 0;
225 return -1;
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))
251 return 0;
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))
262 return PEEL_INVALID;
265 if (o->type != OBJ_TAG)
266 return PEEL_NON_TAG;
268 o = deref_tag_noverify(o);
269 if (!o)
270 return PEEL_INVALID;
272 hashcpy(sha1, o->oid.hash);
273 return PEEL_PEELED;
276 struct warn_if_dangling_data {
277 FILE *fp;
278 const char *refname;
279 const struct string_list *refnames;
280 const char *msg_fmt;
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))
291 return 0;
293 resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
294 if (!resolves_to
295 || (d->refname
296 ? strcmp(resolves_to, d->refname)
297 : !string_list_has_string(d->refnames, resolves_to))) {
298 return 0;
301 fprintf(d->fp, d->msg_fmt, refname);
302 fputc('\n', d->fp);
303 return 0;
306 void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
308 struct warn_if_dangling_data data;
310 data.fp = fp;
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;
321 data.fp = fp;
322 data.refname = NULL;
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),
341 fn, cb_data);
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),
357 fn, cb_data);
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),
373 fn, cb_data);
376 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
378 struct strbuf buf = STRBUF_INIT;
379 int ret = 0;
380 struct object_id oid;
381 int flag;
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);
388 return ret;
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;
396 int ret;
398 if (!prefix && !starts_with(pattern, "refs/"))
399 strbuf_addstr(&real_pattern, "refs/");
400 else if (prefix)
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;
412 filter.fn = fn;
413 filter.cb_data = cb_data;
414 ret = for_each_ref(filter_refs, &filter);
416 strbuf_release(&real_pattern);
417 return ret;
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))
430 ; /* nothing */
431 return name;
434 static const char *ref_rev_parse_rules[] = {
435 "%.*s",
436 "refs/%.*s",
437 "refs/tags/%.*s",
438 "refs/heads/%.*s",
439 "refs/remotes/%.*s",
440 "refs/remotes/%.*s/HEAD",
441 NULL
444 int refname_match(const char *abbrev_name, const char *full_name)
446 const char **p;
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))) {
451 return 1;
455 return 0;
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
461 * to name a branch.
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);
468 if (ret == *len) {
469 size_t size;
470 *string = strbuf_detach(&buf, &size);
471 *len = size;
472 return (char *)*string;
475 return NULL;
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);
482 free(last_branch);
483 return refs_found;
486 int expand_ref(const char *str, int len, unsigned char *sha1, char **ref)
488 const char **p, *r;
489 int refs_found = 0;
490 struct strbuf fullref = STRBUF_INIT;
492 *ref = NULL;
493 for (p = ref_rev_parse_rules; *p; p++) {
494 unsigned char sha1_from_ref[20];
495 unsigned char *this_result;
496 int flag;
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,
502 this_result, &flag);
503 if (r) {
504 if (!refs_found++)
505 *ref = xstrdup(r);
506 if (!warn_ambiguous_refs)
507 break;
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);
515 return refs_found;
518 int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
520 char *last_branch = substitute_branch_name(&str, &len);
521 const char **p;
522 int logs_found = 0;
523 struct strbuf path = STRBUF_INIT;
525 *log = NULL;
526 for (p = ref_rev_parse_rules; *p; p++) {
527 unsigned char hash[20];
528 const char *ref, *it;
530 strbuf_reset(&path);
531 strbuf_addf(&path, *p, len, str);
532 ref = resolve_ref_unsafe(path.buf, RESOLVE_REF_READING,
533 hash, NULL);
534 if (!ref)
535 continue;
536 if (reflog_exists(path.buf))
537 it = path.buf;
538 else if (strcmp(ref, path.buf) && reflog_exists(ref))
539 it = ref;
540 else
541 continue;
542 if (!logs_found++) {
543 *log = xstrdup(it);
544 hashcpy(sha1, hash);
546 if (!warn_ambiguous_refs)
547 break;
549 strbuf_release(&path);
550 free(last_branch);
551 return logs_found;
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)
562 const char *c;
564 for (c = refname; *c; c++) {
565 if (!isupper(*c) && *c != '-' && *c != '_')
566 return 0;
569 return 1;
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;
585 int fd;
586 static struct lock_file lock;
587 struct strbuf buf = STRBUF_INIT;
588 int ret = -1;
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);
594 if (fd < 0) {
595 strbuf_addf(err, "could not open '%s' for writing: %s",
596 filename, strerror(errno));
597 return -1;
600 if (old_sha1) {
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);
608 goto done;
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);
615 goto done;
618 commit_lock_file(&lock);
619 ret = 0;
620 done:
621 strbuf_release(&buf);
622 return ret;
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)) {
633 int fd;
634 unsigned char actual_old_sha1[20];
636 fd = hold_lock_file_for_update(&lock, filename,
637 LOCK_DIE_ON_ERROR);
638 if (fd < 0)
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);
645 return -1;
648 unlink(filename);
649 rollback_lock_file(&lock);
650 } else {
651 unlink(filename);
654 return 0;
657 int refs_delete_ref(struct ref_store *refs, const char *msg,
658 const char *refname,
659 const unsigned char *old_sha1,
660 unsigned int flags)
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);
671 if (!transaction ||
672 ref_transaction_delete(transaction, refname, old_sha1,
673 flags, msg, &err) ||
674 ref_transaction_commit(transaction, &err)) {
675 error("%s", err.buf);
676 ref_transaction_free(transaction);
677 strbuf_release(&err);
678 return 1;
680 ref_transaction_free(transaction);
681 strbuf_release(&err);
682 return 0;
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,
689 old_sha1, flags);
692 int copy_reflog_msg(char *buf, const char *msg)
694 char *cp = buf;
695 char c;
696 int wasspace = 1;
698 *cp++ = '\t';
699 while ((c = *msg++)) {
700 if (wasspace && isspace(c))
701 continue;
702 wasspace = isspace(c);
703 if (wasspace)
704 c = ' ';
705 *cp++ = c;
707 while (buf < cp && isspace(cp[-1]))
708 cp--;
709 *cp++ = '\n';
710 return cp - buf;
713 int should_autocreate_reflog(const char *refname)
715 switch (log_all_ref_updates) {
716 case LOG_REFS_ALWAYS:
717 return 1;
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");
723 default:
724 return 0;
728 int is_branch(const char *refname)
730 return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
733 struct read_ref_at_cb {
734 const char *refname;
735 timestamp_t at_time;
736 int cnt;
737 int reccnt;
738 unsigned char *sha1;
739 int found_it;
741 unsigned char osha1[20];
742 unsigned char nsha1[20];
743 int tz;
744 timestamp_t date;
745 char **msg;
746 timestamp_t *cutoff_time;
747 int *cutoff_tz;
748 int *cutoff_cnt;
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;
757 cb->reccnt++;
758 cb->tz = tz;
759 cb->date = timestamp;
761 if (timestamp <= cb->at_time || cb->cnt == 0) {
762 if (cb->msg)
763 *cb->msg = xstrdup(message);
764 if (cb->cutoff_time)
765 *cb->cutoff_time = timestamp;
766 if (cb->cutoff_tz)
767 *cb->cutoff_tz = tz;
768 if (cb->cutoff_cnt)
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);
788 cb->found_it = 1;
789 return 1;
791 hashcpy(cb->osha1, ooid->hash);
792 hashcpy(cb->nsha1, noid->hash);
793 if (cb->cnt > 0)
794 cb->cnt--;
795 return 0;
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;
804 if (cb->msg)
805 *cb->msg = xstrdup(message);
806 if (cb->cutoff_time)
807 *cb->cutoff_time = timestamp;
808 if (cb->cutoff_tz)
809 *cb->cutoff_tz = tz;
810 if (cb->cutoff_cnt)
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 */
816 return 1;
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;
828 cb.cnt = cnt;
829 cb.msg = msg;
830 cb.cutoff_time = cutoff_time;
831 cb.cutoff_tz = cutoff_tz;
832 cb.cutoff_cnt = cutoff_cnt;
833 cb.sha1 = sha1;
835 for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
837 if (!cb.reccnt) {
838 if (flags & GET_SHA1_QUIETLY)
839 exit(128);
840 else
841 die("Log for %s is empty.", refname);
843 if (cb.found_it)
844 return 0;
846 for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
848 return 1;
851 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
852 struct strbuf *err)
854 struct ref_transaction *tr;
855 assert(err);
857 tr = xcalloc(1, sizeof(struct ref_transaction));
858 tr->ref_store = refs;
859 return tr;
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)
869 size_t i;
871 if (!transaction)
872 return;
874 switch (transaction->state) {
875 case REF_TRANSACTION_OPEN:
876 case REF_TRANSACTION_CLOSED:
877 /* OK */
878 break;
879 case REF_TRANSACTION_PREPARED:
880 die("BUG: free called on a prepared reference transaction");
881 break;
882 default:
883 die("BUG: unexpected reference transaction state");
884 break;
887 for (i = 0; i < transaction->nr; i++) {
888 free(transaction->updates[i]->msg);
889 free(transaction->updates[i]);
891 free(transaction->updates);
892 free(transaction);
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,
900 const char *msg)
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);
921 return update;
924 int ref_transaction_update(struct ref_transaction *transaction,
925 const char *refname,
926 const unsigned char *new_sha1,
927 const unsigned char *old_sha1,
928 unsigned int flags, const char *msg,
929 struct strbuf *err)
931 assert(err);
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'",
937 refname);
938 return -1;
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);
945 return 0;
948 int ref_transaction_create(struct ref_transaction *transaction,
949 const char *refname,
950 const unsigned char *new_sha1,
951 unsigned int flags, const char *msg,
952 struct strbuf *err)
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,
961 const char *refname,
962 const unsigned char *old_sha1,
963 unsigned int flags, const char *msg,
964 struct strbuf *err)
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,
969 null_sha1, old_sha1,
970 flags, msg, err);
973 int ref_transaction_verify(struct ref_transaction *transaction,
974 const char *refname,
975 const unsigned char *old_sha1,
976 unsigned int flags,
977 struct strbuf *err)
979 if (!old_sha1)
980 die("BUG: verify called with old_sha1 set to NULL");
981 return ref_transaction_update(transaction, refname,
982 NULL, old_sha1,
983 flags, NULL, err);
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;
1001 int ret = 0;
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);
1006 } else {
1007 t = ref_store_transaction_begin(refs, &err);
1008 if (!t ||
1009 ref_transaction_update(t, refname, new_sha1, old_sha1,
1010 flags, msg, &err) ||
1011 ref_transaction_commit(t, &err)) {
1012 ret = 1;
1013 ref_transaction_free(t);
1016 if (ret) {
1017 const char *str = "update_ref failed for ref '%s': %s";
1019 switch (onerr) {
1020 case UPDATE_REFS_MSG_ON_ERR:
1021 error(str, refname, err.buf);
1022 break;
1023 case UPDATE_REFS_DIE_ON_ERR:
1024 die(str, refname, err.buf);
1025 break;
1026 case UPDATE_REFS_QUIET_ON_ERR:
1027 break;
1029 strbuf_release(&err);
1030 return 1;
1032 strbuf_release(&err);
1033 if (t)
1034 ref_transaction_free(t);
1035 return 0;
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)
1049 int i;
1050 static char **scanf_fmts;
1051 static int nr_rules;
1052 char *short_name;
1053 struct strbuf resolved_buf = STRBUF_INIT;
1055 if (!nr_rules) {
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;
1063 size_t offset = 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));
1072 offset = 0;
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 */
1082 if (!nr_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) {
1090 int j;
1091 int rules_to_fail = i;
1092 int short_name_len;
1094 if (1 != sscanf(refname, scanf_fmts[i], short_name))
1095 continue;
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
1103 if (strict)
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 */
1114 if (i == j)
1115 continue;
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))
1126 break;
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);
1135 return short_name;
1139 strbuf_release(&resolved_buf);
1140 free(short_name);
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)
1148 const char *key;
1149 if (!strcmp("transfer.hiderefs", var) ||
1150 (!parse_config_key(var, section, NULL, NULL, &key) &&
1151 !strcmp(key, "hiderefs"))) {
1152 char *ref;
1153 int len;
1155 if (!value)
1156 return config_error_nonbool(var);
1157 ref = xstrdup(value);
1158 len = strlen(ref);
1159 while (len && ref[len - 1] == '/')
1160 ref[--len] = '\0';
1161 if (!hide_refs) {
1162 hide_refs = xcalloc(1, sizeof(*hide_refs));
1163 hide_refs->strdup_strings = 1;
1165 string_list_append(hide_refs, ref);
1167 return 0;
1170 int ref_is_hidden(const char *refname, const char *refname_full)
1172 int i;
1174 if (!hide_refs)
1175 return 0;
1176 for (i = hide_refs->nr - 1; i >= 0; i--) {
1177 const char *match = hide_refs->items[i].string;
1178 const char *subject;
1179 int neg = 0;
1180 int len;
1182 if (*match == '!') {
1183 neg = 1;
1184 match++;
1187 if (*match == '^') {
1188 subject = refname_full;
1189 match++;
1190 } else {
1191 subject = refname;
1194 /* refname can be NULL when namespaces are used. */
1195 if (!subject || !starts_with(subject, match))
1196 continue;
1197 len = strlen(match);
1198 if (!subject[len] || subject[len] == '/')
1199 return !neg;
1201 return 0;
1204 const char *find_descendant_ref(const char *dirname,
1205 const struct string_list *extras,
1206 const struct string_list *skip)
1208 int pos;
1210 if (!extras)
1211 return NULL;
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))
1224 break;
1226 if (!skip || !string_list_has_string(skip, extra_refname))
1227 return extra_refname;
1229 return NULL;
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;
1238 int ok;
1240 string_list_insert(&skip, old_refname);
1241 ok = !refs_verify_refname_available(refs, new_refname,
1242 NULL, &skip, &err);
1243 if (!ok)
1244 error("%s", err.buf);
1246 string_list_clear(&skip, 0);
1247 strbuf_release(&err);
1248 return ok;
1251 int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1253 struct object_id oid;
1254 int flag;
1256 if (submodule) {
1257 if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
1258 return fn("HEAD", &oid, 0, cb_data);
1260 return 0;
1263 if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
1264 return fn("HEAD", &oid, flag, cb_data);
1266 return 0;
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);
1282 if (ref_paranoia)
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:
1291 if (trim)
1292 iter = prefix_ref_iterator_begin(iter, "", trim);
1294 return iter;
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;
1311 if (!refs)
1312 return 0;
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;
1349 if (broken)
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;
1368 if (broken)
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),
1379 0, cb_data);
1382 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1384 struct strbuf buf = STRBUF_INIT;
1385 int ret;
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);
1390 return ret;
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,
1414 int resolve_flags,
1415 unsigned char *sha1, int *flags)
1417 static struct strbuf sb_refname = STRBUF_INIT;
1418 int unused_flags;
1419 int symref_count;
1421 if (!flags)
1422 flags = &unused_flags;
1424 *flags = 0;
1426 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1427 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1428 !refname_is_safe(refname)) {
1429 errno = EINVAL;
1430 return NULL;
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
1439 * REF_ISBROKEN yet.
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))
1451 return NULL;
1452 hashclr(sha1);
1453 if (*flags & REF_BAD_NAME)
1454 *flags |= REF_ISBROKEN;
1455 return refname;
1458 *flags |= read_flags;
1460 if (!(read_flags & REF_ISSYMREF)) {
1461 if (*flags & REF_BAD_NAME) {
1462 hashclr(sha1);
1463 *flags |= REF_ISBROKEN;
1465 return refname;
1468 refname = sb_refname.buf;
1469 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1470 hashclr(sha1);
1471 return refname;
1473 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1474 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1475 !refname_is_safe(refname)) {
1476 errno = EINVAL;
1477 return NULL;
1480 *flags |= REF_ISBROKEN | REF_BAD_NAME;
1484 errno = ELOOP;
1485 return NULL;
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;
1508 int flags;
1510 while (len && submodule[len - 1] == '/')
1511 len--;
1513 if (!len)
1514 return -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);
1521 free(stripped);
1522 } else {
1523 refs = get_submodule_ref_store(submodule);
1526 if (!refs)
1527 return -1;
1529 if (!refs_resolve_ref_unsafe(refs, refname, 0, sha1, &flags) ||
1530 is_null_sha1(sha1))
1531 return -1;
1532 return 0;
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));
1561 entry->refs = refs;
1562 return entry;
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,
1579 const char *name)
1581 struct ref_store_hash_entry *entry;
1583 if (!map->tablesize)
1584 /* It's initialized on demand in register_ref_store(). */
1585 return NULL;
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
1593 * gitdir.
1595 static struct ref_store *ref_store_init(const char *gitdir,
1596 unsigned int flags)
1598 const char *be_name = "files";
1599 struct ref_storage_be *be = find_ref_storage_backend(be_name);
1600 struct ref_store *refs;
1602 if (!be)
1603 die("BUG: reference backend %s is unknown", be_name);
1605 refs = be->init(gitdir, flags);
1606 return refs;
1609 struct ref_store *get_main_ref_store(void)
1611 if (main_ref_store)
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,
1623 const char *type,
1624 struct ref_store *refs,
1625 const char *name)
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;
1638 int ret;
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);
1649 if (refs)
1650 return refs;
1652 strbuf_addstr(&submodule_sb, submodule);
1653 ret = is_nonbare_repository_dir(&submodule_sb);
1654 strbuf_release(&submodule_sb);
1655 if (!ret)
1656 return NULL;
1658 ret = submodule_to_gitdir(&submodule_sb, submodule);
1659 if (ret) {
1660 strbuf_release(&submodule_sb);
1661 return NULL;
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",
1668 refs, submodule);
1670 strbuf_release(&submodule_sb);
1671 return refs;
1674 struct ref_store *get_worktree_ref_store(const struct worktree *wt)
1676 struct ref_store *refs;
1677 const char *id;
1679 if (wt->is_current)
1680 return get_main_ref_store();
1682 id = wt->id ? wt->id : "/";
1683 refs = lookup_ref_store_map(&worktree_ref_stores, id);
1684 if (refs)
1685 return refs;
1687 if (wt->id)
1688 refs = ref_store_init(git_common_path("worktrees/%s", wt->id),
1689 REF_STORE_ALL_CAPS);
1690 else
1691 refs = ref_store_init(get_git_common_dir(),
1692 REF_STORE_ALL_CAPS);
1694 if (refs)
1695 register_ref_store_map(&worktree_ref_stores, "worktree",
1696 refs, id);
1697 return refs;
1700 void base_ref_store_init(struct ref_store *refs,
1701 const struct ref_storage_be *be)
1703 refs->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,
1726 const char *logmsg)
1728 return refs->be->create_symref(refs, ref_target,
1729 refs_heads_master,
1730 logmsg);
1733 int create_symref(const char *ref_target, const char *refs_heads_master,
1734 const char *logmsg)
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,
1741 struct strbuf *err)
1743 size_t i, n = refnames->nr;
1745 assert(err);
1747 for (i = 1; i < n; i++) {
1748 int cmp = strcmp(refnames->items[i - 1].string,
1749 refnames->items[i].string);
1751 if (!cmp) {
1752 strbuf_addf(err,
1753 "multiple updates for ref '%s' not allowed.",
1754 refnames->items[i].string);
1755 return 1;
1756 } else if (cmp > 0) {
1757 die("BUG: ref_update_reject_duplicates() received unsorted list");
1760 return 0;
1763 int ref_transaction_prepare(struct ref_transaction *transaction,
1764 struct strbuf *err)
1766 struct ref_store *refs = transaction->ref_store;
1768 switch (transaction->state) {
1769 case REF_TRANSACTION_OPEN:
1770 /* Good. */
1771 break;
1772 case REF_TRANSACTION_PREPARED:
1773 die("BUG: prepare called twice on reference transaction");
1774 break;
1775 case REF_TRANSACTION_CLOSED:
1776 die("BUG: prepare called on a closed reference transaction");
1777 break;
1778 default:
1779 die("BUG: unexpected reference transaction state");
1780 break;
1783 if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
1784 strbuf_addstr(err,
1785 _("ref updates forbidden inside quarantine environment"));
1786 return -1;
1789 return refs->be->transaction_prepare(refs, transaction, err);
1792 int ref_transaction_abort(struct ref_transaction *transaction,
1793 struct strbuf *err)
1795 struct ref_store *refs = transaction->ref_store;
1796 int ret = 0;
1798 switch (transaction->state) {
1799 case REF_TRANSACTION_OPEN:
1800 /* No need to abort explicitly. */
1801 break;
1802 case REF_TRANSACTION_PREPARED:
1803 ret = refs->be->transaction_abort(refs, transaction, err);
1804 break;
1805 case REF_TRANSACTION_CLOSED:
1806 die("BUG: abort called on a closed reference transaction");
1807 break;
1808 default:
1809 die("BUG: unexpected reference transaction state");
1810 break;
1813 ref_transaction_free(transaction);
1814 return ret;
1817 int ref_transaction_commit(struct ref_transaction *transaction,
1818 struct strbuf *err)
1820 struct ref_store *refs = transaction->ref_store;
1821 int ret;
1823 switch (transaction->state) {
1824 case REF_TRANSACTION_OPEN:
1825 /* Need to prepare first. */
1826 ret = ref_transaction_prepare(transaction, err);
1827 if (ret)
1828 return ret;
1829 break;
1830 case REF_TRANSACTION_PREPARED:
1831 /* Fall through to finish. */
1832 break;
1833 case REF_TRANSACTION_CLOSED:
1834 die("BUG: commit called on a closed reference transaction");
1835 break;
1836 default:
1837 die("BUG: unexpected reference transaction state");
1838 break;
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,
1848 struct strbuf *err)
1850 const char *slash;
1851 const char *extra_refname;
1852 struct strbuf dirname = STRBUF_INIT;
1853 struct strbuf referent = STRBUF_INIT;
1854 struct object_id oid;
1855 unsigned int type;
1856 struct ref_iterator *iter;
1857 int ok;
1858 int ret = -1;
1861 * For the sake of comments in this function, suppose that
1862 * refname is "refs/foo/bar".
1865 assert(err);
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))
1878 continue;
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);
1883 goto cleanup;
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);
1889 goto cleanup;
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
1899 * *do* conflict.
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) {
1907 if (skip &&
1908 string_list_has_string(skip, iter->refname))
1909 continue;
1911 strbuf_addf(err, "'%s' exists; cannot create '%s'",
1912 iter->refname, refname);
1913 ref_iterator_abort(iter);
1914 goto cleanup;
1917 if (ok != ITER_DONE)
1918 die("BUG: error while iterating over references");
1920 extra_refname = find_descendant_ref(dirname.buf, extras, skip);
1921 if (extra_refname)
1922 strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
1923 refname, extra_refname);
1924 else
1925 ret = 0;
1927 cleanup:
1928 strbuf_release(&referent);
1929 strbuf_release(&dirname);
1930 return ret;
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,
1950 void *cb_data)
1952 return refs->be->for_each_reflog_ent_reverse(refs, refname,
1953 fn, cb_data);
1956 int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
1957 void *cb_data)
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,
1970 void *cb_data)
1972 return refs_for_each_reflog_ent(get_main_ref_store(), refname,
1973 fn, cb_data);
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,
1993 struct strbuf *err)
1995 return refs_create_reflog(get_main_ref_store(), refname,
1996 force_create, err);
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,
2011 unsigned int flags,
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,
2023 unsigned int flags,
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,
2036 struct strbuf *err)
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,
2050 unsigned int flags)
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);