Skip to content

Commit 0a4cb1f

Browse files
committed
Merge branch 'mr/bisect-in-c-4'
Rewrite of "git bisect" in C continues. * mr/bisect-in-c-4: bisect--helper: retire `--bisect-next-check` subcommand bisect--helper: reimplement `bisect_run` shell function in C bisect--helper: reimplement `bisect_visualize()` shell function in C run-command: make `exists_in_PATH()` non-static t6030-bisect-porcelain: add test for bisect visualize t6030-bisect-porcelain: add tests to control bisect run exit cases
2 parents 57e4a7b + 911aba1 commit 0a4cb1f

File tree

5 files changed

+186
-95
lines changed

5 files changed

+186
-95
lines changed

builtin/bisect--helper.c

Lines changed: 152 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
1818
static GIT_PATH_FUNC(git_path_head_name, "head-name")
1919
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
2020
static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
21+
static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
2122

2223
static const char * const git_bisect_helper_usage[] = {
2324
N_("git bisect--helper --bisect-reset [<commit>]"),
24-
N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
2525
N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
2626
N_("git bisect--helper --bisect-start [--term-{new,bad}=<term> --term-{old,good}=<term>]"
2727
" [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
@@ -30,6 +30,8 @@ static const char * const git_bisect_helper_usage[] = {
3030
N_("git bisect--helper --bisect-state (good|old) [<rev>...]"),
3131
N_("git bisect--helper --bisect-replay <filename>"),
3232
N_("git bisect--helper --bisect-skip [(<rev>|<range>)...]"),
33+
N_("git bisect--helper --bisect-visualize"),
34+
N_("git bisect--helper --bisect-run <cmd>..."),
3335
NULL
3436
};
3537

@@ -143,6 +145,19 @@ static int append_to_file(const char *path, const char *format, ...)
143145
return res;
144146
}
145147

148+
static int print_file_to_stdout(const char *path)
149+
{
150+
int fd = open(path, O_RDONLY);
151+
int ret = 0;
152+
153+
if (fd < 0)
154+
return error_errno(_("cannot open file '%s' for reading"), path);
155+
if (copy_fd(fd, 1) < 0)
156+
ret = error_errno(_("failed to read '%s'"), path);
157+
close(fd);
158+
return ret;
159+
}
160+
146161
static int check_term_format(const char *term, const char *orig_term)
147162
{
148163
int res;
@@ -1036,6 +1051,125 @@ static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar
10361051
return res;
10371052
}
10381053

1054+
static int bisect_visualize(struct bisect_terms *terms, const char **argv, int argc)
1055+
{
1056+
struct strvec args = STRVEC_INIT;
1057+
int flags = RUN_COMMAND_NO_STDIN, res = 0;
1058+
struct strbuf sb = STRBUF_INIT;
1059+
1060+
if (bisect_next_check(terms, NULL) != 0)
1061+
return BISECT_FAILED;
1062+
1063+
if (!argc) {
1064+
if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
1065+
getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
1066+
strvec_push(&args, "gitk");
1067+
} else {
1068+
strvec_push(&args, "log");
1069+
flags |= RUN_GIT_CMD;
1070+
}
1071+
} else {
1072+
if (argv[0][0] == '-') {
1073+
strvec_push(&args, "log");
1074+
flags |= RUN_GIT_CMD;
1075+
} else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
1076+
flags |= RUN_GIT_CMD;
1077+
1078+
strvec_pushv(&args, argv);
1079+
}
1080+
1081+
strvec_pushl(&args, "--bisect", "--", NULL);
1082+
1083+
strbuf_read_file(&sb, git_path_bisect_names(), 0);
1084+
sq_dequote_to_strvec(sb.buf, &args);
1085+
strbuf_release(&sb);
1086+
1087+
res = run_command_v_opt(args.v, flags);
1088+
strvec_clear(&args);
1089+
return res;
1090+
}
1091+
1092+
static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
1093+
{
1094+
int res = BISECT_OK;
1095+
struct strbuf command = STRBUF_INIT;
1096+
struct strvec args = STRVEC_INIT;
1097+
struct strvec run_args = STRVEC_INIT;
1098+
const char *new_state;
1099+
int temporary_stdout_fd, saved_stdout;
1100+
1101+
if (bisect_next_check(terms, NULL))
1102+
return BISECT_FAILED;
1103+
1104+
if (argc)
1105+
sq_quote_argv(&command, argv);
1106+
else {
1107+
error(_("bisect run failed: no command provided."));
1108+
return BISECT_FAILED;
1109+
}
1110+
1111+
strvec_push(&run_args, command.buf);
1112+
1113+
while (1) {
1114+
strvec_clear(&args);
1115+
1116+
printf(_("running %s\n"), command.buf);
1117+
res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
1118+
1119+
if (res < 0 || 128 <= res) {
1120+
error(_("bisect run failed: exit code %d from"
1121+
" '%s' is < 0 or >= 128"), res, command.buf);
1122+
strbuf_release(&command);
1123+
return res;
1124+
}
1125+
1126+
if (res == 125)
1127+
new_state = "skip";
1128+
else if (!res)
1129+
new_state = terms->term_good;
1130+
else
1131+
new_state = terms->term_bad;
1132+
1133+
temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
1134+
1135+
if (temporary_stdout_fd < 0)
1136+
return error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
1137+
1138+
fflush(stdout);
1139+
saved_stdout = dup(1);
1140+
dup2(temporary_stdout_fd, 1);
1141+
1142+
res = bisect_state(terms, &new_state, 1);
1143+
1144+
fflush(stdout);
1145+
dup2(saved_stdout, 1);
1146+
close(saved_stdout);
1147+
close(temporary_stdout_fd);
1148+
1149+
print_file_to_stdout(git_path_bisect_run());
1150+
1151+
if (res == BISECT_ONLY_SKIPPED_LEFT)
1152+
error(_("bisect run cannot continue any more"));
1153+
else if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) {
1154+
printf(_("bisect run success"));
1155+
res = BISECT_OK;
1156+
} else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
1157+
printf(_("bisect found first bad commit"));
1158+
res = BISECT_OK;
1159+
} else if (res) {
1160+
error(_("bisect run failed:'git bisect--helper --bisect-state"
1161+
" %s' exited with error code %d"), args.v[0], res);
1162+
} else {
1163+
continue;
1164+
}
1165+
1166+
strbuf_release(&command);
1167+
strvec_clear(&args);
1168+
strvec_clear(&run_args);
1169+
return res;
1170+
}
1171+
}
1172+
10391173
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
10401174
{
10411175
enum {
@@ -1048,7 +1182,9 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
10481182
BISECT_STATE,
10491183
BISECT_LOG,
10501184
BISECT_REPLAY,
1051-
BISECT_SKIP
1185+
BISECT_SKIP,
1186+
BISECT_VISUALIZE,
1187+
BISECT_RUN,
10521188
} cmdmode = 0;
10531189
int res = 0, nolog = 0;
10541190
struct option options[] = {
@@ -1070,6 +1206,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
10701206
N_("replay the bisection process from the given file"), BISECT_REPLAY),
10711207
OPT_CMDMODE(0, "bisect-skip", &cmdmode,
10721208
N_("skip some commits for checkout"), BISECT_SKIP),
1209+
OPT_CMDMODE(0, "bisect-visualize", &cmdmode,
1210+
N_("visualize the bisection"), BISECT_VISUALIZE),
1211+
OPT_CMDMODE(0, "bisect-run", &cmdmode,
1212+
N_("use <cmd>... to automatically bisect."), BISECT_RUN),
10731213
OPT_BOOL(0, "no-log", &nolog,
10741214
N_("no log for BISECT_WRITE")),
10751215
OPT_END()
@@ -1089,12 +1229,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
10891229
return error(_("--bisect-reset requires either no argument or a commit"));
10901230
res = bisect_reset(argc ? argv[0] : NULL);
10911231
break;
1092-
case BISECT_NEXT_CHECK:
1093-
if (argc != 2 && argc != 3)
1094-
return error(_("--bisect-next-check requires 2 or 3 arguments"));
1095-
set_terms(&terms, argv[1], argv[0]);
1096-
res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
1097-
break;
10981232
case BISECT_TERMS:
10991233
if (argc > 1)
11001234
return error(_("--bisect-terms requires 0 or 1 argument"));
@@ -1131,6 +1265,16 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
11311265
get_terms(&terms);
11321266
res = bisect_skip(&terms, argv, argc);
11331267
break;
1268+
case BISECT_VISUALIZE:
1269+
get_terms(&terms);
1270+
res = bisect_visualize(&terms, argv, argc);
1271+
break;
1272+
case BISECT_RUN:
1273+
if (!argc)
1274+
return error(_("bisect run failed: no command provided."));
1275+
get_terms(&terms);
1276+
res = bisect_run(&terms, argv, argc);
1277+
break;
11341278
default:
11351279
BUG("unknown subcommand %d", cmdmode);
11361280
}

git-bisect.sh

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -37,89 +37,6 @@ OPTIONS_SPEC=
3737
TERM_BAD=bad
3838
TERM_GOOD=good
3939

40-
bisect_visualize() {
41-
git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
42-
43-
if test $# = 0
44-
then
45-
if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
46-
type gitk >/dev/null 2>&1
47-
then
48-
set gitk
49-
else
50-
set git log
51-
fi
52-
else
53-
case "$1" in
54-
git*|tig) ;;
55-
-*) set git log "$@" ;;
56-
*) set git "$@" ;;
57-
esac
58-
fi
59-
60-
eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
61-
}
62-
63-
bisect_run () {
64-
git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
65-
66-
test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
67-
68-
while true
69-
do
70-
command="$@"
71-
eval_gettextln "running \$command"
72-
"$@"
73-
res=$?
74-
75-
# Check for really bad run error.
76-
if [ $res -lt 0 -o $res -ge 128 ]
77-
then
78-
eval_gettextln "bisect run failed:
79-
exit code \$res from '\$command' is < 0 or >= 128" >&2
80-
exit $res
81-
fi
82-
83-
# Find current state depending on run success or failure.
84-
# A special exit code of 125 means cannot test.
85-
if [ $res -eq 125 ]
86-
then
87-
state='skip'
88-
elif [ $res -gt 0 ]
89-
then
90-
state="$TERM_BAD"
91-
else
92-
state="$TERM_GOOD"
93-
fi
94-
95-
git bisect--helper --bisect-state $state >"$GIT_DIR/BISECT_RUN"
96-
res=$?
97-
98-
cat "$GIT_DIR/BISECT_RUN"
99-
100-
if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
101-
>/dev/null
102-
then
103-
gettextln "bisect run cannot continue any more" >&2
104-
exit $res
105-
fi
106-
107-
if [ $res -ne 0 ]
108-
then
109-
eval_gettextln "bisect run failed:
110-
'bisect-state \$state' exited with error code \$res" >&2
111-
exit $res
112-
fi
113-
114-
if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
115-
then
116-
gettextln "bisect run success"
117-
exit 0;
118-
fi
119-
120-
done
121-
}
122-
12340
get_terms () {
12441
if test -s "$GIT_DIR/BISECT_TERMS"
12542
then
@@ -150,15 +67,15 @@ case "$#" in
15067
# Not sure we want "next" at the UI level anymore.
15168
git bisect--helper --bisect-next "$@" || exit ;;
15269
visualize|view)
153-
bisect_visualize "$@" ;;
70+
git bisect--helper --bisect-visualize "$@" || exit;;
15471
reset)
15572
git bisect--helper --bisect-reset "$@" ;;
15673
replay)
15774
git bisect--helper --bisect-replay "$@" || exit;;
15875
log)
15976
git bisect--helper --bisect-log || exit ;;
16077
run)
161-
bisect_run "$@" ;;
78+
git bisect--helper --bisect-run "$@" || exit;;
16279
terms)
16380
git bisect--helper --bisect-terms "$@" || exit;;
16481
*)

run-command.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ static char *locate_in_PATH(const char *file)
211211
return NULL;
212212
}
213213

214-
static int exists_in_PATH(const char *file)
214+
int exists_in_PATH(const char *command)
215215
{
216-
char *r = locate_in_PATH(file);
216+
char *r = locate_in_PATH(command);
217217
int found = r != NULL;
218218
free(r);
219219
return found;

run-command.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ void child_process_clear(struct child_process *);
190190

191191
int is_executable(const char *name);
192192

193+
/**
194+
* Check if the command exists on $PATH. This emulates the path search that
195+
* execvp would perform, without actually executing the command so it
196+
* can be used before fork() to prepare to run a command using
197+
* execve() or after execvp() to diagnose why it failed.
198+
*
199+
* The caller should ensure that command contains no directory separators.
200+
*
201+
* Returns 1 if it is found in $PATH or 0 if the command could not be found.
202+
*/
203+
int exists_in_PATH(const char *command);
204+
193205
/**
194206
* Start a sub-process. Takes a pointer to a `struct child_process`
195207
* that specifies the details and returns pipe FDs (if requested).

t/t6030-bisect-porcelain.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,4 +962,22 @@ test_expect_success 'bisect handles annotated tags' '
962962
grep "$bad is the first bad commit" output
963963
'
964964

965+
test_expect_success 'bisect run fails with exit code equals or greater than 128' '
966+
write_script test_script.sh <<-\EOF &&
967+
exit 128
968+
EOF
969+
test_must_fail git bisect run ./test_script.sh &&
970+
write_script test_script.sh <<-\EOF &&
971+
exit 255
972+
EOF
973+
test_must_fail git bisect run ./test_script.sh
974+
'
975+
976+
test_expect_success 'bisect visualize with a filename with dash and space' '
977+
echo "My test line" >>"./-hello 2" &&
978+
git add -- "./-hello 2" &&
979+
git commit --quiet -m "Add test line" -- "./-hello 2" &&
980+
git bisect visualize -p -- "-hello 2"
981+
'
982+
965983
test_done

0 commit comments

Comments
 (0)