Skip to content

Commit 41c64ae

Browse files
committed
show-branch: -g and --current are incompatible
When "--current" is given to "git show-branch" running in the "--reflog" mode, the code tries to reference a "reflog" message that does not even exist. This is because the --current is not prepared to work in that mode. The reason "--current" exists is to support this request: I list branches on the command line. These are the branchesI care about and I use as anchoring points. I may or may not be on one of these main branches. Please make sure I can view the commits on the current branch with respect to what is in these other branches. And to serve that request, the code checks if the current branch is among the ones listed on the command line, and adds it only if it is not to the end of one array, which essentially lists the objects. The reflog mode additionally uses another array to list reflog messages, which the "--current" code does not add to. This leaves one uninitialized slot at the end of the array of reflog messages, and causes the program to show garbage or segfault. Catch the unsupported (and meaningless) combination and exit with a usage error. There are other combinations of options that are incompatible but have not been tested. Add test to cover them while adding coverage for this new combination. Reported-by: Gregory David <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d516b2d commit 41c64ae

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

builtin/show-branch.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
711711
"--all/--remotes/--independent/--merge-base");
712712
}
713713

714+
if (with_current_branch && reflog)
715+
die(_("options '%s' and '%s' cannot be used together"),
716+
"--reflog", "--current");
717+
714718
/* If nothing is specified, show all branches by default */
715719
if (ac <= topics && all_heads + all_remotes == 0)
716720
all_heads = 1;

t/t3202-show-branch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,18 @@ test_expect_success 'show branch --reflog=2' '
161161
test_cmp actual expect
162162
'
163163

164+
# incompatible options
165+
while read combo
166+
do
167+
test_expect_success "show-branch $combo (should fail)" '
168+
test_must_fail git show-branch $combo 2>error &&
169+
grep -e "cannot be used together" -e "usage:" error
170+
'
171+
done <<\EOF
172+
--all --reflog
173+
--merge-base --reflog
174+
--list --merge-base
175+
--reflog --current
176+
EOF
177+
164178
test_done

0 commit comments

Comments
 (0)