4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-branch.sh by Junio C Hamano.
12 #include "environment.h"
17 #include "object-name.h"
19 #include "parse-options.h"
23 #include "string-list.h"
26 #include "wt-status.h"
27 #include "ref-filter.h"
30 #include "commit-reach.h"
33 static const char * const builtin_branch_usage
[] = {
34 N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"),
35 N_("git branch [<options>] [-f] [--recurse-submodules] <branch-name> [<start-point>]"),
36 N_("git branch [<options>] [-l] [<pattern>...]"),
37 N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
38 N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
39 N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"),
40 N_("git branch [<options>] [-r | -a] [--points-at]"),
41 N_("git branch [<options>] [-r | -a] [--format]"),
45 static const char *head
;
46 static struct object_id head_oid
;
47 static int recurse_submodules
= 0;
48 static int submodule_propagate_branches
= 0;
49 static int omit_empty
= 0;
51 static int branch_use_color
= -1;
52 static char branch_colors
[][COLOR_MAXLEN
] = {
54 GIT_COLOR_NORMAL
, /* PLAIN */
55 GIT_COLOR_RED
, /* REMOTE */
56 GIT_COLOR_NORMAL
, /* LOCAL */
57 GIT_COLOR_GREEN
, /* CURRENT */
58 GIT_COLOR_BLUE
, /* UPSTREAM */
59 GIT_COLOR_CYAN
, /* WORKTREE */
62 BRANCH_COLOR_RESET
= 0,
63 BRANCH_COLOR_PLAIN
= 1,
64 BRANCH_COLOR_REMOTE
= 2,
65 BRANCH_COLOR_LOCAL
= 3,
66 BRANCH_COLOR_CURRENT
= 4,
67 BRANCH_COLOR_UPSTREAM
= 5,
68 BRANCH_COLOR_WORKTREE
= 6
71 static const char *color_branch_slots
[] = {
72 [BRANCH_COLOR_RESET
] = "reset",
73 [BRANCH_COLOR_PLAIN
] = "plain",
74 [BRANCH_COLOR_REMOTE
] = "remote",
75 [BRANCH_COLOR_LOCAL
] = "local",
76 [BRANCH_COLOR_CURRENT
] = "current",
77 [BRANCH_COLOR_UPSTREAM
] = "upstream",
78 [BRANCH_COLOR_WORKTREE
] = "worktree",
81 static struct string_list output
= STRING_LIST_INIT_DUP
;
82 static unsigned int colopts
;
84 define_list_config_array(color_branch_slots
);
86 static int git_branch_config(const char *var
, const char *value
, void *cb
)
88 const char *slot_name
;
90 if (!strcmp(var
, "branch.sort")) {
92 return config_error_nonbool(var
);
93 string_list_append(cb
, value
);
97 if (starts_with(var
, "column."))
98 return git_column_config(var
, value
, "branch", &colopts
);
99 if (!strcmp(var
, "color.branch")) {
100 branch_use_color
= git_config_colorbool(var
, value
);
103 if (skip_prefix(var
, "color.branch.", &slot_name
)) {
104 int slot
= LOOKUP_CONFIG(color_branch_slots
, slot_name
);
108 return config_error_nonbool(var
);
109 return color_parse(value
, branch_colors
[slot
]);
111 if (!strcmp(var
, "submodule.recurse")) {
112 recurse_submodules
= git_config_bool(var
, value
);
115 if (!strcasecmp(var
, "submodule.propagateBranches")) {
116 submodule_propagate_branches
= git_config_bool(var
, value
);
120 if (git_color_config(var
, value
, cb
) < 0)
123 return git_default_config(var
, value
, cb
);
126 static const char *branch_get_color(enum color_branch ix
)
128 if (want_color(branch_use_color
))
129 return branch_colors
[ix
];
133 static int branch_merged(int kind
, const char *name
,
134 struct commit
*rev
, struct commit
*head_rev
)
137 * This checks whether the merge bases of branch and HEAD (or
138 * the other branch this branch builds upon) contains the
139 * branch, which means that the branch has already been merged
140 * safely to HEAD (or the other branch).
142 struct commit
*reference_rev
= NULL
;
143 const char *reference_name
= NULL
;
144 void *reference_name_to_free
= NULL
;
147 if (kind
== FILTER_REFS_BRANCHES
) {
148 struct branch
*branch
= branch_get(name
);
149 const char *upstream
= branch_get_upstream(branch
, NULL
);
150 struct object_id oid
;
153 (reference_name
= reference_name_to_free
=
154 resolve_refdup(upstream
, RESOLVE_REF_READING
,
155 &oid
, NULL
)) != NULL
)
156 reference_rev
= lookup_commit_reference(the_repository
,
160 reference_rev
= head_rev
;
162 merged
= reference_rev
? repo_in_merge_bases(the_repository
, rev
,
166 * After the safety valve is fully redefined to "check with
167 * upstream, if any, otherwise with HEAD", we should just
168 * return the result of the repo_in_merge_bases() above without
169 * any of the following code, but during the transition period,
170 * a gentle reminder is in order.
172 if ((head_rev
!= reference_rev
) &&
173 (head_rev
? repo_in_merge_bases(the_repository
, rev
, head_rev
) : 0) != merged
) {
175 warning(_("deleting branch '%s' that has been merged to\n"
176 " '%s', but not yet merged to HEAD."),
177 name
, reference_name
);
179 warning(_("not deleting branch '%s' that is not yet merged to\n"
180 " '%s', even though it is merged to HEAD."),
181 name
, reference_name
);
183 free(reference_name_to_free
);
187 static int check_branch_commit(const char *branchname
, const char *refname
,
188 const struct object_id
*oid
, struct commit
*head_rev
,
189 int kinds
, int force
)
191 struct commit
*rev
= lookup_commit_reference(the_repository
, oid
);
192 if (!force
&& !rev
) {
193 error(_("Couldn't look up commit object for '%s'"), refname
);
196 if (!force
&& !branch_merged(kinds
, branchname
, rev
, head_rev
)) {
197 error(_("The branch '%s' is not fully merged.\n"
198 "If you are sure you want to delete it, "
199 "run 'git branch -D %s'."), branchname
, branchname
);
205 static void delete_branch_config(const char *branchname
)
207 struct strbuf buf
= STRBUF_INIT
;
208 strbuf_addf(&buf
, "branch.%s", branchname
);
209 if (git_config_rename_section(buf
.buf
, NULL
) < 0)
210 warning(_("Update of config-file failed"));
211 strbuf_release(&buf
);
214 static int delete_branches(int argc
, const char **argv
, int force
, int kinds
,
217 struct commit
*head_rev
= NULL
;
218 struct object_id oid
;
223 int remote_branch
= 0;
224 struct strbuf bname
= STRBUF_INIT
;
225 unsigned allowed_interpret
;
226 struct string_list refs_to_delete
= STRING_LIST_INIT_DUP
;
227 struct string_list_item
*item
;
229 const char *fmt_remotes
= "refs/remotes/%s";
232 case FILTER_REFS_REMOTES
:
234 /* For subsequent UI messages */
236 allowed_interpret
= INTERPRET_BRANCH_REMOTE
;
240 case FILTER_REFS_BRANCHES
:
241 fmt
= "refs/heads/%s";
242 allowed_interpret
= INTERPRET_BRANCH_LOCAL
;
245 die(_("cannot use -a with -d"));
247 branch_name_pos
= strcspn(fmt
, "%");
250 head_rev
= lookup_commit_reference(the_repository
, &head_oid
);
252 for (i
= 0; i
< argc
; i
++, strbuf_reset(&bname
)) {
256 strbuf_branchname(&bname
, argv
[i
], allowed_interpret
);
258 name
= mkpathdup(fmt
, bname
.buf
);
260 if (kinds
== FILTER_REFS_BRANCHES
) {
262 if ((path
= branch_checked_out(name
))) {
263 error(_("Cannot delete branch '%s' "
264 "checked out at '%s'"),
271 target
= resolve_refdup(name
,
273 | RESOLVE_REF_NO_RECURSE
274 | RESOLVE_REF_ALLOW_BAD_NAME
,
278 error(_("remote-tracking branch '%s' not found."), bname
.buf
);
280 char *virtual_name
= mkpathdup(fmt_remotes
, bname
.buf
);
281 char *virtual_target
= resolve_refdup(virtual_name
,
283 | RESOLVE_REF_NO_RECURSE
284 | RESOLVE_REF_ALLOW_BAD_NAME
,
286 FREE_AND_NULL(virtual_name
);
289 error(_("branch '%s' not found.\n"
290 "Did you forget --remote?"),
293 error(_("branch '%s' not found."), bname
.buf
);
294 FREE_AND_NULL(virtual_target
);
300 if (!(flags
& (REF_ISSYMREF
|REF_ISBROKEN
)) &&
301 check_branch_commit(bname
.buf
, name
, &oid
, head_rev
, kinds
,
307 item
= string_list_append(&refs_to_delete
, name
);
308 item
->util
= xstrdup((flags
& REF_ISBROKEN
) ? "broken"
309 : (flags
& REF_ISSYMREF
) ? target
310 : repo_find_unique_abbrev(the_repository
, &oid
, DEFAULT_ABBREV
));
316 if (delete_refs(NULL
, &refs_to_delete
, REF_NO_DEREF
))
319 for_each_string_list_item(item
, &refs_to_delete
) {
320 char *describe_ref
= item
->util
;
321 char *name
= item
->string
;
322 if (!ref_exists(name
)) {
323 char *refname
= name
+ branch_name_pos
;
326 ? _("Deleted remote-tracking branch %s (was %s).\n")
327 : _("Deleted branch %s (was %s).\n"),
328 name
+ branch_name_pos
, describe_ref
);
330 delete_branch_config(refname
);
334 string_list_clear(&refs_to_delete
, 0);
337 strbuf_release(&bname
);
342 static int calc_maxwidth(struct ref_array
*refs
, int remote_bonus
)
345 for (i
= 0; i
< refs
->nr
; i
++) {
346 struct ref_array_item
*it
= refs
->items
[i
];
347 const char *desc
= it
->refname
;
350 skip_prefix(it
->refname
, "refs/heads/", &desc
);
351 skip_prefix(it
->refname
, "refs/remotes/", &desc
);
352 if (it
->kind
== FILTER_REFS_DETACHED_HEAD
) {
353 char *head_desc
= get_head_description();
354 w
= utf8_strwidth(head_desc
);
357 w
= utf8_strwidth(desc
);
359 if (it
->kind
== FILTER_REFS_REMOTES
)
367 static const char *quote_literal_for_format(const char *s
)
369 static struct strbuf buf
= STRBUF_INIT
;
373 const char *ep
= strchrnul(s
, '%');
375 strbuf_add(&buf
, s
, ep
- s
);
377 strbuf_addstr(&buf
, "%%");
386 static char *build_format(struct ref_filter
*filter
, int maxwidth
, const char *remote_prefix
)
388 struct strbuf fmt
= STRBUF_INIT
;
389 struct strbuf local
= STRBUF_INIT
;
390 struct strbuf remote
= STRBUF_INIT
;
392 strbuf_addf(&local
, "%%(if)%%(HEAD)%%(then)* %s%%(else)%%(if)%%(worktreepath)%%(then)+ %s%%(else) %s%%(end)%%(end)",
393 branch_get_color(BRANCH_COLOR_CURRENT
),
394 branch_get_color(BRANCH_COLOR_WORKTREE
),
395 branch_get_color(BRANCH_COLOR_LOCAL
));
396 strbuf_addf(&remote
, " %s",
397 branch_get_color(BRANCH_COLOR_REMOTE
));
399 if (filter
->verbose
) {
400 struct strbuf obname
= STRBUF_INIT
;
402 if (filter
->abbrev
< 0)
403 strbuf_addf(&obname
, "%%(objectname:short)");
404 else if (!filter
->abbrev
)
405 strbuf_addf(&obname
, "%%(objectname)");
407 strbuf_addf(&obname
, "%%(objectname:short=%d)", filter
->abbrev
);
409 strbuf_addf(&local
, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth
);
410 strbuf_addstr(&local
, branch_get_color(BRANCH_COLOR_RESET
));
411 strbuf_addf(&local
, " %s ", obname
.buf
);
413 if (filter
->verbose
> 1)
415 strbuf_addf(&local
, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)",
416 branch_get_color(BRANCH_COLOR_WORKTREE
), branch_get_color(BRANCH_COLOR_RESET
));
417 strbuf_addf(&local
, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
418 "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
419 branch_get_color(BRANCH_COLOR_UPSTREAM
), branch_get_color(BRANCH_COLOR_RESET
));
422 strbuf_addf(&local
, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
424 strbuf_addf(&remote
, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
425 "%%(if)%%(symref)%%(then) -> %%(symref:short)"
426 "%%(else) %s %%(contents:subject)%%(end)",
427 maxwidth
, quote_literal_for_format(remote_prefix
),
428 branch_get_color(BRANCH_COLOR_RESET
), obname
.buf
);
429 strbuf_release(&obname
);
431 strbuf_addf(&local
, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
432 branch_get_color(BRANCH_COLOR_RESET
));
433 strbuf_addf(&remote
, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
434 quote_literal_for_format(remote_prefix
),
435 branch_get_color(BRANCH_COLOR_RESET
));
438 strbuf_addf(&fmt
, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local
.buf
, remote
.buf
);
440 strbuf_release(&local
);
441 strbuf_release(&remote
);
442 return strbuf_detach(&fmt
, NULL
);
445 static void print_ref_list(struct ref_filter
*filter
, struct ref_sorting
*sorting
,
446 struct ref_format
*format
, struct string_list
*output
)
449 struct ref_array array
;
450 struct strbuf out
= STRBUF_INIT
;
451 struct strbuf err
= STRBUF_INIT
;
453 const char *remote_prefix
= "";
454 char *to_free
= NULL
;
457 * If we are listing more than just remote branches,
458 * then remote branches will have a "remotes/" prefix.
459 * We need to account for this in the width.
461 if (filter
->kind
!= FILTER_REFS_REMOTES
)
462 remote_prefix
= "remotes/";
464 memset(&array
, 0, sizeof(array
));
466 filter_refs(&array
, filter
, filter
->kind
);
469 maxwidth
= calc_maxwidth(&array
, strlen(remote_prefix
));
472 format
->format
= to_free
= build_format(filter
, maxwidth
, remote_prefix
);
473 format
->use_color
= branch_use_color
;
475 if (verify_ref_format(format
))
476 die(_("unable to parse format string"));
478 filter_ahead_behind(the_repository
, format
, &array
);
479 ref_array_sort(sorting
, &array
);
481 for (i
= 0; i
< array
.nr
; i
++) {
484 if (format_ref_array_item(array
.items
[i
], format
, &out
, &err
))
486 if (column_active(colopts
)) {
487 assert(!filter
->verbose
&& "--column and --verbose are incompatible");
488 /* format to a string_list to let print_columns() do its job */
489 string_list_append(output
, out
.buf
);
491 fwrite(out
.buf
, 1, out
.len
, stdout
);
492 if (out
.len
|| !omit_empty
)
497 strbuf_release(&err
);
498 strbuf_release(&out
);
499 ref_array_clear(&array
);
503 static void print_current_branch_name(void)
506 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, &flags
);
507 const char *shortname
;
509 die(_("could not resolve HEAD"));
510 else if (!(flags
& REF_ISSYMREF
))
512 else if (skip_prefix(refname
, "refs/heads/", &shortname
))
515 die(_("HEAD (%s) points outside of refs/heads/"), refname
);
518 static void reject_rebase_or_bisect_branch(struct worktree
**worktrees
,
523 for (i
= 0; worktrees
[i
]; i
++) {
524 struct worktree
*wt
= worktrees
[i
];
526 if (!wt
->is_detached
)
529 if (is_worktree_being_rebased(wt
, target
))
530 die(_("Branch %s is being rebased at %s"),
533 if (is_worktree_being_bisected(wt
, target
))
534 die(_("Branch %s is being bisected at %s"),
540 * Update all per-worktree HEADs pointing at the old ref to point the new ref.
541 * This will be used when renaming a branch. Returns 0 if successful, non-zero
544 static int replace_each_worktree_head_symref(struct worktree
**worktrees
,
545 const char *oldref
, const char *newref
,
551 for (i
= 0; worktrees
[i
]; i
++) {
552 struct ref_store
*refs
;
554 if (worktrees
[i
]->is_detached
)
556 if (!worktrees
[i
]->head_ref
)
558 if (strcmp(oldref
, worktrees
[i
]->head_ref
))
561 refs
= get_worktree_ref_store(worktrees
[i
]);
562 if (refs_create_symref(refs
, "HEAD", newref
, logmsg
))
563 ret
= error(_("HEAD of working tree %s is not updated"),
573 static void copy_or_rename_branch(const char *oldname
, const char *newname
, int copy
, int force
)
575 struct strbuf oldref
= STRBUF_INIT
, newref
= STRBUF_INIT
, logmsg
= STRBUF_INIT
;
576 struct strbuf oldsection
= STRBUF_INIT
, newsection
= STRBUF_INIT
;
577 const char *interpreted_oldname
= NULL
;
578 const char *interpreted_newname
= NULL
;
579 int recovery
= 0, oldref_usage
= 0;
580 struct worktree
**worktrees
= get_worktrees();
582 if (strbuf_check_branch_ref(&oldref
, oldname
)) {
584 * Bad name --- this could be an attempt to rename a
585 * ref that we used to allow to be created by accident.
587 if (ref_exists(oldref
.buf
))
590 die(_("Invalid branch name: '%s'"), oldname
);
593 for (int i
= 0; worktrees
[i
]; i
++) {
594 struct worktree
*wt
= worktrees
[i
];
596 if (wt
->head_ref
&& !strcmp(oldref
.buf
, wt
->head_ref
)) {
597 oldref_usage
|= IS_HEAD
;
598 if (is_null_oid(&wt
->head_oid
))
599 oldref_usage
|= IS_ORPHAN
;
604 if ((copy
|| !(oldref_usage
& IS_HEAD
)) && !ref_exists(oldref
.buf
)) {
605 if (oldref_usage
& IS_HEAD
)
606 die(_("No commit on branch '%s' yet."), oldname
);
608 die(_("No branch named '%s'."), oldname
);
612 * A command like "git branch -M currentbranch currentbranch" cannot
613 * cause the worktree to become inconsistent with HEAD, so allow it.
615 if (!strcmp(oldname
, newname
))
616 validate_branchname(newname
, &newref
);
618 validate_new_branchname(newname
, &newref
, force
);
620 reject_rebase_or_bisect_branch(worktrees
, oldref
.buf
);
622 if (!skip_prefix(oldref
.buf
, "refs/heads/", &interpreted_oldname
) ||
623 !skip_prefix(newref
.buf
, "refs/heads/", &interpreted_newname
)) {
624 BUG("expected prefix missing for refs");
628 strbuf_addf(&logmsg
, "Branch: copied %s to %s",
629 oldref
.buf
, newref
.buf
);
631 strbuf_addf(&logmsg
, "Branch: renamed %s to %s",
632 oldref
.buf
, newref
.buf
);
634 if (!copy
&& !(oldref_usage
& IS_ORPHAN
) &&
635 rename_ref(oldref
.buf
, newref
.buf
, logmsg
.buf
))
636 die(_("Branch rename failed"));
637 if (copy
&& copy_existing_ref(oldref
.buf
, newref
.buf
, logmsg
.buf
))
638 die(_("Branch copy failed"));
642 warning(_("Created a copy of a misnamed branch '%s'"),
643 interpreted_oldname
);
645 warning(_("Renamed a misnamed branch '%s' away"),
646 interpreted_oldname
);
649 if (!copy
&& (oldref_usage
& IS_HEAD
) &&
650 replace_each_worktree_head_symref(worktrees
, oldref
.buf
, newref
.buf
,
652 die(_("Branch renamed to %s, but HEAD is not updated!"), newname
);
654 strbuf_release(&logmsg
);
656 strbuf_addf(&oldsection
, "branch.%s", interpreted_oldname
);
657 strbuf_addf(&newsection
, "branch.%s", interpreted_newname
);
658 if (!copy
&& git_config_rename_section(oldsection
.buf
, newsection
.buf
) < 0)
659 die(_("Branch is renamed, but update of config-file failed"));
660 if (copy
&& strcmp(interpreted_oldname
, interpreted_newname
) && git_config_copy_section(oldsection
.buf
, newsection
.buf
) < 0)
661 die(_("Branch is copied, but update of config-file failed"));
662 strbuf_release(&oldref
);
663 strbuf_release(&newref
);
664 strbuf_release(&oldsection
);
665 strbuf_release(&newsection
);
666 free_worktrees(worktrees
);
669 static GIT_PATH_FUNC(edit_description
, "EDIT_DESCRIPTION")
671 static int edit_branch_description(const char *branch_name
)
674 struct strbuf buf
= STRBUF_INIT
;
675 struct strbuf name
= STRBUF_INIT
;
677 exists
= !read_branch_desc(&buf
, branch_name
);
678 if (!buf
.len
|| buf
.buf
[buf
.len
-1] != '\n')
679 strbuf_addch(&buf
, '\n');
680 strbuf_commented_addf(&buf
,
681 _("Please edit the description for the branch\n"
683 "Lines starting with '%c' will be stripped.\n"),
684 branch_name
, comment_line_char
);
685 write_file_buf(edit_description(), buf
.buf
, buf
.len
);
687 if (launch_editor(edit_description(), &buf
, NULL
)) {
688 strbuf_release(&buf
);
691 strbuf_stripspace(&buf
, 1);
693 strbuf_addf(&name
, "branch.%s.description", branch_name
);
694 if (buf
.len
|| exists
)
695 git_config_set(name
.buf
, buf
.len
? buf
.buf
: NULL
);
696 strbuf_release(&name
);
697 strbuf_release(&buf
);
702 int cmd_branch(int argc
, const char **argv
, const char *prefix
)
704 /* possible actions */
705 int delete = 0, rename
= 0, copy
= 0, list
= 0,
706 unset_upstream
= 0, show_current
= 0, edit_description
= 0;
707 const char *new_upstream
= NULL
;
708 int noncreate_actions
= 0;
709 /* possible options */
710 int reflog
= 0, quiet
= 0, icase
= 0, force
= 0,
711 recurse_submodules_explicit
= 0;
712 enum branch_track track
;
713 struct ref_filter filter
;
714 static struct ref_sorting
*sorting
;
715 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
716 struct ref_format format
= REF_FORMAT_INIT
;
718 struct option options
[] = {
719 OPT_GROUP(N_("Generic options")),
720 OPT__VERBOSE(&filter
.verbose
,
721 N_("show hash and subject, give twice for upstream branch")),
722 OPT__QUIET(&quiet
, N_("suppress informational messages")),
723 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
724 N_("set branch tracking configuration"),
726 parse_opt_tracking_mode
),
727 OPT_SET_INT_F(0, "set-upstream", &track
, N_("do not use"),
728 BRANCH_TRACK_OVERRIDE
, PARSE_OPT_HIDDEN
),
729 OPT_STRING('u', "set-upstream-to", &new_upstream
, N_("upstream"), N_("change the upstream info")),
730 OPT_BOOL(0, "unset-upstream", &unset_upstream
, N_("unset the upstream info")),
731 OPT__COLOR(&branch_use_color
, N_("use colored output")),
732 OPT_SET_INT('r', "remotes", &filter
.kind
, N_("act on remote-tracking branches"),
733 FILTER_REFS_REMOTES
),
734 OPT_CONTAINS(&filter
.with_commit
, N_("print only branches that contain the commit")),
735 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only branches that don't contain the commit")),
736 OPT_WITH(&filter
.with_commit
, N_("print only branches that contain the commit")),
737 OPT_WITHOUT(&filter
.no_commit
, N_("print only branches that don't contain the commit")),
738 OPT__ABBREV(&filter
.abbrev
),
740 OPT_GROUP(N_("Specific git-branch actions:")),
741 OPT_SET_INT('a', "all", &filter
.kind
, N_("list both remote-tracking and local branches"),
742 FILTER_REFS_REMOTES
| FILTER_REFS_BRANCHES
),
743 OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1),
744 OPT_BIT('D', NULL
, &delete, N_("delete branch (even if not merged)"), 2),
745 OPT_BIT('m', "move", &rename
, N_("move/rename a branch and its reflog"), 1),
746 OPT_BIT('M', NULL
, &rename
, N_("move/rename a branch, even if target exists"), 2),
747 OPT_BOOL(0, "omit-empty", &omit_empty
,
748 N_("do not output a newline after empty formatted refs")),
749 OPT_BIT('c', "copy", ©
, N_("copy a branch and its reflog"), 1),
750 OPT_BIT('C', NULL
, ©
, N_("copy a branch, even if target exists"), 2),
751 OPT_BOOL('l', "list", &list
, N_("list branch names")),
752 OPT_BOOL(0, "show-current", &show_current
, N_("show current branch name")),
753 OPT_BOOL(0, "create-reflog", &reflog
, N_("create the branch's reflog")),
754 OPT_BOOL(0, "edit-description", &edit_description
,
755 N_("edit the description for the branch")),
756 OPT__FORCE(&force
, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE
),
757 OPT_MERGED(&filter
, N_("print only branches that are merged")),
758 OPT_NO_MERGED(&filter
, N_("print only branches that are not merged")),
759 OPT_COLUMN(0, "column", &colopts
, N_("list branches in columns")),
760 OPT_REF_SORT(&sorting_options
),
761 OPT_CALLBACK(0, "points-at", &filter
.points_at
, N_("object"),
762 N_("print only branches of the object"), parse_opt_object_name
),
763 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
764 OPT_BOOL(0, "recurse-submodules", &recurse_submodules_explicit
, N_("recurse through submodules")),
765 OPT_STRING( 0 , "format", &format
.format
, N_("format"), N_("format to use for the output")),
769 setup_ref_filter_porcelain_msg();
771 memset(&filter
, 0, sizeof(filter
));
772 filter
.kind
= FILTER_REFS_BRANCHES
;
775 if (argc
== 2 && !strcmp(argv
[1], "-h"))
776 usage_with_options(builtin_branch_usage
, options
);
778 git_config(git_branch_config
, &sorting_options
);
780 track
= git_branch_track
;
782 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
784 die(_("Failed to resolve HEAD as a valid ref."));
785 if (!strcmp(head
, "HEAD"))
787 else if (!skip_prefix(head
, "refs/heads/", &head
))
788 die(_("HEAD not found below refs/heads!"));
790 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_branch_usage
,
793 if (!delete && !rename
&& !copy
&& !edit_description
&& !new_upstream
&&
794 !show_current
&& !unset_upstream
&& argc
== 0)
797 if (filter
.with_commit
|| filter
.no_commit
||
798 filter
.reachable_from
|| filter
.unreachable_from
|| filter
.points_at
.nr
)
801 noncreate_actions
= !!delete + !!rename
+ !!copy
+ !!new_upstream
+
802 !!show_current
+ !!list
+ !!edit_description
+
804 if (noncreate_actions
> 1)
805 usage_with_options(builtin_branch_usage
, options
);
807 if (recurse_submodules_explicit
) {
808 if (!submodule_propagate_branches
)
809 die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
810 if (noncreate_actions
)
811 die(_("--recurse-submodules can only be used to create branches"));
815 (recurse_submodules
|| recurse_submodules_explicit
) &&
816 submodule_propagate_branches
;
818 if (filter
.abbrev
== -1)
819 filter
.abbrev
= DEFAULT_ABBREV
;
820 filter
.ignore_case
= icase
;
822 finalize_colopts(&colopts
, -1);
823 if (filter
.verbose
) {
824 if (explicitly_enable_column(colopts
))
825 die(_("options '%s' and '%s' cannot be used together"), "--column", "--verbose");
836 setup_auto_pager("branch", 1);
838 UNLEAK(sorting_options
);
842 die(_("branch name required"));
843 return delete_branches(argc
, argv
, delete > 1, filter
.kind
, quiet
);
844 } else if (show_current
) {
845 print_current_branch_name();
848 /* git branch --list also shows HEAD when it is detached */
849 if ((filter
.kind
& FILTER_REFS_BRANCHES
) && filter
.detached
)
850 filter
.kind
|= FILTER_REFS_DETACHED_HEAD
;
851 filter
.name_patterns
= argv
;
853 * If no sorting parameter is given then we default to sorting
854 * by 'refname'. This would give us an alphabetically sorted
855 * array with the 'HEAD' ref at the beginning followed by
856 * local branches 'refs/heads/...' and finally remote-tracking
857 * branches 'refs/remotes/...'.
859 sorting
= ref_sorting_options(&sorting_options
);
860 ref_sorting_set_sort_flags_all(sorting
, REF_SORTING_ICASE
, icase
);
861 ref_sorting_set_sort_flags_all(
862 sorting
, REF_SORTING_DETACHED_HEAD_FIRST
, 1);
863 print_ref_list(&filter
, sorting
, &format
, &output
);
864 print_columns(&output
, colopts
, NULL
);
865 string_list_clear(&output
, 0);
866 ref_sorting_release(sorting
);
868 } else if (edit_description
) {
869 const char *branch_name
;
870 struct strbuf branch_ref
= STRBUF_INIT
;
871 struct strbuf buf
= STRBUF_INIT
;
872 int ret
= 1; /* assume failure */
876 die(_("Cannot give description to detached HEAD"));
878 } else if (argc
== 1) {
879 strbuf_branchname(&buf
, argv
[0], INTERPRET_BRANCH_LOCAL
);
880 branch_name
= buf
.buf
;
882 die(_("cannot edit description of more than one branch"));
885 strbuf_addf(&branch_ref
, "refs/heads/%s", branch_name
);
886 if (!ref_exists(branch_ref
.buf
))
887 error((!argc
|| branch_checked_out(branch_ref
.buf
))
888 ? _("No commit on branch '%s' yet.")
889 : _("No branch named '%s'."),
891 else if (!edit_branch_description(branch_name
))
894 strbuf_release(&branch_ref
);
895 strbuf_release(&buf
);
898 } else if (copy
|| rename
) {
900 die(_("branch name required"));
901 else if ((argc
== 1) && filter
.detached
)
902 die(copy
? _("cannot copy the current branch while not on any.")
903 : _("cannot rename the current branch while not on any."));
905 copy_or_rename_branch(head
, argv
[0], copy
, copy
+ rename
> 1);
907 copy_or_rename_branch(argv
[0], argv
[1], copy
, copy
+ rename
> 1);
909 die(copy
? _("too many branches for a copy operation")
910 : _("too many arguments for a rename operation"));
911 } else if (new_upstream
) {
912 struct branch
*branch
;
913 struct strbuf buf
= STRBUF_INIT
;
916 branch
= branch_get(NULL
);
917 else if (argc
== 1) {
918 strbuf_branchname(&buf
, argv
[0], INTERPRET_BRANCH_LOCAL
);
919 branch
= branch_get(buf
.buf
);
921 die(_("too many arguments to set new upstream"));
924 if (!argc
|| !strcmp(argv
[0], "HEAD"))
925 die(_("could not set upstream of HEAD to %s when "
926 "it does not point to any branch."),
928 die(_("no such branch '%s'"), argv
[0]);
931 if (!ref_exists(branch
->refname
)) {
932 if (!argc
|| branch_checked_out(branch
->refname
))
933 die(_("No commit on branch '%s' yet."), branch
->name
);
934 die(_("branch '%s' does not exist"), branch
->name
);
937 dwim_and_setup_tracking(the_repository
, branch
->name
,
938 new_upstream
, BRANCH_TRACK_OVERRIDE
,
940 strbuf_release(&buf
);
941 } else if (unset_upstream
) {
942 struct branch
*branch
;
943 struct strbuf buf
= STRBUF_INIT
;
946 branch
= branch_get(NULL
);
947 else if (argc
== 1) {
948 strbuf_branchname(&buf
, argv
[0], INTERPRET_BRANCH_LOCAL
);
949 branch
= branch_get(buf
.buf
);
951 die(_("too many arguments to unset upstream"));
954 if (!argc
|| !strcmp(argv
[0], "HEAD"))
955 die(_("could not unset upstream of HEAD when "
956 "it does not point to any branch."));
957 die(_("no such branch '%s'"), argv
[0]);
960 if (!branch_has_merge_config(branch
))
961 die(_("Branch '%s' has no upstream information"), branch
->name
);
964 strbuf_addf(&buf
, "branch.%s.remote", branch
->name
);
965 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
967 strbuf_addf(&buf
, "branch.%s.merge", branch
->name
);
968 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
969 strbuf_release(&buf
);
970 } else if (!noncreate_actions
&& argc
> 0 && argc
<= 2) {
971 const char *branch_name
= argv
[0];
972 const char *start_name
= argc
== 2 ? argv
[1] : head
;
974 if (filter
.kind
!= FILTER_REFS_BRANCHES
)
975 die(_("The -a, and -r, options to 'git branch' do not take a branch name.\n"
976 "Did you mean to use: -a|-r --list <pattern>?"));
978 if (track
== BRANCH_TRACK_OVERRIDE
)
979 die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
981 if (recurse_submodules
) {
982 create_branches_recursively(the_repository
, branch_name
,
983 start_name
, NULL
, force
,
984 reflog
, quiet
, track
, 0);
987 create_branch(the_repository
, branch_name
, start_name
, force
, 0,
988 reflog
, quiet
, track
, 0);
990 usage_with_options(builtin_branch_usage
, options
);