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