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