Skip to content

Commit d040350

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent de90ff8 commit d040350

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

Documentation/git-branch.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,15 @@ start-point is either a local or remote-tracking branch.
195195

196196
--contains [<commit>]::
197197
Only list branches which contain the specified commit (HEAD
198-
if not specified).
198+
if not specified). Implies `--list`.
199199

200200
--merged [<commit>]::
201201
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`.
203203

204204
--no-merged [<commit>]::
205205
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`.
207207

208208
<branchname>::
209209
The name of the branch to create or delete.

builtin/branch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
825825
if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0)
826826
list = 1;
827827

828+
if (with_commit || merge_filter != NO_FILTER)
829+
list = 1;
830+
828831
if (!!delete + !!rename + !!force_create + !!list + !!new_upstream + !!unset_upstream > 1)
829832
usage_with_options(builtin_branch_usage, options);
830833

t/t3201-branch-contains.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ test_expect_success 'branch --contains=side' '
5555
5656
'
5757

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+
5868
test_expect_success 'side: branch --merged' '
5969
6070
git branch --merged >actual &&
@@ -66,6 +76,16 @@ test_expect_success 'side: branch --merged' '
6676
6777
'
6878

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+
6989
test_expect_success 'side: branch --no-merged' '
7090
7191
git branch --no-merged >actual &&
@@ -95,4 +115,19 @@ test_expect_success 'master: branch --no-merged' '
95115
96116
'
97117

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+
98133
test_done

0 commit comments

Comments
 (0)