branch: introduce --list option

Currently, there is no way to invoke the list mode explicitly, without giving -v to force verbose output. Introduce a --list option which invokes the list mode. This will be beneficial for invoking list mode with pattern matching, which otherwise would be interpreted as branch creation. Along with --list, test also combinations of existing options. Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael J Gruber committed Aug 28, 2011 at 16:54 UTC cddd127b9afe3aa516aafdc38e9a8778f1340e0d
4 files changed +53 -5
Documentation/git-branch.txt
+7 -2
@@ -9,7 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git branch' [--color[=<when>] | --no-color] [-r | -a]
12 - [-v [--abbrev=<length> | --no-abbrev]]
12 + [--list] [-v [--abbrev=<length> | --no-abbrev]]
13 [(--merged | --no-merged | --contains) [<commit>]]
14 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
15 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -20,7 +20,8 @@ DESCRIPTION
20
21 With no arguments, existing branches are listed and the current branch will
22 be highlighted with an asterisk. Option `-r` causes the remote-tracking
23 -branches to be listed, and option `-a` shows both.
23 +branches to be listed, and option `-a` shows both. This list mode is also
24 +activated by the `--list` and `-v` options (see below).
25
26 With `--contains`, shows only the branches that contain the named commit
27 (in other words, the branches whose tip commits are descendants of the
@@ -110,11 +111,15 @@ OPTIONS
111 --all::
112 List both remote-tracking branches and local branches.
113
114 +--list::
115 + Activate the list mode.
116 +
117 -v::
118 --verbose::
119 Show sha1 and commit subject line for each head, along with
120 relationship to upstream branch (if any). If given twice, print
121 the name of the upstream branch, as well.
122 + `--list` is implied by all verbosity options.
123
124 --abbrev=<length>::
125 Alter the sha1's minimum display length in the output listing.
builtin/branch.c
+9 -3
@@ -612,7 +612,7 @@ static int opt_parse_merge_filter(const struct option *opt, const char *arg, int
612
613 int cmd_branch(int argc, const char **argv, const char *prefix)
614 {
615 - int delete = 0, rename = 0, force_create = 0;
615 + int delete = 0, rename = 0, force_create = 0, list = 0;
616 int verbose = 0, abbrev = -1, detached = 0;
617 int reflog = 0;
618 enum branch_track track;
@@ -651,6 +651,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
651 OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2),
652 OPT_BIT('m', "move", &rename, "move/rename a branch and its reflog", 1),
653 OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
654 + OPT_BOOLEAN(0, "list", &list, "list branch names"),
655 OPT_BOOLEAN('l', "create-reflog", &reflog, "create the branch's reflog"),
656 OPT__FORCE(&force_create, "force creation (when already exists)"),
657 {
@@ -693,7 +694,12 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
694
695 argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
696 0);
696 - if (!!delete + !!rename + !!force_create > 1)
697 +
698 + if (!delete && !rename && !force_create &&
699 + (argc == 0 || (verbose && argc)))
700 + list = 1;
701 +
702 + if (!!delete + !!rename + !!force_create + !!list > 1)
703 usage_with_options(builtin_branch_usage, options);
704
705 if (abbrev == -1)
@@ -701,7 +707,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
707
708 if (delete)
709 return delete_branches(argc, argv, delete > 1, kinds);
704 - else if (argc == 0)
710 + else if (list)
711 return print_ref_list(kinds, detached, verbose, abbrev, with_commit);
712 else if (rename && (argc == 1))
713 rename_branch(head, argv[0], rename > 1);
t/t3200-branch.sh
+32
@@ -98,6 +98,38 @@ test_expect_success 'git branch -m q r/q should fail when r exists' '
98 test_must_fail git branch -m q r/q
99 '
100
101 +test_expect_success 'git branch -v -d t should work' '
102 + git branch t &&
103 + test .git/refs/heads/t &&
104 + git branch -v -d t &&
105 + test ! -f .git/refs/heads/t
106 +'
107 +
108 +test_expect_success 'git branch -v -m t s should work' '
109 + git branch t &&
110 + test .git/refs/heads/t &&
111 + git branch -v -m t s &&
112 + test ! -f .git/refs/heads/t &&
113 + test -f .git/refs/heads/s &&
114 + git branch -d s
115 +'
116 +
117 +test_expect_success 'git branch -m -d t s should fail' '
118 + git branch t &&
119 + test .git/refs/heads/t &&
120 + test_must_fail git branch -m -d t s &&
121 + git branch -d t &&
122 + test ! -f .git/refs/heads/t
123 +'
124 +
125 +test_expect_success 'git branch --list -d t should fail' '
126 + git branch t &&
127 + test .git/refs/heads/t &&
128 + test_must_fail git branch --list -d t &&
129 + git branch -d t &&
130 + test ! -f .git/refs/heads/t
131 +'
132 +
133 mv .git/config .git/config-saved
134
135 test_expect_success 'git branch -m q q2 without config should succeed' '
t/t3203-branch-output.sh
+5
@@ -32,6 +32,11 @@ test_expect_success 'git branch shows local branches' '
32 test_cmp expect actual
33 '
34
35 +test_expect_success 'git branch --list shows local branches' '
36 + git branch --list >actual &&
37 + test_cmp expect actual
38 +'
39 +
40 cat >expect <<'EOF'
41 origin/HEAD -> origin/branch-one
42 origin/branch-one