Skip to content

Commit 43f9d9f

Browse files
CanardChouChinoisgitster
authored andcommitted
bisect: replace hardcoded "bad|good" by variables
To add new tags like old/new and have keywords less confusing, the first step is to avoid hardcoding the keywords. The default mode is still bad/good. Signed-off-by: Antoine Delaite <[email protected]> Signed-off-by: Louis Stuber <[email protected]> Signed-off-by: Valentin Duperray <[email protected]> Signed-off-by: Franck Jonas <[email protected]> Signed-off-by: Lucien Kong <[email protected]> Signed-off-by: Thomas Nguy <[email protected]> Signed-off-by: Huynh Khoi Nguyen Nguyen <[email protected]> Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2df5a84 commit 43f9d9f

File tree

2 files changed

+68
-43
lines changed

2 files changed

+68
-43
lines changed

bisect.c

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
2121
static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
2222
static const char *argv_update_ref[] = {"update-ref", "--no-deref", "BISECT_HEAD", NULL, NULL};
2323

24+
static const char *term_bad;
25+
static const char *term_good;
26+
2427
/* Remember to update object flag allocation in object.h */
2528
#define COUNTED (1u<<16)
2629

@@ -403,15 +406,21 @@ struct commit_list *find_bisection(struct commit_list *list,
403406
static int register_ref(const char *refname, const struct object_id *oid,
404407
int flags, void *cb_data)
405408
{
406-
if (!strcmp(refname, "bad")) {
409+
struct strbuf good_prefix = STRBUF_INIT;
410+
strbuf_addstr(&good_prefix, term_good);
411+
strbuf_addstr(&good_prefix, "-");
412+
413+
if (!strcmp(refname, term_bad)) {
407414
current_bad_oid = xmalloc(sizeof(*current_bad_oid));
408415
oidcpy(current_bad_oid, oid);
409-
} else if (starts_with(refname, "good-")) {
416+
} else if (starts_with(refname, good_prefix.buf)) {
410417
sha1_array_append(&good_revs, oid->hash);
411418
} else if (starts_with(refname, "skip-")) {
412419
sha1_array_append(&skipped_revs, oid->hash);
413420
}
414421

422+
strbuf_release(&good_prefix);
423+
415424
return 0;
416425
}
417426

@@ -634,7 +643,7 @@ static void exit_if_skipped_commits(struct commit_list *tried,
634643
return;
635644

636645
printf("There are only 'skip'ped commits left to test.\n"
637-
"The first bad commit could be any of:\n");
646+
"The first %s commit could be any of:\n", term_bad);
638647
print_commit_list(tried, "%s\n", "%s\n");
639648
if (bad)
640649
printf("%s\n", oid_to_hex(bad));
@@ -732,18 +741,24 @@ static void handle_bad_merge_base(void)
732741
if (is_expected_rev(current_bad_oid)) {
733742
char *bad_hex = oid_to_hex(current_bad_oid);
734743
char *good_hex = join_sha1_array_hex(&good_revs, ' ');
735-
736-
fprintf(stderr, "The merge base %s is bad.\n"
737-
"This means the bug has been fixed "
738-
"between %s and [%s].\n",
739-
bad_hex, bad_hex, good_hex);
740-
744+
if (!strcmp(term_bad, "bad") && !strcmp(term_good, "good")) {
745+
fprintf(stderr, "The merge base %s is bad.\n"
746+
"This means the bug has been fixed "
747+
"between %s and [%s].\n",
748+
bad_hex, bad_hex, good_hex);
749+
} else {
750+
fprintf(stderr, "The merge base %s is %s.\n"
751+
"This means the first '%s' commit is "
752+
"between %s and [%s].\n",
753+
bad_hex, term_bad, term_good, bad_hex, good_hex);
754+
}
741755
exit(3);
742756
}
743757

744-
fprintf(stderr, "Some good revs are not ancestor of the bad rev.\n"
758+
fprintf(stderr, "Some %s revs are not ancestor of the %s rev.\n"
745759
"git bisect cannot work properly in this case.\n"
746-
"Maybe you mistook good and bad revs?\n");
760+
"Maybe you mistook %s and %s revs?\n",
761+
term_good, term_bad, term_good, term_bad);
747762
exit(1);
748763
}
749764

@@ -755,10 +770,10 @@ static void handle_skipped_merge_base(const unsigned char *mb)
755770

756771
warning("the merge base between %s and [%s] "
757772
"must be skipped.\n"
758-
"So we cannot be sure the first bad commit is "
773+
"So we cannot be sure the first %s commit is "
759774
"between %s and %s.\n"
760775
"We continue anyway.",
761-
bad_hex, good_hex, mb_hex, bad_hex);
776+
bad_hex, good_hex, term_bad, mb_hex, bad_hex);
762777
free(good_hex);
763778
}
764779

@@ -839,7 +854,7 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout)
839854
int fd;
840855

841856
if (!current_bad_oid)
842-
die("a bad revision is needed");
857+
die("a %s revision is needed", term_bad);
843858

844859
/* Check if file BISECT_ANCESTORS_OK exists. */
845860
if (!stat(filename, &st) && S_ISREG(st.st_mode))
@@ -905,6 +920,8 @@ int bisect_next_all(const char *prefix, int no_checkout)
905920
const unsigned char *bisect_rev;
906921
char bisect_rev_hex[GIT_SHA1_HEXSZ + 1];
907922

923+
term_bad = "bad";
924+
term_good = "good";
908925
if (read_bisect_refs())
909926
die("reading bisect refs failed");
910927

@@ -926,8 +943,10 @@ int bisect_next_all(const char *prefix, int no_checkout)
926943
*/
927944
exit_if_skipped_commits(tried, NULL);
928945

929-
printf("%s was both good and bad\n",
930-
oid_to_hex(current_bad_oid));
946+
printf("%s was both %s and %s\n",
947+
oid_to_hex(current_bad_oid),
948+
term_good,
949+
term_bad);
931950
exit(1);
932951
}
933952

@@ -942,7 +961,8 @@ int bisect_next_all(const char *prefix, int no_checkout)
942961

943962
if (!hashcmp(bisect_rev, current_bad_oid->hash)) {
944963
exit_if_skipped_commits(tried, current_bad_oid);
945-
printf("%s is the first bad commit\n", bisect_rev_hex);
964+
printf("%s is the first %s commit\n", bisect_rev_hex,
965+
term_bad);
946966
show_diff_tree(prefix, revs.commits->item);
947967
/* This means the bisection process succeeded. */
948968
exit(10);

git-bisect.sh

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ OPTIONS_SPEC=
3232

3333
_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
3434
_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
35+
TERM_BAD=bad
36+
TERM_GOOD=good
3537

3638
bisect_head()
3739
{
@@ -100,8 +102,8 @@ bisect_start() {
100102
break
101103
}
102104
case $bad_seen in
103-
0) state='bad' ; bad_seen=1 ;;
104-
*) state='good' ;;
105+
0) state=$TERM_BAD ; bad_seen=1 ;;
106+
*) state=$TERM_GOOD ;;
105107
esac
106108
eval="$eval bisect_write '$state' '$rev' 'nolog' &&"
107109
shift
@@ -184,9 +186,12 @@ bisect_write() {
184186
rev="$2"
185187
nolog="$3"
186188
case "$state" in
187-
bad) tag="$state" ;;
188-
good|skip) tag="$state"-"$rev" ;;
189-
*) die "$(eval_gettext "Bad bisect_write argument: \$state")" ;;
189+
"$TERM_BAD")
190+
tag="$state" ;;
191+
"$TERM_GOOD"|skip)
192+
tag="$state"-"$rev" ;;
193+
*)
194+
die "$(eval_gettext "Bad bisect_write argument: \$state")" ;;
190195
esac
191196
git update-ref "refs/bisect/$tag" "$rev" || exit
192197
echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
@@ -230,12 +235,12 @@ bisect_state() {
230235
case "$#,$state" in
231236
0,*)
232237
die "$(gettext "Please call 'bisect_state' with at least one argument.")" ;;
233-
1,bad|1,good|1,skip)
238+
1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
234239
rev=$(git rev-parse --verify $(bisect_head)) ||
235240
die "$(gettext "Bad rev input: $(bisect_head)")"
236241
bisect_write "$state" "$rev"
237242
check_expected_revs "$rev" ;;
238-
2,bad|*,good|*,skip)
243+
2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
239244
shift
240245
hash_list=''
241246
for rev in "$@"
@@ -249,8 +254,8 @@ bisect_state() {
249254
bisect_write "$state" "$rev"
250255
done
251256
check_expected_revs $hash_list ;;
252-
*,bad)
253-
die "$(gettext "'git bisect bad' can take only one argument.")" ;;
257+
*,"$TERM_BAD")
258+
die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
254259
*)
255260
usage ;;
256261
esac
@@ -259,21 +264,21 @@ bisect_state() {
259264

260265
bisect_next_check() {
261266
missing_good= missing_bad=
262-
git show-ref -q --verify refs/bisect/bad || missing_bad=t
263-
test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
267+
git show-ref -q --verify refs/bisect/$TERM_BAD || missing_bad=t
268+
test -n "$(git for-each-ref "refs/bisect/$TERM_GOOD-*")" || missing_good=t
264269

265270
case "$missing_good,$missing_bad,$1" in
266271
,,*)
267-
: have both good and bad - ok
272+
: have both $TERM_GOOD and $TERM_BAD - ok
268273
;;
269274
*,)
270275
# do not have both but not asked to fail - just report.
271276
false
272277
;;
273-
t,,good)
278+
t,,"$TERM_GOOD")
274279
# have bad but not good. we could bisect although
275280
# this is less optimum.
276-
gettextln "Warning: bisecting only with a bad commit." >&2
281+
eval_gettextln "Warning: bisecting only with a \$TERM_BAD commit." >&2
277282
if test -t 0
278283
then
279284
# TRANSLATORS: Make sure to include [Y] and [n] in your
@@ -283,7 +288,7 @@ bisect_next_check() {
283288
read yesno
284289
case "$yesno" in [Nn]*) exit 1 ;; esac
285290
fi
286-
: bisect without good...
291+
: bisect without $TERM_GOOD...
287292
;;
288293
*)
289294

@@ -307,7 +312,7 @@ bisect_auto_next() {
307312
bisect_next() {
308313
case "$#" in 0) ;; *) usage ;; esac
309314
bisect_autostart
310-
bisect_next_check good
315+
bisect_next_check $TERM_GOOD
311316

312317
# Perform all bisection computation, display and checkout
313318
git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout)
@@ -316,18 +321,18 @@ bisect_next() {
316321
# Check if we should exit because bisection is finished
317322
if test $res -eq 10
318323
then
319-
bad_rev=$(git show-ref --hash --verify refs/bisect/bad)
324+
bad_rev=$(git show-ref --hash --verify refs/bisect/$TERM_BAD)
320325
bad_commit=$(git show-branch $bad_rev)
321-
echo "# first bad commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
326+
echo "# first $TERM_BAD commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
322327
exit 0
323328
elif test $res -eq 2
324329
then
325330
echo "# only skipped commits left to test" >>"$GIT_DIR/BISECT_LOG"
326-
good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/good-*")
327-
for skipped in $(git rev-list refs/bisect/bad --not $good_revs)
331+
good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/$TERM_GOOD-*")
332+
for skipped in $(git rev-list refs/bisect/$TERM_BAD --not $good_revs)
328333
do
329334
skipped_commit=$(git show-branch $skipped)
330-
echo "# possible first bad commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
335+
echo "# possible first $TERM_BAD commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
331336
done
332337
exit $res
333338
fi
@@ -421,7 +426,7 @@ bisect_replay () {
421426
start)
422427
cmd="bisect_start $rev"
423428
eval "$cmd" ;;
424-
good|bad|skip)
429+
$TERM_GOOD|$TERM_BAD|skip)
425430
bisect_write "$command" "$rev" ;;
426431
*)
427432
die "$(gettext "?? what are you talking about?")" ;;
@@ -455,9 +460,9 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
455460
state='skip'
456461
elif [ $res -gt 0 ]
457462
then
458-
state='bad'
463+
state="$TERM_BAD"
459464
else
460-
state='good'
465+
state="$TERM_GOOD"
461466
fi
462467

463468
# We have to use a subshell because "bisect_state" can exit.
@@ -466,7 +471,7 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
466471

467472
cat "$GIT_DIR/BISECT_RUN"
468473

469-
if sane_grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
474+
if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
470475
>/dev/null
471476
then
472477
gettextln "bisect run cannot continue any more" >&2
@@ -480,7 +485,7 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
480485
exit $res
481486
fi
482487

483-
if sane_grep "is the first bad commit" "$GIT_DIR/BISECT_RUN" >/dev/null
488+
if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
484489
then
485490
gettextln "bisect run success"
486491
exit 0;

0 commit comments

Comments
 (0)