Merge branch 'pb/bisect-helper'

An early part of piece-by-piece rewrite of "git bisect".

* pb/bisect-helper:
  bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C
  t6030: explicitly test for bisection cleanup
  bisect--helper: `bisect_clean_state` shell function in C
  bisect--helper: `write_terms` shell function in C
  bisect--helper: rewrite `check_term_format` shell function in C
  bisect--helper: use OPT_CMDMODE instead of OPT_BOOL
This commit is contained in:
Junio C Hamano 2017-11-06 14:24:23 +09:00
commit 5faa27ab05
5 changed files with 204 additions and 89 deletions

View File

@ -433,7 +433,12 @@ static int read_bisect_refs(void)
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
static GIT_PATH_FUNC(git_path_head_name, "head-name")
static void read_bisect_paths(struct argv_array *array)
{
@ -1044,3 +1049,40 @@ int estimate_bisect_steps(int all)
return (e < 3 * x) ? n : n - 1;
}
static int mark_for_removal(const char *refname, const struct object_id *oid,
int flag, void *cb_data)
{
struct string_list *refs = cb_data;
char *ref = xstrfmt("refs/bisect%s", refname);
string_list_append(refs, ref);
return 0;
}
int bisect_clean_state(void)
{
int result = 0;
/* There may be some refs packed during bisection */
struct string_list refs_for_removal = STRING_LIST_INIT_NODUP;
for_each_ref_in("refs/bisect", mark_for_removal, (void *) &refs_for_removal);
string_list_append(&refs_for_removal, xstrdup("BISECT_HEAD"));
result = delete_refs("bisect: remove", &refs_for_removal, REF_NODEREF);
refs_for_removal.strdup_strings = 1;
string_list_clear(&refs_for_removal, 0);
unlink_or_warn(git_path_bisect_expected_rev());
unlink_or_warn(git_path_bisect_ancestors_ok());
unlink_or_warn(git_path_bisect_log());
unlink_or_warn(git_path_bisect_names());
unlink_or_warn(git_path_bisect_run());
unlink_or_warn(git_path_bisect_terms());
/* Cleanup head-name if it got left by an old version of git-bisect */
unlink_or_warn(git_path_head_name());
/*
* Cleanup BISECT_START last to support the --no-checkout option
* introduced in the commit 4796e823a.
*/
unlink_or_warn(git_path_bisect_start());
return result;
}

View File

@ -28,4 +28,6 @@ extern int estimate_bisect_steps(int all);
extern void read_bisect_terms(const char **bad, const char **good);
extern int bisect_clean_state(void);
#endif

View File

@ -2,19 +2,128 @@
#include "cache.h"
#include "parse-options.h"
#include "bisect.h"
#include "refs.h"
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
static const char * const git_bisect_helper_usage[] = {
N_("git bisect--helper --next-all [--no-checkout]"),
N_("git bisect--helper --write-terms <bad_term> <good_term>"),
N_("git bisect--helper --bisect-clean-state"),
NULL
};
/*
* Check whether the string `term` belongs to the set of strings
* included in the variable arguments.
*/
LAST_ARG_MUST_BE_NULL
static int one_of(const char *term, ...)
{
int res = 0;
va_list matches;
const char *match;
va_start(matches, term);
while (!res && (match = va_arg(matches, const char *)))
res = !strcmp(term, match);
va_end(matches);
return res;
}
static int check_term_format(const char *term, const char *orig_term)
{
int res;
char *new_term = xstrfmt("refs/bisect/%s", term);
res = check_refname_format(new_term, 0);
free(new_term);
if (res)
return error(_("'%s' is not a valid term"), term);
if (one_of(term, "help", "start", "skip", "next", "reset",
"visualize", "replay", "log", "run", "terms", NULL))
return error(_("can't use the builtin command '%s' as a term"), term);
/*
* In theory, nothing prevents swapping completely good and bad,
* but this situation could be confusing and hasn't been tested
* enough. Forbid it for now.
*/
if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
(strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
return error(_("can't change the meaning of the term '%s'"), term);
return 0;
}
static int write_terms(const char *bad, const char *good)
{
FILE *fp = NULL;
int res;
if (!strcmp(bad, good))
return error(_("please use two different terms"));
if (check_term_format(bad, "bad") || check_term_format(good, "good"))
return -1;
fp = fopen(git_path_bisect_terms(), "w");
if (!fp)
return error_errno(_("could not open the file BISECT_TERMS"));
res = fprintf(fp, "%s\n%s\n", bad, good);
res |= fclose(fp);
return (res < 0) ? -1 : 0;
}
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());
}
}
}
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
{
int next_all = 0;
enum {
NEXT_ALL = 1,
WRITE_TERMS,
BISECT_CLEAN_STATE,
CHECK_EXPECTED_REVS
} cmdmode = 0;
int no_checkout = 0;
struct option options[] = {
OPT_BOOL(0, "next-all", &next_all,
N_("perform 'git bisect next'")),
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_BOOL(0, "no-checkout", &no_checkout,
N_("update BISECT_HEAD instead of checking out the current commit")),
OPT_END()
@ -23,9 +132,25 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options,
git_bisect_helper_usage, 0);
if (!next_all)
if (!cmdmode)
usage_with_options(git_bisect_helper_usage, options);
/* next-all */
return bisect_next_all(prefix, no_checkout);
switch (cmdmode) {
case NEXT_ALL:
return bisect_next_all(prefix, no_checkout);
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;
default:
return error("BUG: unknown subcommand '%d'", cmdmode);
}
return 0;
}

View File

@ -186,7 +186,7 @@ bisect_start() {
#
# Get rid of any old bisect state.
#
bisect_clean_state || exit
git bisect--helper --bisect-clean-state || exit
#
# Change state.
@ -195,7 +195,7 @@ bisect_start() {
# We have to trap this to be able to clean up using
# "bisect_clean_state".
#
trap 'bisect_clean_state' 0
trap 'git bisect--helper --bisect-clean-state' 0
trap 'exit 255' 1 2 3 15
#
@ -209,7 +209,7 @@ bisect_start() {
eval "$eval true" &&
if test $must_write_terms -eq 1
then
write_terms "$TERM_BAD" "$TERM_GOOD"
git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
fi &&
echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit
#
@ -237,22 +237,6 @@ bisect_write() {
test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
}
is_expected_rev() {
test -f "$GIT_DIR/BISECT_EXPECTED_REV" &&
test "$1" = $(cat "$GIT_DIR/BISECT_EXPECTED_REV")
}
check_expected_revs() {
for _rev in "$@"; do
if ! is_expected_rev "$_rev"
then
rm -f "$GIT_DIR/BISECT_ANCESTORS_OK"
rm -f "$GIT_DIR/BISECT_EXPECTED_REV"
return
fi
done
}
bisect_skip() {
all=''
for arg in "$@"
@ -280,7 +264,7 @@ bisect_state() {
rev=$(git rev-parse --verify "$bisected_head") ||
die "$(eval_gettext "Bad rev input: \$bisected_head")"
bisect_write "$state" "$rev"
check_expected_revs "$rev" ;;
git bisect--helper --check-expected-revs "$rev" ;;
2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
shift
hash_list=''
@ -294,7 +278,7 @@ bisect_state() {
do
bisect_write "$state" "$rev"
done
check_expected_revs $hash_list ;;
git bisect--helper --check-expected-revs $hash_list ;;
*,"$TERM_BAD")
die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
*)
@ -430,27 +414,7 @@ bisect_reset() {
die "$(eval_gettext "Could not check out original HEAD '\$branch'.
Try 'git bisect reset <commit>'.")"
fi
bisect_clean_state
}
bisect_clean_state() {
# There may be some refs packed during bisection.
git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* |
while read ref hash
do
git update-ref -d $ref $hash || exit
done
rm -f "$GIT_DIR/BISECT_EXPECTED_REV" &&
rm -f "$GIT_DIR/BISECT_ANCESTORS_OK" &&
rm -f "$GIT_DIR/BISECT_LOG" &&
rm -f "$GIT_DIR/BISECT_NAMES" &&
rm -f "$GIT_DIR/BISECT_RUN" &&
rm -f "$GIT_DIR/BISECT_TERMS" &&
# Cleanup head-name if it got left by an old version of git-bisect
rm -f "$GIT_DIR/head-name" &&
git update-ref -d --no-deref BISECT_HEAD &&
# clean up BISECT_START last
rm -f "$GIT_DIR/BISECT_START"
git bisect--helper --bisect-clean-state || exit
}
bisect_replay () {
@ -557,45 +521,6 @@ get_terms () {
fi
}
write_terms () {
TERM_BAD=$1
TERM_GOOD=$2
if test "$TERM_BAD" = "$TERM_GOOD"
then
die "$(gettext "please use two different terms")"
fi
check_term_format "$TERM_BAD" bad
check_term_format "$TERM_GOOD" good
printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS"
}
check_term_format () {
term=$1
git check-ref-format refs/bisect/"$term" ||
die "$(eval_gettext "'\$term' is not a valid term")"
case "$term" in
help|start|terms|skip|next|reset|visualize|replay|log|run)
die "$(eval_gettext "can't use the builtin command '\$term' as a term")"
;;
bad|new)
if test "$2" != bad
then
# In theory, nothing prevents swapping
# completely good and bad, but this situation
# could be confusing and hasn't been tested
# enough. Forbid it for now.
die "$(eval_gettext "can't change the meaning of term '\$term'")"
fi
;;
good|old)
if test "$2" != good
then
die "$(eval_gettext "can't change the meaning of term '\$term'")"
fi
;;
esac
}
check_and_set_terms () {
cmd="$1"
case "$cmd" in
@ -609,13 +534,17 @@ check_and_set_terms () {
bad|good)
if ! test -s "$GIT_DIR/BISECT_TERMS"
then
write_terms bad good
TERM_BAD=bad
TERM_GOOD=good
git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
fi
;;
new|old)
if ! test -s "$GIT_DIR/BISECT_TERMS"
then
write_terms new old
TERM_BAD=new
TERM_GOOD=old
git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
fi
;;
esac ;;

View File

@ -894,4 +894,21 @@ test_expect_success 'bisect start takes options and revs in any order' '
test_cmp expected actual
'
test_expect_success 'git bisect reset cleans bisection state properly' '
git bisect reset &&
git bisect start &&
git bisect good $HASH1 &&
git bisect bad $HASH4 &&
git bisect reset &&
test -z "$(git for-each-ref "refs/bisect/*")" &&
test_path_is_missing "$GIT_DIR/BISECT_EXPECTED_REV" &&
test_path_is_missing "$GIT_DIR/BISECT_ANCESTORS_OK" &&
test_path_is_missing "$GIT_DIR/BISECT_LOG" &&
test_path_is_missing "$GIT_DIR/BISECT_RUN" &&
test_path_is_missing "$GIT_DIR/BISECT_TERMS" &&
test_path_is_missing "$GIT_DIR/head-name" &&
test_path_is_missing "$GIT_DIR/BISECT_HEAD" &&
test_path_is_missing "$GIT_DIR/BISECT_START"
'
test_done