@matheusbsbs / matheus-bagliano / commits / 73c6de06af
Fork 0

bisect: don't use invalid oid as rev when starting

In 06f5608c14 (bisect--helper: `bisect_start` shell function partially in C, 2019-01-02), we changed the following shell code: - rev=$(git rev-parse -q --verify "$arg^{commit}") || { - test $has_double_dash -eq 1 && - die "$(eval_gettext "'\$arg' does not appear to be a valid revision")" - break - } - revs="$revs $rev" into: + char *commit_id = xstrfmt("%s^{commit}", arg); + if (get_oid(commit_id, &oid) && has_double_dash) + die(_("'%s' does not appear to be a valid " + "revision"), arg); + + string_list_append(&revs, oid_to_hex(&oid)); + free(commit_id); In case of an invalid "arg" when "has_double_dash" is false, the old code would "break" out of the argument loop. In the new C code though, `oid_to_hex(&oid)` is unconditonally appended to "revs". This is wrong first because "oid" is junk as `get_oid(commit_id, &oid)` failed and second because it doesn't break out of the argument loop. Not breaking out of the argument loop means that "arg" is then not treated as a path restriction (which is wrong). Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Sep 25, 2020 at 15:01 UTC 73c6de06aff8d14b5e9f55f393d25f1213f3d43d
2 files changed +13 -7
builtin/bisect--helper.c
+6 -7
@@ -474,14 +474,13 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
474 } else if (starts_with(arg, "--") &&
475 !one_of(arg, "--term-good", "--term-bad", NULL)) {
476 return error(_("unrecognized option: '%s'"), arg);
477 - } else {
478 - char *commit_id = xstrfmt("%s^{commit}", arg);
479 - if (get_oid(commit_id, &oid) && has_double_dash)
480 - die(_("'%s' does not appear to be a valid "
481 - "revision"), arg);
482 -
477 + } else if (!get_oidf(&oid, "%s^{commit}", arg)) {
478 string_list_append(&revs, oid_to_hex(&oid));
484 - free(commit_id);
479 + } else if (has_double_dash) {
480 + die(_("'%s' does not appear to be a valid "
481 + "revision"), arg);
482 + } else {
483 + break;
484 }
485 }
486 pathspec_pos = i;
t/t6030-bisect-porcelain.sh
+7
@@ -82,6 +82,13 @@ test_expect_success 'bisect fails if given any junk instead of revs' '
82 git bisect bad $HASH4
83 '
84
85 +test_expect_success 'bisect start without -- takes unknown arg as pathspec' '
86 + git bisect reset &&
87 + git bisect start foo bar &&
88 + grep foo ".git/BISECT_NAMES" &&
89 + grep bar ".git/BISECT_NAMES"
90 +'
91 +
92 test_expect_success 'bisect reset: back in the master branch' '
93 git bisect reset &&
94 echo "* master" > branch.expect &&