]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/branch.c
reftable: signal overflow
[thirdparty/git.git] / builtin / branch.c
CommitLineData
c31820c2
LH
1/*
2 * Builtin "git branch"
3 *
d972cce0 4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
c31820c2
LH
5 * Based on git-branch.sh by Junio C Hamano.
6 */
7
8#include "cache.h"
b2141fc1 9#include "config.h"
85023577 10#include "color.h"
c31820c2
LH
11#include "refs.h"
12#include "commit.h"
13#include "builtin.h"
6f084a56 14#include "remote.h"
a8dfd5ea 15#include "parse-options.h"
e496c003 16#include "branch.h"
68067ca1
JH
17#include "diff.h"
18#include "revision.h"
ebe31ef2
NTND
19#include "string-list.h"
20#include "column.h"
1452bd64 21#include "utf8.h"
c8183cd2 22#include "wt-status.h"
1511b22d 23#include "ref-filter.h"
f292244c 24#include "worktree.h"
3ac68a93 25#include "help.h"
64043556 26#include "commit-reach.h"
a8dfd5ea
PH
27
28static const char * const builtin_branch_usage[] = {
21bf9339 29 N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"),
9c9b4f2f
AH
30 N_("git branch [<options>] [-l] [-f] <branch-name> [<start-point>]"),
31 N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
32 N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
52d59cc6 33 N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"),
aa3bc55e 34 N_("git branch [<options>] [-r | -a] [--points-at]"),
3d9e4ce3 35 N_("git branch [<options>] [-r | -a] [--format]"),
a8dfd5ea
PH
36 NULL
37};
c31820c2 38
c31820c2 39static const char *head;
0c77cd24 40static struct object_id head_oid;
c31820c2 41
6b2f2d98 42static int branch_use_color = -1;
a1158cae 43static char branch_colors[][COLOR_MAXLEN] = {
dc6ebd4c 44 GIT_COLOR_RESET,
949af068
KN
45 GIT_COLOR_NORMAL, /* PLAIN */
46 GIT_COLOR_RED, /* REMOTE */
47 GIT_COLOR_NORMAL, /* LOCAL */
48 GIT_COLOR_GREEN, /* CURRENT */
49 GIT_COLOR_BLUE, /* UPSTREAM */
ab313814 50 GIT_COLOR_CYAN, /* WORKTREE */
a1158cae
AP
51};
52enum color_branch {
74bb2dfb
AL
53 BRANCH_COLOR_RESET = 0,
54 BRANCH_COLOR_PLAIN = 1,
55 BRANCH_COLOR_REMOTE = 2,
56 BRANCH_COLOR_LOCAL = 3,
dbda21fa 57 BRANCH_COLOR_CURRENT = 4,
ab313814
NB
58 BRANCH_COLOR_UPSTREAM = 5,
59 BRANCH_COLOR_WORKTREE = 6
a1158cae
AP
60};
61
a73b3680
NTND
62static const char *color_branch_slots[] = {
63 [BRANCH_COLOR_RESET] = "reset",
64 [BRANCH_COLOR_PLAIN] = "plain",
65 [BRANCH_COLOR_REMOTE] = "remote",
66 [BRANCH_COLOR_LOCAL] = "local",
67 [BRANCH_COLOR_CURRENT] = "current",
68 [BRANCH_COLOR_UPSTREAM] = "upstream",
ab313814 69 [BRANCH_COLOR_WORKTREE] = "worktree",
a73b3680
NTND
70};
71
ebe31ef2
NTND
72static struct string_list output = STRING_LIST_INIT_DUP;
73static unsigned int colopts;
74
3ac68a93 75define_list_config_array(color_branch_slots);
a1158cae 76
ef90d6d4 77static int git_branch_config(const char *var, const char *value, void *cb)
a1158cae 78{
e3f1da98 79 const char *slot_name;
560ae1c1
SM
80
81 if (!strcmp(var, "branch.sort")) {
82 if (!value)
83 return config_error_nonbool(var);
98e7ab6d 84 string_list_append(cb, value);
560ae1c1
SM
85 return 0;
86 }
e3f1da98 87
59556548 88 if (starts_with(var, "column."))
ebe31ef2 89 return git_column_config(var, value, "branch", &colopts);
a1158cae 90 if (!strcmp(var, "color.branch")) {
e269eb79 91 branch_use_color = git_config_colorbool(var, value);
a1158cae
AP
92 return 0;
93 }
e3f1da98 94 if (skip_prefix(var, "color.branch.", &slot_name)) {
a73b3680 95 int slot = LOOKUP_CONFIG(color_branch_slots, slot_name);
8b8e8624
JK
96 if (slot < 0)
97 return 0;
5768c98a
JH
98 if (!value)
99 return config_error_nonbool(var);
f6c5a296 100 return color_parse(value, branch_colors[slot]);
a1158cae 101 }
33c643bb 102 return git_color_default_config(var, value, cb);
a1158cae
AP
103}
104
52fae7de 105static const char *branch_get_color(enum color_branch ix)
a1158cae 106{
daa0c3d9 107 if (want_color(branch_use_color))
a1158cae
AP
108 return branch_colors[ix];
109 return "";
110}
111
99c419c9
JH
112static int branch_merged(int kind, const char *name,
113 struct commit *rev, struct commit *head_rev)
114{
115 /*
116 * This checks whether the merge bases of branch and HEAD (or
117 * the other branch this branch builds upon) contains the
118 * branch, which means that the branch has already been merged
119 * safely to HEAD (or the other branch).
120 */
121 struct commit *reference_rev = NULL;
122 const char *reference_name = NULL;
96ec7b1e 123 void *reference_name_to_free = NULL;
99c419c9
JH
124 int merged;
125
1511b22d 126 if (kind == FILTER_REFS_BRANCHES) {
99c419c9 127 struct branch *branch = branch_get(name);
3a429d0a 128 const char *upstream = branch_get_upstream(branch, NULL);
0c77cd24 129 struct object_id oid;
99c419c9 130
a9f9f8cc 131 if (upstream &&
96ec7b1e 132 (reference_name = reference_name_to_free =
a9f9f8cc 133 resolve_refdup(upstream, RESOLVE_REF_READING,
0f2dc722 134 &oid, NULL)) != NULL)
2122f675
SB
135 reference_rev = lookup_commit_reference(the_repository,
136 &oid);
99c419c9
JH
137 }
138 if (!reference_rev)
139 reference_rev = head_rev;
140
a20efee9 141 merged = in_merge_bases(rev, reference_rev);
99c419c9
JH
142
143 /*
144 * After the safety valve is fully redefined to "check with
145 * upstream, if any, otherwise with HEAD", we should just
146 * return the result of the in_merge_bases() above without
147 * any of the following code, but during the transition period,
148 * a gentle reminder is in order.
149 */
150 if ((head_rev != reference_rev) &&
a20efee9 151 in_merge_bases(rev, head_rev) != merged) {
99c419c9 152 if (merged)
49df4b02 153 warning(_("deleting branch '%s' that has been merged to\n"
6c80cd29 154 " '%s', but not yet merged to HEAD."),
99c419c9
JH
155 name, reference_name);
156 else
49df4b02
ÆAB
157 warning(_("not deleting branch '%s' that is not yet merged to\n"
158 " '%s', even though it is merged to HEAD."),
99c419c9
JH
159 name, reference_name);
160 }
96ec7b1e 161 free(reference_name_to_free);
99c419c9
JH
162 return merged;
163}
164
f5d0e162 165static int check_branch_commit(const char *branchname, const char *refname,
0c77cd24 166 const struct object_id *oid, struct commit *head_rev,
f5d0e162
RS
167 int kinds, int force)
168{
2122f675 169 struct commit *rev = lookup_commit_reference(the_repository, oid);
597a9774 170 if (!force && !rev) {
f5d0e162
RS
171 error(_("Couldn't look up commit object for '%s'"), refname);
172 return -1;
173 }
174 if (!force && !branch_merged(kinds, branchname, rev, head_rev)) {
175 error(_("The branch '%s' is not fully merged.\n"
176 "If you are sure you want to delete it, "
177 "run 'git branch -D %s'."), branchname, branchname);
178 return -1;
179 }
180 return 0;
181}
182
22ed7927
RS
183static void delete_branch_config(const char *branchname)
184{
185 struct strbuf buf = STRBUF_INIT;
186 strbuf_addf(&buf, "branch.%s", branchname);
187 if (git_config_rename_section(buf.buf, NULL) < 0)
188 warning(_("Update of config-file failed"));
189 strbuf_release(&buf);
190}
191
d65ddf19
JK
192static int delete_branches(int argc, const char **argv, int force, int kinds,
193 int quiet)
c31820c2 194{
c8dd491f 195 struct worktree **worktrees;
f5d0e162 196 struct commit *head_rev = NULL;
0c77cd24 197 struct object_id oid;
b8e9a00d 198 char *name = NULL;
c179837c 199 const char *fmt;
c31820c2 200 int i;
b8e9a00d 201 int ret = 0;
c179837c 202 int remote_branch = 0;
8415d5c7 203 struct strbuf bname = STRBUF_INIT;
6b145e01 204 unsigned allowed_interpret;
81989077
PH
205 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
206 struct string_list_item *item;
207 int branch_name_pos;
c31820c2 208
f3d985c3 209 switch (kinds) {
1511b22d 210 case FILTER_REFS_REMOTES:
f3d985c3 211 fmt = "refs/remotes/%s";
c179837c
ÆAB
212 /* For subsequent UI messages */
213 remote_branch = 1;
6b145e01 214 allowed_interpret = INTERPRET_BRANCH_REMOTE;
c179837c 215
f3d985c3
JH
216 force = 1;
217 break;
1511b22d 218 case FILTER_REFS_BRANCHES:
f3d985c3 219 fmt = "refs/heads/%s";
6b145e01 220 allowed_interpret = INTERPRET_BRANCH_LOCAL;
f3d985c3
JH
221 break;
222 default:
49df4b02 223 die(_("cannot use -a with -d"));
f3d985c3 224 }
81989077 225 branch_name_pos = strcspn(fmt, "%");
c31820c2 226
67affd51 227 if (!force) {
2122f675 228 head_rev = lookup_commit_reference(the_repository, &head_oid);
67affd51 229 if (!head_rev)
49df4b02 230 die(_("Couldn't look up commit object for HEAD"));
67affd51 231 }
c8dd491f
AK
232
233 worktrees = get_worktrees();
234
a9155c50 235 for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
8bb04553 236 char *target = NULL;
0fe700e3
RS
237 int flags = 0;
238
6b145e01 239 strbuf_branchname(&bname, argv[i], allowed_interpret);
8e0f7003 240 free(name);
4e2d094d 241 name = mkpathdup(fmt, bname.buf);
f292244c
KY
242
243 if (kinds == FILTER_REFS_BRANCHES) {
d3b9ac07 244 const struct worktree *wt =
c8dd491f 245 find_shared_symref(worktrees, "HEAD", name);
d3b9ac07 246 if (wt) {
f292244c
KY
247 error(_("Cannot delete branch '%s' "
248 "checked out at '%s'"),
d3b9ac07 249 bname.buf, wt->path);
f292244c
KY
250 ret = 1;
251 continue;
252 }
253 }
254
8bb04553
MH
255 target = resolve_refdup(name,
256 RESOLVE_REF_READING
257 | RESOLVE_REF_NO_RECURSE
258 | RESOLVE_REF_ALLOW_BAD_NAME,
0f2dc722 259 &oid, &flags);
18f29fc6 260 if (!target) {
c179837c 261 error(remote_branch
ccd593cf 262 ? _("remote-tracking branch '%s' not found.")
c179837c 263 : _("branch '%s' not found."), bname.buf);
b8e9a00d
QT
264 ret = 1;
265 continue;
266 }
c31820c2 267
d0f810f0 268 if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
0c77cd24 269 check_branch_commit(bname.buf, name, &oid, head_rev, kinds,
f5d0e162 270 force)) {
b8e9a00d 271 ret = 1;
8bb04553 272 goto next;
c31820c2
LH
273 }
274
81989077
PH
275 item = string_list_append(&refs_to_delete, name);
276 item->util = xstrdup((flags & REF_ISBROKEN) ? "broken"
277 : (flags & REF_ISSYMREF) ? target
278 : find_unique_abbrev(&oid, DEFAULT_ABBREV));
8bb04553
MH
279
280 next:
281 free(target);
c31820c2 282 }
c31820c2 283
81989077
PH
284 if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF))
285 ret = 1;
286
287 for_each_string_list_item(item, &refs_to_delete) {
288 char *describe_ref = item->util;
289 char *name = item->string;
290 if (!ref_exists(name)) {
291 char *refname = name + branch_name_pos;
292 if (!quiet)
293 printf(remote_branch
294 ? _("Deleted remote-tracking branch %s (was %s).\n")
295 : _("Deleted branch %s (was %s).\n"),
296 name + branch_name_pos, describe_ref);
297
298 delete_branch_config(refname);
299 }
300 free(describe_ref);
301 }
302 string_list_clear(&refs_to_delete, 0);
303
8e0f7003 304 free(name);
a9155c50 305 strbuf_release(&bname);
c8dd491f 306 free_worktrees(worktrees);
b8e9a00d 307
a9155c50 308 return ret;
c31820c2 309}
c31820c2 310
949af068 311static int calc_maxwidth(struct ref_array *refs, int remote_bonus)
94fcb730 312{
949af068
KN
313 int i, max = 0;
314 for (i = 0; i < refs->nr; i++) {
315 struct ref_array_item *it = refs->items[i];
316 const char *desc = it->refname;
317 int w;
dbda21fa 318
949af068
KN
319 skip_prefix(it->refname, "refs/heads/", &desc);
320 skip_prefix(it->refname, "refs/remotes/", &desc);
321 if (it->kind == FILTER_REFS_DETACHED_HEAD) {
322 char *head_desc = get_head_description();
323 w = utf8_strwidth(head_desc);
324 free(head_desc);
325 } else
326 w = utf8_strwidth(desc);
21dfc09d 327
949af068
KN
328 if (it->kind == FILTER_REFS_REMOTES)
329 w += remote_bonus;
330 if (w > max)
331 max = w;
21dfc09d 332 }
949af068 333 return max;
94fcb730
JH
334}
335
949af068 336static const char *quote_literal_for_format(const char *s)
6e0332ec 337{
949af068
KN
338 static struct strbuf buf = STRBUF_INIT;
339
340 strbuf_reset(&buf);
341 while (*s) {
342 const char *ep = strchrnul(s, '%');
343 if (s < ep)
344 strbuf_add(&buf, s, ep - s);
345 if (*ep == '%') {
346 strbuf_addstr(&buf, "%%");
347 s = ep + 1;
348 } else {
349 s = ep;
350 }
6e0332ec 351 }
949af068 352 return buf.buf;
6e0332ec
JN
353}
354
949af068 355static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix)
75e6e213 356{
949af068
KN
357 struct strbuf fmt = STRBUF_INIT;
358 struct strbuf local = STRBUF_INIT;
359 struct strbuf remote = STRBUF_INIT;
75e6e213 360
ab313814
NB
361 strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)%%(if)%%(worktreepath)%%(then)+ %s%%(else) %s%%(end)%%(end)",
362 branch_get_color(BRANCH_COLOR_CURRENT),
363 branch_get_color(BRANCH_COLOR_WORKTREE),
364 branch_get_color(BRANCH_COLOR_LOCAL));
7ca260ab
JK
365 strbuf_addf(&remote, " %s",
366 branch_get_color(BRANCH_COLOR_REMOTE));
75e6e213 367
1511b22d 368 if (filter->verbose) {
ac5bbc02
JH
369 struct strbuf obname = STRBUF_INIT;
370
371 if (filter->abbrev < 0)
372 strbuf_addf(&obname, "%%(objectname:short)");
373 else if (!filter->abbrev)
374 strbuf_addf(&obname, "%%(objectname)");
375 else
376 strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev);
377
949af068 378 strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
72d4a9a7 379 strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET));
ac5bbc02 380 strbuf_addf(&local, " %s ", obname.buf);
949af068
KN
381
382 if (filter->verbose > 1)
6e938146
NB
383 {
384 strbuf_addf(&local, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)",
385 branch_get_color(BRANCH_COLOR_WORKTREE), branch_get_color(BRANCH_COLOR_RESET));
949af068
KN
386 strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
387 "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
388 branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
6e938146 389 }
949af068
KN
390 else
391 strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
392
7ca260ab 393 strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
ac5bbc02
JH
394 "%%(if)%%(symref)%%(then) -> %%(symref:short)"
395 "%%(else) %s %%(contents:subject)%%(end)",
7ca260ab 396 maxwidth, quote_literal_for_format(remote_prefix),
ac5bbc02
JH
397 branch_get_color(BRANCH_COLOR_RESET), obname.buf);
398 strbuf_release(&obname);
ebe31ef2 399 } else {
949af068
KN
400 strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
401 branch_get_color(BRANCH_COLOR_RESET));
7ca260ab
JK
402 strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
403 quote_literal_for_format(remote_prefix),
949af068 404 branch_get_color(BRANCH_COLOR_RESET));
ebe31ef2 405 }
75e6e213 406
949af068 407 strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf);
aedcb7dc 408
949af068
KN
409 strbuf_release(&local);
410 strbuf_release(&remote);
411 return strbuf_detach(&fmt, NULL);
7e9ff00b
LT
412}
413
d72d4f92
ÆAB
414static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting,
415 struct ref_format *format, struct string_list *output)
c31820c2
LH
416{
417 int i;
1511b22d 418 struct ref_array array;
844c3f0b
ZH
419 struct strbuf out = STRBUF_INIT;
420 struct strbuf err = STRBUF_INIT;
1051e40d
KN
421 int maxwidth = 0;
422 const char *remote_prefix = "";
3d9e4ce3 423 char *to_free = NULL;
1051e40d 424
35257aa0 425 /*
1051e40d
KN
426 * If we are listing more than just remote branches,
427 * then remote branches will have a "remotes/" prefix.
428 * We need to account for this in the width.
35257aa0 429 */
1511b22d 430 if (filter->kind != FILTER_REFS_REMOTES)
1051e40d 431 remote_prefix = "remotes/";
8376a704 432
1511b22d 433 memset(&array, 0, sizeof(array));
8376a704 434
1763334c 435 filter_refs(&array, filter, filter->kind);
c31820c2 436
1511b22d
KN
437 if (filter->verbose)
438 maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
bfcc9214 439
4a68e36d
JK
440 if (!format->format)
441 format->format = to_free = build_format(filter, maxwidth, remote_prefix);
11b087ad 442 format->use_color = branch_use_color;
2eda0102
JK
443
444 if (verify_ref_format(format))
445 die(_("unable to parse format string"));
949af068 446
aedcb7dc 447 ref_array_sort(sorting, &array);
1603ade8 448
949af068 449 for (i = 0; i < array.nr; i++) {
844c3f0b
ZH
450 strbuf_reset(&err);
451 strbuf_reset(&out);
3019eca9
OT
452 if (format_ref_array_item(array.items[i], format, &out, &err))
453 die("%s", err.buf);
949af068
KN
454 if (column_active(colopts)) {
455 assert(!filter->verbose && "--column and --verbose are incompatible");
456 /* format to a string_list to let print_columns() do its job */
d72d4f92 457 string_list_append(output, out.buf);
949af068
KN
458 } else {
459 fwrite(out.buf, 1, out.len, stdout);
460 putchar('\n');
461 }
949af068 462 }
0e9716e6 463
844c3f0b
ZH
464 strbuf_release(&err);
465 strbuf_release(&out);
1511b22d 466 ref_array_clear(&array);
3d9e4ce3 467 free(to_free);
c31820c2
LH
468}
469
0ecb1fc7
DU
470static void print_current_branch_name(void)
471{
472 int flags;
473 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
474 const char *shortname;
475 if (!refname)
476 die(_("could not resolve HEAD"));
477 else if (!(flags & REF_ISSYMREF))
478 return;
479 else if (skip_prefix(refname, "refs/heads/", &shortname))
480 puts(shortname);
481 else
482 die(_("HEAD (%s) points outside of refs/heads/"), refname);
483}
484
14ace5b7
NTND
485static void reject_rebase_or_bisect_branch(const char *target)
486{
03f2465b 487 struct worktree **worktrees = get_worktrees();
14ace5b7
NTND
488 int i;
489
490 for (i = 0; worktrees[i]; i++) {
491 struct worktree *wt = worktrees[i];
492
493 if (!wt->is_detached)
494 continue;
495
496 if (is_worktree_being_rebased(wt, target))
497 die(_("Branch %s is being rebased at %s"),
498 target, wt->path);
499
500 if (is_worktree_being_bisected(wt, target))
501 die(_("Branch %s is being bisected at %s"),
502 target, wt->path);
503 }
504
505 free_worktrees(worktrees);
506}
507
52d59cc6 508static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
c976d415 509{
da3f78a2 510 struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
da3f78a2 511 struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
255073ca
KS
512 const char *interpreted_oldname = NULL;
513 const char *interpreted_newname = NULL;
cbdffe40 514 int recovery = 0;
c976d415 515
52d59cc6
SD
516 if (!oldname) {
517 if (copy)
518 die(_("cannot copy the current branch while not on any."));
519 else
520 die(_("cannot rename the current branch while not on any."));
521 }
c847f537 522
cbdffe40
JH
523 if (strbuf_check_branch_ref(&oldref, oldname)) {
524 /*
525 * Bad name --- this could be an attempt to rename a
526 * ref that we used to allow to be created by accident.
527 */
c6893323 528 if (ref_exists(oldref.buf))
cbdffe40
JH
529 recovery = 1;
530 else
49df4b02 531 die(_("Invalid branch name: '%s'"), oldname);
cbdffe40 532 }
c976d415 533
3f59481e
JN
534 /*
535 * A command like "git branch -M currentbranch currentbranch" cannot
536 * cause the worktree to become inconsistent with HEAD, so allow it.
537 */
bc1c9c0e
JH
538 if (!strcmp(oldname, newname))
539 validate_branchname(newname, &newref);
540 else
541 validate_new_branchname(newname, &newref, force);
c976d415 542
14ace5b7
NTND
543 reject_rebase_or_bisect_branch(oldref.buf);
544
255073ca
KS
545 if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
546 !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
033abf97 547 BUG("expected prefix missing for refs");
255073ca
KS
548 }
549
52d59cc6
SD
550 if (copy)
551 strbuf_addf(&logmsg, "Branch: copied %s to %s",
552 oldref.buf, newref.buf);
553 else
554 strbuf_addf(&logmsg, "Branch: renamed %s to %s",
555 oldref.buf, newref.buf);
678d0f4c 556
cfaff3aa
JS
557 if (!copy &&
558 (!head || strcmp(oldname, head) || !is_null_oid(&head_oid)) &&
559 rename_ref(oldref.buf, newref.buf, logmsg.buf))
49df4b02 560 die(_("Branch rename failed"));
52d59cc6
SD
561 if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
562 die(_("Branch copy failed"));
c976d415 563
52d59cc6
SD
564 if (recovery) {
565 if (copy)
a48ebe97 566 warning(_("Created a copy of a misnamed branch '%s'"),
255073ca 567 interpreted_oldname);
52d59cc6
SD
568 else
569 warning(_("Renamed a misnamed branch '%s' away"),
255073ca 570 interpreted_oldname);
52d59cc6 571 }
cbdffe40 572
e5435ff1
JH
573 if (!copy &&
574 replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf))
49df4b02 575 die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
19eba151 576
39ee4c6c
KM
577 strbuf_release(&logmsg);
578
255073ca 579 strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
da3f78a2 580 strbuf_release(&oldref);
255073ca 581 strbuf_addf(&newsection, "branch.%s", interpreted_newname);
da3f78a2 582 strbuf_release(&newref);
52d59cc6 583 if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
49df4b02 584 die(_("Branch is renamed, but update of config-file failed"));
52d59cc6
SD
585 if (copy && strcmp(oldname, newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0)
586 die(_("Branch is copied, but update of config-file failed"));
da3f78a2
MV
587 strbuf_release(&oldsection);
588 strbuf_release(&newsection);
c976d415
LH
589}
590
c10388c7 591static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
b7200e83
JH
592
593static int edit_branch_description(const char *branch_name)
594{
b7200e83
JH
595 struct strbuf buf = STRBUF_INIT;
596 struct strbuf name = STRBUF_INIT;
597
598 read_branch_desc(&buf, branch_name);
599 if (!buf.len || buf.buf[buf.len-1] != '\n')
600 strbuf_addch(&buf, '\n');
eff80a9f 601 strbuf_commented_addf(&buf,
b18237f4
VA
602 _("Please edit the description for the branch\n"
603 " %s\n"
604 "Lines starting with '%c' will be stripped.\n"),
eff80a9f 605 branch_name, comment_line_char);
c10388c7 606 write_file_buf(edit_description(), buf.buf, buf.len);
b7200e83 607 strbuf_reset(&buf);
c10388c7 608 if (launch_editor(edit_description(), &buf, NULL)) {
b7200e83
JH
609 strbuf_release(&buf);
610 return -1;
611 }
63af4a84 612 strbuf_stripspace(&buf, 1);
b7200e83
JH
613
614 strbuf_addf(&name, "branch.%s.description", branch_name);
3d180648 615 git_config_set(name.buf, buf.len ? buf.buf : NULL);
b7200e83
JH
616 strbuf_release(&name);
617 strbuf_release(&buf);
618
bd25f890 619 return 0;
b7200e83
JH
620}
621
c31820c2
LH
622int cmd_branch(int argc, const char **argv, const char *prefix)
623{
52d59cc6 624 int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
0ecb1fc7 625 int show_current = 0;
b7200e83 626 int reflog = 0, edit_description = 0;
b84869ef 627 int quiet = 0, unset_upstream = 0;
6183d826 628 const char *new_upstream = NULL;
9ed36cfa 629 enum branch_track track;
1511b22d 630 struct ref_filter filter;
3bb16a8b 631 int icase = 0;
98e7ab6d
JH
632 static struct ref_sorting *sorting;
633 struct string_list sorting_options = STRING_LIST_INIT_DUP;
4a68e36d 634 struct ref_format format = REF_FORMAT_INIT;
a8dfd5ea
PH
635
636 struct option options[] = {
24a85214 637 OPT_GROUP(N_("Generic options")),
1511b22d 638 OPT__VERBOSE(&filter.verbose,
24a85214
NTND
639 N_("show hash and subject, give twice for upstream branch")),
640 OPT__QUIET(&quiet, N_("suppress informational messages")),
641 OPT_SET_INT('t', "track", &track, N_("set up tracking mode (see git-pull(1))"),
9ed36cfa 642 BRANCH_TRACK_EXPLICIT),
3e4a67b4
NTND
643 OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
644 BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
ab86885a 645 OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
11de8dd7 646 OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("unset the upstream info")),
24a85214 647 OPT__COLOR(&branch_use_color, N_("use colored output")),
1511b22d
KN
648 OPT_SET_INT('r', "remotes", &filter.kind, N_("act on remote-tracking branches"),
649 FILTER_REFS_REMOTES),
650 OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")),
ac3f5a34 651 OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")),
1511b22d 652 OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")),
ac3f5a34 653 OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")),
1511b22d 654 OPT__ABBREV(&filter.abbrev),
a8dfd5ea 655
24a85214 656 OPT_GROUP(N_("Specific git-branch actions:")),
1511b22d
KN
657 OPT_SET_INT('a', "all", &filter.kind, N_("list both remote-tracking and local branches"),
658 FILTER_REFS_REMOTES | FILTER_REFS_BRANCHES),
24a85214
NTND
659 OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1),
660 OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
661 OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
662 OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
52d59cc6
SD
663 OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
664 OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
a15d5981 665 OPT_BOOL('l', "list", &list, N_("list branch names")),
0ecb1fc7 666 OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")),
055930bc 667 OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
d5d09d47
SB
668 OPT_BOOL(0, "edit-description", &edit_description,
669 N_("edit the description for the branch")),
c01b56a3 670 OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
1511b22d
KN
671 OPT_MERGED(&filter, N_("print only branches that are merged")),
672 OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
24a85214 673 OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
98e7ab6d 674 OPT_REF_SORT(&sorting_options),
203c8533
DL
675 OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"),
676 N_("print only branches of the object"), parse_opt_object_name),
3bb16a8b 677 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
4a68e36d 678 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
a8dfd5ea
PH
679 OPT_END(),
680 };
c31820c2 681
56b43607
KN
682 setup_ref_filter_porcelain_msg();
683
1511b22d
KN
684 memset(&filter, 0, sizeof(filter));
685 filter.kind = FILTER_REFS_BRANCHES;
686 filter.abbrev = -1;
687
1dacfbcf
NTND
688 if (argc == 2 && !strcmp(argv[1], "-h"))
689 usage_with_options(builtin_branch_usage, options);
690
98e7ab6d 691 git_config(git_branch_config, &sorting_options);
6b2f2d98 692
9ed36cfa 693 track = git_branch_track;
c976d415 694
0f2dc722 695 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
c31820c2 696 if (!head)
49df4b02 697 die(_("Failed to resolve HEAD as a valid ref."));
e3f1da98 698 if (!strcmp(head, "HEAD"))
1511b22d 699 filter.detached = 1;
e3f1da98
RS
700 else if (!skip_prefix(head, "refs/heads/", &head))
701 die(_("HEAD not found below refs/heads!"));
ebe31ef2 702
37782920
SB
703 argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
704 0);
cddd127b 705
0ecb1fc7
DU
706 if (!delete && !rename && !copy && !edit_description && !new_upstream &&
707 !show_current && !unset_upstream && argc == 0)
cddd127b
MG
708 list = 1;
709
21bf9339
AL
710 if (filter.with_commit || filter.no_commit ||
711 filter.reachable_from || filter.unreachable_from || filter.points_at.nr)
d0403508
JK
712 list = 1;
713
0ecb1fc7 714 if (!!delete + !!rename + !!copy + !!new_upstream + !!show_current +
dc446399 715 list + edit_description + unset_upstream > 1)
049716b3 716 usage_with_options(builtin_branch_usage, options);
c31820c2 717
1511b22d
KN
718 if (filter.abbrev == -1)
719 filter.abbrev = DEFAULT_ABBREV;
3bb16a8b
NTND
720 filter.ignore_case = icase;
721
ebe31ef2 722 finalize_colopts(&colopts, -1);
1511b22d 723 if (filter.verbose) {
ebe31ef2
NTND
724 if (explicitly_enable_column(colopts))
725 die(_("--column and --verbose are incompatible"));
726 colopts = 0;
727 }
b792c067 728
356e91f2
MG
729 if (force) {
730 delete *= 2;
731 rename *= 2;
52d59cc6 732 copy *= 2;
356e91f2
MG
733 }
734
d74b541e 735 if (list)
0ae19de7 736 setup_auto_pager("branch", 1);
d74b541e 737
640d0401
NTND
738 if (delete) {
739 if (!argc)
740 die(_("branch name required"));
1511b22d 741 return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
0ecb1fc7
DU
742 } else if (show_current) {
743 print_current_branch_name();
744 return 0;
640d0401 745 } else if (list) {
ffdd02a5 746 /* git branch --list also shows HEAD when it is detached */
1511b22d
KN
747 if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
748 filter.kind |= FILTER_REFS_DETACHED_HEAD;
749 filter.name_patterns = argv;
3bb16a8b
NTND
750 /*
751 * If no sorting parameter is given then we default to sorting
752 * by 'refname'. This would give us an alphabetically sorted
753 * array with the 'HEAD' ref at the beginning followed by
30aa96cd 754 * local branches 'refs/heads/...' and finally remote-tracking
3bb16a8b
NTND
755 * branches 'refs/remotes/...'.
756 */
98e7ab6d 757 sorting = ref_sorting_options(&sorting_options);
7c269a7b 758 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
2708ce62
ÆAB
759 ref_sorting_set_sort_flags_all(
760 sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
d72d4f92 761 print_ref_list(&filter, sorting, &format, &output);
ebe31ef2
NTND
762 print_columns(&output, colopts, NULL);
763 string_list_clear(&output, 0);
d72d4f92 764 ref_sorting_release(sorting);
ca417990 765 return 0;
2e3c894f 766 } else if (edit_description) {
b7200e83 767 const char *branch_name;
c2d17ba3
JH
768 struct strbuf branch_ref = STRBUF_INIT;
769
75135b23 770 if (!argc) {
1511b22d 771 if (filter.detached)
045e3884 772 die(_("Cannot give description to detached HEAD"));
b7200e83 773 branch_name = head;
75135b23 774 } else if (argc == 1)
b7200e83
JH
775 branch_name = argv[0];
776 else
43722c4d 777 die(_("cannot edit description of more than one branch"));
c2d17ba3
JH
778
779 strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
780 if (!ref_exists(branch_ref.buf)) {
781 strbuf_release(&branch_ref);
782
783 if (!argc)
045e3884 784 return error(_("No commit on branch '%s' yet."),
c2d17ba3
JH
785 branch_name);
786 else
045e3884
NTND
787 return error(_("No branch named '%s'."),
788 branch_name);
c2d17ba3
JH
789 }
790 strbuf_release(&branch_ref);
791
b7200e83
JH
792 if (edit_branch_description(branch_name))
793 return 1;
52d59cc6
SD
794 } else if (copy) {
795 if (!argc)
796 die(_("branch name required"));
797 else if (argc == 1)
798 copy_or_rename_branch(head, argv[0], 1, copy > 1);
799 else if (argc == 2)
800 copy_or_rename_branch(argv[0], argv[1], 1, copy > 1);
801 else
802 die(_("too many branches for a copy operation"));
a4043aea 803 } else if (rename) {
d1520c4b
JM
804 if (!argc)
805 die(_("branch name required"));
806 else if (argc == 1)
52d59cc6 807 copy_or_rename_branch(head, argv[0], 0, rename > 1);
3706ed29 808 else if (argc == 2)
52d59cc6 809 copy_or_rename_branch(argv[0], argv[1], 0, rename > 1);
3706ed29 810 else
f7776235 811 die(_("too many arguments for a rename operation"));
6183d826
CMN
812 } else if (new_upstream) {
813 struct branch *branch = branch_get(argv[0]);
814
8efb8899 815 if (argc > 1)
f7776235 816 die(_("too many arguments to set new upstream"));
8efb8899
NTND
817
818 if (!branch) {
819 if (!argc || !strcmp(argv[0], "HEAD"))
820 die(_("could not set upstream of HEAD to %s when "
821 "it does not point to any branch."),
822 new_upstream);
823 die(_("no such branch '%s'"), argv[0]);
824 }
825
6183d826
CMN
826 if (!ref_exists(branch->refname))
827 die(_("branch '%s' does not exist"), branch->name);
828
829 /*
830 * create_branch takes care of setting up the tracking
831 * info and making sure new_upstream is correct
832 */
4edce172
NTND
833 create_branch(the_repository, branch->name, new_upstream,
834 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
b84869ef
CMN
835 } else if (unset_upstream) {
836 struct branch *branch = branch_get(argv[0]);
837 struct strbuf buf = STRBUF_INIT;
838
8efb8899 839 if (argc > 1)
f7776235 840 die(_("too many arguments to unset upstream"));
8efb8899
NTND
841
842 if (!branch) {
843 if (!argc || !strcmp(argv[0], "HEAD"))
844 die(_("could not unset upstream of HEAD when "
845 "it does not point to any branch."));
846 die(_("no such branch '%s'"), argv[0]);
847 }
848
54d07f2e 849 if (!branch_has_merge_config(branch))
b84869ef 850 die(_("Branch '%s' has no upstream information"), branch->name);
b84869ef
CMN
851
852 strbuf_addf(&buf, "branch.%s.remote", branch->name);
504ee129 853 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
b84869ef
CMN
854 strbuf_reset(&buf);
855 strbuf_addf(&buf, "branch.%s.merge", branch->name);
504ee129 856 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
b84869ef 857 strbuf_release(&buf);
5cd75c7d 858 } else if (argc > 0 && argc <= 2) {
1511b22d 859 if (filter.kind != FILTER_REFS_BRANCHES)
1fde99cf
PO
860 die(_("The -a, and -r, options to 'git branch' do not take a branch name.\n"
861 "Did you mean to use: -a|-r --list <pattern>?"));
b347d06b
CMN
862
863 if (track == BRANCH_TRACK_OVERRIDE)
52668846 864 die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
b347d06b 865
4edce172
NTND
866 create_branch(the_repository,
867 argv[0], (argc == 2) ? argv[1] : head,
e2bbd0cc 868 force, 0, reflog, quiet, track);
b347d06b 869
6e8f993a 870 } else
a8dfd5ea 871 usage_with_options(builtin_branch_usage, options);
c31820c2
LH
872
873 return 0;
874}