]> git.ipfire.org Git - thirdparty/git.git/blame - refs.c
treewide: remove unnecessary includes of cache.h
[thirdparty/git.git] / refs.c
CommitLineData
7bd9bcf3
MH
1/*
2 * The backend-independent part of the reference module.
3 */
4
a6dc3d36 5#include "cache.h"
36bf1958 6#include "alloc.h"
b2141fc1 7#include "config.h"
7d4558c4 8#include "hashmap.h"
f394e093 9#include "gettext.h"
41771fa4 10#include "hex.h"
697cc8ef 11#include "lockfile.h"
b05855b5 12#include "iterator.h"
85023577 13#include "refs.h"
4cb77009 14#include "refs/refs-internal.h"
67541597 15#include "run-command.h"
5e3aba33 16#include "hook.h"
cbd53a21 17#include "object-store.h"
cf0adba7
JH
18#include "object.h"
19#include "tag.h"
5d0bc90e 20#include "submodule.h"
17eff96b 21#include "worktree.h"
dbbcd44f 22#include "strvec.h"
23a3f0cb 23#include "repository.h"
67541597 24#include "sigchain.h"
88c7b4c3 25#include "date.h"
b9342b3f 26#include "commit.h"
d5ebb50d 27#include "wrapper.h"
3581d793 28
3dce444f
RS
29/*
30 * List of all available backends
31 */
32static struct ref_storage_be *refs_backends = &refs_be_files;
33
34static struct ref_storage_be *find_ref_storage_backend(const char *name)
35{
36 struct ref_storage_be *be;
37 for (be = refs_backends; be; be = be->next)
38 if (!strcmp(be->name, name))
39 return be;
40 return NULL;
41}
42
bc5fd6d3 43/*
dde8a902
DT
44 * How to handle various characters in refnames:
45 * 0: An acceptable character for refs
5e650228
JH
46 * 1: End-of-component
47 * 2: ., look for a preceding . to reject .. in refs
48 * 3: {, look for a preceding @ to reject @{ in refs
53a8555e 49 * 4: A bad character: ASCII control characters, and
cd377f45
JK
50 * ":", "?", "[", "\", "^", "~", SP, or TAB
51 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
dde8a902
DT
52 */
53static unsigned char refname_disposition[256] = {
5e650228
JH
54 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
55 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
cd377f45 56 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
5e650228 57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
dde8a902 58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5e650228
JH
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
dde8a902
DT
62};
63
b9342b3f
DS
64struct ref_namespace_info ref_namespace[] = {
65 [NAMESPACE_HEAD] = {
66 .ref = "HEAD",
67 .decoration = DECORATION_REF_HEAD,
68 .exact = 1,
69 },
70 [NAMESPACE_BRANCHES] = {
71 .ref = "refs/heads/",
72 .decoration = DECORATION_REF_LOCAL,
73 },
74 [NAMESPACE_TAGS] = {
75 .ref = "refs/tags/",
76 .decoration = DECORATION_REF_TAG,
77 },
78 [NAMESPACE_REMOTE_REFS] = {
79 /*
80 * The default refspec for new remotes copies refs from
81 * refs/heads/ on the remote into refs/remotes/<remote>/.
82 * As such, "refs/remotes/" has special handling.
83 */
84 .ref = "refs/remotes/",
85 .decoration = DECORATION_REF_REMOTE,
86 },
87 [NAMESPACE_STASH] = {
88 /*
89 * The single ref "refs/stash" stores the latest stash.
90 * Older stashes can be found in the reflog.
91 */
92 .ref = "refs/stash",
93 .exact = 1,
94 .decoration = DECORATION_REF_STASH,
95 },
96 [NAMESPACE_REPLACE] = {
97 /*
98 * This namespace allows Git to act as if one object ID
99 * points to the content of another. Unlike the other
100 * ref namespaces, this one can be changed by the
101 * GIT_REPLACE_REF_BASE environment variable. This
102 * .namespace value will be overwritten in setup_git_env().
103 */
104 .ref = "refs/replace/",
105 .decoration = DECORATION_GRAFTED,
106 },
107 [NAMESPACE_NOTES] = {
108 /*
109 * The refs/notes/commit ref points to the tip of a
110 * parallel commit history that adds metadata to commits
111 * in the normal history. This ref can be overwritten
112 * by the core.notesRef config variable or the
113 * GIT_NOTES_REFS environment variable.
114 */
115 .ref = "refs/notes/commit",
116 .exact = 1,
117 },
118 [NAMESPACE_PREFETCH] = {
119 /*
120 * Prefetch refs are written by the background 'fetch'
121 * maintenance task. It allows faster foreground fetches
122 * by advertising these previously-downloaded tips without
123 * updating refs/remotes/ without user intervention.
124 */
125 .ref = "refs/prefetch/",
126 },
127 [NAMESPACE_REWRITTEN] = {
128 /*
129 * Rewritten refs are used by the 'label' command in the
130 * sequencer. These are particularly useful during an
131 * interactive rebase that uses the 'merge' command.
132 */
133 .ref = "refs/rewritten/",
134 },
135};
136
137void update_ref_namespace(enum ref_namespace namespace, char *ref)
138{
139 struct ref_namespace_info *info = &ref_namespace[namespace];
140 if (info->ref_updated)
141 free(info->ref);
142 info->ref = ref;
143 info->ref_updated = 1;
144}
145
dde8a902
DT
146/*
147 * Try to read one refname component from the front of refname.
148 * Return the length of the component found, or -1 if the component is
149 * not legal. It is legal if it is something reasonable to have under
150 * ".git/refs/"; We do not like it if:
bc5fd6d3 151 *
1de16aec 152 * - it begins with ".", or
bc5fd6d3 153 * - it has double dots "..", or
53a8555e 154 * - it has ASCII control characters, or
cd377f45
JK
155 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
156 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
53a8555e
JK
157 * - it ends with a "/", or
158 * - it ends with ".lock", or
159 * - it contains a "@{" portion
1de16aec
NTND
160 *
161 * When sanitized is not NULL, instead of rejecting the input refname
162 * as an error, try to come up with a usable replacement for the input
163 * refname in it.
bc5fd6d3 164 */
1de16aec
NTND
165static int check_refname_component(const char *refname, int *flags,
166 struct strbuf *sanitized)
bc5fd6d3
MH
167{
168 const char *cp;
169 char last = '\0';
1de16aec
NTND
170 size_t component_start = 0; /* garbage - not a reasonable initial value */
171
172 if (sanitized)
173 component_start = sanitized->len;
bc5fd6d3
MH
174
175 for (cp = refname; ; cp++) {
dde8a902
DT
176 int ch = *cp & 255;
177 unsigned char disp = refname_disposition[ch];
1de16aec
NTND
178
179 if (sanitized && disp != 1)
180 strbuf_addch(sanitized, ch);
181
dde8a902 182 switch (disp) {
5e650228 183 case 1:
dde8a902 184 goto out;
5e650228 185 case 2:
1de16aec
NTND
186 if (last == '.') { /* Refname contains "..". */
187 if (sanitized)
188 /* collapse ".." to single "." */
189 strbuf_setlen(sanitized, sanitized->len - 1);
190 else
191 return -1;
192 }
dde8a902 193 break;
5e650228 194 case 3:
1de16aec
NTND
195 if (last == '@') { /* Refname contains "@{". */
196 if (sanitized)
197 sanitized->buf[sanitized->len-1] = '-';
198 else
199 return -1;
200 }
bc5fd6d3 201 break;
5e650228 202 case 4:
1de16aec
NTND
203 /* forbidden char */
204 if (sanitized)
205 sanitized->buf[sanitized->len-1] = '-';
206 else
207 return -1;
208 break;
cd377f45 209 case 5:
1de16aec
NTND
210 if (!(*flags & REFNAME_REFSPEC_PATTERN)) {
211 /* refspec can't be a pattern */
212 if (sanitized)
213 sanitized->buf[sanitized->len-1] = '-';
214 else
215 return -1;
216 }
cd377f45
JK
217
218 /*
219 * Unset the pattern flag so that we only accept
220 * a single asterisk for one side of refspec.
221 */
222 *flags &= ~ REFNAME_REFSPEC_PATTERN;
223 break;
dde8a902 224 }
bc5fd6d3
MH
225 last = ch;
226 }
dde8a902 227out:
bc5fd6d3 228 if (cp == refname)
dac529e4 229 return 0; /* Component has zero length. */
1de16aec
NTND
230
231 if (refname[0] == '.') { /* Component starts with '.'. */
232 if (sanitized)
233 sanitized->buf[component_start] = '-';
234 else
235 return -1;
236 }
7108ad23 237 if (cp - refname >= LOCK_SUFFIX_LEN &&
1de16aec
NTND
238 !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) {
239 if (!sanitized)
240 return -1;
241 /* Refname ends with ".lock". */
242 while (strbuf_strip_suffix(sanitized, LOCK_SUFFIX)) {
243 /* try again in case we have .lock.lock */
244 }
245 }
bc5fd6d3
MH
246 return cp - refname;
247}
248
1de16aec
NTND
249static int check_or_sanitize_refname(const char *refname, int flags,
250 struct strbuf *sanitized)
bc5fd6d3
MH
251{
252 int component_len, component_count = 0;
253
1de16aec 254 if (!strcmp(refname, "@")) {
9ba89f48 255 /* Refname is a single character '@'. */
1de16aec
NTND
256 if (sanitized)
257 strbuf_addch(sanitized, '-');
258 else
259 return -1;
260 }
9ba89f48 261
bc5fd6d3 262 while (1) {
1de16aec
NTND
263 if (sanitized && sanitized->len)
264 strbuf_complete(sanitized, '/');
265
bc5fd6d3 266 /* We are at the start of a path component. */
1de16aec
NTND
267 component_len = check_refname_component(refname, &flags,
268 sanitized);
269 if (sanitized && component_len == 0)
270 ; /* OK, omit empty component */
271 else if (component_len <= 0)
cd377f45
JK
272 return -1;
273
bc5fd6d3
MH
274 component_count++;
275 if (refname[component_len] == '\0')
276 break;
277 /* Skip to next component. */
278 refname += component_len + 1;
279 }
280
1de16aec
NTND
281 if (refname[component_len - 1] == '.') {
282 /* Refname ends with '.'. */
283 if (sanitized)
284 ; /* omit ending dot */
285 else
286 return -1;
287 }
bc5fd6d3
MH
288 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
289 return -1; /* Refname has only one component. */
290 return 0;
291}
292
1de16aec
NTND
293int check_refname_format(const char *refname, int flags)
294{
295 return check_or_sanitize_refname(refname, flags, NULL);
296}
297
298void sanitize_refname_component(const char *refname, struct strbuf *out)
299{
300 if (check_or_sanitize_refname(refname, REFNAME_ALLOW_ONELEVEL, out))
301 BUG("sanitizing refname '%s' check returned error", refname);
302}
303
4cb77009 304int refname_is_safe(const char *refname)
d0f810f0 305{
39950fef
MH
306 const char *rest;
307
308 if (skip_prefix(refname, "refs/", &rest)) {
d0f810f0
RS
309 char *buf;
310 int result;
e40f3557
MH
311 size_t restlen = strlen(rest);
312
313 /* rest must not be empty, or start or end with "/" */
314 if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
315 return 0;
d0f810f0 316
d0f810f0
RS
317 /*
318 * Does the refname try to escape refs/?
319 * For example: refs/foo/../bar is safe but refs/foo/../../bar
320 * is not.
321 */
e40f3557
MH
322 buf = xmallocz(restlen);
323 result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
d0f810f0
RS
324 free(buf);
325 return result;
326 }
35db25c6
MH
327
328 do {
d0f810f0
RS
329 if (!isupper(*refname) && *refname != '_')
330 return 0;
331 refname++;
35db25c6 332 } while (*refname);
d0f810f0
RS
333 return 1;
334}
335
67be7c5a
MH
336/*
337 * Return true if refname, which has the specified oid and flags, can
338 * be resolved to an object in the database. If the referred-to object
339 * does not exist, emit a warning and return false.
340 */
341int ref_resolves_to_object(const char *refname,
9bc45a28 342 struct repository *repo,
67be7c5a
MH
343 const struct object_id *oid,
344 unsigned int flags)
345{
346 if (flags & REF_ISBROKEN)
347 return 0;
9bc45a28 348 if (!repo_has_object_file(repo, oid)) {
661558f0 349 error(_("%s does not point to a valid object!"), refname);
67be7c5a
MH
350 return 0;
351 }
352 return 1;
353}
354
7d2df051
NTND
355char *refs_resolve_refdup(struct ref_store *refs,
356 const char *refname, int resolve_flags,
0f2dc722 357 struct object_id *oid, int *flags)
7d2df051
NTND
358{
359 const char *result;
360
361 result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
ce14de03 362 oid, flags);
7d2df051
NTND
363 return xstrdup_or_null(result);
364}
365
7bd9bcf3 366char *resolve_refdup(const char *refname, int resolve_flags,
0f2dc722 367 struct object_id *oid, int *flags)
e1e22e37 368{
23a3f0cb 369 return refs_resolve_refdup(get_main_ref_store(the_repository),
7d2df051 370 refname, resolve_flags,
0f2dc722 371 oid, flags);
cddc4258
MH
372}
373
7bd9bcf3
MH
374/* The argument to filter_refs */
375struct ref_filter {
376 const char *pattern;
9ab9b5df 377 const char *prefix;
7bd9bcf3
MH
378 each_ref_fn *fn;
379 void *cb_data;
380};
432ad41e 381
76887df0 382int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags)
732134ed 383{
76887df0
ÆAB
384 struct ref_store *refs = get_main_ref_store(the_repository);
385
f1da24ca 386 if (refs_resolve_ref_unsafe(refs, refname, resolve_flags,
ce14de03 387 oid, flags))
7bd9bcf3
MH
388 return 0;
389 return -1;
732134ed
MH
390}
391
34c290a6 392int read_ref(const char *refname, struct object_id *oid)
cddc4258 393{
34c290a6 394 return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL);
c774aab9
JP
395}
396
3f9f1acc 397int refs_ref_exists(struct ref_store *refs, const char *refname)
b3cd33d0 398{
f1da24ca 399 return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING,
ce14de03 400 NULL, NULL);
b3cd33d0
NTND
401}
402
7bd9bcf3 403int ref_exists(const char *refname)
bc5fd6d3 404{
b3cd33d0 405 return refs_ref_exists(get_main_ref_store(the_repository), refname);
bc5fd6d3
MH
406}
407
7bd9bcf3
MH
408static int filter_refs(const char *refname, const struct object_id *oid,
409 int flags, void *data)
432ad41e 410{
7bd9bcf3
MH
411 struct ref_filter *filter = (struct ref_filter *)data;
412
55d34269 413 if (wildmatch(filter->pattern, refname, 0))
7bd9bcf3 414 return 0;
9ab9b5df
RA
415 if (filter->prefix)
416 skip_prefix(refname, filter->prefix, &refname);
7bd9bcf3 417 return filter->fn(refname, oid, flags, filter->cb_data);
432ad41e
MH
418}
419
ac2ed0d7 420enum peel_status peel_object(const struct object_id *name, struct object_id *oid)
c774aab9 421{
45a187cc 422 struct object *o = lookup_unknown_object(the_repository, name);
c774aab9 423
7bd9bcf3 424 if (o->type == OBJ_NONE) {
0df8e965 425 int type = oid_object_info(the_repository, name, NULL);
6da43d93 426 if (type < 0 || !object_as_type(o, type, 0))
7bd9bcf3
MH
427 return PEEL_INVALID;
428 }
bc5fd6d3 429
7bd9bcf3
MH
430 if (o->type != OBJ_TAG)
431 return PEEL_NON_TAG;
e1980c9d 432
7bd9bcf3
MH
433 o = deref_tag_noverify(o);
434 if (!o)
435 return PEEL_INVALID;
436
ac2ed0d7 437 oidcpy(oid, &o->oid);
7bd9bcf3 438 return PEEL_PEELED;
e1980c9d
JH
439}
440
7bd9bcf3
MH
441struct warn_if_dangling_data {
442 FILE *fp;
443 const char *refname;
444 const struct string_list *refnames;
445 const char *msg_fmt;
446};
bc5fd6d3 447
63e14ee2 448static int warn_if_dangling_symref(const char *refname,
5cf88fd8 449 const struct object_id *oid UNUSED,
7bd9bcf3
MH
450 int flags, void *cb_data)
451{
452 struct warn_if_dangling_data *d = cb_data;
453 const char *resolves_to;
bc5fd6d3 454
7bd9bcf3
MH
455 if (!(flags & REF_ISSYMREF))
456 return 0;
bc5fd6d3 457
744c040b 458 resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL);
7bd9bcf3
MH
459 if (!resolves_to
460 || (d->refname
461 ? strcmp(resolves_to, d->refname)
462 : !string_list_has_string(d->refnames, resolves_to))) {
463 return 0;
464 }
bc5fd6d3 465
7bd9bcf3
MH
466 fprintf(d->fp, d->msg_fmt, refname);
467 fputc('\n', d->fp);
468 return 0;
bc5fd6d3
MH
469}
470
7bd9bcf3 471void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
f348ac92 472{
7bd9bcf3
MH
473 struct warn_if_dangling_data data;
474
475 data.fp = fp;
476 data.refname = refname;
477 data.refnames = NULL;
478 data.msg_fmt = msg_fmt;
479 for_each_rawref(warn_if_dangling_symref, &data);
f348ac92
MH
480}
481
7bd9bcf3 482void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
432ad41e 483{
7bd9bcf3 484 struct warn_if_dangling_data data;
432ad41e 485
7bd9bcf3
MH
486 data.fp = fp;
487 data.refname = NULL;
488 data.refnames = refnames;
489 data.msg_fmt = msg_fmt;
490 for_each_rawref(warn_if_dangling_symref, &data);
432ad41e
MH
491}
492
7d2df051
NTND
493int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
494{
495 return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data);
496}
497
7bd9bcf3 498int for_each_tag_ref(each_ref_fn fn, void *cb_data)
432ad41e 499{
23a3f0cb 500 return refs_for_each_tag_ref(get_main_ref_store(the_repository), fn, cb_data);
432ad41e
MH
501}
502
7d2df051
NTND
503int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
504{
505 return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data);
432ad41e
MH
506}
507
7bd9bcf3 508int for_each_branch_ref(each_ref_fn fn, void *cb_data)
432ad41e 509{
23a3f0cb 510 return refs_for_each_branch_ref(get_main_ref_store(the_repository), fn, cb_data);
432ad41e
MH
511}
512
7d2df051
NTND
513int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
514{
515 return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data);
202a56a9 516}
432ad41e 517
7bd9bcf3 518int for_each_remote_ref(each_ref_fn fn, void *cb_data)
e9c4c111 519{
23a3f0cb 520 return refs_for_each_remote_ref(get_main_ref_store(the_repository), fn, cb_data);
202a56a9
MH
521}
522
7bd9bcf3
MH
523int head_ref_namespaced(each_ref_fn fn, void *cb_data)
524{
525 struct strbuf buf = STRBUF_INIT;
526 int ret = 0;
527 struct object_id oid;
528 int flag;
c774aab9 529
7bd9bcf3 530 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
34c290a6 531 if (!read_ref_full(buf.buf, RESOLVE_REF_READING, &oid, &flag))
7bd9bcf3
MH
532 ret = fn(buf.buf, &oid, flag, cb_data);
533 strbuf_release(&buf);
c774aab9 534
7bd9bcf3 535 return ret;
e9c4c111 536}
c774aab9 537
65516f58
RA
538void normalize_glob_ref(struct string_list_item *item, const char *prefix,
539 const char *pattern)
540{
541 struct strbuf normalized_pattern = STRBUF_INIT;
542
543 if (*pattern == '/')
544 BUG("pattern must not start with '/'");
545
b877e617 546 if (prefix)
65516f58 547 strbuf_addstr(&normalized_pattern, prefix);
b877e617
DS
548 else if (!starts_with(pattern, "refs/") &&
549 strcmp(pattern, "HEAD"))
65516f58 550 strbuf_addstr(&normalized_pattern, "refs/");
b877e617
DS
551 /*
552 * NEEDSWORK: Special case other symrefs such as REBASE_HEAD,
553 * MERGE_HEAD, etc.
554 */
555
65516f58
RA
556 strbuf_addstr(&normalized_pattern, pattern);
557 strbuf_strip_suffix(&normalized_pattern, "/");
558
559 item->string = strbuf_detach(&normalized_pattern, NULL);
560 item->util = has_glob_specials(pattern) ? NULL : item->string;
561 strbuf_release(&normalized_pattern);
562}
563
7bd9bcf3
MH
564int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
565 const char *prefix, void *cb_data)
662428f4 566{
7bd9bcf3
MH
567 struct strbuf real_pattern = STRBUF_INIT;
568 struct ref_filter filter;
569 int ret;
d08bae7e 570
59556548 571 if (!prefix && !starts_with(pattern, "refs/"))
d08bae7e 572 strbuf_addstr(&real_pattern, "refs/");
b09fe971
IL
573 else if (prefix)
574 strbuf_addstr(&real_pattern, prefix);
d08bae7e
IL
575 strbuf_addstr(&real_pattern, pattern);
576
894a9d33 577 if (!has_glob_specials(pattern)) {
9517e6b8 578 /* Append implied '/' '*' if not present. */
00b6c178 579 strbuf_complete(&real_pattern, '/');
d08bae7e
IL
580 /* No need to check for '*', there is none. */
581 strbuf_addch(&real_pattern, '*');
582 }
583
584 filter.pattern = real_pattern.buf;
9ab9b5df 585 filter.prefix = prefix;
d08bae7e
IL
586 filter.fn = fn;
587 filter.cb_data = cb_data;
588 ret = for_each_ref(filter_refs, &filter);
589
590 strbuf_release(&real_pattern);
591 return ret;
592}
593
b09fe971
IL
594int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
595{
596 return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
597}
598
4577e483 599const char *prettify_refname(const char *name)
a9c37a72 600{
3e5b36c6
SG
601 if (skip_prefix(name, "refs/heads/", &name) ||
602 skip_prefix(name, "refs/tags/", &name) ||
603 skip_prefix(name, "refs/remotes/", &name))
604 ; /* nothing */
605 return name;
a9c37a72
DB
606}
607
54457fe5 608static const char *ref_rev_parse_rules[] = {
79803322
SP
609 "%.*s",
610 "refs/%.*s",
611 "refs/tags/%.*s",
612 "refs/heads/%.*s",
613 "refs/remotes/%.*s",
614 "refs/remotes/%.*s/HEAD",
615 NULL
616};
617
60650a48
JH
618#define NUM_REV_PARSE_RULES (ARRAY_SIZE(ref_rev_parse_rules) - 1)
619
620/*
621 * Is it possible that the caller meant full_name with abbrev_name?
622 * If so return a non-zero value to signal "yes"; the magnitude of
623 * the returned value gives the precedence used for disambiguation.
624 *
625 * If abbrev_name cannot mean full_name, return 0.
626 */
54457fe5 627int refname_match(const char *abbrev_name, const char *full_name)
79803322
SP
628{
629 const char **p;
630 const int abbrev_name_len = strlen(abbrev_name);
60650a48 631 const int num_rules = NUM_REV_PARSE_RULES;
79803322 632
60650a48
JH
633 for (p = ref_rev_parse_rules; *p; p++)
634 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name)))
635 return &ref_rev_parse_rules[num_rules] - p;
79803322
SP
636
637 return 0;
638}
639
b4be7410
BW
640/*
641 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
642 * the results to 'prefixes'
643 */
c972bf4c 644void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
b4be7410
BW
645{
646 const char **p;
647 int len = strlen(prefix);
648
649 for (p = ref_rev_parse_rules; *p; p++)
c972bf4c 650 strvec_pushf(prefixes, *p, len, prefix);
b4be7410
BW
651}
652
675704c7
JS
653static const char default_branch_name_advice[] = N_(
654"Using '%s' as the name for the initial branch. This default branch name\n"
655"is subject to change. To configure the initial branch name to use in all\n"
656"of your new repositories, which will suppress this warning, call:\n"
657"\n"
658"\tgit config --global init.defaultBranch <name>\n"
659"\n"
660"Names commonly chosen instead of 'master' are 'main', 'trunk' and\n"
661"'development'. The just-created branch can be renamed via this command:\n"
662"\n"
663"\tgit branch -m <name>\n"
664);
665
cc0f13c5 666char *repo_default_branch_name(struct repository *r, int quiet)
8747ebb7
DGW
667{
668 const char *config_key = "init.defaultbranch";
669 const char *config_display_key = "init.defaultBranch";
670 char *ret = NULL, *full_ref;
704fed9e 671 const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME");
8747ebb7 672
704fed9e
JS
673 if (env && *env)
674 ret = xstrdup(env);
675 else if (repo_config_get_string(r, config_key, &ret) < 0)
8747ebb7
DGW
676 die(_("could not retrieve `%s`"), config_display_key);
677
675704c7 678 if (!ret) {
8747ebb7 679 ret = xstrdup("master");
675704c7
JS
680 if (!quiet)
681 advise(_(default_branch_name_advice), ret);
682 }
8747ebb7
DGW
683
684 full_ref = xstrfmt("refs/heads/%s", ret);
685 if (check_refname_format(full_ref, 0))
686 die(_("invalid branch name: %s = %s"), config_display_key, ret);
687 free(full_ref);
688
689 return ret;
690}
691
cc0f13c5 692const char *git_default_branch_name(int quiet)
8747ebb7
DGW
693{
694 static char *ret;
695
696 if (!ret)
cc0f13c5 697 ret = repo_default_branch_name(the_repository, quiet);
8747ebb7
DGW
698
699 return ret;
700}
701
ff74f7f1
JH
702/*
703 * *string and *len will only be substituted, and *string returned (for
704 * later free()ing) if the string passed in is a magic short-hand form
705 * to name a branch.
706 */
8f56e9d4 707static char *substitute_branch_name(struct repository *r,
f24c30e0
JT
708 const char **string, int *len,
709 int nonfatal_dangling_mark)
ff74f7f1
JH
710{
711 struct strbuf buf = STRBUF_INIT;
f24c30e0
JT
712 struct interpret_branch_name_options options = {
713 .nonfatal_dangling_mark = nonfatal_dangling_mark
714 };
a4f66a78 715 int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options);
ff74f7f1
JH
716
717 if (ret == *len) {
718 size_t size;
719 *string = strbuf_detach(&buf, &size);
720 *len = size;
721 return (char *)*string;
722 }
723
724 return NULL;
725}
726
d8984c53 727int repo_dwim_ref(struct repository *r, const char *str, int len,
f24c30e0 728 struct object_id *oid, char **ref, int nonfatal_dangling_mark)
ff74f7f1 729{
f24c30e0
JT
730 char *last_branch = substitute_branch_name(r, &str, &len,
731 nonfatal_dangling_mark);
d8984c53 732 int refs_found = expand_ref(r, str, len, oid, ref);
41da7111
NTND
733 free(last_branch);
734 return refs_found;
735}
736
0b1dbf53
NTND
737int expand_ref(struct repository *repo, const char *str, int len,
738 struct object_id *oid, char **ref)
41da7111 739{
ff74f7f1
JH
740 const char **p, *r;
741 int refs_found = 0;
6cd4a898 742 struct strbuf fullref = STRBUF_INIT;
ff74f7f1
JH
743
744 *ref = NULL;
745 for (p = ref_rev_parse_rules; *p; p++) {
cca5fa64 746 struct object_id oid_from_ref;
747 struct object_id *this_result;
ff74f7f1 748 int flag;
6582bd31 749 struct ref_store *refs = get_main_ref_store(repo);
ff74f7f1 750
cca5fa64 751 this_result = refs_found ? &oid_from_ref : oid;
6cd4a898
JK
752 strbuf_reset(&fullref);
753 strbuf_addf(&fullref, *p, len, str);
f1da24ca 754 r = refs_resolve_ref_unsafe(refs, fullref.buf,
6582bd31 755 RESOLVE_REF_READING,
ce14de03 756 this_result, &flag);
ff74f7f1
JH
757 if (r) {
758 if (!refs_found++)
759 *ref = xstrdup(r);
760 if (!warn_ambiguous_refs)
761 break;
6cd4a898 762 } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
661558f0 763 warning(_("ignoring dangling symref %s"), fullref.buf);
6cd4a898 764 } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) {
661558f0 765 warning(_("ignoring broken ref %s"), fullref.buf);
55956350 766 }
ff74f7f1 767 }
6cd4a898 768 strbuf_release(&fullref);
ff74f7f1
JH
769 return refs_found;
770}
771
56700903
NTND
772int repo_dwim_log(struct repository *r, const char *str, int len,
773 struct object_id *oid, char **log)
ff74f7f1 774{
56700903 775 struct ref_store *refs = get_main_ref_store(r);
f24c30e0 776 char *last_branch = substitute_branch_name(r, &str, &len, 0);
ff74f7f1
JH
777 const char **p;
778 int logs_found = 0;
6cd4a898 779 struct strbuf path = STRBUF_INIT;
ff74f7f1
JH
780
781 *log = NULL;
782 for (p = ref_rev_parse_rules; *p; p++) {
334dc52f 783 struct object_id hash;
ff74f7f1
JH
784 const char *ref, *it;
785
6cd4a898
JK
786 strbuf_reset(&path);
787 strbuf_addf(&path, *p, len, str);
56700903
NTND
788 ref = refs_resolve_ref_unsafe(refs, path.buf,
789 RESOLVE_REF_READING,
ce14de03 790 oid ? &hash : NULL, NULL);
ff74f7f1
JH
791 if (!ref)
792 continue;
56700903 793 if (refs_reflog_exists(refs, path.buf))
6cd4a898 794 it = path.buf;
56700903
NTND
795 else if (strcmp(ref, path.buf) &&
796 refs_reflog_exists(refs, ref))
ff74f7f1
JH
797 it = ref;
798 else
799 continue;
800 if (!logs_found++) {
801 *log = xstrdup(it);
6f45ec88
ÆAB
802 if (oid)
803 oidcpy(oid, &hash);
ff74f7f1 804 }
7bd9bcf3
MH
805 if (!warn_ambiguous_refs)
806 break;
c0277d15 807 }
6cd4a898 808 strbuf_release(&path);
7bd9bcf3
MH
809 free(last_branch);
810 return logs_found;
2ddb5d17
BK
811}
812
56700903
NTND
813int dwim_log(const char *str, int len, struct object_id *oid, char **log)
814{
815 return repo_dwim_log(the_repository, str, len, oid, log);
816}
817
71e54734 818int is_per_worktree_ref(const char *refname)
266b1827 819{
55dd8b91
HWN
820 return starts_with(refname, "refs/worktree/") ||
821 starts_with(refname, "refs/bisect/") ||
822 starts_with(refname, "refs/rewritten/");
266b1827
DT
823}
824
825static int is_pseudoref_syntax(const char *refname)
826{
827 const char *c;
828
829 for (c = refname; *c; c++) {
830 if (!isupper(*c) && *c != '-' && *c != '_')
831 return 0;
832 }
833
71e54734
HWN
834 /*
835 * HEAD is not a pseudoref, but it certainly uses the
836 * pseudoref syntax.
837 */
266b1827
DT
838 return 1;
839}
840
71e54734
HWN
841static int is_current_worktree_ref(const char *ref) {
842 return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref);
3a3b9d8c
NTND
843}
844
71e54734
HWN
845enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
846 const char **worktree_name, int *worktree_name_length,
847 const char **bare_refname)
3a3b9d8c 848{
71e54734
HWN
849 const char *name_dummy;
850 int name_length_dummy;
851 const char *ref_dummy;
3a3b9d8c 852
71e54734
HWN
853 if (!worktree_name)
854 worktree_name = &name_dummy;
855 if (!worktree_name_length)
856 worktree_name_length = &name_length_dummy;
857 if (!bare_refname)
858 bare_refname = &ref_dummy;
859
860 if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) {
861 const char *slash = strchr(*bare_refname, '/');
862
863 *worktree_name = *bare_refname;
864 if (!slash) {
865 *worktree_name_length = strlen(*worktree_name);
866
867 /* This is an error condition, and the caller tell because the bare_refname is "" */
868 *bare_refname = *worktree_name + *worktree_name_length;
869 return REF_WORKTREE_OTHER;
870 }
871
872 *worktree_name_length = slash - *bare_refname;
873 *bare_refname = slash + 1;
874
875 if (is_current_worktree_ref(*bare_refname))
876 return REF_WORKTREE_OTHER;
877 }
878
879 *worktree_name = NULL;
880 *worktree_name_length = 0;
881
882 if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname)
883 && is_current_worktree_ref(*bare_refname))
884 return REF_WORKTREE_MAIN;
885
886 *bare_refname = maybe_worktree_ref;
887 if (is_current_worktree_ref(maybe_worktree_ref))
888 return REF_WORKTREE_CURRENT;
889
890 return REF_WORKTREE_SHARED;
266b1827
DT
891}
892
4ff0f01c
MH
893long get_files_ref_lock_timeout_ms(void)
894{
895 static int configured = 0;
896
897 /* The default timeout is 100 ms: */
898 static int timeout_ms = 100;
899
900 if (!configured) {
901 git_config_get_int("core.filesreflocktimeout", &timeout_ms);
902 configured = 1;
903 }
904
905 return timeout_ms;
906}
907
c0fe4e8b
NTND
908int refs_delete_ref(struct ref_store *refs, const char *msg,
909 const char *refname,
2616a5e5 910 const struct object_id *old_oid,
c0fe4e8b 911 unsigned int flags)
41b625b0 912{
7521cc46 913 struct ref_transaction *transaction;
a4c653df 914 struct strbuf err = STRBUF_INIT;
8b5157e4 915
c6da34a6 916 transaction = ref_store_transaction_begin(refs, &err);
7521cc46 917 if (!transaction ||
89f3bbdd 918 ref_transaction_delete(transaction, refname, old_oid,
755b49ae 919 flags, msg, &err) ||
db7516ab 920 ref_transaction_commit(transaction, &err)) {
7521cc46
RS
921 error("%s", err.buf);
922 ref_transaction_free(transaction);
923 strbuf_release(&err);
c0277d15 924 return 1;
41b625b0 925 }
7bd9bcf3
MH
926 ref_transaction_free(transaction);
927 strbuf_release(&err);
b531394d
BC
928 return 0;
929}
41b625b0 930
c0fe4e8b 931int delete_ref(const char *msg, const char *refname,
2616a5e5 932 const struct object_id *old_oid, unsigned int flags)
c0fe4e8b 933{
23a3f0cb 934 return refs_delete_ref(get_main_ref_store(the_repository), msg, refname,
2616a5e5 935 old_oid, flags);
c0fe4e8b
NTND
936}
937
523fa69c 938static void copy_reflog_msg(struct strbuf *sb, const char *msg)
0ec29a47 939{
0ec29a47
JH
940 char c;
941 int wasspace = 1;
8b5157e4 942
0ec29a47
JH
943 while ((c = *msg++)) {
944 if (wasspace && isspace(c))
945 continue;
946 wasspace = isspace(c);
947 if (wasspace)
948 c = ' ';
80a6c207 949 strbuf_addch(sb, c);
a4c653df 950 }
80a6c207 951 strbuf_rtrim(sb);
0ec29a47 952}
8b5157e4 953
523fa69c
JH
954static char *normalize_reflog_message(const char *msg)
955{
956 struct strbuf sb = STRBUF_INIT;
957
958 if (msg && *msg)
959 copy_reflog_msg(&sb, msg);
960 return strbuf_detach(&sb, NULL);
961}
962
4cb77009 963int should_autocreate_reflog(const char *refname)
4e2bef57 964{
341fb286
CW
965 switch (log_all_ref_updates) {
966 case LOG_REFS_ALWAYS:
967 return 1;
968 case LOG_REFS_NORMAL:
969 return starts_with(refname, "refs/heads/") ||
970 starts_with(refname, "refs/remotes/") ||
971 starts_with(refname, "refs/notes/") ||
972 !strcmp(refname, "HEAD");
973 default:
4e2bef57 974 return 0;
341fb286 975 }
4e2bef57
DT
976}
977
e7e0f26e 978int is_branch(const char *refname)
c3b0dec5 979{
59556548 980 return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
41b625b0
NP
981}
982
4207ed28
RS
983struct read_ref_at_cb {
984 const char *refname;
dddbad72 985 timestamp_t at_time;
4207ed28
RS
986 int cnt;
987 int reccnt;
8eb36d94 988 struct object_id *oid;
4207ed28
RS
989 int found_it;
990
8eb36d94 991 struct object_id ooid;
992 struct object_id noid;
4207ed28 993 int tz;
dddbad72 994 timestamp_t date;
4207ed28 995 char **msg;
dddbad72 996 timestamp_t *cutoff_time;
4207ed28
RS
997 int *cutoff_tz;
998 int *cutoff_cnt;
999};
1000
95c2a718
DL
1001static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
1002 timestamp_t timestamp, int tz, const char *message)
1003{
1004 if (cb->msg)
1005 *cb->msg = xstrdup(message);
1006 if (cb->cutoff_time)
1007 *cb->cutoff_time = timestamp;
1008 if (cb->cutoff_tz)
1009 *cb->cutoff_tz = tz;
1010 if (cb->cutoff_cnt)
1011 *cb->cutoff_cnt = cb->reccnt;
1012}
1013
9461d272 1014static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
5cf88fd8 1015 const char *email UNUSED,
c006e9fa
JK
1016 timestamp_t timestamp, int tz,
1017 const char *message, void *cb_data)
4207ed28
RS
1018{
1019 struct read_ref_at_cb *cb = cb_data;
6436a202 1020 int reached_count;
4207ed28 1021
4207ed28
RS
1022 cb->tz = tz;
1023 cb->date = timestamp;
1024
6436a202
DL
1025 /*
1026 * It is not possible for cb->cnt == 0 on the first iteration because
1027 * that special case is handled in read_ref_at().
1028 */
1029 if (cb->cnt > 0)
1030 cb->cnt--;
1031 reached_count = cb->cnt == 0 && !is_null_oid(ooid);
1032 if (timestamp <= cb->at_time || reached_count) {
95c2a718 1033 set_read_ref_cutoffs(cb, timestamp, tz, message);
4207ed28 1034 /*
78fb4579 1035 * we have not yet updated cb->[n|o]oid so they still
4207ed28
RS
1036 * hold the values for the previous record.
1037 */
6436a202
DL
1038 if (!is_null_oid(&cb->ooid) && !oideq(&cb->ooid, noid))
1039 warning(_("log for ref %s has gap after %s"),
a5481a6c 1040 cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
6436a202
DL
1041 if (reached_count)
1042 oidcpy(cb->oid, ooid);
1043 else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time)
8eb36d94 1044 oidcpy(cb->oid, noid);
9001dc2a 1045 else if (!oideq(noid, cb->oid))
661558f0 1046 warning(_("log for ref %s unexpectedly ended on %s"),
4207ed28 1047 cb->refname, show_date(cb->date, cb->tz,
a5481a6c 1048 DATE_MODE(RFC2822)));
4207ed28 1049 cb->found_it = 1;
4207ed28 1050 }
95c2a718 1051 cb->reccnt++;
8eb36d94 1052 oidcpy(&cb->ooid, ooid);
1053 oidcpy(&cb->noid, noid);
6436a202
DL
1054 return cb->found_it;
1055}
1056
5cf88fd8 1057static int read_ref_at_ent_newest(struct object_id *ooid UNUSED,
63e14ee2 1058 struct object_id *noid,
5cf88fd8 1059 const char *email UNUSED,
63e14ee2
JK
1060 timestamp_t timestamp, int tz,
1061 const char *message, void *cb_data)
6436a202
DL
1062{
1063 struct read_ref_at_cb *cb = cb_data;
1064
1065 set_read_ref_cutoffs(cb, timestamp, tz, message);
1066 oidcpy(cb->oid, noid);
1067 /* We just want the first entry */
1068 return 1;
4207ed28
RS
1069}
1070
9461d272 1071static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
5cf88fd8 1072 const char *email UNUSED,
c006e9fa
JK
1073 timestamp_t timestamp, int tz,
1074 const char *message, void *cb_data)
4207ed28
RS
1075{
1076 struct read_ref_at_cb *cb = cb_data;
1077
95c2a718 1078 set_read_ref_cutoffs(cb, timestamp, tz, message);
8eb36d94 1079 oidcpy(cb->oid, ooid);
1080 if (is_null_oid(cb->oid))
1081 oidcpy(cb->oid, noid);
4207ed28
RS
1082 /* We just want the first entry */
1083 return 1;
16d7cc90
JH
1084}
1085
7fdff474
NTND
1086int read_ref_at(struct ref_store *refs, const char *refname,
1087 unsigned int flags, timestamp_t at_time, int cnt,
8eb36d94 1088 struct object_id *oid, char **msg,
dddbad72 1089 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
d556fae2 1090{
4207ed28 1091 struct read_ref_at_cb cb;
d556fae2 1092
4207ed28
RS
1093 memset(&cb, 0, sizeof(cb));
1094 cb.refname = refname;
1095 cb.at_time = at_time;
1096 cb.cnt = cnt;
1097 cb.msg = msg;
1098 cb.cutoff_time = cutoff_time;
1099 cb.cutoff_tz = cutoff_tz;
1100 cb.cutoff_cnt = cutoff_cnt;
8eb36d94 1101 cb.oid = oid;
4207ed28 1102
6436a202
DL
1103 if (cb.cnt == 0) {
1104 refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent_newest, &cb);
1105 return 0;
1106 }
1107
7fdff474 1108 refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb);
4207ed28 1109
c41a87dd 1110 if (!cb.reccnt) {
321c89bf 1111 if (flags & GET_OID_QUIETLY)
c41a87dd
DA
1112 exit(128);
1113 else
661558f0 1114 die(_("log for %s is empty"), refname);
c41a87dd 1115 }
4207ed28
RS
1116 if (cb.found_it)
1117 return 0;
1118
7fdff474 1119 refs_for_each_reflog_ent(refs, refname, read_ref_at_ent_oldest, &cb);
d556fae2 1120
16d7cc90 1121 return 1;
d556fae2 1122}
2ff81662 1123
c0fe4e8b
NTND
1124struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
1125 struct strbuf *err)
caa4046c 1126{
c0fe4e8b 1127 struct ref_transaction *tr;
5a603b04
JN
1128 assert(err);
1129
ca56dadb 1130 CALLOC_ARRAY(tr, 1);
c0fe4e8b
NTND
1131 tr->ref_store = refs;
1132 return tr;
1133}
1134
1135struct ref_transaction *ref_transaction_begin(struct strbuf *err)
1136{
c6da34a6 1137 return ref_store_transaction_begin(get_main_ref_store(the_repository), err);
caa4046c
MH
1138}
1139
026bd1d3 1140void ref_transaction_free(struct ref_transaction *transaction)
caa4046c 1141{
43a2dfde 1142 size_t i;
caa4046c 1143
1b07255c
RS
1144 if (!transaction)
1145 return;
1146
30173b88
MH
1147 switch (transaction->state) {
1148 case REF_TRANSACTION_OPEN:
1149 case REF_TRANSACTION_CLOSED:
1150 /* OK */
1151 break;
1152 case REF_TRANSACTION_PREPARED:
033abf97 1153 BUG("free called on a prepared reference transaction");
30173b88
MH
1154 break;
1155 default:
033abf97 1156 BUG("unexpected reference transaction state");
30173b88
MH
1157 break;
1158 }
1159
db7516ab
RS
1160 for (i = 0; i < transaction->nr; i++) {
1161 free(transaction->updates[i]->msg);
88615910 1162 free(transaction->updates[i]);
db7516ab 1163 }
caa4046c
MH
1164 free(transaction->updates);
1165 free(transaction);
1166}
1167
71564516
MH
1168struct ref_update *ref_transaction_add_update(
1169 struct ref_transaction *transaction,
1170 const char *refname, unsigned int flags,
89f3bbdd 1171 const struct object_id *new_oid,
1172 const struct object_id *old_oid,
71564516 1173 const char *msg)
caa4046c 1174{
96ffc06f 1175 struct ref_update *update;
71564516
MH
1176
1177 if (transaction->state != REF_TRANSACTION_OPEN)
033abf97 1178 BUG("update called for transaction that is not open");
71564516 1179
96ffc06f 1180 FLEX_ALLOC_STR(update, refname, refname);
caa4046c
MH
1181 ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
1182 transaction->updates[transaction->nr++] = update;
71564516
MH
1183
1184 update->flags = flags;
1185
1186 if (flags & REF_HAVE_NEW)
89f3bbdd 1187 oidcpy(&update->new_oid, new_oid);
71564516 1188 if (flags & REF_HAVE_OLD)
89f3bbdd 1189 oidcpy(&update->old_oid, old_oid);
523fa69c 1190 update->msg = normalize_reflog_message(msg);
caa4046c
MH
1191 return update;
1192}
1193
8e34800e
RS
1194int ref_transaction_update(struct ref_transaction *transaction,
1195 const char *refname,
89f3bbdd 1196 const struct object_id *new_oid,
1197 const struct object_id *old_oid,
1d147bdf 1198 unsigned int flags, const char *msg,
8e34800e 1199 struct strbuf *err)
caa4046c 1200{
5a603b04
JN
1201 assert(err);
1202
3c966c7b
HWN
1203 if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
1204 ((new_oid && !is_null_oid(new_oid)) ?
1205 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
1206 !refname_is_safe(refname))) {
661558f0 1207 strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
d0f810f0
RS
1208 refname);
1209 return -1;
1210 }
1211
a9bbbcec
MH
1212 if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
1213 BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
c788c54c 1214
49f1eb3b
JK
1215 /*
1216 * Clear flags outside the allowed set; this should be a noop because
1217 * of the BUG() check above, but it works around a -Wnonnull warning
1218 * with some versions of "gcc -O3".
1219 */
1220 flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
1221
89f3bbdd 1222 flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
71564516
MH
1223
1224 ref_transaction_add_update(transaction, refname, flags,
89f3bbdd 1225 new_oid, old_oid, msg);
8e34800e 1226 return 0;
caa4046c
MH
1227}
1228
b416af5b
RS
1229int ref_transaction_create(struct ref_transaction *transaction,
1230 const char *refname,
89f3bbdd 1231 const struct object_id *new_oid,
fec14ec3 1232 unsigned int flags, const char *msg,
b416af5b 1233 struct strbuf *err)
caa4046c 1234{
d097a23b
DS
1235 if (!new_oid || is_null_oid(new_oid)) {
1236 strbuf_addf(err, "'%s' has a null OID", refname);
1237 return 1;
1238 }
89f3bbdd 1239 return ref_transaction_update(transaction, refname, new_oid,
14228447 1240 null_oid(), flags, msg, err);
caa4046c
MH
1241}
1242
8c8bdc0d
RS
1243int ref_transaction_delete(struct ref_transaction *transaction,
1244 const char *refname,
89f3bbdd 1245 const struct object_id *old_oid,
fb5a6bb6 1246 unsigned int flags, const char *msg,
8c8bdc0d 1247 struct strbuf *err)
caa4046c 1248{
89f3bbdd 1249 if (old_oid && is_null_oid(old_oid))
033abf97 1250 BUG("delete called with old_oid set to zeros");
1d147bdf 1251 return ref_transaction_update(transaction, refname,
14228447 1252 null_oid(), old_oid,
1d147bdf 1253 flags, msg, err);
caa4046c
MH
1254}
1255
16180334
MH
1256int ref_transaction_verify(struct ref_transaction *transaction,
1257 const char *refname,
89f3bbdd 1258 const struct object_id *old_oid,
16180334
MH
1259 unsigned int flags,
1260 struct strbuf *err)
1261{
89f3bbdd 1262 if (!old_oid)
033abf97 1263 BUG("verify called with old_oid set to NULL");
16180334 1264 return ref_transaction_update(transaction, refname,
89f3bbdd 1265 NULL, old_oid,
16180334
MH
1266 flags, NULL, err);
1267}
1268
c0fe4e8b 1269int refs_update_ref(struct ref_store *refs, const char *msg,
ae077771 1270 const char *refname, const struct object_id *new_oid,
1271 const struct object_id *old_oid, unsigned int flags,
c0fe4e8b 1272 enum action_on_err onerr)
4738a333 1273{
74ec19d4 1274 struct ref_transaction *t = NULL;
b4d75ac1 1275 struct strbuf err = STRBUF_INIT;
74ec19d4 1276 int ret = 0;
b4d75ac1 1277
c6da34a6 1278 t = ref_store_transaction_begin(refs, &err);
09743417
HWN
1279 if (!t ||
1280 ref_transaction_update(t, refname, new_oid, old_oid, flags, msg,
1281 &err) ||
1282 ref_transaction_commit(t, &err)) {
1283 ret = 1;
1284 ref_transaction_free(t);
74ec19d4
DT
1285 }
1286 if (ret) {
661558f0 1287 const char *str = _("update_ref failed for ref '%s': %s");
b4d75ac1 1288
b4d75ac1
RS
1289 switch (onerr) {
1290 case UPDATE_REFS_MSG_ON_ERR:
1291 error(str, refname, err.buf);
1292 break;
1293 case UPDATE_REFS_DIE_ON_ERR:
1294 die(str, refname, err.buf);
1295 break;
1296 case UPDATE_REFS_QUIET_ON_ERR:
1297 break;
1298 }
1299 strbuf_release(&err);
4738a333 1300 return 1;
b4d75ac1
RS
1301 }
1302 strbuf_release(&err);
74ec19d4
DT
1303 if (t)
1304 ref_transaction_free(t);
b4d75ac1 1305 return 0;
4738a333
BK
1306}
1307
c0fe4e8b 1308int update_ref(const char *msg, const char *refname,
ae077771 1309 const struct object_id *new_oid,
1310 const struct object_id *old_oid,
c0fe4e8b
NTND
1311 unsigned int flags, enum action_on_err onerr)
1312{
23a3f0cb 1313 return refs_update_ref(get_main_ref_store(the_repository), msg, refname, new_oid,
ae077771 1314 old_oid, flags, onerr);
c0fe4e8b
NTND
1315}
1316
613bef56
JK
1317/*
1318 * Check that the string refname matches a rule of the form
1319 * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule
1320 * "foo/%.*s/baz", and return the string "bar".
1321 */
1322static const char *match_parse_rule(const char *refname, const char *rule,
1323 size_t *len)
7c2b3029 1324{
613bef56
JK
1325 /*
1326 * Check that rule matches refname up to the first percent in the rule.
1327 * We can bail immediately if not, but otherwise we leave "rule" at the
1328 * %-placeholder, and "refname" at the start of the potential matched
1329 * name.
1330 */
1331 while (*rule != '%') {
1332 if (!*rule)
1333 BUG("rev-parse rule did not have percent");
1334 if (*refname++ != *rule++)
1335 return NULL;
1336 }
7c2b3029 1337
613bef56
JK
1338 /*
1339 * Check that our "%" is the expected placeholder. This assumes there
1340 * are no other percents (placeholder or quoted) in the string, but
1341 * that is sufficient for our rev-parse rules.
1342 */
1343 if (!skip_prefix(rule, "%.*s", &rule))
1344 return NULL;
7c2b3029 1345
613bef56
JK
1346 /*
1347 * And now check that our suffix (if any) matches.
1348 */
1349 if (!strip_suffix(refname, rule, len))
1350 return NULL;
7c2b3029 1351
613bef56
JK
1352 return refname; /* len set by strip_suffix() */
1353}
7c2b3029 1354
546edf37
NTND
1355char *refs_shorten_unambiguous_ref(struct ref_store *refs,
1356 const char *refname, int strict)
7c2b3029
JK
1357{
1358 int i;
6cd4a898 1359 struct strbuf resolved_buf = STRBUF_INIT;
7c2b3029 1360
7c2b3029 1361 /* skip first rule, it will always match */
8f416f65 1362 for (i = NUM_REV_PARSE_RULES - 1; i > 0 ; --i) {
7c2b3029 1363 int j;
6e7b3309 1364 int rules_to_fail = i;
613bef56 1365 const char *short_name;
dd5e4d39 1366 size_t short_name_len;
7c2b3029 1367
613bef56
JK
1368 short_name = match_parse_rule(refname, ref_rev_parse_rules[i],
1369 &short_name_len);
1370 if (!short_name)
7c2b3029
JK
1371 continue;
1372
6e7b3309
BW
1373 /*
1374 * in strict mode, all (except the matched one) rules
1375 * must fail to resolve to a valid non-ambiguous ref
1376 */
1377 if (strict)
8f416f65 1378 rules_to_fail = NUM_REV_PARSE_RULES;
6e7b3309 1379
7c2b3029
JK
1380 /*
1381 * check if the short name resolves to a valid ref,
1382 * but use only rules prior to the matched one
1383 */
6e7b3309 1384 for (j = 0; j < rules_to_fail; j++) {
7c2b3029 1385 const char *rule = ref_rev_parse_rules[j];
7c2b3029 1386
6e7b3309
BW
1387 /* skip matched rule */
1388 if (i == j)
1389 continue;
1390
7c2b3029
JK
1391 /*
1392 * the short name is ambiguous, if it resolves
1393 * (with this previous rule) to a valid ref
1394 * read_ref() returns 0 on success
1395 */
6cd4a898
JK
1396 strbuf_reset(&resolved_buf);
1397 strbuf_addf(&resolved_buf, rule,
dd5e4d39
JK
1398 cast_size_t_to_int(short_name_len),
1399 short_name);
546edf37 1400 if (refs_ref_exists(refs, resolved_buf.buf))
7c2b3029
JK
1401 break;
1402 }
1403
1404 /*
1405 * short name is non-ambiguous if all previous rules
1406 * haven't resolved to a valid ref
1407 */
6cd4a898
JK
1408 if (j == rules_to_fail) {
1409 strbuf_release(&resolved_buf);
613bef56 1410 return xmemdupz(short_name, short_name_len);
6cd4a898 1411 }
7c2b3029
JK
1412 }
1413
6cd4a898 1414 strbuf_release(&resolved_buf);
dfefa935 1415 return xstrdup(refname);
7c2b3029 1416}
daebaa78 1417
546edf37
NTND
1418char *shorten_unambiguous_ref(const char *refname, int strict)
1419{
1420 return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
1421 refname, strict);
1422}
1423
9b67eb6f
PS
1424int parse_hide_refs_config(const char *var, const char *value, const char *section,
1425 struct string_list *hide_refs)
daebaa78 1426{
ad8c7cda 1427 const char *key;
daebaa78 1428 if (!strcmp("transfer.hiderefs", var) ||
ad8c7cda
JK
1429 (!parse_config_key(var, section, NULL, NULL, &key) &&
1430 !strcmp(key, "hiderefs"))) {
daebaa78
JH
1431 char *ref;
1432 int len;
1433
1434 if (!value)
1435 return config_error_nonbool(var);
1436 ref = xstrdup(value);
1437 len = strlen(ref);
1438 while (len && ref[len - 1] == '/')
1439 ref[--len] = '\0';
5eeb9aa2 1440 string_list_append_nodup(hide_refs, ref);
daebaa78
JH
1441 }
1442 return 0;
1443}
1444
9b67eb6f
PS
1445int ref_is_hidden(const char *refname, const char *refname_full,
1446 const struct string_list *hide_refs)
daebaa78 1447{
2bc31d16 1448 int i;
daebaa78 1449
2bc31d16
JK
1450 for (i = hide_refs->nr - 1; i >= 0; i--) {
1451 const char *match = hide_refs->items[i].string;
78a766ab 1452 const char *subject;
2bc31d16 1453 int neg = 0;
7a40a95e 1454 const char *p;
2bc31d16
JK
1455
1456 if (*match == '!') {
1457 neg = 1;
1458 match++;
1459 }
1460
78a766ab
LF
1461 if (*match == '^') {
1462 subject = refname_full;
1463 match++;
1464 } else {
1465 subject = refname;
1466 }
1467
1468 /* refname can be NULL when namespaces are used. */
7a40a95e
CC
1469 if (subject &&
1470 skip_prefix(subject, match, &p) &&
1471 (!*p || *p == '/'))
2bc31d16 1472 return !neg;
daebaa78
JH
1473 }
1474 return 0;
1475}
fa5b1830 1476
0845122c
DT
1477const char *find_descendant_ref(const char *dirname,
1478 const struct string_list *extras,
1479 const struct string_list *skip)
fa5b1830 1480{
0845122c 1481 int pos;
fa5b1830 1482
0845122c
DT
1483 if (!extras)
1484 return NULL;
fa5b1830
MH
1485
1486 /*
0845122c
DT
1487 * Look at the place where dirname would be inserted into
1488 * extras. If there is an entry at that position that starts
1489 * with dirname (remember, dirname includes the trailing
1490 * slash) and is not in skip, then we have a conflict.
fa5b1830 1491 */
0845122c
DT
1492 for (pos = string_list_find_insert_index(extras, dirname, 0);
1493 pos < extras->nr; pos++) {
1494 const char *extra_refname = extras->items[pos].string;
fa5b1830 1495
0845122c
DT
1496 if (!starts_with(extra_refname, dirname))
1497 break;
1498
1499 if (!skip || !string_list_has_string(skip, extra_refname))
1500 return extra_refname;
fa5b1830 1501 }
0845122c
DT
1502 return NULL;
1503}
fa5b1830 1504
62f0b399 1505int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2bf68ed5
DT
1506{
1507 struct object_id oid;
1508 int flag;
1509
f1da24ca 1510 if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING,
ce14de03 1511 &oid, &flag))
2bf68ed5
DT
1512 return fn("HEAD", &oid, flag, cb_data);
1513
1514 return 0;
1515}
1516
1517int head_ref(each_ref_fn fn, void *cb_data)
1518{
23a3f0cb 1519 return refs_head_ref(get_main_ref_store(the_repository), fn, cb_data);
2bf68ed5 1520}
93770590 1521
e121b9cb
MH
1522struct ref_iterator *refs_ref_iterator_begin(
1523 struct ref_store *refs,
9aab952e
JK
1524 const char *prefix, int trim,
1525 enum do_for_each_ref_flags flags)
e121b9cb
MH
1526{
1527 struct ref_iterator *iter;
1528
6d751be4 1529 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
5d1f5b8c
JK
1530 static int ref_paranoia = -1;
1531
6d751be4 1532 if (ref_paranoia < 0)
968f12fd 1533 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 1);
6d751be4
JK
1534 if (ref_paranoia) {
1535 flags |= DO_FOR_EACH_INCLUDE_BROKEN;
1536 flags |= DO_FOR_EACH_OMIT_DANGLING_SYMREFS;
1537 }
1538 }
0a0865b8 1539
e121b9cb 1540 iter = refs->be->iterator_begin(refs, prefix, flags);
c7599718
MH
1541
1542 /*
1543 * `iterator_begin()` already takes care of prefix, but we
1544 * might need to do some trimming:
1545 */
1546 if (trim)
1547 iter = prefix_ref_iterator_begin(iter, "", trim);
e121b9cb 1548
8738a8a4
MH
1549 /* Sanity check for subclasses: */
1550 if (!iter->ordered)
1551 BUG("reference iterator is not ordered");
1552
e121b9cb
MH
1553 return iter;
1554}
1555
4c4de895
MH
1556/*
1557 * Call fn for each reference in the specified submodule for which the
1558 * refname begins with prefix. If trim is non-zero, then trim that
1559 * many characters off the beginning of each refname before passing
1560 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1561 * include broken references in the iteration. If fn ever returns a
1562 * non-zero value, stop the iteration and return that value;
1563 * otherwise, return 0.
1564 */
4a6067cd
SB
1565static int do_for_each_repo_ref(struct repository *r, const char *prefix,
1566 each_repo_ref_fn fn, int trim, int flags,
1567 void *cb_data)
1568{
1569 struct ref_iterator *iter;
1570 struct ref_store *refs = get_main_ref_store(r);
1571
1572 if (!refs)
1573 return 0;
1574
1575 iter = refs_ref_iterator_begin(refs, prefix, trim, flags);
1576
1577 return do_for_each_repo_ref_iterator(r, iter, fn, cb_data);
1578}
1579
1580struct do_for_each_ref_help {
1581 each_ref_fn *fn;
1582 void *cb_data;
1583};
1584
1585static int do_for_each_ref_helper(struct repository *r,
1586 const char *refname,
1587 const struct object_id *oid,
1588 int flags,
1589 void *cb_data)
1590{
1591 struct do_for_each_ref_help *hp = cb_data;
1592
1593 return hp->fn(refname, oid, flags, hp->cb_data);
1594}
1595
7d2df051 1596static int do_for_each_ref(struct ref_store *refs, const char *prefix,
9aab952e
JK
1597 each_ref_fn fn, int trim,
1598 enum do_for_each_ref_flags flags, void *cb_data)
4c4de895
MH
1599{
1600 struct ref_iterator *iter;
4a6067cd 1601 struct do_for_each_ref_help hp = { fn, cb_data };
4c4de895 1602
00eebe35
MH
1603 if (!refs)
1604 return 0;
1605
e121b9cb 1606 iter = refs_ref_iterator_begin(refs, prefix, trim, flags);
4c4de895 1607
4a6067cd
SB
1608 return do_for_each_repo_ref_iterator(the_repository, iter,
1609 do_for_each_ref_helper, &hp);
4c4de895
MH
1610}
1611
7d2df051
NTND
1612int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1613{
1614 return do_for_each_ref(refs, "", fn, 0, 0, cb_data);
1615}
1616
93770590
DT
1617int for_each_ref(each_ref_fn fn, void *cb_data)
1618{
23a3f0cb 1619 return refs_for_each_ref(get_main_ref_store(the_repository), fn, cb_data);
93770590
DT
1620}
1621
7d2df051
NTND
1622int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
1623 each_ref_fn fn, void *cb_data)
1624{
1625 return do_for_each_ref(refs, prefix, fn, strlen(prefix), 0, cb_data);
93770590
DT
1626}
1627
1628int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1629{
23a3f0cb 1630 return refs_for_each_ref_in(get_main_ref_store(the_repository), prefix, fn, cb_data);
93770590
DT
1631}
1632
67985e4e 1633int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data)
93770590 1634{
23a3f0cb 1635 return do_for_each_ref(get_main_ref_store(the_repository),
67985e4e 1636 prefix, fn, 0, 0, cb_data);
93770590
DT
1637}
1638
073cf63c 1639int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
67985e4e 1640 each_ref_fn fn, void *cb_data)
03df567f 1641{
67985e4e 1642 return do_for_each_ref(refs, prefix, fn, 0, 0, cb_data);
03df567f
MH
1643}
1644
212e0f7e 1645int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data)
93770590 1646{
97e61e0f 1647 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
212e0f7e
SB
1648 return do_for_each_repo_ref(r, git_replace_ref_base, fn,
1649 strlen(git_replace_ref_base),
1650 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
93770590
DT
1651}
1652
1653int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1654{
1655 struct strbuf buf = STRBUF_INIT;
1656 int ret;
1657 strbuf_addf(&buf, "%srefs/", get_git_namespace());
23a3f0cb 1658 ret = do_for_each_ref(get_main_ref_store(the_repository),
7d2df051 1659 buf.buf, fn, 0, 0, cb_data);
93770590
DT
1660 strbuf_release(&buf);
1661 return ret;
1662}
1663
7d2df051 1664int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
93770590 1665{
7d2df051 1666 return do_for_each_ref(refs, "", fn, 0,
93770590
DT
1667 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1668}
2d0663b2 1669
7d2df051
NTND
1670int for_each_rawref(each_ref_fn fn, void *cb_data)
1671{
23a3f0cb 1672 return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data);
7d2df051
NTND
1673}
1674
16b1985b
TB
1675static int qsort_strcmp(const void *va, const void *vb)
1676{
1677 const char *a = *(const char **)va;
1678 const char *b = *(const char **)vb;
1679
1680 return strcmp(a, b);
1681}
1682
1683static void find_longest_prefixes_1(struct string_list *out,
1684 struct strbuf *prefix,
1685 const char **patterns, size_t nr)
1686{
1687 size_t i;
1688
1689 for (i = 0; i < nr; i++) {
1690 char c = patterns[i][prefix->len];
1691 if (!c || is_glob_special(c)) {
1692 string_list_append(out, prefix->buf);
1693 return;
1694 }
1695 }
1696
1697 i = 0;
1698 while (i < nr) {
1699 size_t end;
1700
1701 /*
1702 * Set "end" to the index of the element _after_ the last one
1703 * in our group.
1704 */
1705 for (end = i + 1; end < nr; end++) {
1706 if (patterns[i][prefix->len] != patterns[end][prefix->len])
1707 break;
1708 }
1709
1710 strbuf_addch(prefix, patterns[i][prefix->len]);
1711 find_longest_prefixes_1(out, prefix, patterns + i, end - i);
1712 strbuf_setlen(prefix, prefix->len - 1);
1713
1714 i = end;
1715 }
1716}
1717
1718static void find_longest_prefixes(struct string_list *out,
1719 const char **patterns)
1720{
1721 struct strvec sorted = STRVEC_INIT;
1722 struct strbuf prefix = STRBUF_INIT;
1723
1724 strvec_pushv(&sorted, patterns);
1725 QSORT(sorted.v, sorted.nr, qsort_strcmp);
1726
1727 find_longest_prefixes_1(out, &prefix, sorted.v, sorted.nr);
1728
1729 strvec_clear(&sorted);
1730 strbuf_release(&prefix);
1731}
1732
91e2ab15
JK
1733int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store,
1734 const char *namespace,
1735 const char **patterns,
1736 each_ref_fn fn, void *cb_data)
16b1985b
TB
1737{
1738 struct string_list prefixes = STRING_LIST_INIT_DUP;
1739 struct string_list_item *prefix;
1740 struct strbuf buf = STRBUF_INIT;
1741 int ret = 0, namespace_len;
1742
1743 find_longest_prefixes(&prefixes, patterns);
1744
1745 if (namespace)
1746 strbuf_addstr(&buf, namespace);
1747 namespace_len = buf.len;
1748
1749 for_each_string_list_item(prefix, &prefixes) {
1750 strbuf_addstr(&buf, prefix->string);
91e2ab15 1751 ret = refs_for_each_fullref_in(ref_store, buf.buf, fn, cb_data);
16b1985b
TB
1752 if (ret)
1753 break;
1754 strbuf_setlen(&buf, namespace_len);
1755 }
1756
1757 string_list_clear(&prefixes, 0);
1758 strbuf_release(&buf);
1759 return ret;
1760}
1761
e8115302
HWN
1762static int refs_read_special_head(struct ref_store *ref_store,
1763 const char *refname, struct object_id *oid,
df3458e9
HWN
1764 struct strbuf *referent, unsigned int *type,
1765 int *failure_errno)
e8115302
HWN
1766{
1767 struct strbuf full_path = STRBUF_INIT;
1768 struct strbuf content = STRBUF_INIT;
1769 int result = -1;
1770 strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname);
1771
1772 if (strbuf_read_file(&content, full_path.buf, 0) < 0)
1773 goto done;
1774
df3458e9
HWN
1775 result = parse_loose_ref_contents(content.buf, oid, referent, type,
1776 failure_errno);
e8115302
HWN
1777
1778done:
1779 strbuf_release(&full_path);
1780 strbuf_release(&content);
1781 return result;
1782}
1783
8b72fea7
HWN
1784int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
1785 struct object_id *oid, struct strbuf *referent,
1786 unsigned int *type, int *failure_errno)
470be518 1787{
8b72fea7 1788 assert(failure_errno);
e8115302
HWN
1789 if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) {
1790 return refs_read_special_head(ref_store, refname, oid, referent,
df3458e9 1791 type, failure_errno);
e8115302
HWN
1792 }
1793
1794 return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
8b72fea7 1795 type, failure_errno);
470be518
MH
1796}
1797
cd475b3b
PS
1798int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
1799 struct strbuf *referent)
1800{
5b875404 1801 return ref_store->be->read_symbolic_ref(ref_store, refname, referent);
cd475b3b
PS
1802}
1803
7d2df051 1804const char *refs_resolve_ref_unsafe(struct ref_store *refs,
3c0cb0cb
MH
1805 const char *refname,
1806 int resolve_flags,
ef18119d 1807 struct object_id *oid,
ce14de03 1808 int *flags)
2d0663b2
DT
1809{
1810 static struct strbuf sb_refname = STRBUF_INIT;
54fad661 1811 struct object_id unused_oid;
2d0663b2
DT
1812 int unused_flags;
1813 int symref_count;
1814
49e61479 1815 if (!oid)
1816 oid = &unused_oid;
2d0663b2
DT
1817 if (!flags)
1818 flags = &unused_flags;
1819
1820 *flags = 0;
1821
1822 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1823 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
ce14de03 1824 !refname_is_safe(refname))
2d0663b2 1825 return NULL;
2d0663b2
DT
1826
1827 /*
1828 * dwim_ref() uses REF_ISBROKEN to distinguish between
1829 * missing refs and refs that were present but invalid,
1830 * to complain about the latter to stderr.
1831 *
1832 * We don't know whether the ref exists, so don't set
1833 * REF_ISBROKEN yet.
1834 */
1835 *flags |= REF_BAD_NAME;
1836 }
1837
1838 for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1839 unsigned int read_flags = 0;
ce14de03 1840 int failure_errno;
2d0663b2 1841
8b72fea7 1842 if (refs_read_raw_ref(refs, refname, oid, &sb_refname,
ce14de03 1843 &read_flags, &failure_errno)) {
2d0663b2 1844 *flags |= read_flags;
a1c1d817
JK
1845
1846 /* In reading mode, refs must eventually resolve */
1847 if (resolve_flags & RESOLVE_REF_READING)
1848 return NULL;
1849
1850 /*
1851 * Otherwise a missing ref is OK. But the files backend
1852 * may show errors besides ENOENT if there are
1853 * similarly-named refs.
1854 */
ce14de03
ÆAB
1855 if (failure_errno != ENOENT &&
1856 failure_errno != EISDIR &&
1857 failure_errno != ENOTDIR)
2d0663b2 1858 return NULL;
a1c1d817 1859
49e61479 1860 oidclr(oid);
2d0663b2
DT
1861 if (*flags & REF_BAD_NAME)
1862 *flags |= REF_ISBROKEN;
1863 return refname;
1864 }
1865
1866 *flags |= read_flags;
1867
1868 if (!(read_flags & REF_ISSYMREF)) {
1869 if (*flags & REF_BAD_NAME) {
49e61479 1870 oidclr(oid);
2d0663b2
DT
1871 *flags |= REF_ISBROKEN;
1872 }
1873 return refname;
1874 }
1875
1876 refname = sb_refname.buf;
1877 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
49e61479 1878 oidclr(oid);
2d0663b2
DT
1879 return refname;
1880 }
1881 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1882 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
ce14de03 1883 !refname_is_safe(refname))
2d0663b2 1884 return NULL;
2d0663b2
DT
1885
1886 *flags |= REF_ISBROKEN | REF_BAD_NAME;
1887 }
1888 }
1889
2d0663b2
DT
1890 return NULL;
1891}
00eebe35 1892
6fb5acfd
DT
1893/* backend functions */
1894int refs_init_db(struct strbuf *err)
1895{
23a3f0cb 1896 struct ref_store *refs = get_main_ref_store(the_repository);
6fb5acfd
DT
1897
1898 return refs->be->init_db(refs, err);
1899}
1900
bd40dcda 1901const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
49e61479 1902 struct object_id *oid, int *flags)
bd40dcda 1903{
23a3f0cb 1904 return refs_resolve_ref_unsafe(get_main_ref_store(the_repository), refname,
ce14de03 1905 resolve_flags, oid, flags);
bd40dcda
MH
1906}
1907
a8355bb7 1908int resolve_gitlink_ref(const char *submodule, const char *refname,
a98e6101 1909 struct object_id *oid)
424dcc76 1910{
424dcc76
MH
1911 struct ref_store *refs;
1912 int flags;
1913
29babbee 1914 refs = get_submodule_ref_store(submodule);
48a8475f 1915
424dcc76
MH
1916 if (!refs)
1917 return -1;
1918
ce14de03
ÆAB
1919 if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) ||
1920 is_null_oid(oid))
424dcc76
MH
1921 return -1;
1922 return 0;
1923}
1924
0c064d90 1925struct ref_store_hash_entry
7d4558c4 1926{
e2b5038d 1927 struct hashmap_entry ent;
7d4558c4
MH
1928
1929 struct ref_store *refs;
1930
0c064d90
NTND
1931 /* NUL-terminated identifier of the ref store: */
1932 char name[FLEX_ARRAY];
7d4558c4
MH
1933};
1934
5cf88fd8 1935static int ref_store_hash_cmp(const void *cmp_data UNUSED,
939af16e
EW
1936 const struct hashmap_entry *eptr,
1937 const struct hashmap_entry *entry_or_key,
7d4558c4
MH
1938 const void *keydata)
1939{
939af16e
EW
1940 const struct ref_store_hash_entry *e1, *e2;
1941 const char *name;
1942
1943 e1 = container_of(eptr, const struct ref_store_hash_entry, ent);
1944 e2 = container_of(entry_or_key, const struct ref_store_hash_entry, ent);
1945 name = keydata ? keydata : e2->name;
7d4558c4 1946
0c064d90 1947 return strcmp(e1->name, name);
7d4558c4
MH
1948}
1949
0c064d90
NTND
1950static struct ref_store_hash_entry *alloc_ref_store_hash_entry(
1951 const char *name, struct ref_store *refs)
7d4558c4 1952{
0c064d90 1953 struct ref_store_hash_entry *entry;
7d4558c4 1954
0c064d90 1955 FLEX_ALLOC_STR(entry, name, name);
d22245a2 1956 hashmap_entry_init(&entry->ent, strhash(name));
7d4558c4
MH
1957 entry->refs = refs;
1958 return entry;
1959}
1960
7d4558c4
MH
1961/* A hashmap of ref_stores, stored by submodule name: */
1962static struct hashmap submodule_ref_stores;
00eebe35 1963
17eff96b
NTND
1964/* A hashmap of ref_stores, stored by worktree id: */
1965static struct hashmap worktree_ref_stores;
1966
c468da4e 1967/*
0c064d90
NTND
1968 * Look up a ref store by name. If that ref_store hasn't been
1969 * registered yet, return NULL.
c468da4e 1970 */
0c064d90
NTND
1971static struct ref_store *lookup_ref_store_map(struct hashmap *map,
1972 const char *name)
00eebe35 1973{
0c064d90 1974 struct ref_store_hash_entry *entry;
f23a4651 1975 unsigned int hash;
00eebe35 1976
0c064d90 1977 if (!map->tablesize)
7d4558c4
MH
1978 /* It's initialized on demand in register_ref_store(). */
1979 return NULL;
620a66b9 1980
f23a4651
EW
1981 hash = strhash(name);
1982 entry = hashmap_get_entry_from_hash(map, hash, name,
1983 struct ref_store_hash_entry, ent);
7d4558c4 1984 return entry ? entry->refs : NULL;
00eebe35
MH
1985}
1986
c468da4e
MH
1987/*
1988 * Create, record, and return a ref_store instance for the specified
5d0bc90e 1989 * gitdir.
c468da4e 1990 */
34224e14
JT
1991static struct ref_store *ref_store_init(struct repository *repo,
1992 const char *gitdir,
9e7ec634 1993 unsigned int flags)
00eebe35
MH
1994{
1995 const char *be_name = "files";
1996 struct ref_storage_be *be = find_ref_storage_backend(be_name);
ba88add5 1997 struct ref_store *refs;
00eebe35
MH
1998
1999 if (!be)
033abf97 2000 BUG("reference backend %s is unknown", be_name);
00eebe35 2001
34224e14 2002 refs = be->init(repo, gitdir, flags);
ba88add5 2003 return refs;
00eebe35
MH
2004}
2005
64a74161 2006struct ref_store *get_main_ref_store(struct repository *r)
24c8407e 2007{
02204610
JK
2008 if (r->refs_private)
2009 return r->refs_private;
24c8407e 2010
2dc417ab
JK
2011 if (!r->gitdir)
2012 BUG("attempting to get main_ref_store outside of repository");
2013
34224e14 2014 r->refs_private = ref_store_init(r, r->gitdir, REF_STORE_ALL_CAPS);
4441f427 2015 r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private);
02204610 2016 return r->refs_private;
378dc910
NTND
2017}
2018
2019/*
0c064d90
NTND
2020 * Associate a ref store with a name. It is a fatal error to call this
2021 * function twice for the same name.
378dc910 2022 */
0c064d90
NTND
2023static void register_ref_store_map(struct hashmap *map,
2024 const char *type,
2025 struct ref_store *refs,
2026 const char *name)
378dc910 2027{
26b455f2
EW
2028 struct ref_store_hash_entry *entry;
2029
0c064d90 2030 if (!map->tablesize)
7663cdc8 2031 hashmap_init(map, ref_store_hash_cmp, NULL, 0);
378dc910 2032
26b455f2
EW
2033 entry = alloc_ref_store_hash_entry(name, refs);
2034 if (hashmap_put(map, &entry->ent))
033abf97 2035 BUG("%s ref_store '%s' initialized twice", type, name);
24c8407e
NTND
2036}
2037
18d0002d 2038struct ref_store *get_submodule_ref_store(const char *submodule)
00eebe35 2039{
126c9e05 2040 struct strbuf submodule_sb = STRBUF_INIT;
00eebe35 2041 struct ref_store *refs;
29babbee
NTND
2042 char *to_free = NULL;
2043 size_t len;
34224e14 2044 struct repository *subrepo;
00eebe35 2045
82a150f2
NTND
2046 if (!submodule)
2047 return NULL;
2048
873ea90d
NTND
2049 len = strlen(submodule);
2050 while (len && is_dir_sep(submodule[len - 1]))
2051 len--;
2052 if (!len)
2053 return NULL;
00eebe35 2054
29babbee
NTND
2055 if (submodule[len])
2056 /* We need to strip off one or more trailing slashes */
2057 submodule = to_free = xmemdupz(submodule, len);
00eebe35 2058
0c064d90 2059 refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
126c9e05 2060 if (refs)
2c616c17 2061 goto done;
00eebe35 2062
126c9e05 2063 strbuf_addstr(&submodule_sb, submodule);
2c616c17
NTND
2064 if (!is_nonbare_repository_dir(&submodule_sb))
2065 goto done;
00eebe35 2066
2c616c17
NTND
2067 if (submodule_to_gitdir(&submodule_sb, submodule))
2068 goto done;
00eebe35 2069
34224e14
JT
2070 subrepo = xmalloc(sizeof(*subrepo));
2071 /*
2072 * NEEDSWORK: Make get_submodule_ref_store() work with arbitrary
2073 * superprojects other than the_repository. This probably should be
2074 * done by making it take a struct repository * parameter instead of a
2075 * submodule path.
2076 */
2077 if (repo_submodule_init(subrepo, the_repository, submodule,
2078 null_oid())) {
2079 free(subrepo);
2080 goto done;
2081 }
2082 refs = ref_store_init(subrepo, submodule_sb.buf,
9e7ec634 2083 REF_STORE_READ | REF_STORE_ODB);
0c064d90
NTND
2084 register_ref_store_map(&submodule_ref_stores, "submodule",
2085 refs, submodule);
5d0bc90e 2086
2c616c17 2087done:
5d0bc90e 2088 strbuf_release(&submodule_sb);
29babbee
NTND
2089 free(to_free);
2090
00eebe35
MH
2091 return refs;
2092}
2093
17eff96b
NTND
2094struct ref_store *get_worktree_ref_store(const struct worktree *wt)
2095{
2096 struct ref_store *refs;
2097 const char *id;
2098
2099 if (wt->is_current)
23a3f0cb 2100 return get_main_ref_store(the_repository);
17eff96b
NTND
2101
2102 id = wt->id ? wt->id : "/";
2103 refs = lookup_ref_store_map(&worktree_ref_stores, id);
2104 if (refs)
2105 return refs;
2106
2107 if (wt->id)
34224e14
JT
2108 refs = ref_store_init(the_repository,
2109 git_common_path("worktrees/%s", wt->id),
17eff96b
NTND
2110 REF_STORE_ALL_CAPS);
2111 else
34224e14
JT
2112 refs = ref_store_init(the_repository,
2113 get_git_common_dir(),
17eff96b
NTND
2114 REF_STORE_ALL_CAPS);
2115
2116 if (refs)
2117 register_ref_store_map(&worktree_ref_stores, "worktree",
2118 refs, id);
2119 return refs;
2120}
2121
f9f7fd3b
HWN
2122void base_ref_store_init(struct ref_store *refs, struct repository *repo,
2123 const char *path, const struct ref_storage_be *be)
00eebe35 2124{
620a66b9 2125 refs->be = be;
f9f7fd3b
HWN
2126 refs->repo = repo;
2127 refs->gitdir = xstrdup(path);
00eebe35 2128}
127b42a1
RS
2129
2130/* backend functions */
7d2df051 2131int refs_pack_refs(struct ref_store *refs, unsigned int flags)
8231527e 2132{
8231527e
MH
2133 return refs->be->pack_refs(refs, flags);
2134}
2135
36a31792 2136int peel_iterated_oid(const struct object_id *base, struct object_id *peeled)
7d2df051 2137{
36a31792
JK
2138 if (current_ref_iter &&
2139 (current_ref_iter->oid == base ||
2140 oideq(current_ref_iter->oid, base)))
2141 return ref_iterator_peel(current_ref_iter, peeled);
ba1c052f 2142
617480d7 2143 return peel_object(base, peeled) ? -1 : 0;
7d2df051 2144}
bd427cf2 2145
7d2df051
NTND
2146int refs_create_symref(struct ref_store *refs,
2147 const char *ref_target,
2148 const char *refs_heads_master,
2149 const char *logmsg)
2150{
523fa69c
JH
2151 char *msg;
2152 int retval;
2153
2154 msg = normalize_reflog_message(logmsg);
2155 retval = refs->be->create_symref(refs, ref_target, refs_heads_master,
2156 msg);
2157 free(msg);
2158 return retval;
bd427cf2
MH
2159}
2160
284689ba
MH
2161int create_symref(const char *ref_target, const char *refs_heads_master,
2162 const char *logmsg)
2163{
23a3f0cb 2164 return refs_create_symref(get_main_ref_store(the_repository), ref_target,
7d2df051 2165 refs_heads_master, logmsg);
284689ba
MH
2166}
2167
2ced105c
MH
2168int ref_update_reject_duplicates(struct string_list *refnames,
2169 struct strbuf *err)
2170{
a552e50e 2171 size_t i, n = refnames->nr;
2ced105c
MH
2172
2173 assert(err);
2174
8556f8d6
MH
2175 for (i = 1; i < n; i++) {
2176 int cmp = strcmp(refnames->items[i - 1].string,
2177 refnames->items[i].string);
2178
2179 if (!cmp) {
2ced105c 2180 strbuf_addf(err,
661558f0 2181 _("multiple updates for ref '%s' not allowed"),
2ced105c
MH
2182 refnames->items[i].string);
2183 return 1;
8556f8d6 2184 } else if (cmp > 0) {
033abf97 2185 BUG("ref_update_reject_duplicates() received unsorted list");
2ced105c 2186 }
8556f8d6 2187 }
2ced105c
MH
2188 return 0;
2189}
2190
67541597
PS
2191static int run_transaction_hook(struct ref_transaction *transaction,
2192 const char *state)
2193{
2194 struct child_process proc = CHILD_PROCESS_INIT;
2195 struct strbuf buf = STRBUF_INIT;
0a0fbbe3 2196 const char *hook;
67541597
PS
2197 int ret = 0, i;
2198
0a0fbbe3 2199 hook = find_hook("reference-transaction");
67541597 2200 if (!hook)
67541597 2201 return ret;
67541597 2202
c972bf4c 2203 strvec_pushl(&proc.args, hook, state, NULL);
67541597
PS
2204 proc.in = -1;
2205 proc.stdout_to_stderr = 1;
2206 proc.trace2_hook_name = "reference-transaction";
2207
2208 ret = start_command(&proc);
2209 if (ret)
2210 return ret;
2211
2212 sigchain_push(SIGPIPE, SIG_IGN);
2213
2214 for (i = 0; i < transaction->nr; i++) {
2215 struct ref_update *update = transaction->updates[i];
2216
2217 strbuf_reset(&buf);
2218 strbuf_addf(&buf, "%s %s %s\n",
2219 oid_to_hex(&update->old_oid),
2220 oid_to_hex(&update->new_oid),
2221 update->refname);
2222
2223 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
4755d7df
ÆAB
2224 if (errno != EPIPE) {
2225 /* Don't leak errno outside this API */
2226 errno = 0;
67541597 2227 ret = -1;
4755d7df 2228 }
67541597
PS
2229 break;
2230 }
2231 }
2232
2233 close(proc.in);
2234 sigchain_pop(SIGPIPE);
2235 strbuf_release(&buf);
2236
2237 ret |= finish_command(&proc);
2238 return ret;
2239}
2240
30173b88
MH
2241int ref_transaction_prepare(struct ref_transaction *transaction,
2242 struct strbuf *err)
127b42a1 2243{
c0fe4e8b 2244 struct ref_store *refs = transaction->ref_store;
67541597 2245 int ret;
127b42a1 2246
8d4240d3
MH
2247 switch (transaction->state) {
2248 case REF_TRANSACTION_OPEN:
2249 /* Good. */
2250 break;
30173b88 2251 case REF_TRANSACTION_PREPARED:
033abf97 2252 BUG("prepare called twice on reference transaction");
30173b88 2253 break;
8d4240d3 2254 case REF_TRANSACTION_CLOSED:
033abf97 2255 BUG("prepare called on a closed reference transaction");
8d4240d3
MH
2256 break;
2257 default:
033abf97 2258 BUG("unexpected reference transaction state");
8d4240d3
MH
2259 break;
2260 }
2261
ecd81dfc 2262 if (refs->repo->objects->odb->disable_ref_updates) {
d8f4481c
JK
2263 strbuf_addstr(err,
2264 _("ref updates forbidden inside quarantine environment"));
2265 return -1;
2266 }
2267
67541597
PS
2268 ret = refs->be->transaction_prepare(refs, transaction, err);
2269 if (ret)
2270 return ret;
2271
2272 ret = run_transaction_hook(transaction, "prepared");
2273 if (ret) {
2274 ref_transaction_abort(transaction, err);
2275 die(_("ref updates aborted by hook"));
2276 }
2277
2278 return 0;
30173b88
MH
2279}
2280
2281int ref_transaction_abort(struct ref_transaction *transaction,
2282 struct strbuf *err)
2283{
2284 struct ref_store *refs = transaction->ref_store;
2285 int ret = 0;
2286
2287 switch (transaction->state) {
2288 case REF_TRANSACTION_OPEN:
2289 /* No need to abort explicitly. */
2290 break;
2291 case REF_TRANSACTION_PREPARED:
2292 ret = refs->be->transaction_abort(refs, transaction, err);
2293 break;
2294 case REF_TRANSACTION_CLOSED:
033abf97 2295 BUG("abort called on a closed reference transaction");
30173b88
MH
2296 break;
2297 default:
033abf97 2298 BUG("unexpected reference transaction state");
30173b88
MH
2299 break;
2300 }
2301
67541597
PS
2302 run_transaction_hook(transaction, "aborted");
2303
30173b88
MH
2304 ref_transaction_free(transaction);
2305 return ret;
2306}
2307
2308int ref_transaction_commit(struct ref_transaction *transaction,
2309 struct strbuf *err)
2310{
2311 struct ref_store *refs = transaction->ref_store;
2312 int ret;
2313
2314 switch (transaction->state) {
2315 case REF_TRANSACTION_OPEN:
2316 /* Need to prepare first. */
2317 ret = ref_transaction_prepare(transaction, err);
2318 if (ret)
2319 return ret;
2320 break;
2321 case REF_TRANSACTION_PREPARED:
2322 /* Fall through to finish. */
2323 break;
2324 case REF_TRANSACTION_CLOSED:
033abf97 2325 BUG("commit called on a closed reference transaction");
30173b88
MH
2326 break;
2327 default:
033abf97 2328 BUG("unexpected reference transaction state");
30173b88
MH
2329 break;
2330 }
2331
67541597
PS
2332 ret = refs->be->transaction_finish(refs, transaction, err);
2333 if (!ret)
2334 run_transaction_hook(transaction, "committed");
2335 return ret;
127b42a1 2336}
62665823 2337
7d2df051
NTND
2338int refs_verify_refname_available(struct ref_store *refs,
2339 const char *refname,
b05855b5 2340 const struct string_list *extras,
7d2df051
NTND
2341 const struct string_list *skip,
2342 struct strbuf *err)
62665823 2343{
b05855b5
MH
2344 const char *slash;
2345 const char *extra_refname;
2346 struct strbuf dirname = STRBUF_INIT;
2347 struct strbuf referent = STRBUF_INIT;
2348 struct object_id oid;
2349 unsigned int type;
2350 struct ref_iterator *iter;
2351 int ok;
2352 int ret = -1;
2353
2354 /*
2355 * For the sake of comments in this function, suppose that
2356 * refname is "refs/foo/bar".
2357 */
2358
2359 assert(err);
2360
2361 strbuf_grow(&dirname, strlen(refname) + 1);
2362 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
8b72fea7
HWN
2363 /*
2364 * Just saying "Is a directory" when we e.g. can't
2365 * lock some multi-level ref isn't very informative,
2366 * the user won't be told *what* is a directory, so
2367 * let's not use strerror() below.
2368 */
2369 int ignore_errno;
b05855b5
MH
2370 /* Expand dirname to the new prefix, not including the trailing slash: */
2371 strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
2372
2373 /*
2374 * We are still at a leading dir of the refname (e.g.,
2375 * "refs/foo"; if there is a reference with that name,
2376 * it is a conflict, *unless* it is in skip.
2377 */
2378 if (skip && string_list_has_string(skip, dirname.buf))
2379 continue;
2380
8b72fea7
HWN
2381 if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
2382 &type, &ignore_errno)) {
661558f0 2383 strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
b05855b5
MH
2384 dirname.buf, refname);
2385 goto cleanup;
2386 }
2387
2388 if (extras && string_list_has_string(extras, dirname.buf)) {
661558f0 2389 strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
b05855b5
MH
2390 refname, dirname.buf);
2391 goto cleanup;
2392 }
2393 }
2394
2395 /*
2396 * We are at the leaf of our refname (e.g., "refs/foo/bar").
2397 * There is no point in searching for a reference with that
2398 * name, because a refname isn't considered to conflict with
2399 * itself. But we still need to check for references whose
2400 * names are in the "refs/foo/bar/" namespace, because they
2401 * *do* conflict.
2402 */
2403 strbuf_addstr(&dirname, refname + dirname.len);
2404 strbuf_addch(&dirname, '/');
2405
2406 iter = refs_ref_iterator_begin(refs, dirname.buf, 0,
2407 DO_FOR_EACH_INCLUDE_BROKEN);
2408 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
2409 if (skip &&
2410 string_list_has_string(skip, iter->refname))
2411 continue;
2412
661558f0 2413 strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
b05855b5
MH
2414 iter->refname, refname);
2415 ref_iterator_abort(iter);
2416 goto cleanup;
2417 }
2418
2419 if (ok != ITER_DONE)
033abf97 2420 BUG("error while iterating over references");
b05855b5
MH
2421
2422 extra_refname = find_descendant_ref(dirname.buf, extras, skip);
2423 if (extra_refname)
661558f0 2424 strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
b05855b5
MH
2425 refname, extra_refname);
2426 else
2427 ret = 0;
2428
2429cleanup:
2430 strbuf_release(&referent);
2431 strbuf_release(&dirname);
2432 return ret;
62665823 2433}
e3688bd6 2434
7d2df051 2435int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
e3688bd6 2436{
e3688bd6 2437 struct ref_iterator *iter;
4a6067cd 2438 struct do_for_each_ref_help hp = { fn, cb_data };
e3688bd6
DT
2439
2440 iter = refs->be->reflog_iterator_begin(refs);
2441
4a6067cd
SB
2442 return do_for_each_repo_ref_iterator(the_repository, iter,
2443 do_for_each_ref_helper, &hp);
e3688bd6
DT
2444}
2445
7d2df051 2446int for_each_reflog(each_ref_fn fn, void *cb_data)
e3688bd6 2447{
23a3f0cb 2448 return refs_for_each_reflog(get_main_ref_store(the_repository), fn, cb_data);
7d2df051 2449}
e3688bd6 2450
7d2df051
NTND
2451int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
2452 const char *refname,
2453 each_reflog_ent_fn fn,
2454 void *cb_data)
2455{
e3688bd6
DT
2456 return refs->be->for_each_reflog_ent_reverse(refs, refname,
2457 fn, cb_data);
2458}
2459
7d2df051
NTND
2460int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
2461 void *cb_data)
2462{
23a3f0cb 2463 return refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository),
7d2df051
NTND
2464 refname, fn, cb_data);
2465}
2466
2467int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
2468 each_reflog_ent_fn fn, void *cb_data)
2469{
2470 return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
2471}
2472
e3688bd6
DT
2473int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,
2474 void *cb_data)
2475{
23a3f0cb 2476 return refs_for_each_reflog_ent(get_main_ref_store(the_repository), refname,
7d2df051
NTND
2477 fn, cb_data);
2478}
e3688bd6 2479
7d2df051
NTND
2480int refs_reflog_exists(struct ref_store *refs, const char *refname)
2481{
2482 return refs->be->reflog_exists(refs, refname);
e3688bd6
DT
2483}
2484
2485int reflog_exists(const char *refname)
2486{
23a3f0cb 2487 return refs_reflog_exists(get_main_ref_store(the_repository), refname);
7d2df051 2488}
e3688bd6 2489
7d2df051 2490int refs_create_reflog(struct ref_store *refs, const char *refname,
7b089120 2491 struct strbuf *err)
7d2df051 2492{
7b089120 2493 return refs->be->create_reflog(refs, refname, err);
e3688bd6
DT
2494}
2495
7b089120 2496int safe_create_reflog(const char *refname, struct strbuf *err)
e3688bd6 2497{
23a3f0cb 2498 return refs_create_reflog(get_main_ref_store(the_repository), refname,
7b089120 2499 err);
7d2df051 2500}
e3688bd6 2501
7d2df051
NTND
2502int refs_delete_reflog(struct ref_store *refs, const char *refname)
2503{
2504 return refs->be->delete_reflog(refs, refname);
e3688bd6
DT
2505}
2506
2507int delete_reflog(const char *refname)
2508{
23a3f0cb 2509 return refs_delete_reflog(get_main_ref_store(the_repository), refname);
7d2df051 2510}
e3688bd6 2511
7d2df051 2512int refs_reflog_expire(struct ref_store *refs,
cc40b5ce 2513 const char *refname,
7d2df051
NTND
2514 unsigned int flags,
2515 reflog_expiry_prepare_fn prepare_fn,
2516 reflog_expiry_should_prune_fn should_prune_fn,
2517 reflog_expiry_cleanup_fn cleanup_fn,
2518 void *policy_cb_data)
2519{
cc40b5ce 2520 return refs->be->reflog_expire(refs, refname, flags,
7d2df051
NTND
2521 prepare_fn, should_prune_fn,
2522 cleanup_fn, policy_cb_data);
e3688bd6
DT
2523}
2524
cc40b5ce 2525int reflog_expire(const char *refname,
e3688bd6
DT
2526 unsigned int flags,
2527 reflog_expiry_prepare_fn prepare_fn,
2528 reflog_expiry_should_prune_fn should_prune_fn,
2529 reflog_expiry_cleanup_fn cleanup_fn,
2530 void *policy_cb_data)
2531{
23a3f0cb 2532 return refs_reflog_expire(get_main_ref_store(the_repository),
cc40b5ce 2533 refname, flags,
7d2df051
NTND
2534 prepare_fn, should_prune_fn,
2535 cleanup_fn, policy_cb_data);
e3688bd6 2536}
fc681463
DT
2537
2538int initial_ref_transaction_commit(struct ref_transaction *transaction,
2539 struct strbuf *err)
2540{
c0fe4e8b 2541 struct ref_store *refs = transaction->ref_store;
fc681463
DT
2542
2543 return refs->be->initial_transaction_commit(refs, transaction, err);
2544}
a27dcf89 2545
4f2ba2d0
PS
2546void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
2547 ref_transaction_for_each_queued_update_fn cb,
2548 void *cb_data)
2549{
2550 int i;
2551
2552 for (i = 0; i < transaction->nr; i++) {
2553 struct ref_update *update = transaction->updates[i];
2554
2555 cb(update->refname,
2556 (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL,
2557 (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL,
2558 cb_data);
2559 }
2560}
2561
523fa69c 2562int refs_delete_refs(struct ref_store *refs, const char *logmsg,
64da4199 2563 struct string_list *refnames, unsigned int flags)
a27dcf89 2564{
523fa69c
JH
2565 char *msg;
2566 int retval;
2567
2568 msg = normalize_reflog_message(logmsg);
2569 retval = refs->be->delete_refs(refs, msg, refnames, flags);
2570 free(msg);
2571 return retval;
a27dcf89 2572}
9b6b40d9 2573
64da4199
MH
2574int delete_refs(const char *msg, struct string_list *refnames,
2575 unsigned int flags)
9b6b40d9 2576{
23a3f0cb 2577 return refs_delete_refs(get_main_ref_store(the_repository), msg, refnames, flags);
7d2df051 2578}
9b6b40d9 2579
7d2df051
NTND
2580int refs_rename_ref(struct ref_store *refs, const char *oldref,
2581 const char *newref, const char *logmsg)
2582{
523fa69c
JH
2583 char *msg;
2584 int retval;
2585
2586 msg = normalize_reflog_message(logmsg);
2587 retval = refs->be->rename_ref(refs, oldref, newref, msg);
2588 free(msg);
2589 return retval;
9b6b40d9 2590}
7d2df051
NTND
2591
2592int rename_ref(const char *oldref, const char *newref, const char *logmsg)
2593{
23a3f0cb 2594 return refs_rename_ref(get_main_ref_store(the_repository), oldref, newref, logmsg);
7d2df051 2595}
52d59cc6
SD
2596
2597int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
2598 const char *newref, const char *logmsg)
2599{
523fa69c
JH
2600 char *msg;
2601 int retval;
2602
2603 msg = normalize_reflog_message(logmsg);
2604 retval = refs->be->copy_ref(refs, oldref, newref, msg);
2605 free(msg);
2606 return retval;
52d59cc6
SD
2607}
2608
2609int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg)
2610{
23a3f0cb 2611 return refs_copy_existing_ref(get_main_ref_store(the_repository), oldref, newref, logmsg);
52d59cc6 2612}