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