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