OSDN Git Service

refs_read_raw_ref(): new function
[git-core/git.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         return name + (
408                 starts_with(name, "refs/heads/") ? 11 :
409                 starts_with(name, "refs/tags/") ? 10 :
410                 starts_with(name, "refs/remotes/") ? 13 :
411                 0);
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
471         *ref = NULL;
472         for (p = ref_rev_parse_rules; *p; p++) {
473                 char fullref[PATH_MAX];
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                 mksnpath(fullref, sizeof(fullref), *p, len, str);
480                 r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING,
481                                        this_result, &flag);
482                 if (r) {
483                         if (!refs_found++)
484                                 *ref = xstrdup(r);
485                         if (!warn_ambiguous_refs)
486                                 break;
487                 } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) {
488                         warning("ignoring dangling symref %s.", fullref);
489                 } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) {
490                         warning("ignoring broken ref %s.", fullref);
491                 }
492         }
493         return refs_found;
494 }
495
496 int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
497 {
498         char *last_branch = substitute_branch_name(&str, &len);
499         const char **p;
500         int logs_found = 0;
501
502         *log = NULL;
503         for (p = ref_rev_parse_rules; *p; p++) {
504                 unsigned char hash[20];
505                 char path[PATH_MAX];
506                 const char *ref, *it;
507
508                 mksnpath(path, sizeof(path), *p, len, str);
509                 ref = resolve_ref_unsafe(path, RESOLVE_REF_READING,
510                                          hash, NULL);
511                 if (!ref)
512                         continue;
513                 if (reflog_exists(path))
514                         it = path;
515                 else if (strcmp(ref, path) && reflog_exists(ref))
516                         it = ref;
517                 else
518                         continue;
519                 if (!logs_found++) {
520                         *log = xstrdup(it);
521                         hashcpy(sha1, hash);
522                 }
523                 if (!warn_ambiguous_refs)
524                         break;
525         }
526         free(last_branch);
527         return logs_found;
528 }
529
530 static int is_per_worktree_ref(const char *refname)
531 {
532         return !strcmp(refname, "HEAD") ||
533                 starts_with(refname, "refs/bisect/");
534 }
535
536 static int is_pseudoref_syntax(const char *refname)
537 {
538         const char *c;
539
540         for (c = refname; *c; c++) {
541                 if (!isupper(*c) && *c != '-' && *c != '_')
542                         return 0;
543         }
544
545         return 1;
546 }
547
548 enum ref_type ref_type(const char *refname)
549 {
550         if (is_per_worktree_ref(refname))
551                 return REF_TYPE_PER_WORKTREE;
552         if (is_pseudoref_syntax(refname))
553                 return REF_TYPE_PSEUDOREF;
554        return REF_TYPE_NORMAL;
555 }
556
557 static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
558                            const unsigned char *old_sha1, struct strbuf *err)
559 {
560         const char *filename;
561         int fd;
562         static struct lock_file lock;
563         struct strbuf buf = STRBUF_INIT;
564         int ret = -1;
565
566         strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
567
568         filename = git_path("%s", pseudoref);
569         fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
570         if (fd < 0) {
571                 strbuf_addf(err, "could not open '%s' for writing: %s",
572                             filename, strerror(errno));
573                 return -1;
574         }
575
576         if (old_sha1) {
577                 unsigned char actual_old_sha1[20];
578
579                 if (read_ref(pseudoref, actual_old_sha1))
580                         die("could not read ref '%s'", pseudoref);
581                 if (hashcmp(actual_old_sha1, old_sha1)) {
582                         strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref);
583                         rollback_lock_file(&lock);
584                         goto done;
585                 }
586         }
587
588         if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
589                 strbuf_addf(err, "could not write to '%s'", filename);
590                 rollback_lock_file(&lock);
591                 goto done;
592         }
593
594         commit_lock_file(&lock);
595         ret = 0;
596 done:
597         strbuf_release(&buf);
598         return ret;
599 }
600
601 static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1)
602 {
603         static struct lock_file lock;
604         const char *filename;
605
606         filename = git_path("%s", pseudoref);
607
608         if (old_sha1 && !is_null_sha1(old_sha1)) {
609                 int fd;
610                 unsigned char actual_old_sha1[20];
611
612                 fd = hold_lock_file_for_update(&lock, filename,
613                                                LOCK_DIE_ON_ERROR);
614                 if (fd < 0)
615                         die_errno(_("Could not open '%s' for writing"), filename);
616                 if (read_ref(pseudoref, actual_old_sha1))
617                         die("could not read ref '%s'", pseudoref);
618                 if (hashcmp(actual_old_sha1, old_sha1)) {
619                         warning("Unexpected sha1 when deleting %s", pseudoref);
620                         rollback_lock_file(&lock);
621                         return -1;
622                 }
623
624                 unlink(filename);
625                 rollback_lock_file(&lock);
626         } else {
627                 unlink(filename);
628         }
629
630         return 0;
631 }
632
633 int refs_delete_ref(struct ref_store *refs, const char *msg,
634                     const char *refname,
635                     const unsigned char *old_sha1,
636                     unsigned int flags)
637 {
638         struct ref_transaction *transaction;
639         struct strbuf err = STRBUF_INIT;
640
641         if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
642                 assert(refs == get_main_ref_store());
643                 return delete_pseudoref(refname, old_sha1);
644         }
645
646         transaction = ref_store_transaction_begin(refs, &err);
647         if (!transaction ||
648             ref_transaction_delete(transaction, refname, old_sha1,
649                                    flags, msg, &err) ||
650             ref_transaction_commit(transaction, &err)) {
651                 error("%s", err.buf);
652                 ref_transaction_free(transaction);
653                 strbuf_release(&err);
654                 return 1;
655         }
656         ref_transaction_free(transaction);
657         strbuf_release(&err);
658         return 0;
659 }
660
661 int delete_ref(const char *msg, const char *refname,
662                const unsigned char *old_sha1, unsigned int flags)
663 {
664         return refs_delete_ref(get_main_ref_store(), msg, refname,
665                                old_sha1, flags);
666 }
667
668 int copy_reflog_msg(char *buf, const char *msg)
669 {
670         char *cp = buf;
671         char c;
672         int wasspace = 1;
673
674         *cp++ = '\t';
675         while ((c = *msg++)) {
676                 if (wasspace && isspace(c))
677                         continue;
678                 wasspace = isspace(c);
679                 if (wasspace)
680                         c = ' ';
681                 *cp++ = c;
682         }
683         while (buf < cp && isspace(cp[-1]))
684                 cp--;
685         *cp++ = '\n';
686         return cp - buf;
687 }
688
689 int should_autocreate_reflog(const char *refname)
690 {
691         switch (log_all_ref_updates) {
692         case LOG_REFS_ALWAYS:
693                 return 1;
694         case LOG_REFS_NORMAL:
695                 return starts_with(refname, "refs/heads/") ||
696                         starts_with(refname, "refs/remotes/") ||
697                         starts_with(refname, "refs/notes/") ||
698                         !strcmp(refname, "HEAD");
699         default:
700                 return 0;
701         }
702 }
703
704 int is_branch(const char *refname)
705 {
706         return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
707 }
708
709 struct read_ref_at_cb {
710         const char *refname;
711         unsigned long at_time;
712         int cnt;
713         int reccnt;
714         unsigned char *sha1;
715         int found_it;
716
717         unsigned char osha1[20];
718         unsigned char nsha1[20];
719         int tz;
720         unsigned long date;
721         char **msg;
722         unsigned long *cutoff_time;
723         int *cutoff_tz;
724         int *cutoff_cnt;
725 };
726
727 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
728                 const char *email, unsigned long timestamp, int tz,
729                 const char *message, void *cb_data)
730 {
731         struct read_ref_at_cb *cb = cb_data;
732
733         cb->reccnt++;
734         cb->tz = tz;
735         cb->date = timestamp;
736
737         if (timestamp <= cb->at_time || cb->cnt == 0) {
738                 if (cb->msg)
739                         *cb->msg = xstrdup(message);
740                 if (cb->cutoff_time)
741                         *cb->cutoff_time = timestamp;
742                 if (cb->cutoff_tz)
743                         *cb->cutoff_tz = tz;
744                 if (cb->cutoff_cnt)
745                         *cb->cutoff_cnt = cb->reccnt - 1;
746                 /*
747                  * we have not yet updated cb->[n|o]sha1 so they still
748                  * hold the values for the previous record.
749                  */
750                 if (!is_null_sha1(cb->osha1)) {
751                         hashcpy(cb->sha1, noid->hash);
752                         if (hashcmp(cb->osha1, noid->hash))
753                                 warning("Log for ref %s has gap after %s.",
754                                         cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
755                 }
756                 else if (cb->date == cb->at_time)
757                         hashcpy(cb->sha1, noid->hash);
758                 else if (hashcmp(noid->hash, cb->sha1))
759                         warning("Log for ref %s unexpectedly ended on %s.",
760                                 cb->refname, show_date(cb->date, cb->tz,
761                                                        DATE_MODE(RFC2822)));
762                 hashcpy(cb->osha1, ooid->hash);
763                 hashcpy(cb->nsha1, noid->hash);
764                 cb->found_it = 1;
765                 return 1;
766         }
767         hashcpy(cb->osha1, ooid->hash);
768         hashcpy(cb->nsha1, noid->hash);
769         if (cb->cnt > 0)
770                 cb->cnt--;
771         return 0;
772 }
773
774 static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
775                                   const char *email, unsigned long timestamp,
776                                   int tz, const char *message, void *cb_data)
777 {
778         struct read_ref_at_cb *cb = cb_data;
779
780         if (cb->msg)
781                 *cb->msg = xstrdup(message);
782         if (cb->cutoff_time)
783                 *cb->cutoff_time = timestamp;
784         if (cb->cutoff_tz)
785                 *cb->cutoff_tz = tz;
786         if (cb->cutoff_cnt)
787                 *cb->cutoff_cnt = cb->reccnt;
788         hashcpy(cb->sha1, ooid->hash);
789         if (is_null_sha1(cb->sha1))
790                 hashcpy(cb->sha1, noid->hash);
791         /* We just want the first entry */
792         return 1;
793 }
794
795 int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt,
796                 unsigned char *sha1, char **msg,
797                 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
798 {
799         struct read_ref_at_cb cb;
800
801         memset(&cb, 0, sizeof(cb));
802         cb.refname = refname;
803         cb.at_time = at_time;
804         cb.cnt = cnt;
805         cb.msg = msg;
806         cb.cutoff_time = cutoff_time;
807         cb.cutoff_tz = cutoff_tz;
808         cb.cutoff_cnt = cutoff_cnt;
809         cb.sha1 = sha1;
810
811         for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
812
813         if (!cb.reccnt) {
814                 if (flags & GET_SHA1_QUIETLY)
815                         exit(128);
816                 else
817                         die("Log for %s is empty.", refname);
818         }
819         if (cb.found_it)
820                 return 0;
821
822         for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
823
824         return 1;
825 }
826
827 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
828                                                     struct strbuf *err)
829 {
830         struct ref_transaction *tr;
831         assert(err);
832
833         tr = xcalloc(1, sizeof(struct ref_transaction));
834         tr->ref_store = refs;
835         return tr;
836 }
837
838 struct ref_transaction *ref_transaction_begin(struct strbuf *err)
839 {
840         return ref_store_transaction_begin(get_main_ref_store(), err);
841 }
842
843 void ref_transaction_free(struct ref_transaction *transaction)
844 {
845         int i;
846
847         if (!transaction)
848                 return;
849
850         for (i = 0; i < transaction->nr; i++) {
851                 free(transaction->updates[i]->msg);
852                 free(transaction->updates[i]);
853         }
854         free(transaction->updates);
855         free(transaction);
856 }
857
858 struct ref_update *ref_transaction_add_update(
859                 struct ref_transaction *transaction,
860                 const char *refname, unsigned int flags,
861                 const unsigned char *new_sha1,
862                 const unsigned char *old_sha1,
863                 const char *msg)
864 {
865         struct ref_update *update;
866
867         if (transaction->state != REF_TRANSACTION_OPEN)
868                 die("BUG: update called for transaction that is not open");
869
870         if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF))
871                 die("BUG: REF_ISPRUNING set without REF_NODEREF");
872
873         FLEX_ALLOC_STR(update, refname, refname);
874         ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
875         transaction->updates[transaction->nr++] = update;
876
877         update->flags = flags;
878
879         if (flags & REF_HAVE_NEW)
880                 hashcpy(update->new_sha1, new_sha1);
881         if (flags & REF_HAVE_OLD)
882                 hashcpy(update->old_sha1, old_sha1);
883         update->msg = xstrdup_or_null(msg);
884         return update;
885 }
886
887 int ref_transaction_update(struct ref_transaction *transaction,
888                            const char *refname,
889                            const unsigned char *new_sha1,
890                            const unsigned char *old_sha1,
891                            unsigned int flags, const char *msg,
892                            struct strbuf *err)
893 {
894         assert(err);
895
896         if ((new_sha1 && !is_null_sha1(new_sha1)) ?
897             check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
898             !refname_is_safe(refname)) {
899                 strbuf_addf(err, "refusing to update ref with bad name '%s'",
900                             refname);
901                 return -1;
902         }
903
904         flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
905
906         ref_transaction_add_update(transaction, refname, flags,
907                                    new_sha1, old_sha1, msg);
908         return 0;
909 }
910
911 int ref_transaction_create(struct ref_transaction *transaction,
912                            const char *refname,
913                            const unsigned char *new_sha1,
914                            unsigned int flags, const char *msg,
915                            struct strbuf *err)
916 {
917         if (!new_sha1 || is_null_sha1(new_sha1))
918                 die("BUG: create called without valid new_sha1");
919         return ref_transaction_update(transaction, refname, new_sha1,
920                                       null_sha1, flags, msg, err);
921 }
922
923 int ref_transaction_delete(struct ref_transaction *transaction,
924                            const char *refname,
925                            const unsigned char *old_sha1,
926                            unsigned int flags, const char *msg,
927                            struct strbuf *err)
928 {
929         if (old_sha1 && is_null_sha1(old_sha1))
930                 die("BUG: delete called with old_sha1 set to zeros");
931         return ref_transaction_update(transaction, refname,
932                                       null_sha1, old_sha1,
933                                       flags, msg, err);
934 }
935
936 int ref_transaction_verify(struct ref_transaction *transaction,
937                            const char *refname,
938                            const unsigned char *old_sha1,
939                            unsigned int flags,
940                            struct strbuf *err)
941 {
942         if (!old_sha1)
943                 die("BUG: verify called with old_sha1 set to NULL");
944         return ref_transaction_update(transaction, refname,
945                                       NULL, old_sha1,
946                                       flags, NULL, err);
947 }
948
949 int update_ref_oid(const char *msg, const char *refname,
950                const struct object_id *new_oid, const struct object_id *old_oid,
951                unsigned int flags, enum action_on_err onerr)
952 {
953         return update_ref(msg, refname, new_oid ? new_oid->hash : NULL,
954                 old_oid ? old_oid->hash : NULL, flags, onerr);
955 }
956
957 int refs_update_ref(struct ref_store *refs, const char *msg,
958                     const char *refname, const unsigned char *new_sha1,
959                     const unsigned char *old_sha1, unsigned int flags,
960                     enum action_on_err onerr)
961 {
962         struct ref_transaction *t = NULL;
963         struct strbuf err = STRBUF_INIT;
964         int ret = 0;
965
966         if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
967                 assert(refs == get_main_ref_store());
968                 ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
969         } else {
970                 t = ref_store_transaction_begin(refs, &err);
971                 if (!t ||
972                     ref_transaction_update(t, refname, new_sha1, old_sha1,
973                                            flags, msg, &err) ||
974                     ref_transaction_commit(t, &err)) {
975                         ret = 1;
976                         ref_transaction_free(t);
977                 }
978         }
979         if (ret) {
980                 const char *str = "update_ref failed for ref '%s': %s";
981
982                 switch (onerr) {
983                 case UPDATE_REFS_MSG_ON_ERR:
984                         error(str, refname, err.buf);
985                         break;
986                 case UPDATE_REFS_DIE_ON_ERR:
987                         die(str, refname, err.buf);
988                         break;
989                 case UPDATE_REFS_QUIET_ON_ERR:
990                         break;
991                 }
992                 strbuf_release(&err);
993                 return 1;
994         }
995         strbuf_release(&err);
996         if (t)
997                 ref_transaction_free(t);
998         return 0;
999 }
1000
1001 int update_ref(const char *msg, const char *refname,
1002                const unsigned char *new_sha1,
1003                const unsigned char *old_sha1,
1004                unsigned int flags, enum action_on_err onerr)
1005 {
1006         return refs_update_ref(get_main_ref_store(), msg, refname, new_sha1,
1007                                old_sha1, flags, onerr);
1008 }
1009
1010 char *shorten_unambiguous_ref(const char *refname, int strict)
1011 {
1012         int i;
1013         static char **scanf_fmts;
1014         static int nr_rules;
1015         char *short_name;
1016
1017         if (!nr_rules) {
1018                 /*
1019                  * Pre-generate scanf formats from ref_rev_parse_rules[].
1020                  * Generate a format suitable for scanf from a
1021                  * ref_rev_parse_rules rule by interpolating "%s" at the
1022                  * location of the "%.*s".
1023                  */
1024                 size_t total_len = 0;
1025                 size_t offset = 0;
1026
1027                 /* the rule list is NULL terminated, count them first */
1028                 for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
1029                         /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
1030                         total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
1031
1032                 scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), nr_rules), total_len));
1033
1034                 offset = 0;
1035                 for (i = 0; i < nr_rules; i++) {
1036                         assert(offset < total_len);
1037                         scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
1038                         offset += snprintf(scanf_fmts[i], total_len - offset,
1039                                            ref_rev_parse_rules[i], 2, "%s") + 1;
1040                 }
1041         }
1042
1043         /* bail out if there are no rules */
1044         if (!nr_rules)
1045                 return xstrdup(refname);
1046
1047         /* buffer for scanf result, at most refname must fit */
1048         short_name = xstrdup(refname);
1049
1050         /* skip first rule, it will always match */
1051         for (i = nr_rules - 1; i > 0 ; --i) {
1052                 int j;
1053                 int rules_to_fail = i;
1054                 int short_name_len;
1055
1056                 if (1 != sscanf(refname, scanf_fmts[i], short_name))
1057                         continue;
1058
1059                 short_name_len = strlen(short_name);
1060
1061                 /*
1062                  * in strict mode, all (except the matched one) rules
1063                  * must fail to resolve to a valid non-ambiguous ref
1064                  */
1065                 if (strict)
1066                         rules_to_fail = nr_rules;
1067
1068                 /*
1069                  * check if the short name resolves to a valid ref,
1070                  * but use only rules prior to the matched one
1071                  */
1072                 for (j = 0; j < rules_to_fail; j++) {
1073                         const char *rule = ref_rev_parse_rules[j];
1074                         char refname[PATH_MAX];
1075
1076                         /* skip matched rule */
1077                         if (i == j)
1078                                 continue;
1079
1080                         /*
1081                          * the short name is ambiguous, if it resolves
1082                          * (with this previous rule) to a valid ref
1083                          * read_ref() returns 0 on success
1084                          */
1085                         mksnpath(refname, sizeof(refname),
1086                                  rule, short_name_len, short_name);
1087                         if (ref_exists(refname))
1088                                 break;
1089                 }
1090
1091                 /*
1092                  * short name is non-ambiguous if all previous rules
1093                  * haven't resolved to a valid ref
1094                  */
1095                 if (j == rules_to_fail)
1096                         return short_name;
1097         }
1098
1099         free(short_name);
1100         return xstrdup(refname);
1101 }
1102
1103 static struct string_list *hide_refs;
1104
1105 int parse_hide_refs_config(const char *var, const char *value, const char *section)
1106 {
1107         const char *key;
1108         if (!strcmp("transfer.hiderefs", var) ||
1109             (!parse_config_key(var, section, NULL, NULL, &key) &&
1110              !strcmp(key, "hiderefs"))) {
1111                 char *ref;
1112                 int len;
1113
1114                 if (!value)
1115                         return config_error_nonbool(var);
1116                 ref = xstrdup(value);
1117                 len = strlen(ref);
1118                 while (len && ref[len - 1] == '/')
1119                         ref[--len] = '\0';
1120                 if (!hide_refs) {
1121                         hide_refs = xcalloc(1, sizeof(*hide_refs));
1122                         hide_refs->strdup_strings = 1;
1123                 }
1124                 string_list_append(hide_refs, ref);
1125         }
1126         return 0;
1127 }
1128
1129 int ref_is_hidden(const char *refname, const char *refname_full)
1130 {
1131         int i;
1132
1133         if (!hide_refs)
1134                 return 0;
1135         for (i = hide_refs->nr - 1; i >= 0; i--) {
1136                 const char *match = hide_refs->items[i].string;
1137                 const char *subject;
1138                 int neg = 0;
1139                 int len;
1140
1141                 if (*match == '!') {
1142                         neg = 1;
1143                         match++;
1144                 }
1145
1146                 if (*match == '^') {
1147                         subject = refname_full;
1148                         match++;
1149                 } else {
1150                         subject = refname;
1151                 }
1152
1153                 /* refname can be NULL when namespaces are used. */
1154                 if (!subject || !starts_with(subject, match))
1155                         continue;
1156                 len = strlen(match);
1157                 if (!subject[len] || subject[len] == '/')
1158                         return !neg;
1159         }
1160         return 0;
1161 }
1162
1163 const char *find_descendant_ref(const char *dirname,
1164                                 const struct string_list *extras,
1165                                 const struct string_list *skip)
1166 {
1167         int pos;
1168
1169         if (!extras)
1170                 return NULL;
1171
1172         /*
1173          * Look at the place where dirname would be inserted into
1174          * extras. If there is an entry at that position that starts
1175          * with dirname (remember, dirname includes the trailing
1176          * slash) and is not in skip, then we have a conflict.
1177          */
1178         for (pos = string_list_find_insert_index(extras, dirname, 0);
1179              pos < extras->nr; pos++) {
1180                 const char *extra_refname = extras->items[pos].string;
1181
1182                 if (!starts_with(extra_refname, dirname))
1183                         break;
1184
1185                 if (!skip || !string_list_has_string(skip, extra_refname))
1186                         return extra_refname;
1187         }
1188         return NULL;
1189 }
1190
1191 int refs_rename_ref_available(struct ref_store *refs,
1192                               const char *old_refname,
1193                               const char *new_refname)
1194 {
1195         struct string_list skip = STRING_LIST_INIT_NODUP;
1196         struct strbuf err = STRBUF_INIT;
1197         int ok;
1198
1199         string_list_insert(&skip, old_refname);
1200         ok = !refs_verify_refname_available(refs, new_refname,
1201                                             NULL, &skip, &err);
1202         if (!ok)
1203                 error("%s", err.buf);
1204
1205         string_list_clear(&skip, 0);
1206         strbuf_release(&err);
1207         return ok;
1208 }
1209
1210 int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1211 {
1212         struct object_id oid;
1213         int flag;
1214
1215         if (submodule) {
1216                 if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
1217                         return fn("HEAD", &oid, 0, cb_data);
1218
1219                 return 0;
1220         }
1221
1222         if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
1223                 return fn("HEAD", &oid, flag, cb_data);
1224
1225         return 0;
1226 }
1227
1228 int head_ref(each_ref_fn fn, void *cb_data)
1229 {
1230         return head_ref_submodule(NULL, fn, cb_data);
1231 }
1232
1233 /*
1234  * Call fn for each reference in the specified submodule for which the
1235  * refname begins with prefix. If trim is non-zero, then trim that
1236  * many characters off the beginning of each refname before passing
1237  * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1238  * include broken references in the iteration. If fn ever returns a
1239  * non-zero value, stop the iteration and return that value;
1240  * otherwise, return 0.
1241  */
1242 static int do_for_each_ref(struct ref_store *refs, const char *prefix,
1243                            each_ref_fn fn, int trim, int flags, void *cb_data)
1244 {
1245         struct ref_iterator *iter;
1246
1247         if (!refs)
1248                 return 0;
1249
1250         iter = refs->be->iterator_begin(refs, prefix, flags);
1251         iter = prefix_ref_iterator_begin(iter, prefix, trim);
1252
1253         return do_for_each_ref_iterator(iter, fn, cb_data);
1254 }
1255
1256 int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1257 {
1258         return do_for_each_ref(refs, "", fn, 0, 0, cb_data);
1259 }
1260
1261 int for_each_ref(each_ref_fn fn, void *cb_data)
1262 {
1263         return refs_for_each_ref(get_main_ref_store(), fn, cb_data);
1264 }
1265
1266 int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1267 {
1268         return refs_for_each_ref(get_submodule_ref_store(submodule), fn, cb_data);
1269 }
1270
1271 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
1272                          each_ref_fn fn, void *cb_data)
1273 {
1274         return do_for_each_ref(refs, prefix, fn, strlen(prefix), 0, cb_data);
1275 }
1276
1277 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1278 {
1279         return refs_for_each_ref_in(get_main_ref_store(), prefix, fn, cb_data);
1280 }
1281
1282 int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1283 {
1284         unsigned int flag = 0;
1285
1286         if (broken)
1287                 flag = DO_FOR_EACH_INCLUDE_BROKEN;
1288         return do_for_each_ref(get_main_ref_store(),
1289                                prefix, fn, 0, flag, cb_data);
1290 }
1291
1292 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1293                               each_ref_fn fn, void *cb_data)
1294 {
1295         return refs_for_each_ref_in(get_submodule_ref_store(submodule),
1296                                     prefix, fn, cb_data);
1297 }
1298
1299 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1300 {
1301         return do_for_each_ref(get_main_ref_store(),
1302                                git_replace_ref_base, fn,
1303                                strlen(git_replace_ref_base),
1304                                0, cb_data);
1305 }
1306
1307 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1308 {
1309         struct strbuf buf = STRBUF_INIT;
1310         int ret;
1311         strbuf_addf(&buf, "%srefs/", get_git_namespace());
1312         ret = do_for_each_ref(get_main_ref_store(),
1313                               buf.buf, fn, 0, 0, cb_data);
1314         strbuf_release(&buf);
1315         return ret;
1316 }
1317
1318 int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1319 {
1320         return do_for_each_ref(refs, "", fn, 0,
1321                                DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1322 }
1323
1324 int for_each_rawref(each_ref_fn fn, void *cb_data)
1325 {
1326         return refs_for_each_rawref(get_main_ref_store(), fn, cb_data);
1327 }
1328
1329 int refs_read_raw_ref(struct ref_store *ref_store,
1330                       const char *refname, unsigned char *sha1,
1331                       struct strbuf *referent, unsigned int *type)
1332 {
1333         return ref_store->be->read_raw_ref(ref_store, refname, sha1, referent, type);
1334 }
1335
1336 /* This function needs to return a meaningful errno on failure */
1337 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1338                                     const char *refname,
1339                                     int resolve_flags,
1340                                     unsigned char *sha1, int *flags)
1341 {
1342         static struct strbuf sb_refname = STRBUF_INIT;
1343         int unused_flags;
1344         int symref_count;
1345
1346         if (!flags)
1347                 flags = &unused_flags;
1348
1349         *flags = 0;
1350
1351         if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1352                 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1353                     !refname_is_safe(refname)) {
1354                         errno = EINVAL;
1355                         return NULL;
1356                 }
1357
1358                 /*
1359                  * dwim_ref() uses REF_ISBROKEN to distinguish between
1360                  * missing refs and refs that were present but invalid,
1361                  * to complain about the latter to stderr.
1362                  *
1363                  * We don't know whether the ref exists, so don't set
1364                  * REF_ISBROKEN yet.
1365                  */
1366                 *flags |= REF_BAD_NAME;
1367         }
1368
1369         for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1370                 unsigned int read_flags = 0;
1371
1372                 if (refs_read_raw_ref(refs, refname,
1373                                       sha1, &sb_refname, &read_flags)) {
1374                         *flags |= read_flags;
1375                         if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1376                                 return NULL;
1377                         hashclr(sha1);
1378                         if (*flags & REF_BAD_NAME)
1379                                 *flags |= REF_ISBROKEN;
1380                         return refname;
1381                 }
1382
1383                 *flags |= read_flags;
1384
1385                 if (!(read_flags & REF_ISSYMREF)) {
1386                         if (*flags & REF_BAD_NAME) {
1387                                 hashclr(sha1);
1388                                 *flags |= REF_ISBROKEN;
1389                         }
1390                         return refname;
1391                 }
1392
1393                 refname = sb_refname.buf;
1394                 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1395                         hashclr(sha1);
1396                         return refname;
1397                 }
1398                 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1399                         if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1400                             !refname_is_safe(refname)) {
1401                                 errno = EINVAL;
1402                                 return NULL;
1403                         }
1404
1405                         *flags |= REF_ISBROKEN | REF_BAD_NAME;
1406                 }
1407         }
1408
1409         errno = ELOOP;
1410         return NULL;
1411 }
1412
1413 /* backend functions */
1414 int refs_init_db(struct strbuf *err)
1415 {
1416         struct ref_store *refs = get_main_ref_store();
1417
1418         return refs->be->init_db(refs, err);
1419 }
1420
1421 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1422                                unsigned char *sha1, int *flags)
1423 {
1424         return refs_resolve_ref_unsafe(get_main_ref_store(), refname,
1425                                        resolve_flags, sha1, flags);
1426 }
1427
1428 int resolve_gitlink_ref(const char *submodule, const char *refname,
1429                         unsigned char *sha1)
1430 {
1431         size_t len = strlen(submodule);
1432         struct ref_store *refs;
1433         int flags;
1434
1435         while (len && submodule[len - 1] == '/')
1436                 len--;
1437
1438         if (!len)
1439                 return -1;
1440
1441         if (submodule[len]) {
1442                 /* We need to strip off one or more trailing slashes */
1443                 char *stripped = xmemdupz(submodule, len);
1444
1445                 refs = get_submodule_ref_store(stripped);
1446                 free(stripped);
1447         } else {
1448                 refs = get_submodule_ref_store(submodule);
1449         }
1450
1451         if (!refs)
1452                 return -1;
1453
1454         if (!refs_resolve_ref_unsafe(refs, refname, 0, sha1, &flags) ||
1455             is_null_sha1(sha1))
1456                 return -1;
1457         return 0;
1458 }
1459
1460 struct submodule_hash_entry
1461 {
1462         struct hashmap_entry ent; /* must be the first member! */
1463
1464         struct ref_store *refs;
1465
1466         /* NUL-terminated name of submodule: */
1467         char submodule[FLEX_ARRAY];
1468 };
1469
1470 static int submodule_hash_cmp(const void *entry, const void *entry_or_key,
1471                               const void *keydata)
1472 {
1473         const struct submodule_hash_entry *e1 = entry, *e2 = entry_or_key;
1474         const char *submodule = keydata ? keydata : e2->submodule;
1475
1476         return strcmp(e1->submodule, submodule);
1477 }
1478
1479 static struct submodule_hash_entry *alloc_submodule_hash_entry(
1480                 const char *submodule, struct ref_store *refs)
1481 {
1482         struct submodule_hash_entry *entry;
1483
1484         FLEX_ALLOC_STR(entry, submodule, submodule);
1485         hashmap_entry_init(entry, strhash(submodule));
1486         entry->refs = refs;
1487         return entry;
1488 }
1489
1490 /* A pointer to the ref_store for the main repository: */
1491 static struct ref_store *main_ref_store;
1492
1493 /* A hashmap of ref_stores, stored by submodule name: */
1494 static struct hashmap submodule_ref_stores;
1495
1496 /*
1497  * Return the ref_store instance for the specified submodule. If that
1498  * ref_store hasn't been initialized yet, return NULL.
1499  */
1500 static struct ref_store *lookup_submodule_ref_store(const char *submodule)
1501 {
1502         struct submodule_hash_entry *entry;
1503
1504         if (!submodule_ref_stores.tablesize)
1505                 /* It's initialized on demand in register_ref_store(). */
1506                 return NULL;
1507
1508         entry = hashmap_get_from_hash(&submodule_ref_stores,
1509                                       strhash(submodule), submodule);
1510         return entry ? entry->refs : NULL;
1511 }
1512
1513 /*
1514  * Create, record, and return a ref_store instance for the specified
1515  * gitdir.
1516  */
1517 static struct ref_store *ref_store_init(const char *gitdir,
1518                                         unsigned int flags)
1519 {
1520         const char *be_name = "files";
1521         struct ref_storage_be *be = find_ref_storage_backend(be_name);
1522         struct ref_store *refs;
1523
1524         if (!be)
1525                 die("BUG: reference backend %s is unknown", be_name);
1526
1527         refs = be->init(gitdir, flags);
1528         return refs;
1529 }
1530
1531 struct ref_store *get_main_ref_store(void)
1532 {
1533         if (main_ref_store)
1534                 return main_ref_store;
1535
1536         main_ref_store = ref_store_init(get_git_dir(),
1537                                         (REF_STORE_READ |
1538                                          REF_STORE_WRITE |
1539                                          REF_STORE_ODB |
1540                                          REF_STORE_MAIN));
1541         return main_ref_store;
1542 }
1543
1544 /*
1545  * Register the specified ref_store to be the one that should be used
1546  * for submodule. It is a fatal error to call this function twice for
1547  * the same submodule.
1548  */
1549 static void register_submodule_ref_store(struct ref_store *refs,
1550                                          const char *submodule)
1551 {
1552         if (!submodule_ref_stores.tablesize)
1553                 hashmap_init(&submodule_ref_stores, submodule_hash_cmp, 0);
1554
1555         if (hashmap_put(&submodule_ref_stores,
1556                         alloc_submodule_hash_entry(submodule, refs)))
1557                 die("BUG: ref_store for submodule '%s' initialized twice",
1558                     submodule);
1559 }
1560
1561 struct ref_store *get_submodule_ref_store(const char *submodule)
1562 {
1563         struct strbuf submodule_sb = STRBUF_INIT;
1564         struct ref_store *refs;
1565         int ret;
1566
1567         if (!submodule || !*submodule) {
1568                 /*
1569                  * FIXME: This case is ideally not allowed. But that
1570                  * can't happen until we clean up all the callers.
1571                  */
1572                 return get_main_ref_store();
1573         }
1574
1575         refs = lookup_submodule_ref_store(submodule);
1576         if (refs)
1577                 return refs;
1578
1579         strbuf_addstr(&submodule_sb, submodule);
1580         ret = is_nonbare_repository_dir(&submodule_sb);
1581         strbuf_release(&submodule_sb);
1582         if (!ret)
1583                 return NULL;
1584
1585         ret = submodule_to_gitdir(&submodule_sb, submodule);
1586         if (ret) {
1587                 strbuf_release(&submodule_sb);
1588                 return NULL;
1589         }
1590
1591         /* assume that add_submodule_odb() has been called */
1592         refs = ref_store_init(submodule_sb.buf,
1593                               REF_STORE_READ | REF_STORE_ODB);
1594         register_submodule_ref_store(refs, submodule);
1595
1596         strbuf_release(&submodule_sb);
1597         return refs;
1598 }
1599
1600 void base_ref_store_init(struct ref_store *refs,
1601                          const struct ref_storage_be *be)
1602 {
1603         refs->be = be;
1604 }
1605
1606 /* backend functions */
1607 int refs_pack_refs(struct ref_store *refs, unsigned int flags)
1608 {
1609         return refs->be->pack_refs(refs, flags);
1610 }
1611
1612 int refs_peel_ref(struct ref_store *refs, const char *refname,
1613                   unsigned char *sha1)
1614 {
1615         return refs->be->peel_ref(refs, refname, sha1);
1616 }
1617
1618 int peel_ref(const char *refname, unsigned char *sha1)
1619 {
1620         return refs_peel_ref(get_main_ref_store(), refname, sha1);
1621 }
1622
1623 int refs_create_symref(struct ref_store *refs,
1624                        const char *ref_target,
1625                        const char *refs_heads_master,
1626                        const char *logmsg)
1627 {
1628         return refs->be->create_symref(refs, ref_target,
1629                                        refs_heads_master,
1630                                        logmsg);
1631 }
1632
1633 int create_symref(const char *ref_target, const char *refs_heads_master,
1634                   const char *logmsg)
1635 {
1636         return refs_create_symref(get_main_ref_store(), ref_target,
1637                                   refs_heads_master, logmsg);
1638 }
1639
1640 int ref_transaction_commit(struct ref_transaction *transaction,
1641                            struct strbuf *err)
1642 {
1643         struct ref_store *refs = transaction->ref_store;
1644
1645         return refs->be->transaction_commit(refs, transaction, err);
1646 }
1647
1648 int refs_verify_refname_available(struct ref_store *refs,
1649                                   const char *refname,
1650                                   const struct string_list *extra,
1651                                   const struct string_list *skip,
1652                                   struct strbuf *err)
1653 {
1654         return refs->be->verify_refname_available(refs, refname, extra, skip, err);
1655 }
1656
1657 int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1658 {
1659         struct ref_iterator *iter;
1660
1661         iter = refs->be->reflog_iterator_begin(refs);
1662
1663         return do_for_each_ref_iterator(iter, fn, cb_data);
1664 }
1665
1666 int for_each_reflog(each_ref_fn fn, void *cb_data)
1667 {
1668         return refs_for_each_reflog(get_main_ref_store(), fn, cb_data);
1669 }
1670
1671 int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
1672                                      const char *refname,
1673                                      each_reflog_ent_fn fn,
1674                                      void *cb_data)
1675 {
1676         return refs->be->for_each_reflog_ent_reverse(refs, refname,
1677                                                      fn, cb_data);
1678 }
1679
1680 int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
1681                                 void *cb_data)
1682 {
1683         return refs_for_each_reflog_ent_reverse(get_main_ref_store(),
1684                                                 refname, fn, cb_data);
1685 }
1686
1687 int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
1688                              each_reflog_ent_fn fn, void *cb_data)
1689 {
1690         return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
1691 }
1692
1693 int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,
1694                         void *cb_data)
1695 {
1696         return refs_for_each_reflog_ent(get_main_ref_store(), refname,
1697                                         fn, cb_data);
1698 }
1699
1700 int refs_reflog_exists(struct ref_store *refs, const char *refname)
1701 {
1702         return refs->be->reflog_exists(refs, refname);
1703 }
1704
1705 int reflog_exists(const char *refname)
1706 {
1707         return refs_reflog_exists(get_main_ref_store(), refname);
1708 }
1709
1710 int refs_create_reflog(struct ref_store *refs, const char *refname,
1711                        int force_create, struct strbuf *err)
1712 {
1713         return refs->be->create_reflog(refs, refname, force_create, err);
1714 }
1715
1716 int safe_create_reflog(const char *refname, int force_create,
1717                        struct strbuf *err)
1718 {
1719         return refs_create_reflog(get_main_ref_store(), refname,
1720                                   force_create, err);
1721 }
1722
1723 int refs_delete_reflog(struct ref_store *refs, const char *refname)
1724 {
1725         return refs->be->delete_reflog(refs, refname);
1726 }
1727
1728 int delete_reflog(const char *refname)
1729 {
1730         return refs_delete_reflog(get_main_ref_store(), refname);
1731 }
1732
1733 int refs_reflog_expire(struct ref_store *refs,
1734                        const char *refname, const unsigned char *sha1,
1735                        unsigned int flags,
1736                        reflog_expiry_prepare_fn prepare_fn,
1737                        reflog_expiry_should_prune_fn should_prune_fn,
1738                        reflog_expiry_cleanup_fn cleanup_fn,
1739                        void *policy_cb_data)
1740 {
1741         return refs->be->reflog_expire(refs, refname, sha1, flags,
1742                                        prepare_fn, should_prune_fn,
1743                                        cleanup_fn, policy_cb_data);
1744 }
1745
1746 int reflog_expire(const char *refname, const unsigned char *sha1,
1747                   unsigned int flags,
1748                   reflog_expiry_prepare_fn prepare_fn,
1749                   reflog_expiry_should_prune_fn should_prune_fn,
1750                   reflog_expiry_cleanup_fn cleanup_fn,
1751                   void *policy_cb_data)
1752 {
1753         return refs_reflog_expire(get_main_ref_store(),
1754                                   refname, sha1, flags,
1755                                   prepare_fn, should_prune_fn,
1756                                   cleanup_fn, policy_cb_data);
1757 }
1758
1759 int initial_ref_transaction_commit(struct ref_transaction *transaction,
1760                                    struct strbuf *err)
1761 {
1762         struct ref_store *refs = transaction->ref_store;
1763
1764         return refs->be->initial_transaction_commit(refs, transaction, err);
1765 }
1766
1767 int refs_delete_refs(struct ref_store *refs, struct string_list *refnames,
1768                      unsigned int flags)
1769 {
1770         return refs->be->delete_refs(refs, refnames, flags);
1771 }
1772
1773 int delete_refs(struct string_list *refnames, unsigned int flags)
1774 {
1775         return refs_delete_refs(get_main_ref_store(), refnames, flags);
1776 }
1777
1778 int refs_rename_ref(struct ref_store *refs, const char *oldref,
1779                     const char *newref, const char *logmsg)
1780 {
1781         return refs->be->rename_ref(refs, oldref, newref, logmsg);
1782 }
1783
1784 int rename_ref(const char *oldref, const char *newref, const char *logmsg)
1785 {
1786         return refs_rename_ref(get_main_ref_store(), oldref, newref, logmsg);
1787 }