worktree: move subcommand
[git] / refs.c
1 /*
2  * The backend-independent part of the reference module.
3  */
4
5 #include "cache.h"
6 #include "hashmap.h"
7 #include "lockfile.h"
8 #include "refs.h"
9 #include "refs/refs-internal.h"
10 #include "object.h"
11 #include "tag.h"
12 #include "submodule.h"
13
14 /*
15  * List of all available backends
16  */
17 static struct ref_storage_be *refs_backends = &refs_be_files;
18
19 static struct ref_storage_be *find_ref_storage_backend(const char *name)
20 {
21         struct ref_storage_be *be;
22         for (be = refs_backends; be; be = be->next)
23                 if (!strcmp(be->name, name))
24                         return be;
25         return NULL;
26 }
27
28 int ref_storage_backend_exists(const char *name)
29 {
30         return find_ref_storage_backend(name) != NULL;
31 }
32
33 /*
34  * How to handle various characters in refnames:
35  * 0: An acceptable character for refs
36  * 1: End-of-component
37  * 2: ., look for a preceding . to reject .. in refs
38  * 3: {, look for a preceding @ to reject @{ in refs
39  * 4: A bad character: ASCII control characters, and
40  *    ":", "?", "[", "\", "^", "~", SP, or TAB
41  * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
42  */
43 static unsigned char refname_disposition[256] = {
44         1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
45         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
46         4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
47         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
48         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
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, 3, 0, 0, 4, 4
52 };
53
54 /*
55  * Try to read one refname component from the front of refname.
56  * Return the length of the component found, or -1 if the component is
57  * not legal.  It is legal if it is something reasonable to have under
58  * ".git/refs/"; We do not like it if:
59  *
60  * - any path component of it begins with ".", or
61  * - it has double dots "..", or
62  * - it has ASCII control characters, or
63  * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
64  * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
65  * - it ends with a "/", or
66  * - it ends with ".lock", or
67  * - it contains a "@{" portion
68  */
69 static int check_refname_component(const char *refname, int *flags)
70 {
71         const char *cp;
72         char last = '\0';
73
74         for (cp = refname; ; cp++) {
75                 int ch = *cp & 255;
76                 unsigned char disp = refname_disposition[ch];
77                 switch (disp) {
78                 case 1:
79                         goto out;
80                 case 2:
81                         if (last == '.')
82                                 return -1; /* Refname contains "..". */
83                         break;
84                 case 3:
85                         if (last == '@')
86                                 return -1; /* Refname contains "@{". */
87                         break;
88                 case 4:
89                         return -1;
90                 case 5:
91                         if (!(*flags & REFNAME_REFSPEC_PATTERN))
92                                 return -1; /* refspec can't be a pattern */
93
94                         /*
95                          * Unset the pattern flag so that we only accept
96                          * a single asterisk for one side of refspec.
97                          */
98                         *flags &= ~ REFNAME_REFSPEC_PATTERN;
99                         break;
100                 }
101                 last = ch;
102         }
103 out:
104         if (cp == refname)
105                 return 0; /* Component has zero length. */
106         if (refname[0] == '.')
107                 return -1; /* Component starts with '.'. */
108         if (cp - refname >= LOCK_SUFFIX_LEN &&
109             !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
110                 return -1; /* Refname ends with ".lock". */
111         return cp - refname;
112 }
113
114 int check_refname_format(const char *refname, int flags)
115 {
116         int component_len, component_count = 0;
117
118         if (!strcmp(refname, "@"))
119                 /* Refname is a single character '@'. */
120                 return -1;
121
122         while (1) {
123                 /* We are at the start of a path component. */
124                 component_len = check_refname_component(refname, &flags);
125                 if (component_len <= 0)
126                         return -1;
127
128                 component_count++;
129                 if (refname[component_len] == '\0')
130                         break;
131                 /* Skip to next component. */
132                 refname += component_len + 1;
133         }
134
135         if (refname[component_len - 1] == '.')
136                 return -1; /* Refname ends with '.'. */
137         if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
138                 return -1; /* Refname has only one component. */
139         return 0;
140 }
141
142 int refname_is_safe(const char *refname)
143 {
144         const char *rest;
145
146         if (skip_prefix(refname, "refs/", &rest)) {
147                 char *buf;
148                 int result;
149                 size_t restlen = strlen(rest);
150
151                 /* rest must not be empty, or start or end with "/" */
152                 if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
153                         return 0;
154
155                 /*
156                  * Does the refname try to escape refs/?
157                  * For example: refs/foo/../bar is safe but refs/foo/../../bar
158                  * is not.
159                  */
160                 buf = xmallocz(restlen);
161                 result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
162                 free(buf);
163                 return result;
164         }
165
166         do {
167                 if (!isupper(*refname) && *refname != '_')
168                         return 0;
169                 refname++;
170         } while (*refname);
171         return 1;
172 }
173
174 char *refs_resolve_refdup(struct ref_store *refs,
175                           const char *refname, int resolve_flags,
176                           unsigned char *sha1, int *flags)
177 {
178         const char *result;
179
180         result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
181                                          sha1, flags);
182         return xstrdup_or_null(result);
183 }
184
185 char *resolve_refdup(const char *refname, int resolve_flags,
186                      unsigned char *sha1, int *flags)
187 {
188         return refs_resolve_refdup(get_main_ref_store(),
189                                    refname, resolve_flags,
190                                    sha1, flags);
191 }
192
193 /* The argument to filter_refs */
194 struct ref_filter {
195         const char *pattern;
196         each_ref_fn *fn;
197         void *cb_data;
198 };
199
200 int refs_read_ref_full(struct ref_store *refs, const char *refname,
201                        int resolve_flags, unsigned char *sha1, int *flags)
202 {
203         if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, sha1, flags))
204                 return 0;
205         return -1;
206 }
207
208 int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
209 {
210         return refs_read_ref_full(get_main_ref_store(), refname,
211                                   resolve_flags, sha1, flags);
212 }
213
214 int read_ref(const char *refname, unsigned char *sha1)
215 {
216         return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL);
217 }
218
219 int ref_exists(const char *refname)
220 {
221         unsigned char sha1[20];
222         return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
223 }
224
225 static int filter_refs(const char *refname, const struct object_id *oid,
226                            int flags, void *data)
227 {
228         struct ref_filter *filter = (struct ref_filter *)data;
229
230         if (wildmatch(filter->pattern, refname, 0, NULL))
231                 return 0;
232         return filter->fn(refname, oid, flags, filter->cb_data);
233 }
234
235 enum peel_status peel_object(const unsigned char *name, unsigned char *sha1)
236 {
237         struct object *o = lookup_unknown_object(name);
238
239         if (o->type == OBJ_NONE) {
240                 int type = sha1_object_info(name, NULL);
241                 if (type < 0 || !object_as_type(o, type, 0))
242                         return PEEL_INVALID;
243         }
244
245         if (o->type != OBJ_TAG)
246                 return PEEL_NON_TAG;
247
248         o = deref_tag_noverify(o);
249         if (!o)
250                 return PEEL_INVALID;
251
252         hashcpy(sha1, o->oid.hash);
253         return PEEL_PEELED;
254 }
255
256 struct warn_if_dangling_data {
257         FILE *fp;
258         const char *refname;
259         const struct string_list *refnames;
260         const char *msg_fmt;
261 };
262
263 static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
264                                    int flags, void *cb_data)
265 {
266         struct warn_if_dangling_data *d = cb_data;
267         const char *resolves_to;
268         struct object_id junk;
269
270         if (!(flags & REF_ISSYMREF))
271                 return 0;
272
273         resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
274         if (!resolves_to
275             || (d->refname
276                 ? strcmp(resolves_to, d->refname)
277                 : !string_list_has_string(d->refnames, resolves_to))) {
278                 return 0;
279         }
280
281         fprintf(d->fp, d->msg_fmt, refname);
282         fputc('\n', d->fp);
283         return 0;
284 }
285
286 void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
287 {
288         struct warn_if_dangling_data data;
289
290         data.fp = fp;
291         data.refname = refname;
292         data.refnames = NULL;
293         data.msg_fmt = msg_fmt;
294         for_each_rawref(warn_if_dangling_symref, &data);
295 }
296
297 void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
298 {
299         struct warn_if_dangling_data data;
300
301         data.fp = fp;
302         data.refname = NULL;
303         data.refnames = refnames;
304         data.msg_fmt = msg_fmt;
305         for_each_rawref(warn_if_dangling_symref, &data);
306 }
307
308 int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
309 {
310         return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data);
311 }
312
313 int for_each_tag_ref(each_ref_fn fn, void *cb_data)
314 {
315         return refs_for_each_tag_ref(get_main_ref_store(), fn, cb_data);
316 }
317
318 int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
319 {
320         return refs_for_each_tag_ref(get_submodule_ref_store(submodule),
321                                      fn, cb_data);
322 }
323
324 int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
325 {
326         return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data);
327 }
328
329 int for_each_branch_ref(each_ref_fn fn, void *cb_data)
330 {
331         return refs_for_each_branch_ref(get_main_ref_store(), fn, cb_data);
332 }
333
334 int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
335 {
336         return refs_for_each_branch_ref(get_submodule_ref_store(submodule),
337                                         fn, cb_data);
338 }
339
340 int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
341 {
342         return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data);
343 }
344
345 int for_each_remote_ref(each_ref_fn fn, void *cb_data)
346 {
347         return refs_for_each_remote_ref(get_main_ref_store(), fn, cb_data);
348 }
349
350 int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
351 {
352         return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
353                                         fn, cb_data);
354 }
355
356 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
357 {
358         struct strbuf buf = STRBUF_INIT;
359         int ret = 0;
360         struct object_id oid;
361         int flag;
362
363         strbuf_addf(&buf, "%sHEAD", get_git_namespace());
364         if (!read_ref_full(buf.buf, RESOLVE_REF_READING, oid.hash, &flag))
365                 ret = fn(buf.buf, &oid, flag, cb_data);
366         strbuf_release(&buf);
367
368         return ret;
369 }
370
371 int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
372         const char *prefix, void *cb_data)
373 {
374         struct strbuf real_pattern = STRBUF_INIT;
375         struct ref_filter filter;
376         int ret;
377
378         if (!prefix && !starts_with(pattern, "refs/"))
379                 strbuf_addstr(&real_pattern, "refs/");
380         else if (prefix)
381                 strbuf_addstr(&real_pattern, prefix);
382         strbuf_addstr(&real_pattern, pattern);
383
384         if (!has_glob_specials(pattern)) {
385                 /* Append implied '/' '*' if not present. */
386                 strbuf_complete(&real_pattern, '/');
387                 /* No need to check for '*', there is none. */
388                 strbuf_addch(&real_pattern, '*');
389         }
390
391         filter.pattern = real_pattern.buf;
392         filter.fn = fn;
393         filter.cb_data = cb_data;
394         ret = for_each_ref(filter_refs, &filter);
395
396         strbuf_release(&real_pattern);
397         return ret;
398 }
399
400 int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
401 {
402         return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
403 }
404
405 const char *prettify_refname(const char *name)
406 {
407         if (skip_prefix(name, "refs/heads/", &name) ||
408             skip_prefix(name, "refs/tags/", &name) ||
409             skip_prefix(name, "refs/remotes/", &name))
410                 ; /* nothing */
411         return name;
412 }
413
414 static const char *ref_rev_parse_rules[] = {
415         "%.*s",
416         "refs/%.*s",
417         "refs/tags/%.*s",
418         "refs/heads/%.*s",
419         "refs/remotes/%.*s",
420         "refs/remotes/%.*s/HEAD",
421         NULL
422 };
423
424 int refname_match(const char *abbrev_name, const char *full_name)
425 {
426         const char **p;
427         const int abbrev_name_len = strlen(abbrev_name);
428
429         for (p = ref_rev_parse_rules; *p; p++) {
430                 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
431                         return 1;
432                 }
433         }
434
435         return 0;
436 }
437
438 /*
439  * *string and *len will only be substituted, and *string returned (for
440  * later free()ing) if the string passed in is a magic short-hand form
441  * to name a branch.
442  */
443 static char *substitute_branch_name(const char **string, int *len)
444 {
445         struct strbuf buf = STRBUF_INIT;
446         int ret = interpret_branch_name(*string, *len, &buf, 0);
447
448         if (ret == *len) {
449                 size_t size;
450                 *string = strbuf_detach(&buf, &size);
451                 *len = size;
452                 return (char *)*string;
453         }
454
455         return NULL;
456 }
457
458 int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
459 {
460         char *last_branch = substitute_branch_name(&str, &len);
461         int   refs_found  = expand_ref(str, len, sha1, ref);
462         free(last_branch);
463         return refs_found;
464 }
465
466 int expand_ref(const char *str, int len, unsigned char *sha1, char **ref)
467 {
468         const char **p, *r;
469         int refs_found = 0;
470         struct strbuf fullref = STRBUF_INIT;
471
472         *ref = NULL;
473         for (p = ref_rev_parse_rules; *p; p++) {
474                 unsigned char sha1_from_ref[20];
475                 unsigned char *this_result;
476                 int flag;
477
478                 this_result = refs_found ? sha1_from_ref : sha1;
479                 strbuf_reset(&fullref);
480                 strbuf_addf(&fullref, *p, len, str);
481                 r = resolve_ref_unsafe(fullref.buf, RESOLVE_REF_READING,
482                                        this_result, &flag);
483                 if (r) {
484                         if (!refs_found++)
485                                 *ref = xstrdup(r);
486                         if (!warn_ambiguous_refs)
487                                 break;
488                 } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
489                         warning("ignoring dangling symref %s.", fullref.buf);
490                 } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) {
491                         warning("ignoring broken ref %s.", fullref.buf);
492                 }
493         }
494         strbuf_release(&fullref);
495         return refs_found;
496 }
497
498 int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
499 {
500         char *last_branch = substitute_branch_name(&str, &len);
501         const char **p;
502         int logs_found = 0;
503         struct strbuf path = STRBUF_INIT;
504
505         *log = NULL;
506         for (p = ref_rev_parse_rules; *p; p++) {
507                 unsigned char hash[20];
508                 const char *ref, *it;
509
510                 strbuf_reset(&path);
511                 strbuf_addf(&path, *p, len, str);
512                 ref = resolve_ref_unsafe(path.buf, RESOLVE_REF_READING,
513                                          hash, NULL);
514                 if (!ref)
515                         continue;
516                 if (reflog_exists(path.buf))
517                         it = path.buf;
518                 else if (strcmp(ref, path.buf) && reflog_exists(ref))
519                         it = ref;
520                 else
521                         continue;
522                 if (!logs_found++) {
523                         *log = xstrdup(it);
524                         hashcpy(sha1, hash);
525                 }
526                 if (!warn_ambiguous_refs)
527                         break;
528         }
529         strbuf_release(&path);
530         free(last_branch);
531         return logs_found;
532 }
533
534 static int is_per_worktree_ref(const char *refname)
535 {
536         return !strcmp(refname, "HEAD") ||
537                 starts_with(refname, "refs/bisect/");
538 }
539
540 static int is_pseudoref_syntax(const char *refname)
541 {
542         const char *c;
543
544         for (c = refname; *c; c++) {
545                 if (!isupper(*c) && *c != '-' && *c != '_')
546                         return 0;
547         }
548
549         return 1;
550 }
551
552 enum ref_type ref_type(const char *refname)
553 {
554         if (is_per_worktree_ref(refname))
555                 return REF_TYPE_PER_WORKTREE;
556         if (is_pseudoref_syntax(refname))
557                 return REF_TYPE_PSEUDOREF;
558        return REF_TYPE_NORMAL;
559 }
560
561 static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
562                            const unsigned char *old_sha1, struct strbuf *err)
563 {
564         const char *filename;
565         int fd;
566         static struct lock_file lock;
567         struct strbuf buf = STRBUF_INIT;
568         int ret = -1;
569
570         strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
571
572         filename = git_path("%s", pseudoref);
573         fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
574         if (fd < 0) {
575                 strbuf_addf(err, "could not open '%s' for writing: %s",
576                             filename, strerror(errno));
577                 return -1;
578         }
579
580         if (old_sha1) {
581                 unsigned char actual_old_sha1[20];
582
583                 if (read_ref(pseudoref, actual_old_sha1))
584                         die("could not read ref '%s'", pseudoref);
585                 if (hashcmp(actual_old_sha1, old_sha1)) {
586                         strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref);
587                         rollback_lock_file(&lock);
588                         goto done;
589                 }
590         }
591
592         if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
593                 strbuf_addf(err, "could not write to '%s'", filename);
594                 rollback_lock_file(&lock);
595                 goto done;
596         }
597
598         commit_lock_file(&lock);
599         ret = 0;
600 done:
601         strbuf_release(&buf);
602         return ret;
603 }
604
605 static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1)
606 {
607         static struct lock_file lock;
608         const char *filename;
609
610         filename = git_path("%s", pseudoref);
611
612         if (old_sha1 && !is_null_sha1(old_sha1)) {
613                 int fd;
614                 unsigned char actual_old_sha1[20];
615
616                 fd = hold_lock_file_for_update(&lock, filename,
617                                                LOCK_DIE_ON_ERROR);
618                 if (fd < 0)
619                         die_errno(_("Could not open '%s' for writing"), filename);
620                 if (read_ref(pseudoref, actual_old_sha1))
621                         die("could not read ref '%s'", pseudoref);
622                 if (hashcmp(actual_old_sha1, old_sha1)) {
623                         warning("Unexpected sha1 when deleting %s", pseudoref);
624                         rollback_lock_file(&lock);
625                         return -1;
626                 }
627
628                 unlink(filename);
629                 rollback_lock_file(&lock);
630         } else {
631                 unlink(filename);
632         }
633
634         return 0;
635 }
636
637 int refs_delete_ref(struct ref_store *refs, const char *msg,
638                     const char *refname,
639                     const unsigned char *old_sha1,
640                     unsigned int flags)
641 {
642         struct ref_transaction *transaction;
643         struct strbuf err = STRBUF_INIT;
644
645         if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
646                 assert(refs == get_main_ref_store());
647                 return delete_pseudoref(refname, old_sha1);
648         }
649
650         transaction = ref_store_transaction_begin(refs, &err);
651         if (!transaction ||
652             ref_transaction_delete(transaction, refname, old_sha1,
653                                    flags, msg, &err) ||
654             ref_transaction_commit(transaction, &err)) {
655                 error("%s", err.buf);
656                 ref_transaction_free(transaction);
657                 strbuf_release(&err);
658                 return 1;
659         }
660         ref_transaction_free(transaction);
661         strbuf_release(&err);
662         return 0;
663 }
664
665 int delete_ref(const char *msg, const char *refname,
666                const unsigned char *old_sha1, unsigned int flags)
667 {
668         return refs_delete_ref(get_main_ref_store(), msg, refname,
669                                old_sha1, flags);
670 }
671
672 int copy_reflog_msg(char *buf, const char *msg)
673 {
674         char *cp = buf;
675         char c;
676         int wasspace = 1;
677
678         *cp++ = '\t';
679         while ((c = *msg++)) {
680                 if (wasspace && isspace(c))
681                         continue;
682                 wasspace = isspace(c);
683                 if (wasspace)
684                         c = ' ';
685                 *cp++ = c;
686         }
687         while (buf < cp && isspace(cp[-1]))
688                 cp--;
689         *cp++ = '\n';
690         return cp - buf;
691 }
692
693 int should_autocreate_reflog(const char *refname)
694 {
695         switch (log_all_ref_updates) {
696         case LOG_REFS_ALWAYS:
697                 return 1;
698         case LOG_REFS_NORMAL:
699                 return starts_with(refname, "refs/heads/") ||
700                         starts_with(refname, "refs/remotes/") ||
701                         starts_with(refname, "refs/notes/") ||
702                         !strcmp(refname, "HEAD");
703         default:
704                 return 0;
705         }
706 }
707
708 int is_branch(const char *refname)
709 {
710         return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
711 }
712
713 struct read_ref_at_cb {
714         const char *refname;
715         unsigned long at_time;
716         int cnt;
717         int reccnt;
718         unsigned char *sha1;
719         int found_it;
720
721         unsigned char osha1[20];
722         unsigned char nsha1[20];
723         int tz;
724         unsigned long date;
725         char **msg;
726         unsigned long *cutoff_time;
727         int *cutoff_tz;
728         int *cutoff_cnt;
729 };
730
731 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
732                 const char *email, unsigned long timestamp, int tz,
733                 const char *message, void *cb_data)
734 {
735         struct read_ref_at_cb *cb = cb_data;
736
737         cb->reccnt++;
738         cb->tz = tz;
739         cb->date = timestamp;
740
741         if (timestamp <= cb->at_time || cb->cnt == 0) {
742                 if (cb->msg)
743                         *cb->msg = xstrdup(message);
744                 if (cb->cutoff_time)
745                         *cb->cutoff_time = timestamp;
746                 if (cb->cutoff_tz)
747                         *cb->cutoff_tz = tz;
748                 if (cb->cutoff_cnt)
749                         *cb->cutoff_cnt = cb->reccnt - 1;
750                 /*
751                  * we have not yet updated cb->[n|o]sha1 so they still
752                  * hold the values for the previous record.
753                  */
754                 if (!is_null_sha1(cb->osha1)) {
755                         hashcpy(cb->sha1, noid->hash);
756                         if (hashcmp(cb->osha1, noid->hash))
757                                 warning("Log for ref %s has gap after %s.",
758                                         cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
759                 }
760                 else if (cb->date == cb->at_time)
761                         hashcpy(cb->sha1, noid->hash);
762                 else if (hashcmp(noid->hash, cb->sha1))
763                         warning("Log for ref %s unexpectedly ended on %s.",
764                                 cb->refname, show_date(cb->date, cb->tz,
765                                                        DATE_MODE(RFC2822)));
766                 hashcpy(cb->osha1, ooid->hash);
767                 hashcpy(cb->nsha1, noid->hash);
768                 cb->found_it = 1;
769                 return 1;
770         }
771         hashcpy(cb->osha1, ooid->hash);
772         hashcpy(cb->nsha1, noid->hash);
773         if (cb->cnt > 0)
774                 cb->cnt--;
775         return 0;
776 }
777
778 static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
779                                   const char *email, unsigned long timestamp,
780                                   int tz, const char *message, void *cb_data)
781 {
782         struct read_ref_at_cb *cb = cb_data;
783
784         if (cb->msg)
785                 *cb->msg = xstrdup(message);
786         if (cb->cutoff_time)
787                 *cb->cutoff_time = timestamp;
788         if (cb->cutoff_tz)
789                 *cb->cutoff_tz = tz;
790         if (cb->cutoff_cnt)
791                 *cb->cutoff_cnt = cb->reccnt;
792         hashcpy(cb->sha1, ooid->hash);
793         if (is_null_sha1(cb->sha1))
794                 hashcpy(cb->sha1, noid->hash);
795         /* We just want the first entry */
796         return 1;
797 }
798
799 int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt,
800                 unsigned char *sha1, char **msg,
801                 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
802 {
803         struct read_ref_at_cb cb;
804
805         memset(&cb, 0, sizeof(cb));
806         cb.refname = refname;
807         cb.at_time = at_time;
808         cb.cnt = cnt;
809         cb.msg = msg;
810         cb.cutoff_time = cutoff_time;
811         cb.cutoff_tz = cutoff_tz;
812         cb.cutoff_cnt = cutoff_cnt;
813         cb.sha1 = sha1;
814
815         for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
816
817         if (!cb.reccnt) {
818                 if (flags & GET_SHA1_QUIETLY)
819                         exit(128);
820                 else
821                         die("Log for %s is empty.", refname);
822         }
823         if (cb.found_it)
824                 return 0;
825
826         for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
827
828         return 1;
829 }
830
831 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
832                                                     struct strbuf *err)
833 {
834         struct ref_transaction *tr;
835         assert(err);
836
837         tr = xcalloc(1, sizeof(struct ref_transaction));
838         tr->ref_store = refs;
839         return tr;
840 }
841
842 struct ref_transaction *ref_transaction_begin(struct strbuf *err)
843 {
844         return ref_store_transaction_begin(get_main_ref_store(), err);
845 }
846
847 void ref_transaction_free(struct ref_transaction *transaction)
848 {
849         int i;
850
851         if (!transaction)
852                 return;
853
854         for (i = 0; i < transaction->nr; i++) {
855                 free(transaction->updates[i]->msg);
856                 free(transaction->updates[i]);
857         }
858         free(transaction->updates);
859         free(transaction);
860 }
861
862 struct ref_update *ref_transaction_add_update(
863                 struct ref_transaction *transaction,
864                 const char *refname, unsigned int flags,
865                 const unsigned char *new_sha1,
866                 const unsigned char *old_sha1,
867                 const char *msg)
868 {
869         struct ref_update *update;
870
871         if (transaction->state != REF_TRANSACTION_OPEN)
872                 die("BUG: update called for transaction that is not open");
873
874         if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF))
875                 die("BUG: REF_ISPRUNING set without REF_NODEREF");
876
877         FLEX_ALLOC_STR(update, refname, refname);
878         ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
879         transaction->updates[transaction->nr++] = update;
880
881         update->flags = flags;
882
883         if (flags & REF_HAVE_NEW)
884                 hashcpy(update->new_sha1, new_sha1);
885         if (flags & REF_HAVE_OLD)
886                 hashcpy(update->old_sha1, old_sha1);
887         update->msg = xstrdup_or_null(msg);
888         return update;
889 }
890
891 int ref_transaction_update(struct ref_transaction *transaction,
892                            const char *refname,
893                            const unsigned char *new_sha1,
894                            const unsigned char *old_sha1,
895                            unsigned int flags, const char *msg,
896                            struct strbuf *err)
897 {
898         assert(err);
899
900         if ((new_sha1 && !is_null_sha1(new_sha1)) ?
901             check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
902             !refname_is_safe(refname)) {
903                 strbuf_addf(err, "refusing to update ref with bad name '%s'",
904                             refname);
905                 return -1;
906         }
907
908         flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
909
910         ref_transaction_add_update(transaction, refname, flags,
911                                    new_sha1, old_sha1, msg);
912         return 0;
913 }
914
915 int ref_transaction_create(struct ref_transaction *transaction,
916                            const char *refname,
917                            const unsigned char *new_sha1,
918                            unsigned int flags, const char *msg,
919                            struct strbuf *err)
920 {
921         if (!new_sha1 || is_null_sha1(new_sha1))
922                 die("BUG: create called without valid new_sha1");
923         return ref_transaction_update(transaction, refname, new_sha1,
924                                       null_sha1, flags, msg, err);
925 }
926
927 int ref_transaction_delete(struct ref_transaction *transaction,
928                            const char *refname,
929                            const unsigned char *old_sha1,
930                            unsigned int flags, const char *msg,
931                            struct strbuf *err)
932 {
933         if (old_sha1 && is_null_sha1(old_sha1))
934                 die("BUG: delete called with old_sha1 set to zeros");
935         return ref_transaction_update(transaction, refname,
936                                       null_sha1, old_sha1,
937                                       flags, msg, err);
938 }
939
940 int ref_transaction_verify(struct ref_transaction *transaction,
941                            const char *refname,
942                            const unsigned char *old_sha1,
943                            unsigned int flags,
944                            struct strbuf *err)
945 {
946         if (!old_sha1)
947                 die("BUG: verify called with old_sha1 set to NULL");
948         return ref_transaction_update(transaction, refname,
949                                       NULL, old_sha1,
950                                       flags, NULL, err);
951 }
952
953 int update_ref_oid(const char *msg, const char *refname,
954                const struct object_id *new_oid, const struct object_id *old_oid,
955                unsigned int flags, enum action_on_err onerr)
956 {
957         return update_ref(msg, refname, new_oid ? new_oid->hash : NULL,
958                 old_oid ? old_oid->hash : NULL, flags, onerr);
959 }
960
961 int refs_update_ref(struct ref_store *refs, const char *msg,
962                     const char *refname, const unsigned char *new_sha1,
963                     const unsigned char *old_sha1, unsigned int flags,
964                     enum action_on_err onerr)
965 {
966         struct ref_transaction *t = NULL;
967         struct strbuf err = STRBUF_INIT;
968         int ret = 0;
969
970         if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
971                 assert(refs == get_main_ref_store());
972                 ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
973         } else {
974                 t = ref_store_transaction_begin(refs, &err);
975                 if (!t ||
976                     ref_transaction_update(t, refname, new_sha1, old_sha1,
977                                            flags, msg, &err) ||
978                     ref_transaction_commit(t, &err)) {
979                         ret = 1;
980                         ref_transaction_free(t);
981                 }
982         }
983         if (ret) {
984                 const char *str = "update_ref failed for ref '%s': %s";
985
986                 switch (onerr) {
987                 case UPDATE_REFS_MSG_ON_ERR:
988                         error(str, refname, err.buf);
989                         break;
990                 case UPDATE_REFS_DIE_ON_ERR:
991                         die(str, refname, err.buf);
992                         break;
993                 case UPDATE_REFS_QUIET_ON_ERR:
994                         break;
995                 }
996                 strbuf_release(&err);
997                 return 1;
998         }
999         strbuf_release(&err);
1000         if (t)
1001                 ref_transaction_free(t);
1002         return 0;
1003 }
1004
1005 int update_ref(const char *msg, const char *refname,
1006                const unsigned char *new_sha1,
1007                const unsigned char *old_sha1,
1008                unsigned int flags, enum action_on_err onerr)
1009 {
1010         return refs_update_ref(get_main_ref_store(), msg, refname, new_sha1,
1011                                old_sha1, flags, onerr);
1012 }
1013
1014 char *shorten_unambiguous_ref(const char *refname, int strict)
1015 {
1016         int i;
1017         static char **scanf_fmts;
1018         static int nr_rules;
1019         char *short_name;
1020         struct strbuf resolved_buf = STRBUF_INIT;
1021
1022         if (!nr_rules) {
1023                 /*
1024                  * Pre-generate scanf formats from ref_rev_parse_rules[].
1025                  * Generate a format suitable for scanf from a
1026                  * ref_rev_parse_rules rule by interpolating "%s" at the
1027                  * location of the "%.*s".
1028                  */
1029                 size_t total_len = 0;
1030                 size_t offset = 0;
1031
1032                 /* the rule list is NULL terminated, count them first */
1033                 for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
1034                         /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
1035                         total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
1036
1037                 scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), nr_rules), total_len));
1038
1039                 offset = 0;
1040                 for (i = 0; i < nr_rules; i++) {
1041                         assert(offset < total_len);
1042                         scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
1043                         offset += snprintf(scanf_fmts[i], total_len - offset,
1044                                            ref_rev_parse_rules[i], 2, "%s") + 1;
1045                 }
1046         }
1047
1048         /* bail out if there are no rules */
1049         if (!nr_rules)
1050                 return xstrdup(refname);
1051
1052         /* buffer for scanf result, at most refname must fit */
1053         short_name = xstrdup(refname);
1054
1055         /* skip first rule, it will always match */
1056         for (i = nr_rules - 1; i > 0 ; --i) {
1057                 int j;
1058                 int rules_to_fail = i;
1059                 int short_name_len;
1060
1061                 if (1 != sscanf(refname, scanf_fmts[i], short_name))
1062                         continue;
1063
1064                 short_name_len = strlen(short_name);
1065
1066                 /*
1067                  * in strict mode, all (except the matched one) rules
1068                  * must fail to resolve to a valid non-ambiguous ref
1069                  */
1070                 if (strict)
1071                         rules_to_fail = nr_rules;
1072
1073                 /*
1074                  * check if the short name resolves to a valid ref,
1075                  * but use only rules prior to the matched one
1076                  */
1077                 for (j = 0; j < rules_to_fail; j++) {
1078                         const char *rule = ref_rev_parse_rules[j];
1079
1080                         /* skip matched rule */
1081                         if (i == j)
1082                                 continue;
1083
1084                         /*
1085                          * the short name is ambiguous, if it resolves
1086                          * (with this previous rule) to a valid ref
1087                          * read_ref() returns 0 on success
1088                          */
1089                         strbuf_reset(&resolved_buf);
1090                         strbuf_addf(&resolved_buf, rule,
1091                                     short_name_len, short_name);
1092                         if (ref_exists(resolved_buf.buf))
1093                                 break;
1094                 }
1095
1096                 /*
1097                  * short name is non-ambiguous if all previous rules
1098                  * haven't resolved to a valid ref
1099                  */
1100                 if (j == rules_to_fail) {
1101                         strbuf_release(&resolved_buf);
1102                         return short_name;
1103                 }
1104         }
1105
1106         strbuf_release(&resolved_buf);
1107         free(short_name);
1108         return xstrdup(refname);
1109 }
1110
1111 static struct string_list *hide_refs;
1112
1113 int parse_hide_refs_config(const char *var, const char *value, const char *section)
1114 {
1115         const char *key;
1116         if (!strcmp("transfer.hiderefs", var) ||
1117             (!parse_config_key(var, section, NULL, NULL, &key) &&
1118              !strcmp(key, "hiderefs"))) {
1119                 char *ref;
1120                 int len;
1121
1122                 if (!value)
1123                         return config_error_nonbool(var);
1124                 ref = xstrdup(value);
1125                 len = strlen(ref);
1126                 while (len && ref[len - 1] == '/')
1127                         ref[--len] = '\0';
1128                 if (!hide_refs) {
1129                         hide_refs = xcalloc(1, sizeof(*hide_refs));
1130                         hide_refs->strdup_strings = 1;
1131                 }
1132                 string_list_append(hide_refs, ref);
1133         }
1134         return 0;
1135 }
1136
1137 int ref_is_hidden(const char *refname, const char *refname_full)
1138 {
1139         int i;
1140
1141         if (!hide_refs)
1142                 return 0;
1143         for (i = hide_refs->nr - 1; i >= 0; i--) {
1144                 const char *match = hide_refs->items[i].string;
1145                 const char *subject;
1146                 int neg = 0;
1147                 int len;
1148
1149                 if (*match == '!') {
1150                         neg = 1;
1151                         match++;
1152                 }
1153
1154                 if (*match == '^') {
1155                         subject = refname_full;
1156                         match++;
1157                 } else {
1158                         subject = refname;
1159                 }
1160
1161                 /* refname can be NULL when namespaces are used. */
1162                 if (!subject || !starts_with(subject, match))
1163                         continue;
1164                 len = strlen(match);
1165                 if (!subject[len] || subject[len] == '/')
1166                         return !neg;
1167         }
1168         return 0;
1169 }
1170
1171 const char *find_descendant_ref(const char *dirname,
1172                                 const struct string_list *extras,
1173                                 const struct string_list *skip)
1174 {
1175         int pos;
1176
1177         if (!extras)
1178                 return NULL;
1179
1180         /*
1181          * Look at the place where dirname would be inserted into
1182          * extras. If there is an entry at that position that starts
1183          * with dirname (remember, dirname includes the trailing
1184          * slash) and is not in skip, then we have a conflict.
1185          */
1186         for (pos = string_list_find_insert_index(extras, dirname, 0);
1187              pos < extras->nr; pos++) {
1188                 const char *extra_refname = extras->items[pos].string;
1189
1190                 if (!starts_with(extra_refname, dirname))
1191                         break;
1192
1193                 if (!skip || !string_list_has_string(skip, extra_refname))
1194                         return extra_refname;
1195         }
1196         return NULL;
1197 }
1198
1199 int refs_rename_ref_available(struct ref_store *refs,
1200                               const char *old_refname,
1201                               const char *new_refname)
1202 {
1203         struct string_list skip = STRING_LIST_INIT_NODUP;
1204         struct strbuf err = STRBUF_INIT;
1205         int ok;
1206
1207         string_list_insert(&skip, old_refname);
1208         ok = !refs_verify_refname_available(refs, new_refname,
1209                                             NULL, &skip, &err);
1210         if (!ok)
1211                 error("%s", err.buf);
1212
1213         string_list_clear(&skip, 0);
1214         strbuf_release(&err);
1215         return ok;
1216 }
1217
1218 int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1219 {
1220         struct object_id oid;
1221         int flag;
1222
1223         if (submodule) {
1224                 if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
1225                         return fn("HEAD", &oid, 0, cb_data);
1226
1227                 return 0;
1228         }
1229
1230         if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
1231                 return fn("HEAD", &oid, flag, cb_data);
1232
1233         return 0;
1234 }
1235
1236 int head_ref(each_ref_fn fn, void *cb_data)
1237 {
1238         return head_ref_submodule(NULL, fn, cb_data);
1239 }
1240
1241 /*
1242  * Call fn for each reference in the specified submodule for which the
1243  * refname begins with prefix. If trim is non-zero, then trim that
1244  * many characters off the beginning of each refname before passing
1245  * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1246  * include broken references in the iteration. If fn ever returns a
1247  * non-zero value, stop the iteration and return that value;
1248  * otherwise, return 0.
1249  */
1250 static int do_for_each_ref(struct ref_store *refs, const char *prefix,
1251                            each_ref_fn fn, int trim, int flags, void *cb_data)
1252 {
1253         struct ref_iterator *iter;
1254
1255         if (!refs)
1256                 return 0;
1257
1258         iter = refs->be->iterator_begin(refs, prefix, flags);
1259         iter = prefix_ref_iterator_begin(iter, prefix, trim);
1260
1261         return do_for_each_ref_iterator(iter, fn, cb_data);
1262 }
1263
1264 int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1265 {
1266         return do_for_each_ref(refs, "", fn, 0, 0, cb_data);
1267 }
1268
1269 int for_each_ref(each_ref_fn fn, void *cb_data)
1270 {
1271         return refs_for_each_ref(get_main_ref_store(), fn, cb_data);
1272 }
1273
1274 int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1275 {
1276         return refs_for_each_ref(get_submodule_ref_store(submodule), fn, cb_data);
1277 }
1278
1279 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
1280                          each_ref_fn fn, void *cb_data)
1281 {
1282         return do_for_each_ref(refs, prefix, fn, strlen(prefix), 0, cb_data);
1283 }
1284
1285 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1286 {
1287         return refs_for_each_ref_in(get_main_ref_store(), prefix, fn, cb_data);
1288 }
1289
1290 int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1291 {
1292         unsigned int flag = 0;
1293
1294         if (broken)
1295                 flag = DO_FOR_EACH_INCLUDE_BROKEN;
1296         return do_for_each_ref(get_main_ref_store(),
1297                                prefix, fn, 0, flag, cb_data);
1298 }
1299
1300 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1301                               each_ref_fn fn, void *cb_data)
1302 {
1303         return refs_for_each_ref_in(get_submodule_ref_store(submodule),
1304                                     prefix, fn, cb_data);
1305 }
1306
1307 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1308 {
1309         return do_for_each_ref(get_main_ref_store(),
1310                                git_replace_ref_base, fn,
1311                                strlen(git_replace_ref_base),
1312                                0, cb_data);
1313 }
1314
1315 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1316 {
1317         struct strbuf buf = STRBUF_INIT;
1318         int ret;
1319         strbuf_addf(&buf, "%srefs/", get_git_namespace());
1320         ret = do_for_each_ref(get_main_ref_store(),
1321                               buf.buf, fn, 0, 0, cb_data);
1322         strbuf_release(&buf);
1323         return ret;
1324 }
1325
1326 int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1327 {
1328         return do_for_each_ref(refs, "", fn, 0,
1329                                DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1330 }
1331
1332 int for_each_rawref(each_ref_fn fn, void *cb_data)
1333 {
1334         return refs_for_each_rawref(get_main_ref_store(), fn, cb_data);
1335 }
1336
1337 /* This function needs to return a meaningful errno on failure */
1338 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1339                                     const char *refname,
1340                                     int resolve_flags,
1341                                     unsigned char *sha1, int *flags)
1342 {
1343         static struct strbuf sb_refname = STRBUF_INIT;
1344         int unused_flags;
1345         int symref_count;
1346
1347         if (!flags)
1348                 flags = &unused_flags;
1349
1350         *flags = 0;
1351
1352         if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1353                 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1354                     !refname_is_safe(refname)) {
1355                         errno = EINVAL;
1356                         return NULL;
1357                 }
1358
1359                 /*
1360                  * dwim_ref() uses REF_ISBROKEN to distinguish between
1361                  * missing refs and refs that were present but invalid,
1362                  * to complain about the latter to stderr.
1363                  *
1364                  * We don't know whether the ref exists, so don't set
1365                  * REF_ISBROKEN yet.
1366                  */
1367                 *flags |= REF_BAD_NAME;
1368         }
1369
1370         for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1371                 unsigned int read_flags = 0;
1372
1373                 if (refs->be->read_raw_ref(refs, refname,
1374                                            sha1, &sb_refname, &read_flags)) {
1375                         *flags |= read_flags;
1376                         if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1377                                 return NULL;
1378                         hashclr(sha1);
1379                         if (*flags & REF_BAD_NAME)
1380                                 *flags |= REF_ISBROKEN;
1381                         return refname;
1382                 }
1383
1384                 *flags |= read_flags;
1385
1386                 if (!(read_flags & REF_ISSYMREF)) {
1387                         if (*flags & REF_BAD_NAME) {
1388                                 hashclr(sha1);
1389                                 *flags |= REF_ISBROKEN;
1390                         }
1391                         return refname;
1392                 }
1393
1394                 refname = sb_refname.buf;
1395                 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1396                         hashclr(sha1);
1397                         return refname;
1398                 }
1399                 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1400                         if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1401                             !refname_is_safe(refname)) {
1402                                 errno = EINVAL;
1403                                 return NULL;
1404                         }
1405
1406                         *flags |= REF_ISBROKEN | REF_BAD_NAME;
1407                 }
1408         }
1409
1410         errno = ELOOP;
1411         return NULL;
1412 }
1413
1414 /* backend functions */
1415 int refs_init_db(struct strbuf *err)
1416 {
1417         struct ref_store *refs = get_main_ref_store();
1418
1419         return refs->be->init_db(refs, err);
1420 }
1421
1422 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1423                                unsigned char *sha1, int *flags)
1424 {
1425         return refs_resolve_ref_unsafe(get_main_ref_store(), refname,
1426                                        resolve_flags, sha1, flags);
1427 }
1428
1429 int resolve_gitlink_ref(const char *submodule, const char *refname,
1430                         unsigned char *sha1)
1431 {
1432         size_t len = strlen(submodule);
1433         struct ref_store *refs;
1434         int flags;
1435
1436         while (len && submodule[len - 1] == '/')
1437                 len--;
1438
1439         if (!len)
1440                 return -1;
1441
1442         if (submodule[len]) {
1443                 /* We need to strip off one or more trailing slashes */
1444                 char *stripped = xmemdupz(submodule, len);
1445
1446                 refs = get_submodule_ref_store(stripped);
1447                 free(stripped);
1448         } else {
1449                 refs = get_submodule_ref_store(submodule);
1450         }
1451
1452         if (!refs)
1453                 return -1;
1454
1455         if (!refs_resolve_ref_unsafe(refs, refname, 0, sha1, &flags) ||
1456             is_null_sha1(sha1))
1457                 return -1;
1458         return 0;
1459 }
1460
1461 struct submodule_hash_entry
1462 {
1463         struct hashmap_entry ent; /* must be the first member! */
1464
1465         struct ref_store *refs;
1466
1467         /* NUL-terminated name of submodule: */
1468         char submodule[FLEX_ARRAY];
1469 };
1470
1471 static int submodule_hash_cmp(const void *entry, const void *entry_or_key,
1472                               const void *keydata)
1473 {
1474         const struct submodule_hash_entry *e1 = entry, *e2 = entry_or_key;
1475         const char *submodule = keydata ? keydata : e2->submodule;
1476
1477         return strcmp(e1->submodule, submodule);
1478 }
1479
1480 static struct submodule_hash_entry *alloc_submodule_hash_entry(
1481                 const char *submodule, struct ref_store *refs)
1482 {
1483         struct submodule_hash_entry *entry;
1484
1485         FLEX_ALLOC_STR(entry, submodule, submodule);
1486         hashmap_entry_init(entry, strhash(submodule));
1487         entry->refs = refs;
1488         return entry;
1489 }
1490
1491 /* A pointer to the ref_store for the main repository: */
1492 static struct ref_store *main_ref_store;
1493
1494 /* A hashmap of ref_stores, stored by submodule name: */
1495 static struct hashmap submodule_ref_stores;
1496
1497 /*
1498  * Return the ref_store instance for the specified submodule. If that
1499  * ref_store hasn't been initialized yet, return NULL.
1500  */
1501 static struct ref_store *lookup_submodule_ref_store(const char *submodule)
1502 {
1503         struct submodule_hash_entry *entry;
1504
1505         if (!submodule_ref_stores.tablesize)
1506                 /* It's initialized on demand in register_ref_store(). */
1507                 return NULL;
1508
1509         entry = hashmap_get_from_hash(&submodule_ref_stores,
1510                                       strhash(submodule), submodule);
1511         return entry ? entry->refs : NULL;
1512 }
1513
1514 /*
1515  * Create, record, and return a ref_store instance for the specified
1516  * gitdir.
1517  */
1518 static struct ref_store *ref_store_init(const char *gitdir,
1519                                         unsigned int flags)
1520 {
1521         const char *be_name = "files";
1522         struct ref_storage_be *be = find_ref_storage_backend(be_name);
1523         struct ref_store *refs;
1524
1525         if (!be)
1526                 die("BUG: reference backend %s is unknown", be_name);
1527
1528         refs = be->init(gitdir, flags);
1529         return refs;
1530 }
1531
1532 struct ref_store *get_main_ref_store(void)
1533 {
1534         if (main_ref_store)
1535                 return main_ref_store;
1536
1537         main_ref_store = ref_store_init(get_git_dir(),
1538                                         (REF_STORE_READ |
1539                                          REF_STORE_WRITE |
1540                                          REF_STORE_ODB |
1541                                          REF_STORE_MAIN));
1542         return main_ref_store;
1543 }
1544
1545 /*
1546  * Register the specified ref_store to be the one that should be used
1547  * for submodule. It is a fatal error to call this function twice for
1548  * the same submodule.
1549  */
1550 static void register_submodule_ref_store(struct ref_store *refs,
1551                                          const char *submodule)
1552 {
1553         if (!submodule_ref_stores.tablesize)
1554                 hashmap_init(&submodule_ref_stores, submodule_hash_cmp, 0);
1555
1556         if (hashmap_put(&submodule_ref_stores,
1557                         alloc_submodule_hash_entry(submodule, refs)))
1558                 die("BUG: ref_store for submodule '%s' initialized twice",
1559                     submodule);
1560 }
1561
1562 struct ref_store *get_submodule_ref_store(const char *submodule)
1563 {
1564         struct strbuf submodule_sb = STRBUF_INIT;
1565         struct ref_store *refs;
1566         int ret;
1567
1568         if (!submodule || !*submodule) {
1569                 /*
1570                  * FIXME: This case is ideally not allowed. But that
1571                  * can't happen until we clean up all the callers.
1572                  */
1573                 return get_main_ref_store();
1574         }
1575
1576         refs = lookup_submodule_ref_store(submodule);
1577         if (refs)
1578                 return refs;
1579
1580         strbuf_addstr(&submodule_sb, submodule);
1581         ret = is_nonbare_repository_dir(&submodule_sb);
1582         strbuf_release(&submodule_sb);
1583         if (!ret)
1584                 return NULL;
1585
1586         ret = submodule_to_gitdir(&submodule_sb, submodule);
1587         if (ret) {
1588                 strbuf_release(&submodule_sb);
1589                 return NULL;
1590         }
1591
1592         /* assume that add_submodule_odb() has been called */
1593         refs = ref_store_init(submodule_sb.buf,
1594                               REF_STORE_READ | REF_STORE_ODB);
1595         register_submodule_ref_store(refs, submodule);
1596
1597         strbuf_release(&submodule_sb);
1598         return refs;
1599 }
1600
1601 void base_ref_store_init(struct ref_store *refs,
1602                          const struct ref_storage_be *be)
1603 {
1604         refs->be = be;
1605 }
1606
1607 /* backend functions */
1608 int refs_pack_refs(struct ref_store *refs, unsigned int flags)
1609 {
1610         return refs->be->pack_refs(refs, flags);
1611 }
1612
1613 int refs_peel_ref(struct ref_store *refs, const char *refname,
1614                   unsigned char *sha1)
1615 {
1616         return refs->be->peel_ref(refs, refname, sha1);
1617 }
1618
1619 int peel_ref(const char *refname, unsigned char *sha1)
1620 {
1621         return refs_peel_ref(get_main_ref_store(), refname, sha1);
1622 }
1623
1624 int refs_create_symref(struct ref_store *refs,
1625                        const char *ref_target,
1626                        const char *refs_heads_master,
1627                        const char *logmsg)
1628 {
1629         return refs->be->create_symref(refs, ref_target,
1630                                        refs_heads_master,
1631                                        logmsg);
1632 }
1633
1634 int create_symref(const char *ref_target, const char *refs_heads_master,
1635                   const char *logmsg)
1636 {
1637         return refs_create_symref(get_main_ref_store(), ref_target,
1638                                   refs_heads_master, logmsg);
1639 }
1640
1641 int ref_transaction_commit(struct ref_transaction *transaction,
1642                            struct strbuf *err)
1643 {
1644         struct ref_store *refs = transaction->ref_store;
1645
1646         return refs->be->transaction_commit(refs, transaction, err);
1647 }
1648
1649 int refs_verify_refname_available(struct ref_store *refs,
1650                                   const char *refname,
1651                                   const struct string_list *extra,
1652                                   const struct string_list *skip,
1653                                   struct strbuf *err)
1654 {
1655         return refs->be->verify_refname_available(refs, refname, extra, skip, err);
1656 }
1657
1658 int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1659 {
1660         struct ref_iterator *iter;
1661
1662         iter = refs->be->reflog_iterator_begin(refs);
1663
1664         return do_for_each_ref_iterator(iter, fn, cb_data);
1665 }
1666
1667 int for_each_reflog(each_ref_fn fn, void *cb_data)
1668 {
1669         return refs_for_each_reflog(get_main_ref_store(), fn, cb_data);
1670 }
1671
1672 int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
1673                                      const char *refname,
1674                                      each_reflog_ent_fn fn,
1675                                      void *cb_data)
1676 {
1677         return refs->be->for_each_reflog_ent_reverse(refs, refname,
1678                                                      fn, cb_data);
1679 }
1680
1681 int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
1682                                 void *cb_data)
1683 {
1684         return refs_for_each_reflog_ent_reverse(get_main_ref_store(),
1685                                                 refname, fn, cb_data);
1686 }
1687
1688 int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
1689                              each_reflog_ent_fn fn, void *cb_data)
1690 {
1691         return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
1692 }
1693
1694 int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,
1695                         void *cb_data)
1696 {
1697         return refs_for_each_reflog_ent(get_main_ref_store(), refname,
1698                                         fn, cb_data);
1699 }
1700
1701 int refs_reflog_exists(struct ref_store *refs, const char *refname)
1702 {
1703         return refs->be->reflog_exists(refs, refname);
1704 }
1705
1706 int reflog_exists(const char *refname)
1707 {
1708         return refs_reflog_exists(get_main_ref_store(), refname);
1709 }
1710
1711 int refs_create_reflog(struct ref_store *refs, const char *refname,
1712                        int force_create, struct strbuf *err)
1713 {
1714         return refs->be->create_reflog(refs, refname, force_create, err);
1715 }
1716
1717 int safe_create_reflog(const char *refname, int force_create,
1718                        struct strbuf *err)
1719 {
1720         return refs_create_reflog(get_main_ref_store(), refname,
1721                                   force_create, err);
1722 }
1723
1724 int refs_delete_reflog(struct ref_store *refs, const char *refname)
1725 {
1726         return refs->be->delete_reflog(refs, refname);
1727 }
1728
1729 int delete_reflog(const char *refname)
1730 {
1731         return refs_delete_reflog(get_main_ref_store(), refname);
1732 }
1733
1734 int refs_reflog_expire(struct ref_store *refs,
1735                        const char *refname, const unsigned char *sha1,
1736                        unsigned int flags,
1737                        reflog_expiry_prepare_fn prepare_fn,
1738                        reflog_expiry_should_prune_fn should_prune_fn,
1739                        reflog_expiry_cleanup_fn cleanup_fn,
1740                        void *policy_cb_data)
1741 {
1742         return refs->be->reflog_expire(refs, refname, sha1, flags,
1743                                        prepare_fn, should_prune_fn,
1744                                        cleanup_fn, policy_cb_data);
1745 }
1746
1747 int reflog_expire(const char *refname, const unsigned char *sha1,
1748                   unsigned int flags,
1749                   reflog_expiry_prepare_fn prepare_fn,
1750                   reflog_expiry_should_prune_fn should_prune_fn,
1751                   reflog_expiry_cleanup_fn cleanup_fn,
1752                   void *policy_cb_data)
1753 {
1754         return refs_reflog_expire(get_main_ref_store(),
1755                                   refname, sha1, flags,
1756                                   prepare_fn, should_prune_fn,
1757                                   cleanup_fn, policy_cb_data);
1758 }
1759
1760 int initial_ref_transaction_commit(struct ref_transaction *transaction,
1761                                    struct strbuf *err)
1762 {
1763         struct ref_store *refs = transaction->ref_store;
1764
1765         return refs->be->initial_transaction_commit(refs, transaction, err);
1766 }
1767
1768 int refs_delete_refs(struct ref_store *refs, struct string_list *refnames,
1769                      unsigned int flags)
1770 {
1771         return refs->be->delete_refs(refs, refnames, flags);
1772 }
1773
1774 int delete_refs(struct string_list *refnames, unsigned int flags)
1775 {
1776         return refs_delete_refs(get_main_ref_store(), refnames, flags);
1777 }
1778
1779 int refs_rename_ref(struct ref_store *refs, const char *oldref,
1780                     const char *newref, const char *logmsg)
1781 {
1782         return refs->be->rename_ref(refs, oldref, newref, logmsg);
1783 }
1784
1785 int rename_ref(const char *oldref, const char *newref, const char *logmsg)
1786 {
1787         return refs_rename_ref(get_main_ref_store(), oldref, newref, logmsg);
1788 }