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