Merge branch 'mr/bisect-in-c-3'

Rewriting "git bisect" in C continues.

* mr/bisect-in-c-3:
  bisect--helper: retire `--bisect-autostart` subcommand
  bisect--helper: retire `--write-terms` subcommand
  bisect--helper: retire `--check-expected-revs` subcommand
  bisect--helper: reimplement `bisect_state` & `bisect_head` shell functions in C
  bisect--helper: retire `--next-all` subcommand
  bisect--helper: retire `--bisect-clean-state` subcommand
  bisect--helper: finish porting `bisect_start()` to C
This commit is contained in:
Junio C Hamano 2020-11-09 14:06:25 -08:00
commit cfdc70b299
2 changed files with 117 additions and 151 deletions

View File

@ -20,9 +20,6 @@ static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
static const char * const git_bisect_helper_usage[] = {
N_("git bisect--helper --next-all"),
N_("git bisect--helper --write-terms <bad_term> <good_term>"),
N_("git bisect--helper --bisect-clean-state"),
N_("git bisect--helper --bisect-reset [<commit>]"),
N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
@ -32,7 +29,8 @@ static const char * const git_bisect_helper_usage[] = {
" [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
N_("git bisect--helper --bisect-next"),
N_("git bisect--helper --bisect-auto-next"),
N_("git bisect--helper --bisect-autostart"),
N_("git bisect--helper --bisect-state (bad|new) [<rev>]"),
N_("git bisect--helper --bisect-state (good|old) [<rev>...]"),
NULL
};
@ -85,6 +83,19 @@ static int one_of(const char *term, ...)
return res;
}
/*
* return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
* and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
* that indicate special success.
*/
static int is_bisect_success(enum bisect_error res)
{
return !res ||
res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND ||
res == BISECT_INTERNAL_SUCCESS_MERGE_BASE;
}
static int write_in_file(const char *path, const char *mode, const char *format, va_list args)
{
FILE *fp = NULL;
@ -174,30 +185,6 @@ static int write_terms(const char *bad, const char *good)
return res;
}
static int is_expected_rev(const char *expected_hex)
{
struct strbuf actual_hex = STRBUF_INIT;
int res = 0;
if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
strbuf_trim(&actual_hex);
res = !strcmp(actual_hex.buf, expected_hex);
}
strbuf_release(&actual_hex);
return res;
}
static void check_expected_revs(const char **revs, int rev_nr)
{
int i;
for (i = 0; i < rev_nr; i++) {
if (!is_expected_rev(revs[i])) {
unlink_or_warn(git_path_bisect_ancestors_ok());
unlink_or_warn(git_path_bisect_expected_rev());
}
}
}
static int bisect_reset(const char *commit)
{
struct strbuf branch = STRBUF_INIT;
@ -609,12 +596,13 @@ static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char
return bisect_next(terms, prefix);
}
static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
static enum bisect_error bisect_start(struct bisect_terms *terms, const char **argv, int argc)
{
int no_checkout = 0;
int first_parent_only = 0;
int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
int flags, pathspec_pos, res = 0;
int flags, pathspec_pos;
enum bisect_error res = BISECT_OK;
struct string_list revs = STRING_LIST_INIT_DUP;
struct string_list states = STRING_LIST_INIT_DUP;
struct strbuf start_head = STRBUF_INIT;
@ -753,14 +741,7 @@ static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
* Get rid of any old bisect state.
*/
if (bisect_clean_state())
return -1;
/*
* In case of mistaken revs or checkout error, or signals received,
* "bisect_auto_next" below may exit or misbehave.
* We have to trap this to be able to clean up using
* "bisect_clean_state".
*/
return BISECT_FAILED;
/*
* Write new start state
@ -777,7 +758,7 @@ static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
}
if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
UPDATE_REFS_MSG_ON_ERR)) {
res = -1;
res = BISECT_FAILED;
goto finish;
}
}
@ -789,25 +770,31 @@ static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
for (i = 0; i < states.nr; i++)
if (bisect_write(states.items[i].string,
revs.items[i].string, terms, 1)) {
res = -1;
res = BISECT_FAILED;
goto finish;
}
if (must_write_terms && write_terms(terms->term_bad,
terms->term_good)) {
res = -1;
res = BISECT_FAILED;
goto finish;
}
res = bisect_append_log_quoted(argv);
if (res)
res = -1;
res = BISECT_FAILED;
finish:
string_list_clear(&revs, 0);
string_list_clear(&states, 0);
strbuf_release(&start_head);
strbuf_release(&bisect_names);
if (res)
return res;
res = bisect_auto_next(terms, NULL);
if (!is_bisect_success(res))
bisect_clean_state();
return res;
}
@ -843,14 +830,84 @@ static int bisect_autostart(struct bisect_terms *terms)
return res;
}
static enum bisect_error bisect_state(struct bisect_terms *terms, const char **argv,
int argc)
{
const char *state;
int i, verify_expected = 1;
struct object_id oid, expected;
struct strbuf buf = STRBUF_INIT;
struct oid_array revs = OID_ARRAY_INIT;
if (!argc)
return error(_("Please call `--bisect-state` with at least one argument"));
if (bisect_autostart(terms))
return BISECT_FAILED;
state = argv[0];
if (check_and_set_terms(terms, state) ||
!one_of(state, terms->term_good, terms->term_bad, "skip", NULL))
return BISECT_FAILED;
argv++;
argc--;
if (argc > 1 && !strcmp(state, terms->term_bad))
return error(_("'git bisect %s' can take only one argument."), terms->term_bad);
if (argc == 0) {
const char *head = "BISECT_HEAD";
enum get_oid_result res_head = get_oid(head, &oid);
if (res_head == MISSING_OBJECT) {
head = "HEAD";
res_head = get_oid(head, &oid);
}
if (res_head)
error(_("Bad rev input: %s"), head);
oid_array_append(&revs, &oid);
}
/*
* All input revs must be checked before executing bisect_write()
* to discard junk revs.
*/
for (; argc; argc--, argv++) {
if (get_oid(*argv, &oid)){
error(_("Bad rev input: %s"), *argv);
oid_array_clear(&revs);
return BISECT_FAILED;
}
oid_array_append(&revs, &oid);
}
if (strbuf_read_file(&buf, git_path_bisect_expected_rev(), 0) < the_hash_algo->hexsz ||
get_oid_hex(buf.buf, &expected) < 0)
verify_expected = 0; /* Ignore invalid file contents */
strbuf_release(&buf);
for (i = 0; i < revs.nr; i++) {
if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0)) {
oid_array_clear(&revs);
return BISECT_FAILED;
}
if (verify_expected && !oideq(&revs.oid[i], &expected)) {
unlink_or_warn(git_path_bisect_ancestors_ok());
unlink_or_warn(git_path_bisect_expected_rev());
verify_expected = 0;
}
}
oid_array_clear(&revs);
return bisect_auto_next(terms, NULL);
}
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
{
enum {
NEXT_ALL = 1,
WRITE_TERMS,
BISECT_CLEAN_STATE,
CHECK_EXPECTED_REVS,
BISECT_RESET,
BISECT_RESET = 1,
BISECT_WRITE,
CHECK_AND_SET_TERMS,
BISECT_NEXT_CHECK,
@ -858,18 +915,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
BISECT_START,
BISECT_AUTOSTART,
BISECT_NEXT,
BISECT_AUTO_NEXT
BISECT_AUTO_NEXT,
BISECT_STATE
} cmdmode = 0;
int res = 0, nolog = 0;
struct option options[] = {
OPT_CMDMODE(0, "next-all", &cmdmode,
N_("perform 'git bisect next'"), NEXT_ALL),
OPT_CMDMODE(0, "write-terms", &cmdmode,
N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
N_("check for expected revs"), CHECK_EXPECTED_REVS),
OPT_CMDMODE(0, "bisect-reset", &cmdmode,
N_("reset the bisection state"), BISECT_RESET),
OPT_CMDMODE(0, "bisect-write", &cmdmode,
@ -886,8 +936,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
N_("find the next bisection commit"), BISECT_NEXT),
OPT_CMDMODE(0, "bisect-auto-next", &cmdmode,
N_("verify the next bisection state then checkout the next bisection commit"), BISECT_AUTO_NEXT),
OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
OPT_CMDMODE(0, "bisect-state", &cmdmode,
N_("mark the state of ref (or refs)"), BISECT_STATE),
OPT_BOOL(0, "no-log", &nolog,
N_("no log for BISECT_WRITE")),
OPT_END()
@ -902,20 +952,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
usage_with_options(git_bisect_helper_usage, options);
switch (cmdmode) {
case NEXT_ALL:
res = bisect_next_all(the_repository, prefix);
break;
case WRITE_TERMS:
if (argc != 2)
return error(_("--write-terms requires two arguments"));
return write_terms(argv[0], argv[1]);
case BISECT_CLEAN_STATE:
if (argc != 0)
return error(_("--bisect-clean-state requires no arguments"));
return bisect_clean_state();
case CHECK_EXPECTED_REVS:
check_expected_revs(argv, argc);
return 0;
case BISECT_RESET:
if (argc > 1)
return error(_("--bisect-reset requires either no argument or a commit"));
@ -959,11 +995,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
get_terms(&terms);
res = bisect_auto_next(&terms, prefix);
break;
case BISECT_AUTOSTART:
if (argc)
return error(_("--bisect-autostart does not accept arguments"));
case BISECT_STATE:
set_terms(&terms, "bad", "good");
res = bisect_autostart(&terms);
get_terms(&terms);
res = bisect_state(&terms, argv, argc);
break;
default:
BUG("unknown subcommand %d", cmdmode);

View File

@ -39,37 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
TERM_BAD=bad
TERM_GOOD=good
bisect_head()
{
if git rev-parse --verify -q BISECT_HEAD > /dev/null
then
echo BISECT_HEAD
else
echo HEAD
fi
}
bisect_start() {
git bisect--helper --bisect-start $@ || exit
#
# Change state.
# In case of mistaken revs or checkout error, or signals received,
# "bisect_auto_next" below may exit or misbehave.
# We have to trap this to be able to clean up using
# "bisect_clean_state".
#
trap 'git bisect--helper --bisect-clean-state' 0
trap 'exit 255' 1 2 3 15
#
# Check if we can proceed to the next bisect state.
#
git bisect--helper --bisect-auto-next || exit
trap '-' 0
}
bisect_skip() {
all=''
for arg in "$@"
@ -82,43 +51,7 @@ bisect_skip() {
esac
all="$all $revs"
done
eval bisect_state 'skip' $all
}
bisect_state() {
git bisect--helper --bisect-autostart || exit
state=$1
git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
get_terms
case "$#,$state" in
0,*)
die "Please call 'bisect_state' with at least one argument." ;;
1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
bisected_head=$(bisect_head)
rev=$(git rev-parse --verify "$bisected_head") ||
die "$(eval_gettext "Bad rev input: \$bisected_head")"
git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
git bisect--helper --check-expected-revs "$rev" ;;
2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
shift
hash_list=''
for rev in "$@"
do
sha=$(git rev-parse --verify "$rev^{commit}") ||
die "$(eval_gettext "Bad rev input: \$rev")"
hash_list="$hash_list $sha"
done
for rev in $hash_list
do
git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
done
git bisect--helper --check-expected-revs $hash_list ;;
*,"$TERM_BAD")
die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
*)
usage ;;
esac
git bisect--helper --bisect-auto-next
eval git bisect--helper --bisect-state 'skip' $all
}
bisect_visualize() {
@ -163,8 +96,7 @@ bisect_replay () {
get_terms
case "$command" in
start)
cmd="bisect_start $rev $tail"
eval "$cmd" ;;
eval "git bisect--helper --bisect-start $rev $tail" ;;
"$TERM_GOOD"|"$TERM_BAD"|skip)
git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
terms)
@ -209,8 +141,7 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
state="$TERM_GOOD"
fi
# We have to use a subshell because "bisect_state" can exit.
( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
git bisect--helper --bisect-state $state >"$GIT_DIR/BISECT_RUN"
res=$?
cat "$GIT_DIR/BISECT_RUN"
@ -225,7 +156,7 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
if [ $res -ne 0 ]
then
eval_gettextln "bisect run failed:
'bisect_state \$state' exited with error code \$res" >&2
'bisect-state \$state' exited with error code \$res" >&2
exit $res
fi
@ -264,9 +195,9 @@ case "$#" in
help)
git bisect -h ;;
start)
bisect_start "$@" ;;
git bisect--helper --bisect-start "$@" ;;
bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
bisect_state "$cmd" "$@" ;;
git bisect--helper --bisect-state "$cmd" "$@" ;;
skip)
bisect_skip "$@" ;;
next)