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