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