]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - refs.c
The sixth batch
[thirdparty/git.git] / refs.c
... / ...
CommitLineData
1/*
2 * The backend-independent part of the reference module.
3 */
4
5#define USE_THE_REPOSITORY_VARIABLE
6#define DISABLE_SIGN_COMPARE_WARNINGS
7
8#include "git-compat-util.h"
9#include "advice.h"
10#include "config.h"
11#include "environment.h"
12#include "strmap.h"
13#include "gettext.h"
14#include "hex.h"
15#include "lockfile.h"
16#include "iterator.h"
17#include "refs.h"
18#include "refs/refs-internal.h"
19#include "run-command.h"
20#include "hook.h"
21#include "object-name.h"
22#include "object-store.h"
23#include "object.h"
24#include "path.h"
25#include "submodule.h"
26#include "worktree.h"
27#include "strvec.h"
28#include "repo-settings.h"
29#include "setup.h"
30#include "sigchain.h"
31#include "date.h"
32#include "commit.h"
33#include "wildmatch.h"
34#include "ident.h"
35
36/*
37 * List of all available backends
38 */
39static const struct ref_storage_be *refs_backends[] = {
40 [REF_STORAGE_FORMAT_FILES] = &refs_be_files,
41 [REF_STORAGE_FORMAT_REFTABLE] = &refs_be_reftable,
42};
43
44static const struct ref_storage_be *find_ref_storage_backend(
45 enum ref_storage_format ref_storage_format)
46{
47 if (ref_storage_format < ARRAY_SIZE(refs_backends))
48 return refs_backends[ref_storage_format];
49 return NULL;
50}
51
52enum ref_storage_format ref_storage_format_by_name(const char *name)
53{
54 for (unsigned int i = 0; i < ARRAY_SIZE(refs_backends); i++)
55 if (refs_backends[i] && !strcmp(refs_backends[i]->name, name))
56 return i;
57 return REF_STORAGE_FORMAT_UNKNOWN;
58}
59
60const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format)
61{
62 const struct ref_storage_be *be = find_ref_storage_backend(ref_storage_format);
63 if (!be)
64 return "unknown";
65 return be->name;
66}
67
68/*
69 * How to handle various characters in refnames:
70 * 0: An acceptable character for refs
71 * 1: End-of-component
72 * 2: ., look for a preceding . to reject .. in refs
73 * 3: {, look for a preceding @ to reject @{ in refs
74 * 4: A bad character: ASCII control characters, and
75 * ":", "?", "[", "\", "^", "~", SP, or TAB
76 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
77 */
78static unsigned char refname_disposition[256] = {
79 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
80 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
81 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
87};
88
89struct ref_namespace_info ref_namespace[] = {
90 [NAMESPACE_HEAD] = {
91 .ref = "HEAD",
92 .decoration = DECORATION_REF_HEAD,
93 .exact = 1,
94 },
95 [NAMESPACE_BRANCHES] = {
96 .ref = "refs/heads/",
97 .decoration = DECORATION_REF_LOCAL,
98 },
99 [NAMESPACE_TAGS] = {
100 .ref = "refs/tags/",
101 .decoration = DECORATION_REF_TAG,
102 },
103 [NAMESPACE_REMOTE_REFS] = {
104 /*
105 * The default refspec for new remotes copies refs from
106 * refs/heads/ on the remote into refs/remotes/<remote>/.
107 * As such, "refs/remotes/" has special handling.
108 */
109 .ref = "refs/remotes/",
110 .decoration = DECORATION_REF_REMOTE,
111 },
112 [NAMESPACE_STASH] = {
113 /*
114 * The single ref "refs/stash" stores the latest stash.
115 * Older stashes can be found in the reflog.
116 */
117 .ref = "refs/stash",
118 .exact = 1,
119 .decoration = DECORATION_REF_STASH,
120 },
121 [NAMESPACE_REPLACE] = {
122 /*
123 * This namespace allows Git to act as if one object ID
124 * points to the content of another. Unlike the other
125 * ref namespaces, this one can be changed by the
126 * GIT_REPLACE_REF_BASE environment variable. This
127 * .namespace value will be overwritten in setup_git_env().
128 */
129 .ref = "refs/replace/",
130 .decoration = DECORATION_GRAFTED,
131 },
132 [NAMESPACE_NOTES] = {
133 /*
134 * The refs/notes/commit ref points to the tip of a
135 * parallel commit history that adds metadata to commits
136 * in the normal history. This ref can be overwritten
137 * by the core.notesRef config variable or the
138 * GIT_NOTES_REFS environment variable.
139 */
140 .ref = "refs/notes/commit",
141 .exact = 1,
142 },
143 [NAMESPACE_PREFETCH] = {
144 /*
145 * Prefetch refs are written by the background 'fetch'
146 * maintenance task. It allows faster foreground fetches
147 * by advertising these previously-downloaded tips without
148 * updating refs/remotes/ without user intervention.
149 */
150 .ref = "refs/prefetch/",
151 },
152 [NAMESPACE_REWRITTEN] = {
153 /*
154 * Rewritten refs are used by the 'label' command in the
155 * sequencer. These are particularly useful during an
156 * interactive rebase that uses the 'merge' command.
157 */
158 .ref = "refs/rewritten/",
159 },
160};
161
162void update_ref_namespace(enum ref_namespace namespace, char *ref)
163{
164 struct ref_namespace_info *info = &ref_namespace[namespace];
165 if (info->ref_updated)
166 free((char *)info->ref);
167 info->ref = ref;
168 info->ref_updated = 1;
169}
170
171/*
172 * Try to read one refname component from the front of refname.
173 * Return the length of the component found, or -1 if the component is
174 * not legal. It is legal if it is something reasonable to have under
175 * ".git/refs/"; We do not like it if:
176 *
177 * - it begins with ".", or
178 * - it has double dots "..", or
179 * - it has ASCII control characters, or
180 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
181 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
182 * - it ends with a "/", or
183 * - it ends with ".lock", or
184 * - it contains a "@{" portion
185 *
186 * When sanitized is not NULL, instead of rejecting the input refname
187 * as an error, try to come up with a usable replacement for the input
188 * refname in it.
189 */
190static int check_refname_component(const char *refname, int *flags,
191 struct strbuf *sanitized)
192{
193 const char *cp;
194 char last = '\0';
195 size_t component_start = 0; /* garbage - not a reasonable initial value */
196
197 if (sanitized)
198 component_start = sanitized->len;
199
200 for (cp = refname; ; cp++) {
201 int ch = *cp & 255;
202 unsigned char disp = refname_disposition[ch];
203
204 if (sanitized && disp != 1)
205 strbuf_addch(sanitized, ch);
206
207 switch (disp) {
208 case 1:
209 goto out;
210 case 2:
211 if (last == '.') { /* Refname contains "..". */
212 if (sanitized)
213 /* collapse ".." to single "." */
214 strbuf_setlen(sanitized, sanitized->len - 1);
215 else
216 return -1;
217 }
218 break;
219 case 3:
220 if (last == '@') { /* Refname contains "@{". */
221 if (sanitized)
222 sanitized->buf[sanitized->len-1] = '-';
223 else
224 return -1;
225 }
226 break;
227 case 4:
228 /* forbidden char */
229 if (sanitized)
230 sanitized->buf[sanitized->len-1] = '-';
231 else
232 return -1;
233 break;
234 case 5:
235 if (!(*flags & REFNAME_REFSPEC_PATTERN)) {
236 /* refspec can't be a pattern */
237 if (sanitized)
238 sanitized->buf[sanitized->len-1] = '-';
239 else
240 return -1;
241 }
242
243 /*
244 * Unset the pattern flag so that we only accept
245 * a single asterisk for one side of refspec.
246 */
247 *flags &= ~ REFNAME_REFSPEC_PATTERN;
248 break;
249 }
250 last = ch;
251 }
252out:
253 if (cp == refname)
254 return 0; /* Component has zero length. */
255
256 if (refname[0] == '.') { /* Component starts with '.'. */
257 if (sanitized)
258 sanitized->buf[component_start] = '-';
259 else
260 return -1;
261 }
262 if (cp - refname >= LOCK_SUFFIX_LEN &&
263 !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) {
264 if (!sanitized)
265 return -1;
266 /* Refname ends with ".lock". */
267 while (strbuf_strip_suffix(sanitized, LOCK_SUFFIX)) {
268 /* try again in case we have .lock.lock */
269 }
270 }
271 return cp - refname;
272}
273
274static int check_or_sanitize_refname(const char *refname, int flags,
275 struct strbuf *sanitized)
276{
277 int component_len, component_count = 0;
278
279 if (!strcmp(refname, "@")) {
280 /* Refname is a single character '@'. */
281 if (sanitized)
282 strbuf_addch(sanitized, '-');
283 else
284 return -1;
285 }
286
287 while (1) {
288 if (sanitized && sanitized->len)
289 strbuf_complete(sanitized, '/');
290
291 /* We are at the start of a path component. */
292 component_len = check_refname_component(refname, &flags,
293 sanitized);
294 if (sanitized && component_len == 0)
295 ; /* OK, omit empty component */
296 else if (component_len <= 0)
297 return -1;
298
299 component_count++;
300 if (refname[component_len] == '\0')
301 break;
302 /* Skip to next component. */
303 refname += component_len + 1;
304 }
305
306 if (refname[component_len - 1] == '.') {
307 /* Refname ends with '.'. */
308 if (sanitized)
309 ; /* omit ending dot */
310 else
311 return -1;
312 }
313 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
314 return -1; /* Refname has only one component. */
315 return 0;
316}
317
318int check_refname_format(const char *refname, int flags)
319{
320 return check_or_sanitize_refname(refname, flags, NULL);
321}
322
323int refs_fsck(struct ref_store *refs, struct fsck_options *o,
324 struct worktree *wt)
325{
326 return refs->be->fsck(refs, o, wt);
327}
328
329void sanitize_refname_component(const char *refname, struct strbuf *out)
330{
331 if (check_or_sanitize_refname(refname, REFNAME_ALLOW_ONELEVEL, out))
332 BUG("sanitizing refname '%s' check returned error", refname);
333}
334
335int refname_is_safe(const char *refname)
336{
337 const char *rest;
338
339 if (skip_prefix(refname, "refs/", &rest)) {
340 char *buf;
341 int result;
342 size_t restlen = strlen(rest);
343
344 /* rest must not be empty, or start or end with "/" */
345 if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
346 return 0;
347
348 /*
349 * Does the refname try to escape refs/?
350 * For example: refs/foo/../bar is safe but refs/foo/../../bar
351 * is not.
352 */
353 buf = xmallocz(restlen);
354 result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
355 free(buf);
356 return result;
357 }
358
359 do {
360 if (!isupper(*refname) && *refname != '_')
361 return 0;
362 refname++;
363 } while (*refname);
364 return 1;
365}
366
367/*
368 * Return true if refname, which has the specified oid and flags, can
369 * be resolved to an object in the database. If the referred-to object
370 * does not exist, emit a warning and return false.
371 */
372int ref_resolves_to_object(const char *refname,
373 struct repository *repo,
374 const struct object_id *oid,
375 unsigned int flags)
376{
377 if (flags & REF_ISBROKEN)
378 return 0;
379 if (!has_object(repo, oid, HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) {
380 error(_("%s does not point to a valid object!"), refname);
381 return 0;
382 }
383 return 1;
384}
385
386char *refs_resolve_refdup(struct ref_store *refs,
387 const char *refname, int resolve_flags,
388 struct object_id *oid, int *flags)
389{
390 const char *result;
391
392 result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
393 oid, flags);
394 return xstrdup_or_null(result);
395}
396
397/* The argument to for_each_filter_refs */
398struct for_each_ref_filter {
399 const char *pattern;
400 const char *prefix;
401 each_ref_fn *fn;
402 void *cb_data;
403};
404
405int refs_read_ref_full(struct ref_store *refs, const char *refname,
406 int resolve_flags, struct object_id *oid, int *flags)
407{
408 if (refs_resolve_ref_unsafe(refs, refname, resolve_flags,
409 oid, flags))
410 return 0;
411 return -1;
412}
413
414int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid)
415{
416 return refs_read_ref_full(refs, refname, RESOLVE_REF_READING, oid, NULL);
417}
418
419int refs_ref_exists(struct ref_store *refs, const char *refname)
420{
421 return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING,
422 NULL, NULL);
423}
424
425static int for_each_filter_refs(const char *refname, const char *referent,
426 const struct object_id *oid,
427 int flags, void *data)
428{
429 struct for_each_ref_filter *filter = data;
430
431 if (wildmatch(filter->pattern, refname, 0))
432 return 0;
433 if (filter->prefix)
434 skip_prefix(refname, filter->prefix, &refname);
435 return filter->fn(refname, referent, oid, flags, filter->cb_data);
436}
437
438struct warn_if_dangling_data {
439 struct ref_store *refs;
440 FILE *fp;
441 const char *refname;
442 const struct string_list *refnames;
443 const char *msg_fmt;
444};
445
446static int warn_if_dangling_symref(const char *refname, const char *referent UNUSED,
447 const struct object_id *oid UNUSED,
448 int flags, void *cb_data)
449{
450 struct warn_if_dangling_data *d = cb_data;
451 const char *resolves_to;
452
453 if (!(flags & REF_ISSYMREF))
454 return 0;
455
456 resolves_to = refs_resolve_ref_unsafe(d->refs, refname, 0, NULL, NULL);
457 if (!resolves_to
458 || (d->refname
459 ? strcmp(resolves_to, d->refname)
460 : !string_list_has_string(d->refnames, resolves_to))) {
461 return 0;
462 }
463
464 fprintf(d->fp, d->msg_fmt, refname);
465 fputc('\n', d->fp);
466 return 0;
467}
468
469void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp,
470 const char *msg_fmt, const char *refname)
471{
472 struct warn_if_dangling_data data = {
473 .refs = refs,
474 .fp = fp,
475 .refname = refname,
476 .msg_fmt = msg_fmt,
477 };
478 refs_for_each_rawref(refs, warn_if_dangling_symref, &data);
479}
480
481void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
482 const char *msg_fmt, const struct string_list *refnames)
483{
484 struct warn_if_dangling_data data = {
485 .refs = refs,
486 .fp = fp,
487 .refnames = refnames,
488 .msg_fmt = msg_fmt,
489 };
490 refs_for_each_rawref(refs, warn_if_dangling_symref, &data);
491}
492
493int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
494{
495 return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data);
496}
497
498int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
499{
500 return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data);
501}
502
503int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
504{
505 return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data);
506}
507
508int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data)
509{
510 struct strbuf buf = STRBUF_INIT;
511 int ret = 0;
512 struct object_id oid;
513 int flag;
514
515 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
516 if (!refs_read_ref_full(refs, buf.buf, RESOLVE_REF_READING, &oid, &flag))
517 ret = fn(buf.buf, NULL, &oid, flag, cb_data);
518 strbuf_release(&buf);
519
520 return ret;
521}
522
523void normalize_glob_ref(struct string_list_item *item, const char *prefix,
524 const char *pattern)
525{
526 struct strbuf normalized_pattern = STRBUF_INIT;
527
528 if (*pattern == '/')
529 BUG("pattern must not start with '/'");
530
531 if (prefix)
532 strbuf_addstr(&normalized_pattern, prefix);
533 else if (!starts_with(pattern, "refs/") &&
534 strcmp(pattern, "HEAD"))
535 strbuf_addstr(&normalized_pattern, "refs/");
536 /*
537 * NEEDSWORK: Special case other symrefs such as REBASE_HEAD,
538 * MERGE_HEAD, etc.
539 */
540
541 strbuf_addstr(&normalized_pattern, pattern);
542 strbuf_strip_suffix(&normalized_pattern, "/");
543
544 item->string = strbuf_detach(&normalized_pattern, NULL);
545 item->util = has_glob_specials(pattern) ? NULL : item->string;
546 strbuf_release(&normalized_pattern);
547}
548
549int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn,
550 const char *pattern, const char *prefix, void *cb_data)
551{
552 struct strbuf real_pattern = STRBUF_INIT;
553 struct for_each_ref_filter filter;
554 int ret;
555
556 if (!prefix && !starts_with(pattern, "refs/"))
557 strbuf_addstr(&real_pattern, "refs/");
558 else if (prefix)
559 strbuf_addstr(&real_pattern, prefix);
560 strbuf_addstr(&real_pattern, pattern);
561
562 if (!has_glob_specials(pattern)) {
563 /* Append implied '/' '*' if not present. */
564 strbuf_complete(&real_pattern, '/');
565 /* No need to check for '*', there is none. */
566 strbuf_addch(&real_pattern, '*');
567 }
568
569 filter.pattern = real_pattern.buf;
570 filter.prefix = prefix;
571 filter.fn = fn;
572 filter.cb_data = cb_data;
573 ret = refs_for_each_ref(refs, for_each_filter_refs, &filter);
574
575 strbuf_release(&real_pattern);
576 return ret;
577}
578
579int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
580 const char *pattern, void *cb_data)
581{
582 return refs_for_each_glob_ref_in(refs, fn, pattern, NULL, cb_data);
583}
584
585const char *prettify_refname(const char *name)
586{
587 if (skip_prefix(name, "refs/heads/", &name) ||
588 skip_prefix(name, "refs/tags/", &name) ||
589 skip_prefix(name, "refs/remotes/", &name))
590 ; /* nothing */
591 return name;
592}
593
594static const char *ref_rev_parse_rules[] = {
595 "%.*s",
596 "refs/%.*s",
597 "refs/tags/%.*s",
598 "refs/heads/%.*s",
599 "refs/remotes/%.*s",
600 "refs/remotes/%.*s/HEAD",
601 NULL
602};
603
604#define NUM_REV_PARSE_RULES (ARRAY_SIZE(ref_rev_parse_rules) - 1)
605
606/*
607 * Is it possible that the caller meant full_name with abbrev_name?
608 * If so return a non-zero value to signal "yes"; the magnitude of
609 * the returned value gives the precedence used for disambiguation.
610 *
611 * If abbrev_name cannot mean full_name, return 0.
612 */
613int refname_match(const char *abbrev_name, const char *full_name)
614{
615 const char **p;
616 const int abbrev_name_len = strlen(abbrev_name);
617 const int num_rules = NUM_REV_PARSE_RULES;
618
619 for (p = ref_rev_parse_rules; *p; p++)
620 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name)))
621 return &ref_rev_parse_rules[num_rules] - p;
622
623 return 0;
624}
625
626/*
627 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
628 * the results to 'prefixes'
629 */
630void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
631{
632 const char **p;
633 int len = strlen(prefix);
634
635 for (p = ref_rev_parse_rules; *p; p++)
636 strvec_pushf(prefixes, *p, len, prefix);
637}
638
639static const char default_branch_name_advice[] = N_(
640"Using '%s' as the name for the initial branch. This default branch name\n"
641"is subject to change. To configure the initial branch name to use in all\n"
642"of your new repositories, which will suppress this warning, call:\n"
643"\n"
644"\tgit config --global init.defaultBranch <name>\n"
645"\n"
646"Names commonly chosen instead of 'master' are 'main', 'trunk' and\n"
647"'development'. The just-created branch can be renamed via this command:\n"
648"\n"
649"\tgit branch -m <name>\n"
650);
651
652char *repo_default_branch_name(struct repository *r, int quiet)
653{
654 const char *config_key = "init.defaultbranch";
655 const char *config_display_key = "init.defaultBranch";
656 char *ret = NULL, *full_ref;
657 const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME");
658
659 if (env && *env)
660 ret = xstrdup(env);
661 else if (repo_config_get_string(r, config_key, &ret) < 0)
662 die(_("could not retrieve `%s`"), config_display_key);
663
664 if (!ret) {
665 ret = xstrdup("master");
666 if (!quiet)
667 advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME,
668 _(default_branch_name_advice), ret);
669 }
670
671 full_ref = xstrfmt("refs/heads/%s", ret);
672 if (check_refname_format(full_ref, 0))
673 die(_("invalid branch name: %s = %s"), config_display_key, ret);
674 free(full_ref);
675
676 return ret;
677}
678
679/*
680 * *string and *len will only be substituted, and *string returned (for
681 * later free()ing) if the string passed in is a magic short-hand form
682 * to name a branch.
683 */
684static char *substitute_branch_name(struct repository *r,
685 const char **string, int *len,
686 int nonfatal_dangling_mark)
687{
688 struct strbuf buf = STRBUF_INIT;
689 struct interpret_branch_name_options options = {
690 .nonfatal_dangling_mark = nonfatal_dangling_mark
691 };
692 int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options);
693
694 if (ret == *len) {
695 size_t size;
696 *string = strbuf_detach(&buf, &size);
697 *len = size;
698 return (char *)*string;
699 }
700
701 return NULL;
702}
703
704void copy_branchname(struct strbuf *sb, const char *name, unsigned allowed)
705{
706 int len = strlen(name);
707 struct interpret_branch_name_options options = {
708 .allowed = allowed
709 };
710 int used = repo_interpret_branch_name(the_repository, name, len, sb,
711 &options);
712
713 if (used < 0)
714 used = 0;
715 strbuf_add(sb, name + used, len - used);
716}
717
718int check_branch_ref(struct strbuf *sb, const char *name)
719{
720 if (startup_info->have_repository)
721 copy_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
722 else
723 strbuf_addstr(sb, name);
724
725 /*
726 * This splice must be done even if we end up rejecting the
727 * name; builtin/branch.c::copy_or_rename_branch() still wants
728 * to see what the name expanded to so that "branch -m" can be
729 * used as a tool to correct earlier mistakes.
730 */
731 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
732
733 if (*name == '-' ||
734 !strcmp(sb->buf, "refs/heads/HEAD"))
735 return -1;
736
737 return check_refname_format(sb->buf, 0);
738}
739
740int check_tag_ref(struct strbuf *sb, const char *name)
741{
742 if (name[0] == '-' || !strcmp(name, "HEAD"))
743 return -1;
744
745 strbuf_reset(sb);
746 strbuf_addf(sb, "refs/tags/%s", name);
747
748 return check_refname_format(sb->buf, 0);
749}
750
751int repo_dwim_ref(struct repository *r, const char *str, int len,
752 struct object_id *oid, char **ref, int nonfatal_dangling_mark)
753{
754 char *last_branch = substitute_branch_name(r, &str, &len,
755 nonfatal_dangling_mark);
756 int refs_found = expand_ref(r, str, len, oid, ref);
757 free(last_branch);
758 return refs_found;
759}
760
761int expand_ref(struct repository *repo, const char *str, int len,
762 struct object_id *oid, char **ref)
763{
764 const char **p, *r;
765 int refs_found = 0;
766 struct strbuf fullref = STRBUF_INIT;
767
768 *ref = NULL;
769 for (p = ref_rev_parse_rules; *p; p++) {
770 struct object_id oid_from_ref;
771 struct object_id *this_result;
772 int flag;
773 struct ref_store *refs = get_main_ref_store(repo);
774
775 this_result = refs_found ? &oid_from_ref : oid;
776 strbuf_reset(&fullref);
777 strbuf_addf(&fullref, *p, len, str);
778 r = refs_resolve_ref_unsafe(refs, fullref.buf,
779 RESOLVE_REF_READING,
780 this_result, &flag);
781 if (r) {
782 if (!refs_found++)
783 *ref = xstrdup(r);
784 if (!repo_settings_get_warn_ambiguous_refs(repo))
785 break;
786 } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
787 warning(_("ignoring dangling symref %s"), fullref.buf);
788 } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) {
789 warning(_("ignoring broken ref %s"), fullref.buf);
790 }
791 }
792 strbuf_release(&fullref);
793 return refs_found;
794}
795
796int repo_dwim_log(struct repository *r, const char *str, int len,
797 struct object_id *oid, char **log)
798{
799 struct ref_store *refs = get_main_ref_store(r);
800 char *last_branch = substitute_branch_name(r, &str, &len, 0);
801 const char **p;
802 int logs_found = 0;
803 struct strbuf path = STRBUF_INIT;
804
805 *log = NULL;
806 for (p = ref_rev_parse_rules; *p; p++) {
807 struct object_id hash;
808 const char *ref, *it;
809
810 strbuf_reset(&path);
811 strbuf_addf(&path, *p, len, str);
812 ref = refs_resolve_ref_unsafe(refs, path.buf,
813 RESOLVE_REF_READING,
814 oid ? &hash : NULL, NULL);
815 if (!ref)
816 continue;
817 if (refs_reflog_exists(refs, path.buf))
818 it = path.buf;
819 else if (strcmp(ref, path.buf) &&
820 refs_reflog_exists(refs, ref))
821 it = ref;
822 else
823 continue;
824 if (!logs_found++) {
825 *log = xstrdup(it);
826 if (oid)
827 oidcpy(oid, &hash);
828 }
829 if (!repo_settings_get_warn_ambiguous_refs(r))
830 break;
831 }
832 strbuf_release(&path);
833 free(last_branch);
834 return logs_found;
835}
836
837int is_per_worktree_ref(const char *refname)
838{
839 return starts_with(refname, "refs/worktree/") ||
840 starts_with(refname, "refs/bisect/") ||
841 starts_with(refname, "refs/rewritten/");
842}
843
844int is_pseudo_ref(const char *refname)
845{
846 static const char * const pseudo_refs[] = {
847 "FETCH_HEAD",
848 "MERGE_HEAD",
849 };
850 size_t i;
851
852 for (i = 0; i < ARRAY_SIZE(pseudo_refs); i++)
853 if (!strcmp(refname, pseudo_refs[i]))
854 return 1;
855
856 return 0;
857}
858
859static int is_root_ref_syntax(const char *refname)
860{
861 const char *c;
862
863 for (c = refname; *c; c++) {
864 if (!isupper(*c) && *c != '-' && *c != '_')
865 return 0;
866 }
867
868 return 1;
869}
870
871int is_root_ref(const char *refname)
872{
873 static const char *const irregular_root_refs[] = {
874 "HEAD",
875 "AUTO_MERGE",
876 "BISECT_EXPECTED_REV",
877 "NOTES_MERGE_PARTIAL",
878 "NOTES_MERGE_REF",
879 "MERGE_AUTOSTASH",
880 };
881 size_t i;
882
883 if (!is_root_ref_syntax(refname) ||
884 is_pseudo_ref(refname))
885 return 0;
886
887 if (ends_with(refname, "_HEAD"))
888 return 1;
889
890 for (i = 0; i < ARRAY_SIZE(irregular_root_refs); i++)
891 if (!strcmp(refname, irregular_root_refs[i]))
892 return 1;
893
894 return 0;
895}
896
897static int is_current_worktree_ref(const char *ref) {
898 return is_root_ref_syntax(ref) || is_per_worktree_ref(ref);
899}
900
901enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
902 const char **worktree_name, int *worktree_name_length,
903 const char **bare_refname)
904{
905 const char *name_dummy;
906 int name_length_dummy;
907 const char *ref_dummy;
908
909 if (!worktree_name)
910 worktree_name = &name_dummy;
911 if (!worktree_name_length)
912 worktree_name_length = &name_length_dummy;
913 if (!bare_refname)
914 bare_refname = &ref_dummy;
915
916 if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) {
917 const char *slash = strchr(*bare_refname, '/');
918
919 *worktree_name = *bare_refname;
920 if (!slash) {
921 *worktree_name_length = strlen(*worktree_name);
922
923 /* This is an error condition, and the caller tell because the bare_refname is "" */
924 *bare_refname = *worktree_name + *worktree_name_length;
925 return REF_WORKTREE_OTHER;
926 }
927
928 *worktree_name_length = slash - *bare_refname;
929 *bare_refname = slash + 1;
930
931 if (is_current_worktree_ref(*bare_refname))
932 return REF_WORKTREE_OTHER;
933 }
934
935 *worktree_name = NULL;
936 *worktree_name_length = 0;
937
938 if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname)
939 && is_current_worktree_ref(*bare_refname))
940 return REF_WORKTREE_MAIN;
941
942 *bare_refname = maybe_worktree_ref;
943 if (is_current_worktree_ref(maybe_worktree_ref))
944 return REF_WORKTREE_CURRENT;
945
946 return REF_WORKTREE_SHARED;
947}
948
949long get_files_ref_lock_timeout_ms(void)
950{
951 static int configured = 0;
952
953 /* The default timeout is 100 ms: */
954 static int timeout_ms = 100;
955
956 if (!configured) {
957 git_config_get_int("core.filesreflocktimeout", &timeout_ms);
958 configured = 1;
959 }
960
961 return timeout_ms;
962}
963
964int refs_delete_ref(struct ref_store *refs, const char *msg,
965 const char *refname,
966 const struct object_id *old_oid,
967 unsigned int flags)
968{
969 struct ref_transaction *transaction;
970 struct strbuf err = STRBUF_INIT;
971
972 transaction = ref_store_transaction_begin(refs, 0, &err);
973 if (!transaction ||
974 ref_transaction_delete(transaction, refname, old_oid,
975 NULL, flags, msg, &err) ||
976 ref_transaction_commit(transaction, &err)) {
977 error("%s", err.buf);
978 ref_transaction_free(transaction);
979 strbuf_release(&err);
980 return 1;
981 }
982 ref_transaction_free(transaction);
983 strbuf_release(&err);
984 return 0;
985}
986
987static void copy_reflog_msg(struct strbuf *sb, const char *msg)
988{
989 char c;
990 int wasspace = 1;
991
992 while ((c = *msg++)) {
993 if (wasspace && isspace(c))
994 continue;
995 wasspace = isspace(c);
996 if (wasspace)
997 c = ' ';
998 strbuf_addch(sb, c);
999 }
1000 strbuf_rtrim(sb);
1001}
1002
1003static char *normalize_reflog_message(const char *msg)
1004{
1005 struct strbuf sb = STRBUF_INIT;
1006
1007 if (msg && *msg)
1008 copy_reflog_msg(&sb, msg);
1009 return strbuf_detach(&sb, NULL);
1010}
1011
1012int should_autocreate_reflog(enum log_refs_config log_all_ref_updates,
1013 const char *refname)
1014{
1015 switch (log_all_ref_updates) {
1016 case LOG_REFS_ALWAYS:
1017 return 1;
1018 case LOG_REFS_NORMAL:
1019 return starts_with(refname, "refs/heads/") ||
1020 starts_with(refname, "refs/remotes/") ||
1021 starts_with(refname, "refs/notes/") ||
1022 !strcmp(refname, "HEAD");
1023 default:
1024 return 0;
1025 }
1026}
1027
1028int is_branch(const char *refname)
1029{
1030 return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
1031}
1032
1033struct read_ref_at_cb {
1034 const char *refname;
1035 timestamp_t at_time;
1036 int cnt;
1037 int reccnt;
1038 struct object_id *oid;
1039 int found_it;
1040
1041 struct object_id ooid;
1042 struct object_id noid;
1043 int tz;
1044 timestamp_t date;
1045 char **msg;
1046 timestamp_t *cutoff_time;
1047 int *cutoff_tz;
1048 int *cutoff_cnt;
1049};
1050
1051static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
1052 timestamp_t timestamp, int tz, const char *message)
1053{
1054 if (cb->msg)
1055 *cb->msg = xstrdup(message);
1056 if (cb->cutoff_time)
1057 *cb->cutoff_time = timestamp;
1058 if (cb->cutoff_tz)
1059 *cb->cutoff_tz = tz;
1060 if (cb->cutoff_cnt)
1061 *cb->cutoff_cnt = cb->reccnt;
1062}
1063
1064static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
1065 const char *email UNUSED,
1066 timestamp_t timestamp, int tz,
1067 const char *message, void *cb_data)
1068{
1069 struct read_ref_at_cb *cb = cb_data;
1070
1071 cb->tz = tz;
1072 cb->date = timestamp;
1073
1074 if (timestamp <= cb->at_time || cb->cnt == 0) {
1075 set_read_ref_cutoffs(cb, timestamp, tz, message);
1076 /*
1077 * we have not yet updated cb->[n|o]oid so they still
1078 * hold the values for the previous record.
1079 */
1080 if (!is_null_oid(&cb->ooid)) {
1081 oidcpy(cb->oid, noid);
1082 if (!oideq(&cb->ooid, noid))
1083 warning(_("log for ref %s has gap after %s"),
1084 cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
1085 }
1086 else if (cb->date == cb->at_time)
1087 oidcpy(cb->oid, noid);
1088 else if (!oideq(noid, cb->oid))
1089 warning(_("log for ref %s unexpectedly ended on %s"),
1090 cb->refname, show_date(cb->date, cb->tz,
1091 DATE_MODE(RFC2822)));
1092 cb->reccnt++;
1093 oidcpy(&cb->ooid, ooid);
1094 oidcpy(&cb->noid, noid);
1095 cb->found_it = 1;
1096 return 1;
1097 }
1098 cb->reccnt++;
1099 oidcpy(&cb->ooid, ooid);
1100 oidcpy(&cb->noid, noid);
1101 if (cb->cnt > 0)
1102 cb->cnt--;
1103 return 0;
1104}
1105
1106static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
1107 const char *email UNUSED,
1108 timestamp_t timestamp, int tz,
1109 const char *message, void *cb_data)
1110{
1111 struct read_ref_at_cb *cb = cb_data;
1112
1113 set_read_ref_cutoffs(cb, timestamp, tz, message);
1114 oidcpy(cb->oid, ooid);
1115 if (cb->at_time && is_null_oid(cb->oid))
1116 oidcpy(cb->oid, noid);
1117 /* We just want the first entry */
1118 return 1;
1119}
1120
1121int read_ref_at(struct ref_store *refs, const char *refname,
1122 unsigned int flags, timestamp_t at_time, int cnt,
1123 struct object_id *oid, char **msg,
1124 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
1125{
1126 struct read_ref_at_cb cb;
1127
1128 memset(&cb, 0, sizeof(cb));
1129 cb.refname = refname;
1130 cb.at_time = at_time;
1131 cb.cnt = cnt;
1132 cb.msg = msg;
1133 cb.cutoff_time = cutoff_time;
1134 cb.cutoff_tz = cutoff_tz;
1135 cb.cutoff_cnt = cutoff_cnt;
1136 cb.oid = oid;
1137
1138 refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb);
1139
1140 if (!cb.reccnt) {
1141 if (cnt == 0) {
1142 /*
1143 * The caller asked for ref@{0}, and we had no entries.
1144 * It's a bit subtle, but in practice all callers have
1145 * prepped the "oid" field with the current value of
1146 * the ref, which is the most reasonable fallback.
1147 *
1148 * We'll put dummy values into the out-parameters (so
1149 * they're not just uninitialized garbage), and the
1150 * caller can take our return value as a hint that
1151 * we did not find any such reflog.
1152 */
1153 set_read_ref_cutoffs(&cb, 0, 0, "empty reflog");
1154 return 1;
1155 }
1156 if (flags & GET_OID_QUIETLY)
1157 exit(128);
1158 else
1159 die(_("log for %s is empty"), refname);
1160 }
1161 if (cb.found_it)
1162 return 0;
1163
1164 refs_for_each_reflog_ent(refs, refname, read_ref_at_ent_oldest, &cb);
1165
1166 return 1;
1167}
1168
1169struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
1170 unsigned int flags,
1171 struct strbuf *err)
1172{
1173 struct ref_transaction *tr;
1174 assert(err);
1175
1176 CALLOC_ARRAY(tr, 1);
1177 tr->ref_store = refs;
1178 tr->flags = flags;
1179 string_list_init_dup(&tr->refnames);
1180
1181 if (flags & REF_TRANSACTION_ALLOW_FAILURE)
1182 CALLOC_ARRAY(tr->rejections, 1);
1183
1184 return tr;
1185}
1186
1187void ref_transaction_free(struct ref_transaction *transaction)
1188{
1189 size_t i;
1190
1191 if (!transaction)
1192 return;
1193
1194 switch (transaction->state) {
1195 case REF_TRANSACTION_OPEN:
1196 case REF_TRANSACTION_CLOSED:
1197 /* OK */
1198 break;
1199 case REF_TRANSACTION_PREPARED:
1200 BUG("free called on a prepared reference transaction");
1201 break;
1202 default:
1203 BUG("unexpected reference transaction state");
1204 break;
1205 }
1206
1207 for (i = 0; i < transaction->nr; i++) {
1208 free(transaction->updates[i]->msg);
1209 free(transaction->updates[i]->committer_info);
1210 free((char *)transaction->updates[i]->new_target);
1211 free((char *)transaction->updates[i]->old_target);
1212 free(transaction->updates[i]);
1213 }
1214
1215 if (transaction->rejections)
1216 free(transaction->rejections->update_indices);
1217 free(transaction->rejections);
1218
1219 string_list_clear(&transaction->refnames, 0);
1220 free(transaction->updates);
1221 free(transaction);
1222}
1223
1224int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,
1225 size_t update_idx,
1226 enum ref_transaction_error err)
1227{
1228 if (update_idx >= transaction->nr)
1229 BUG("trying to set rejection on invalid update index");
1230
1231 if (!(transaction->flags & REF_TRANSACTION_ALLOW_FAILURE))
1232 return 0;
1233
1234 if (!transaction->rejections)
1235 BUG("transaction not inititalized with failure support");
1236
1237 /*
1238 * Don't accept generic errors, since these errors are not user
1239 * input related.
1240 */
1241 if (err == REF_TRANSACTION_ERROR_GENERIC)
1242 return 0;
1243
1244 transaction->updates[update_idx]->rejection_err = err;
1245 ALLOC_GROW(transaction->rejections->update_indices,
1246 transaction->rejections->nr + 1,
1247 transaction->rejections->alloc);
1248 transaction->rejections->update_indices[transaction->rejections->nr++] = update_idx;
1249
1250 return 1;
1251}
1252
1253struct ref_update *ref_transaction_add_update(
1254 struct ref_transaction *transaction,
1255 const char *refname, unsigned int flags,
1256 const struct object_id *new_oid,
1257 const struct object_id *old_oid,
1258 const char *new_target, const char *old_target,
1259 const char *committer_info,
1260 const char *msg)
1261{
1262 struct string_list_item *item;
1263 struct ref_update *update;
1264
1265 if (transaction->state != REF_TRANSACTION_OPEN)
1266 BUG("update called for transaction that is not open");
1267
1268 if (old_oid && old_target)
1269 BUG("only one of old_oid and old_target should be non NULL");
1270 if (new_oid && new_target)
1271 BUG("only one of new_oid and new_target should be non NULL");
1272
1273 FLEX_ALLOC_STR(update, refname, refname);
1274 ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
1275 transaction->updates[transaction->nr++] = update;
1276
1277 update->flags = flags;
1278 update->rejection_err = 0;
1279
1280 update->new_target = xstrdup_or_null(new_target);
1281 update->old_target = xstrdup_or_null(old_target);
1282 if ((flags & REF_HAVE_NEW) && new_oid)
1283 oidcpy(&update->new_oid, new_oid);
1284 if ((flags & REF_HAVE_OLD) && old_oid)
1285 oidcpy(&update->old_oid, old_oid);
1286 if (!(flags & REF_SKIP_CREATE_REFLOG)) {
1287 update->committer_info = xstrdup_or_null(committer_info);
1288 update->msg = normalize_reflog_message(msg);
1289 }
1290
1291 /*
1292 * This list is generally used by the backends to avoid duplicates.
1293 * But we do support multiple log updates for a given refname within
1294 * a single transaction.
1295 */
1296 if (!(update->flags & REF_LOG_ONLY)) {
1297 item = string_list_append(&transaction->refnames, refname);
1298 item->util = update;
1299 }
1300
1301 return update;
1302}
1303
1304static int transaction_refname_valid(const char *refname,
1305 const struct object_id *new_oid,
1306 unsigned int flags, struct strbuf *err)
1307{
1308 if (flags & REF_SKIP_REFNAME_VERIFICATION)
1309 return 1;
1310
1311 if (is_pseudo_ref(refname)) {
1312 const char *refusal_msg;
1313 if (flags & REF_LOG_ONLY)
1314 refusal_msg = _("refusing to update reflog for pseudoref '%s'");
1315 else
1316 refusal_msg = _("refusing to update pseudoref '%s'");
1317 strbuf_addf(err, refusal_msg, refname);
1318 return 0;
1319 } else if ((new_oid && !is_null_oid(new_oid)) ?
1320 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
1321 !refname_is_safe(refname)) {
1322 const char *refusal_msg;
1323 if (flags & REF_LOG_ONLY)
1324 refusal_msg = _("refusing to update reflog with bad name '%s'");
1325 else
1326 refusal_msg = _("refusing to update ref with bad name '%s'");
1327 strbuf_addf(err, refusal_msg, refname);
1328 return 0;
1329 }
1330
1331 return 1;
1332}
1333
1334int ref_transaction_update(struct ref_transaction *transaction,
1335 const char *refname,
1336 const struct object_id *new_oid,
1337 const struct object_id *old_oid,
1338 const char *new_target,
1339 const char *old_target,
1340 unsigned int flags, const char *msg,
1341 struct strbuf *err)
1342{
1343 assert(err);
1344
1345 if ((flags & REF_FORCE_CREATE_REFLOG) &&
1346 (flags & REF_SKIP_CREATE_REFLOG)) {
1347 strbuf_addstr(err, _("refusing to force and skip creation of reflog"));
1348 return -1;
1349 }
1350
1351 if (!transaction_refname_valid(refname, new_oid, flags, err))
1352 return -1;
1353
1354 if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
1355 BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
1356
1357 /*
1358 * Clear flags outside the allowed set; this should be a noop because
1359 * of the BUG() check above, but it works around a -Wnonnull warning
1360 * with some versions of "gcc -O3".
1361 */
1362 flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
1363
1364 flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
1365 flags |= (new_target ? REF_HAVE_NEW : 0) | (old_target ? REF_HAVE_OLD : 0);
1366
1367 ref_transaction_add_update(transaction, refname, flags,
1368 new_oid, old_oid, new_target,
1369 old_target, NULL, msg);
1370
1371 return 0;
1372}
1373
1374/*
1375 * Similar to`ref_transaction_update`, but this function is only for adding
1376 * a reflog update. Supports providing custom committer information. The index
1377 * field can be utiltized to order updates as desired. When not used, the
1378 * updates default to being ordered by refname.
1379 */
1380static int ref_transaction_update_reflog(struct ref_transaction *transaction,
1381 const char *refname,
1382 const struct object_id *new_oid,
1383 const struct object_id *old_oid,
1384 const char *committer_info,
1385 unsigned int flags,
1386 const char *msg,
1387 uint64_t index,
1388 struct strbuf *err)
1389{
1390 struct ref_update *update;
1391
1392 assert(err);
1393
1394 flags |= REF_LOG_ONLY | REF_FORCE_CREATE_REFLOG | REF_NO_DEREF;
1395
1396 if (!transaction_refname_valid(refname, new_oid, flags, err))
1397 return -1;
1398
1399 update = ref_transaction_add_update(transaction, refname, flags,
1400 new_oid, old_oid, NULL, NULL,
1401 committer_info, msg);
1402 /*
1403 * While we do set the old_oid value, we unset the flag to skip
1404 * old_oid verification which only makes sense for refs.
1405 */
1406 update->flags &= ~REF_HAVE_OLD;
1407 update->index = index;
1408
1409 /*
1410 * Reference backends may need to know the max index to optimize
1411 * their writes. So we store the max_index on the transaction level.
1412 */
1413 if (index > transaction->max_index)
1414 transaction->max_index = index;
1415
1416 return 0;
1417}
1418
1419int ref_transaction_create(struct ref_transaction *transaction,
1420 const char *refname,
1421 const struct object_id *new_oid,
1422 const char *new_target,
1423 unsigned int flags, const char *msg,
1424 struct strbuf *err)
1425{
1426 if (new_oid && new_target)
1427 BUG("create called with both new_oid and new_target set");
1428 if ((!new_oid || is_null_oid(new_oid)) && !new_target) {
1429 strbuf_addf(err, "'%s' has neither a valid OID nor a target", refname);
1430 return 1;
1431 }
1432 return ref_transaction_update(transaction, refname, new_oid,
1433 null_oid(the_hash_algo), new_target, NULL, flags,
1434 msg, err);
1435}
1436
1437int ref_transaction_delete(struct ref_transaction *transaction,
1438 const char *refname,
1439 const struct object_id *old_oid,
1440 const char *old_target,
1441 unsigned int flags,
1442 const char *msg,
1443 struct strbuf *err)
1444{
1445 if (old_oid && is_null_oid(old_oid))
1446 BUG("delete called with old_oid set to zeros");
1447 if (old_oid && old_target)
1448 BUG("delete called with both old_oid and old_target set");
1449 if (old_target && !(flags & REF_NO_DEREF))
1450 BUG("delete cannot operate on symrefs with deref mode");
1451 return ref_transaction_update(transaction, refname,
1452 null_oid(the_hash_algo), old_oid,
1453 NULL, old_target, flags,
1454 msg, err);
1455}
1456
1457int ref_transaction_verify(struct ref_transaction *transaction,
1458 const char *refname,
1459 const struct object_id *old_oid,
1460 const char *old_target,
1461 unsigned int flags,
1462 struct strbuf *err)
1463{
1464 if (!old_target && !old_oid)
1465 BUG("verify called with old_oid and old_target set to NULL");
1466 if (old_oid && old_target)
1467 BUG("verify called with both old_oid and old_target set");
1468 if (old_target && !(flags & REF_NO_DEREF))
1469 BUG("verify cannot operate on symrefs with deref mode");
1470 return ref_transaction_update(transaction, refname,
1471 NULL, old_oid,
1472 NULL, old_target,
1473 flags, NULL, err);
1474}
1475
1476int refs_update_ref(struct ref_store *refs, const char *msg,
1477 const char *refname, const struct object_id *new_oid,
1478 const struct object_id *old_oid, unsigned int flags,
1479 enum action_on_err onerr)
1480{
1481 struct ref_transaction *t = NULL;
1482 struct strbuf err = STRBUF_INIT;
1483 int ret = 0;
1484
1485 t = ref_store_transaction_begin(refs, 0, &err);
1486 if (!t ||
1487 ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL,
1488 flags, msg, &err) ||
1489 ref_transaction_commit(t, &err)) {
1490 ret = 1;
1491 ref_transaction_free(t);
1492 }
1493 if (ret) {
1494 const char *str = _("update_ref failed for ref '%s': %s");
1495
1496 switch (onerr) {
1497 case UPDATE_REFS_MSG_ON_ERR:
1498 error(str, refname, err.buf);
1499 break;
1500 case UPDATE_REFS_DIE_ON_ERR:
1501 die(str, refname, err.buf);
1502 break;
1503 case UPDATE_REFS_QUIET_ON_ERR:
1504 break;
1505 }
1506 strbuf_release(&err);
1507 return 1;
1508 }
1509 strbuf_release(&err);
1510 if (t)
1511 ref_transaction_free(t);
1512 return 0;
1513}
1514
1515/*
1516 * Check that the string refname matches a rule of the form
1517 * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule
1518 * "foo/%.*s/baz", and return the string "bar".
1519 */
1520static const char *match_parse_rule(const char *refname, const char *rule,
1521 size_t *len)
1522{
1523 /*
1524 * Check that rule matches refname up to the first percent in the rule.
1525 * We can bail immediately if not, but otherwise we leave "rule" at the
1526 * %-placeholder, and "refname" at the start of the potential matched
1527 * name.
1528 */
1529 while (*rule != '%') {
1530 if (!*rule)
1531 BUG("rev-parse rule did not have percent");
1532 if (*refname++ != *rule++)
1533 return NULL;
1534 }
1535
1536 /*
1537 * Check that our "%" is the expected placeholder. This assumes there
1538 * are no other percents (placeholder or quoted) in the string, but
1539 * that is sufficient for our rev-parse rules.
1540 */
1541 if (!skip_prefix(rule, "%.*s", &rule))
1542 return NULL;
1543
1544 /*
1545 * And now check that our suffix (if any) matches.
1546 */
1547 if (!strip_suffix(refname, rule, len))
1548 return NULL;
1549
1550 return refname; /* len set by strip_suffix() */
1551}
1552
1553char *refs_shorten_unambiguous_ref(struct ref_store *refs,
1554 const char *refname, int strict)
1555{
1556 int i;
1557 struct strbuf resolved_buf = STRBUF_INIT;
1558
1559 /* skip first rule, it will always match */
1560 for (i = NUM_REV_PARSE_RULES - 1; i > 0 ; --i) {
1561 int j;
1562 int rules_to_fail = i;
1563 const char *short_name;
1564 size_t short_name_len;
1565
1566 short_name = match_parse_rule(refname, ref_rev_parse_rules[i],
1567 &short_name_len);
1568 if (!short_name)
1569 continue;
1570
1571 /*
1572 * in strict mode, all (except the matched one) rules
1573 * must fail to resolve to a valid non-ambiguous ref
1574 */
1575 if (strict)
1576 rules_to_fail = NUM_REV_PARSE_RULES;
1577
1578 /*
1579 * check if the short name resolves to a valid ref,
1580 * but use only rules prior to the matched one
1581 */
1582 for (j = 0; j < rules_to_fail; j++) {
1583 const char *rule = ref_rev_parse_rules[j];
1584
1585 /* skip matched rule */
1586 if (i == j)
1587 continue;
1588
1589 /*
1590 * the short name is ambiguous, if it resolves
1591 * (with this previous rule) to a valid ref
1592 * read_ref() returns 0 on success
1593 */
1594 strbuf_reset(&resolved_buf);
1595 strbuf_addf(&resolved_buf, rule,
1596 cast_size_t_to_int(short_name_len),
1597 short_name);
1598 if (refs_ref_exists(refs, resolved_buf.buf))
1599 break;
1600 }
1601
1602 /*
1603 * short name is non-ambiguous if all previous rules
1604 * haven't resolved to a valid ref
1605 */
1606 if (j == rules_to_fail) {
1607 strbuf_release(&resolved_buf);
1608 return xmemdupz(short_name, short_name_len);
1609 }
1610 }
1611
1612 strbuf_release(&resolved_buf);
1613 return xstrdup(refname);
1614}
1615
1616int parse_hide_refs_config(const char *var, const char *value, const char *section,
1617 struct strvec *hide_refs)
1618{
1619 const char *key;
1620 if (!strcmp("transfer.hiderefs", var) ||
1621 (!parse_config_key(var, section, NULL, NULL, &key) &&
1622 !strcmp(key, "hiderefs"))) {
1623 char *ref;
1624 int len;
1625
1626 if (!value)
1627 return config_error_nonbool(var);
1628
1629 /* drop const to remove trailing '/' characters */
1630 ref = (char *)strvec_push(hide_refs, value);
1631 len = strlen(ref);
1632 while (len && ref[len - 1] == '/')
1633 ref[--len] = '\0';
1634 }
1635 return 0;
1636}
1637
1638int ref_is_hidden(const char *refname, const char *refname_full,
1639 const struct strvec *hide_refs)
1640{
1641 int i;
1642
1643 for (i = hide_refs->nr - 1; i >= 0; i--) {
1644 const char *match = hide_refs->v[i];
1645 const char *subject;
1646 int neg = 0;
1647 const char *p;
1648
1649 if (*match == '!') {
1650 neg = 1;
1651 match++;
1652 }
1653
1654 if (*match == '^') {
1655 subject = refname_full;
1656 match++;
1657 } else {
1658 subject = refname;
1659 }
1660
1661 /* refname can be NULL when namespaces are used. */
1662 if (subject &&
1663 skip_prefix(subject, match, &p) &&
1664 (!*p || *p == '/'))
1665 return !neg;
1666 }
1667 return 0;
1668}
1669
1670const char **hidden_refs_to_excludes(const struct strvec *hide_refs)
1671{
1672 const char **pattern;
1673 for (pattern = hide_refs->v; *pattern; pattern++) {
1674 /*
1675 * We can't feed any excludes from hidden refs config
1676 * sections, since later rules may override previous
1677 * ones. For example, with rules "refs/foo" and
1678 * "!refs/foo/bar", we should show "refs/foo/bar" (and
1679 * everything underneath it), but the earlier exclusion
1680 * would cause us to skip all of "refs/foo". We
1681 * likewise don't implement the namespace stripping
1682 * required for '^' rules.
1683 *
1684 * Both are possible to do, but complicated, so avoid
1685 * populating the jump list at all if we see either of
1686 * these patterns.
1687 */
1688 if (**pattern == '!' || **pattern == '^')
1689 return NULL;
1690 }
1691 return hide_refs->v;
1692}
1693
1694const char **get_namespaced_exclude_patterns(const char **exclude_patterns,
1695 const char *namespace,
1696 struct strvec *out)
1697{
1698 if (!namespace || !*namespace || !exclude_patterns || !*exclude_patterns)
1699 return exclude_patterns;
1700
1701 for (size_t i = 0; exclude_patterns[i]; i++)
1702 strvec_pushf(out, "%s%s", namespace, exclude_patterns[i]);
1703
1704 return out->v;
1705}
1706
1707const char *find_descendant_ref(const char *dirname,
1708 const struct string_list *extras,
1709 const struct string_list *skip)
1710{
1711 int pos;
1712
1713 if (!extras)
1714 return NULL;
1715
1716 /*
1717 * Look at the place where dirname would be inserted into
1718 * extras. If there is an entry at that position that starts
1719 * with dirname (remember, dirname includes the trailing
1720 * slash) and is not in skip, then we have a conflict.
1721 */
1722 for (pos = string_list_find_insert_index(extras, dirname, 0);
1723 pos < extras->nr; pos++) {
1724 const char *extra_refname = extras->items[pos].string;
1725
1726 if (!starts_with(extra_refname, dirname))
1727 break;
1728
1729 if (!skip || !string_list_has_string(skip, extra_refname))
1730 return extra_refname;
1731 }
1732 return NULL;
1733}
1734
1735int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1736{
1737 struct object_id oid;
1738 int flag;
1739
1740 if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING,
1741 &oid, &flag))
1742 return fn("HEAD", NULL, &oid, flag, cb_data);
1743
1744 return 0;
1745}
1746
1747struct ref_iterator *refs_ref_iterator_begin(
1748 struct ref_store *refs,
1749 const char *prefix,
1750 const char **exclude_patterns,
1751 int trim,
1752 enum do_for_each_ref_flags flags)
1753{
1754 struct ref_iterator *iter;
1755 struct strvec normalized_exclude_patterns = STRVEC_INIT;
1756
1757 if (exclude_patterns) {
1758 for (size_t i = 0; exclude_patterns[i]; i++) {
1759 const char *pattern = exclude_patterns[i];
1760 size_t len = strlen(pattern);
1761 if (!len)
1762 continue;
1763
1764 if (pattern[len - 1] == '/')
1765 strvec_push(&normalized_exclude_patterns, pattern);
1766 else
1767 strvec_pushf(&normalized_exclude_patterns, "%s/",
1768 pattern);
1769 }
1770
1771 exclude_patterns = normalized_exclude_patterns.v;
1772 }
1773
1774 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
1775 static int ref_paranoia = -1;
1776
1777 if (ref_paranoia < 0)
1778 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 1);
1779 if (ref_paranoia) {
1780 flags |= DO_FOR_EACH_INCLUDE_BROKEN;
1781 flags |= DO_FOR_EACH_OMIT_DANGLING_SYMREFS;
1782 }
1783 }
1784
1785 iter = refs->be->iterator_begin(refs, prefix, exclude_patterns, flags);
1786 /*
1787 * `iterator_begin()` already takes care of prefix, but we
1788 * might need to do some trimming:
1789 */
1790 if (trim)
1791 iter = prefix_ref_iterator_begin(iter, "", trim);
1792
1793 strvec_clear(&normalized_exclude_patterns);
1794
1795 return iter;
1796}
1797
1798static int do_for_each_ref(struct ref_store *refs, const char *prefix,
1799 const char **exclude_patterns,
1800 each_ref_fn fn, int trim,
1801 enum do_for_each_ref_flags flags, void *cb_data)
1802{
1803 struct ref_iterator *iter;
1804
1805 if (!refs)
1806 return 0;
1807
1808 iter = refs_ref_iterator_begin(refs, prefix, exclude_patterns, trim,
1809 flags);
1810
1811 return do_for_each_ref_iterator(iter, fn, cb_data);
1812}
1813
1814int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1815{
1816 return do_for_each_ref(refs, "", NULL, fn, 0, 0, cb_data);
1817}
1818
1819int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
1820 each_ref_fn fn, void *cb_data)
1821{
1822 return do_for_each_ref(refs, prefix, NULL, fn, strlen(prefix), 0, cb_data);
1823}
1824
1825int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
1826 const char **exclude_patterns,
1827 each_ref_fn fn, void *cb_data)
1828{
1829 return do_for_each_ref(refs, prefix, exclude_patterns, fn, 0, 0, cb_data);
1830}
1831
1832int refs_for_each_replace_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1833{
1834 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
1835 return do_for_each_ref(refs, git_replace_ref_base, NULL, fn,
1836 strlen(git_replace_ref_base),
1837 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1838}
1839
1840int refs_for_each_namespaced_ref(struct ref_store *refs,
1841 const char **exclude_patterns,
1842 each_ref_fn fn, void *cb_data)
1843{
1844 struct strvec namespaced_exclude_patterns = STRVEC_INIT;
1845 struct strbuf prefix = STRBUF_INIT;
1846 int ret;
1847
1848 exclude_patterns = get_namespaced_exclude_patterns(exclude_patterns,
1849 get_git_namespace(),
1850 &namespaced_exclude_patterns);
1851
1852 strbuf_addf(&prefix, "%srefs/", get_git_namespace());
1853 ret = do_for_each_ref(refs, prefix.buf, exclude_patterns, fn, 0, 0, cb_data);
1854
1855 strvec_clear(&namespaced_exclude_patterns);
1856 strbuf_release(&prefix);
1857 return ret;
1858}
1859
1860int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1861{
1862 return do_for_each_ref(refs, "", NULL, fn, 0,
1863 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1864}
1865
1866int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
1867 void *cb_data)
1868{
1869 return do_for_each_ref(refs, "", NULL, fn, 0,
1870 DO_FOR_EACH_INCLUDE_ROOT_REFS, cb_data);
1871}
1872
1873static int qsort_strcmp(const void *va, const void *vb)
1874{
1875 const char *a = *(const char **)va;
1876 const char *b = *(const char **)vb;
1877
1878 return strcmp(a, b);
1879}
1880
1881static void find_longest_prefixes_1(struct string_list *out,
1882 struct strbuf *prefix,
1883 const char **patterns, size_t nr)
1884{
1885 size_t i;
1886
1887 for (i = 0; i < nr; i++) {
1888 char c = patterns[i][prefix->len];
1889 if (!c || is_glob_special(c)) {
1890 string_list_append(out, prefix->buf);
1891 return;
1892 }
1893 }
1894
1895 i = 0;
1896 while (i < nr) {
1897 size_t end;
1898
1899 /*
1900 * Set "end" to the index of the element _after_ the last one
1901 * in our group.
1902 */
1903 for (end = i + 1; end < nr; end++) {
1904 if (patterns[i][prefix->len] != patterns[end][prefix->len])
1905 break;
1906 }
1907
1908 strbuf_addch(prefix, patterns[i][prefix->len]);
1909 find_longest_prefixes_1(out, prefix, patterns + i, end - i);
1910 strbuf_setlen(prefix, prefix->len - 1);
1911
1912 i = end;
1913 }
1914}
1915
1916static void find_longest_prefixes(struct string_list *out,
1917 const char **patterns)
1918{
1919 struct strvec sorted = STRVEC_INIT;
1920 struct strbuf prefix = STRBUF_INIT;
1921
1922 strvec_pushv(&sorted, patterns);
1923 QSORT(sorted.v, sorted.nr, qsort_strcmp);
1924
1925 find_longest_prefixes_1(out, &prefix, sorted.v, sorted.nr);
1926
1927 strvec_clear(&sorted);
1928 strbuf_release(&prefix);
1929}
1930
1931int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store,
1932 const char *namespace,
1933 const char **patterns,
1934 const char **exclude_patterns,
1935 each_ref_fn fn, void *cb_data)
1936{
1937 struct strvec namespaced_exclude_patterns = STRVEC_INIT;
1938 struct string_list prefixes = STRING_LIST_INIT_DUP;
1939 struct string_list_item *prefix;
1940 struct strbuf buf = STRBUF_INIT;
1941 int ret = 0, namespace_len;
1942
1943 find_longest_prefixes(&prefixes, patterns);
1944
1945 if (namespace)
1946 strbuf_addstr(&buf, namespace);
1947 namespace_len = buf.len;
1948
1949 exclude_patterns = get_namespaced_exclude_patterns(exclude_patterns,
1950 namespace,
1951 &namespaced_exclude_patterns);
1952
1953 for_each_string_list_item(prefix, &prefixes) {
1954 strbuf_addstr(&buf, prefix->string);
1955 ret = refs_for_each_fullref_in(ref_store, buf.buf,
1956 exclude_patterns, fn, cb_data);
1957 if (ret)
1958 break;
1959 strbuf_setlen(&buf, namespace_len);
1960 }
1961
1962 strvec_clear(&namespaced_exclude_patterns);
1963 string_list_clear(&prefixes, 0);
1964 strbuf_release(&buf);
1965 return ret;
1966}
1967
1968static int refs_read_special_head(struct ref_store *ref_store,
1969 const char *refname, struct object_id *oid,
1970 struct strbuf *referent, unsigned int *type,
1971 int *failure_errno)
1972{
1973 struct strbuf full_path = STRBUF_INIT;
1974 struct strbuf content = STRBUF_INIT;
1975 int result = -1;
1976 strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname);
1977
1978 if (strbuf_read_file(&content, full_path.buf, 0) < 0) {
1979 *failure_errno = errno;
1980 goto done;
1981 }
1982
1983 result = parse_loose_ref_contents(ref_store->repo->hash_algo, content.buf,
1984 oid, referent, type, NULL, failure_errno);
1985
1986done:
1987 strbuf_release(&full_path);
1988 strbuf_release(&content);
1989 return result;
1990}
1991
1992int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
1993 struct object_id *oid, struct strbuf *referent,
1994 unsigned int *type, int *failure_errno)
1995{
1996 assert(failure_errno);
1997 if (is_pseudo_ref(refname))
1998 return refs_read_special_head(ref_store, refname, oid, referent,
1999 type, failure_errno);
2000
2001 return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
2002 type, failure_errno);
2003}
2004
2005int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
2006 struct strbuf *referent)
2007{
2008 return ref_store->be->read_symbolic_ref(ref_store, refname, referent);
2009}
2010
2011const char *refs_resolve_ref_unsafe(struct ref_store *refs,
2012 const char *refname,
2013 int resolve_flags,
2014 struct object_id *oid,
2015 int *flags)
2016{
2017 static struct strbuf sb_refname = STRBUF_INIT;
2018 struct object_id unused_oid;
2019 int unused_flags;
2020 int symref_count;
2021
2022 if (!oid)
2023 oid = &unused_oid;
2024 if (!flags)
2025 flags = &unused_flags;
2026
2027 *flags = 0;
2028
2029 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
2030 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
2031 !refname_is_safe(refname))
2032 return NULL;
2033
2034 /*
2035 * repo_dwim_ref() uses REF_ISBROKEN to distinguish between
2036 * missing refs and refs that were present but invalid,
2037 * to complain about the latter to stderr.
2038 *
2039 * We don't know whether the ref exists, so don't set
2040 * REF_ISBROKEN yet.
2041 */
2042 *flags |= REF_BAD_NAME;
2043 }
2044
2045 for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
2046 unsigned int read_flags = 0;
2047 int failure_errno;
2048
2049 if (refs_read_raw_ref(refs, refname, oid, &sb_refname,
2050 &read_flags, &failure_errno)) {
2051 *flags |= read_flags;
2052
2053 /* In reading mode, refs must eventually resolve */
2054 if (resolve_flags & RESOLVE_REF_READING)
2055 return NULL;
2056
2057 /*
2058 * Otherwise a missing ref is OK. But the files backend
2059 * may show errors besides ENOENT if there are
2060 * similarly-named refs.
2061 */
2062 if (failure_errno != ENOENT &&
2063 failure_errno != EISDIR &&
2064 failure_errno != ENOTDIR)
2065 return NULL;
2066
2067 oidclr(oid, refs->repo->hash_algo);
2068 if (*flags & REF_BAD_NAME)
2069 *flags |= REF_ISBROKEN;
2070 return refname;
2071 }
2072
2073 *flags |= read_flags;
2074
2075 if (!(read_flags & REF_ISSYMREF)) {
2076 if (*flags & REF_BAD_NAME) {
2077 oidclr(oid, refs->repo->hash_algo);
2078 *flags |= REF_ISBROKEN;
2079 }
2080 return refname;
2081 }
2082
2083 refname = sb_refname.buf;
2084 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
2085 oidclr(oid, refs->repo->hash_algo);
2086 return refname;
2087 }
2088 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
2089 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
2090 !refname_is_safe(refname))
2091 return NULL;
2092
2093 *flags |= REF_ISBROKEN | REF_BAD_NAME;
2094 }
2095 }
2096
2097 return NULL;
2098}
2099
2100/* backend functions */
2101int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err)
2102{
2103 return refs->be->create_on_disk(refs, flags, err);
2104}
2105
2106int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err)
2107{
2108 return refs->be->remove_on_disk(refs, err);
2109}
2110
2111int repo_resolve_gitlink_ref(struct repository *r,
2112 const char *submodule, const char *refname,
2113 struct object_id *oid)
2114{
2115 struct ref_store *refs;
2116 int flags;
2117
2118 refs = repo_get_submodule_ref_store(r, submodule);
2119 if (!refs)
2120 return -1;
2121
2122 if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) ||
2123 is_null_oid(oid))
2124 return -1;
2125 return 0;
2126}
2127
2128/*
2129 * Look up a ref store by name. If that ref_store hasn't been
2130 * registered yet, return NULL.
2131 */
2132static struct ref_store *lookup_ref_store_map(struct strmap *map,
2133 const char *name)
2134{
2135 struct strmap_entry *entry;
2136
2137 if (!map->map.tablesize)
2138 /* It's initialized on demand in register_ref_store(). */
2139 return NULL;
2140
2141 entry = strmap_get_entry(map, name);
2142 return entry ? entry->value : NULL;
2143}
2144
2145/*
2146 * Create, record, and return a ref_store instance for the specified
2147 * gitdir using the given ref storage format.
2148 */
2149static struct ref_store *ref_store_init(struct repository *repo,
2150 enum ref_storage_format format,
2151 const char *gitdir,
2152 unsigned int flags)
2153{
2154 const struct ref_storage_be *be;
2155 struct ref_store *refs;
2156
2157 be = find_ref_storage_backend(format);
2158 if (!be)
2159 BUG("reference backend is unknown");
2160
2161 refs = be->init(repo, gitdir, flags);
2162 return refs;
2163}
2164
2165void ref_store_release(struct ref_store *ref_store)
2166{
2167 ref_store->be->release(ref_store);
2168 free(ref_store->gitdir);
2169}
2170
2171struct ref_store *get_main_ref_store(struct repository *r)
2172{
2173 if (r->refs_private)
2174 return r->refs_private;
2175
2176 if (!r->gitdir)
2177 BUG("attempting to get main_ref_store outside of repository");
2178
2179 r->refs_private = ref_store_init(r, r->ref_storage_format,
2180 r->gitdir, REF_STORE_ALL_CAPS);
2181 r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private);
2182 return r->refs_private;
2183}
2184
2185/*
2186 * Associate a ref store with a name. It is a fatal error to call this
2187 * function twice for the same name.
2188 */
2189static void register_ref_store_map(struct strmap *map,
2190 const char *type,
2191 struct ref_store *refs,
2192 const char *name)
2193{
2194 if (!map->map.tablesize)
2195 strmap_init(map);
2196 if (strmap_put(map, name, refs))
2197 BUG("%s ref_store '%s' initialized twice", type, name);
2198}
2199
2200struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
2201 const char *submodule)
2202{
2203 struct strbuf submodule_sb = STRBUF_INIT;
2204 struct ref_store *refs;
2205 char *to_free = NULL;
2206 size_t len;
2207 struct repository *subrepo;
2208
2209 if (!submodule)
2210 return NULL;
2211
2212 len = strlen(submodule);
2213 while (len && is_dir_sep(submodule[len - 1]))
2214 len--;
2215 if (!len)
2216 return NULL;
2217
2218 if (submodule[len])
2219 /* We need to strip off one or more trailing slashes */
2220 submodule = to_free = xmemdupz(submodule, len);
2221
2222 refs = lookup_ref_store_map(&repo->submodule_ref_stores, submodule);
2223 if (refs)
2224 goto done;
2225
2226 strbuf_addstr(&submodule_sb, submodule);
2227 if (!is_nonbare_repository_dir(&submodule_sb))
2228 goto done;
2229
2230 if (submodule_to_gitdir(repo, &submodule_sb, submodule))
2231 goto done;
2232
2233 subrepo = xmalloc(sizeof(*subrepo));
2234
2235 if (repo_submodule_init(subrepo, repo, submodule,
2236 null_oid(the_hash_algo))) {
2237 free(subrepo);
2238 goto done;
2239 }
2240 refs = ref_store_init(subrepo, subrepo->ref_storage_format,
2241 submodule_sb.buf,
2242 REF_STORE_READ | REF_STORE_ODB);
2243 register_ref_store_map(&repo->submodule_ref_stores, "submodule",
2244 refs, submodule);
2245
2246done:
2247 strbuf_release(&submodule_sb);
2248 free(to_free);
2249
2250 return refs;
2251}
2252
2253struct ref_store *get_worktree_ref_store(const struct worktree *wt)
2254{
2255 struct ref_store *refs;
2256 const char *id;
2257
2258 if (wt->is_current)
2259 return get_main_ref_store(wt->repo);
2260
2261 id = wt->id ? wt->id : "/";
2262 refs = lookup_ref_store_map(&wt->repo->worktree_ref_stores, id);
2263 if (refs)
2264 return refs;
2265
2266 if (wt->id) {
2267 struct strbuf common_path = STRBUF_INIT;
2268 repo_common_path_append(wt->repo, &common_path,
2269 "worktrees/%s", wt->id);
2270 refs = ref_store_init(wt->repo, wt->repo->ref_storage_format,
2271 common_path.buf, REF_STORE_ALL_CAPS);
2272 strbuf_release(&common_path);
2273 } else {
2274 refs = ref_store_init(wt->repo, wt->repo->ref_storage_format,
2275 wt->repo->commondir, REF_STORE_ALL_CAPS);
2276 }
2277
2278 if (refs)
2279 register_ref_store_map(&wt->repo->worktree_ref_stores,
2280 "worktree", refs, id);
2281
2282 return refs;
2283}
2284
2285void base_ref_store_init(struct ref_store *refs, struct repository *repo,
2286 const char *path, const struct ref_storage_be *be)
2287{
2288 refs->be = be;
2289 refs->repo = repo;
2290 refs->gitdir = xstrdup(path);
2291}
2292
2293/* backend functions */
2294int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)
2295{
2296 return refs->be->pack_refs(refs, opts);
2297}
2298
2299int peel_iterated_oid(struct repository *r, const struct object_id *base, struct object_id *peeled)
2300{
2301 if (current_ref_iter &&
2302 (current_ref_iter->oid == base ||
2303 oideq(current_ref_iter->oid, base)))
2304 return ref_iterator_peel(current_ref_iter, peeled);
2305
2306 return peel_object(r, base, peeled) ? -1 : 0;
2307}
2308
2309int refs_update_symref(struct ref_store *refs, const char *ref,
2310 const char *target, const char *logmsg)
2311{
2312 return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
2313}
2314
2315int refs_update_symref_extended(struct ref_store *refs, const char *ref,
2316 const char *target, const char *logmsg,
2317 struct strbuf *referent, int create_only)
2318{
2319 struct ref_transaction *transaction;
2320 struct strbuf err = STRBUF_INIT;
2321 int ret = 0, prepret = 0;
2322
2323 transaction = ref_store_transaction_begin(refs, 0, &err);
2324 if (!transaction) {
2325 error_return:
2326 ret = error("%s", err.buf);
2327 goto cleanup;
2328 }
2329 if (create_only) {
2330 if (ref_transaction_create(transaction, ref, NULL, target,
2331 REF_NO_DEREF, logmsg, &err))
2332 goto error_return;
2333 prepret = ref_transaction_prepare(transaction, &err);
2334 if (prepret && prepret != REF_TRANSACTION_ERROR_CREATE_EXISTS)
2335 goto error_return;
2336 } else {
2337 if (ref_transaction_update(transaction, ref, NULL, NULL,
2338 target, NULL, REF_NO_DEREF,
2339 logmsg, &err) ||
2340 ref_transaction_prepare(transaction, &err))
2341 goto error_return;
2342 }
2343
2344 if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) {
2345 struct object_id oid;
2346 if (!refs_read_ref(refs, ref, &oid)) {
2347 strbuf_addstr(referent, oid_to_hex(&oid));
2348 ret = NOT_A_SYMREF;
2349 }
2350 }
2351
2352 if (prepret == REF_TRANSACTION_ERROR_CREATE_EXISTS)
2353 goto cleanup;
2354
2355 if (ref_transaction_commit(transaction, &err))
2356 goto error_return;
2357
2358cleanup:
2359 strbuf_release(&err);
2360 if (transaction)
2361 ref_transaction_free(transaction);
2362
2363 return ret;
2364}
2365
2366/*
2367 * Write an error to `err` and return a nonzero value iff the same
2368 * refname appears multiple times in `refnames`. `refnames` must be
2369 * sorted on entry to this function.
2370 */
2371static int ref_update_reject_duplicates(struct string_list *refnames,
2372 struct strbuf *err)
2373{
2374 size_t i, n = refnames->nr;
2375
2376 assert(err);
2377
2378 for (i = 1; i < n; i++) {
2379 int cmp = strcmp(refnames->items[i - 1].string,
2380 refnames->items[i].string);
2381
2382 if (!cmp) {
2383 strbuf_addf(err,
2384 _("multiple updates for ref '%s' not allowed"),
2385 refnames->items[i].string);
2386 return 1;
2387 } else if (cmp > 0) {
2388 BUG("ref_update_reject_duplicates() received unsorted list");
2389 }
2390 }
2391 return 0;
2392}
2393
2394static int run_transaction_hook(struct ref_transaction *transaction,
2395 const char *state)
2396{
2397 struct child_process proc = CHILD_PROCESS_INIT;
2398 struct strbuf buf = STRBUF_INIT;
2399 const char *hook;
2400 int ret = 0, i;
2401
2402 hook = find_hook(transaction->ref_store->repo, "reference-transaction");
2403 if (!hook)
2404 return ret;
2405
2406 strvec_pushl(&proc.args, hook, state, NULL);
2407 proc.in = -1;
2408 proc.stdout_to_stderr = 1;
2409 proc.trace2_hook_name = "reference-transaction";
2410
2411 ret = start_command(&proc);
2412 if (ret)
2413 return ret;
2414
2415 sigchain_push(SIGPIPE, SIG_IGN);
2416
2417 for (i = 0; i < transaction->nr; i++) {
2418 struct ref_update *update = transaction->updates[i];
2419
2420 if (update->flags & REF_LOG_ONLY)
2421 continue;
2422
2423 strbuf_reset(&buf);
2424
2425 if (!(update->flags & REF_HAVE_OLD))
2426 strbuf_addf(&buf, "%s ", oid_to_hex(null_oid(the_hash_algo)));
2427 else if (update->old_target)
2428 strbuf_addf(&buf, "ref:%s ", update->old_target);
2429 else
2430 strbuf_addf(&buf, "%s ", oid_to_hex(&update->old_oid));
2431
2432 if (!(update->flags & REF_HAVE_NEW))
2433 strbuf_addf(&buf, "%s ", oid_to_hex(null_oid(the_hash_algo)));
2434 else if (update->new_target)
2435 strbuf_addf(&buf, "ref:%s ", update->new_target);
2436 else
2437 strbuf_addf(&buf, "%s ", oid_to_hex(&update->new_oid));
2438
2439 strbuf_addf(&buf, "%s\n", update->refname);
2440
2441 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
2442 if (errno != EPIPE) {
2443 /* Don't leak errno outside this API */
2444 errno = 0;
2445 ret = -1;
2446 }
2447 break;
2448 }
2449 }
2450
2451 close(proc.in);
2452 sigchain_pop(SIGPIPE);
2453 strbuf_release(&buf);
2454
2455 ret |= finish_command(&proc);
2456 return ret;
2457}
2458
2459int ref_transaction_prepare(struct ref_transaction *transaction,
2460 struct strbuf *err)
2461{
2462 struct ref_store *refs = transaction->ref_store;
2463 int ret;
2464
2465 switch (transaction->state) {
2466 case REF_TRANSACTION_OPEN:
2467 /* Good. */
2468 break;
2469 case REF_TRANSACTION_PREPARED:
2470 BUG("prepare called twice on reference transaction");
2471 break;
2472 case REF_TRANSACTION_CLOSED:
2473 BUG("prepare called on a closed reference transaction");
2474 break;
2475 default:
2476 BUG("unexpected reference transaction state");
2477 break;
2478 }
2479
2480 if (refs->repo->objects->odb->disable_ref_updates) {
2481 strbuf_addstr(err,
2482 _("ref updates forbidden inside quarantine environment"));
2483 return -1;
2484 }
2485
2486 string_list_sort(&transaction->refnames);
2487 if (ref_update_reject_duplicates(&transaction->refnames, err))
2488 return REF_TRANSACTION_ERROR_GENERIC;
2489
2490 ret = refs->be->transaction_prepare(refs, transaction, err);
2491 if (ret)
2492 return ret;
2493
2494 ret = run_transaction_hook(transaction, "prepared");
2495 if (ret) {
2496 ref_transaction_abort(transaction, err);
2497 die(_("ref updates aborted by hook"));
2498 }
2499
2500 return 0;
2501}
2502
2503int ref_transaction_abort(struct ref_transaction *transaction,
2504 struct strbuf *err)
2505{
2506 struct ref_store *refs = transaction->ref_store;
2507 int ret = 0;
2508
2509 switch (transaction->state) {
2510 case REF_TRANSACTION_OPEN:
2511 /* No need to abort explicitly. */
2512 break;
2513 case REF_TRANSACTION_PREPARED:
2514 ret = refs->be->transaction_abort(refs, transaction, err);
2515 break;
2516 case REF_TRANSACTION_CLOSED:
2517 BUG("abort called on a closed reference transaction");
2518 break;
2519 default:
2520 BUG("unexpected reference transaction state");
2521 break;
2522 }
2523
2524 run_transaction_hook(transaction, "aborted");
2525
2526 ref_transaction_free(transaction);
2527 return ret;
2528}
2529
2530int ref_transaction_commit(struct ref_transaction *transaction,
2531 struct strbuf *err)
2532{
2533 struct ref_store *refs = transaction->ref_store;
2534 int ret;
2535
2536 switch (transaction->state) {
2537 case REF_TRANSACTION_OPEN:
2538 /* Need to prepare first. */
2539 ret = ref_transaction_prepare(transaction, err);
2540 if (ret)
2541 return ret;
2542 break;
2543 case REF_TRANSACTION_PREPARED:
2544 /* Fall through to finish. */
2545 break;
2546 case REF_TRANSACTION_CLOSED:
2547 BUG("commit called on a closed reference transaction");
2548 break;
2549 default:
2550 BUG("unexpected reference transaction state");
2551 break;
2552 }
2553
2554 ret = refs->be->transaction_finish(refs, transaction, err);
2555 if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))
2556 run_transaction_hook(transaction, "committed");
2557 return ret;
2558}
2559
2560enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,
2561 const struct string_list *refnames,
2562 const struct string_list *extras,
2563 const struct string_list *skip,
2564 struct ref_transaction *transaction,
2565 unsigned int initial_transaction,
2566 struct strbuf *err)
2567{
2568 struct strbuf dirname = STRBUF_INIT;
2569 struct strbuf referent = STRBUF_INIT;
2570 struct string_list_item *item;
2571 struct ref_iterator *iter = NULL;
2572 struct strset conflicting_dirnames;
2573 struct strset dirnames;
2574 int ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
2575
2576 /*
2577 * For the sake of comments in this function, suppose that
2578 * refname is "refs/foo/bar".
2579 */
2580
2581 assert(err);
2582
2583 strset_init(&conflicting_dirnames);
2584 strset_init(&dirnames);
2585
2586 for_each_string_list_item(item, refnames) {
2587 const size_t *update_idx = (size_t *)item->util;
2588 const char *refname = item->string;
2589 const char *extra_refname;
2590 struct object_id oid;
2591 unsigned int type;
2592 const char *slash;
2593
2594 strbuf_reset(&dirname);
2595
2596 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
2597 /*
2598 * Just saying "Is a directory" when we e.g. can't
2599 * lock some multi-level ref isn't very informative,
2600 * the user won't be told *what* is a directory, so
2601 * let's not use strerror() below.
2602 */
2603 int ignore_errno;
2604
2605 /* Expand dirname to the new prefix, not including the trailing slash: */
2606 strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
2607
2608 /*
2609 * We are still at a leading dir of the refname (e.g.,
2610 * "refs/foo"; if there is a reference with that name,
2611 * it is a conflict, *unless* it is in skip.
2612 */
2613 if (skip && string_list_has_string(skip, dirname.buf))
2614 continue;
2615
2616 /*
2617 * If we've already seen the directory we don't need to
2618 * process it again. Skip it to avoid checking common
2619 * prefixes like "refs/heads/" repeatedly.
2620 */
2621 if (!strset_add(&dirnames, dirname.buf))
2622 continue;
2623
2624 if (!initial_transaction &&
2625 (strset_contains(&conflicting_dirnames, dirname.buf) ||
2626 !refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
2627 &type, &ignore_errno))) {
2628 if (transaction && ref_transaction_maybe_set_rejected(
2629 transaction, *update_idx,
2630 REF_TRANSACTION_ERROR_NAME_CONFLICT)) {
2631 strset_remove(&dirnames, dirname.buf);
2632 strset_add(&conflicting_dirnames, dirname.buf);
2633 continue;
2634 }
2635
2636 strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
2637 dirname.buf, refname);
2638 goto cleanup;
2639 }
2640
2641 if (extras && string_list_has_string(extras, dirname.buf)) {
2642 if (transaction && ref_transaction_maybe_set_rejected(
2643 transaction, *update_idx,
2644 REF_TRANSACTION_ERROR_NAME_CONFLICT)) {
2645 strset_remove(&dirnames, dirname.buf);
2646 continue;
2647 }
2648
2649 strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
2650 refname, dirname.buf);
2651 goto cleanup;
2652 }
2653 }
2654
2655 /*
2656 * We are at the leaf of our refname (e.g., "refs/foo/bar").
2657 * There is no point in searching for a reference with that
2658 * name, because a refname isn't considered to conflict with
2659 * itself. But we still need to check for references whose
2660 * names are in the "refs/foo/bar/" namespace, because they
2661 * *do* conflict.
2662 */
2663 strbuf_addstr(&dirname, refname + dirname.len);
2664 strbuf_addch(&dirname, '/');
2665
2666 if (!initial_transaction) {
2667 int ok;
2668
2669 if (!iter) {
2670 iter = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,
2671 DO_FOR_EACH_INCLUDE_BROKEN);
2672 } else if (ref_iterator_seek(iter, dirname.buf) < 0) {
2673 goto cleanup;
2674 }
2675
2676 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
2677 if (skip &&
2678 string_list_has_string(skip, iter->refname))
2679 continue;
2680
2681 if (transaction && ref_transaction_maybe_set_rejected(
2682 transaction, *update_idx,
2683 REF_TRANSACTION_ERROR_NAME_CONFLICT))
2684 continue;
2685
2686 strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
2687 iter->refname, refname);
2688 goto cleanup;
2689 }
2690
2691 if (ok != ITER_DONE)
2692 BUG("error while iterating over references");
2693 }
2694
2695 extra_refname = find_descendant_ref(dirname.buf, extras, skip);
2696 if (extra_refname) {
2697 if (transaction && ref_transaction_maybe_set_rejected(
2698 transaction, *update_idx,
2699 REF_TRANSACTION_ERROR_NAME_CONFLICT))
2700 continue;
2701
2702 strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
2703 refname, extra_refname);
2704 goto cleanup;
2705 }
2706 }
2707
2708 ret = 0;
2709
2710cleanup:
2711 strbuf_release(&referent);
2712 strbuf_release(&dirname);
2713 strset_clear(&conflicting_dirnames);
2714 strset_clear(&dirnames);
2715 ref_iterator_free(iter);
2716 return ret;
2717}
2718
2719enum ref_transaction_error refs_verify_refname_available(
2720 struct ref_store *refs,
2721 const char *refname,
2722 const struct string_list *extras,
2723 const struct string_list *skip,
2724 unsigned int initial_transaction,
2725 struct strbuf *err)
2726{
2727 struct string_list_item item = { .string = (char *) refname };
2728 struct string_list refnames = {
2729 .items = &item,
2730 .nr = 1,
2731 };
2732
2733 return refs_verify_refnames_available(refs, &refnames, extras, skip,
2734 NULL, initial_transaction, err);
2735}
2736
2737struct do_for_each_reflog_help {
2738 each_reflog_fn *fn;
2739 void *cb_data;
2740};
2741
2742static int do_for_each_reflog_helper(const char *refname,
2743 const char *referent UNUSED,
2744 const struct object_id *oid UNUSED,
2745 int flags UNUSED,
2746 void *cb_data)
2747{
2748 struct do_for_each_reflog_help *hp = cb_data;
2749 return hp->fn(refname, hp->cb_data);
2750}
2751
2752int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data)
2753{
2754 struct ref_iterator *iter;
2755 struct do_for_each_reflog_help hp = { fn, cb_data };
2756
2757 iter = refs->be->reflog_iterator_begin(refs);
2758
2759 return do_for_each_ref_iterator(iter, do_for_each_reflog_helper, &hp);
2760}
2761
2762int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
2763 const char *refname,
2764 each_reflog_ent_fn fn,
2765 void *cb_data)
2766{
2767 return refs->be->for_each_reflog_ent_reverse(refs, refname,
2768 fn, cb_data);
2769}
2770
2771int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
2772 each_reflog_ent_fn fn, void *cb_data)
2773{
2774 return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
2775}
2776
2777int refs_reflog_exists(struct ref_store *refs, const char *refname)
2778{
2779 return refs->be->reflog_exists(refs, refname);
2780}
2781
2782int refs_create_reflog(struct ref_store *refs, const char *refname,
2783 struct strbuf *err)
2784{
2785 return refs->be->create_reflog(refs, refname, err);
2786}
2787
2788int refs_delete_reflog(struct ref_store *refs, const char *refname)
2789{
2790 return refs->be->delete_reflog(refs, refname);
2791}
2792
2793int refs_reflog_expire(struct ref_store *refs,
2794 const char *refname,
2795 unsigned int flags,
2796 reflog_expiry_prepare_fn prepare_fn,
2797 reflog_expiry_should_prune_fn should_prune_fn,
2798 reflog_expiry_cleanup_fn cleanup_fn,
2799 void *policy_cb_data)
2800{
2801 return refs->be->reflog_expire(refs, refname, flags,
2802 prepare_fn, should_prune_fn,
2803 cleanup_fn, policy_cb_data);
2804}
2805
2806void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
2807 ref_transaction_for_each_queued_update_fn cb,
2808 void *cb_data)
2809{
2810 int i;
2811
2812 for (i = 0; i < transaction->nr; i++) {
2813 struct ref_update *update = transaction->updates[i];
2814
2815 cb(update->refname,
2816 (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL,
2817 (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL,
2818 cb_data);
2819 }
2820}
2821
2822void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction,
2823 ref_transaction_for_each_rejected_update_fn cb,
2824 void *cb_data)
2825{
2826 if (!transaction->rejections)
2827 return;
2828
2829 for (size_t i = 0; i < transaction->rejections->nr; i++) {
2830 size_t update_index = transaction->rejections->update_indices[i];
2831 struct ref_update *update = transaction->updates[update_index];
2832
2833 if (!update->rejection_err)
2834 continue;
2835
2836 cb(update->refname,
2837 (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL,
2838 (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL,
2839 update->old_target, update->new_target,
2840 update->rejection_err, cb_data);
2841 }
2842}
2843
2844int refs_delete_refs(struct ref_store *refs, const char *logmsg,
2845 struct string_list *refnames, unsigned int flags)
2846{
2847 struct ref_transaction *transaction;
2848 struct strbuf err = STRBUF_INIT;
2849 struct string_list_item *item;
2850 int ret = 0, failures = 0;
2851 char *msg;
2852
2853 if (!refnames->nr)
2854 return 0;
2855
2856 msg = normalize_reflog_message(logmsg);
2857
2858 /*
2859 * Since we don't check the references' old_oids, the
2860 * individual updates can't fail, so we can pack all of the
2861 * updates into a single transaction.
2862 */
2863 transaction = ref_store_transaction_begin(refs, 0, &err);
2864 if (!transaction) {
2865 ret = error("%s", err.buf);
2866 goto out;
2867 }
2868
2869 for_each_string_list_item(item, refnames) {
2870 ret = ref_transaction_delete(transaction, item->string,
2871 NULL, NULL, flags, msg, &err);
2872 if (ret) {
2873 warning(_("could not delete reference %s: %s"),
2874 item->string, err.buf);
2875 strbuf_reset(&err);
2876 failures = 1;
2877 }
2878 }
2879
2880 ret = ref_transaction_commit(transaction, &err);
2881 if (ret) {
2882 if (refnames->nr == 1)
2883 error(_("could not delete reference %s: %s"),
2884 refnames->items[0].string, err.buf);
2885 else
2886 error(_("could not delete references: %s"), err.buf);
2887 }
2888
2889out:
2890 if (!ret && failures)
2891 ret = -1;
2892 ref_transaction_free(transaction);
2893 strbuf_release(&err);
2894 free(msg);
2895 return ret;
2896}
2897
2898int refs_rename_ref(struct ref_store *refs, const char *oldref,
2899 const char *newref, const char *logmsg)
2900{
2901 char *msg;
2902 int retval;
2903
2904 msg = normalize_reflog_message(logmsg);
2905 retval = refs->be->rename_ref(refs, oldref, newref, msg);
2906 free(msg);
2907 return retval;
2908}
2909
2910int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
2911 const char *newref, const char *logmsg)
2912{
2913 char *msg;
2914 int retval;
2915
2916 msg = normalize_reflog_message(logmsg);
2917 retval = refs->be->copy_ref(refs, oldref, newref, msg);
2918 free(msg);
2919 return retval;
2920}
2921
2922const char *ref_update_original_update_refname(struct ref_update *update)
2923{
2924 while (update->parent_update)
2925 update = update->parent_update;
2926
2927 return update->refname;
2928}
2929
2930int ref_update_has_null_new_value(struct ref_update *update)
2931{
2932 return !update->new_target && is_null_oid(&update->new_oid);
2933}
2934
2935enum ref_transaction_error ref_update_check_old_target(const char *referent,
2936 struct ref_update *update,
2937 struct strbuf *err)
2938{
2939 if (!update->old_target)
2940 BUG("called without old_target set");
2941
2942 if (!strcmp(referent, update->old_target))
2943 return 0;
2944
2945 if (!strcmp(referent, "")) {
2946 strbuf_addf(err, "verifying symref target: '%s': "
2947 "reference is missing but expected %s",
2948 ref_update_original_update_refname(update),
2949 update->old_target);
2950 return REF_TRANSACTION_ERROR_NONEXISTENT_REF;
2951 }
2952
2953 strbuf_addf(err, "verifying symref target: '%s': is at %s but expected %s",
2954 ref_update_original_update_refname(update),
2955 referent, update->old_target);
2956 return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;
2957}
2958
2959struct migration_data {
2960 struct ref_store *old_refs;
2961 struct ref_transaction *transaction;
2962 struct strbuf *errbuf;
2963 struct strbuf sb;
2964};
2965
2966static int migrate_one_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
2967 int flags, void *cb_data)
2968{
2969 struct migration_data *data = cb_data;
2970 struct strbuf symref_target = STRBUF_INIT;
2971 int ret;
2972
2973 if (flags & REF_ISSYMREF) {
2974 ret = refs_read_symbolic_ref(data->old_refs, refname, &symref_target);
2975 if (ret < 0)
2976 goto done;
2977
2978 ret = ref_transaction_update(data->transaction, refname, NULL, null_oid(the_hash_algo),
2979 symref_target.buf, NULL,
2980 REF_SKIP_CREATE_REFLOG | REF_NO_DEREF, NULL, data->errbuf);
2981 if (ret < 0)
2982 goto done;
2983 } else {
2984 ret = ref_transaction_create(data->transaction, refname, oid, NULL,
2985 REF_SKIP_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION,
2986 NULL, data->errbuf);
2987 if (ret < 0)
2988 goto done;
2989 }
2990
2991done:
2992 strbuf_release(&symref_target);
2993 return ret;
2994}
2995
2996struct reflog_migration_data {
2997 uint64_t index;
2998 const char *refname;
2999 struct ref_store *old_refs;
3000 struct ref_transaction *transaction;
3001 struct strbuf *errbuf;
3002 struct strbuf *sb;
3003};
3004
3005static int migrate_one_reflog_entry(struct object_id *old_oid,
3006 struct object_id *new_oid,
3007 const char *committer,
3008 timestamp_t timestamp, int tz,
3009 const char *msg, void *cb_data)
3010{
3011 struct reflog_migration_data *data = cb_data;
3012 const char *date;
3013 int ret;
3014
3015 date = show_date(timestamp, tz, DATE_MODE(NORMAL));
3016 strbuf_reset(data->sb);
3017 /* committer contains name and email */
3018 strbuf_addstr(data->sb, fmt_ident("", committer, WANT_BLANK_IDENT, date, 0));
3019
3020 ret = ref_transaction_update_reflog(data->transaction, data->refname,
3021 new_oid, old_oid, data->sb->buf,
3022 REF_HAVE_NEW | REF_HAVE_OLD, msg,
3023 data->index++, data->errbuf);
3024 return ret;
3025}
3026
3027static int migrate_one_reflog(const char *refname, void *cb_data)
3028{
3029 struct migration_data *migration_data = cb_data;
3030 struct reflog_migration_data data = {
3031 .refname = refname,
3032 .old_refs = migration_data->old_refs,
3033 .transaction = migration_data->transaction,
3034 .errbuf = migration_data->errbuf,
3035 .sb = &migration_data->sb,
3036 };
3037
3038 return refs_for_each_reflog_ent(migration_data->old_refs, refname,
3039 migrate_one_reflog_entry, &data);
3040}
3041
3042static int move_files(const char *from_path, const char *to_path, struct strbuf *errbuf)
3043{
3044 struct strbuf from_buf = STRBUF_INIT, to_buf = STRBUF_INIT;
3045 size_t from_len, to_len;
3046 DIR *from_dir;
3047 int ret;
3048
3049 from_dir = opendir(from_path);
3050 if (!from_dir) {
3051 strbuf_addf(errbuf, "could not open source directory '%s': %s",
3052 from_path, strerror(errno));
3053 ret = -1;
3054 goto done;
3055 }
3056
3057 strbuf_addstr(&from_buf, from_path);
3058 strbuf_complete(&from_buf, '/');
3059 from_len = from_buf.len;
3060
3061 strbuf_addstr(&to_buf, to_path);
3062 strbuf_complete(&to_buf, '/');
3063 to_len = to_buf.len;
3064
3065 while (1) {
3066 struct dirent *ent;
3067
3068 errno = 0;
3069 ent = readdir(from_dir);
3070 if (!ent)
3071 break;
3072
3073 if (!strcmp(ent->d_name, ".") ||
3074 !strcmp(ent->d_name, ".."))
3075 continue;
3076
3077 strbuf_setlen(&from_buf, from_len);
3078 strbuf_addstr(&from_buf, ent->d_name);
3079
3080 strbuf_setlen(&to_buf, to_len);
3081 strbuf_addstr(&to_buf, ent->d_name);
3082
3083 ret = rename(from_buf.buf, to_buf.buf);
3084 if (ret < 0) {
3085 strbuf_addf(errbuf, "could not link file '%s' to '%s': %s",
3086 from_buf.buf, to_buf.buf, strerror(errno));
3087 goto done;
3088 }
3089 }
3090
3091 if (errno) {
3092 strbuf_addf(errbuf, "could not read entry from directory '%s': %s",
3093 from_path, strerror(errno));
3094 ret = -1;
3095 goto done;
3096 }
3097
3098 ret = 0;
3099
3100done:
3101 strbuf_release(&from_buf);
3102 strbuf_release(&to_buf);
3103 if (from_dir)
3104 closedir(from_dir);
3105 return ret;
3106}
3107
3108static int has_worktrees(void)
3109{
3110 struct worktree **worktrees = get_worktrees();
3111 int ret = 0;
3112 size_t i;
3113
3114 for (i = 0; worktrees[i]; i++) {
3115 if (is_main_worktree(worktrees[i]))
3116 continue;
3117 ret = 1;
3118 }
3119
3120 free_worktrees(worktrees);
3121 return ret;
3122}
3123
3124int repo_migrate_ref_storage_format(struct repository *repo,
3125 enum ref_storage_format format,
3126 unsigned int flags,
3127 struct strbuf *errbuf)
3128{
3129 struct ref_store *old_refs = NULL, *new_refs = NULL;
3130 struct ref_transaction *transaction = NULL;
3131 struct strbuf new_gitdir = STRBUF_INIT;
3132 struct migration_data data = {
3133 .sb = STRBUF_INIT,
3134 };
3135 int did_migrate_refs = 0;
3136 int ret;
3137
3138 if (repo->ref_storage_format == format) {
3139 strbuf_addstr(errbuf, "current and new ref storage format are equal");
3140 ret = -1;
3141 goto done;
3142 }
3143
3144 old_refs = get_main_ref_store(repo);
3145
3146 /*
3147 * Worktrees complicate the migration because every worktree has a
3148 * separate ref storage. While it should be feasible to implement, this
3149 * is pushed out to a future iteration.
3150 *
3151 * TODO: we should really be passing the caller-provided repository to
3152 * `has_worktrees()`, but our worktree subsystem doesn't yet support
3153 * that.
3154 */
3155 if (has_worktrees()) {
3156 strbuf_addstr(errbuf, "migrating repositories with worktrees is not supported yet");
3157 ret = -1;
3158 goto done;
3159 }
3160
3161 /*
3162 * The overall logic looks like this:
3163 *
3164 * 1. Set up a new temporary directory and initialize it with the new
3165 * format. This is where all refs will be migrated into.
3166 *
3167 * 2. Enumerate all refs and write them into the new ref storage.
3168 * This operation is safe as we do not yet modify the main
3169 * repository.
3170 *
3171 * 3. Enumerate all reflogs and write them into the new ref storage.
3172 * This operation is safe as we do not yet modify the main
3173 * repository.
3174 *
3175 * 4. If we're in dry-run mode then we are done and can hand over the
3176 * directory to the caller for inspection. If not, we now start
3177 * with the destructive part.
3178 *
3179 * 5. Delete the old ref storage from disk. As we have a copy of refs
3180 * in the new ref storage it's okay(ish) if we now get interrupted
3181 * as there is an equivalent copy of all refs available.
3182 *
3183 * 6. Move the new ref storage files into place.
3184 *
3185 * 7. Change the repository format to the new ref format.
3186 */
3187 strbuf_addf(&new_gitdir, "%s/%s", old_refs->gitdir, "ref_migration.XXXXXX");
3188 if (!mkdtemp(new_gitdir.buf)) {
3189 strbuf_addf(errbuf, "cannot create migration directory: %s",
3190 strerror(errno));
3191 ret = -1;
3192 goto done;
3193 }
3194
3195 new_refs = ref_store_init(repo, format, new_gitdir.buf,
3196 REF_STORE_ALL_CAPS);
3197 ret = ref_store_create_on_disk(new_refs, 0, errbuf);
3198 if (ret < 0)
3199 goto done;
3200
3201 transaction = ref_store_transaction_begin(new_refs, REF_TRANSACTION_FLAG_INITIAL,
3202 errbuf);
3203 if (!transaction)
3204 goto done;
3205
3206 data.old_refs = old_refs;
3207 data.transaction = transaction;
3208 data.errbuf = errbuf;
3209
3210 /*
3211 * We need to use the internal `do_for_each_ref()` here so that we can
3212 * also include broken refs and symrefs. These would otherwise be
3213 * skipped silently.
3214 *
3215 * Ideally, we would do this call while locking the old ref storage
3216 * such that there cannot be any concurrent modifications. We do not
3217 * have the infra for that though, and the "files" backend does not
3218 * allow for a central lock due to its design. It's thus on the user to
3219 * ensure that there are no concurrent writes.
3220 */
3221 ret = do_for_each_ref(old_refs, "", NULL, migrate_one_ref, 0,
3222 DO_FOR_EACH_INCLUDE_ROOT_REFS | DO_FOR_EACH_INCLUDE_BROKEN,
3223 &data);
3224 if (ret < 0)
3225 goto done;
3226
3227 if (!(flags & REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG)) {
3228 ret = refs_for_each_reflog(old_refs, migrate_one_reflog, &data);
3229 if (ret < 0)
3230 goto done;
3231 }
3232
3233 ret = ref_transaction_commit(transaction, errbuf);
3234 if (ret < 0)
3235 goto done;
3236 did_migrate_refs = 1;
3237
3238 if (flags & REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN) {
3239 printf(_("Finished dry-run migration of refs, "
3240 "the result can be found at '%s'\n"), new_gitdir.buf);
3241 ret = 0;
3242 goto done;
3243 }
3244
3245 /*
3246 * Release the new ref store such that any potentially-open files will
3247 * be closed. This is required for platforms like Cygwin, where
3248 * renaming an open file results in EPERM.
3249 */
3250 ref_store_release(new_refs);
3251 FREE_AND_NULL(new_refs);
3252
3253 /*
3254 * Until now we were in the non-destructive phase, where we only
3255 * populated the new ref store. From hereon though we are about
3256 * to get hands by deleting the old ref store and then moving
3257 * the new one into place.
3258 *
3259 * Assuming that there were no concurrent writes, the new ref
3260 * store should have all information. So if we fail from hereon
3261 * we may be in an in-between state, but it would still be able
3262 * to recover by manually moving remaining files from the
3263 * temporary migration directory into place.
3264 */
3265 ret = ref_store_remove_on_disk(old_refs, errbuf);
3266 if (ret < 0)
3267 goto done;
3268
3269 ret = move_files(new_gitdir.buf, old_refs->gitdir, errbuf);
3270 if (ret < 0)
3271 goto done;
3272
3273 if (rmdir(new_gitdir.buf) < 0)
3274 warning_errno(_("could not remove temporary migration directory '%s'"),
3275 new_gitdir.buf);
3276
3277 /*
3278 * We have migrated the repository, so we now need to adjust the
3279 * repository format so that clients will use the new ref store.
3280 * We also need to swap out the repository's main ref store.
3281 */
3282 initialize_repository_version(hash_algo_by_ptr(repo->hash_algo), format, 1);
3283
3284 /*
3285 * Unset the old ref store and release it. `get_main_ref_store()` will
3286 * make sure to lazily re-initialize the repository's ref store with
3287 * the new format.
3288 */
3289 ref_store_release(old_refs);
3290 FREE_AND_NULL(old_refs);
3291 repo->refs_private = NULL;
3292
3293 ret = 0;
3294
3295done:
3296 if (ret && did_migrate_refs) {
3297 strbuf_complete(errbuf, '\n');
3298 strbuf_addf(errbuf, _("migrated refs can be found at '%s'"),
3299 new_gitdir.buf);
3300 }
3301
3302 if (new_refs) {
3303 ref_store_release(new_refs);
3304 free(new_refs);
3305 }
3306 ref_transaction_free(transaction);
3307 strbuf_release(&new_gitdir);
3308 strbuf_release(&data.sb);
3309 return ret;
3310}
3311
3312int ref_update_expects_existing_old_ref(struct ref_update *update)
3313{
3314 return (update->flags & REF_HAVE_OLD) &&
3315 (!is_null_oid(&update->old_oid) || update->old_target);
3316}