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