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