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/builtin/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 / builtin / branch.c` 1326 lines · 42175 bytes Raw Blame History 1 /* 2 * Builtin "git branch" 3 * 4 * Copyright (c) 2006 Kristian Høgsberg 5 * Based on git-branch.sh by Junio C Hamano. 6 */ 7 8 #define USE_THE_REPOSITORY_VARIABLE 9 10 #include "builtin.h" 11 #include "config.h" 12 #include "color.h" 13 #include "editor.h" 14 #include "environment.h" 15 #include "refs.h" 16 #include "commit.h" 17 #include "gettext.h" 18 #include "object-name.h" 19 #include "remote.h" 20 #include "parse-options.h" 21 #include "branch.h" 22 #include "path.h" 23 #include "string-list.h" 24 #include "strmap.h" 25 #include "column.h" 26 #include "utf8.h" 27 #include "ref-filter.h" 28 #include "worktree.h" 29 #include "help.h" 30 #include "advice.h" 31 #include "commit-reach.h" 32 33 static const char * const builtin_branch_usage[] = { 34 N_("git branch [] [-r | -a] [--merged] [--no-merged] [(--forked )...]"), 35 N_("git branch [] [-f] [--recurse-submodules] []"), 36 N_("git branch [] [-l] [...]"), 37 N_("git branch [] [-r] (-d | -D) ..."), 38 N_("git branch [] (-m | -M) [] "), 39 N_("git branch [] (-c | -C) [] "), 40 N_("git branch [] [-r | -a] [--points-at]"), 41 N_("git branch [] [-r | -a] [--format]"), 42 N_("git branch [] (--delete-merged )... " 43 "[...]"), 44 NULL 45 }; 46 47 static const char *head; 48 static struct object_id head_oid; 49 static int recurse_submodules = 0; 50 static int submodule_propagate_branches = 0; 51 52 static enum git_colorbool branch_use_color = GIT_COLOR_UNKNOWN; 53 static char branch_colors[][COLOR_MAXLEN] = { 54 GIT_COLOR_RESET, 55 GIT_COLOR_NORMAL, /* PLAIN */ 56 GIT_COLOR_RED, /* REMOTE */ 57 GIT_COLOR_NORMAL, /* LOCAL */ 58 GIT_COLOR_GREEN, /* CURRENT */ 59 GIT_COLOR_BLUE, /* UPSTREAM */ 60 GIT_COLOR_CYAN, /* WORKTREE */ 61 }; 62 enum color_branch { 63 BRANCH_COLOR_RESET = 0, 64 BRANCH_COLOR_PLAIN = 1, 65 BRANCH_COLOR_REMOTE = 2, 66 BRANCH_COLOR_LOCAL = 3, 67 BRANCH_COLOR_CURRENT = 4, 68 BRANCH_COLOR_UPSTREAM = 5, 69 BRANCH_COLOR_WORKTREE = 6 70 }; 71 72 static const char *color_branch_slots[] = { 73 [BRANCH_COLOR_RESET] = "reset", 74 [BRANCH_COLOR_PLAIN] = "plain", 75 [BRANCH_COLOR_REMOTE] = "remote", 76 [BRANCH_COLOR_LOCAL] = "local", 77 [BRANCH_COLOR_CURRENT] = "current", 78 [BRANCH_COLOR_UPSTREAM] = "upstream", 79 [BRANCH_COLOR_WORKTREE] = "worktree", 80 }; 81 82 static struct string_list output = STRING_LIST_INIT_DUP; 83 static unsigned int colopts; 84 85 define_list_config_array(color_branch_slots); 86 87 static int git_branch_config(const char *var, const char *value, 88 const struct config_context *ctx, void *cb) 89 { 90 const char *slot_name; 91 92 if (!strcmp(var, "branch.sort")) { 93 if (!value) 94 return config_error_nonbool(var); 95 string_list_append(cb, value); 96 return 0; 97 } 98 99 if (starts_with(var, "column.")) 100 return git_column_config(var, value, "branch", &colopts); 101 if (!strcmp(var, "color.branch")) { 102 branch_use_color = git_config_colorbool(var, value); 103 return 0; 104 } 105 if (skip_prefix(var, "color.branch.", &slot_name)) { 106 int slot = LOOKUP_CONFIG(color_branch_slots, slot_name); 107 if (slot < 0) 108 return 0; 109 if (!value) 110 return config_error_nonbool(var); 111 return color_parse(value, branch_colors[slot]); 112 } 113 if (!strcmp(var, "submodule.recurse")) { 114 recurse_submodules = git_config_bool(var, value); 115 return 0; 116 } 117 if (!strcasecmp(var, "submodule.propagateBranches")) { 118 submodule_propagate_branches = git_config_bool(var, value); 119 return 0; 120 } 121 122 if (git_color_config(var, value, cb) < 0) 123 return -1; 124 125 return git_default_config(var, value, ctx, cb); 126 } 127 128 static const char *branch_get_color(enum color_branch ix) 129 { 130 if (want_color(branch_use_color)) 131 return branch_colors[ix]; 132 return ""; 133 } 134 135 static int branch_merged(int kind, const char *name, 136 struct commit *rev, struct commit *head_rev) 137 { 138 /* 139 * This checks whether the merge bases of branch and HEAD (or 140 * the other branch this branch builds upon) contains the 141 * branch, which means that the branch has already been merged 142 * safely to HEAD (or the other branch). 143 */ 144 struct commit *reference_rev = NULL; 145 const char *reference_name = NULL; 146 void *reference_name_to_free = NULL; 147 int merged; 148 149 if (kind == FILTER_REFS_BRANCHES) { 150 struct branch *branch = branch_get(name); 151 const char *upstream = branch_get_upstream(branch, NULL); 152 struct object_id oid; 153 154 if (upstream && 155 (reference_name = reference_name_to_free = 156 refs_resolve_refdup(get_main_ref_store(the_repository), upstream, RESOLVE_REF_READING, 157 &oid, NULL)) != NULL) 158 reference_rev = lookup_commit_reference(the_repository, 159 &oid); 160 } 161 if (!reference_rev) 162 reference_rev = head_rev; 163 164 merged = reference_rev ? repo_in_merge_bases(the_repository, rev, 165 reference_rev) : 0; 166 if (merged < 0) 167 exit(128); 168 169 /* 170 * After the safety valve is fully redefined to "check with 171 * upstream, if any, otherwise with HEAD", we should just 172 * return the result of the repo_in_merge_bases() above without 173 * any of the following code, but during the transition period, 174 * a gentle reminder is in order. Callers that opt out of the 175 * HEAD fallback by passing head_rev=NULL are not interested in 176 * the reminder either: they have already established that the 177 * branch has an upstream, so HEAD is irrelevant to the decision. 178 */ 179 if (head_rev && head_rev != reference_rev) { 180 int expect = repo_in_merge_bases(the_repository, rev, head_rev); 181 if (expect < 0) 182 exit(128); 183 if (expect == merged) 184 ; /* okay */ 185 else if (merged) 186 warning(_("deleting branch '%s' that has been merged to\n" 187 " '%s', but not yet merged to HEAD"), 188 name, reference_name); 189 else 190 warning(_("not deleting branch '%s' that is not yet merged to\n" 191 " '%s', even though it is merged to HEAD"), 192 name, reference_name); 193 } 194 free(reference_name_to_free); 195 return merged; 196 } 197 198 enum delete_branch_flags { 199 DELETE_BRANCH_FORCE = (1 << 0), 200 DELETE_BRANCH_QUIET = (1 << 1), 201 DELETE_BRANCH_SKIP_UNMERGED = (1 << 2), 202 DELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3), 203 DELETE_BRANCH_DRY_RUN = (1 << 4), 204 }; 205 206 static int check_branch_commit(const char *branchname, const char *refname, 207 const struct object_id *oid, struct commit *head_rev, 208 int kinds, unsigned int flags) 209 { 210 struct commit *rev = lookup_commit_reference(the_repository, oid); 211 if (!(flags & DELETE_BRANCH_FORCE) && !rev) { 212 error(_("couldn't look up commit object for '%s'"), refname); 213 return -1; 214 } 215 if (!(flags & DELETE_BRANCH_FORCE) && 216 !branch_merged(kinds, branchname, rev, head_rev)) { 217 if (!(flags & DELETE_BRANCH_SKIP_UNMERGED)) { 218 error(_("the branch '%s' is not fully merged"), 219 branchname); 220 advise_if_enabled(ADVICE_FORCE_DELETE_BRANCH, 221 _("If you are sure you want to delete it, " 222 "run 'git branch -D %s'"), branchname); 223 } 224 return -1; 225 } 226 return 0; 227 } 228 229 static void delete_branch_config(const char *branchname) 230 { 231 struct strbuf buf = STRBUF_INIT; 232 strbuf_addf(&buf, "branch.%s", branchname); 233 if (repo_config_rename_section(the_repository, buf.buf, NULL) < 0) 234 warning(_("update of config-file failed")); 235 strbuf_release(&buf); 236 } 237 238 static int delete_branches(int argc, const char **argv, int kinds, 239 unsigned int flags) 240 { 241 struct commit *head_rev = NULL; 242 struct object_id oid; 243 char *name = NULL; 244 const char *fmt; 245 int i; 246 int ret = 0; 247 int remote_branch = 0; 248 struct strbuf bname = STRBUF_INIT; 249 enum interpret_branch_kind allowed_interpret; 250 struct string_list refs_to_delete = STRING_LIST_INIT_DUP; 251 struct string_list_item *item; 252 int branch_name_pos; 253 const char *fmt_remotes = "refs/remotes/%s"; 254 255 switch (kinds) { 256 case FILTER_REFS_REMOTES: 257 fmt = fmt_remotes; 258 /* For subsequent UI messages */ 259 remote_branch = 1; 260 allowed_interpret = INTERPRET_BRANCH_REMOTE; 261 262 flags |= DELETE_BRANCH_FORCE; 263 break; 264 case FILTER_REFS_BRANCHES: 265 fmt = "refs/heads/%s"; 266 allowed_interpret = INTERPRET_BRANCH_LOCAL; 267 break; 268 default: 269 die(_("cannot use -a with -d")); 270 } 271 branch_name_pos = strcspn(fmt, "%"); 272 273 if (!(flags & DELETE_BRANCH_FORCE) && 274 !(flags & DELETE_BRANCH_NO_HEAD_FALLBACK)) 275 head_rev = lookup_commit_reference(the_repository, &head_oid); 276 277 for (i = 0; i < argc; i++, strbuf_reset(&bname)) { 278 char *target = NULL; 279 int ref_flags = 0; 280 281 copy_branchname(the_repository, &bname, 282 argv[i], allowed_interpret); 283 free(name); 284 name = mkpathdup(fmt, bname.buf); 285 286 if (kinds == FILTER_REFS_BRANCHES) { 287 const char *path; 288 if ((path = branch_bisecting(name))) { 289 error(_("cannot delete branch '%s' " 290 "used by worktree at '%s' for bisect"), 291 bname.buf, path); 292 ret = 1; 293 continue; 294 } 295 if ((path = branch_checked_out(name))) { 296 error(_("cannot delete branch '%s' " 297 "used by worktree at '%s'"), 298 bname.buf, path); 299 ret = 1; 300 continue; 301 } 302 } 303 304 target = refs_resolve_refdup(get_main_ref_store(the_repository), 305 name, 306 RESOLVE_REF_READING 307 | RESOLVE_REF_NO_RECURSE 308 | RESOLVE_REF_ALLOW_BAD_NAME, 309 &oid, &ref_flags); 310 if (!target) { 311 if (remote_branch) { 312 error(_("remote-tracking branch '%s' not found"), bname.buf); 313 } else { 314 char *virtual_name = mkpathdup(fmt_remotes, bname.buf); 315 char *virtual_target = refs_resolve_refdup(get_main_ref_store(the_repository), 316 virtual_name, 317 RESOLVE_REF_READING 318 | RESOLVE_REF_NO_RECURSE 319 | RESOLVE_REF_ALLOW_BAD_NAME, 320 &oid, 321 &ref_flags); 322 FREE_AND_NULL(virtual_name); 323 324 if (virtual_target) 325 error(_("branch '%s' not found.\n" 326 "Did you forget --remote?"), 327 bname.buf); 328 else 329 error(_("branch '%s' not found"), bname.buf); 330 FREE_AND_NULL(virtual_target); 331 } 332 ret = 1; 333 continue; 334 } 335 336 if (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) && 337 check_branch_commit(bname.buf, name, &oid, head_rev, kinds, 338 flags)) { 339 if (!(flags & DELETE_BRANCH_SKIP_UNMERGED)) 340 ret = 1; 341 goto next; 342 } 343 344 item = string_list_append(&refs_to_delete, name); 345 item->util = xstrdup((ref_flags & REF_ISBROKEN) ? "broken" 346 : (ref_flags & REF_ISSYMREF) ? target 347 : repo_find_unique_abbrev(the_repository, &oid, DEFAULT_ABBREV)); 348 349 next: 350 free(target); 351 } 352 353 if (!(flags & DELETE_BRANCH_DRY_RUN) && 354 refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF)) 355 ret = 1; 356 357 for_each_string_list_item(item, &refs_to_delete) { 358 char *describe_ref = item->util; 359 char *name = item->string; 360 if (flags & DELETE_BRANCH_DRY_RUN) { 361 if (!(flags & DELETE_BRANCH_QUIET)) 362 printf(remote_branch 363 ? _("Would delete remote-tracking branch %s (was %s).\n") 364 : _("Would delete branch %s (was %s).\n"), 365 name + branch_name_pos, describe_ref); 366 } else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) { 367 char *refname = name + branch_name_pos; 368 if (!(flags & DELETE_BRANCH_QUIET)) 369 printf(remote_branch 370 ? _("Deleted remote-tracking branch %s (was %s).\n") 371 : _("Deleted branch %s (was %s).\n"), 372 name + branch_name_pos, describe_ref); 373 374 delete_branch_config(refname); 375 } 376 free(describe_ref); 377 } 378 string_list_clear(&refs_to_delete, 0); 379 380 free(name); 381 strbuf_release(&bname); 382 383 return ret; 384 } 385 386 static int calc_maxwidth(struct ref_array *refs, int remote_bonus) 387 { 388 int i, max = 0; 389 for (i = 0; i < refs->nr; i++) { 390 struct ref_array_item *it = refs->items[i]; 391 const char *desc = it->refname; 392 int w; 393 394 skip_prefix(it->refname, "refs/heads/", &desc); 395 skip_prefix(it->refname, "refs/remotes/", &desc); 396 if (it->kind == FILTER_REFS_DETACHED_HEAD) { 397 char *head_desc = get_head_description(); 398 w = utf8_strwidth(head_desc); 399 free(head_desc); 400 } else 401 w = utf8_strwidth(desc); 402 403 if (it->kind == FILTER_REFS_REMOTES) 404 w += remote_bonus; 405 if (w > max) 406 max = w; 407 } 408 return max; 409 } 410 411 static const char *quote_literal_for_format(const char *s) 412 { 413 static struct strbuf buf = STRBUF_INIT; 414 415 strbuf_reset(&buf); 416 while (strbuf_expand_step(&buf, &s)) 417 strbuf_addstr(&buf, "%%"); 418 return buf.buf; 419 } 420 421 static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix) 422 { 423 struct strbuf fmt = STRBUF_INIT; 424 struct strbuf local = STRBUF_INIT; 425 struct strbuf remote = STRBUF_INIT; 426 427 strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)%%(if)%%(worktreepath)%%(then)+ %s%%(else) %s%%(end)%%(end)", 428 branch_get_color(BRANCH_COLOR_CURRENT), 429 branch_get_color(BRANCH_COLOR_WORKTREE), 430 branch_get_color(BRANCH_COLOR_LOCAL)); 431 strbuf_addf(&remote, " %s", 432 branch_get_color(BRANCH_COLOR_REMOTE)); 433 434 if (filter->verbose) { 435 struct strbuf obname = STRBUF_INIT; 436 437 if (filter->abbrev < 0) 438 strbuf_addf(&obname, "%%(objectname:short)"); 439 else if (!filter->abbrev) 440 strbuf_addf(&obname, "%%(objectname)"); 441 else 442 strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev); 443 444 strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth); 445 strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET)); 446 strbuf_addf(&local, " %s ", obname.buf); 447 448 if (filter->verbose > 1) 449 { 450 strbuf_addf(&local, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)", 451 branch_get_color(BRANCH_COLOR_WORKTREE), branch_get_color(BRANCH_COLOR_RESET)); 452 strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)" 453 "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)", 454 branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET)); 455 } 456 else 457 strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)"); 458 459 strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s" 460 "%%(if)%%(symref)%%(then) -> %%(symref:short)" 461 "%%(else) %s %%(contents:subject)%%(end)", 462 maxwidth, quote_literal_for_format(remote_prefix), 463 branch_get_color(BRANCH_COLOR_RESET), obname.buf); 464 strbuf_release(&obname); 465 } else { 466 strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", 467 branch_get_color(BRANCH_COLOR_RESET)); 468 strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", 469 quote_literal_for_format(remote_prefix), 470 branch_get_color(BRANCH_COLOR_RESET)); 471 } 472 473 strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf); 474 475 strbuf_release(&local); 476 strbuf_release(&remote); 477 return strbuf_detach(&fmt, NULL); 478 } 479 480 static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, 481 struct ref_format *format, struct string_list *output) 482 { 483 int i; 484 struct ref_array array; 485 int maxwidth = 0; 486 const char *remote_prefix = ""; 487 char *to_free = NULL; 488 489 /* 490 * If we are listing more than just remote branches, 491 * then remote branches will have a "remotes/" prefix. 492 * We need to account for this in the width. 493 */ 494 if (filter->kind != FILTER_REFS_REMOTES) 495 remote_prefix = "remotes/"; 496 497 memset(&array, 0, sizeof(array)); 498 499 filter_refs(&array, filter, filter->kind); 500 501 if (filter->verbose) 502 maxwidth = calc_maxwidth(&array, strlen(remote_prefix)); 503 504 if (!format->format) 505 format->format = to_free = build_format(filter, maxwidth, remote_prefix); 506 format->use_color = branch_use_color; 507 508 if (verify_ref_format(format)) 509 die(_("unable to parse format string")); 510 511 filter_ahead_behind(the_repository, &array); 512 ref_array_sort(sorting, &array); 513 514 if (column_active(colopts)) { 515 struct strbuf out = STRBUF_INIT, err = STRBUF_INIT; 516 517 assert(!filter->verbose && "--column and --verbose are incompatible"); 518 519 for (i = 0; i < array.nr; i++) { 520 strbuf_reset(&err); 521 strbuf_reset(&out); 522 if (format_ref_array_item(array.items[i], format, &out, &err)) 523 die("%s", err.buf); 524 525 /* format to a string_list to let print_columns() do its job */ 526 string_list_append(output, out.buf); 527 } 528 529 strbuf_release(&err); 530 strbuf_release(&out); 531 } else { 532 print_formatted_ref_array(&array, format); 533 } 534 535 ref_array_clear(&array); 536 free(to_free); 537 } 538 539 static void print_current_branch_name(void) 540 { 541 int flags; 542 const char *refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 543 "HEAD", 0, NULL, &flags); 544 const char *shortname; 545 if (!refname) 546 die(_("could not resolve HEAD")); 547 else if (!(flags & REF_ISSYMREF)) 548 return; 549 else if (skip_prefix(refname, "refs/heads/", &shortname)) 550 puts(shortname); 551 else 552 die(_("HEAD (%s) points outside of refs/heads/"), refname); 553 } 554 555 static void reject_rebase_or_bisect_branch(struct worktree **worktrees, 556 const char *target) 557 { 558 int i; 559 560 for (i = 0; worktrees[i]; i++) { 561 struct worktree *wt = worktrees[i]; 562 563 if (!wt->is_detached) 564 continue; 565 566 if (is_worktree_being_rebased(wt, target)) 567 die(_("branch %s is being rebased at %s"), 568 target, wt->path); 569 570 if (is_worktree_being_bisected(wt, target)) 571 die(_("branch %s is being bisected at %s"), 572 target, wt->path); 573 } 574 } 575 576 /* 577 * Update all per-worktree HEADs pointing at the old ref to point the new ref. 578 * This will be used when renaming a branch. Returns 0 if successful, non-zero 579 * otherwise. 580 */ 581 static int replace_each_worktree_head_symref(struct worktree **worktrees, 582 const char *oldref, const char *newref, 583 const char *logmsg) 584 { 585 int ret = 0; 586 int i; 587 588 for (i = 0; worktrees[i]; i++) { 589 struct ref_store *refs; 590 591 if (worktrees[i]->is_detached) 592 continue; 593 if (!worktrees[i]->head_ref) 594 continue; 595 if (strcmp(oldref, worktrees[i]->head_ref)) 596 continue; 597 598 refs = get_worktree_ref_store(worktrees[i]); 599 if (refs_update_symref(refs, "HEAD", newref, logmsg)) 600 ret = error(_("HEAD of working tree %s is not updated"), 601 worktrees[i]->path); 602 } 603 604 return ret; 605 } 606 607 #define IS_HEAD 1 608 #define IS_ORPHAN 2 609 610 static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force) 611 { 612 struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; 613 struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; 614 const char *interpreted_oldname = NULL; 615 const char *interpreted_newname = NULL; 616 int recovery = 0, oldref_usage = 0; 617 struct worktree **worktrees = get_worktrees(the_repository); 618 619 if (check_branch_ref(the_repository, &oldref, oldname)) { 620 /* 621 * Bad name --- this could be an attempt to rename a 622 * ref that we used to allow to be created by accident. 623 */ 624 if (refs_ref_exists(get_main_ref_store(the_repository), oldref.buf)) 625 recovery = 1; 626 else { 627 int code = die_message(_("invalid branch name: '%s'"), oldname); 628 advise_if_enabled(ADVICE_REF_SYNTAX, 629 _("See 'git help check-ref-format'")); 630 exit(code); 631 } 632 } 633 634 for (int i = 0; worktrees[i]; i++) { 635 struct worktree *wt = worktrees[i]; 636 637 if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) { 638 oldref_usage |= IS_HEAD; 639 if (is_null_oid(&wt->head_oid)) 640 oldref_usage |= IS_ORPHAN; 641 break; 642 } 643 } 644 645 if ((copy || !(oldref_usage & IS_HEAD)) && !refs_ref_exists(get_main_ref_store(the_repository), oldref.buf)) { 646 if (oldref_usage & IS_HEAD) 647 die(_("no commit on branch '%s' yet"), oldname); 648 else 649 die(_("no branch named '%s'"), oldname); 650 } 651 652 /* 653 * A command like "git branch -M currentbranch currentbranch" cannot 654 * cause the worktree to become inconsistent with HEAD, so allow it. 655 */ 656 if (!strcmp(oldname, newname)) 657 validate_branchname(newname, &newref); 658 else 659 validate_new_branchname(newname, &newref, force); 660 661 reject_rebase_or_bisect_branch(worktrees, oldref.buf); 662 663 if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) || 664 !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) { 665 BUG("expected prefix missing for refs"); 666 } 667 668 if (copy) 669 strbuf_addf(&logmsg, "Branch: copied %s to %s", 670 oldref.buf, newref.buf); 671 else 672 strbuf_addf(&logmsg, "Branch: renamed %s to %s", 673 oldref.buf, newref.buf); 674 675 if (!copy && !(oldref_usage & IS_ORPHAN) && 676 refs_rename_ref(get_main_ref_store(the_repository), oldref.buf, newref.buf, logmsg.buf)) 677 die(_("branch rename failed")); 678 if (copy && refs_copy_existing_ref(get_main_ref_store(the_repository), oldref.buf, newref.buf, logmsg.buf)) 679 die(_("branch copy failed")); 680 681 if (recovery) { 682 if (copy) 683 warning(_("created a copy of a misnamed branch '%s'"), 684 interpreted_oldname); 685 else 686 warning(_("renamed a misnamed branch '%s' away"), 687 interpreted_oldname); 688 } 689 690 if (!copy && (oldref_usage & IS_HEAD) && 691 replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf, 692 logmsg.buf)) 693 die(_("branch renamed to %s, but HEAD is not updated"), newname); 694 695 strbuf_release(&logmsg); 696 697 strbuf_addf(&oldsection, "branch.%s", interpreted_oldname); 698 strbuf_addf(&newsection, "branch.%s", interpreted_newname); 699 if (!copy && repo_config_rename_section(the_repository, oldsection.buf, newsection.buf) < 0) 700 die(_("branch is renamed, but update of config-file failed")); 701 if (copy && strcmp(interpreted_oldname, interpreted_newname) && 702 repo_config_copy_section(the_repository, oldsection.buf, newsection.buf) < 0) 703 die(_("branch is copied, but update of config-file failed")); 704 strbuf_release(&oldref); 705 strbuf_release(&newref); 706 strbuf_release(&oldsection); 707 strbuf_release(&newsection); 708 free_worktrees(worktrees); 709 } 710 711 static int parse_opt_forked(const struct option *opt, const char *arg, int unset) 712 { 713 struct ref_filter *filter = opt->value; 714 715 BUG_ON_OPT_NEG(unset); 716 if (ref_filter_forked_add(filter, arg) < 0) 717 die(_("'%s' is not a valid branch or pattern"), arg); 718 return 0; 719 } 720 721 struct stacked_branch_data { 722 struct strset *deletable_branch_names; 723 struct strset *protected_branch_names; 724 }; 725 726 static int collect_stacked_branch_base(const struct reference *ref, 727 void *cb_data) 728 { 729 struct stacked_branch_data *data = cb_data; 730 const char *branch_name; 731 struct branch *branch; 732 const char *upstream_refname; 733 const char *upstream_branch_name; 734 735 if (!skip_prefix(ref->name, "refs/heads/", &branch_name)) 736 BUG("expected local branch ref, got '%s'", ref->name); 737 if (strset_contains(data->deletable_branch_names, branch_name)) 738 return 0; 739 740 branch = branch_get(branch_name); 741 upstream_refname = branch_get_upstream(branch, NULL); 742 if (!upstream_refname || 743 !skip_prefix(upstream_refname, "refs/heads/", 744 &upstream_branch_name) || 745 !strset_contains(data->deletable_branch_names, 746 upstream_branch_name)) 747 return 0; 748 749 strset_add(data->protected_branch_names, upstream_branch_name); 750 return 0; 751 } 752 753 static void protect_stacked_branch_bases(struct ref_store *refs, 754 struct strset *deletable_branch_names, 755 struct strset *protected_branch_names) 756 { 757 struct stacked_branch_data data = { 758 .deletable_branch_names = deletable_branch_names, 759 .protected_branch_names = protected_branch_names, 760 }; 761 struct refs_for_each_ref_options opts = { 762 .prefix = "refs/heads/", 763 }; 764 struct hashmap_iter iter; 765 struct strmap_entry *entry; 766 767 refs_for_each_ref_ext(refs, collect_stacked_branch_base, &data, &opts); 768 769 strset_for_each_entry(protected_branch_names, &iter, entry) 770 strset_remove(deletable_branch_names, entry->key); 771 } 772 773 static void clear_deleted_upstreams(struct strset *protected_branch_names, 774 struct strset *deletable_branch_names) 775 { 776 struct strbuf key = STRBUF_INIT; 777 struct hashmap_iter iter; 778 struct strmap_entry *entry; 779 780 strset_for_each_entry(protected_branch_names, &iter, entry) { 781 struct branch *branch = branch_get(entry->key); 782 const char *upstream_refname = branch_get_upstream(branch, NULL); 783 const char *upstream_branch_name; 784 785 if (!upstream_refname || 786 !skip_prefix(upstream_refname, "refs/heads/", 787 &upstream_branch_name) || 788 !strset_contains(deletable_branch_names, 789 upstream_branch_name)) 790 continue; 791 792 strbuf_addf(&key, "branch.%s.merge", branch->name); 793 repo_config_set_gently(the_repository, key.buf, NULL); 794 strbuf_reset(&key); 795 strbuf_addf(&key, "branch.%s.remote", branch->name); 796 repo_config_set_gently(the_repository, key.buf, NULL); 797 strbuf_reset(&key); 798 } 799 800 strbuf_release(&key); 801 } 802 803 static int branch_pushes_to_upstream(struct branch *branch, 804 const char *upstream) 805 { 806 struct remote *remote = remote_get(remote_for_branch(branch, NULL)); 807 char *push_refname = NULL; 808 char *tracking = NULL; 809 int ret = 0; 810 811 if (!remote) 812 return 0; 813 if (remote->push.nr) 814 push_refname = apply_refspecs(&remote->push, branch->refname); 815 else 816 push_refname = xstrdup(branch->refname); 817 if (push_refname) 818 tracking = apply_refspecs(&remote->fetch, push_refname); 819 if (tracking && !strcmp(tracking, upstream)) 820 ret = 1; 821 822 free(push_refname); 823 free(tracking); 824 return ret; 825 } 826 827 static int delete_merged_branches(const struct strvec *upstreams, 828 const char **argv, unsigned int flags) 829 { 830 struct ref_store *refs = get_main_ref_store(the_repository); 831 struct ref_filter filter = REF_FILTER_INIT; 832 struct ref_array candidates = { 0 }; 833 struct strset deletable_branch_names = STRSET_INIT; 834 struct strset protected_branch_names = STRSET_INIT; 835 struct strvec branches_to_delete = STRVEC_INIT; 836 struct strbuf key = STRBUF_INIT; 837 struct hashmap_iter iter; 838 struct strmap_entry *entry; 839 int ret = 0; 840 841 for (size_t i = 0; i < upstreams->nr; i++) 842 if (ref_filter_forked_add(&filter, upstreams->v[i]) < 0) 843 die(_("'%s' is not a valid branch or pattern"), 844 upstreams->v[i]); 845 846 filter.kind = FILTER_REFS_BRANCHES; 847 filter.name_patterns = argv; 848 filter_refs(&candidates, &filter, filter.kind); 849 850 for (int i = 0; i < candidates.nr; i++) { 851 const char *branch_refname = candidates.items[i]->refname; 852 const char *branch_name; 853 struct branch *branch; 854 const char *upstream_refname; 855 int opt_out; 856 857 if (!skip_prefix(branch_refname, "refs/heads/", &branch_name)) 858 BUG("filter returned non-branch ref '%s'", branch_refname); 859 if (branch_checked_out(branch_refname)) 860 continue; 861 862 branch = branch_get(branch_name); 863 upstream_refname = branch_get_upstream(branch, NULL); 864 if (!upstream_refname || !refs_ref_exists(refs, upstream_refname)) 865 continue; 866 if (branch_pushes_to_upstream(branch, upstream_refname)) 867 continue; 868 if (check_branch_commit(branch_name, branch_name, 869 &candidates.items[i]->objectname, NULL, 870 FILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED)) 871 continue; 872 873 strbuf_reset(&key); 874 strbuf_addf(&key, "branch.%s.deletemerged", branch_name); 875 if (!repo_config_get_bool(the_repository, key.buf, &opt_out) && 876 !opt_out) { 877 if (!(flags & DELETE_BRANCH_QUIET)) 878 fprintf(stderr, 879 _("Skipping '%s' (branch.%s.deleteMerged is false)\n"), 880 branch_name, branch_name); 881 continue; 882 } 883 884 strset_add(&deletable_branch_names, branch_name); 885 } 886 887 protect_stacked_branch_bases(refs, &deletable_branch_names, 888 &protected_branch_names); 889 890 strset_for_each_entry(&deletable_branch_names, &iter, entry) 891 strvec_push(&branches_to_delete, entry->key); 892 893 if (branches_to_delete.nr) 894 ret = delete_branches(branches_to_delete.nr, branches_to_delete.v, 895 FILTER_REFS_BRANCHES, 896 DELETE_BRANCH_SKIP_UNMERGED | 897 DELETE_BRANCH_NO_HEAD_FALLBACK | 898 flags); 899 900 if (!ret && !(flags & DELETE_BRANCH_DRY_RUN)) 901 clear_deleted_upstreams(&protected_branch_names, 902 &deletable_branch_names); 903 904 strbuf_release(&key); 905 strvec_clear(&branches_to_delete); 906 strset_clear(&protected_branch_names); 907 strset_clear(&deletable_branch_names); 908 ref_array_clear(&candidates); 909 ref_filter_clear(&filter); 910 return ret; 911 } 912 913 static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION") 914 915 static int edit_branch_description(const char *branch_name) 916 { 917 int exists; 918 struct strbuf buf = STRBUF_INIT; 919 struct strbuf name = STRBUF_INIT; 920 921 exists = !read_branch_desc(&buf, branch_name); 922 if (!buf.len || buf.buf[buf.len-1] != '\n') 923 strbuf_addch(&buf, '\n'); 924 strbuf_commented_addf(&buf, comment_line_str, 925 _("Please edit the description for the branch\n" 926 " %s\n" 927 "Lines starting with '%s' will be stripped.\n"), 928 branch_name, comment_line_str); 929 write_file_buf(edit_description(), buf.buf, buf.len); 930 strbuf_reset(&buf); 931 if (launch_editor(edit_description(), &buf, NULL)) { 932 strbuf_release(&buf); 933 return -1; 934 } 935 strbuf_stripspace(&buf, comment_line_str); 936 937 strbuf_addf(&name, "branch.%s.description", branch_name); 938 if (buf.len || exists) 939 repo_config_set(the_repository, name.buf, buf.len ? buf.buf : NULL); 940 strbuf_release(&name); 941 strbuf_release(&buf); 942 943 return 0; 944 } 945 946 static void die_if_upstream_looks_like_remote(const char *new_upstream, const char *branch_name) 947 { 948 struct strbuf remote_ref = STRBUF_INIT; 949 int code; 950 951 if (strchr(new_upstream, '/') || 952 !remote_is_configured(remote_get(new_upstream), 0)) 953 return; 954 955 strbuf_addf(&remote_ref, "refs/remotes/%s/%s", new_upstream, branch_name); 956 if (!refs_ref_exists(get_main_ref_store(the_repository), remote_ref.buf)) { 957 strbuf_release(&remote_ref); 958 return; 959 } 960 961 code = die_message(_("--set-upstream-to takes a single / argument")); 962 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE, 963 _("Did you mean to use: git branch --set-upstream-to=%s/%s?"), 964 new_upstream, branch_name); 965 strbuf_release(&remote_ref); 966 exit(code); 967 } 968 969 int cmd_branch(int argc, 970 const char **argv, 971 const char *prefix, 972 struct repository *repo UNUSED) 973 { 974 /* possible actions */ 975 int delete = 0, rename = 0, copy = 0, list = 0, 976 unset_upstream = 0, show_current = 0, edit_description = 0; 977 struct strvec delete_merged = STRVEC_INIT; 978 int dry_run = 0; 979 const char *new_upstream = NULL; 980 int noncreate_actions = 0; 981 /* possible options */ 982 int reflog = 0, quiet = 0, icase = 0, force = 0, 983 recurse_submodules_explicit = 0; 984 enum branch_track track; 985 struct ref_filter filter = REF_FILTER_INIT; 986 static struct ref_sorting *sorting; 987 struct string_list sorting_options = STRING_LIST_INIT_DUP; 988 struct ref_format format = REF_FORMAT_INIT; 989 struct repo_config_values *cfg = repo_config_values(the_repository); 990 int ret; 991 992 struct option options[] = { 993 OPT_GROUP(N_("Generic options")), 994 OPT__VERBOSE(&filter.verbose, 995 N_("show hash and subject, give twice for upstream branch")), 996 OPT__QUIET(&quiet, N_("suppress informational messages")), 997 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)", 998 N_("set branch tracking configuration"), 999 PARSE_OPT_OPTARG, 1000 parse_opt_tracking_mode), 1001 OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"), 1002 BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN), 1003 OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")), 1004 OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("unset the upstream info")), 1005 OPT__COLOR(&branch_use_color, N_("use colored output")), 1006 OPT_SET_INT_F('r', "remotes", &filter.kind, N_("act on remote-tracking branches"), 1007 FILTER_REFS_REMOTES, 1008 PARSE_OPT_NONEG), 1009 OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")), 1010 OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")), 1011 OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")), 1012 OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")), 1013 OPT__ABBREV(&filter.abbrev), 1014 1015 OPT_GROUP(N_("Specific git-branch actions:")), 1016 OPT_SET_INT_F('a', "all", &filter.kind, N_("list both remote-tracking and local branches"), 1017 FILTER_REFS_REMOTES | FILTER_REFS_BRANCHES, 1018 PARSE_OPT_NONEG), 1019 OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1), 1020 OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2), 1021 OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1), 1022 OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2), 1023 OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty, 1024 N_("do not output a newline after empty formatted refs")), 1025 OPT_BIT('c', "copy", ©, N_("copy a branch and its reflog"), 1), 1026 OPT_BIT('C', NULL, ©, N_("copy a branch, even if target exists"), 2), 1027 OPT_BOOL('l', "list", &list, N_("list branch names")), 1028 OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")), 1029 OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")), 1030 OPT_BOOL(0, "edit-description", &edit_description, 1031 N_("edit the description for the branch")), 1032 OPT_CALLBACK_F(0, "delete-merged", &delete_merged, N_("pattern"), 1033 N_("delete merged branches whose upstream matches (repeatable)"), 1034 PARSE_OPT_NONEG, parse_opt_strvec), 1035 OPT_BOOL(0, "dry-run", &dry_run, 1036 N_("with --delete-merged, only print which branches would be deleted")), 1037 OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE), 1038 OPT_MERGED(&filter, N_("print only branches that are merged")), 1039 OPT_NO_MERGED(&filter, N_("print only branches that are not merged")), 1040 OPT_CALLBACK_F(0, "forked", &filter, N_("branch"), 1041 N_("print only branches whose upstream matches (repeatable)"), 1042 PARSE_OPT_NONEG, parse_opt_forked), 1043 OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")), 1044 OPT_REF_SORT(&sorting_options), 1045 OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"), 1046 N_("print only branches of the object"), parse_opt_object_name), 1047 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), 1048 OPT_BOOL(0, "recurse-submodules", &recurse_submodules_explicit, N_("recurse through submodules")), 1049 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), 1050 OPT_END(), 1051 }; 1052 1053 setup_ref_filter_porcelain_msg(); 1054 1055 filter.kind = FILTER_REFS_BRANCHES; 1056 filter.abbrev = -1; 1057 1058 show_usage_with_options_if_asked(argc, argv, 1059 builtin_branch_usage, options); 1060 1061 /* 1062 * Try to set sort keys from config. If config does not set any, 1063 * fall back on default (refname) sorting. 1064 */ 1065 repo_config(the_repository, git_branch_config, &sorting_options); 1066 if (!sorting_options.nr) 1067 string_list_append(&sorting_options, "refname"); 1068 1069 track = cfg->branch_track; 1070 1071 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD", 1072 0, &head_oid, NULL); 1073 if (!head) 1074 die(_("failed to resolve HEAD as a valid ref")); 1075 if (!strcmp(head, "HEAD")) 1076 filter.detached = 1; 1077 else if (!skip_prefix(head, "refs/heads/", &head)) 1078 die(_("HEAD not found below refs/heads!")); 1079 1080 argc = parse_options(argc, argv, prefix, options, builtin_branch_usage, 1081 0); 1082 1083 if (!delete && !rename && !copy && !edit_description && !new_upstream && 1084 !show_current && !unset_upstream && !delete_merged.nr && 1085 argc == 0) 1086 list = 1; 1087 1088 if (filter.with_commit || filter.no_commit || 1089 filter.reachable_from || filter.unreachable_from || 1090 filter.points_at.nr || filter.forked.nr) 1091 list = 1; 1092 1093 noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream + 1094 !!show_current + !!list + !!edit_description + 1095 !!unset_upstream + !!delete_merged.nr; 1096 if (noncreate_actions > 1) 1097 usage_with_options(builtin_branch_usage, options); 1098 1099 if (dry_run && !delete_merged.nr) 1100 die(_("--dry-run requires --delete-merged")); 1101 1102 if (recurse_submodules_explicit) { 1103 if (!submodule_propagate_branches) 1104 die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled")); 1105 if (noncreate_actions) 1106 die(_("--recurse-submodules can only be used to create branches")); 1107 } 1108 1109 recurse_submodules = 1110 (recurse_submodules || recurse_submodules_explicit) && 1111 submodule_propagate_branches; 1112 1113 if (filter.abbrev == -1) 1114 filter.abbrev = DEFAULT_ABBREV; 1115 filter.ignore_case = icase; 1116 1117 finalize_colopts(&colopts, -1); 1118 if (filter.verbose) { 1119 if (explicitly_enable_column(colopts)) 1120 die(_("options '%s' and '%s' cannot be used together"), "--column", "--verbose"); 1121 colopts = 0; 1122 } 1123 1124 if (force) { 1125 delete *= 2; 1126 rename *= 2; 1127 copy *= 2; 1128 } 1129 1130 if (list) 1131 setup_auto_pager("branch", 1); 1132 1133 if (delete) { 1134 if (!argc) 1135 die(_("branch name required")); 1136 ret = delete_branches(argc, argv, filter.kind, 1137 (delete > 1 ? DELETE_BRANCH_FORCE : 0) | 1138 (quiet ? DELETE_BRANCH_QUIET : 0)); 1139 goto out; 1140 } else if (delete_merged.nr) { 1141 ret = delete_merged_branches(&delete_merged, argv, 1142 (quiet ? DELETE_BRANCH_QUIET : 0) | 1143 (dry_run ? DELETE_BRANCH_DRY_RUN : 0)); 1144 goto out; 1145 } else if (show_current) { 1146 print_current_branch_name(); 1147 ret = 0; 1148 goto out; 1149 } else if (list) { 1150 /* git branch --list also shows HEAD when it is detached */ 1151 if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached) 1152 filter.kind |= FILTER_REFS_DETACHED_HEAD; 1153 filter.name_patterns = argv; 1154 /* 1155 * If no sorting parameter is given then we default to sorting 1156 * by 'refname'. This would give us an alphabetically sorted 1157 * array with the 'HEAD' ref at the beginning followed by 1158 * local branches 'refs/heads/...' and finally remote-tracking 1159 * branches 'refs/remotes/...'. 1160 */ 1161 sorting = ref_sorting_options(&sorting_options); 1162 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); 1163 ref_sorting_set_sort_flags_all( 1164 sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1); 1165 print_ref_list(&filter, sorting, &format, &output); 1166 print_columns(&output, colopts, NULL); 1167 string_list_clear(&output, 0); 1168 ref_sorting_release(sorting); 1169 ref_filter_clear(&filter); 1170 1171 ret = 0; 1172 goto out; 1173 } else if (edit_description) { 1174 const char *branch_name; 1175 struct strbuf branch_ref = STRBUF_INIT; 1176 struct strbuf buf = STRBUF_INIT; 1177 1178 if (!argc) { 1179 if (filter.detached) 1180 die(_("cannot give description to detached HEAD")); 1181 branch_name = head; 1182 } else if (argc == 1) { 1183 copy_branchname(the_repository, &buf, argv[0], 1184 INTERPRET_BRANCH_LOCAL); 1185 branch_name = buf.buf; 1186 } else { 1187 die(_("cannot edit description of more than one branch")); 1188 } 1189 1190 strbuf_addf(&branch_ref, "refs/heads/%s", branch_name); 1191 if (!refs_ref_exists(get_main_ref_store(the_repository), branch_ref.buf)) { 1192 error((!argc || branch_checked_out(branch_ref.buf)) 1193 ? _("no commit on branch '%s' yet") 1194 : _("no branch named '%s'"), 1195 branch_name); 1196 ret = 1; 1197 } else if (!edit_branch_description(branch_name)) { 1198 ret = 0; /* happy */ 1199 } else { 1200 ret = 1; 1201 } 1202 1203 strbuf_release(&branch_ref); 1204 strbuf_release(&buf); 1205 1206 goto out; 1207 } else if (copy || rename) { 1208 if (!argc) 1209 die(_("branch name required")); 1210 else if ((argc == 1) && filter.detached) 1211 die(copy? _("cannot copy the current branch while not on any") 1212 : _("cannot rename the current branch while not on any")); 1213 else if (argc == 1) 1214 copy_or_rename_branch(head, argv[0], copy, copy + rename > 1); 1215 else if (argc == 2) 1216 copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1); 1217 else 1218 die(copy? _("too many branches for a copy operation") 1219 : _("too many arguments for a rename operation")); 1220 } else if (new_upstream) { 1221 struct branch *branch; 1222 struct strbuf buf = STRBUF_INIT; 1223 1224 if (!argc) 1225 branch = branch_get(NULL); 1226 else if (argc == 1) { 1227 copy_branchname(the_repository, &buf, argv[0], 1228 INTERPRET_BRANCH_LOCAL); 1229 branch = branch_get(buf.buf); 1230 } else 1231 die(_("too many arguments to set new upstream")); 1232 1233 if (!branch) { 1234 if (!argc || !strcmp(argv[0], "HEAD")) 1235 die(_("could not set upstream of HEAD to %s when " 1236 "it does not point to any branch"), 1237 new_upstream); 1238 die(_("no such branch '%s'"), argv[0]); 1239 } 1240 1241 if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) { 1242 if (!argc || branch_checked_out(branch->refname)) 1243 die(_("no commit on branch '%s' yet"), branch->name); 1244 /* 1245 * Check the advice up front to avoid the ref 1246 * lookups when the hint is off. The helper still 1247 * calls advise_if_enabled() so the hint carries the 1248 * standard "disable this message" instructions. 1249 */ 1250 if (argc == 1 && 1251 advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) 1252 die_if_upstream_looks_like_remote(new_upstream, argv[0]); 1253 die(_("branch '%s' does not exist"), branch->name); 1254 } 1255 1256 if (dwim_and_setup_tracking(the_repository, branch->name, 1257 new_upstream, BRANCH_TRACK_OVERRIDE, 1258 quiet)) { 1259 strbuf_release(&buf); 1260 ret = 1; 1261 goto out; 1262 } 1263 strbuf_release(&buf); 1264 } else if (unset_upstream) { 1265 struct branch *branch; 1266 struct strbuf buf = STRBUF_INIT; 1267 1268 if (!argc) 1269 branch = branch_get(NULL); 1270 else if (argc == 1) { 1271 copy_branchname(the_repository, &buf, argv[0], 1272 INTERPRET_BRANCH_LOCAL); 1273 branch = branch_get(buf.buf); 1274 } else 1275 die(_("too many arguments to unset upstream")); 1276 1277 if (!branch) { 1278 if (!argc || !strcmp(argv[0], "HEAD")) 1279 die(_("could not unset upstream of HEAD when " 1280 "it does not point to any branch")); 1281 die(_("no such branch '%s'"), argv[0]); 1282 } 1283 1284 if (!branch_has_merge_config(branch)) 1285 die(_("branch '%s' has no upstream information"), branch->name); 1286 1287 strbuf_reset(&buf); 1288 strbuf_addf(&buf, "branch.%s.remote", branch->name); 1289 repo_config_set_multivar(the_repository, buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); 1290 strbuf_reset(&buf); 1291 strbuf_addf(&buf, "branch.%s.merge", branch->name); 1292 repo_config_set_multivar(the_repository, buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); 1293 strbuf_release(&buf); 1294 } else if (!noncreate_actions && argc > 0 && argc <= 2) { 1295 const char *branch_name = argv[0]; 1296 const char *start_name = argc == 2 ? argv[1] : head; 1297 1298 if (filter.kind != FILTER_REFS_BRANCHES) 1299 die(_("the -a, and -r, options to 'git branch' do not take a branch name.\n" 1300 "Did you mean to use: -a|-r --list ?")); 1301 1302 if (track == BRANCH_TRACK_OVERRIDE) 1303 die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead")); 1304 1305 if (recurse_submodules) { 1306 create_branches_recursively(the_repository, branch_name, 1307 start_name, NULL, force, 1308 reflog, quiet, track, 0); 1309 ret = 0; 1310 goto out; 1311 } 1312 if (create_branch(the_repository, branch_name, start_name, 1313 force, 0, reflog, quiet, track, 0)) { 1314 ret = 1; 1315 goto out; 1316 } 1317 } else 1318 usage_with_options(builtin_branch_usage, options); 1319 1320 ret = 0; 1321 1322 out: 1323 strvec_clear(&delete_merged); 1324 string_list_clear(&sorting_options, 0); 1325 return ret; 1326 }