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