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