Skip to content

Commit

Permalink
branch: let branch filters imply --list
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Feb 1, 2013
1 parent de90ff8 commit d040350
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Documentation/git-branch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ start-point is either a local or remote-tracking branch.

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

--merged [<commit>]::
Only list branches whose tips are reachable from the
specified commit (HEAD if not specified).
specified commit (HEAD if not specified). Implies `--list`.

--no-merged [<commit>]::
Only list branches whose tips are not reachable from the
specified commit (HEAD if not specified).
specified commit (HEAD if not specified). Implies `--list`.

<branchname>::
The name of the branch to create or delete.
Expand Down
3 changes: 3 additions & 0 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0)
list = 1;

if (with_commit || merge_filter != NO_FILTER)
list = 1;

if (!!delete + !!rename + !!force_create + !!list + !!new_upstream + !!unset_upstream > 1)
usage_with_options(builtin_branch_usage, options);

Expand Down
35 changes: 35 additions & 0 deletions t/t3201-branch-contains.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ test_expect_success 'branch --contains=side' '
'

test_expect_success 'branch --contains with pattern implies --list' '
git branch --contains=master master >actual &&
{
echo " master"
} >expect &&
test_cmp expect actual
'

test_expect_success 'side: branch --merged' '
git branch --merged >actual &&
Expand All @@ -66,6 +76,16 @@ test_expect_success 'side: branch --merged' '
'

test_expect_success 'branch --merged with pattern implies --list' '
git branch --merged=side master >actual &&
{
echo " master"
} >expect &&
test_cmp expect actual
'

test_expect_success 'side: branch --no-merged' '
git branch --no-merged >actual &&
Expand Down Expand Up @@ -95,4 +115,19 @@ test_expect_success 'master: branch --no-merged' '
'

test_expect_success 'branch --no-merged with pattern implies --list' '
git branch --no-merged=master master >actual &&
>expect &&
test_cmp expect actual
'

test_expect_success 'implicit --list conflicts with modification options' '
test_must_fail git branch --contains=master -d &&
test_must_fail git branch --contains=master -m foo
'

test_done

0 comments on commit d040350

Please sign in to comment.