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