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