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