Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/branch.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / branch.c` 913 lines ยท 27036 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 3 #include "git-compat-util.h" 4 #include "advice.h" 5 #include "config.h" 6 #include "branch.h" 7 #include "environment.h" 8 #include "gettext.h" 9 #include "hex.h" 10 #include "object-name.h" 11 #include "path.h" 12 #include "refs.h" 13 #include "refspec.h" 14 #include "remote.h" 15 #include "repository.h" 16 #include "sequencer.h" 17 #include "commit.h" 18 #include "worktree.h" 19 #include "submodule-config.h" 20 #include "run-command.h" 21 #include "strmap.h" 22 23 struct tracking { 24 struct refspec_item spec; 25 struct string_list *srcs; 26 const char *remote; 27 int matches; 28 }; 29 30 struct find_tracked_branch_cb { 31 struct tracking *tracking; 32 struct string_list ambiguous_remotes; 33 }; 34 35 static int find_tracked_branch(struct remote *remote, void *priv) 36 { 37 struct find_tracked_branch_cb *ftb = priv; 38 struct tracking *tracking = ftb->tracking; 39 40 if (!remote_find_tracking(remote, &tracking->spec)) { 41 switch (++tracking->matches) { 42 case 1: 43 string_list_append_nodup(tracking->srcs, tracking->spec.src); 44 tracking->remote = remote->name; 45 break; 46 case 2: 47 /* there are at least two remotes; backfill the first one */ 48 string_list_append(&ftb->ambiguous_remotes, tracking->remote); 49 /* fall through */ 50 default: 51 string_list_append(&ftb->ambiguous_remotes, remote->name); 52 free(tracking->spec.src); 53 string_list_clear(tracking->srcs, 0); 54 break; 55 } 56 /* remote_find_tracking() searches by src if present */ 57 tracking->spec.src = NULL; 58 } 59 return 0; 60 } 61 62 static int should_setup_rebase(const char *origin) 63 { 64 switch (repo_config_values(the_repository)->autorebase) { 65 case AUTOREBASE_NEVER: 66 return 0; 67 case AUTOREBASE_LOCAL: 68 return origin == NULL; 69 case AUTOREBASE_REMOTE: 70 return origin != NULL; 71 case AUTOREBASE_ALWAYS: 72 return 1; 73 } 74 return 0; 75 } 76 77 /** 78 * Install upstream tracking configuration for a branch; specifically, add 79 * `branch..remote` and `branch..merge` entries. 80 * 81 * `flag` contains integer flags for options; currently only 82 * BRANCH_CONFIG_VERBOSE is checked. 83 * 84 * `local` is the name of the branch whose configuration we're installing. 85 * 86 * `origin` is the name of the remote owning the upstream branches. NULL means 87 * the upstream branches are local to this repo. 88 * 89 * `remotes` is a list of refs that are upstream of local 90 */ 91 static int install_branch_config_multiple_remotes(int flag, const char *local, 92 const char *origin, struct string_list *remotes) 93 { 94 const char *shortname = NULL; 95 struct strbuf key = STRBUF_INIT; 96 struct string_list_item *item; 97 int rebasing = should_setup_rebase(origin); 98 99 if (!remotes->nr) 100 BUG("must provide at least one remote for branch config"); 101 if (rebasing && remotes->nr > 1) 102 die(_("cannot inherit upstream tracking configuration of " 103 "multiple refs when rebasing is requested")); 104 105 /* 106 * If the new branch is trying to track itself, something has gone 107 * wrong. Warn the user and don't proceed any further. 108 */ 109 if (!origin) 110 for_each_string_list_item(item, remotes) 111 if (skip_prefix(item->string, "refs/heads/", &shortname) 112 && !strcmp(local, shortname)) { 113 warning(_("not setting branch '%s' as its own upstream"), 114 local); 115 return 0; 116 } 117 118 strbuf_addf(&key, "branch.%s.remote", local); 119 if (repo_config_set_gently(the_repository, key.buf, origin ? origin : ".") < 0) 120 goto out_err; 121 122 strbuf_reset(&key); 123 strbuf_addf(&key, "branch.%s.merge", local); 124 /* 125 * We want to overwrite any existing config with all the branches in 126 * "remotes". Override any existing config, then write our branches. If 127 * more than one is provided, use CONFIG_REGEX_NONE to preserve what 128 * we've written so far. 129 */ 130 if (repo_config_set_gently(the_repository, key.buf, NULL) < 0) 131 goto out_err; 132 for_each_string_list_item(item, remotes) 133 if (repo_config_set_multivar_gently(the_repository, key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0) 134 goto out_err; 135 136 if (rebasing) { 137 strbuf_reset(&key); 138 strbuf_addf(&key, "branch.%s.rebase", local); 139 if (repo_config_set_gently(the_repository, key.buf, "true") < 0) 140 goto out_err; 141 } 142 strbuf_release(&key); 143 144 if (flag & BRANCH_CONFIG_VERBOSE) { 145 struct strbuf tmp_ref_name = STRBUF_INIT; 146 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP; 147 148 for_each_string_list_item(item, remotes) { 149 shortname = item->string; 150 skip_prefix(shortname, "refs/heads/", &shortname); 151 if (origin) { 152 strbuf_addf(&tmp_ref_name, "%s/%s", 153 origin, shortname); 154 string_list_append_nodup( 155 &friendly_ref_names, 156 strbuf_detach(&tmp_ref_name, NULL)); 157 } else { 158 string_list_append( 159 &friendly_ref_names, shortname); 160 } 161 } 162 163 if (remotes->nr == 1) { 164 /* 165 * Rebasing is only allowed in the case of a single 166 * upstream branch. 167 */ 168 printf_ln(rebasing ? 169 _("branch '%s' set up to track '%s' by rebasing.") : 170 _("branch '%s' set up to track '%s'."), 171 local, friendly_ref_names.items[0].string); 172 } else { 173 printf_ln(_("branch '%s' set up to track:"), local); 174 for_each_string_list_item(item, &friendly_ref_names) 175 printf_ln(" %s", item->string); 176 } 177 178 string_list_clear(&friendly_ref_names, 0); 179 } 180 181 return 0; 182 183 out_err: 184 strbuf_release(&key); 185 error(_("unable to write upstream branch configuration")); 186 187 advise(_("\nAfter fixing the error cause you may try to fix up\n" 188 "the remote tracking information by invoking:")); 189 if (remotes->nr == 1) { 190 shortname = remotes->items[0].string; 191 skip_prefix(shortname, "refs/heads/", &shortname); 192 advise(" git branch --set-upstream-to=%s%s%s", 193 origin ? origin : "", 194 origin ? "/" : "", 195 shortname); 196 } else { 197 advise(" git config --add branch.\"%s\".remote %s", 198 local, origin ? origin : "."); 199 for_each_string_list_item(item, remotes) 200 advise(" git config --add branch.\"%s\".merge %s", 201 local, item->string); 202 } 203 204 return -1; 205 } 206 207 int install_branch_config(int flag, const char *local, const char *origin, 208 const char *remote) 209 { 210 int ret; 211 struct string_list remotes = STRING_LIST_INIT_DUP; 212 213 string_list_append(&remotes, remote); 214 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes); 215 string_list_clear(&remotes, 0); 216 return ret; 217 } 218 219 static int inherit_tracking(struct tracking *tracking, const char *orig_ref) 220 { 221 const char *bare_ref; 222 struct branch *branch; 223 int i; 224 225 bare_ref = orig_ref; 226 skip_prefix(orig_ref, "refs/heads/", &bare_ref); 227 228 branch = branch_get(bare_ref); 229 if (!branch->remote_name) { 230 warning(_("asked to inherit tracking from '%s', but no remote is set"), 231 bare_ref); 232 return -1; 233 } 234 235 if (branch->merge_nr < 1 || !branch->merge || !branch->merge[0] || !branch->merge[0]->src) { 236 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"), 237 bare_ref); 238 return -1; 239 } 240 241 tracking->remote = branch->remote_name; 242 for (i = 0; i < branch->merge_nr; i++) 243 string_list_append(tracking->srcs, branch->merge[i]->src); 244 return 0; 245 } 246 247 /* 248 * Used internally to set the branch..{remote,merge} config 249 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike 250 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main" 251 * will not be expanded to "refs/remotes/origin/main", so it is not safe 252 * for 'orig_ref' to be raw user input. 253 */ 254 static int setup_tracking(const char *new_ref, const char *orig_ref, 255 enum branch_track track, int quiet) 256 { 257 struct tracking tracking; 258 struct string_list tracking_srcs = STRING_LIST_INIT_DUP; 259 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE; 260 int ret = 0; 261 struct find_tracked_branch_cb ftb_cb = { 262 .tracking = &tracking, 263 .ambiguous_remotes = STRING_LIST_INIT_DUP, 264 }; 265 266 if (!track) 267 BUG("asked to set up tracking, but tracking is disallowed"); 268 269 memset(&tracking, 0, sizeof(tracking)); 270 tracking.spec.dst = (char *)orig_ref; 271 tracking.srcs = &tracking_srcs; 272 if (track != BRANCH_TRACK_INHERIT) 273 for_each_remote(find_tracked_branch, &ftb_cb); 274 else if (inherit_tracking(&tracking, orig_ref)) 275 goto cleanup; 276 277 if (!tracking.matches) 278 switch (track) { 279 /* If ref is not remote, still use local */ 280 case BRANCH_TRACK_ALWAYS: 281 case BRANCH_TRACK_EXPLICIT: 282 case BRANCH_TRACK_OVERRIDE: 283 /* Remote matches not evaluated */ 284 case BRANCH_TRACK_INHERIT: 285 break; 286 /* Otherwise, if no remote don't track */ 287 default: 288 goto cleanup; 289 } 290 291 /* 292 * This check does not apply to BRANCH_TRACK_INHERIT; 293 * that supports multiple entries in tracking_srcs but 294 * leaves tracking.matches at 0. 295 */ 296 if (tracking.matches > 1) { 297 int status = die_message(_("not tracking: ambiguous information for ref '%s'"), 298 orig_ref); 299 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) { 300 struct strbuf remotes_advice = STRBUF_INIT; 301 struct string_list_item *item; 302 303 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes) 304 /* 305 * TRANSLATORS: This is a line listing a remote with duplicate 306 * refspecs in the advice message below. For RTL languages you'll 307 * probably want to swap the "%s" and leading " " space around. 308 */ 309 strbuf_addf(&remotes_advice, _(" %s\n"), item->string); 310 311 /* 312 * TRANSLATORS: The second argument is a \n-delimited list of 313 * duplicate refspecs, composed above. 314 */ 315 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n" 316 "tracking ref '%s':\n" 317 "%s" 318 "\n" 319 "This is typically a configuration error.\n" 320 "\n" 321 "To support setting up tracking branches, ensure that\n" 322 "different remotes' fetch refspecs map into different\n" 323 "tracking namespaces."), orig_ref, 324 remotes_advice.buf); 325 strbuf_release(&remotes_advice); 326 } 327 exit(status); 328 } 329 330 if (track == BRANCH_TRACK_SIMPLE) { 331 /* 332 * Only track if remote branch name matches. 333 * Reaching into items[0].string is safe because 334 * we know there is at least one and not more than 335 * one entry (because only BRANCH_TRACK_INHERIT can 336 * produce more than one entry). 337 */ 338 const char *tracked_branch; 339 if (!skip_prefix(tracking.srcs->items[0].string, 340 "refs/heads/", &tracked_branch) || 341 strcmp(tracked_branch, new_ref)) 342 goto cleanup; 343 } 344 345 if (tracking.srcs->nr < 1) 346 string_list_append(tracking.srcs, orig_ref); 347 if (install_branch_config_multiple_remotes(config_flags, new_ref, 348 tracking.remote, tracking.srcs) < 0) 349 ret = -1; 350 351 cleanup: 352 string_list_clear(&tracking_srcs, 0); 353 string_list_clear(&ftb_cb.ambiguous_remotes, 0); 354 return ret; 355 } 356 357 int read_branch_desc(struct strbuf *buf, const char *branch_name) 358 { 359 char *v = NULL; 360 struct strbuf name = STRBUF_INIT; 361 strbuf_addf(&name, "branch.%s.description", branch_name); 362 if (repo_config_get_string(the_repository, name.buf, &v)) { 363 strbuf_release(&name); 364 return -1; 365 } 366 strbuf_addstr(buf, v); 367 free(v); 368 strbuf_release(&name); 369 return 0; 370 } 371 372 /* 373 * Check if 'name' can be a valid name for a branch; die otherwise. 374 * Return 1 if the named branch already exists; return 0 otherwise. 375 * Fill ref with the full refname for the branch. 376 */ 377 int validate_branchname(const char *name, struct strbuf *ref) 378 { 379 if (check_branch_ref(the_repository, ref, name)) { 380 int code = die_message(_("'%s' is not a valid branch name"), name); 381 advise_if_enabled(ADVICE_REF_SYNTAX, 382 _("See 'git help check-ref-format'")); 383 exit(code); 384 } 385 386 return refs_ref_exists(get_main_ref_store(the_repository), ref->buf); 387 } 388 389 static int initialized_checked_out_branches; 390 static struct strmap current_checked_out_branches = STRMAP_INIT; 391 392 enum branch_checkout_kind { 393 BRANCH_CHECKOUT_KIND_CHECKOUT, 394 BRANCH_CHECKOUT_KIND_REBASE, 395 BRANCH_CHECKOUT_KIND_BISECT, 396 BRANCH_CHECKOUT_KIND_UPDATE_REF, 397 }; 398 399 struct checked_out_branch { 400 char *refname; 401 char *path; 402 enum branch_checkout_kind kind; 403 }; 404 405 static struct checked_out_branch *checked_out_branches; 406 static size_t checked_out_branches_alloc, checked_out_branches_nr; 407 408 static void register_checked_out_branch(const char *prefix, const char *name, 409 const char *path, 410 enum branch_checkout_kind kind) 411 { 412 char *refname = xstrfmt("%s%s", prefix, name); 413 char *path_copy = xstrdup(path); 414 415 ALLOC_GROW(checked_out_branches, checked_out_branches_nr + 1, 416 checked_out_branches_alloc); 417 checked_out_branches[checked_out_branches_nr].refname = refname; 418 checked_out_branches[checked_out_branches_nr].path = path_copy; 419 checked_out_branches[checked_out_branches_nr].kind = kind; 420 checked_out_branches_nr++; 421 422 strmap_put(¤t_checked_out_branches, refname, path_copy); 423 } 424 425 static void prepare_checked_out_branches(void) 426 { 427 int i = 0; 428 struct worktree **worktrees; 429 430 if (initialized_checked_out_branches) 431 return; 432 initialized_checked_out_branches = 1; 433 434 worktrees = get_worktrees(the_repository); 435 436 while (worktrees[i]) { 437 char *wt_gitdir; 438 struct wt_status_state state = { 0 }; 439 struct worktree *wt = worktrees[i++]; 440 struct string_list update_refs = STRING_LIST_INIT_DUP; 441 442 if (wt->is_bare) 443 continue; 444 445 if (wt->head_ref) { 446 register_checked_out_branch("", wt->head_ref, wt->path, 447 BRANCH_CHECKOUT_KIND_CHECKOUT); 448 } 449 450 if (wt_status_check_rebase(wt, &state) && 451 (state.rebase_in_progress || state.rebase_interactive_in_progress) && 452 state.branch) { 453 register_checked_out_branch("refs/heads/", state.branch, 454 wt->path, 455 BRANCH_CHECKOUT_KIND_REBASE); 456 } 457 wt_status_state_free_buffers(&state); 458 459 if (wt_status_check_bisect(wt, &state) && 460 state.bisecting_from) { 461 register_checked_out_branch("refs/heads/", 462 state.bisecting_from, 463 wt->path, 464 BRANCH_CHECKOUT_KIND_BISECT); 465 } 466 wt_status_state_free_buffers(&state); 467 468 wt_gitdir = get_worktree_git_dir(wt); 469 if (!sequencer_get_update_refs_state(wt_gitdir, 470 &update_refs)) { 471 struct string_list_item *item; 472 for_each_string_list_item(item, &update_refs) { 473 register_checked_out_branch("", item->string, 474 wt->path, 475 BRANCH_CHECKOUT_KIND_UPDATE_REF); 476 } 477 string_list_clear(&update_refs, 1); 478 } 479 480 free(wt_gitdir); 481 } 482 483 free_worktrees(worktrees); 484 } 485 486 const char *branch_checked_out(const char *refname) 487 { 488 prepare_checked_out_branches(); 489 return strmap_get(¤t_checked_out_branches, refname); 490 } 491 492 const char *branch_bisecting(const char *refname) 493 { 494 prepare_checked_out_branches(); 495 for (size_t i = 0; i < checked_out_branches_nr; i++) { 496 if (!strcmp(refname, checked_out_branches[i].refname) && 497 checked_out_branches[i].kind == BRANCH_CHECKOUT_KIND_BISECT) 498 return checked_out_branches[i].path; 499 } 500 return NULL; 501 } 502 503 /* 504 * Check if a branch 'name' can be created as a new branch; die otherwise. 505 * 'force' can be used when it is OK for the named branch already exists. 506 * Return 1 if the named branch already exists; return 0 otherwise. 507 * Fill ref with the full refname for the branch. 508 */ 509 int validate_new_branchname(const char *name, struct strbuf *ref, int force) 510 { 511 const char *path; 512 if (!validate_branchname(name, ref)) 513 return 0; 514 515 if (!force) 516 die(_("a branch named '%s' already exists"), 517 ref->buf + strlen("refs/heads/")); 518 519 if ((path = branch_checked_out(ref->buf))) 520 die(_("cannot force update the branch '%s' " 521 "used by worktree at '%s'"), 522 ref->buf + strlen("refs/heads/"), path); 523 524 return 1; 525 } 526 527 static int check_tracking_branch(struct remote *remote, void *cb_data) 528 { 529 char *tracking_branch = cb_data; 530 struct refspec_item query; 531 int res; 532 memset(&query, 0, sizeof(struct refspec_item)); 533 query.dst = tracking_branch; 534 res = !remote_find_tracking(remote, &query); 535 free(query.src); 536 return res; 537 } 538 539 static int validate_remote_tracking_branch(char *ref) 540 { 541 return !for_each_remote(check_tracking_branch, ref); 542 } 543 544 static const char upstream_not_branch[] = 545 N_("cannot set up tracking information; starting point '%s' is not a branch"); 546 static const char upstream_missing[] = 547 N_("the requested upstream branch '%s' does not exist"); 548 static const char upstream_advice[] = 549 N_("\n" 550 "If you are planning on basing your work on an upstream\n" 551 "branch that already exists at the remote, you may need to\n" 552 "run \"git fetch\" to retrieve it.\n" 553 "\n" 554 "If you are planning to push out a new local branch that\n" 555 "will track its remote counterpart, you may want to use\n" 556 "\"git push -u\" to set the upstream config as you push."); 557 558 /** 559 * DWIMs a user-provided ref to determine the starting point for a 560 * branch and validates it, where: 561 * 562 * - r is the repository to validate the branch for 563 * 564 * - start_name is the ref that we would like to test. This is 565 * expanded with DWIM and assigned to out_real_ref. 566 * 567 * - track is the tracking mode of the new branch. If tracking is 568 * explicitly requested, start_name must be a branch (because 569 * otherwise start_name cannot be tracked) 570 * 571 * - out_oid is an out parameter containing the object_id of start_name 572 * 573 * - out_real_ref is an out parameter containing the full, 'real' form 574 * of start_name e.g. refs/heads/main instead of main 575 * 576 */ 577 static void dwim_branch_start(struct repository *r, const char *start_name, 578 enum branch_track track, char **out_real_ref, 579 struct object_id *out_oid) 580 { 581 struct commit *commit; 582 struct object_id oid; 583 char *real_ref; 584 int explicit_tracking = 0; 585 586 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE) 587 explicit_tracking = 1; 588 589 real_ref = NULL; 590 if (repo_get_oid_mb(r, start_name, &oid)) { 591 if (explicit_tracking) { 592 int code = die_message(_(upstream_missing), start_name); 593 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE, 594 _(upstream_advice)); 595 exit(code); 596 } 597 die(_("not a valid object name: '%s'"), start_name); 598 } 599 600 switch (repo_dwim_ref(r, start_name, strlen(start_name), &oid, 601 &real_ref, 0)) { 602 case 0: 603 /* Not branching from any existing branch */ 604 if (explicit_tracking) 605 die(_(upstream_not_branch), start_name); 606 break; 607 case 1: 608 /* Unique completion -- good, only if it is a real branch */ 609 if (!starts_with(real_ref, "refs/heads/") && 610 validate_remote_tracking_branch(real_ref)) { 611 if (explicit_tracking) 612 die(_(upstream_not_branch), start_name); 613 else 614 FREE_AND_NULL(real_ref); 615 } 616 break; 617 default: 618 die(_("ambiguous object name: '%s'"), start_name); 619 break; 620 } 621 622 if (!(commit = lookup_commit_reference(r, &oid))) 623 die(_("not a valid branch point: '%s'"), start_name); 624 if (out_real_ref) { 625 *out_real_ref = real_ref; 626 real_ref = NULL; 627 } 628 if (out_oid) 629 oidcpy(out_oid, &commit->object.oid); 630 631 FREE_AND_NULL(real_ref); 632 } 633 634 int create_branch(struct repository *r, 635 const char *name, const char *start_name, 636 int force, int clobber_head_ok, int reflog, 637 int quiet, enum branch_track track, int dry_run) 638 { 639 struct object_id oid; 640 char *real_ref; 641 struct strbuf ref = STRBUF_INIT; 642 int forcing = 0; 643 struct ref_transaction *transaction; 644 struct strbuf err = STRBUF_INIT; 645 int flags = 0; 646 int ret = 0; 647 char *msg; 648 649 if (track == BRANCH_TRACK_OVERRIDE) 650 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?"); 651 if (clobber_head_ok && !force) 652 BUG("'clobber_head_ok' can only be used with 'force'"); 653 654 if (clobber_head_ok ? 655 validate_branchname(name, &ref) : 656 validate_new_branchname(name, &ref, force)) { 657 forcing = 1; 658 } 659 660 dwim_branch_start(r, start_name, track, &real_ref, &oid); 661 if (dry_run) 662 goto cleanup; 663 664 if (reflog) 665 flags |= REF_FORCE_CREATE_REFLOG; 666 667 if (forcing) 668 msg = xstrfmt("branch: Reset to %s", start_name); 669 else 670 msg = xstrfmt("branch: Created from %s", start_name); 671 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 672 0, &err); 673 if (!transaction || 674 ref_transaction_update(transaction, ref.buf, 675 &oid, forcing ? NULL : null_oid(the_hash_algo), 676 NULL, NULL, flags, msg, &err) || 677 ref_transaction_commit(transaction, &err)) 678 die("%s", err.buf); 679 ref_transaction_free(transaction); 680 strbuf_release(&err); 681 free(msg); 682 683 if (real_ref && track) 684 ret = setup_tracking(ref.buf + 11, real_ref, track, quiet); 685 686 cleanup: 687 strbuf_release(&ref); 688 free(real_ref); 689 return ret; 690 } 691 692 int dwim_and_setup_tracking(struct repository *r, const char *new_ref, 693 const char *orig_ref, enum branch_track track, 694 int quiet) 695 { 696 char *real_orig_ref = NULL; 697 int ret; 698 699 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL); 700 ret = setup_tracking(new_ref, real_orig_ref, track, quiet); 701 free(real_orig_ref); 702 return ret; 703 } 704 705 /** 706 * Creates a branch in a submodule by calling 707 * create_branches_recursively() in a child process. The child process 708 * is necessary because install_branch_config_multiple_remotes() (which 709 * is called by setup_tracking()) does not support writing configs to 710 * submodules. 711 */ 712 static int submodule_create_branch(struct repository *r, 713 const struct submodule *submodule, 714 const char *name, const char *start_oid, 715 const char *tracking_name, int force, 716 int reflog, int quiet, 717 enum branch_track track, int dry_run) 718 { 719 int ret = 0; 720 struct child_process child = CHILD_PROCESS_INIT; 721 struct strbuf child_err = STRBUF_INIT; 722 struct strbuf out_buf = STRBUF_INIT; 723 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name); 724 child.git_cmd = 1; 725 child.err = -1; 726 child.stdout_to_stderr = 1; 727 728 prepare_other_repo_env(&child.env, r->gitdir); 729 /* 730 * submodule_create_branch() is indirectly invoked by "git 731 * branch", but we cannot invoke "git branch" in the child 732 * process. "git branch" accepts a branch name and start point, 733 * where the start point is assumed to provide both the OID 734 * (start_oid) and the branch to use for tracking 735 * (tracking_name). But when recursing through submodules, 736 * start_oid and tracking name need to be specified separately 737 * (see create_branches_recursively()). 738 */ 739 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL); 740 if (dry_run) 741 strvec_push(&child.args, "--dry-run"); 742 if (force) 743 strvec_push(&child.args, "--force"); 744 if (quiet) 745 strvec_push(&child.args, "--quiet"); 746 if (reflog) 747 strvec_push(&child.args, "--create-reflog"); 748 749 switch (track) { 750 case BRANCH_TRACK_NEVER: 751 strvec_push(&child.args, "--no-track"); 752 break; 753 case BRANCH_TRACK_ALWAYS: 754 case BRANCH_TRACK_EXPLICIT: 755 strvec_push(&child.args, "--track=direct"); 756 break; 757 case BRANCH_TRACK_OVERRIDE: 758 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch."); 759 break; 760 case BRANCH_TRACK_INHERIT: 761 strvec_push(&child.args, "--track=inherit"); 762 break; 763 case BRANCH_TRACK_UNSPECIFIED: 764 /* Default for "git checkout". Do not pass --track. */ 765 case BRANCH_TRACK_REMOTE: 766 /* Default for "git branch". Do not pass --track. */ 767 case BRANCH_TRACK_SIMPLE: 768 /* Config-driven only. Do not pass --track. */ 769 break; 770 } 771 772 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL); 773 774 if ((ret = start_command(&child))) 775 return ret; 776 ret = finish_command(&child); 777 strbuf_read(&child_err, child.err, 0); 778 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len); 779 780 if (ret) 781 fprintf(stderr, "%s", out_buf.buf); 782 else 783 printf("%s", out_buf.buf); 784 785 strbuf_release(&child_err); 786 strbuf_release(&out_buf); 787 free(out_prefix); 788 return ret; 789 } 790 791 void create_branches_recursively(struct repository *r, const char *name, 792 const char *start_committish, 793 const char *tracking_name, int force, 794 int reflog, int quiet, enum branch_track track, 795 int dry_run) 796 { 797 int i = 0; 798 char *branch_point = NULL; 799 struct object_id super_oid; 800 struct submodule_entry_list submodule_entry_list; 801 802 /* Perform dwim on start_committish to get super_oid and branch_point. */ 803 dwim_branch_start(r, start_committish, BRANCH_TRACK_NEVER, 804 &branch_point, &super_oid); 805 806 /* 807 * If we were not given an explicit name to track, then assume we are at 808 * the top level and, just like the non-recursive case, the tracking 809 * name is the branch point. 810 */ 811 if (!tracking_name) 812 tracking_name = branch_point; 813 814 submodules_of_tree(r, &super_oid, &submodule_entry_list); 815 /* 816 * Before creating any branches, first check that the branch can 817 * be created in every submodule. 818 */ 819 for (i = 0; i < submodule_entry_list.entry_nr; i++) { 820 if (!submodule_entry_list.entries[i].repo) { 821 int code = die_message( 822 _("submodule '%s': unable to find submodule"), 823 submodule_entry_list.entries[i].submodule->name); 824 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED)) 825 advise(_("You may try updating the submodules using 'git checkout --no-recurse-submodules %s && git submodule update --init'"), 826 start_committish); 827 exit(code); 828 } 829 830 if (submodule_create_branch( 831 submodule_entry_list.entries[i].repo, 832 submodule_entry_list.entries[i].submodule, name, 833 oid_to_hex(&submodule_entry_list.entries[i] 834 .name_entry->oid), 835 tracking_name, force, reflog, quiet, track, 1)) 836 die(_("submodule '%s': cannot create branch '%s'"), 837 submodule_entry_list.entries[i].submodule->name, 838 name); 839 } 840 841 create_branch(r, name, start_committish, force, 0, reflog, quiet, 842 BRANCH_TRACK_NEVER, dry_run); 843 if (dry_run) 844 goto out; 845 /* 846 * NEEDSWORK If tracking was set up in the superproject but not the 847 * submodule, users might expect "git branch --recurse-submodules" to 848 * fail or give a warning, but this is not yet implemented because it is 849 * tedious to determine whether or not tracking was set up in the 850 * superproject. 851 */ 852 if (track && setup_tracking(name, tracking_name, track, quiet)) 853 exit(1); 854 855 for (i = 0; i < submodule_entry_list.entry_nr; i++) { 856 if (submodule_create_branch( 857 submodule_entry_list.entries[i].repo, 858 submodule_entry_list.entries[i].submodule, name, 859 oid_to_hex(&submodule_entry_list.entries[i] 860 .name_entry->oid), 861 tracking_name, force, reflog, quiet, track, 0)) 862 die(_("submodule '%s': cannot create branch '%s'"), 863 submodule_entry_list.entries[i].submodule->name, 864 name); 865 } 866 867 out: 868 submodule_entry_list_release(&submodule_entry_list); 869 free(branch_point); 870 } 871 872 static void do_remove_merge_branch_state(struct repository *r, unsigned flags) 873 { 874 unlink(git_path_merge_head(r)); 875 if (!(flags & REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS)) 876 unlink(git_path_merge_labels(r)); 877 unlink(git_path_merge_rr(r)); 878 unlink(git_path_merge_msg(r)); 879 unlink(git_path_merge_mode(r)); 880 refs_delete_ref(get_main_ref_store(r), "", "AUTO_MERGE", 881 NULL, REF_NO_DEREF); 882 save_autostash_ref(r, "MERGE_AUTOSTASH"); 883 } 884 885 void remove_merge_branch_state(struct repository *r) 886 { 887 do_remove_merge_branch_state(r, 0); 888 } 889 890 void remove_branch_state(struct repository *r, unsigned flags) 891 { 892 sequencer_post_commit_cleanup(r, flags & REMOVE_BRANCH_STATE_VERBOSE); 893 unlink(git_path_squash_msg(r)); 894 do_remove_merge_branch_state(r, flags); 895 } 896 897 void die_if_checked_out(const char *branch, int ignore_current_worktree) 898 { 899 struct worktree **worktrees = get_worktrees(the_repository); 900 901 for (int i = 0; worktrees[i]; i++) { 902 if (worktrees[i]->is_current && ignore_current_worktree) 903 continue; 904 905 if (is_shared_symref(worktrees[i], "HEAD", branch)) { 906 skip_prefix(branch, "refs/heads/", &branch); 907 die(_("'%s' is already used by worktree at '%s'"), 908 branch, worktrees[i]->path); 909 } 910 } 911 912 free_worktrees(worktrees); 913 }