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