Skip to content

Commit f507784

Browse files
committed
Merge branch 'nd/branch-error-cases'
Fix various error messages and conditions in "git branch", e.g. we advertised "branch -d/-D" to remove one or more branches but actually implemented removal of zero or more branches---request to remove no branches was not rejected. * nd/branch-error-cases: branch: let branch filters imply --list docs: clarify git-branch --list behavior branch: mark more strings for translation branch: give a more helpful message on redundant arguments branch: reject -D/-d without branch name
2 parents 9a1ab9e + d040350 commit f507784

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

Documentation/git-branch.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ SYNOPSIS
2222
DESCRIPTION
2323
-----------
2424

25-
With no arguments, existing branches are listed and the current branch will
26-
be highlighted with an asterisk. Option `-r` causes the remote-tracking
27-
branches to be listed, and option `-a` shows both. This list mode is also
28-
activated by the `--list` option (see below).
29-
<pattern> restricts the output to matching branches, the pattern is a shell
30-
wildcard (i.e., matched using fnmatch(3)).
31-
Multiple patterns may be given; if any of them matches, the branch is shown.
25+
If `--list` is given, or if there are no non-option arguments, existing
26+
branches are listed; the current branch will be highlighted with an
27+
asterisk. Option `-r` causes the remote-tracking branches to be listed,
28+
and option `-a` shows both local and remote branches. If a `<pattern>`
29+
is given, it is used as a shell wildcard to restrict the output to
30+
matching branches. If multiple patterns are given, a branch is shown if
31+
it matches any of the patterns. Note that when providing a
32+
`<pattern>`, you must use `--list`; otherwise the command is interpreted
33+
as branch creation.
3234

3335
With `--contains`, shows only the branches that contain the named commit
3436
(in other words, the branches whose tip commits are descendants of the
@@ -193,15 +195,15 @@ start-point is either a local or remote-tracking branch.
193195

194196
--contains [<commit>]::
195197
Only list branches which contain the specified commit (HEAD
196-
if not specified).
198+
if not specified). Implies `--list`.
197199

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

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

206208
<branchname>::
207209
The name of the branch to create or delete.

builtin/branch.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
466466
int verbose, int abbrev)
467467
{
468468
struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
469-
const char *sub = " **** invalid ref ****";
469+
const char *sub = _(" **** invalid ref ****");
470470
struct commit *commit = item->commit;
471471

472472
if (commit && !parse_commit(commit)) {
@@ -590,7 +590,7 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
590590
struct commit *filter;
591591
filter = lookup_commit_reference_gently(merge_filter_ref, 0);
592592
if (!filter)
593-
die("object '%s' does not point to a commit",
593+
die(_("object '%s' does not point to a commit"),
594594
sha1_to_hex(merge_filter_ref));
595595

596596
filter->object.flags |= UNINTERESTING;
@@ -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

@@ -837,9 +840,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
837840
colopts = 0;
838841
}
839842

840-
if (delete)
843+
if (delete) {
844+
if (!argc)
845+
die(_("branch name required"));
841846
return delete_branches(argc, argv, delete > 1, kinds, quiet);
842-
else if (list) {
847+
} else if (list) {
843848
int ret = print_ref_list(kinds, detached, verbose, abbrev,
844849
with_commit, argv);
845850
print_columns(&output, colopts, NULL);
@@ -852,22 +857,23 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
852857

853858
if (!argc) {
854859
if (detached)
855-
die("Cannot give description to detached HEAD");
860+
die(_("Cannot give description to detached HEAD"));
856861
branch_name = head;
857862
} else if (argc == 1)
858863
branch_name = argv[0];
859864
else
860-
usage_with_options(builtin_branch_usage, options);
865+
die(_("cannot edit description of more than one branch"));
861866

862867
strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
863868
if (!ref_exists(branch_ref.buf)) {
864869
strbuf_release(&branch_ref);
865870

866871
if (!argc)
867-
return error("No commit on branch '%s' yet.",
872+
return error(_("No commit on branch '%s' yet."),
868873
branch_name);
869874
else
870-
return error("No such branch '%s'.", branch_name);
875+
return error(_("No branch named '%s'."),
876+
branch_name);
871877
}
872878
strbuf_release(&branch_ref);
873879

@@ -879,7 +885,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
879885
else if (argc == 2)
880886
rename_branch(argv[0], argv[1], rename > 1);
881887
else
882-
usage_with_options(builtin_branch_usage, options);
888+
die(_("too many branches for a rename operation"));
883889
} else if (new_upstream) {
884890
struct branch *branch = branch_get(argv[0]);
885891

t/t3200-branch.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ test_expect_success \
7373

7474
test_expect_success \
7575
'git branch -m dumps usage' \
76-
'test_expect_code 129 git branch -m 2>err &&
77-
test_i18ngrep "[Uu]sage: git branch" err'
76+
'test_expect_code 128 git branch -m 2>err &&
77+
test_i18ngrep "too many branches for a rename operation" err'
7878

7979
test_expect_success \
8080
'git branch -m m m/m should work' \

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)