branch: let branch filters imply --list

Currently, a branch filter like `--contains`, `--merged`, or `--no-merged` is ignored when we are not in listing mode. For example: git branch --contains=foo bar will create the branch "bar" from the current HEAD, ignoring the `--contains` argument entirely. This is not very helpful. There are two reasonable behaviors for git here: 1. Flag an error; the arguments do not make sense. 2. Implicitly go into `--list` mode This patch chooses the latter, as it is more convenient, and there should not be any ambiguity with attempting to create a branch; using `--contains` and not wanting to list is nonsensical. That leaves the case where an explicit modification option like `-d` is given. We already catch the case where `--list` is given alongside `-d` and flag an error. With this patch, we will also catch the use of `--contains` and other filter options alongside `-d`. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 31, 2013 at 01:46 UTC d040350813986e0f4bb3aeb977077975c1552606
3 files changed +41 -3
Documentation/git-branch.txt
+3 -3
@@ -195,15 +195,15 @@ start-point is either a local or remote-tracking branch.
195
196 --contains [<commit>]::
197 Only list branches which contain the specified commit (HEAD
198 - if not specified).
198 + if not specified). Implies `--list`.
199
200 --merged [<commit>]::
201 Only list branches whose tips are reachable from the
202 - specified commit (HEAD if not specified).
202 + specified commit (HEAD if not specified). Implies `--list`.
203
204 --no-merged [<commit>]::
205 Only list branches whose tips are not reachable from the
206 - specified commit (HEAD if not specified).
206 + specified commit (HEAD if not specified). Implies `--list`.
207
208 <branchname>::
209 The name of the branch to create or delete.
builtin/branch.c
+3
@@ -825,6 +825,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
825 if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0)
826 list = 1;
827
828 + if (with_commit || merge_filter != NO_FILTER)
829 + list = 1;
830 +
831 if (!!delete + !!rename + !!force_create + !!list + !!new_upstream + !!unset_upstream > 1)
832 usage_with_options(builtin_branch_usage, options);
833
t/t3201-branch-contains.sh
+35
@@ -55,6 +55,16 @@ test_expect_success 'branch --contains=side' '
55
56 '
57
58 +test_expect_success 'branch --contains with pattern implies --list' '
59 +
60 + git branch --contains=master master >actual &&
61 + {
62 + echo " master"
63 + } >expect &&
64 + test_cmp expect actual
65 +
66 +'
67 +
68 test_expect_success 'side: branch --merged' '
69
70 git branch --merged >actual &&
@@ -66,6 +76,16 @@ test_expect_success 'side: branch --merged' '
76
77 '
78
79 +test_expect_success 'branch --merged with pattern implies --list' '
80 +
81 + git branch --merged=side master >actual &&
82 + {
83 + echo " master"
84 + } >expect &&
85 + test_cmp expect actual
86 +
87 +'
88 +
89 test_expect_success 'side: branch --no-merged' '
90
91 git branch --no-merged >actual &&
@@ -95,4 +115,19 @@ test_expect_success 'master: branch --no-merged' '
115
116 '
117
118 +test_expect_success 'branch --no-merged with pattern implies --list' '
119 +
120 + git branch --no-merged=master master >actual &&
121 + >expect &&
122 + test_cmp expect actual
123 +
124 +'
125 +
126 +test_expect_success 'implicit --list conflicts with modification options' '
127 +
128 + test_must_fail git branch --contains=master -d &&
129 + test_must_fail git branch --contains=master -m foo
130 +
131 +'
132 +
133 test_done