* [PATCH 0/9] Resend of gitster/pb/bisect @ 2016-07-12 22:35 Pranit Bauva 2016-07-12 22:35 ` [PATCH 1/9] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (11 more replies) 0 siblings, 12 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git Hey Junio, A small mistake got unnoticed by me which Lars recently pointed out. The naming convention is "git_path_<name_of_file>" and underscore instead of spaces. Thanks! The interdiff is: diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index c2f3cee..88a1df8 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -7,7 +7,7 @@ #include "argv-array.h" #include "run-command.h" -static GIT_PATH_FUNC(git_path_bisect_write_terms, "BISECT_TERMS") +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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") @@ -100,7 +100,7 @@ static int write_terms(const char *bad, const char *good) if (check_term_format(bad, "bad") || check_term_format(good, "good")) return -1; - fp = fopen(git_path_bisect_write_terms(), "w"); + fp = fopen(git_path_bisect_terms(), "w"); if (!fp) return error_errno(_("could not open the file BISECT_TERMS")); @@ -134,7 +134,7 @@ static int bisect_clean_state(void) remove_path(git_path_bisect_log()); remove_path(git_path_bisect_names()); remove_path(git_path_bisect_run()); - remove_path(git_path_bisect_write_terms()); + remove_path(git_path_bisect_terms()); /* Cleanup head-name if it got left by an old version of git-bisect */ remove_path(git_path_head_name()); /* Pranit Bauva (9): bisect--helper: use OPT_CMDMODE instead of OPT_BOOL bisect: rewrite `check_term_format` shell function in C bisect--helper: `write_terms` shell function in C bisect--helper: `bisect_clean_state` shell function in C t6030: explicitly test for bisection cleanup wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() bisect--helper: `bisect_reset` shell function in C bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C bisect--helper: `bisect_write` shell function in C builtin/am.c | 20 +-- builtin/bisect--helper.c | 310 +++++++++++++++++++++++++++++++++++++++++++- cache.h | 3 + git-bisect.sh | 146 +++------------------ t/t6030-bisect-porcelain.sh | 17 +++ wrapper.c | 13 ++ 6 files changed, 355 insertions(+), 154 deletions(-) -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 1/9] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-12 22:35 ` [PATCH 2/9] bisect: rewrite `check_term_format` shell function in C Pranit Bauva ` (10 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git `--next-all` is meant to be used as a subcommand to support multiple "operation mode" though the current implementation does not contain any other subcommand along side with `--next-all` but further commits will include some more subcommands. Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3324229..8111c91 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -10,11 +10,11 @@ static const char * const git_bisect_helper_usage[] = { int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - int next_all = 0; + enum { NEXT_ALL = 1 } 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -23,9 +23,14 @@ 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); + default: + die("BUG: unknown subcommand '%d'", cmdmode); + } + return 0; } -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 2/9] bisect: rewrite `check_term_format` shell function in C 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva 2016-07-12 22:35 ` [PATCH 1/9] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-12 22:35 ` [PATCH 3/9] bisect--helper: `write_terms` " Pranit Bauva ` (9 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git Reimplement the `check_term_format` shell function in C and add a `--check-term-format` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-term-format` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other method/subcommand. For eg. In conversion of write_terms() of git-bisect.sh, the subcommand will be removed and instead check_term_format() will be called in its C implementation while a new subcommand will be introduced for write_terms(). Helped-by: Johannes Schindelein <Johannes.Schindelein@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 31 ++----------------------- 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 8111c91..3c748d1 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -2,19 +2,72 @@ #include "cache.h" #include "parse-options.h" #include "bisect.h" +#include "refs.h" static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), + N_("git bisect--helper --check-term-format <term> <orig_term>"), NULL }; +/* + * Check whether the string `term` belongs to the set of strings + * included in the variable arguments. + */ +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) +{ + struct strbuf new_term = STRBUF_INIT; + strbuf_addf(&new_term, "refs/bisect/%s", term); + + if (check_refname_format(new_term.buf, 0)) { + strbuf_release(&new_term); + return error(_("'%s' is not a valid term"), term); + } + strbuf_release(&new_term); + + if (one_of(term, "help", "start", "skip", "next", "reset", + "visualize", "replay", "log", "run", 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - enum { NEXT_ALL = 1 } cmdmode = 0; + enum { + NEXT_ALL = 1, + CHECK_TERM_FMT + } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), + OPT_CMDMODE(0, "check-term-format", &cmdmode, + N_("check format of the term"), CHECK_TERM_FMT), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -29,6 +82,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); + case CHECK_TERM_FMT: + if (argc != 2) + die(_("--check-term-format requires two arguments")); + return check_term_format(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 5d1cb00..7d7965d 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -564,38 +564,11 @@ write_terms () { then die "$(gettext "please use two different terms")" fi - check_term_format "$TERM_BAD" bad - check_term_format "$TERM_GOOD" good + git bisect--helper --check-term-format "$TERM_BAD" bad || exit + git bisect--helper --check-term-format "$TERM_GOOD" good || exit 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 -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 3/9] bisect--helper: `write_terms` shell function in C 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva 2016-07-12 22:35 ` [PATCH 1/9] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 2016-07-12 22:35 ` [PATCH 2/9] bisect: rewrite `check_term_format` shell function in C Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-12 22:35 ` [PATCH 4/9] bisect--helper: `bisect_clean_state` " Pranit Bauva ` (8 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git Reimplement the `write_terms` shell function in C and add a `write-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also remove the subcommand `--check-term-format` as it can now be called from inside the function write_terms() C implementation. Also `|| exit` is added when calling write-terms subcommand from git-bisect.sh so as to exit whenever there is an error. Using `--write-terms` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other method. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 36 +++++++++++++++++++++++++++++------- git-bisect.sh | 22 +++++++--------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3c748d1..bec63d6 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,9 +4,11 @@ #include "bisect.h" #include "refs.h" +static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") + static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), - N_("git bisect--helper --check-term-format <term> <orig_term>"), + N_("git bisect--helper --write-terms <bad_term> <good_term>"), NULL }; @@ -56,18 +58,38 @@ static int check_term_format(const char *term, const char *orig_term) return 0; } +static int write_terms(const char *bad, const char *good) +{ + FILE *fp; + 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); + fclose(fp); + return (res < 0) ? -1 : 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - CHECK_TERM_FMT + WRITE_TERMS } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), - OPT_CMDMODE(0, "check-term-format", &cmdmode, - N_("check format of the term"), CHECK_TERM_FMT), + OPT_CMDMODE(0, "write-terms", &cmdmode, + N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -82,10 +104,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); - case CHECK_TERM_FMT: + case WRITE_TERMS: if (argc != 2) - die(_("--check-term-format requires two arguments")); - return check_term_format(argv[0], argv[1]); + die(_("--write-terms requires two arguments")); + return write_terms(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 7d7965d..cd39bd0 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -210,7 +210,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" fi && echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit # @@ -557,18 +557,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 - git bisect--helper --check-term-format "$TERM_BAD" bad || exit - git bisect--helper --check-term-format "$TERM_GOOD" good || exit - printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS" -} - check_and_set_terms () { cmd="$1" case "$cmd" in @@ -582,13 +570,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 ;; -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 4/9] bisect--helper: `bisect_clean_state` shell function in C 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (2 preceding siblings ...) 2016-07-12 22:35 ` [PATCH 3/9] bisect--helper: `write_terms` " Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-12 22:35 ` [PATCH 5/9] t6030: explicitly test for bisection cleanup Pranit Bauva ` (7 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git Reimplement `bisect_clean_state` shell function in C and add a `bisect-clean-state` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-clean-state` subcommand is a measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by bisect_reset() and bisect_start(). Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 26 +++-------------------- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index bec63d6..3089433 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -3,12 +3,21 @@ #include "parse-options.h" #include "bisect.h" #include "refs.h" +#include "dir.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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") +static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") +static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") +static GIT_PATH_FUNC(git_path_head_name, "head-name") +static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") 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 }; @@ -78,11 +87,49 @@ static int write_terms(const char *bad, const char *good) return (res < 0) ? -1 : 0; } +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; +} + +static 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(&refs_for_removal); + refs_for_removal.strdup_strings = 1; + string_list_clear(&refs_for_removal, 0); + remove_path(git_path_bisect_expected_rev()); + remove_path(git_path_bisect_ancestors_ok()); + remove_path(git_path_bisect_log()); + remove_path(git_path_bisect_names()); + remove_path(git_path_bisect_run()); + remove_path(git_path_bisect_terms()); + /* Cleanup head-name if it got left by an old version of git-bisect */ + remove_path(git_path_head_name()); + /* + * Cleanup BISECT_START last to support the --no-checkout option + * introduced in the commit 4796e823a. + */ + remove_path(git_path_bisect_start()); + + return result; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - WRITE_TERMS + WRITE_TERMS, + BISECT_CLEAN_STATE } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -90,6 +137,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -108,6 +157,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 2) die(_("--write-terms requires two arguments")); return write_terms(argv[0], argv[1]); + case BISECT_CLEAN_STATE: + if (argc != 0) + die(_("--bisect-clean-state requires no arguments")); + return bisect_clean_state(); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index cd39bd0..bbc57d2 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -187,7 +187,7 @@ bisect_start() { # # Get rid of any old bisect state. # - bisect_clean_state || exit + git bisect--helper --bisect-clean-state || exit # # Change state. @@ -196,7 +196,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 # @@ -430,27 +430,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 () { -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 5/9] t6030: explicitly test for bisection cleanup 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (3 preceding siblings ...) 2016-07-12 22:35 ` [PATCH 4/9] bisect--helper: `bisect_clean_state` " Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-12 22:35 ` [PATCH 6/9] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva ` (6 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git Add test to explicitly check that 'git bisect reset' is working as expected. This is already covered implicitly by the test suite. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- I faced this problem while converting `bisect_clean_state` and the tests where showing breakages but it wasn't clear as to where exactly are they breaking. This will patch will help in that. Also I tested the test coverage of the test suite before this patch and it covers this (I did this by purposely changing names of files in git-bisect.sh and running the test suite). Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- t/t6030-bisect-porcelain.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index e74662b..a17f7a6 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -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 -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 6/9] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (4 preceding siblings ...) 2016-07-12 22:35 ` [PATCH 5/9] t6030: explicitly test for bisection cleanup Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-12 22:35 ` [PATCH 7/9] bisect--helper: `bisect_reset` shell function in C Pranit Bauva ` (5 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git is_empty_file() can help to refactor a lot of code. This will be very helpful in porting "git bisect" to C. Suggested-by: Torsten Bögershausen <tboegi@web.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/am.c | 20 ++------------------ cache.h | 3 +++ wrapper.c | 13 +++++++++++++ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 3dfe70b..6ee158f 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -30,22 +30,6 @@ #include "mailinfo.h" /** - * Returns 1 if the file is empty or does not exist, 0 otherwise. - */ -static int is_empty_file(const char *filename) -{ - struct stat st; - - if (stat(filename, &st) < 0) { - if (errno == ENOENT) - return 1; - die_errno(_("could not stat %s"), filename); - } - - return !st.st_size; -} - -/** * Returns the length of the first line of msg. */ static int linelen(const char *msg) @@ -1323,7 +1307,7 @@ static int parse_mail(struct am_state *state, const char *mail) goto finish; } - if (is_empty_file(am_path(state, "patch"))) { + if (is_empty_or_missing_file(am_path(state, "patch"))) { printf_ln(_("Patch is empty. Was it split wrong?")); die_user_resolve(state); } @@ -1911,7 +1895,7 @@ next: resume = 0; } - if (!is_empty_file(am_path(state, "rewritten"))) { + if (!is_empty_or_missing_file(am_path(state, "rewritten"))) { assert(state->rebasing); copy_notes_for_rebase(state); run_post_rewrite_hook(state); diff --git a/cache.h b/cache.h index 6049f86..91e2f81 100644 --- a/cache.h +++ b/cache.h @@ -1870,4 +1870,7 @@ void sleep_millisec(int millisec); */ void safe_create_dir(const char *dir, int share); +/* Return 1 if the file is empty or does not exists, 0 otherwise. */ +extern int is_empty_or_missing_file(const char *filename); + #endif /* CACHE_H */ diff --git a/wrapper.c b/wrapper.c index 5dc4e15..e70e4d1 100644 --- a/wrapper.c +++ b/wrapper.c @@ -696,3 +696,16 @@ void sleep_millisec(int millisec) { poll(NULL, 0, millisec); } + +int is_empty_or_missing_file(const char *filename) +{ + struct stat st; + + if (stat(filename, &st) < 0) { + if (errno == ENOENT) + return 1; + die_errno(_("could not stat %s"), filename); + } + + return !st.st_size; +} -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 7/9] bisect--helper: `bisect_reset` shell function in C 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (5 preceding siblings ...) 2016-07-12 22:35 ` [PATCH 6/9] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-12 22:35 ` [PATCH 8/9] bisect--helper: `is_expected_rev` & `check_expected_revs` " Pranit Bauva ` (4 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git Reimplement `bisect_reset` shell function in C and add a `--bisect-reset` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `bisect_reset` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired and will be called by some other method. Note: --bisect-clean-state subcommand has not been retired as there are still a function namely `bisect_start()` which still uses this subcommand. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 28 ++-------------------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3089433..636044a 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,6 +4,8 @@ #include "bisect.h" #include "refs.h" #include "dir.h" +#include "argv-array.h" +#include "run-command.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -13,11 +15,13 @@ static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") static GIT_PATH_FUNC(git_path_head_name, "head-name") static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") +static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD") 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"), + N_("git bisect--helper --bisect-reset [<commit>]"), NULL }; @@ -124,12 +128,47 @@ static int bisect_clean_state(void) return result; } +static int bisect_reset(const char *commit) +{ + struct strbuf branch = STRBUF_INIT; + + if (!commit) { + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { + printf("We are not bisecting.\n"); + return 0; + } + strbuf_rtrim(&branch); + } else { + struct object_id oid; + if (get_oid(commit, &oid)) + return error(_("'%s' is not a valid commit"), commit); + strbuf_addstr(&branch, commit); + } + + if (!file_exists(git_path_bisect_head())) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("Could not check out original HEAD '%s'. Try" + "'git bisect reset <commit>'."), branch.buf); + strbuf_release(&branch); + argv_array_clear(&argv); + return -1; + } + argv_array_clear(&argv); + } + + strbuf_release(&branch); + return bisect_clean_state(); +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, - BISECT_CLEAN_STATE + BISECT_CLEAN_STATE, + BISECT_RESET } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -139,6 +178,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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, "bisect-reset", &cmdmode, + N_("reset the bisection state"), BISECT_RESET), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -161,6 +202,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 0) die(_("--bisect-clean-state requires no arguments")); return bisect_clean_state(); + case BISECT_RESET: + if (argc > 1) + die(_("--bisect-reset requires either zero or one arguments")); + return bisect_reset(argc ? argv[0] : NULL); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index bbc57d2..18580b7 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -409,35 +409,11 @@ bisect_visualize() { eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES") } -bisect_reset() { - test -s "$GIT_DIR/BISECT_START" || { - gettextln "We are not bisecting." - return - } - case "$#" in - 0) branch=$(cat "$GIT_DIR/BISECT_START") ;; - 1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || { - invalid="$1" - die "$(eval_gettext "'\$invalid' is not a valid commit")" - } - branch="$1" ;; - *) - usage ;; - esac - - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- - then - die "$(eval_gettext "Could not check out original HEAD '\$branch'. -Try 'git bisect reset <commit>'.")" - fi - git bisect--helper --bisect-clean-state || exit -} - bisect_replay () { file="$1" test "$#" -eq 1 || die "$(gettext "No logfile given")" test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")" - bisect_reset + git bisect--helper --bisect-reset || exit while read git bisect command rev do test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue @@ -627,7 +603,7 @@ case "$#" in visualize|view) bisect_visualize "$@" ;; reset) - bisect_reset "$@" ;; + git bisect--helper --bisect-reset "$@" ;; replay) bisect_replay "$@" ;; log) -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 8/9] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (6 preceding siblings ...) 2016-07-12 22:35 ` [PATCH 7/9] bisect--helper: `bisect_reset` shell function in C Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-12 22:35 ` [PATCH 9/9] bisect--helper: `bisect_write` " Pranit Bauva ` (3 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git Reimplement `is_expected_rev` & `check_expected_revs` shell function in C and add a `--check-expected-revs` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--check-expected-revs` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired and will be called by some other method. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 33 ++++++++++++++++++++++++++++++++- git-bisect.sh | 20 ++------------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 636044a..88b5d0a 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -162,13 +162,40 @@ static int bisect_reset(const char *commit) return bisect_clean_state(); } +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) >= 0) { + strbuf_trim(&actual_hex); + res = !strcmp(actual_hex.buf, expected_hex); + } + strbuf_release(&actual_hex); + return res; +} + +static int check_expected_revs(const char **revs, int rev_nr) +{ + int i; + + for (i = 0; i < rev_nr; i++) { + if (!is_expected_rev(revs[i])) { + remove_path(git_path_bisect_ancestors_ok()); + remove_path(git_path_bisect_expected_rev()); + return 0; + } + } + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, BISECT_CLEAN_STATE, - BISECT_RESET + BISECT_RESET, + CHECK_EXPECTED_REVS } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -180,6 +207,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("cleanup the bisection state"), BISECT_CLEAN_STATE), OPT_CMDMODE(0, "bisect-reset", &cmdmode, N_("reset the bisection state"), BISECT_RESET), + 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() @@ -206,6 +235,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); return bisect_reset(argc ? argv[0] : NULL); + case CHECK_EXPECTED_REVS: + return check_expected_revs(argv, argc); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 18580b7..4f6545e 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -238,22 +238,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 $(bisect_head)) || die "$(gettext "Bad rev input: $(bisect_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.")" ;; *) -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH 9/9] bisect--helper: `bisect_write` shell function in C 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (7 preceding siblings ...) 2016-07-12 22:35 ` [PATCH 8/9] bisect--helper: `is_expected_rev` & `check_expected_revs` " Pranit Bauva @ 2016-07-12 22:35 ` Pranit Bauva 2016-07-13 7:47 ` [PATCH 0/9] Resend of gitster/pb/bisect Christian Couder ` (2 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-12 22:35 UTC (permalink / raw) To: gitster; +Cc: Pranit Bauva, larsxschneider, christian.couder, chriscool, git Reimplement the `bisect_write` shell function in C and add a `bisect-write` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--bisect-write` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. Note: bisect_write() uses two variables namely TERM_GOOD and TERM_BAD from the global shell script thus we need to pass it to the subcommand using the arguments. We then store them in a struct bisect_terms and pass the memory address around functions. This patch also introduces new methods namely bisect_state_init() and bisect_terms_release() for easy memory management for the struct bisect_terms. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 97 ++++++++++++++++++++++++++++++++++++++++++++---- git-bisect.sh | 25 ++----------- 2 files changed, 94 insertions(+), 28 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 88b5d0a..88a1df8 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -22,9 +22,27 @@ static const char * const git_bisect_helper_usage[] = { 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 <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), NULL }; +struct bisect_terms { + struct strbuf term_good; + struct strbuf term_bad; +}; + +static void bisect_terms_init(struct bisect_terms *terms) +{ + strbuf_init(&terms->term_good, 0); + strbuf_init(&terms->term_bad, 0); +} + +static void bisect_terms_release(struct bisect_terms *terms) +{ + strbuf_release(&terms->term_good); + strbuf_release(&terms->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -188,6 +206,52 @@ static int check_expected_revs(const char **revs, int rev_nr) return 0; } +static int bisect_write(const char *state, const char *rev, + const struct bisect_terms *terms, int nolog) +{ + struct strbuf tag = STRBUF_INIT; + struct strbuf commit_name = STRBUF_INIT; + struct object_id oid; + struct commit *commit; + struct pretty_print_context pp = {0}; + FILE *fp; + + if (!strcmp(state, terms->term_bad.buf)) + strbuf_addf(&tag, "refs/bisect/%s", state); + else if(one_of(state, terms->term_good.buf, "skip", NULL)) + strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev); + else + return error(_("Bad bisect_write argument: %s"), state); + + if (get_oid(rev, &oid)) { + strbuf_release(&tag); + return error(_("couldn't get the oid of the rev '%s'"), rev); + } + + if (update_ref(NULL, tag.buf, oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + strbuf_release(&tag); + return -1; + } + strbuf_release(&tag); + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) + return error_errno(_("couldn't open the file '%s'"), git_path_bisect_log()); + + commit = lookup_commit_reference(oid.hash); + format_commit_message(commit, "%s", &commit_name, &pp); + fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash), + commit_name.buf); + strbuf_release(&commit_name); + + if (!nolog) + fprintf(fp, "git bisect %s %s\n", state, rev); + + fclose(fp); + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -195,9 +259,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) WRITE_TERMS, BISECT_CLEAN_STATE, BISECT_RESET, - CHECK_EXPECTED_REVS + CHECK_EXPECTED_REVS, + BISECT_WRITE } cmdmode = 0; - int no_checkout = 0; + int no_checkout = 0, res = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), @@ -209,10 +274,14 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("reset the bisection state"), BISECT_RESET), OPT_CMDMODE(0, "check-expected-revs", &cmdmode, N_("check for expected revs"), CHECK_EXPECTED_REVS), + OPT_CMDMODE(0, "bisect-write", &cmdmode, + N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() }; + struct bisect_terms terms; + bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, git_bisect_helper_usage, 0); @@ -221,24 +290,38 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) usage_with_options(git_bisect_helper_usage, options); switch (cmdmode) { + int nolog; case NEXT_ALL: return bisect_next_all(prefix, no_checkout); case WRITE_TERMS: if (argc != 2) die(_("--write-terms requires two arguments")); - return write_terms(argv[0], argv[1]); + res = write_terms(argv[0], argv[1]); + break; case BISECT_CLEAN_STATE: if (argc != 0) die(_("--bisect-clean-state requires no arguments")); - return bisect_clean_state(); + res = bisect_clean_state(); + break; case BISECT_RESET: if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); - return bisect_reset(argc ? argv[0] : NULL); + res = bisect_reset(argc ? argv[0] : NULL); + break; case CHECK_EXPECTED_REVS: - return check_expected_revs(argv, argc); + res = check_expected_revs(argv, argc); + break; + case BISECT_WRITE: + if (argc != 4 && argc != 5) + die(_("--bisect-write requires either 4 or 5 arguments")); + nolog = (argc == 5) && !strcmp(argv[4], "nolog"); + strbuf_addstr(&terms.term_good, argv[2]); + strbuf_addstr(&terms.term_bad, argv[3]); + res = bisect_write(argv[0], argv[1], &terms, nolog); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } - return 0; + bisect_terms_release(&terms); + return res; } diff --git a/git-bisect.sh b/git-bisect.sh index 4f6545e..b9896a4 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -145,7 +145,7 @@ bisect_start() { 0) state=$TERM_BAD ; bad_seen=1 ;; *) state=$TERM_GOOD ;; esac - eval="$eval bisect_write '$state' '$rev' 'nolog' &&" + eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" done # # Verify HEAD. @@ -221,23 +221,6 @@ bisect_start() { trap '-' 0 } -bisect_write() { - state="$1" - rev="$2" - nolog="$3" - case "$state" in - "$TERM_BAD") - tag="$state" ;; - "$TERM_GOOD"|skip) - tag="$state"-"$rev" ;; - *) - die "$(eval_gettext "Bad bisect_write argument: \$state")" ;; - esac - git update-ref "refs/bisect/$tag" "$rev" || exit - echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG" - test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" -} - bisect_skip() { all='' for arg in "$@" @@ -263,7 +246,7 @@ bisect_state() { 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip) rev=$(git rev-parse --verify $(bisect_head)) || die "$(gettext "Bad rev input: $(bisect_head)")" - bisect_write "$state" "$rev" + 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 @@ -276,7 +259,7 @@ bisect_state() { done for rev in $hash_list do - bisect_write "$state" "$rev" + git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit done git bisect--helper --check-expected-revs $hash_list ;; *,"$TERM_BAD") @@ -413,7 +396,7 @@ bisect_replay () { cmd="bisect_start $rev" eval "$cmd" ;; "$TERM_GOOD"|"$TERM_BAD"|skip) - bisect_write "$command" "$rev" ;; + git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) bisect_terms $rev ;; *) -- 2.9.0 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH 0/9] Resend of gitster/pb/bisect 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (8 preceding siblings ...) 2016-07-12 22:35 ` [PATCH 9/9] bisect--helper: `bisect_write` " Pranit Bauva @ 2016-07-13 7:47 ` Christian Couder 2016-07-20 16:00 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 11 siblings, 0 replies; 320+ messages in thread From: Christian Couder @ 2016-07-13 7:47 UTC (permalink / raw) To: Pranit Bauva; +Cc: Junio C Hamano, Lars Schneider, Christian Couder, git Hi Pranit, On Wed, Jul 13, 2016 at 12:35 AM, Pranit Bauva <pranit.bauva@gmail.com> wrote: > Hey Junio, > > A small mistake got unnoticed by me which Lars recently pointed out. > The naming convention is "git_path_<name_of_file>" and underscore > instead of spaces. It's a good thing to resend when you find mistakes, but please use a version number for your patch series (like "PATCH v3" or something). Thanks, Christian. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH 0/9] Resend of gitster/pb/bisect 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (9 preceding siblings ...) 2016-07-13 7:47 ` [PATCH 0/9] Resend of gitster/pb/bisect Christian Couder @ 2016-07-20 16:00 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 16:00 UTC (permalink / raw) To: Junio C Hamano Cc: Pranit Bauva, Lars Schneider, Christian Couder, Christian Couder, Git List On Wed, Jul 13, 2016 at 4:05 AM, Pranit Bauva <pranit.bauva@gmail.com> wrote: > Hey Junio, > > A small mistake got unnoticed by me which Lars recently pointed out. > The naming convention is "git_path_<name_of_file>" and underscore > instead of spaces. > > Thanks! > > The interdiff is: > diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c > index c2f3cee..88a1df8 100644 > --- a/builtin/bisect--helper.c > +++ b/builtin/bisect--helper.c > @@ -7,7 +7,7 @@ > #include "argv-array.h" > #include "run-command.h" > > -static GIT_PATH_FUNC(git_path_bisect_write_terms, "BISECT_TERMS") > +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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") > @@ -100,7 +100,7 @@ static int write_terms(const char *bad, const char *good) > if (check_term_format(bad, "bad") || check_term_format(good, "good")) > return -1; > > - fp = fopen(git_path_bisect_write_terms(), "w"); > + fp = fopen(git_path_bisect_terms(), "w"); > if (!fp) > return error_errno(_("could not open the file BISECT_TERMS")); > > @@ -134,7 +134,7 @@ static int bisect_clean_state(void) > remove_path(git_path_bisect_log()); > remove_path(git_path_bisect_names()); > remove_path(git_path_bisect_run()); > - remove_path(git_path_bisect_write_terms()); > + remove_path(git_path_bisect_terms()); > /* Cleanup head-name if it got left by an old version of git-bisect */ > remove_path(git_path_head_name()); > /* > > > Pranit Bauva (9): > bisect--helper: use OPT_CMDMODE instead of OPT_BOOL > bisect: rewrite `check_term_format` shell function in C > bisect--helper: `write_terms` shell function in C > bisect--helper: `bisect_clean_state` shell function in C > t6030: explicitly test for bisection cleanup > wrapper: move is_empty_file() and rename it as > is_empty_or_missing_file() > bisect--helper: `bisect_reset` shell function in C > bisect--helper: `is_expected_rev` & `check_expected_revs` shell > function in C > bisect--helper: `bisect_write` shell function in C > > builtin/am.c | 20 +-- > builtin/bisect--helper.c | 310 +++++++++++++++++++++++++++++++++++++++++++- > cache.h | 3 + > git-bisect.sh | 146 +++------------------ > t/t6030-bisect-porcelain.sh | 17 +++ > wrapper.c | 13 ++ > 6 files changed, 355 insertions(+), 154 deletions(-) Could someone please look into this series and review so that Junio can merge this into next which is a vital part of my GSoC project? Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-07-12 22:35 [PATCH 0/9] Resend of gitster/pb/bisect Pranit Bauva ` (10 preceding siblings ...) 2016-07-20 16:00 ` Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 08/12] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C Pranit Bauva ` (11 more replies) 11 siblings, 12 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git `--next-all` is meant to be used as a subcommand to support multiple "operation mode" though the current implementation does not contain any other subcommand along side with `--next-all` but further commits will include some more subcommands. Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3324229..8111c91 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -10,11 +10,11 @@ static const char * const git_bisect_helper_usage[] = { int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - int next_all = 0; + enum { NEXT_ALL = 1 } 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -23,9 +23,14 @@ 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); + default: + die("BUG: unknown subcommand '%d'", cmdmode); + } + return 0; } -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 08/12] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 05/12] t6030: explicitly test for bisection cleanup Pranit Bauva ` (10 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement `is_expected_rev` & `check_expected_revs` shell function in C and add a `--check-expected-revs` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--check-expected-revs` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired and will be called by some other method. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 33 ++++++++++++++++++++++++++++++++- git-bisect.sh | 20 ++------------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index d125fd3..86bb334 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -162,13 +162,40 @@ static int bisect_reset(const char *commit) return bisect_clean_state(); } +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) >= 0) { + strbuf_trim(&actual_hex); + res = !strcmp(actual_hex.buf, expected_hex); + } + strbuf_release(&actual_hex); + return res; +} + +static int check_expected_revs(const char **revs, int rev_nr) +{ + int i; + + for (i = 0; i < rev_nr; i++) { + if (!is_expected_rev(revs[i])) { + remove_path(git_path_bisect_ancestors_ok()); + remove_path(git_path_bisect_expected_rev()); + return 0; + } + } + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, BISECT_CLEAN_STATE, - BISECT_RESET + BISECT_RESET, + CHECK_EXPECTED_REVS } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -180,6 +207,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("cleanup the bisection state"), BISECT_CLEAN_STATE), OPT_CMDMODE(0, "bisect-reset", &cmdmode, N_("reset the bisection state"), BISECT_RESET), + 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() @@ -206,6 +235,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); return bisect_reset(argc ? argv[0] : NULL); + case CHECK_EXPECTED_REVS: + return check_expected_revs(argv, argc); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 18580b7..4f6545e 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -238,22 +238,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 $(bisect_head)) || die "$(gettext "Bad rev input: $(bisect_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.")" ;; *) -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 05/12] t6030: explicitly test for bisection cleanup 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 08/12] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 11/12] bisect--helper: `bisect_next_check` shell function in C Pranit Bauva ` (9 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Add test to explicitly check that 'git bisect reset' is working as expected. This is already covered implicitly by the test suite. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- I faced this problem while converting `bisect_clean_state` and the tests where showing breakages but it wasn't clear as to where exactly are they breaking. This will patch will help in that. Also I tested the test coverage of the test suite before this patch and it covers this (I did this by purposely changing names of files in git-bisect.sh and running the test suite). --- t/t6030-bisect-porcelain.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index e74662b..a17f7a6 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -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 -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 11/12] bisect--helper: `bisect_next_check` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 08/12] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 05/12] t6030: explicitly test for bisection cleanup Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 10/12] bisect--helper: `check_and_set_terms` " Pranit Bauva ` (8 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement `bisect_next_check` shell function in C and add `bisect-next-check` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-next-check` is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. bisect_voc() is removed as it is redundant and does not serve any useful purpose. We are better off specifying "bad|new" "good|old" as and when we require in bisect_next_check(). Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 60 +++--------------------------------- 2 files changed, 82 insertions(+), 57 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index b9119e3..001096a 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -6,6 +6,7 @@ #include "dir.h" #include "argv-array.h" #include "run-command.h" +#include "prompt.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -24,6 +25,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), + N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), NULL }; @@ -292,6 +294,71 @@ static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) return 0; } +static int mark_good(const char *refname, const struct object_id *oid, + int flag, void *cb_data) +{ + int *m_good = (int *)cb_data; + *m_good = 0; + return 0; +} + +static int bisect_next_check(const struct bisect_terms *terms, + const char *current_term) +{ + int missing_good = 1, missing_bad = 1; + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); + char *good_glob = xstrfmt("%s*", terms->term_good.buf); + + if (ref_exists(bad_ref)) + missing_bad = 0; + free(bad_ref); + + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", + (void *) &missing_good); + free(good_glob); + + if (!missing_good && !missing_bad) + return 0; + + if (!current_term) + return -1; + + if (missing_good && !missing_bad && current_term && + !strcmp(current_term, terms->term_good.buf)) { + char *yesno; + /* + * have bad (or new) but not good (or old). We could bisect + * although this is less optimum. + */ + fprintf(stderr, "Warning: bisecting only with a %s commit\n", + terms->term_bad.buf); + if (!isatty(0)) + return 0; + /* + * TRANSLATORS: Make sure to include [Y] and [n] in your + * translation. The program will only accept English input + * at this point. + */ + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); + if (starts_with(yesno, "N") || starts_with(yesno, "n")) + return -1; + return 0; + } + if (!is_empty_or_missing_file(git_path_bisect_start())) + return error(_("You need to give me at least one good|old and " + "bad|new revision. You can use \"git bisect " + "bad|new\" and \"git bisect good|old\" for " + "that. \n")); + else + return error(_("You need to start by \"git bisect start\". " + "You then need to give me at least one good|" + "old and bad|new revision. You can use \"git " + "bisect bad|new\" and \"git bisect good|old\" " + " for that.\n")); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -301,7 +368,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_RESET, CHECK_EXPECTED_REVS, BISECT_WRITE, - CHECK_AND_SET_TERMS + CHECK_AND_SET_TERMS, + BISECT_NEXT_CHECK } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -319,6 +387,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), + OPT_CMDMODE(0, "bisect-next-check", &cmdmode, + N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -369,6 +439,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[2]); res = check_and_set_terms(&terms, argv[0]); break; + case BISECT_NEXT_CHECK: + if (argc != 2 && argc != 3) + die(_("--bisect-next-check requires 2 or 3 arguments")); + strbuf_addstr(&terms.term_good, argv[0]); + strbuf_addstr(&terms.term_bad, argv[1]); + res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index a41e69b..c2d6319 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -271,59 +271,14 @@ bisect_state() { bisect_auto_next } -bisect_next_check() { - missing_good= missing_bad= - git show-ref -q --verify refs/bisect/$TERM_BAD || missing_bad=t - test -n "$(git for-each-ref "refs/bisect/$TERM_GOOD-*")" || missing_good=t - - case "$missing_good,$missing_bad,$1" in - ,,*) - : have both $TERM_GOOD and $TERM_BAD - ok - ;; - *,) - # do not have both but not asked to fail - just report. - false - ;; - t,,"$TERM_GOOD") - # have bad (or new) but not good (or old). we could bisect although - # this is less optimum. - eval_gettextln "Warning: bisecting only with a \$TERM_BAD commit." >&2 - if test -t 0 - then - # TRANSLATORS: Make sure to include [Y] and [n] in your - # translation. The program will only accept English input - # at this point. - gettext "Are you sure [Y/n]? " >&2 - read yesno - case "$yesno" in [Nn]*) exit 1 ;; esac - fi - : bisect without $TERM_GOOD... - ;; - *) - bad_syn=$(bisect_voc bad) - good_syn=$(bisect_voc good) - if test -s "$GIT_DIR/BISECT_START" - then - - eval_gettextln "You need to give me at least one \$bad_syn and one \$good_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - else - eval_gettextln "You need to start by \"git bisect start\". -You then need to give me at least one \$good_syn and one \$bad_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - fi - exit 1 ;; - esac -} - bisect_auto_next() { - bisect_next_check && bisect_next || : + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || : } bisect_next() { case "$#" in 0) ;; *) usage ;; esac bisect_autostart - bisect_next_check $TERM_GOOD + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit # Perform all bisection computation, display and checkout git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout) @@ -355,7 +310,7 @@ bisect_next() { } bisect_visualize() { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit if test $# = 0 then @@ -409,7 +364,7 @@ bisect_replay () { } bisect_run () { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit while true do @@ -482,13 +437,6 @@ get_terms () { fi } -bisect_voc () { - case "$1" in - bad) echo "bad|new" ;; - good) echo "good|old" ;; - esac -} - bisect_terms () { get_terms if ! test -s "$GIT_DIR/BISECT_TERMS" -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 10/12] bisect--helper: `check_and_set_terms` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (2 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 11/12] bisect--helper: `bisect_next_check` shell function in C Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva ` (7 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement the `check_and_set_terms` shell function in C and add `check-and-set-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-and-set-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. check_and_set_terms() sets and receives two global variables namely TERM_GOOD and TERM_BAD in the shell script. Luckily the file BISECT_TERMS also contains the value of those variables so its appropriate to evoke the method get_terms() after calling the subcommand so that it retrieves the value of TERM_GOOD and TERM_BAD from the file BISECT_TERMS. The two global variables are passed as arguments to the subcommand. Also introduce bisect_terms_reset() to empty the contents of `term_good` and `term_bad` of `struct bisect_terms`. Also introduce set_terms() to copy the `term_good` and `term_bad` into `struct bisect_terms` and write it out to the file BISECT_TERMS. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 36 ++++----------------------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index d1d12f2..b9119e3 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -23,6 +23,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-clean-state"), N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), + N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), NULL }; @@ -43,6 +44,12 @@ static void bisect_terms_release(struct bisect_terms *terms) strbuf_release(&terms->term_bad); } +static void bisect_terms_reset(struct bisect_terms *term) +{ + strbuf_reset(&term->term_good); + strbuf_reset(&term->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -252,6 +259,39 @@ static int bisect_write(const char *state, const char *rev, return 0; } +static int set_terms(struct bisect_terms *terms, const char *bad, + const char *good) +{ + bisect_terms_reset(terms); + strbuf_addstr(&terms->term_good, good); + strbuf_addstr(&terms->term_bad, bad); + return write_terms(terms->term_bad.buf, terms->term_good.buf); +} + +static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) +{ + int no_term_file = is_empty_or_missing_file(git_path_bisect_terms()); + + if (one_of(cmd, "skip", "start", "terms", NULL)) + return 0; + + if (!no_term_file && + strcmp(cmd, terms->term_bad.buf) && + strcmp(cmd, terms->term_good.buf)) + return error(_("Invalid command: you're currently in a " + "'%s' '%s' bisect"), terms->term_bad.buf, + terms->term_good.buf); + + if (no_term_file) { + if (one_of(cmd, "bad", "good", NULL)) + return set_terms(terms, "bad", "good"); + if (one_of(cmd, "new", "old", NULL)) + return set_terms(terms, "new", "old"); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -260,7 +300,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_CLEAN_STATE, BISECT_RESET, CHECK_EXPECTED_REVS, - BISECT_WRITE + BISECT_WRITE, + CHECK_AND_SET_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -276,6 +317,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check for expected revs"), CHECK_EXPECTED_REVS), OPT_CMDMODE(0, "bisect-write", &cmdmode, N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), + OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, + N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -319,6 +362,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[3]); res = bisect_write(argv[0], argv[1], &terms, nolog); break; + case CHECK_AND_SET_TERMS: + if (argc != 3) + die(_("--check-and-set-terms requires 3 arguments")); + strbuf_addstr(&terms.term_good, argv[1]); + strbuf_addstr(&terms.term_bad, argv[2]); + res = check_and_set_terms(&terms, argv[0]); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index b9896a4..a41e69b 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -239,7 +239,8 @@ bisect_skip() { bisect_state() { bisect_autostart state=$1 - check_and_set_terms $state + git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit + get_terms case "$#,$state" in 0,*) die "$(gettext "Please call 'bisect_state' with at least one argument.")" ;; @@ -390,7 +391,8 @@ bisect_replay () { command="$bisect" fi get_terms - check_and_set_terms "$command" + git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit + get_terms case "$command" in start) cmd="bisect_start $rev" @@ -480,36 +482,6 @@ get_terms () { fi } -check_and_set_terms () { - cmd="$1" - case "$cmd" in - skip|start|terms) ;; - *) - if test -s "$GIT_DIR/BISECT_TERMS" && test "$cmd" != "$TERM_BAD" && test "$cmd" != "$TERM_GOOD" - then - die "$(eval_gettext "Invalid command: you're currently in a \$TERM_BAD/\$TERM_GOOD bisect.")" - fi - case "$cmd" in - bad|good) - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - 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 - TERM_BAD=new - TERM_GOOD=old - git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit - fi - ;; - esac ;; - esac -} - bisect_voc () { case "$1" in bad) echo "bad|new" ;; -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (3 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 10/12] bisect--helper: `check_and_set_terms` " Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-22 2:29 ` Torsten Bögershausen 2016-07-20 21:47 ` [PATCH v10 09/12] bisect--helper: `bisect_write` " Pranit Bauva ` (6 subsequent siblings) 11 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement the `get_terms` and `bisect_terms` shell function in C and add `bisect-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . In the shell version, the terms were identified by strings but in C version its done by bit manipulation and passing the integer value to the function. Using `--bisect-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 35 ++--------------------- 2 files changed, 75 insertions(+), 34 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 001096a..185a8ad 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -8,6 +8,13 @@ #include "run-command.h" #include "prompt.h" +enum terms_defined { + TERM_BAD = 1, + TERM_GOOD = 2, + TERM_NEW = 4, + TERM_OLD = 8 +}; + 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") @@ -26,6 +33,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), + N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), NULL }; @@ -359,6 +367,43 @@ static int bisect_next_check(const struct bisect_terms *terms, return 0; } +static int get_terms(struct bisect_terms *terms) +{ + FILE *fp; + int res; + fp = fopen(git_path_bisect_terms(), "r"); + if (!fp) + return -1; + + bisect_terms_reset(terms); + res = strbuf_getline(&terms->term_bad, fp) == EOF || + strbuf_getline(&terms->term_good, fp) == EOF; + + fclose(fp); + return res ? -1 : 0; +} + +static int bisect_terms(struct bisect_terms *terms, int term_defined) +{ + if (get_terms(terms)) { + fprintf(stderr, "no terms defined\n"); + return -1; + } + if (!term_defined) { + printf("Your current terms are %s for the old state\nand " + "%s for the new state.\n", terms->term_good.buf, + terms->term_bad.buf); + return 0; + } + + if (term_defined == TERM_GOOD || term_defined == TERM_OLD) + printf("%s\n", terms->term_good.buf); + if (term_defined == TERM_BAD || term_defined == TERM_NEW) + printf("%s\n", terms->term_bad.buf); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -369,9 +414,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) CHECK_EXPECTED_REVS, BISECT_WRITE, CHECK_AND_SET_TERMS, - BISECT_NEXT_CHECK + BISECT_NEXT_CHECK, + BISECT_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; + enum terms_defined term_defined = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), @@ -389,6 +436,16 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_CMDMODE(0, "bisect-next-check", &cmdmode, N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), + OPT_CMDMODE(0, "bisect-terms", &cmdmode, + N_("print out the bisect terms"), BISECT_TERMS), + OPT_BIT(0, "term-bad", &term_defined, + N_("show the bad term"), TERM_BAD), + OPT_BIT(0, "term-good", &term_defined, + N_("show the good term"), TERM_GOOD), + OPT_BIT(0, "term-new", &term_defined, + N_("show the new term"), TERM_NEW), + OPT_BIT(0, "term-old", &term_defined, + N_("show the old term"), TERM_OLD), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -399,6 +456,16 @@ 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 (cmdmode != BISECT_TERMS && term_defined) + die(_("--term-bad, --term-good, --term-new and --term-old " + "can be used only with --bisect-terms")); + + if (term_defined != 0 && term_defined != TERM_BAD && + term_defined != TERM_GOOD && term_defined != TERM_NEW && + term_defined != TERM_OLD) + die(_("only one option among --term-bad, --term-good, " + "--term-new and --term-old can be used.")); + if (!cmdmode) usage_with_options(git_bisect_helper_usage, options); @@ -446,6 +513,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[1]); res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); break; + case BISECT_TERMS: + if (argc > 1) + die(_("--bisect-terms requires 0 or 1 argument")); + res = bisect_terms(&terms, term_defined); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index c2d6319..aea97c5f 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -355,7 +355,7 @@ bisect_replay () { "$TERM_GOOD"|"$TERM_BAD"|skip) git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) - bisect_terms $rev ;; + git bisect--helper --bisect-terms $rev || exit;; *) die "$(gettext "?? what are you talking about?")" ;; esac @@ -437,37 +437,6 @@ get_terms () { fi } -bisect_terms () { - get_terms - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - die "$(gettext "no terms defined")" - fi - case "$#" in - 0) - gettextln "Your current terms are $TERM_GOOD for the old state -and $TERM_BAD for the new state." - ;; - 1) - arg=$1 - case "$arg" in - --term-good|--term-old) - printf '%s\n' "$TERM_GOOD" - ;; - --term-bad|--term-new) - printf '%s\n' "$TERM_BAD" - ;; - *) - die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'. -Supported options are: --term-good|--term-old and --term-bad|--term-new.")" - ;; - esac - ;; - *) - usage ;; - esac -} - case "$#" in 0) usage ;; @@ -498,7 +467,7 @@ case "$#" in run) bisect_run "$@" ;; terms) - bisect_terms "$@" ;; + git bisect--helper --bisect-terms "$@" || exit;; *) usage ;; esac -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-20 21:47 ` [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva @ 2016-07-22 2:29 ` Torsten Bögershausen 2016-07-22 14:07 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Torsten Bögershausen @ 2016-07-22 2:29 UTC (permalink / raw) To: Pranit Bauva, git On 07/20/2016 11:47 PM, Pranit Bauva wrote: > Reimplement the `get_terms` and `bisect_terms` shell function in C and > add `bisect-terms` subcommand to `git bisect--helper` to call it from > git-bisect.sh . > > In the shell version, the terms were identified by strings but in C > version its done by bit manipulation and passing the integer value to > the function. > > Using `--bisect-terms` subcommand is a temporary measure to port shell > function in C so as to use the existing test suite. As more functions > are ported, this subcommand will be retired and will be called by some > other methods. > > Mentored-by: Lars Schneider <larsxschneider@gmail.com> > Mentored-by: Christian Couder <chriscool@tuxfamily.org> > Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> > --- > builtin/bisect--helper.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++- > git-bisect.sh | 35 ++--------------------- > 2 files changed, 75 insertions(+), 34 deletions(-) > > diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c > index 001096a..185a8ad 100644 > --- a/builtin/bisect--helper.c > +++ b/builtin/bisect--helper.c > @@ -8,6 +8,13 @@ > #include "run-command.h" > #include "prompt.h" > > +enum terms_defined { > + TERM_BAD = 1, > + TERM_GOOD = 2, > + TERM_NEW = 4, > + TERM_OLD = 8 > +}; > + What does TERM stand for ? It could be TERMinal, TERMinator or just TERM. Something like BIS_TERM_DEF_BAD .. may be more intuitive, and may avoid name clashes in the long run. And why are the defines 1,2,4,8 ? It looks as if a #define bitmap may be a better choice here ? How do we do these kind of bit-wise opions otherwise ? > 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") > @@ -26,6 +33,7 @@ static const char * const git_bisect_helper_usage[] = { > N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), > N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), > N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), > + N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), > NULL > }; > > @@ -359,6 +367,43 @@ static int bisect_next_check(const struct bisect_terms *terms, > return 0; > } > > +static int get_terms(struct bisect_terms *terms) > +{ > + FILE *fp; > + int res; > + fp = fopen(git_path_bisect_terms(), "r"); > + if (!fp) > + return -1; > + > + bisect_terms_reset(terms); > + res = strbuf_getline(&terms->term_bad, fp) == EOF || > + strbuf_getline(&terms->term_good, fp) == EOF; > + > + fclose(fp); > + return res ? -1 : 0; > +} > + > +static int bisect_terms(struct bisect_terms *terms, int term_defined) > +{ > + if (get_terms(terms)) { > + fprintf(stderr, "no terms defined\n"); > + return -1; > + } > + if (!term_defined) { > + printf("Your current terms are %s for the old state\nand " > + "%s for the new state.\n", terms->term_good.buf, > + terms->term_bad.buf); > + return 0; > + } > + > + if (term_defined == TERM_GOOD || term_defined == TERM_OLD) > + printf("%s\n", terms->term_good.buf); > + if (term_defined == TERM_BAD || term_defined == TERM_NEW) > + printf("%s\n", terms->term_bad.buf); May be a switch-case ? Or at least "else if" ? > + > + return 0; > +} > + > int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > { > enum { > @@ -369,9 +414,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > CHECK_EXPECTED_REVS, > BISECT_WRITE, > CHECK_AND_SET_TERMS, > - BISECT_NEXT_CHECK > + BISECT_NEXT_CHECK, > + BISECT_TERMS > } cmdmode = 0; > int no_checkout = 0, res = 0; > + enum terms_defined term_defined = 0; > struct option options[] = { > OPT_CMDMODE(0, "next-all", &cmdmode, > N_("perform 'git bisect next'"), NEXT_ALL), > @@ -389,6 +436,16 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), > OPT_CMDMODE(0, "bisect-next-check", &cmdmode, > N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), > + OPT_CMDMODE(0, "bisect-terms", &cmdmode, > + N_("print out the bisect terms"), BISECT_TERMS), > + OPT_BIT(0, "term-bad", &term_defined, > + N_("show the bad term"), TERM_BAD), > + OPT_BIT(0, "term-good", &term_defined, > + N_("show the good term"), TERM_GOOD), > + OPT_BIT(0, "term-new", &term_defined, > + N_("show the new term"), TERM_NEW), > + OPT_BIT(0, "term-old", &term_defined, > + N_("show the old term"), TERM_OLD), > OPT_BOOL(0, "no-checkout", &no_checkout, > N_("update BISECT_HEAD instead of checking out the current commit")), > OPT_END() > @@ -399,6 +456,16 @@ 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 (cmdmode != BISECT_TERMS && term_defined) > + die(_("--term-bad, --term-good, --term-new and --term-old " > + "can be used only with --bisect-terms")); > + > + if (term_defined != 0 && term_defined != TERM_BAD && > + term_defined != TERM_GOOD && term_defined != TERM_NEW && > + term_defined != TERM_OLD) > + die(_("only one option among --term-bad, --term-good, " > + "--term-new and --term-old can be used.")); > + [] ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-22 2:29 ` Torsten Bögershausen @ 2016-07-22 14:07 ` Pranit Bauva 2016-07-25 16:53 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-22 14:07 UTC (permalink / raw) To: Torsten Bögershausen; +Cc: Git List Hey Torsten, On Fri, Jul 22, 2016 at 7:59 AM, Torsten Bögershausen <tboegi@web.de> wrote: > > > On 07/20/2016 11:47 PM, Pranit Bauva wrote: >> >> Reimplement the `get_terms` and `bisect_terms` shell function in C and >> add `bisect-terms` subcommand to `git bisect--helper` to call it from >> git-bisect.sh . >> >> In the shell version, the terms were identified by strings but in C >> version its done by bit manipulation and passing the integer value to >> the function. >> >> Using `--bisect-terms` subcommand is a temporary measure to port shell >> function in C so as to use the existing test suite. As more functions >> are ported, this subcommand will be retired and will be called by some >> other methods. >> >> Mentored-by: Lars Schneider <larsxschneider@gmail.com> >> Mentored-by: Christian Couder <chriscool@tuxfamily.org> >> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> >> --- >> builtin/bisect--helper.c | 74 >> +++++++++++++++++++++++++++++++++++++++++++++++- >> git-bisect.sh | 35 ++--------------------- >> 2 files changed, 75 insertions(+), 34 deletions(-) >> >> diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c >> index 001096a..185a8ad 100644 >> --- a/builtin/bisect--helper.c >> +++ b/builtin/bisect--helper.c >> @@ -8,6 +8,13 @@ >> #include "run-command.h" >> #include "prompt.h" >> >> +enum terms_defined { >> + TERM_BAD = 1, >> + TERM_GOOD = 2, >> + TERM_NEW = 4, >> + TERM_OLD = 8 >> +}; >> + > > What does TERM stand for ? > It could be TERMinal, TERMinator or just TERM. > Something like BIS_TERM_DEF_BAD .. may be more intuitive, > and may avoid name clashes in the long run. > > And why are the defines 1,2,4,8 ? > It looks as if a #define bitmap may be a better choice here ? > How do we do these kind of bit-wise opions otherwise ? I am not sure as why bitmaps would be a better choice except for git's source code. I saw the source code (especially config.c) and it uses "#defines" bitmap style. I haven't been able to find this method before. Also it uses "(1<<2)" instead of "4". >> 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") >> @@ -26,6 +33,7 @@ static const char * const git_bisect_helper_usage[] = { >> N_("git bisect--helper --bisect-write <state> <revision> >> <TERM_GOOD> <TERM_BAD> [<nolog>]"), >> N_("git bisect--helper --bisect-check-and-set-terms <command> >> <TERM_GOOD> <TERM_BAD>"), >> N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> >> <TERM_BAD"), >> + N_("git bisect--helper --bisect-terms [--term-good | --term-old | >> --term-bad | --term-new]"), >> NULL >> }; >> >> @@ -359,6 +367,43 @@ static int bisect_next_check(const struct >> bisect_terms *terms, >> return 0; >> } >> >> +static int get_terms(struct bisect_terms *terms) >> +{ >> + FILE *fp; >> + int res; >> + fp = fopen(git_path_bisect_terms(), "r"); >> + if (!fp) >> + return -1; >> + >> + bisect_terms_reset(terms); >> + res = strbuf_getline(&terms->term_bad, fp) == EOF || >> + strbuf_getline(&terms->term_good, fp) == EOF; >> + >> + fclose(fp); >> + return res ? -1 : 0; >> +} >> + >> +static int bisect_terms(struct bisect_terms *terms, int term_defined) >> +{ >> + if (get_terms(terms)) { >> + fprintf(stderr, "no terms defined\n"); >> + return -1; >> + } >> + if (!term_defined) { >> + printf("Your current terms are %s for the old state\nand " >> + "%s for the new state.\n", terms->term_good.buf, >> + terms->term_bad.buf); >> + return 0; >> + } >> + >> + if (term_defined == TERM_GOOD || term_defined == TERM_OLD) >> + printf("%s\n", terms->term_good.buf); >> + if (term_defined == TERM_BAD || term_defined == TERM_NEW) >> + printf("%s\n", terms->term_bad.buf); > > May be a switch-case ? > Or at least "else if" ? Yes. I will use a "else if". Thanks! >> + >> + return 0; >> +} >> + >> int cmd_bisect__helper(int argc, const char **argv, const char *prefix) >> { >> enum { >> @@ -369,9 +414,11 @@ int cmd_bisect__helper(int argc, const char **argv, >> const char *prefix) >> CHECK_EXPECTED_REVS, >> BISECT_WRITE, >> CHECK_AND_SET_TERMS, >> - BISECT_NEXT_CHECK >> + BISECT_NEXT_CHECK, >> + BISECT_TERMS >> } cmdmode = 0; >> int no_checkout = 0, res = 0; >> + enum terms_defined term_defined = 0; >> struct option options[] = { >> OPT_CMDMODE(0, "next-all", &cmdmode, >> N_("perform 'git bisect next'"), NEXT_ALL), >> @@ -389,6 +436,16 @@ int cmd_bisect__helper(int argc, const char **argv, >> const char *prefix) >> N_("check and set terms in a bisection state"), >> CHECK_AND_SET_TERMS), >> OPT_CMDMODE(0, "bisect-next-check", &cmdmode, >> N_("check whether bad or good terms exist"), >> BISECT_NEXT_CHECK), >> + OPT_CMDMODE(0, "bisect-terms", &cmdmode, >> + N_("print out the bisect terms"), BISECT_TERMS), >> + OPT_BIT(0, "term-bad", &term_defined, >> + N_("show the bad term"), TERM_BAD), >> + OPT_BIT(0, "term-good", &term_defined, >> + N_("show the good term"), TERM_GOOD), >> + OPT_BIT(0, "term-new", &term_defined, >> + N_("show the new term"), TERM_NEW), >> + OPT_BIT(0, "term-old", &term_defined, >> + N_("show the old term"), TERM_OLD), >> OPT_BOOL(0, "no-checkout", &no_checkout, >> N_("update BISECT_HEAD instead of checking out >> the current commit")), >> OPT_END() >> @@ -399,6 +456,16 @@ 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 (cmdmode != BISECT_TERMS && term_defined) >> + die(_("--term-bad, --term-good, --term-new and --term-old >> " >> + "can be used only with --bisect-terms")); >> + >> + if (term_defined != 0 && term_defined != TERM_BAD && >> + term_defined != TERM_GOOD && term_defined != TERM_NEW && >> + term_defined != TERM_OLD) >> + die(_("only one option among --term-bad, --term-good, " >> + "--term-new and --term-old can be used.")); >> + > > [] However I suspect handling "--term-good/--term-bad" is creating problems in bisect_start(). I am finding a way around. If not then I will have to get back to using "OPT_ARGUMENT" and handling it in the individual function. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-22 14:07 ` Pranit Bauva @ 2016-07-25 16:53 ` Junio C Hamano 2016-07-25 21:28 ` Christian Couder 2016-07-26 1:42 ` Torsten Bögershausen 0 siblings, 2 replies; 320+ messages in thread From: Junio C Hamano @ 2016-07-25 16:53 UTC (permalink / raw) To: Pranit Bauva; +Cc: Torsten Bögershausen, Git List Pranit Bauva <pranit.bauva@gmail.com> writes: >>> +enum terms_defined { >>> + TERM_BAD = 1, >>> + TERM_GOOD = 2, >>> + TERM_NEW = 4, >>> + TERM_OLD = 8 >>> +}; >>> + >> >> What does TERM stand for ? The terms (words) used to denote the newer and the older parts of the history. Traditionally, as a regression-hunting tool (i.e. it used to work, where did I break it?), we called older parts of the history "good" and newer one "bad", but as people gained experience with the tool, it was found that the pair of words was error-prone to use for an opposite use case "I do not recall fixing it, but it seems to have been fixed magically, when did that happen?", and a more explicit "new" and "old" were introduced. >> And why are the defines 1,2,4,8 ? >> It looks as if a #define bitmap may be a better choice here ? >> How do we do these kind of bit-wise opions otherwise ? We might want to ask if these should even be bitwise option. A word with individually controllable bits (i.e. "flag word") makes sense only when the bits within it are largely independent. But the code does this pretty much upfront: >>> + if (term_defined != 0 && term_defined != TERM_BAD && >>> + term_defined != TERM_GOOD && term_defined != TERM_NEW && >>> + term_defined != TERM_OLD) >>> + die(_("only one option among --term-bad, --term-good, " >>> + "--term-new and --term-old can be used.")); which is a very strong indication that these bits are not. I suspect that OPTION_CMDMODE would be a better choice to group these four options and mark them mutually incompatible automatically than OPT_BIT? ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-25 16:53 ` Junio C Hamano @ 2016-07-25 21:28 ` Christian Couder 2016-07-26 1:42 ` Torsten Bögershausen 1 sibling, 0 replies; 320+ messages in thread From: Christian Couder @ 2016-07-25 21:28 UTC (permalink / raw) To: Junio C Hamano; +Cc: Pranit Bauva, Torsten Bögershausen, Git List On Mon, Jul 25, 2016 at 6:53 PM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >>> And why are the defines 1,2,4,8 ? >>> It looks as if a #define bitmap may be a better choice here ? >>> How do we do these kind of bit-wise opions otherwise ? > > We might want to ask if these should even be bitwise option. A word > with individually controllable bits (i.e. "flag word") makes sense > only when the bits within it are largely independent. But the code > does this pretty much upfront: > >>>> + if (term_defined != 0 && term_defined != TERM_BAD && >>>> + term_defined != TERM_GOOD && term_defined != TERM_NEW && >>>> + term_defined != TERM_OLD) >>>> + die(_("only one option among --term-bad, --term-good, " >>>> + "--term-new and --term-old can be used.")); > > which is a very strong indication that these bits are not. > > I suspect that OPTION_CMDMODE would be a better choice to group > these four options and mark them mutually incompatible automatically > than OPT_BIT? I must say that Pranit used that at one point, but it felt weird to me to use that for things that is not really a command. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-25 16:53 ` Junio C Hamano 2016-07-25 21:28 ` Christian Couder @ 2016-07-26 1:42 ` Torsten Bögershausen 2016-07-26 17:32 ` Junio C Hamano 1 sibling, 1 reply; 320+ messages in thread From: Torsten Bögershausen @ 2016-07-26 1:42 UTC (permalink / raw) To: Junio C Hamano, Pranit Bauva; +Cc: Git List On 07/25/2016 06:53 PM, Junio C Hamano wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >>>> >>> +enum terms_defined { >>>> >>> + TERM_BAD = 1, >>>> >>> + TERM_GOOD = 2, >>>> >>> + TERM_NEW = 4, >>>> >>> + TERM_OLD = 8 >>>> >>> +}; >>>> >>> + >>> >> >>> >> What does TERM stand for ? > The terms (words) used to denote the newer and the older parts of > the history. Traditionally, as a regression-hunting tool (i.e. it > used to work, where did I break it?), we called older parts of the > history "good" and newer one "bad", but as people gained experience > with the tool, it was found that the pair of words was error-prone > to use for an opposite use case "I do not recall fixing it, but it > seems to have been fixed magically, when did that happen?", and a > more explicit "new" and "old" were introduced. > Thanks for the explanation. Is there any risk that a more generic term like "TERM_BAD" may collide with some other definition some day ? Would it make sense to call it GIT_BISECT_TERM_BAD, GBS_TERM_BAD, BIS_TERM_BAD or something more unique ? ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-26 1:42 ` Torsten Bögershausen @ 2016-07-26 17:32 ` Junio C Hamano 2016-07-27 4:20 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-07-26 17:32 UTC (permalink / raw) To: Torsten Bögershausen; +Cc: Pranit Bauva, Git List Torsten Bögershausen <tboegi@web.de> writes: > On 07/25/2016 06:53 PM, Junio C Hamano wrote: >> Pranit Bauva <pranit.bauva@gmail.com> writes: >> >>>>> >>> +enum terms_defined { >>>>> >>> + TERM_BAD = 1, >>>>> >>> + TERM_GOOD = 2, >>>>> >>> + TERM_NEW = 4, >>>>> >>> + TERM_OLD = 8 >>>>> >>> +}; >>>>> >>> + >>>> >> ... > Is there any risk that a more generic term like "TERM_BAD" may collide > with some other definition some day ? > > Would it make sense to call it GIT_BISECT_TERM_BAD, GBS_TERM_BAD, > BIS_TERM_BAD or something more unique ? I am not sure if the scope of these symbols would ever escape outside bisect-helper.c (and builtin/bisect.c eventually when we retire git-bisect.sh), but BISECT_TERM_{GOOD,BAD,OLD,NEW} would not be too bad. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-26 17:32 ` Junio C Hamano @ 2016-07-27 4:20 ` Pranit Bauva 2016-07-27 16:13 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-27 4:20 UTC (permalink / raw) To: Junio C Hamano; +Cc: Torsten Bögershausen, Git List Hey Junio, On Tue, Jul 26, 2016 at 11:02 PM, Junio C Hamano <gitster@pobox.com> wrote: > Torsten Bögershausen <tboegi@web.de> writes: > >> On 07/25/2016 06:53 PM, Junio C Hamano wrote: >>> Pranit Bauva <pranit.bauva@gmail.com> writes: >>> >>>>>> >>> +enum terms_defined { >>>>>> >>> + TERM_BAD = 1, >>>>>> >>> + TERM_GOOD = 2, >>>>>> >>> + TERM_NEW = 4, >>>>>> >>> + TERM_OLD = 8 >>>>>> >>> +}; >>>>>> >>> + >>>>> >> ... >> Is there any risk that a more generic term like "TERM_BAD" may collide >> with some other definition some day ? >> >> Would it make sense to call it GIT_BISECT_TERM_BAD, GBS_TERM_BAD, >> BIS_TERM_BAD or something more unique ? > > I am not sure if the scope of these symbols would ever escape > outside bisect-helper.c (and builtin/bisect.c eventually when we > retire git-bisect.sh), but BISECT_TERM_{GOOD,BAD,OLD,NEW} would not > be too bad. I agree that it wouldn't be too bad. This can be considered as low hanging fruit and picked up after the completion of the project as after the whole conversion, some re-ordering of code would need to be done. For eg. there is read_bisect_terms() is in bisect.c while get_terms() is in builtin/bisect--helper.c but they both do the same stuff except the later one uses strbuf and a lot more important stuff. Maybe after the whole conversion, the above enum (or #define bitmap) should also be moved to bisect.h and be used consistently in bisect.c too. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-27 4:20 ` Pranit Bauva @ 2016-07-27 16:13 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-07-27 16:13 UTC (permalink / raw) To: Pranit Bauva; +Cc: Torsten Bögershausen, Git List Pranit Bauva <pranit.bauva@gmail.com> writes: > On Tue, Jul 26, 2016 at 11:02 PM, Junio C Hamano <gitster@pobox.com> wrote: >> Torsten Bögershausen <tboegi@web.de> writes: >> >>> On 07/25/2016 06:53 PM, Junio C Hamano wrote: >>>> Pranit Bauva <pranit.bauva@gmail.com> writes: >>>> >>>>>>> >>> +enum terms_defined { >>>>>>> >>> + TERM_BAD = 1, >>>>>>> >>> + TERM_GOOD = 2, >>>>>>> >>> + TERM_NEW = 4, >>>>>>> >>> + TERM_OLD = 8 >>>>>>> >>> +}; >>>>>>> >>> + >>>>>> >> ... >>> Is there any risk that a more generic term like "TERM_BAD" may collide >>> with some other definition some day ? >>> >>> Would it make sense to call it GIT_BISECT_TERM_BAD, GBS_TERM_BAD, >>> BIS_TERM_BAD or something more unique ? >> >> I am not sure if the scope of these symbols would ever escape >> outside bisect-helper.c (and builtin/bisect.c eventually when we >> retire git-bisect.sh), but BISECT_TERM_{GOOD,BAD,OLD,NEW} would not >> be too bad. > > I agree that it wouldn't be too bad. This can be considered as low > hanging fruit and picked up after the completion of the project as > after the whole conversion, some re-ordering of code would need to be > done. > For eg. there is read_bisect_terms() is in bisect.c while > get_terms() is in builtin/bisect--helper.c but they both do the same > stuff except the later one uses strbuf and a lot more important stuff. The criteria to decide if a known "room for improvement" can be left as a "can be later touched up" is "would it be a long-term maintenance burden if it is left unfixed?", and from that point of view, I actually view the latter as a part of the necessary "polishing in response to review comments" for the initial version of a new topic to land in the official project's tree. As to TERM vs BISECT_TERM, I do not think it matters that much as I said (IOW, I do not think it would make it a maintenance burden if we kept using TERM_{BAD,GOOD,NEW,OLD}). ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v10 09/12] bisect--helper: `bisect_write` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (4 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 12/12] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 07/12] bisect--helper: `bisect_reset` " Pranit Bauva ` (5 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement the `bisect_write` shell function in C and add a `bisect-write` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--bisect-write` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. Note: bisect_write() uses two variables namely TERM_GOOD and TERM_BAD from the global shell script thus we need to pass it to the subcommand using the arguments. We then store them in a struct bisect_terms and pass the memory address around functions. This patch also introduces new methods namely bisect_state_init() and bisect_terms_release() for easy memory management for the struct bisect_terms. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 97 ++++++++++++++++++++++++++++++++++++++++++++---- git-bisect.sh | 25 ++----------- 2 files changed, 94 insertions(+), 28 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 86bb334..d1d12f2 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -22,9 +22,27 @@ static const char * const git_bisect_helper_usage[] = { 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 <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), NULL }; +struct bisect_terms { + struct strbuf term_good; + struct strbuf term_bad; +}; + +static void bisect_terms_init(struct bisect_terms *terms) +{ + strbuf_init(&terms->term_good, 0); + strbuf_init(&terms->term_bad, 0); +} + +static void bisect_terms_release(struct bisect_terms *terms) +{ + strbuf_release(&terms->term_good); + strbuf_release(&terms->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -188,6 +206,52 @@ static int check_expected_revs(const char **revs, int rev_nr) return 0; } +static int bisect_write(const char *state, const char *rev, + const struct bisect_terms *terms, int nolog) +{ + struct strbuf tag = STRBUF_INIT; + struct strbuf commit_name = STRBUF_INIT; + struct object_id oid; + struct commit *commit; + struct pretty_print_context pp = {0}; + FILE *fp; + + if (!strcmp(state, terms->term_bad.buf)) + strbuf_addf(&tag, "refs/bisect/%s", state); + else if (one_of(state, terms->term_good.buf, "skip", NULL)) + strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev); + else + return error(_("Bad bisect_write argument: %s"), state); + + if (get_oid(rev, &oid)) { + strbuf_release(&tag); + return error(_("couldn't get the oid of the rev '%s'"), rev); + } + + if (update_ref(NULL, tag.buf, oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + strbuf_release(&tag); + return -1; + } + strbuf_release(&tag); + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) + return error_errno(_("couldn't open the file '%s'"), git_path_bisect_log()); + + commit = lookup_commit_reference(oid.hash); + format_commit_message(commit, "%s", &commit_name, &pp); + fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash), + commit_name.buf); + strbuf_release(&commit_name); + + if (!nolog) + fprintf(fp, "git bisect %s %s\n", state, rev); + + fclose(fp); + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -195,9 +259,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) WRITE_TERMS, BISECT_CLEAN_STATE, BISECT_RESET, - CHECK_EXPECTED_REVS + CHECK_EXPECTED_REVS, + BISECT_WRITE } cmdmode = 0; - int no_checkout = 0; + int no_checkout = 0, res = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), @@ -209,10 +274,14 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("reset the bisection state"), BISECT_RESET), OPT_CMDMODE(0, "check-expected-revs", &cmdmode, N_("check for expected revs"), CHECK_EXPECTED_REVS), + OPT_CMDMODE(0, "bisect-write", &cmdmode, + N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() }; + struct bisect_terms terms; + bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, git_bisect_helper_usage, 0); @@ -221,24 +290,38 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) usage_with_options(git_bisect_helper_usage, options); switch (cmdmode) { + int nolog; case NEXT_ALL: return bisect_next_all(prefix, no_checkout); case WRITE_TERMS: if (argc != 2) die(_("--write-terms requires two arguments")); - return write_terms(argv[0], argv[1]); + res = write_terms(argv[0], argv[1]); + break; case BISECT_CLEAN_STATE: if (argc != 0) die(_("--bisect-clean-state requires no arguments")); - return bisect_clean_state(); + res = bisect_clean_state(); + break; case BISECT_RESET: if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); - return bisect_reset(argc ? argv[0] : NULL); + res = bisect_reset(argc ? argv[0] : NULL); + break; case CHECK_EXPECTED_REVS: - return check_expected_revs(argv, argc); + res = check_expected_revs(argv, argc); + break; + case BISECT_WRITE: + if (argc != 4 && argc != 5) + die(_("--bisect-write requires either 4 or 5 arguments")); + nolog = (argc == 5) && !strcmp(argv[4], "nolog"); + strbuf_addstr(&terms.term_good, argv[2]); + strbuf_addstr(&terms.term_bad, argv[3]); + res = bisect_write(argv[0], argv[1], &terms, nolog); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } - return 0; + bisect_terms_release(&terms); + return res; } diff --git a/git-bisect.sh b/git-bisect.sh index 4f6545e..b9896a4 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -145,7 +145,7 @@ bisect_start() { 0) state=$TERM_BAD ; bad_seen=1 ;; *) state=$TERM_GOOD ;; esac - eval="$eval bisect_write '$state' '$rev' 'nolog' &&" + eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" done # # Verify HEAD. @@ -221,23 +221,6 @@ bisect_start() { trap '-' 0 } -bisect_write() { - state="$1" - rev="$2" - nolog="$3" - case "$state" in - "$TERM_BAD") - tag="$state" ;; - "$TERM_GOOD"|skip) - tag="$state"-"$rev" ;; - *) - die "$(eval_gettext "Bad bisect_write argument: \$state")" ;; - esac - git update-ref "refs/bisect/$tag" "$rev" || exit - echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG" - test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" -} - bisect_skip() { all='' for arg in "$@" @@ -263,7 +246,7 @@ bisect_state() { 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip) rev=$(git rev-parse --verify $(bisect_head)) || die "$(gettext "Bad rev input: $(bisect_head)")" - bisect_write "$state" "$rev" + 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 @@ -276,7 +259,7 @@ bisect_state() { done for rev in $hash_list do - bisect_write "$state" "$rev" + git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit done git bisect--helper --check-expected-revs $hash_list ;; *,"$TERM_BAD") @@ -413,7 +396,7 @@ bisect_replay () { cmd="bisect_start $rev" eval "$cmd" ;; "$TERM_GOOD"|"$TERM_BAD"|skip) - bisect_write "$command" "$rev" ;; + git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) bisect_terms $rev ;; *) -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 07/12] bisect--helper: `bisect_reset` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (5 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 09/12] bisect--helper: `bisect_write` " Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 04/12] bisect--helper: `bisect_clean_state` " Pranit Bauva ` (4 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement `bisect_reset` shell function in C and add a `--bisect-reset` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `bisect_reset` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired and will be called by some other method. Note: --bisect-clean-state subcommand has not been retired as there are still a function namely `bisect_start()` which still uses this subcommand. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 28 ++-------------------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index ad67a97..d125fd3 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,6 +4,8 @@ #include "bisect.h" #include "refs.h" #include "dir.h" +#include "argv-array.h" +#include "run-command.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -13,11 +15,13 @@ static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") static GIT_PATH_FUNC(git_path_head_name, "head-name") static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") +static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD") 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"), + N_("git bisect--helper --bisect-reset [<commit>]"), NULL }; @@ -124,12 +128,47 @@ static int bisect_clean_state(void) return result; } +static int bisect_reset(const char *commit) +{ + struct strbuf branch = STRBUF_INIT; + + if (!commit) { + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { + printf("We are not bisecting.\n"); + return 0; + } + strbuf_rtrim(&branch); + } else { + struct object_id oid; + if (get_oid(commit, &oid)) + return error(_("'%s' is not a valid commit"), commit); + strbuf_addstr(&branch, commit); + } + + if (!file_exists(git_path_bisect_head())) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("Could not check out original HEAD '%s'. Try" + "'git bisect reset <commit>'."), branch.buf); + strbuf_release(&branch); + argv_array_clear(&argv); + return -1; + } + argv_array_clear(&argv); + } + + strbuf_release(&branch); + return bisect_clean_state(); +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, - BISECT_CLEAN_STATE + BISECT_CLEAN_STATE, + BISECT_RESET } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -139,6 +178,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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, "bisect-reset", &cmdmode, + N_("reset the bisection state"), BISECT_RESET), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -161,6 +202,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 0) die(_("--bisect-clean-state requires no arguments")); return bisect_clean_state(); + case BISECT_RESET: + if (argc > 1) + die(_("--bisect-reset requires either zero or one arguments")); + return bisect_reset(argc ? argv[0] : NULL); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index bbc57d2..18580b7 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -409,35 +409,11 @@ bisect_visualize() { eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES") } -bisect_reset() { - test -s "$GIT_DIR/BISECT_START" || { - gettextln "We are not bisecting." - return - } - case "$#" in - 0) branch=$(cat "$GIT_DIR/BISECT_START") ;; - 1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || { - invalid="$1" - die "$(eval_gettext "'\$invalid' is not a valid commit")" - } - branch="$1" ;; - *) - usage ;; - esac - - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- - then - die "$(eval_gettext "Could not check out original HEAD '\$branch'. -Try 'git bisect reset <commit>'.")" - fi - git bisect--helper --bisect-clean-state || exit -} - bisect_replay () { file="$1" test "$#" -eq 1 || die "$(gettext "No logfile given")" test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")" - bisect_reset + git bisect--helper --bisect-reset || exit while read git bisect command rev do test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue @@ -627,7 +603,7 @@ case "$#" in visualize|view) bisect_visualize "$@" ;; reset) - bisect_reset "$@" ;; + git bisect--helper --bisect-reset "$@" ;; replay) bisect_replay "$@" ;; log) -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 04/12] bisect--helper: `bisect_clean_state` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (6 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 07/12] bisect--helper: `bisect_reset` " Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 03/12] bisect--helper: `write_terms` " Pranit Bauva ` (3 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement `bisect_clean_state` shell function in C and add a `bisect-clean-state` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-clean-state` subcommand is a measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by bisect_reset() and bisect_start(). Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 26 +++-------------------- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index ef87c82..ad67a97 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -3,12 +3,21 @@ #include "parse-options.h" #include "bisect.h" #include "refs.h" +#include "dir.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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") +static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") +static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") +static GIT_PATH_FUNC(git_path_head_name, "head-name") +static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") 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 }; @@ -78,11 +87,49 @@ static int write_terms(const char *bad, const char *good) return (res < 0) ? -1 : 0; } +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; +} + +static 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(&refs_for_removal); + refs_for_removal.strdup_strings = 1; + string_list_clear(&refs_for_removal, 0); + remove_path(git_path_bisect_expected_rev()); + remove_path(git_path_bisect_ancestors_ok()); + remove_path(git_path_bisect_log()); + remove_path(git_path_bisect_names()); + remove_path(git_path_bisect_run()); + remove_path(git_path_bisect_terms()); + /* Cleanup head-name if it got left by an old version of git-bisect */ + remove_path(git_path_head_name()); + /* + * Cleanup BISECT_START last to support the --no-checkout option + * introduced in the commit 4796e823a. + */ + remove_path(git_path_bisect_start()); + + return result; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - WRITE_TERMS + WRITE_TERMS, + BISECT_CLEAN_STATE } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -90,6 +137,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -108,6 +157,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 2) die(_("--write-terms requires two arguments")); return write_terms(argv[0], argv[1]); + case BISECT_CLEAN_STATE: + if (argc != 0) + die(_("--bisect-clean-state requires no arguments")); + return bisect_clean_state(); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index cd39bd0..bbc57d2 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -187,7 +187,7 @@ bisect_start() { # # Get rid of any old bisect state. # - bisect_clean_state || exit + git bisect--helper --bisect-clean-state || exit # # Change state. @@ -196,7 +196,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 # @@ -430,27 +430,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 () { -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 03/12] bisect--helper: `write_terms` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (7 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 04/12] bisect--helper: `bisect_clean_state` " Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 02/12] bisect: rewrite `check_term_format` " Pranit Bauva ` (2 subsequent siblings) 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement the `write_terms` shell function in C and add a `write-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also remove the subcommand `--check-term-format` as it can now be called from inside the function write_terms() C implementation. Also `|| exit` is added when calling write-terms subcommand from git-bisect.sh so as to exit whenever there is an error. Using `--write-terms` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other method. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 36 +++++++++++++++++++++++++++++------- git-bisect.sh | 22 +++++++--------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 48285d4..ef87c82 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,9 +4,11 @@ #include "bisect.h" #include "refs.h" +static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") + static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), - N_("git bisect--helper --check-term-format <term> <orig_term>"), + N_("git bisect--helper --write-terms <bad_term> <good_term>"), NULL }; @@ -56,18 +58,38 @@ static int check_term_format(const char *term, const char *orig_term) return 0; } +static int write_terms(const char *bad, const char *good) +{ + FILE *fp; + 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); + fclose(fp); + return (res < 0) ? -1 : 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - CHECK_TERM_FMT + WRITE_TERMS } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), - OPT_CMDMODE(0, "check-term-format", &cmdmode, - N_("check format of the term"), CHECK_TERM_FMT), + OPT_CMDMODE(0, "write-terms", &cmdmode, + N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -82,10 +104,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); - case CHECK_TERM_FMT: + case WRITE_TERMS: if (argc != 2) - die(_("--check-term-format requires two arguments")); - return check_term_format(argv[0], argv[1]); + die(_("--write-terms requires two arguments")); + return write_terms(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 7d7965d..cd39bd0 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -210,7 +210,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" fi && echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit # @@ -557,18 +557,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 - git bisect--helper --check-term-format "$TERM_BAD" bad || exit - git bisect--helper --check-term-format "$TERM_GOOD" good || exit - printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS" -} - check_and_set_terms () { cmd="$1" case "$cmd" in @@ -582,13 +570,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 ;; -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 02/12] bisect: rewrite `check_term_format` shell function in C 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (8 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 03/12] bisect--helper: `write_terms` " Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-20 21:47 ` [PATCH v10 06/12] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git Reimplement the `check_term_format` shell function in C and add a `--check-term-format` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-term-format` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other method/subcommand. For eg. In conversion of write_terms() of git-bisect.sh, the subcommand will be removed and instead check_term_format() will be called in its C implementation while a new subcommand will be introduced for write_terms(). Helped-by: Johannes Schindelein <Johannes.Schindelein@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 31 ++----------------------- 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 8111c91..48285d4 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -2,19 +2,72 @@ #include "cache.h" #include "parse-options.h" #include "bisect.h" +#include "refs.h" static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), + N_("git bisect--helper --check-term-format <term> <orig_term>"), NULL }; +/* + * Check whether the string `term` belongs to the set of strings + * included in the variable arguments. + */ +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", 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - enum { NEXT_ALL = 1 } cmdmode = 0; + enum { + NEXT_ALL = 1, + CHECK_TERM_FMT + } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), + OPT_CMDMODE(0, "check-term-format", &cmdmode, + N_("check format of the term"), CHECK_TERM_FMT), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -29,6 +82,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); + case CHECK_TERM_FMT: + if (argc != 2) + die(_("--check-term-format requires two arguments")); + return check_term_format(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 5d1cb00..7d7965d 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -564,38 +564,11 @@ write_terms () { then die "$(gettext "please use two different terms")" fi - check_term_format "$TERM_BAD" bad - check_term_format "$TERM_GOOD" good + git bisect--helper --check-term-format "$TERM_BAD" bad || exit + git bisect--helper --check-term-format "$TERM_GOOD" good || exit 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 -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v10 06/12] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (9 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 02/12] bisect: rewrite `check_term_format` " Pranit Bauva @ 2016-07-20 21:47 ` Pranit Bauva 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 11 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-20 21:47 UTC (permalink / raw) To: git is_empty_file() can help to refactor a lot of code. This will be very helpful in porting "git bisect" to C. Suggested-by: Torsten Bögershausen <tboegi@web.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/am.c | 20 ++------------------ cache.h | 3 +++ wrapper.c | 13 +++++++++++++ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 3dfe70b..6ee158f 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -30,22 +30,6 @@ #include "mailinfo.h" /** - * Returns 1 if the file is empty or does not exist, 0 otherwise. - */ -static int is_empty_file(const char *filename) -{ - struct stat st; - - if (stat(filename, &st) < 0) { - if (errno == ENOENT) - return 1; - die_errno(_("could not stat %s"), filename); - } - - return !st.st_size; -} - -/** * Returns the length of the first line of msg. */ static int linelen(const char *msg) @@ -1323,7 +1307,7 @@ static int parse_mail(struct am_state *state, const char *mail) goto finish; } - if (is_empty_file(am_path(state, "patch"))) { + if (is_empty_or_missing_file(am_path(state, "patch"))) { printf_ln(_("Patch is empty. Was it split wrong?")); die_user_resolve(state); } @@ -1911,7 +1895,7 @@ static void am_run(struct am_state *state, int resume) resume = 0; } - if (!is_empty_file(am_path(state, "rewritten"))) { + if (!is_empty_or_missing_file(am_path(state, "rewritten"))) { assert(state->rebasing); copy_notes_for_rebase(state); run_post_rewrite_hook(state); diff --git a/cache.h b/cache.h index 6049f86..91e2f81 100644 --- a/cache.h +++ b/cache.h @@ -1870,4 +1870,7 @@ void sleep_millisec(int millisec); */ void safe_create_dir(const char *dir, int share); +/* Return 1 if the file is empty or does not exists, 0 otherwise. */ +extern int is_empty_or_missing_file(const char *filename); + #endif /* CACHE_H */ diff --git a/wrapper.c b/wrapper.c index 5dc4e15..e70e4d1 100644 --- a/wrapper.c +++ b/wrapper.c @@ -696,3 +696,16 @@ void sleep_millisec(int millisec) { poll(NULL, 0, millisec); } + +int is_empty_or_missing_file(const char *filename) +{ + struct stat st; + + if (stat(filename, &st) < 0) { + if (errno == ENOENT) + return 1; + die_errno(_("could not stat %s"), filename); + } + + return !st.st_size; +} -- https://github.com/git/git/pull/273 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-07-20 21:47 ` [PATCH v10 01/12] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (10 preceding siblings ...) 2016-07-20 21:47 ` [PATCH v10 06/12] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-07-31 9:21 ` [RFC/PATCH v11 07/13] bisect--helper: `bisect_reset` shell function in C Pranit Bauva ` (13 more replies) 11 siblings, 14 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git `--next-all` is meant to be used as a subcommand to support multiple "operation mode" though the current implementation does not contain any other subcommand along side with `--next-all` but further commits will include some more subcommands. Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3324229..8111c91 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -10,11 +10,11 @@ static const char * const git_bisect_helper_usage[] = { int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - int next_all = 0; + enum { NEXT_ALL = 1 } 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -23,9 +23,14 @@ 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); + default: + die("BUG: unknown subcommand '%d'", cmdmode); + } + return 0; } -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 07/13] bisect--helper: `bisect_reset` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 18:44 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 03/13] bisect--helper: `write_terms` " Pranit Bauva ` (12 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement `bisect_reset` shell function in C and add a `--bisect-reset` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `bisect_reset` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired and will be called by some other method. Note: --bisect-clean-state subcommand has not been retired as there are still a function namely `bisect_start()` which still uses this subcommand. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 28 ++-------------------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 10dfb68..0e09630 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,6 +4,8 @@ #include "bisect.h" #include "refs.h" #include "dir.h" +#include "argv-array.h" +#include "run-command.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -13,11 +15,13 @@ static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") static GIT_PATH_FUNC(git_path_head_name, "head-name") static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") +static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD") 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"), + N_("git bisect--helper --bisect-reset [<commit>]"), NULL }; @@ -124,12 +128,47 @@ static int bisect_clean_state(void) return result; } +static int bisect_reset(const char *commit) +{ + struct strbuf branch = STRBUF_INIT; + + if (!commit) { + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { + printf("We are not bisecting.\n"); + return 0; + } + strbuf_rtrim(&branch); + } else { + struct object_id oid; + if (get_oid(commit, &oid)) + return error(_("'%s' is not a valid commit"), commit); + strbuf_addstr(&branch, commit); + } + + if (!file_exists(git_path_bisect_head())) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("Could not check out original HEAD '%s'. Try " + "'git bisect reset <commit>'."), branch.buf); + strbuf_release(&branch); + argv_array_clear(&argv); + return -1; + } + argv_array_clear(&argv); + } + + strbuf_release(&branch); + return bisect_clean_state(); +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, - BISECT_CLEAN_STATE + BISECT_CLEAN_STATE, + BISECT_RESET } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -139,6 +178,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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, "bisect-reset", &cmdmode, + N_("reset the bisection state"), BISECT_RESET), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -161,6 +202,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 0) die(_("--bisect-clean-state requires no arguments")); return bisect_clean_state(); + case BISECT_RESET: + if (argc > 1) + die(_("--bisect-reset requires either zero or one arguments")); + return bisect_reset(argc ? argv[0] : NULL); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index bbc57d2..18580b7 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -409,35 +409,11 @@ bisect_visualize() { eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES") } -bisect_reset() { - test -s "$GIT_DIR/BISECT_START" || { - gettextln "We are not bisecting." - return - } - case "$#" in - 0) branch=$(cat "$GIT_DIR/BISECT_START") ;; - 1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || { - invalid="$1" - die "$(eval_gettext "'\$invalid' is not a valid commit")" - } - branch="$1" ;; - *) - usage ;; - esac - - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- - then - die "$(eval_gettext "Could not check out original HEAD '\$branch'. -Try 'git bisect reset <commit>'.")" - fi - git bisect--helper --bisect-clean-state || exit -} - bisect_replay () { file="$1" test "$#" -eq 1 || die "$(gettext "No logfile given")" test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")" - bisect_reset + git bisect--helper --bisect-reset || exit while read git bisect command rev do test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue @@ -627,7 +603,7 @@ case "$#" in visualize|view) bisect_visualize "$@" ;; reset) - bisect_reset "$@" ;; + git bisect--helper --bisect-reset "$@" ;; replay) bisect_replay "$@" ;; log) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 07/13] bisect--helper: `bisect_reset` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 07/13] bisect--helper: `bisect_reset` shell function in C Pranit Bauva @ 2016-08-02 18:44 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 18:44 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > + if (!file_exists(git_path_bisect_head())) { > + struct argv_array argv = ARGV_ARRAY_INIT; > + argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL); > + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { > + error(_("Could not check out original HEAD '%s'. Try " > + "'git bisect reset <commit>'."), branch.buf); Somebody seems to have a keen eye. Looks much better with a space after "Try" ;-) ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 03/13] bisect--helper: `write_terms` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 2016-07-31 9:21 ` [RFC/PATCH v11 07/13] bisect--helper: `bisect_reset` shell function in C Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 17:38 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 11/13] bisect--helper: `bisect_next_check` " Pranit Bauva ` (11 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement the `write_terms` shell function in C and add a `write-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also remove the subcommand `--check-term-format` as it can now be called from inside the function write_terms() C implementation. Also `|| exit` is added when calling write-terms subcommand from git-bisect.sh so as to exit whenever there is an error. Using `--write-terms` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other method. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 36 +++++++++++++++++++++++++++++------- git-bisect.sh | 22 +++++++--------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 48285d4..965bcc1 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,9 +4,11 @@ #include "bisect.h" #include "refs.h" +static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") + static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), - N_("git bisect--helper --check-term-format <term> <orig_term>"), + N_("git bisect--helper --write-terms <bad_term> <good_term>"), NULL }; @@ -56,18 +58,38 @@ static int check_term_format(const char *term, const char *orig_term) return 0; } +static int write_terms(const char *bad, const char *good) +{ + FILE *fp; + 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - CHECK_TERM_FMT + WRITE_TERMS } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), - OPT_CMDMODE(0, "check-term-format", &cmdmode, - N_("check format of the term"), CHECK_TERM_FMT), + OPT_CMDMODE(0, "write-terms", &cmdmode, + N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -82,10 +104,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); - case CHECK_TERM_FMT: + case WRITE_TERMS: if (argc != 2) - die(_("--check-term-format requires two arguments")); - return check_term_format(argv[0], argv[1]); + die(_("--write-terms requires two arguments")); + return write_terms(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 7d7965d..cd39bd0 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -210,7 +210,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" fi && echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit # @@ -557,18 +557,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 - git bisect--helper --check-term-format "$TERM_BAD" bad || exit - git bisect--helper --check-term-format "$TERM_GOOD" good || exit - printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS" -} - check_and_set_terms () { cmd="$1" case "$cmd" in @@ -582,13 +570,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 ;; -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 03/13] bisect--helper: `write_terms` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 03/13] bisect--helper: `write_terms` " Pranit Bauva @ 2016-08-02 17:38 ` Junio C Hamano 2016-08-03 20:21 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 17:38 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static int write_terms(const char *bad, const char *good) > +{ > + FILE *fp; > + 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; > +} If fprintf(3) were a function that returns 0 on success and negative on error (like fclose(3) is), the pattern to cascade the error return with "res |= another_call()" is appropriate, but the made me hiccup a bit while reading it. It is not wrong per-se and it would certainly be making it worse if we did something silly like res = fprintf(...) < 0 ? -1 : 0; res |= fclose(fp); so I guess what you have is the most succinct way to do this. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 03/13] bisect--helper: `write_terms` shell function in C 2016-08-02 17:38 ` Junio C Hamano @ 2016-08-03 20:21 ` Pranit Bauva 2016-08-04 15:39 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:21 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Tue, Aug 2, 2016 at 11:08 PM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +static int write_terms(const char *bad, const char *good) >> +{ >> + FILE *fp; >> + 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; >> +} > > If fprintf(3) were a function that returns 0 on success and negative > on error (like fclose(3) is), the pattern to cascade the error > return with "res |= another_call()" is appropriate, but the made me > hiccup a bit while reading it. It is not wrong per-se and it would > certainly be making it worse if we did something silly like > > res = fprintf(...) < 0 ? -1 : 0; > res |= fclose(fp); > > so I guess what you have is the most succinct way to do this. I agree with your point and your suggested code is better! Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 03/13] bisect--helper: `write_terms` shell function in C 2016-08-03 20:21 ` Pranit Bauva @ 2016-08-04 15:39 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-04 15:39 UTC (permalink / raw) To: Pranit Bauva; +Cc: Git List Pranit Bauva <pranit.bauva@gmail.com> writes: >>> + res = fprintf(fp, "%s\n%s\n", bad, good); >>> + res |= fclose(fp); >>> + return (res < 0) ? -1 : 0; >>> +} >> >> If fprintf(3) were a function that returns 0 on success and negative >> on error (like fclose(3) is), the pattern to cascade the error >> return with "res |= another_call()" is appropriate, but the made me >> hiccup a bit while reading it. It is not wrong per-se and it would >> certainly be making it worse if we did something silly like >> >> res = fprintf(...) < 0 ? -1 : 0; >> res |= fclose(fp); >> >> so I guess what you have is the most succinct way to do this. > > I agree with your point and your suggested code is better! Puzzled... Read it again, I was not suggesting it---I was saying "this could be a silly rewrite, which I think is making it worse". ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 11/13] bisect--helper: `bisect_next_check` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 2016-07-31 9:21 ` [RFC/PATCH v11 07/13] bisect--helper: `bisect_reset` shell function in C Pranit Bauva 2016-07-31 9:21 ` [RFC/PATCH v11 03/13] bisect--helper: `write_terms` " Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 19:17 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 05/13] t6030: explicitly test for bisection cleanup Pranit Bauva ` (10 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement `bisect_next_check` shell function in C and add `bisect-next-check` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-next-check` is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. bisect_voc() is removed as it is redundant and does not serve any useful purpose. We are better off specifying "bad|new" "good|old" as and when we require in bisect_next_check(). Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 60 +++--------------------------------- 2 files changed, 82 insertions(+), 57 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 99c9f90..71f4cf0 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -6,6 +6,7 @@ #include "dir.h" #include "argv-array.h" #include "run-command.h" +#include "prompt.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -24,6 +25,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), + N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), NULL }; @@ -292,6 +294,71 @@ static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) return 0; } +static int mark_good(const char *refname, const struct object_id *oid, + int flag, void *cb_data) +{ + int *m_good = (int *)cb_data; + *m_good = 0; + return 0; +} + +static int bisect_next_check(const struct bisect_terms *terms, + const char *current_term) +{ + int missing_good = 1, missing_bad = 1; + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); + char *good_glob = xstrfmt("%s*", terms->term_good.buf); + + if (ref_exists(bad_ref)) + missing_bad = 0; + free(bad_ref); + + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", + (void *) &missing_good); + free(good_glob); + + if (!missing_good && !missing_bad) + return 0; + + if (!current_term) + return -1; + + if (missing_good && !missing_bad && current_term && + !strcmp(current_term, terms->term_good.buf)) { + char *yesno; + /* + * have bad (or new) but not good (or old). We could bisect + * although this is less optimum. + */ + fprintf(stderr, "Warning: bisecting only with a %s commit\n", + terms->term_bad.buf); + if (!isatty(0)) + return 0; + /* + * TRANSLATORS: Make sure to include [Y] and [n] in your + * translation. The program will only accept English input + * at this point. + */ + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); + if (starts_with(yesno, "N") || starts_with(yesno, "n")) + return -1; + return 0; + } + if (!is_empty_or_missing_file(git_path_bisect_start())) + return error(_("You need to give me at least one good|old and " + "bad|new revision. You can use \"git bisect " + "bad|new\" and \"git bisect good|old\" for " + "that. \n")); + else + return error(_("You need to start by \"git bisect start\". " + "You then need to give me at least one good|" + "old and bad|new revision. You can use \"git " + "bisect bad|new\" and \"git bisect good|old\" " + " for that.\n")); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -301,7 +368,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_RESET, CHECK_EXPECTED_REVS, BISECT_WRITE, - CHECK_AND_SET_TERMS + CHECK_AND_SET_TERMS, + BISECT_NEXT_CHECK } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -319,6 +387,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), + OPT_CMDMODE(0, "bisect-next-check", &cmdmode, + N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -369,6 +439,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[2]); res = check_and_set_terms(&terms, argv[0]); break; + case BISECT_NEXT_CHECK: + if (argc != 2 && argc != 3) + die(_("--bisect-next-check requires 2 or 3 arguments")); + strbuf_addstr(&terms.term_good, argv[0]); + strbuf_addstr(&terms.term_bad, argv[1]); + res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index a41e69b..c2d6319 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -271,59 +271,14 @@ bisect_state() { bisect_auto_next } -bisect_next_check() { - missing_good= missing_bad= - git show-ref -q --verify refs/bisect/$TERM_BAD || missing_bad=t - test -n "$(git for-each-ref "refs/bisect/$TERM_GOOD-*")" || missing_good=t - - case "$missing_good,$missing_bad,$1" in - ,,*) - : have both $TERM_GOOD and $TERM_BAD - ok - ;; - *,) - # do not have both but not asked to fail - just report. - false - ;; - t,,"$TERM_GOOD") - # have bad (or new) but not good (or old). we could bisect although - # this is less optimum. - eval_gettextln "Warning: bisecting only with a \$TERM_BAD commit." >&2 - if test -t 0 - then - # TRANSLATORS: Make sure to include [Y] and [n] in your - # translation. The program will only accept English input - # at this point. - gettext "Are you sure [Y/n]? " >&2 - read yesno - case "$yesno" in [Nn]*) exit 1 ;; esac - fi - : bisect without $TERM_GOOD... - ;; - *) - bad_syn=$(bisect_voc bad) - good_syn=$(bisect_voc good) - if test -s "$GIT_DIR/BISECT_START" - then - - eval_gettextln "You need to give me at least one \$bad_syn and one \$good_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - else - eval_gettextln "You need to start by \"git bisect start\". -You then need to give me at least one \$good_syn and one \$bad_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - fi - exit 1 ;; - esac -} - bisect_auto_next() { - bisect_next_check && bisect_next || : + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || : } bisect_next() { case "$#" in 0) ;; *) usage ;; esac bisect_autostart - bisect_next_check $TERM_GOOD + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit # Perform all bisection computation, display and checkout git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout) @@ -355,7 +310,7 @@ bisect_next() { } bisect_visualize() { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit if test $# = 0 then @@ -409,7 +364,7 @@ bisect_replay () { } bisect_run () { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit while true do @@ -482,13 +437,6 @@ get_terms () { fi } -bisect_voc () { - case "$1" in - bad) echo "bad|new" ;; - good) echo "good|old" ;; - esac -} - bisect_terms () { get_terms if ! test -s "$GIT_DIR/BISECT_TERMS" -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 11/13] bisect--helper: `bisect_next_check` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 11/13] bisect--helper: `bisect_next_check` " Pranit Bauva @ 2016-08-02 19:17 ` Junio C Hamano 2016-08-03 20:33 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 19:17 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static int mark_good(const char *refname, const struct object_id *oid, > + int flag, void *cb_data) > +{ > + int *m_good = (int *)cb_data; > + *m_good = 0; > + return 0; > +} See below. > +static int bisect_next_check(const struct bisect_terms *terms, > + const char *current_term) > +{ > + int missing_good = 1, missing_bad = 1; It is somewhat unusual to start with "assume we are OK" and then "it turns out that we are not". > + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); > + char *good_glob = xstrfmt("%s*", terms->term_good.buf); The original runs git for-each-ref "refs/bisect/$TERM_GOOD-* but this one lacks the final dash. > + if (ref_exists(bad_ref)) > + missing_bad = 0; > + free(bad_ref); > + > + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", > + (void *) &missing_good); > + free(good_glob); The for-each helper does not return until it iterates over all the matching refs, but you are only interested in seeing if at least one exists. It may make sense to return 1 from mark_good() to terminate the traversal early. > + if (!missing_good && !missing_bad) > + return 0; > + > + if (!current_term) > + return -1; > + > + if (missing_good && !missing_bad && current_term && > + !strcmp(current_term, terms->term_good.buf)) { > + char *yesno; > + /* > + * have bad (or new) but not good (or old). We could bisect > + * although this is less optimum. > + */ > + fprintf(stderr, "Warning: bisecting only with a %s commit\n", > + terms->term_bad.buf); In the original, this message goes through gettext. > + if (!isatty(0)) > + return 0; > + /* > + * TRANSLATORS: Make sure to include [Y] and [n] in your > + * translation. The program will only accept English input > + * at this point. > + */ > + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); > + if (starts_with(yesno, "N") || starts_with(yesno, "n")) > + return -1; > + return 0; > + } When the control falls into the above if(){} block, the function will always return. It will clarify that this is the end of such a logical block to have a blank line here. > + if (!is_empty_or_missing_file(git_path_bisect_start())) > + return error(_("You need to give me at least one good|old and " > + "bad|new revision. You can use \"git bisect " > + "bad|new\" and \"git bisect good|old\" for " > + "that. \n")); > + else > + return error(_("You need to start by \"git bisect start\". " > + "You then need to give me at least one good|" > + "old and bad|new revision. You can use \"git " > + "bisect bad|new\" and \"git bisect good|old\" " > + " for that.\n")); The i18n on these two messages seem to be different from the original, which asks bisect_voc to learn what 'bad' and 'good' are called and attempts to use these words from the vocabulary. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 11/13] bisect--helper: `bisect_next_check` shell function in C 2016-08-02 19:17 ` Junio C Hamano @ 2016-08-03 20:33 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:33 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Wed, Aug 3, 2016 at 12:47 AM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +static int mark_good(const char *refname, const struct object_id *oid, >> + int flag, void *cb_data) >> +{ >> + int *m_good = (int *)cb_data; >> + *m_good = 0; >> + return 0; >> +} > > See below. > >> +static int bisect_next_check(const struct bisect_terms *terms, >> + const char *current_term) >> +{ >> + int missing_good = 1, missing_bad = 1; > > It is somewhat unusual to start with "assume we are OK" and then > "it turns out that we are not". > >> + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); >> + char *good_glob = xstrfmt("%s*", terms->term_good.buf); > > The original runs > > git for-each-ref "refs/bisect/$TERM_GOOD-* > > but this one lacks the final dash. My bad. Will include it. >> + if (ref_exists(bad_ref)) >> + missing_bad = 0; >> + free(bad_ref); >> + >> + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", >> + (void *) &missing_good); >> + free(good_glob); > > The for-each helper does not return until it iterates over all the > matching refs, but you are only interested in seeing if at least one > exists. It may make sense to return 1 from mark_good() to terminate > the traversal early. Seems a better way. Thanks! >> + if (!missing_good && !missing_bad) >> + return 0; >> + >> + if (!current_term) >> + return -1; >> + >> + if (missing_good && !missing_bad && current_term && >> + !strcmp(current_term, terms->term_good.buf)) { >> + char *yesno; >> + /* >> + * have bad (or new) but not good (or old). We could bisect >> + * although this is less optimum. >> + */ >> + fprintf(stderr, "Warning: bisecting only with a %s commit\n", >> + terms->term_bad.buf); > > In the original, this message goes through gettext. Will do. >> + if (!isatty(0)) >> + return 0; >> + /* >> + * TRANSLATORS: Make sure to include [Y] and [n] in your >> + * translation. The program will only accept English input >> + * at this point. >> + */ >> + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); >> + if (starts_with(yesno, "N") || starts_with(yesno, "n")) >> + return -1; >> + return 0; >> + } > > When the control falls into the above if(){} block, the function > will always return. It will clarify that this is the end of such a > logical block to have a blank line here. Will do. >> + if (!is_empty_or_missing_file(git_path_bisect_start())) >> + return error(_("You need to give me at least one good|old and " >> + "bad|new revision. You can use \"git bisect " >> + "bad|new\" and \"git bisect good|old\" for " >> + "that. \n")); >> + else >> + return error(_("You need to start by \"git bisect start\". " >> + "You then need to give me at least one good|" >> + "old and bad|new revision. You can use \"git " >> + "bisect bad|new\" and \"git bisect good|old\" " >> + " for that.\n")); > > The i18n on these two messages seem to be different from the > original, which asks bisect_voc to learn what 'bad' and 'good' are > called and attempts to use these words from the vocabulary. I have little idea about i18n. Will look more into it. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 05/13] t6030: explicitly test for bisection cleanup 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (2 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 11/13] bisect--helper: `bisect_next_check` " Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-07-31 9:21 ` [RFC/PATCH v11 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva ` (9 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Add test to explicitly check that 'git bisect reset' is working as expected. This is already covered implicitly by the test suite. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- I faced this problem while converting `bisect_clean_state` and the tests where showing breakages but it wasn't clear as to where exactly are they breaking. This will patch will help in that. Also I tested the test coverage of the test suite before this patch and it covers this (I did this by purposely changing names of files in git-bisect.sh and running the test suite). --- t/t6030-bisect-porcelain.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index e74662b..a17f7a6 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -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 -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (3 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 05/13] t6030: explicitly test for bisection cleanup Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-07-31 9:21 ` [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C Pranit Bauva ` (8 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git is_empty_file() can help to refactor a lot of code. This will be very helpful in porting "git bisect" to C. Suggested-by: Torsten Bögershausen <tboegi@web.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/am.c | 20 ++------------------ cache.h | 3 +++ wrapper.c | 13 +++++++++++++ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 3dfe70b..6ee158f 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -30,22 +30,6 @@ #include "mailinfo.h" /** - * Returns 1 if the file is empty or does not exist, 0 otherwise. - */ -static int is_empty_file(const char *filename) -{ - struct stat st; - - if (stat(filename, &st) < 0) { - if (errno == ENOENT) - return 1; - die_errno(_("could not stat %s"), filename); - } - - return !st.st_size; -} - -/** * Returns the length of the first line of msg. */ static int linelen(const char *msg) @@ -1323,7 +1307,7 @@ static int parse_mail(struct am_state *state, const char *mail) goto finish; } - if (is_empty_file(am_path(state, "patch"))) { + if (is_empty_or_missing_file(am_path(state, "patch"))) { printf_ln(_("Patch is empty. Was it split wrong?")); die_user_resolve(state); } @@ -1911,7 +1895,7 @@ static void am_run(struct am_state *state, int resume) resume = 0; } - if (!is_empty_file(am_path(state, "rewritten"))) { + if (!is_empty_or_missing_file(am_path(state, "rewritten"))) { assert(state->rebasing); copy_notes_for_rebase(state); run_post_rewrite_hook(state); diff --git a/cache.h b/cache.h index 6049f86..91e2f81 100644 --- a/cache.h +++ b/cache.h @@ -1870,4 +1870,7 @@ void sleep_millisec(int millisec); */ void safe_create_dir(const char *dir, int share); +/* Return 1 if the file is empty or does not exists, 0 otherwise. */ +extern int is_empty_or_missing_file(const char *filename); + #endif /* CACHE_H */ diff --git a/wrapper.c b/wrapper.c index 5dc4e15..e70e4d1 100644 --- a/wrapper.c +++ b/wrapper.c @@ -696,3 +696,16 @@ void sleep_millisec(int millisec) { poll(NULL, 0, millisec); } + +int is_empty_or_missing_file(const char *filename) +{ + struct stat st; + + if (stat(filename, &st) < 0) { + if (errno == ENOENT) + return 1; + die_errno(_("could not stat %s"), filename); + } + + return !st.st_size; +} -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (4 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 17:46 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 13/13] bisect--helper: `bisect_start` shell function partially " Pranit Bauva ` (7 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement `bisect_clean_state` shell function in C and add a `bisect-clean-state` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-clean-state` subcommand is a measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by bisect_reset() and bisect_start(). Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 26 +++-------------------- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 965bcc1..10dfb68 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -3,12 +3,21 @@ #include "parse-options.h" #include "bisect.h" #include "refs.h" +#include "dir.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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") +static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") +static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") +static GIT_PATH_FUNC(git_path_head_name, "head-name") +static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") 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 }; @@ -78,11 +87,49 @@ static int write_terms(const char *bad, const char *good) return (res < 0) ? -1 : 0; } +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; +} + +static 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(&refs_for_removal); + refs_for_removal.strdup_strings = 1; + string_list_clear(&refs_for_removal, 0); + remove_path(git_path_bisect_expected_rev()); + remove_path(git_path_bisect_ancestors_ok()); + remove_path(git_path_bisect_log()); + remove_path(git_path_bisect_names()); + remove_path(git_path_bisect_run()); + remove_path(git_path_bisect_terms()); + /* Cleanup head-name if it got left by an old version of git-bisect */ + remove_path(git_path_head_name()); + /* + * Cleanup BISECT_START last to support the --no-checkout option + * introduced in the commit 4796e823a. + */ + remove_path(git_path_bisect_start()); + + return result; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - WRITE_TERMS + WRITE_TERMS, + BISECT_CLEAN_STATE } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -90,6 +137,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -108,6 +157,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 2) die(_("--write-terms requires two arguments")); return write_terms(argv[0], argv[1]); + case BISECT_CLEAN_STATE: + if (argc != 0) + die(_("--bisect-clean-state requires no arguments")); + return bisect_clean_state(); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index cd39bd0..bbc57d2 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -187,7 +187,7 @@ bisect_start() { # # Get rid of any old bisect state. # - bisect_clean_state || exit + git bisect--helper --bisect-clean-state || exit # # Change state. @@ -196,7 +196,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 # @@ -430,27 +430,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 () { -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C Pranit Bauva @ 2016-08-02 17:46 ` Junio C Hamano 2016-08-03 20:27 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 17:46 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static 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(&refs_for_removal); > + refs_for_removal.strdup_strings = 1; > + string_list_clear(&refs_for_removal, 0); > + remove_path(git_path_bisect_expected_rev()); > + remove_path(git_path_bisect_ancestors_ok()); > + remove_path(git_path_bisect_log()); > + remove_path(git_path_bisect_names()); > + remove_path(git_path_bisect_run()); > + remove_path(git_path_bisect_terms()); > + /* Cleanup head-name if it got left by an old version of git-bisect */ > + remove_path(git_path_head_name()); > + * Cleanup BISECT_START last to support the --no-checkout option > + * introduced in the commit 4796e823a. > + */ > + remove_path(git_path_bisect_start()); I can see that refs/files-backend.c misuses it already, but remove_path() helper is about removing a path in the working tree, together with any parent directory that becomes empty due to the removal. You do not expect $GIT_DIR/ to become an empty directory after removing $GIT_DIR/BISECT_LOG nor want to rmdir $GIT_DIR even if it becomes empty. It is a wrong helper function to use here. Also you do not seem to check the error from the function to smudge the "result" you are returning from this function. Isn't unlink_or_warn() more correct helper to use here? > + return result; > +} ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-02 17:46 ` Junio C Hamano @ 2016-08-03 20:27 ` Pranit Bauva 2016-08-04 15:45 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:27 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Tue, Aug 2, 2016 at 11:16 PM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +static 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(&refs_for_removal); >> + refs_for_removal.strdup_strings = 1; >> + string_list_clear(&refs_for_removal, 0); >> + remove_path(git_path_bisect_expected_rev()); >> + remove_path(git_path_bisect_ancestors_ok()); >> + remove_path(git_path_bisect_log()); >> + remove_path(git_path_bisect_names()); >> + remove_path(git_path_bisect_run()); >> + remove_path(git_path_bisect_terms()); >> + /* Cleanup head-name if it got left by an old version of git-bisect */ >> + remove_path(git_path_head_name()); >> + * Cleanup BISECT_START last to support the --no-checkout option >> + * introduced in the commit 4796e823a. >> + */ >> + remove_path(git_path_bisect_start()); > > I can see that refs/files-backend.c misuses it already, but > remove_path() helper is about removing a path in the working tree, > together with any parent directory that becomes empty due to the > removal. You do not expect $GIT_DIR/ to become an empty directory > after removing $GIT_DIR/BISECT_LOG nor want to rmdir $GIT_DIR even > if it becomes empty. It is a wrong helper function to use here. > > Also you do not seem to check the error from the function to smudge > the "result" you are returning from this function. Yes I should combine the results from every removal. > Isn't unlink_or_warn() more correct helper to use here? The shell code uses rm -f which is silent and it removes only if present. So it makes me wonder which would be more appropriate unlink_or_warn() or remove_or_warn() or remove_path(). Is remove_path() different from its shell equivalent "rm -f"? >> + return result; >> +} Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-03 20:27 ` Pranit Bauva @ 2016-08-04 15:45 ` Junio C Hamano 2016-08-04 16:07 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-04 15:45 UTC (permalink / raw) To: Pranit Bauva; +Cc: Git List Pranit Bauva <pranit.bauva@gmail.com> writes: >> Also you do not seem to check the error from the function to smudge >> the "result" you are returning from this function. > > Yes I should combine the results from every removal. > >> Isn't unlink_or_warn() more correct helper to use here? > > The shell code uses rm -f which is silent and it removes only if > present. Isn't that what unlink_or_warn() do? Call unlink() and happily return if unlink() succeeds or errors with ENOENT (i.e. path didn't exist in the first place), but otherwise reports an error (imagine: EPERM). > So it makes me wonder which would be more appropriate > unlink_or_warn() or remove_or_warn() or remove_path(). Is > remove_path() different from its shell equivalent "rm -f"? Read it again. >>> + remove_path(git_path_bisect_start()); >> >> I can see that refs/files-backend.c misuses it already, but >> remove_path() helper is about removing a path in the working tree, >> together with any parent directory that becomes empty due to the >> removal. You do not expect $GIT_DIR/ to become an empty directory >> after removing $GIT_DIR/BISECT_LOG nor want to rmdir $GIT_DIR even >> if it becomes empty. It is a wrong helper function to use here. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-04 15:45 ` Junio C Hamano @ 2016-08-04 16:07 ` Pranit Bauva 2016-08-04 16:50 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-04 16:07 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Thu, Aug 4, 2016 at 9:15 PM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >>> Also you do not seem to check the error from the function to smudge >>> the "result" you are returning from this function. >> >> Yes I should combine the results from every removal. >> >>> Isn't unlink_or_warn() more correct helper to use here? >> >> The shell code uses rm -f which is silent and it removes only if >> present. > > Isn't that what unlink_or_warn() do? Call unlink() and happily > return if unlink() succeeds or errors with ENOENT (i.e. path didn't > exist in the first place), but otherwise reports an error (imagine: > EPERM). Umm, I am confused. I tried "rm -f" with a non-existing file and it does not show any warning or error. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-04 16:07 ` Pranit Bauva @ 2016-08-04 16:50 ` Junio C Hamano 2016-08-04 16:57 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-04 16:50 UTC (permalink / raw) To: Pranit Bauva; +Cc: Git List Pranit Bauva <pranit.bauva@gmail.com> writes: > Hey Junio, > > On Thu, Aug 4, 2016 at 9:15 PM, Junio C Hamano <gitster@pobox.com> wrote: >> Pranit Bauva <pranit.bauva@gmail.com> writes: >> >>>> Also you do not seem to check the error from the function to smudge >>>> the "result" you are returning from this function. >>> >>> Yes I should combine the results from every removal. >>> >>>> Isn't unlink_or_warn() more correct helper to use here? >>> >>> The shell code uses rm -f which is silent and it removes only if >>> present. >> >> Isn't that what unlink_or_warn() do? Call unlink() and happily >> return if unlink() succeeds or errors with ENOENT (i.e. path didn't >> exist in the first place), but otherwise reports an error (imagine: >> EPERM). > > Umm, I am confused. I tried "rm -f" with a non-existing file and it > does not show any warning or error. You are, or you were? I hope it is the latter, iow, you are no longer confused and now understand why unlink_or_warn() was suggested. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-04 16:50 ` Junio C Hamano @ 2016-08-04 16:57 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-04 16:57 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Thu, Aug 4, 2016 at 10:20 PM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> Hey Junio, >> >> On Thu, Aug 4, 2016 at 9:15 PM, Junio C Hamano <gitster@pobox.com> wrote: >>> Pranit Bauva <pranit.bauva@gmail.com> writes: >>> >>>>> Also you do not seem to check the error from the function to smudge >>>>> the "result" you are returning from this function. >>>> >>>> Yes I should combine the results from every removal. >>>> >>>>> Isn't unlink_or_warn() more correct helper to use here? >>>> >>>> The shell code uses rm -f which is silent and it removes only if >>>> present. >>> >>> Isn't that what unlink_or_warn() do? Call unlink() and happily >>> return if unlink() succeeds or errors with ENOENT (i.e. path didn't >>> exist in the first place), but otherwise reports an error (imagine: >>> EPERM). >> >> Umm, I am confused. I tried "rm -f" with a non-existing file and it >> does not show any warning or error. > > You are, or you were? I hope it is the latter, iow, you are no > longer confused and now understand why unlink_or_warn() was > suggested. I meant to use past tense. Did not re-check before sending it. ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (5 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 04/13] bisect--helper: `bisect_clean_state` shell function in C Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 20:19 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function " Pranit Bauva ` (6 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement the `bisect_start` shell function partially in C and add `bisect-start` subcommand to `git bisect--helper` to call it from git-bisect.sh . The last part is not converted because it calls another shell function. `bisect_start` shell function will be completed after the `bisect_next` shell function is ported in C. Using `--bisect-start` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- This patch contains a small bug. The option handling for `--term-good` and `--term-bad` needs to be decided as it is now shared between `--bisect-terms` and `--bisect-start` and the later one also requires string support. Can comments on which approach would seem the most feasible? Here[1] is a normal output of t6030 and here[2] is a verbose output of [2]. [1]: http://paste.ubuntu.com/21252069/ [2]: http://paste.ubuntu.com/21252140/ --- builtin/bisect--helper.c | 232 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 132 +-------------------------- 2 files changed, 232 insertions(+), 132 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 81a16a5..ab18786 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -404,6 +404,228 @@ static int bisect_terms(struct bisect_terms *terms, int term_defined) return 0; } +static int bisect_start(struct bisect_terms *terms, int no_checkout, + const char **argv, int argc) +{ + int i, j, has_double_dash = 0, must_write_terms = 0, bad_seen = 0; + int flag; + struct string_list revs = STRING_LIST_INIT_DUP; + struct string_list states = STRING_LIST_INIT_DUP; + struct strbuf start_head = STRBUF_INIT; + const char *head; + unsigned char sha1[20]; + FILE *fp; + struct object_id oid; + + if (is_bare_repository()) + no_checkout = 1; + + for(i = 0; i < argc; i++) { + if (!strcmp(argv[i], "--")) { + has_double_dash = 1; + break; + } + if (!strcmp(argv[i], "--term-good")) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[++i]); + break; + } + if (!strcmp(argv[i], "--term-bad")) { + must_write_terms = 1; + strbuf_reset(&terms->term_bad); + strbuf_addstr(&terms->term_bad, argv[++i]); + break; + } + if (starts_with(argv[i], "--") && + !one_of(argv[i], "--term-good", "--term-bad", NULL)) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("unrecognised option: '%s'"), argv[i]); + } + if (get_oid(argv[i], &oid) || has_double_dash) { + string_list_clear(&revs, 0); + string_list_clear(&revs, 0); + die(_("'%s' does not appear to be a valid revision"), argv[i]); + } + else + string_list_append(&revs, oid_to_hex(&oid)); + } + + for (j = 0; j < revs.nr; j++) { + struct strbuf state = STRBUF_INIT; + /* + * The user ran "git bisect start <sha1> <sha1>", hence + * did not explicitly specify the terms, but we are already + * starting to set references named with the default terms, + * and won't be able to change afterwards. + */ + must_write_terms = 1; + + if (bad_seen) + strbuf_addstr(&state, terms->term_good.buf); + else { + bad_seen = 1; + strbuf_addstr(&state, terms->term_bad.buf); + } + string_list_append(&states, state.buf); + strbuf_release(&state); + } + + /* + * Verify HEAD + */ + head = resolve_ref_unsafe("HEAD", 0, sha1, &flag); + if (!head) { + if (get_sha1("HEAD", sha1)) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("Bad HEAD - I need a HEAD")); + } + } + if (!is_empty_or_missing_file(git_path_bisect_start())) { + /* Reset to the rev from where we started */ + strbuf_read_file(&start_head, git_path_bisect_start(), 0); + strbuf_trim(&start_head); + if (!no_checkout) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", start_head.buf, + "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("checking out '%s' failed. Try again."), + start_head.buf); + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + } else { + if (starts_with(head, "refs/head/") || !get_oid(head, &oid)) { + /* + * This error message should only be triggered by + * cogito usage, and cogito users should understand + * it relates to cg-seek. + */ + if (!is_empty_or_missing_file(git_path_head_name())) { + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("won't bisect on cg-seek'ed tree")); + } + if (starts_with(head, "refs/heads/")) { + strbuf_reset(&start_head); + strbuf_addstr(&start_head, head + 11); + } + else { + strbuf_reset(&start_head); + strbuf_addstr(&start_head, sha1_to_hex(sha1)); + } + } else { + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("Bad HEAD - strange symbolic ref")); + } + } + + /* + * Get rid of any old bisect state. + */ + if (bisect_clean_state()) { + return -1; + } + + /* + * Write new start state + */ + fp = fopen(git_path_bisect_start(), "w"); + if (!fp) { + bisect_clean_state(); + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + if (!fprintf(fp, "%s\n", start_head.buf)) { + fclose(fp); + bisect_clean_state(); + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + fclose(fp); + + if (no_checkout) { + get_oid(start_head.buf, &oid); + if (update_ref(NULL, "BISECT_HEAD", oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + bisect_clean_state(); + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + strbuf_release(&start_head); + fp = fopen(git_path_bisect_names(), "w"); + + for (; i < argc; i++) { + if (!fprintf(fp, "%s ", argv[i])) { + fclose(fp); + bisect_clean_state(); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + fclose(fp); + + for (j = 0; j < states.nr; j ++) { + if (bisect_write(states.items[j].string, + revs.items[j].string, terms, 1)) { + bisect_clean_state(); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + + if (must_write_terms) + if (write_terms(terms->term_bad.buf, terms->term_good.buf)) { + bisect_clean_state(); + return -1; + } + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) { + bisect_clean_state(); + return -1; + } + if (!fprintf(fp, "git bisect start")) { + bisect_clean_state(); + return -1; + } + for (i = 0; i < argc; i++) { + if (!fprintf(fp, " '%s'", argv[i])) { + fclose(fp); + bisect_clean_state(); + return -1; + } + } + if (!fprintf(fp, "\n")) { + fclose(fp); + bisect_clean_state(); + return -1; + } + fclose(fp); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -415,7 +637,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_WRITE, CHECK_AND_SET_TERMS, BISECT_NEXT_CHECK, - BISECT_TERMS + BISECT_TERMS, + BISECT_START } cmdmode = 0; int no_checkout = 0, res = 0; enum terms_defined term_defined = 0; @@ -446,6 +669,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("show the new term"), TERM_NEW), OPT_BIT(0, "term-old", &term_defined, N_("show the old term"), TERM_OLD), + OPT_CMDMODE(0, "bisect-start", &cmdmode, + N_("start the bisect session"), BISECT_START), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -518,6 +743,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) die(_("--bisect-terms requires 0 or 1 argument")); res = bisect_terms(&terms, term_defined); break; + case BISECT_START: + strbuf_addstr(&terms.term_good, "good"); + strbuf_addstr(&terms.term_bad, "bad"); + res = bisect_start(&terms, no_checkout, argv, argc); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index aea97c5f..51d0a71 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -72,122 +72,7 @@ bisect_autostart() { } bisect_start() { - # - # Check for one bad and then some good revisions. - # - has_double_dash=0 - for arg; do - case "$arg" in --) has_double_dash=1; break ;; esac - done - orig_args=$(git rev-parse --sq-quote "$@") - bad_seen=0 - eval='' - must_write_terms=0 - revs='' - if test "z$(git rev-parse --is-bare-repository)" != zfalse - then - mode=--no-checkout - else - mode='' - fi - while [ $# -gt 0 ]; do - arg="$1" - case "$arg" in - --) - shift - break - ;; - --no-checkout) - mode=--no-checkout - shift ;; - --term-good|--term-old) - shift - must_write_terms=1 - TERM_GOOD=$1 - shift ;; - --term-good=*|--term-old=*) - must_write_terms=1 - TERM_GOOD=${1#*=} - shift ;; - --term-bad|--term-new) - shift - must_write_terms=1 - TERM_BAD=$1 - shift ;; - --term-bad=*|--term-new=*) - must_write_terms=1 - TERM_BAD=${1#*=} - shift ;; - --*) - die "$(eval_gettext "unrecognised option: '\$arg'")" ;; - *) - 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" - shift - ;; - esac - done - - for rev in $revs - do - # The user ran "git bisect start <sha1> - # <sha1>", hence did not explicitly specify - # the terms, but we are already starting to - # set references named with the default terms, - # and won't be able to change afterwards. - must_write_terms=1 - - case $bad_seen in - 0) state=$TERM_BAD ; bad_seen=1 ;; - *) state=$TERM_GOOD ;; - esac - eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" - done - # - # Verify HEAD. - # - head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) || - head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) || - die "$(gettext "Bad HEAD - I need a HEAD")" - - # - # Check if we are bisecting. - # - start_head='' - if test -s "$GIT_DIR/BISECT_START" - then - # Reset to the rev from where we started. - start_head=$(cat "$GIT_DIR/BISECT_START") - if test "z$mode" != "z--no-checkout" - then - git checkout "$start_head" -- || - die "$(eval_gettext "Checking out '\$start_head' failed. Try 'git bisect reset <valid-branch>'.")" - fi - else - # Get rev from where we start. - case "$head" in - refs/heads/*|$_x40) - # This error message should only be triggered by - # cogito usage, and cogito users should understand - # it relates to cg-seek. - [ -s "$GIT_DIR/head-name" ] && - die "$(gettext "won't bisect on cg-seek'ed tree")" - start_head="${head#refs/heads/}" - ;; - *) - die "$(gettext "Bad HEAD - strange symbolic ref")" - ;; - esac - fi - - # - # Get rid of any old bisect state. - # - git bisect--helper --bisect-clean-state || exit + git bisect--helper --bisect-start $@ || exit # # Change state. @@ -198,21 +83,6 @@ bisect_start() { # trap 'git bisect--helper --bisect-clean-state' 0 trap 'exit 255' 1 2 3 15 - - # - # Write new start state. - # - echo "$start_head" >"$GIT_DIR/BISECT_START" && { - test "z$mode" != "z--no-checkout" || - git update-ref --no-deref BISECT_HEAD "$start_head" - } && - git rev-parse --sq-quote "$@" >"$GIT_DIR/BISECT_NAMES" && - eval "$eval true" && - if test $must_write_terms -eq 1 - then - git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" - fi && - echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit # # Check if we can proceed to the next bisect state. # -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-07-31 9:21 ` [RFC/PATCH v11 13/13] bisect--helper: `bisect_start` shell function partially " Pranit Bauva @ 2016-08-02 20:19 ` Junio C Hamano 2016-08-03 20:49 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 20:19 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static int bisect_start(struct bisect_terms *terms, int no_checkout, > + const char **argv, int argc) > +{ > + int i, j, has_double_dash = 0, must_write_terms = 0, bad_seen = 0; > + int flag; > + struct string_list revs = STRING_LIST_INIT_DUP; > + struct string_list states = STRING_LIST_INIT_DUP; > + struct strbuf start_head = STRBUF_INIT; > + const char *head; > + unsigned char sha1[20]; > + FILE *fp; > + struct object_id oid; > + > + if (is_bare_repository()) > + no_checkout = 1; > + > + for(i = 0; i < argc; i++) { SP after for. > + if (!strcmp(argv[i], "--")) { > + has_double_dash = 1; > + break; > + } > + if (!strcmp(argv[i], "--term-good")) { > + must_write_terms = 1; > + strbuf_reset(&terms->term_good); > + strbuf_addstr(&terms->term_good, argv[++i]); > + break; > + } > + if (!strcmp(argv[i], "--term-bad")) { > + must_write_terms = 1; > + strbuf_reset(&terms->term_bad); > + strbuf_addstr(&terms->term_bad, argv[++i]); > + break; > + } The original was not careful, either, but what if the user ends the command line with "--term-good", without anything after it? Also the original is prepared to handle --term-good=boa; because this function can be be called directly from the UI (i.e. "git bisect start --term-good=boa"), not supporting that form would be seen as a regression. > + if (starts_with(argv[i], "--") && > + !one_of(argv[i], "--term-good", "--term-bad", NULL)) { > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + die(_("unrecognised option: '%s'"), argv[i]); > + } > + if (get_oid(argv[i], &oid) || has_double_dash) { Calling get_oid() alone is insufficient to make sure argv[i] refers to an existing object that is a committish. The "^{commit}" suffix in the original is there for a reason. > + string_list_clear(&revs, 0); > + string_list_clear(&revs, 0); You seem to want the revs list really really clean ;-) > + die(_("'%s' does not appear to be a valid revision"), argv[i]); > + } > + else > + string_list_append(&revs, oid_to_hex(&oid)); > + } > + > + for (j = 0; j < revs.nr; j++) { Why "j", not "i", as clearly the previous loop has finished at this point? The only reason why replacing "j" with "i" would make this function buggy would be if a later part of this function depended on the value of "i" when the control left the above loop, but if that were the case (I didn't check carefully), such a precious value that has long term effect throughout the remainder of the function must not be kept in an otherwise throw-away loop counter variable "i". Introduce a new "int pathspec_pos" and set it to "i" immediately after the "for (i = 0; i < argc; i++) { ... }" loop above, perhaps. > + struct strbuf state = STRBUF_INIT; > + /* > + * The user ran "git bisect start <sha1> <sha1>", hence > + * did not explicitly specify the terms, but we are already > + * starting to set references named with the default terms, > + * and won't be able to change afterwards. > + */ > + must_write_terms = 1; > + > + if (bad_seen) > + strbuf_addstr(&state, terms->term_good.buf); > + else { > + bad_seen = 1; > + strbuf_addstr(&state, terms->term_bad.buf); > + } > + string_list_append(&states, state.buf); > + strbuf_release(&state); > + } How about this instead? /* * that comment block goes here */ must_write_terms = !!revs.nr; for (i = 0; i < revs.nr; i++) { if (bad_seen) string_list_append(&states, terms->term_good.buf); else string_list_append(&states, terms->term_bad.buf); } > + > + /* > + * Verify HEAD > + */ > + head = resolve_ref_unsafe("HEAD", 0, sha1, &flag); The last parameter is a set of flag bits, so call it flags. > + if (!head) { > + if (get_sha1("HEAD", sha1)) { > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + die(_("Bad HEAD - I need a HEAD")); We see many repeated calls to clear these two string lists before exiting with failure, either by dying or return -1. I wonder how bad the resulting code would look like if we employed the standard pattern of having a "fail_return:" label at the end of the function (after the "return" for the usual control flow) to clear them. If the result becomes less readable (and I suspect that you would end up making it less readable), leaving the current code structure is OK. > + } > + } > + if (!is_empty_or_missing_file(git_path_bisect_start())) { > + /* Reset to the rev from where we started */ > + strbuf_read_file(&start_head, git_path_bisect_start(), 0); > + strbuf_trim(&start_head); > + if (!no_checkout) { > + struct argv_array argv = ARGV_ARRAY_INIT; > + argv_array_pushl(&argv, "checkout", start_head.buf, > + "--", NULL); > + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { > + error(_("checking out '%s' failed. Try again."), > + start_head.buf); The original suggests to try "git bisect reset" here to recover. > + strbuf_release(&start_head); > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + return -1; > + } > + } > + } else { > + if (starts_with(head, "refs/head/") || !get_oid(head, &oid)) { get_oid() is insufficient to ensure what you have in $head is 40-hex. I think you meant get_oid_hex() here. > + /* > + * This error message should only be triggered by > + * cogito usage, and cogito users should understand > + * it relates to cg-seek. > + */ > + if (!is_empty_or_missing_file(git_path_head_name())) { > + strbuf_release(&start_head); > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + die(_("won't bisect on cg-seek'ed tree")); > + } > + if (starts_with(head, "refs/heads/")) { skip_prefix(), perhaps, if "head" is no longer used from here on? > + /* > + * Write new start state > + */ > + fp = fopen(git_path_bisect_start(), "w"); > + if (!fp) { > + bisect_clean_state(); > + strbuf_release(&start_head); > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + return -1; > + } > + if (!fprintf(fp, "%s\n", start_head.buf)) { man 3 fprintf and look for "Return Value"? > + fclose(fp); > + bisect_clean_state(); > + strbuf_release(&start_head); > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + return -1; > + } > + fclose(fp); Perhaps use write_file() instead of the above block of text? > + if (no_checkout) { > + get_oid(start_head.buf, &oid); > + if (update_ref(NULL, "BISECT_HEAD", oid.hash, NULL, 0, > + UPDATE_REFS_MSG_ON_ERR)) { Doesn't the original use --no-deref for this update-ref call? > + bisect_clean_state(); > + strbuf_release(&start_head); > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + return -1; > + } > + } > + strbuf_release(&start_head); > + fp = fopen(git_path_bisect_names(), "w"); > + > + for (; i < argc; i++) { > + if (!fprintf(fp, "%s ", argv[i])) { man 3 fprintf and look for "Return Value"? More importantly, the original does --sq-quote so that BISECT_NAMES file can be read back by a shell. This is important as argv[i] can have whitespace in it, and you are concatenating them with SP in between here. Also you are not terminating that line. > + fclose(fp); > + bisect_clean_state(); > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + return -1; > + } > + } > + fclose(fp); Perhaps strbuf_reset(&bisect_names); if (pathspec_pos < argc) sq_quote_argv(&bisect_names, argv + pathspec_pos, 0); write_file(git_path_bisect_names(), "%s\n", bisect_names.buf); or something like that? > + for (j = 0; j < states.nr; j ++) { Again, is "i" still precious here? Style: drop SP between j and ++. > + fp = fopen(git_path_bisect_log(), "a"); > + if (!fp) { > + bisect_clean_state(); > + return -1; > + } > + if (!fprintf(fp, "git bisect start")) { > + bisect_clean_state(); > + return -1; > + } > + for (i = 0; i < argc; i++) { > + if (!fprintf(fp, " '%s'", argv[i])) { > + fclose(fp); > + bisect_clean_state(); > + return -1; > + } > + } > + if (!fprintf(fp, "\n")) { > + fclose(fp); > + bisect_clean_state(); > + return -1; > + } Again, the original writes orig_args which was protected with --sq-quote. > + fclose(fp); > + > + return 0; > +} > + ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-08-02 20:19 ` Junio C Hamano @ 2016-08-03 20:49 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:49 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Wed, Aug 3, 2016 at 1:49 AM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +static int bisect_start(struct bisect_terms *terms, int no_checkout, >> + const char **argv, int argc) >> +{ >> + int i, j, has_double_dash = 0, must_write_terms = 0, bad_seen = 0; >> + int flag; >> + struct string_list revs = STRING_LIST_INIT_DUP; >> + struct string_list states = STRING_LIST_INIT_DUP; >> + struct strbuf start_head = STRBUF_INIT; >> + const char *head; >> + unsigned char sha1[20]; >> + FILE *fp; >> + struct object_id oid; >> + >> + if (is_bare_repository()) >> + no_checkout = 1; >> + >> + for(i = 0; i < argc; i++) { > > SP after for. Sure! >> + if (!strcmp(argv[i], "--")) { >> + has_double_dash = 1; >> + break; >> + } >> + if (!strcmp(argv[i], "--term-good")) { >> + must_write_terms = 1; >> + strbuf_reset(&terms->term_good); >> + strbuf_addstr(&terms->term_good, argv[++i]); >> + break; >> + } >> + if (!strcmp(argv[i], "--term-bad")) { >> + must_write_terms = 1; >> + strbuf_reset(&terms->term_bad); >> + strbuf_addstr(&terms->term_bad, argv[++i]); >> + break; >> + } > > The original was not careful, either, but what if the user ends the > command line with "--term-good", without anything after it? > Also the original is prepared to handle --term-good=boa; because > this function can be be called directly from the UI (i.e. "git > bisect start --term-good=boa"), not supporting that form would be > seen as a regression. I wanted to discuss this precisely by this RFC. I was initially thinking of using OPT_ARGUMENT() for bisect_terms() which would in turn cover up for bisect_start() too. Currently the code does not support --term-good=boa because it treats --term-good as a boolean Do you have any other thing in mind? >> + if (starts_with(argv[i], "--") && >> + !one_of(argv[i], "--term-good", "--term-bad", NULL)) { >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + die(_("unrecognised option: '%s'"), argv[i]); >> + } >> + if (get_oid(argv[i], &oid) || has_double_dash) { > > Calling get_oid() alone is insufficient to make sure argv[i] refers > to an existing object that is a committish. The "^{commit}" suffix > in the original is there for a reason. Yes sure! >> + string_list_clear(&revs, 0); >> + string_list_clear(&revs, 0); > > You seem to want the revs list really really clean ;-) Haha! ;) My bad. Will remove the extra line! >> + die(_("'%s' does not appear to be a valid revision"), argv[i]); >> + } >> + else >> + string_list_append(&revs, oid_to_hex(&oid)); >> + } >> + >> + for (j = 0; j < revs.nr; j++) { > > Why "j", not "i", as clearly the previous loop has finished at this > point? The only reason why replacing "j" with "i" would make this > function buggy would be if a later part of this function depended on > the value of "i" when the control left the above loop, but if that > were the case (I didn't check carefully), such a precious value that > has long term effect throughout the remainder of the function must > not be kept in an otherwise throw-away loop counter variable "i". > > Introduce a new "int pathspec_pos" and set it to "i" immediately > after the "for (i = 0; i < argc; i++) { ... }" loop above, perhaps. I am using i afterwards for writing the arguments to BISECT_NAMES. But I think it would be better to use pathspec_pos and discard j altogether. Thanks! > >> + struct strbuf state = STRBUF_INIT; >> + /* >> + * The user ran "git bisect start <sha1> <sha1>", hence >> + * did not explicitly specify the terms, but we are already >> + * starting to set references named with the default terms, >> + * and won't be able to change afterwards. >> + */ >> + must_write_terms = 1; >> + >> + if (bad_seen) >> + strbuf_addstr(&state, terms->term_good.buf); >> + else { >> + bad_seen = 1; >> + strbuf_addstr(&state, terms->term_bad.buf); >> + } >> + string_list_append(&states, state.buf); >> + strbuf_release(&state); >> + } > > How about this instead? > > /* > * that comment block goes here > */ > must_write_terms = !!revs.nr; > for (i = 0; i < revs.nr; i++) { > if (bad_seen) > string_list_append(&states, terms->term_good.buf); > else > string_list_append(&states, terms->term_bad.buf); > } Seems better. Thanks! >> + >> + /* >> + * Verify HEAD >> + */ >> + head = resolve_ref_unsafe("HEAD", 0, sha1, &flag); > > The last parameter is a set of flag bits, so call it flags. Sure! >> + if (!head) { >> + if (get_sha1("HEAD", sha1)) { >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + die(_("Bad HEAD - I need a HEAD")); > > We see many repeated calls to clear these two string lists before > exiting with failure, either by dying or return -1. > > I wonder how bad the resulting code would look like if we employed > the standard pattern of having a "fail_return:" label at the end of > the function (after the "return" for the usual control flow) to > clear them. If the result becomes less readable (and I suspect that > you would end up making it less readable), leaving the current code > structure is OK. Its becoming really bad. I tried it once. Different things are being cleaned up at different times which makes it all the more tedious to maintain. >> + } >> + } >> + if (!is_empty_or_missing_file(git_path_bisect_start())) { >> + /* Reset to the rev from where we started */ >> + strbuf_read_file(&start_head, git_path_bisect_start(), 0); >> + strbuf_trim(&start_head); >> + if (!no_checkout) { >> + struct argv_array argv = ARGV_ARRAY_INIT; >> + argv_array_pushl(&argv, "checkout", start_head.buf, >> + "--", NULL); >> + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { >> + error(_("checking out '%s' failed. Try again."), >> + start_head.buf); > > The original suggests to try "git bisect reset" here to recover. Will include it. >> + strbuf_release(&start_head); >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + return -1; >> + } >> + } >> + } else { >> + if (starts_with(head, "refs/head/") || !get_oid(head, &oid)) { > > get_oid() is insufficient to ensure what you have in $head is > 40-hex. I think you meant get_oid_hex() here. Yes definitely. Might have been missed. >> + /* >> + * This error message should only be triggered by >> + * cogito usage, and cogito users should understand >> + * it relates to cg-seek. >> + */ >> + if (!is_empty_or_missing_file(git_path_head_name())) { >> + strbuf_release(&start_head); >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + die(_("won't bisect on cg-seek'ed tree")); >> + } >> + if (starts_with(head, "refs/heads/")) { > > skip_prefix(), perhaps, if "head" is no longer used from here on? Sure! >> + /* >> + * Write new start state >> + */ >> + fp = fopen(git_path_bisect_start(), "w"); >> + if (!fp) { >> + bisect_clean_state(); >> + strbuf_release(&start_head); >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + return -1; >> + } >> + if (!fprintf(fp, "%s\n", start_head.buf)) { > > man 3 fprintf and look for "Return Value"? I should have been more careful about fprintf's return value. >> + fclose(fp); >> + bisect_clean_state(); >> + strbuf_release(&start_head); >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + return -1; >> + } >> + fclose(fp); > > Perhaps use write_file() instead of the above block of text? Yes, that seems better. Thanks! >> + if (no_checkout) { >> + get_oid(start_head.buf, &oid); >> + if (update_ref(NULL, "BISECT_HEAD", oid.hash, NULL, 0, >> + UPDATE_REFS_MSG_ON_ERR)) { > > Doesn't the original use --no-deref for this update-ref call? Yes, will change. >> + bisect_clean_state(); >> + strbuf_release(&start_head); >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + return -1; >> + } >> + } >> + strbuf_release(&start_head); >> + fp = fopen(git_path_bisect_names(), "w"); >> + >> + for (; i < argc; i++) { >> + if (!fprintf(fp, "%s ", argv[i])) { > > man 3 fprintf and look for "Return Value"? > > More importantly, the original does --sq-quote so that BISECT_NAMES > file can be read back by a shell. This is important as argv[i] can > have whitespace in it, and you are concatenating them with SP in > between here. Also you are not terminating that line. Yes its a good idea to retain its --sq-quote nature. >> + fclose(fp); >> + bisect_clean_state(); >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + return -1; >> + } >> + } >> + fclose(fp); > > Perhaps > > strbuf_reset(&bisect_names); > if (pathspec_pos < argc) > sq_quote_argv(&bisect_names, argv + pathspec_pos, 0); > write_file(git_path_bisect_names(), "%s\n", bisect_names.buf); > > or something like that? Yes! Thanks! Looks pretty good to me. >> + for (j = 0; j < states.nr; j ++) { > > Again, is "i" still precious here? Style: drop SP between j and ++. After BISECT_NAMES, "i" ceases to be precious. >> + fp = fopen(git_path_bisect_log(), "a"); >> + if (!fp) { >> + bisect_clean_state(); >> + return -1; >> + } >> + if (!fprintf(fp, "git bisect start")) { >> + bisect_clean_state(); >> + return -1; >> + } >> + for (i = 0; i < argc; i++) { >> + if (!fprintf(fp, " '%s'", argv[i])) { >> + fclose(fp); >> + bisect_clean_state(); >> + return -1; >> + } >> + } >> + if (!fprintf(fp, "\n")) { >> + fclose(fp); >> + bisect_clean_state(); >> + return -1; >> + } > > Again, the original writes orig_args which was protected with --sq-quote. Yes. Will take care of it. >> + fclose(fp); >> + >> + return 0; >> +} >> + Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (6 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 13/13] bisect--helper: `bisect_start` shell function partially " Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 18:46 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 12/13] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva ` (5 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement `is_expected_rev` & `check_expected_revs` shell function in C and add a `--check-expected-revs` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--check-expected-revs` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired and will be called by some other method. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 33 ++++++++++++++++++++++++++++++++- git-bisect.sh | 20 ++------------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 0e09630..c0f7091 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -162,13 +162,40 @@ static int bisect_reset(const char *commit) return bisect_clean_state(); } +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) >= 0) { + strbuf_trim(&actual_hex); + res = !strcmp(actual_hex.buf, expected_hex); + } + strbuf_release(&actual_hex); + return res; +} + +static int check_expected_revs(const char **revs, int rev_nr) +{ + int i; + + for (i = 0; i < rev_nr; i++) { + if (!is_expected_rev(revs[i])) { + remove_path(git_path_bisect_ancestors_ok()); + remove_path(git_path_bisect_expected_rev()); + return 0; + } + } + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, BISECT_CLEAN_STATE, - BISECT_RESET + BISECT_RESET, + CHECK_EXPECTED_REVS } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -180,6 +207,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("cleanup the bisection state"), BISECT_CLEAN_STATE), OPT_CMDMODE(0, "bisect-reset", &cmdmode, N_("reset the bisection state"), BISECT_RESET), + 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() @@ -206,6 +235,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); return bisect_reset(argc ? argv[0] : NULL); + case CHECK_EXPECTED_REVS: + return check_expected_revs(argv, argc); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 18580b7..4f6545e 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -238,22 +238,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 $(bisect_head)) || die "$(gettext "Bad rev input: $(bisect_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.")" ;; *) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function " Pranit Bauva @ 2016-08-02 18:46 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 18:46 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > + for (i = 0; i < rev_nr; i++) { > + if (!is_expected_rev(revs[i])) { > + remove_path(git_path_bisect_ancestors_ok()); > + remove_path(git_path_bisect_expected_rev()); > + return 0; Same comment on the use of this helper function applies here, too. ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 12/13] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (7 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function " Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 19:22 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 02/13] bisect: rewrite `check_term_format` " Pranit Bauva ` (4 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement the `get_terms` and `bisect_terms` shell function in C and add `bisect-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . In the shell version, the terms were identified by strings but in C version its done by bit manipulation and passing the integer value to the function. Using `--bisect-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 35 ++--------------------- 2 files changed, 75 insertions(+), 34 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 71f4cf0..81a16a5 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -8,6 +8,13 @@ #include "run-command.h" #include "prompt.h" +enum terms_defined { + TERM_BAD = 1, + TERM_GOOD = 2, + TERM_NEW = 4, + TERM_OLD = 8 +}; + 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") @@ -26,6 +33,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), + N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), NULL }; @@ -359,6 +367,43 @@ static int bisect_next_check(const struct bisect_terms *terms, return 0; } +static int get_terms(struct bisect_terms *terms) +{ + FILE *fp; + int res; + fp = fopen(git_path_bisect_terms(), "r"); + if (!fp) + return -1; + + bisect_terms_reset(terms); + res = strbuf_getline(&terms->term_bad, fp) == EOF || + strbuf_getline(&terms->term_good, fp) == EOF; + + fclose(fp); + return res ? -1 : 0; +} + +static int bisect_terms(struct bisect_terms *terms, int term_defined) +{ + if (get_terms(terms)) { + fprintf(stderr, "no terms defined\n"); + return -1; + } + if (!term_defined) { + printf("Your current terms are %s for the old state\nand " + "%s for the new state.\n", terms->term_good.buf, + terms->term_bad.buf); + return 0; + } + + if (term_defined == TERM_GOOD || term_defined == TERM_OLD) + printf("%s\n", terms->term_good.buf); + if (term_defined == TERM_BAD || term_defined == TERM_NEW) + printf("%s\n", terms->term_bad.buf); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -369,9 +414,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) CHECK_EXPECTED_REVS, BISECT_WRITE, CHECK_AND_SET_TERMS, - BISECT_NEXT_CHECK + BISECT_NEXT_CHECK, + BISECT_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; + enum terms_defined term_defined = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), @@ -389,6 +436,16 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_CMDMODE(0, "bisect-next-check", &cmdmode, N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), + OPT_CMDMODE(0, "bisect-terms", &cmdmode, + N_("print out the bisect terms"), BISECT_TERMS), + OPT_BIT(0, "term-bad", &term_defined, + N_("show the bad term"), TERM_BAD), + OPT_BIT(0, "term-good", &term_defined, + N_("show the good term"), TERM_GOOD), + OPT_BIT(0, "term-new", &term_defined, + N_("show the new term"), TERM_NEW), + OPT_BIT(0, "term-old", &term_defined, + N_("show the old term"), TERM_OLD), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -399,6 +456,16 @@ 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 (cmdmode != BISECT_TERMS && term_defined) + die(_("--term-bad, --term-good, --term-new and --term-old " + "can be used only with --bisect-terms")); + + if (term_defined != 0 && term_defined != TERM_BAD && + term_defined != TERM_GOOD && term_defined != TERM_NEW && + term_defined != TERM_OLD) + die(_("only one option among --term-bad, --term-good, " + "--term-new and --term-old can be used.")); + if (!cmdmode) usage_with_options(git_bisect_helper_usage, options); @@ -446,6 +513,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[1]); res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); break; + case BISECT_TERMS: + if (argc > 1) + die(_("--bisect-terms requires 0 or 1 argument")); + res = bisect_terms(&terms, term_defined); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index c2d6319..aea97c5f 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -355,7 +355,7 @@ bisect_replay () { "$TERM_GOOD"|"$TERM_BAD"|skip) git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) - bisect_terms $rev ;; + git bisect--helper --bisect-terms $rev || exit;; *) die "$(gettext "?? what are you talking about?")" ;; esac @@ -437,37 +437,6 @@ get_terms () { fi } -bisect_terms () { - get_terms - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - die "$(gettext "no terms defined")" - fi - case "$#" in - 0) - gettextln "Your current terms are $TERM_GOOD for the old state -and $TERM_BAD for the new state." - ;; - 1) - arg=$1 - case "$arg" in - --term-good|--term-old) - printf '%s\n' "$TERM_GOOD" - ;; - --term-bad|--term-new) - printf '%s\n' "$TERM_BAD" - ;; - *) - die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'. -Supported options are: --term-good|--term-old and --term-bad|--term-new.")" - ;; - esac - ;; - *) - usage ;; - esac -} - case "$#" in 0) usage ;; @@ -498,7 +467,7 @@ case "$#" in run) bisect_run "$@" ;; terms) - bisect_terms "$@" ;; + git bisect--helper --bisect-terms "$@" || exit;; *) usage ;; esac -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 12/13] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 12/13] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva @ 2016-08-02 19:22 ` Junio C Hamano 2016-08-03 20:33 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 19:22 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static int bisect_terms(struct bisect_terms *terms, int term_defined) > +{ > + if (get_terms(terms)) { > + fprintf(stderr, "no terms defined\n"); > + return -1; > + } > + if (!term_defined) { > + printf("Your current terms are %s for the old state\nand " > + "%s for the new state.\n", terms->term_good.buf, > + terms->term_bad.buf); > + return 0; > + } In the original, all of the above messages go through gettext; this rewrite should do the same. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 12/13] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-08-02 19:22 ` Junio C Hamano @ 2016-08-03 20:33 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:33 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Wed, Aug 3, 2016 at 12:52 AM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +static int bisect_terms(struct bisect_terms *terms, int term_defined) >> +{ >> + if (get_terms(terms)) { >> + fprintf(stderr, "no terms defined\n"); >> + return -1; >> + } >> + if (!term_defined) { >> + printf("Your current terms are %s for the old state\nand " >> + "%s for the new state.\n", terms->term_good.buf, >> + terms->term_bad.buf); >> + return 0; >> + } > > In the original, all of the above messages go through gettext; this > rewrite should do the same. Sure! Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 02/13] bisect: rewrite `check_term_format` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (8 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 12/13] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 17:31 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 09/13] bisect--helper: `bisect_write` " Pranit Bauva ` (3 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement the `check_term_format` shell function in C and add a `--check-term-format` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-term-format` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other method/subcommand. For eg. In conversion of write_terms() of git-bisect.sh, the subcommand will be removed and instead check_term_format() will be called in its C implementation while a new subcommand will be introduced for write_terms(). Helped-by: Johannes Schindelein <Johannes.Schindelein@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 31 ++----------------------- 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 8111c91..48285d4 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -2,19 +2,72 @@ #include "cache.h" #include "parse-options.h" #include "bisect.h" +#include "refs.h" static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), + N_("git bisect--helper --check-term-format <term> <orig_term>"), NULL }; +/* + * Check whether the string `term` belongs to the set of strings + * included in the variable arguments. + */ +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", 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - enum { NEXT_ALL = 1 } cmdmode = 0; + enum { + NEXT_ALL = 1, + CHECK_TERM_FMT + } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), + OPT_CMDMODE(0, "check-term-format", &cmdmode, + N_("check format of the term"), CHECK_TERM_FMT), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -29,6 +82,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); + case CHECK_TERM_FMT: + if (argc != 2) + die(_("--check-term-format requires two arguments")); + return check_term_format(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 5d1cb00..7d7965d 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -564,38 +564,11 @@ write_terms () { then die "$(gettext "please use two different terms")" fi - check_term_format "$TERM_BAD" bad - check_term_format "$TERM_GOOD" good + git bisect--helper --check-term-format "$TERM_BAD" bad || exit + git bisect--helper --check-term-format "$TERM_GOOD" good || exit 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 -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 02/13] bisect: rewrite `check_term_format` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 02/13] bisect: rewrite `check_term_format` " Pranit Bauva @ 2016-08-02 17:31 ` Junio C Hamano 2016-08-03 20:20 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 17:31 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +/* > + * Check whether the string `term` belongs to the set of strings > + * included in the variable arguments. > + */ > +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; > +} It might be safer to mark this function with LAST_ARG_MUST_BE_NULL, but because this is static to this function, it may not matter too much. Just an observation, not a strong suggestion to change the patch. > +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); Yup, that looks much more straight-forward than using a one-time-use strbuf. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 02/13] bisect: rewrite `check_term_format` shell function in C 2016-08-02 17:31 ` Junio C Hamano @ 2016-08-03 20:20 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:20 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Tue, Aug 2, 2016 at 11:01 PM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +/* >> + * Check whether the string `term` belongs to the set of strings >> + * included in the variable arguments. >> + */ >> +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; >> +} > > It might be safer to mark this function with LAST_ARG_MUST_BE_NULL, > but because this is static to this function, it may not matter too > much. Just an observation, not a strong suggestion to change the > patch. Yes, I could do that. >> +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); > > Yup, that looks much more straight-forward than using a one-time-use > strbuf. Thanks! Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 09/13] bisect--helper: `bisect_write` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (9 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 02/13] bisect: rewrite `check_term_format` " Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 20:25 ` Junio C Hamano 2016-07-31 9:21 ` [RFC/PATCH v11 10/13] bisect--helper: `check_and_set_terms` " Pranit Bauva ` (2 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement the `bisect_write` shell function in C and add a `bisect-write` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--bisect-write` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. Note: bisect_write() uses two variables namely TERM_GOOD and TERM_BAD from the global shell script thus we need to pass it to the subcommand using the arguments. We then store them in a struct bisect_terms and pass the memory address around functions. This patch also introduces new methods namely bisect_state_init() and bisect_terms_release() for easy memory management for the struct bisect_terms. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 97 ++++++++++++++++++++++++++++++++++++++++++++---- git-bisect.sh | 25 ++----------- 2 files changed, 94 insertions(+), 28 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index c0f7091..70b953f 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -22,9 +22,27 @@ static const char * const git_bisect_helper_usage[] = { 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 <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), NULL }; +struct bisect_terms { + struct strbuf term_good; + struct strbuf term_bad; +}; + +static void bisect_terms_init(struct bisect_terms *terms) +{ + strbuf_init(&terms->term_good, 0); + strbuf_init(&terms->term_bad, 0); +} + +static void bisect_terms_release(struct bisect_terms *terms) +{ + strbuf_release(&terms->term_good); + strbuf_release(&terms->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -188,6 +206,52 @@ static int check_expected_revs(const char **revs, int rev_nr) return 0; } +static int bisect_write(const char *state, const char *rev, + const struct bisect_terms *terms, int nolog) +{ + struct strbuf tag = STRBUF_INIT; + struct strbuf commit_name = STRBUF_INIT; + struct object_id oid; + struct commit *commit; + struct pretty_print_context pp = {0}; + FILE *fp; + + if (!strcmp(state, terms->term_bad.buf)) + strbuf_addf(&tag, "refs/bisect/%s", state); + else if (one_of(state, terms->term_good.buf, "skip", NULL)) + strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev); + else + return error(_("Bad bisect_write argument: %s"), state); + + if (get_oid(rev, &oid)) { + strbuf_release(&tag); + return error(_("couldn't get the oid of the rev '%s'"), rev); + } + + if (update_ref(NULL, tag.buf, oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + strbuf_release(&tag); + return -1; + } + strbuf_release(&tag); + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) + return error_errno(_("couldn't open the file '%s'"), git_path_bisect_log()); + + commit = lookup_commit_reference(oid.hash); + format_commit_message(commit, "%s", &commit_name, &pp); + fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash), + commit_name.buf); + strbuf_release(&commit_name); + + if (!nolog) + fprintf(fp, "git bisect %s %s\n", state, rev); + + fclose(fp); + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -195,9 +259,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) WRITE_TERMS, BISECT_CLEAN_STATE, BISECT_RESET, - CHECK_EXPECTED_REVS + CHECK_EXPECTED_REVS, + BISECT_WRITE } cmdmode = 0; - int no_checkout = 0; + int no_checkout = 0, res = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), @@ -209,10 +274,14 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("reset the bisection state"), BISECT_RESET), OPT_CMDMODE(0, "check-expected-revs", &cmdmode, N_("check for expected revs"), CHECK_EXPECTED_REVS), + OPT_CMDMODE(0, "bisect-write", &cmdmode, + N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() }; + struct bisect_terms terms; + bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, git_bisect_helper_usage, 0); @@ -221,24 +290,38 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) usage_with_options(git_bisect_helper_usage, options); switch (cmdmode) { + int nolog; case NEXT_ALL: return bisect_next_all(prefix, no_checkout); case WRITE_TERMS: if (argc != 2) die(_("--write-terms requires two arguments")); - return write_terms(argv[0], argv[1]); + res = write_terms(argv[0], argv[1]); + break; case BISECT_CLEAN_STATE: if (argc != 0) die(_("--bisect-clean-state requires no arguments")); - return bisect_clean_state(); + res = bisect_clean_state(); + break; case BISECT_RESET: if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); - return bisect_reset(argc ? argv[0] : NULL); + res = bisect_reset(argc ? argv[0] : NULL); + break; case CHECK_EXPECTED_REVS: - return check_expected_revs(argv, argc); + res = check_expected_revs(argv, argc); + break; + case BISECT_WRITE: + if (argc != 4 && argc != 5) + die(_("--bisect-write requires either 4 or 5 arguments")); + nolog = (argc == 5) && !strcmp(argv[4], "nolog"); + strbuf_addstr(&terms.term_good, argv[2]); + strbuf_addstr(&terms.term_bad, argv[3]); + res = bisect_write(argv[0], argv[1], &terms, nolog); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } - return 0; + bisect_terms_release(&terms); + return res; } diff --git a/git-bisect.sh b/git-bisect.sh index 4f6545e..b9896a4 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -145,7 +145,7 @@ bisect_start() { 0) state=$TERM_BAD ; bad_seen=1 ;; *) state=$TERM_GOOD ;; esac - eval="$eval bisect_write '$state' '$rev' 'nolog' &&" + eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" done # # Verify HEAD. @@ -221,23 +221,6 @@ bisect_start() { trap '-' 0 } -bisect_write() { - state="$1" - rev="$2" - nolog="$3" - case "$state" in - "$TERM_BAD") - tag="$state" ;; - "$TERM_GOOD"|skip) - tag="$state"-"$rev" ;; - *) - die "$(eval_gettext "Bad bisect_write argument: \$state")" ;; - esac - git update-ref "refs/bisect/$tag" "$rev" || exit - echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG" - test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" -} - bisect_skip() { all='' for arg in "$@" @@ -263,7 +246,7 @@ bisect_state() { 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip) rev=$(git rev-parse --verify $(bisect_head)) || die "$(gettext "Bad rev input: $(bisect_head)")" - bisect_write "$state" "$rev" + 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 @@ -276,7 +259,7 @@ bisect_state() { done for rev in $hash_list do - bisect_write "$state" "$rev" + git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit done git bisect--helper --check-expected-revs $hash_list ;; *,"$TERM_BAD") @@ -413,7 +396,7 @@ bisect_replay () { cmd="bisect_start $rev" eval "$cmd" ;; "$TERM_GOOD"|"$TERM_BAD"|skip) - bisect_write "$command" "$rev" ;; + git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) bisect_terms $rev ;; *) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 09/13] bisect--helper: `bisect_write` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 09/13] bisect--helper: `bisect_write` " Pranit Bauva @ 2016-08-02 20:25 ` Junio C Hamano 2016-08-02 22:17 ` Junio C Hamano 2016-08-03 20:51 ` Pranit Bauva 0 siblings, 2 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 20:25 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > Reimplement the `bisect_write` shell function in C and add a > `bisect-write` subcommand to `git bisect--helper` to call it from > git-bisect.sh Up to around this step we've seen these patches well enough and I think with another reroll or two, they are in good enough shape to be split out and frozen for 'next'. We may not be there quite yet, but I think we are getting pretty close. Thanks. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 09/13] bisect--helper: `bisect_write` shell function in C 2016-08-02 20:25 ` Junio C Hamano @ 2016-08-02 22:17 ` Junio C Hamano 2016-08-03 20:52 ` Pranit Bauva 2016-08-03 20:51 ` Pranit Bauva 1 sibling, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 22:17 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Junio C Hamano <gitster@pobox.com> writes: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> Reimplement the `bisect_write` shell function in C and add a >> `bisect-write` subcommand to `git bisect--helper` to call it from >> git-bisect.sh > > Up to around this step we've seen these patches well enough and I > think with another reroll or two, they are in good enough shape to > be split out and frozen for 'next'. We may not be there quite yet, > but I think we are getting pretty close. > > Thanks. By the way, this series applied as a whole seems to break t6030. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 09/13] bisect--helper: `bisect_write` shell function in C 2016-08-02 22:17 ` Junio C Hamano @ 2016-08-03 20:52 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:52 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Wed, Aug 3, 2016 at 3:47 AM, Junio C Hamano <gitster@pobox.com> wrote: > Junio C Hamano <gitster@pobox.com> writes: > >> Pranit Bauva <pranit.bauva@gmail.com> writes: >> >>> Reimplement the `bisect_write` shell function in C and add a >>> `bisect-write` subcommand to `git bisect--helper` to call it from >>> git-bisect.sh >> >> Up to around this step we've seen these patches well enough and I >> think with another reroll or two, they are in good enough shape to >> be split out and frozen for 'next'. We may not be there quite yet, >> but I think we are getting pretty close. >> >> Thanks. > > By the way, this series applied as a whole seems to break t6030. This is only because of argument handling problem. I seemed to have mentioned it in patch 13/13 and also gave the output of the tests. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 09/13] bisect--helper: `bisect_write` shell function in C 2016-08-02 20:25 ` Junio C Hamano 2016-08-02 22:17 ` Junio C Hamano @ 2016-08-03 20:51 ` Pranit Bauva 1 sibling, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:51 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Wed, Aug 3, 2016 at 1:55 AM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> Reimplement the `bisect_write` shell function in C and add a >> `bisect-write` subcommand to `git bisect--helper` to call it from >> git-bisect.sh > > Up to around this step we've seen these patches well enough and I > think with another reroll or two, they are in good enough shape to > be split out and frozen for 'next'. We may not be there quite yet, > but I think we are getting pretty close. Thanks for taking out your time to review this series. I will follow up with the patches in a day or so. I think the first 11, many things wouldn't change (except for your reviews). Most of the changes would happen in 12 and 13 and more specifically the argument handling. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [RFC/PATCH v11 10/13] bisect--helper: `check_and_set_terms` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (10 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 09/13] bisect--helper: `bisect_write` " Pranit Bauva @ 2016-07-31 9:21 ` Pranit Bauva 2016-08-02 18:53 ` Junio C Hamano 2016-08-02 17:25 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Junio C Hamano 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-07-31 9:21 UTC (permalink / raw) To: git Reimplement the `check_and_set_terms` shell function in C and add `check-and-set-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-and-set-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. check_and_set_terms() sets and receives two global variables namely TERM_GOOD and TERM_BAD in the shell script. Luckily the file BISECT_TERMS also contains the value of those variables so its appropriate to evoke the method get_terms() after calling the subcommand so that it retrieves the value of TERM_GOOD and TERM_BAD from the file BISECT_TERMS. The two global variables are passed as arguments to the subcommand. Also introduce bisect_terms_reset() to empty the contents of `term_good` and `term_bad` of `struct bisect_terms`. Also introduce set_terms() to copy the `term_good` and `term_bad` into `struct bisect_terms` and write it out to the file BISECT_TERMS. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 36 ++++----------------------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 70b953f..99c9f90 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -23,6 +23,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-clean-state"), N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), + N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), NULL }; @@ -43,6 +44,12 @@ static void bisect_terms_release(struct bisect_terms *terms) strbuf_release(&terms->term_bad); } +static void bisect_terms_reset(struct bisect_terms *term) +{ + strbuf_reset(&term->term_good); + strbuf_reset(&term->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -252,6 +259,39 @@ static int bisect_write(const char *state, const char *rev, return 0; } +static int set_terms(struct bisect_terms *terms, const char *bad, + const char *good) +{ + bisect_terms_reset(terms); + strbuf_addstr(&terms->term_good, good); + strbuf_addstr(&terms->term_bad, bad); + return write_terms(terms->term_bad.buf, terms->term_good.buf); +} + +static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) +{ + int no_term_file = is_empty_or_missing_file(git_path_bisect_terms()); + + if (one_of(cmd, "skip", "start", "terms", NULL)) + return 0; + + if (!no_term_file && + strcmp(cmd, terms->term_bad.buf) && + strcmp(cmd, terms->term_good.buf)) + return error(_("Invalid command: you're currently in a " + "'%s' '%s' bisect"), terms->term_bad.buf, + terms->term_good.buf); + + if (no_term_file) { + if (one_of(cmd, "bad", "good", NULL)) + return set_terms(terms, "bad", "good"); + if (one_of(cmd, "new", "old", NULL)) + return set_terms(terms, "new", "old"); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -260,7 +300,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_CLEAN_STATE, BISECT_RESET, CHECK_EXPECTED_REVS, - BISECT_WRITE + BISECT_WRITE, + CHECK_AND_SET_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -276,6 +317,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check for expected revs"), CHECK_EXPECTED_REVS), OPT_CMDMODE(0, "bisect-write", &cmdmode, N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), + OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, + N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -319,6 +362,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[3]); res = bisect_write(argv[0], argv[1], &terms, nolog); break; + case CHECK_AND_SET_TERMS: + if (argc != 3) + die(_("--check-and-set-terms requires 3 arguments")); + strbuf_addstr(&terms.term_good, argv[1]); + strbuf_addstr(&terms.term_bad, argv[2]); + res = check_and_set_terms(&terms, argv[0]); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index b9896a4..a41e69b 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -239,7 +239,8 @@ bisect_skip() { bisect_state() { bisect_autostart state=$1 - check_and_set_terms $state + git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit + get_terms case "$#,$state" in 0,*) die "$(gettext "Please call 'bisect_state' with at least one argument.")" ;; @@ -390,7 +391,8 @@ bisect_replay () { command="$bisect" fi get_terms - check_and_set_terms "$command" + git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit + get_terms case "$command" in start) cmd="bisect_start $rev" @@ -480,36 +482,6 @@ get_terms () { fi } -check_and_set_terms () { - cmd="$1" - case "$cmd" in - skip|start|terms) ;; - *) - if test -s "$GIT_DIR/BISECT_TERMS" && test "$cmd" != "$TERM_BAD" && test "$cmd" != "$TERM_GOOD" - then - die "$(eval_gettext "Invalid command: you're currently in a \$TERM_BAD/\$TERM_GOOD bisect.")" - fi - case "$cmd" in - bad|good) - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - 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 - TERM_BAD=new - TERM_GOOD=old - git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit - fi - ;; - esac ;; - esac -} - bisect_voc () { case "$1" in bad) echo "bad|new" ;; -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 10/13] bisect--helper: `check_and_set_terms` shell function in C 2016-07-31 9:21 ` [RFC/PATCH v11 10/13] bisect--helper: `check_and_set_terms` " Pranit Bauva @ 2016-08-02 18:53 ` Junio C Hamano 2016-08-03 20:29 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 18:53 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > Reimplement the `check_and_set_terms` shell function in C and add > `check-and-set-terms` subcommand to `git bisect--helper` to call it from > git-bisect.sh > > Using `--check-and-set-terms` subcommand is a temporary measure to port > shell function in C so as to use the existing test suite. As more > functions are ported, this subcommand will be retired and will be called > by some other methods. I think "this subcommand will be retired but its implementation will be called by ..." would clarify the direction. > + if (!no_term_file && > + strcmp(cmd, terms->term_bad.buf) && > + strcmp(cmd, terms->term_good.buf)) > + return error(_("Invalid command: you're currently in a " > + "'%s' '%s' bisect"), terms->term_bad.buf, This changes a message text, switching from "... good/bad bisect." to "... 'good' 'bad' bisect". Intended? ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 10/13] bisect--helper: `check_and_set_terms` shell function in C 2016-08-02 18:53 ` Junio C Hamano @ 2016-08-03 20:29 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-03 20:29 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Wed, Aug 3, 2016 at 12:23 AM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> Reimplement the `check_and_set_terms` shell function in C and add >> `check-and-set-terms` subcommand to `git bisect--helper` to call it from >> git-bisect.sh >> >> Using `--check-and-set-terms` subcommand is a temporary measure to port >> shell function in C so as to use the existing test suite. As more >> functions are ported, this subcommand will be retired and will be called >> by some other methods. > > I think "this subcommand will be retired but its implementation will > be called by ..." would clarify the direction. Sure. That seems better. >> + if (!no_term_file && >> + strcmp(cmd, terms->term_bad.buf) && >> + strcmp(cmd, terms->term_good.buf)) >> + return error(_("Invalid command: you're currently in a " >> + "'%s' '%s' bisect"), terms->term_bad.buf, > > This changes a message text, switching from "... good/bad bisect." > to "... 'good' 'bad' bisect". Intended? Nope its not intended but its a mistake from my side. Will rectify. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (11 preceding siblings ...) 2016-07-31 9:21 ` [RFC/PATCH v11 10/13] bisect--helper: `check_and_set_terms` " Pranit Bauva @ 2016-08-02 17:25 ` Junio C Hamano 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva 13 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-02 17:25 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > `--next-all` is meant to be used as a subcommand to support multiple > "operation mode" though the current implementation does not contain any > other subcommand along side with `--next-all` but further commits will > include some more subcommands. Sounds sensible. As long as the dispatch happens inside cmd_bisect__helper() itself, limiting the enum definition local to the function also looks like a good thing to do (and I do not see a reason why we need the world outside this function to know about the enum in the future). > Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> > Mentored-by: Lars Schneider <larsxschneider@gmail.com> > Mentored-by: Christian Couder <chriscool@tuxfamily.org> > Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> > --- > builtin/bisect--helper.c | 17 +++++++++++------ > 1 file changed, 11 insertions(+), 6 deletions(-) > > diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c > index 3324229..8111c91 100644 > --- a/builtin/bisect--helper.c > +++ b/builtin/bisect--helper.c > @@ -10,11 +10,11 @@ static const char * const git_bisect_helper_usage[] = { > > int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > { > - int next_all = 0; > + enum { NEXT_ALL = 1 } 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_BOOL(0, "no-checkout", &no_checkout, > N_("update BISECT_HEAD instead of checking out the current commit")), > OPT_END() > @@ -23,9 +23,14 @@ 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); > + default: > + die("BUG: unknown subcommand '%d'", cmdmode); > + } > + return 0; > } > > -- > https://github.com/git/git/pull/281 ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v12 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-07-31 9:21 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva ` (12 preceding siblings ...) 2016-08-02 17:25 ` [RFC/PATCH v11 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Junio C Hamano @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 03/13] bisect--helper: `write_terms` shell function in C Pranit Bauva ` (13 more replies) 13 siblings, 14 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git `--next-all` is meant to be used as a subcommand to support multiple "operation mode" though the current implementation does not contain any other subcommand along side with `--next-all` but further commits will include some more subcommands. Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3324229..8111c91 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -10,11 +10,11 @@ static const char * const git_bisect_helper_usage[] = { int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - int next_all = 0; + enum { NEXT_ALL = 1 } 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -23,9 +23,14 @@ 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); + default: + die("BUG: unknown subcommand '%d'", cmdmode); + } + return 0; } -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 03/13] bisect--helper: `write_terms` shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 12/13] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva ` (12 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement the `write_terms` shell function in C and add a `write-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also remove the subcommand `--check-term-format` as it can now be called from inside the function write_terms() C implementation. Also `|| exit` is added when calling write-terms subcommand from git-bisect.sh so as to exit whenever there is an error. Using `--write-terms` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and its implementation will be called by some other method. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 36 +++++++++++++++++++++++++++++------- git-bisect.sh | 22 +++++++--------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index a47f1f2..30e1031 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,9 +4,11 @@ #include "bisect.h" #include "refs.h" +static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") + static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), - N_("git bisect--helper --check-term-format <term> <orig_term>"), + N_("git bisect--helper --write-terms <bad_term> <good_term>"), NULL }; @@ -57,18 +59,38 @@ static int check_term_format(const char *term, const char *orig_term) return 0; } +static int write_terms(const char *bad, const char *good) +{ + FILE *fp; + 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - CHECK_TERM_FMT + WRITE_TERMS } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), - OPT_CMDMODE(0, "check-term-format", &cmdmode, - N_("check format of the term"), CHECK_TERM_FMT), + OPT_CMDMODE(0, "write-terms", &cmdmode, + N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -83,10 +105,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); - case CHECK_TERM_FMT: + case WRITE_TERMS: if (argc != 2) - die(_("--check-term-format requires two arguments")); - return check_term_format(argv[0], argv[1]); + die(_("--write-terms requires two arguments")); + return write_terms(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 7d7965d..cd39bd0 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -210,7 +210,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" fi && echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit # @@ -557,18 +557,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 - git bisect--helper --check-term-format "$TERM_BAD" bad || exit - git bisect--helper --check-term-format "$TERM_GOOD" good || exit - printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS" -} - check_and_set_terms () { cmd="$1" case "$cmd" in @@ -582,13 +570,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 ;; -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 12/13] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 03/13] bisect--helper: `write_terms` shell function in C Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 04/13] bisect--helper: `bisect_clean_state` " Pranit Bauva ` (11 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement the `get_terms` and `bisect_terms` shell function in C and add `bisect-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++-- git-bisect.sh | 35 ++-------------------------- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index b6e9973..f912010 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -26,6 +26,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), + N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), NULL }; @@ -384,6 +385,52 @@ static int bisect_next_check(const struct bisect_terms *terms, return 0; } +static int get_terms(struct bisect_terms *terms) +{ + FILE *fp; + int res; + fp = fopen(git_path_bisect_terms(), "r"); + if (!fp) + return -1; + + bisect_terms_reset(terms); + res = strbuf_getline(&terms->term_bad, fp) == EOF || + strbuf_getline(&terms->term_good, fp) == EOF; + + fclose(fp); + return res ? -1 : 0; +} + +static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) +{ + int i; + + if (get_terms(terms)) { + fprintf(stderr, N_("no terms defined\n")); + return -1; + } + if (argc == 0) { + printf(N_("Your current terms are %s for the old state\nand " + "%s for the new state.\n"), terms->term_good.buf, + terms->term_bad.buf); + return 0; + } + + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "--term-good")) + printf(N_("%s\n"), terms->term_good.buf); + else if (!strcmp(argv[i], "--term-bad")) + printf(N_("%s\n"), terms->term_bad.buf); + else + printf(N_("invalid argument %s for 'git bisect " + "terms'.\nSupported options are: " + "--term-good|--term-old and " + "--term-bad|--term-new."), argv[i]); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -394,7 +441,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) CHECK_EXPECTED_REVS, BISECT_WRITE, CHECK_AND_SET_TERMS, - BISECT_NEXT_CHECK + BISECT_NEXT_CHECK, + BISECT_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -414,6 +462,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_CMDMODE(0, "bisect-next-check", &cmdmode, N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), + OPT_CMDMODE(0, "bisect-terms", &cmdmode, + N_("print out the bisect terms"), BISECT_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -422,7 +472,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, - git_bisect_helper_usage, 0); + git_bisect_helper_usage, PARSE_OPT_KEEP_UNKNOWN); if (!cmdmode) usage_with_options(git_bisect_helper_usage, options); @@ -471,6 +521,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[1]); res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); break; + case BISECT_TERMS: + if (argc > 1) + die(_("--bisect-terms requires 0 or 1 argument")); + res = bisect_terms(&terms, argv, argc); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index c2d6319..aea97c5f 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -355,7 +355,7 @@ bisect_replay () { "$TERM_GOOD"|"$TERM_BAD"|skip) git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) - bisect_terms $rev ;; + git bisect--helper --bisect-terms $rev || exit;; *) die "$(gettext "?? what are you talking about?")" ;; esac @@ -437,37 +437,6 @@ get_terms () { fi } -bisect_terms () { - get_terms - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - die "$(gettext "no terms defined")" - fi - case "$#" in - 0) - gettextln "Your current terms are $TERM_GOOD for the old state -and $TERM_BAD for the new state." - ;; - 1) - arg=$1 - case "$arg" in - --term-good|--term-old) - printf '%s\n' "$TERM_GOOD" - ;; - --term-bad|--term-new) - printf '%s\n' "$TERM_BAD" - ;; - *) - die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'. -Supported options are: --term-good|--term-old and --term-bad|--term-new.")" - ;; - esac - ;; - *) - usage ;; - esac -} - case "$#" in 0) usage ;; @@ -498,7 +467,7 @@ case "$#" in run) bisect_run "$@" ;; terms) - bisect_terms "$@" ;; + git bisect--helper --bisect-terms "$@" || exit;; *) usage ;; esac -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 03/13] bisect--helper: `write_terms` shell function in C Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 12/13] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-12 19:24 ` Junio C Hamano 2016-08-10 21:57 ` [PATCH v12 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva ` (10 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement `bisect_clean_state` shell function in C and add a `bisect-clean-state` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-clean-state` subcommand is a measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by bisect_reset() and bisect_start(). Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 26 +++-------------------- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 30e1031..3fffa78 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -3,12 +3,21 @@ #include "parse-options.h" #include "bisect.h" #include "refs.h" +#include "dir.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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") +static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") +static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") +static GIT_PATH_FUNC(git_path_head_name, "head-name") +static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") 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 }; @@ -79,11 +88,49 @@ static int write_terms(const char *bad, const char *good) return (res < 0) ? -1 : 0; } +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; +} + +static 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(&refs_for_removal); + 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - WRITE_TERMS + WRITE_TERMS, + BISECT_CLEAN_STATE } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -91,6 +138,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -109,6 +158,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 2) die(_("--write-terms requires two arguments")); return write_terms(argv[0], argv[1]); + case BISECT_CLEAN_STATE: + if (argc != 0) + die(_("--bisect-clean-state requires no arguments")); + return bisect_clean_state(); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index cd39bd0..bbc57d2 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -187,7 +187,7 @@ bisect_start() { # # Get rid of any old bisect state. # - bisect_clean_state || exit + git bisect--helper --bisect-clean-state || exit # # Change state. @@ -196,7 +196,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 # @@ -430,27 +430,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 () { -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v12 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-10 21:57 ` [PATCH v12 04/13] bisect--helper: `bisect_clean_state` " Pranit Bauva @ 2016-08-12 19:24 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-12 19:24 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static 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(&refs_for_removal); I think this function has changed its signature recently. I am planning to tag 2.10-rc0 this weekend, and it may be a good time to rebase the series on to an updated codebase. ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v12 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (2 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 04/13] bisect--helper: `bisect_clean_state` " Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C Pranit Bauva ` (9 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git is_empty_file() can help to refactor a lot of code. This will be very helpful in porting "git bisect" to C. Suggested-by: Torsten Bögershausen <tboegi@web.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/am.c | 20 ++------------------ cache.h | 3 +++ wrapper.c | 13 +++++++++++++ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 3dfe70b..6ee158f 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -30,22 +30,6 @@ #include "mailinfo.h" /** - * Returns 1 if the file is empty or does not exist, 0 otherwise. - */ -static int is_empty_file(const char *filename) -{ - struct stat st; - - if (stat(filename, &st) < 0) { - if (errno == ENOENT) - return 1; - die_errno(_("could not stat %s"), filename); - } - - return !st.st_size; -} - -/** * Returns the length of the first line of msg. */ static int linelen(const char *msg) @@ -1323,7 +1307,7 @@ static int parse_mail(struct am_state *state, const char *mail) goto finish; } - if (is_empty_file(am_path(state, "patch"))) { + if (is_empty_or_missing_file(am_path(state, "patch"))) { printf_ln(_("Patch is empty. Was it split wrong?")); die_user_resolve(state); } @@ -1911,7 +1895,7 @@ static void am_run(struct am_state *state, int resume) resume = 0; } - if (!is_empty_file(am_path(state, "rewritten"))) { + if (!is_empty_or_missing_file(am_path(state, "rewritten"))) { assert(state->rebasing); copy_notes_for_rebase(state); run_post_rewrite_hook(state); diff --git a/cache.h b/cache.h index 6049f86..91e2f81 100644 --- a/cache.h +++ b/cache.h @@ -1870,4 +1870,7 @@ void sleep_millisec(int millisec); */ void safe_create_dir(const char *dir, int share); +/* Return 1 if the file is empty or does not exists, 0 otherwise. */ +extern int is_empty_or_missing_file(const char *filename); + #endif /* CACHE_H */ diff --git a/wrapper.c b/wrapper.c index 5dc4e15..e70e4d1 100644 --- a/wrapper.c +++ b/wrapper.c @@ -696,3 +696,16 @@ void sleep_millisec(int millisec) { poll(NULL, 0, millisec); } + +int is_empty_or_missing_file(const char *filename) +{ + struct stat st; + + if (stat(filename, &st) < 0) { + if (errno == ENOENT) + return 1; + die_errno(_("could not stat %s"), filename); + } + + return !st.st_size; +} -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (3 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-12 19:25 ` Junio C Hamano 2016-08-13 7:34 ` Christian Couder 2016-08-10 21:57 ` [PATCH v12 10/13] bisect--helper: `check_and_set_terms` shell function " Pranit Bauva ` (8 subsequent siblings) 13 siblings, 2 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement the `bisect_start` shell function partially in C and add `bisect-start` subcommand to `git bisect--helper` to call it from git-bisect.sh . The last part is not converted because it calls another shell function. `bisect_start` shell function will be completed after the `bisect_next` shell function is ported in C. Using `--bisect-start` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 254 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 133 +------------------------ 2 files changed, 254 insertions(+), 133 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index f912010..80116ba 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -7,6 +7,7 @@ #include "argv-array.h" #include "run-command.h" #include "prompt.h" +#include "quote.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -27,6 +28,8 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), + N_("git bisect--helper --bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]" + "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"), NULL }; @@ -431,6 +434,244 @@ static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) return 0; } +static int bisect_start(struct bisect_terms *terms, int no_checkout, + const char **argv, int argc) +{ + int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0; + int flags, pathspec_pos; + struct string_list revs = STRING_LIST_INIT_DUP; + struct string_list states = STRING_LIST_INIT_DUP; + struct strbuf start_head = STRBUF_INIT; + struct strbuf bisect_names = STRBUF_INIT; + struct strbuf orig_args = STRBUF_INIT; + const char *head; + unsigned char sha1[20]; + FILE *fp; + struct object_id oid; + + if (is_bare_repository()) + no_checkout = 1; + + for (i = 0; i < argc; i++) { + char *commit_id = xstrfmt("%s^{commit}", argv[i]); + if (!strcmp(argv[i], "--")) { + has_double_dash = 1; + break; + } + else if (!strcmp(argv[i], "--no-checkout")) + no_checkout = 1; + else if (!strcmp(argv[i], "--term-good") || + !strcmp(argv[i], "--term-old")) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[++i]); + } + else if (skip_prefix(argv[i], "--term-good=", &argv[i])) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[i]); + } + else if (skip_prefix(argv[i], "--term-old=", &argv[i])) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[i]); + } + else if (!strcmp(argv[i], "--term-bad") || + !strcmp(argv[i], "--term-new")) { + must_write_terms = 1; + strbuf_reset(&terms->term_bad); + strbuf_addstr(&terms->term_bad, argv[++i]); + } + else if (skip_prefix(argv[i], "--term-bad=", &argv[i])) { + must_write_terms = 1; + strbuf_reset(&terms->term_bad); + strbuf_addstr(&terms->term_bad, argv[i]); + } + else if (skip_prefix(argv[i], "--term-new=", &argv[i])) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[i]); + } + else if (starts_with(argv[i], "--") && + !one_of(argv[i], "--term-good", "--term-bad", NULL)) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("unrecognised option: '%s'"), argv[i]); + } + else if (get_oid(argv[i], &oid) && !has_double_dash) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + free(commit_id); + die(_("'%s' does not appear to be a valid revision"), argv[i]); + } + else { + free(commit_id); + string_list_append(&revs, oid_to_hex(&oid)); + } + } + pathspec_pos = i; + + /* + * The user ran "git bisect start <sha1> <sha1>", hence did not + * explicitly specify the terms, but we are already starting to + * set references named with the default terms, and won't be able + * to change afterwards. + */ + must_write_terms |= !!revs.nr; + for (i = 0; i < revs.nr; i++) { + if (bad_seen) + string_list_append(&states, terms->term_good.buf); + else { + bad_seen = 1; + string_list_append(&states, terms->term_bad.buf); + } + } + + /* + * Verify HEAD + */ + head = resolve_ref_unsafe("HEAD", 0, sha1, &flags); + if (!head) { + if (get_sha1("HEAD", sha1)) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("Bad HEAD - I need a HEAD")); + } + } + if (!is_empty_or_missing_file(git_path_bisect_start())) { + /* Reset to the rev from where we started */ + strbuf_read_file(&start_head, git_path_bisect_start(), 0); + strbuf_trim(&start_head); + if (!no_checkout) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", start_head.buf, + "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("checking out '%s' failed. Try 'git " + "bisect start <valid-branch>'."), + start_head.buf); + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + } else { + if (starts_with(head, "refs/heads/") || + !get_oid_hex(head, &oid) || ref_exists(head)) { + /* + * This error message should only be triggered by + * cogito usage, and cogito users should understand + * it relates to cg-seek. + */ + if (!is_empty_or_missing_file(git_path_head_name())) { + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("won't bisect on cg-seek'ed tree")); + } + if (starts_with(head, "refs/heads/")) { + strbuf_reset(&start_head); + strbuf_addstr(&start_head, head + 11); + } + else { + strbuf_reset(&start_head); + strbuf_addstr(&start_head, sha1_to_hex(sha1)); + } + } else { + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("Bad HEAD - strange symbolic ref")); + } + } + + /* + * 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". + */ + + /* + * Write new start state + */ + if (write_file(git_path_bisect_start(), "%s\n", start_head.buf)) { + bisect_clean_state(); + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + + if (no_checkout) { + get_oid(start_head.buf, &oid); + if (update_ref(NULL, "BISECT_HEAD", oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + bisect_clean_state(); + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + strbuf_release(&start_head); + + if (pathspec_pos < argc - 1) + sq_quote_argv(&bisect_names, argv + pathspec_pos, 0); + write_file(git_path_bisect_names(), "%s\n", bisect_names.buf); + strbuf_release(&bisect_names); + + for (i = 0; i < states.nr; i++) { + if (bisect_write(states.items[i].string, + revs.items[i].string, terms, 1)) { + bisect_clean_state(); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + + if (must_write_terms) + if (write_terms(terms->term_bad.buf, terms->term_good.buf)) { + bisect_clean_state(); + return -1; + } + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) { + bisect_clean_state(); + return -1; + } + if (fprintf(fp, "git bisect start") < 1) { + bisect_clean_state(); + return -1; + } + sq_quote_argv(&orig_args, argv, 0); + if (fprintf(fp, "%s", orig_args.buf) < 0) { + bisect_clean_state(); + strbuf_release(&orig_args); + return -1; + } + strbuf_release(&orig_args); + if (fprintf(fp, "\n") < 1) { + fclose(fp); + bisect_clean_state(); + return -1; + } + fclose(fp); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -442,7 +683,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_WRITE, CHECK_AND_SET_TERMS, BISECT_NEXT_CHECK, - BISECT_TERMS + BISECT_TERMS, + BISECT_START } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -464,6 +706,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), OPT_CMDMODE(0, "bisect-terms", &cmdmode, N_("print out the bisect terms"), BISECT_TERMS), + OPT_CMDMODE(0, "bisect-start", &cmdmode, + N_("start the bisect session"), BISECT_START), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -472,7 +716,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, - git_bisect_helper_usage, PARSE_OPT_KEEP_UNKNOWN); + git_bisect_helper_usage, + PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN); if (!cmdmode) usage_with_options(git_bisect_helper_usage, options); @@ -526,6 +771,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) die(_("--bisect-terms requires 0 or 1 argument")); res = bisect_terms(&terms, argv, argc); break; + case BISECT_START: + strbuf_addstr(&terms.term_good, "good"); + strbuf_addstr(&terms.term_bad, "bad"); + res = bisect_start(&terms, no_checkout, argv, argc); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index aea97c5f..ee504ca 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -72,122 +72,7 @@ bisect_autostart() { } bisect_start() { - # - # Check for one bad and then some good revisions. - # - has_double_dash=0 - for arg; do - case "$arg" in --) has_double_dash=1; break ;; esac - done - orig_args=$(git rev-parse --sq-quote "$@") - bad_seen=0 - eval='' - must_write_terms=0 - revs='' - if test "z$(git rev-parse --is-bare-repository)" != zfalse - then - mode=--no-checkout - else - mode='' - fi - while [ $# -gt 0 ]; do - arg="$1" - case "$arg" in - --) - shift - break - ;; - --no-checkout) - mode=--no-checkout - shift ;; - --term-good|--term-old) - shift - must_write_terms=1 - TERM_GOOD=$1 - shift ;; - --term-good=*|--term-old=*) - must_write_terms=1 - TERM_GOOD=${1#*=} - shift ;; - --term-bad|--term-new) - shift - must_write_terms=1 - TERM_BAD=$1 - shift ;; - --term-bad=*|--term-new=*) - must_write_terms=1 - TERM_BAD=${1#*=} - shift ;; - --*) - die "$(eval_gettext "unrecognised option: '\$arg'")" ;; - *) - 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" - shift - ;; - esac - done - - for rev in $revs - do - # The user ran "git bisect start <sha1> - # <sha1>", hence did not explicitly specify - # the terms, but we are already starting to - # set references named with the default terms, - # and won't be able to change afterwards. - must_write_terms=1 - - case $bad_seen in - 0) state=$TERM_BAD ; bad_seen=1 ;; - *) state=$TERM_GOOD ;; - esac - eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" - done - # - # Verify HEAD. - # - head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) || - head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) || - die "$(gettext "Bad HEAD - I need a HEAD")" - - # - # Check if we are bisecting. - # - start_head='' - if test -s "$GIT_DIR/BISECT_START" - then - # Reset to the rev from where we started. - start_head=$(cat "$GIT_DIR/BISECT_START") - if test "z$mode" != "z--no-checkout" - then - git checkout "$start_head" -- || - die "$(eval_gettext "Checking out '\$start_head' failed. Try 'git bisect reset <valid-branch>'.")" - fi - else - # Get rev from where we start. - case "$head" in - refs/heads/*|$_x40) - # This error message should only be triggered by - # cogito usage, and cogito users should understand - # it relates to cg-seek. - [ -s "$GIT_DIR/head-name" ] && - die "$(gettext "won't bisect on cg-seek'ed tree")" - start_head="${head#refs/heads/}" - ;; - *) - die "$(gettext "Bad HEAD - strange symbolic ref")" - ;; - esac - fi - - # - # Get rid of any old bisect state. - # - git bisect--helper --bisect-clean-state || exit + git bisect--helper --bisect-start $@ || exit # # Change state. @@ -198,24 +83,10 @@ bisect_start() { # trap 'git bisect--helper --bisect-clean-state' 0 trap 'exit 255' 1 2 3 15 - - # - # Write new start state. - # - echo "$start_head" >"$GIT_DIR/BISECT_START" && { - test "z$mode" != "z--no-checkout" || - git update-ref --no-deref BISECT_HEAD "$start_head" - } && - git rev-parse --sq-quote "$@" >"$GIT_DIR/BISECT_NAMES" && - eval "$eval true" && - if test $must_write_terms -eq 1 - then - git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" - fi && - echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit # # Check if we can proceed to the next bisect state. # + get_terms bisect_auto_next trap '-' 0 -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-08-10 21:57 ` [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C Pranit Bauva @ 2016-08-12 19:25 ` Junio C Hamano 2016-08-13 6:33 ` Pranit Bauva 2016-08-13 7:34 ` Christian Couder 1 sibling, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-12 19:25 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > + ... > + /* > + * Write new start state > + */ > + if (write_file(git_path_bisect_start(), "%s\n", start_head.buf)) { I think this function has changed its signature recently. I am planning to tag 2.10-rc0 this weekend, and it may be a good time to rebase the series on to an updated codebase. Thanks. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-08-12 19:25 ` Junio C Hamano @ 2016-08-13 6:33 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-13 6:33 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Sat, Aug 13, 2016 at 12:55 AM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> + ... >> + /* >> + * Write new start state >> + */ >> + if (write_file(git_path_bisect_start(), "%s\n", start_head.buf)) { > > I think this function has changed its signature recently. I am > planning to tag 2.10-rc0 this weekend, and it may be a good time to > rebase the series on to an updated codebase. > > Thanks. Yes sure will do that and send an updated version. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-08-10 21:57 ` [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C Pranit Bauva 2016-08-12 19:25 ` Junio C Hamano @ 2016-08-13 7:34 ` Christian Couder 2016-08-13 13:50 ` Pranit Bauva 1 sibling, 1 reply; 320+ messages in thread From: Christian Couder @ 2016-08-13 7:34 UTC (permalink / raw) To: Pranit Bauva; +Cc: git On Wed, Aug 10, 2016 at 11:57 PM, Pranit Bauva <pranit.bauva@gmail.com> wrote: > > @@ -431,6 +434,244 @@ static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) > return 0; > } > > +static int bisect_start(struct bisect_terms *terms, int no_checkout, > + const char **argv, int argc) > +{ > + int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0; > + int flags, pathspec_pos; > + struct string_list revs = STRING_LIST_INIT_DUP; > + struct string_list states = STRING_LIST_INIT_DUP; > + struct strbuf start_head = STRBUF_INIT; > + struct strbuf bisect_names = STRBUF_INIT; > + struct strbuf orig_args = STRBUF_INIT; > + const char *head; > + unsigned char sha1[20]; > + FILE *fp; > + struct object_id oid; > + > + if (is_bare_repository()) > + no_checkout = 1; > + > + for (i = 0; i < argc; i++) { > + char *commit_id = xstrfmt("%s^{commit}", argv[i]); > + if (!strcmp(argv[i], "--")) { > + has_double_dash = 1; > + break; > + } In the shell code there is a loop dedicated to checking if there is a double dash in the arguments before the real argument parsing loop. There is a reason for that. If you do it in the same loop, has_double_dash will not be set when the arguments that are before the double dash will be parsed. > + else if (!strcmp(argv[i], "--no-checkout")) > + no_checkout = 1; > + else if (!strcmp(argv[i], "--term-good") || > + !strcmp(argv[i], "--term-old")) { > + must_write_terms = 1; > + strbuf_reset(&terms->term_good); > + strbuf_addstr(&terms->term_good, argv[++i]); > + } > + else if (skip_prefix(argv[i], "--term-good=", &argv[i])) { (Maybe you could put the "else if (...) {" on the same line as the "}" above.) > + must_write_terms = 1; > + strbuf_reset(&terms->term_good); > + strbuf_addstr(&terms->term_good, argv[i]); > + } > + else if (skip_prefix(argv[i], "--term-old=", &argv[i])) { > + must_write_terms = 1; > + strbuf_reset(&terms->term_good); > + strbuf_addstr(&terms->term_good, argv[i]); > + } > + else if (!strcmp(argv[i], "--term-bad") || > + !strcmp(argv[i], "--term-new")) { > + must_write_terms = 1; > + strbuf_reset(&terms->term_bad); > + strbuf_addstr(&terms->term_bad, argv[++i]); > + } > + else if (skip_prefix(argv[i], "--term-bad=", &argv[i])) { > + must_write_terms = 1; > + strbuf_reset(&terms->term_bad); > + strbuf_addstr(&terms->term_bad, argv[i]); > + } > + else if (skip_prefix(argv[i], "--term-new=", &argv[i])) { > + must_write_terms = 1; > + strbuf_reset(&terms->term_good); > + strbuf_addstr(&terms->term_good, argv[i]); > + } > + else if (starts_with(argv[i], "--") && > + !one_of(argv[i], "--term-good", "--term-bad", NULL)) { > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + die(_("unrecognised option: '%s'"), argv[i]); > + } > + else if (get_oid(argv[i], &oid) && !has_double_dash) { And here checking "!has_double_dash" has no meaning if you check for a double dash in the same loop, because there is a "break" after has_double_dash is set above. > + string_list_clear(&revs, 0); > + string_list_clear(&states, 0); > + free(commit_id); > + die(_("'%s' does not appear to be a valid revision"), argv[i]); > + } > + else { > + free(commit_id); > + string_list_append(&revs, oid_to_hex(&oid)); > + } > + } > + pathspec_pos = i; > + ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-08-13 7:34 ` Christian Couder @ 2016-08-13 13:50 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-13 13:50 UTC (permalink / raw) To: Christian Couder; +Cc: git Hey Christian, On Sat, Aug 13, 2016 at 1:04 PM, Christian Couder <christian.couder@gmail.com> wrote: > On Wed, Aug 10, 2016 at 11:57 PM, Pranit Bauva <pranit.bauva@gmail.com> wrote: >> >> @@ -431,6 +434,244 @@ static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) >> return 0; >> } >> >> +static int bisect_start(struct bisect_terms *terms, int no_checkout, >> + const char **argv, int argc) >> +{ >> + int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0; >> + int flags, pathspec_pos; >> + struct string_list revs = STRING_LIST_INIT_DUP; >> + struct string_list states = STRING_LIST_INIT_DUP; >> + struct strbuf start_head = STRBUF_INIT; >> + struct strbuf bisect_names = STRBUF_INIT; >> + struct strbuf orig_args = STRBUF_INIT; >> + const char *head; >> + unsigned char sha1[20]; >> + FILE *fp; >> + struct object_id oid; >> + >> + if (is_bare_repository()) >> + no_checkout = 1; >> + >> + for (i = 0; i < argc; i++) { >> + char *commit_id = xstrfmt("%s^{commit}", argv[i]); >> + if (!strcmp(argv[i], "--")) { >> + has_double_dash = 1; >> + break; >> + } > > In the shell code there is a loop dedicated to checking if there is a > double dash in the arguments before the real argument parsing loop. > There is a reason for that. > If you do it in the same loop, has_double_dash will not be set when > the arguments that are before the double dash will be parsed. I had tried that before. But somehow I couldn't get it to run so I reverted back. I have now successfully tried it and its working. You can checkout the PR[1]. >> + else if (!strcmp(argv[i], "--no-checkout")) >> + no_checkout = 1; >> + else if (!strcmp(argv[i], "--term-good") || >> + !strcmp(argv[i], "--term-old")) { >> + must_write_terms = 1; >> + strbuf_reset(&terms->term_good); >> + strbuf_addstr(&terms->term_good, argv[++i]); >> + } >> + else if (skip_prefix(argv[i], "--term-good=", &argv[i])) { > > (Maybe you could put the "else if (...) {" on the same line as the "}" above.) > >> + must_write_terms = 1; >> + strbuf_reset(&terms->term_good); >> + strbuf_addstr(&terms->term_good, argv[i]); >> + } >> + else if (skip_prefix(argv[i], "--term-old=", &argv[i])) { >> + must_write_terms = 1; >> + strbuf_reset(&terms->term_good); >> + strbuf_addstr(&terms->term_good, argv[i]); >> + } >> + else if (!strcmp(argv[i], "--term-bad") || >> + !strcmp(argv[i], "--term-new")) { >> + must_write_terms = 1; >> + strbuf_reset(&terms->term_bad); >> + strbuf_addstr(&terms->term_bad, argv[++i]); >> + } >> + else if (skip_prefix(argv[i], "--term-bad=", &argv[i])) { >> + must_write_terms = 1; >> + strbuf_reset(&terms->term_bad); >> + strbuf_addstr(&terms->term_bad, argv[i]); >> + } >> + else if (skip_prefix(argv[i], "--term-new=", &argv[i])) { >> + must_write_terms = 1; >> + strbuf_reset(&terms->term_good); >> + strbuf_addstr(&terms->term_good, argv[i]); >> + } >> + else if (starts_with(argv[i], "--") && >> + !one_of(argv[i], "--term-good", "--term-bad", NULL)) { >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + die(_("unrecognised option: '%s'"), argv[i]); >> + } >> + else if (get_oid(argv[i], &oid) && !has_double_dash) { > > And here checking "!has_double_dash" has no meaning if you check for a > double dash in the same loop, because there is a "break" after > has_double_dash is set above. I think this is the situation in the shell script too. >> + string_list_clear(&revs, 0); >> + string_list_clear(&states, 0); >> + free(commit_id); >> + die(_("'%s' does not appear to be a valid revision"), argv[i]); >> + } >> + else { >> + free(commit_id); >> + string_list_append(&revs, oid_to_hex(&oid)); >> + } >> + } >> + pathspec_pos = i; >> + Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v12 10/13] bisect--helper: `check_and_set_terms` shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (4 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 13/13] bisect--helper: `bisect_start` shell function partially in C Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` " Pranit Bauva ` (7 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement the `check_and_set_terms` shell function in C and add `check-and-set-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-and-set-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. check_and_set_terms() sets and receives two global variables namely TERM_GOOD and TERM_BAD in the shell script. Luckily the file BISECT_TERMS also contains the value of those variables so its appropriate to evoke the method get_terms() after calling the subcommand so that it retrieves the value of TERM_GOOD and TERM_BAD from the file BISECT_TERMS. The two global variables are passed as arguments to the subcommand. Also introduce bisect_terms_reset() to empty the contents of `term_good` and `term_bad` of `struct bisect_terms`. Also introduce set_terms() to copy the `term_good` and `term_bad` into `struct bisect_terms` and write it out to the file BISECT_TERMS. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 36 ++++----------------------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 9f70edb..5c4350f 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -23,6 +23,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-clean-state"), N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), + N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), NULL }; @@ -43,6 +44,12 @@ static void bisect_terms_release(struct bisect_terms *terms) strbuf_release(&terms->term_bad); } +static void bisect_terms_reset(struct bisect_terms *term) +{ + strbuf_reset(&term->term_good); + strbuf_reset(&term->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -253,6 +260,39 @@ static int bisect_write(const char *state, const char *rev, return 0; } +static int set_terms(struct bisect_terms *terms, const char *bad, + const char *good) +{ + bisect_terms_reset(terms); + strbuf_addstr(&terms->term_good, good); + strbuf_addstr(&terms->term_bad, bad); + return write_terms(terms->term_bad.buf, terms->term_good.buf); +} + +static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) +{ + int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms()); + + if (one_of(cmd, "skip", "start", "terms", NULL)) + return 0; + + if (has_term_file && + strcmp(cmd, terms->term_bad.buf) && + strcmp(cmd, terms->term_good.buf)) + return error(_("Invalid command: you're currently in a " + "%s/%s bisect"), terms->term_bad.buf, + terms->term_good.buf); + + if (!has_term_file) { + if (one_of(cmd, "bad", "good", NULL)) + return set_terms(terms, "bad", "good"); + if (one_of(cmd, "new", "old", NULL)) + return set_terms(terms, "new", "old"); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -261,7 +301,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_CLEAN_STATE, BISECT_RESET, CHECK_EXPECTED_REVS, - BISECT_WRITE + BISECT_WRITE, + CHECK_AND_SET_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -277,6 +318,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check for expected revs"), CHECK_EXPECTED_REVS), OPT_CMDMODE(0, "bisect-write", &cmdmode, N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), + OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, + N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -320,6 +363,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[3]); res = bisect_write(argv[0], argv[1], &terms, nolog); break; + case CHECK_AND_SET_TERMS: + if (argc != 3) + die(_("--check-and-set-terms requires 3 arguments")); + strbuf_addstr(&terms.term_good, argv[1]); + strbuf_addstr(&terms.term_bad, argv[2]); + res = check_and_set_terms(&terms, argv[0]); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index b9896a4..a41e69b 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -239,7 +239,8 @@ bisect_skip() { bisect_state() { bisect_autostart state=$1 - check_and_set_terms $state + git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit + get_terms case "$#,$state" in 0,*) die "$(gettext "Please call 'bisect_state' with at least one argument.")" ;; @@ -390,7 +391,8 @@ bisect_replay () { command="$bisect" fi get_terms - check_and_set_terms "$command" + git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit + get_terms case "$command" in start) cmd="bisect_start $rev" @@ -480,36 +482,6 @@ get_terms () { fi } -check_and_set_terms () { - cmd="$1" - case "$cmd" in - skip|start|terms) ;; - *) - if test -s "$GIT_DIR/BISECT_TERMS" && test "$cmd" != "$TERM_BAD" && test "$cmd" != "$TERM_GOOD" - then - die "$(eval_gettext "Invalid command: you're currently in a \$TERM_BAD/\$TERM_GOOD bisect.")" - fi - case "$cmd" in - bad|good) - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - 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 - TERM_BAD=new - TERM_GOOD=old - git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit - fi - ;; - esac ;; - esac -} - bisect_voc () { case "$1" in bad) echo "bad|new" ;; -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (5 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 10/13] bisect--helper: `check_and_set_terms` shell function " Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 05/13] t6030: explicitly test for bisection cleanup Pranit Bauva ` (6 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement `is_expected_rev` & `check_expected_revs` shell function in C and add a `--check-expected-revs` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--check-expected-revs` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired but its implementation will be called by some other method. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 33 ++++++++++++++++++++++++++++++++- git-bisect.sh | 20 ++------------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 409e268..c03042f 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -163,13 +163,40 @@ static int bisect_reset(const char *commit) return bisect_clean_state(); } +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) >= 0) { + strbuf_trim(&actual_hex); + res = !strcmp(actual_hex.buf, expected_hex); + } + strbuf_release(&actual_hex); + return res; +} + +static int 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()); + return 0; + } + } + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, BISECT_CLEAN_STATE, - BISECT_RESET + BISECT_RESET, + CHECK_EXPECTED_REVS } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -181,6 +208,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("cleanup the bisection state"), BISECT_CLEAN_STATE), OPT_CMDMODE(0, "bisect-reset", &cmdmode, N_("reset the bisection state"), BISECT_RESET), + 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() @@ -207,6 +236,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); return bisect_reset(argc ? argv[0] : NULL); + case CHECK_EXPECTED_REVS: + return check_expected_revs(argv, argc); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 18580b7..4f6545e 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -238,22 +238,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 $(bisect_head)) || die "$(gettext "Bad rev input: $(bisect_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.")" ;; *) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 05/13] t6030: explicitly test for bisection cleanup 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (6 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` " Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 09/13] bisect--helper: `bisect_write` shell function in C Pranit Bauva ` (5 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Add test to explicitly check that 'git bisect reset' is working as expected. This is already covered implicitly by the test suite. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- I faced this problem while converting `bisect_clean_state` and the tests where showing breakages but it wasn't clear as to where exactly are they breaking. This will patch will help in that. Also I tested the test coverage of the test suite before this patch and it covers this (I did this by purposely changing names of files in git-bisect.sh and running the test suite). --- t/t6030-bisect-porcelain.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index e74662b..a17f7a6 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -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 -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 09/13] bisect--helper: `bisect_write` shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (7 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 05/13] t6030: explicitly test for bisection cleanup Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 07/13] bisect--helper: `bisect_reset` " Pranit Bauva ` (4 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement the `bisect_write` shell function in C and add a `bisect-write` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--bisect-write` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Note: bisect_write() uses two variables namely TERM_GOOD and TERM_BAD from the global shell script thus we need to pass it to the subcommand using the arguments. We then store them in a struct bisect_terms and pass the memory address around functions. This patch also introduces new methods namely bisect_state_init() and bisect_terms_release() for easy memory management for the struct bisect_terms. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 97 ++++++++++++++++++++++++++++++++++++++++++++---- git-bisect.sh | 25 ++----------- 2 files changed, 94 insertions(+), 28 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index c03042f..9f70edb 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -22,9 +22,27 @@ static const char * const git_bisect_helper_usage[] = { 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 <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), NULL }; +struct bisect_terms { + struct strbuf term_good; + struct strbuf term_bad; +}; + +static void bisect_terms_init(struct bisect_terms *terms) +{ + strbuf_init(&terms->term_good, 0); + strbuf_init(&terms->term_bad, 0); +} + +static void bisect_terms_release(struct bisect_terms *terms) +{ + strbuf_release(&terms->term_good); + strbuf_release(&terms->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -189,6 +207,52 @@ static int check_expected_revs(const char **revs, int rev_nr) return 0; } +static int bisect_write(const char *state, const char *rev, + const struct bisect_terms *terms, int nolog) +{ + struct strbuf tag = STRBUF_INIT; + struct strbuf commit_name = STRBUF_INIT; + struct object_id oid; + struct commit *commit; + struct pretty_print_context pp = {0}; + FILE *fp; + + if (!strcmp(state, terms->term_bad.buf)) + strbuf_addf(&tag, "refs/bisect/%s", state); + else if (one_of(state, terms->term_good.buf, "skip", NULL)) + strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev); + else + return error(_("Bad bisect_write argument: %s"), state); + + if (get_oid(rev, &oid)) { + strbuf_release(&tag); + return error(_("couldn't get the oid of the rev '%s'"), rev); + } + + if (update_ref(NULL, tag.buf, oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + strbuf_release(&tag); + return -1; + } + strbuf_release(&tag); + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) + return error_errno(_("couldn't open the file '%s'"), git_path_bisect_log()); + + commit = lookup_commit_reference(oid.hash); + format_commit_message(commit, "%s", &commit_name, &pp); + fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash), + commit_name.buf); + strbuf_release(&commit_name); + + if (!nolog) + fprintf(fp, "git bisect %s %s\n", state, rev); + + fclose(fp); + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -196,9 +260,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) WRITE_TERMS, BISECT_CLEAN_STATE, BISECT_RESET, - CHECK_EXPECTED_REVS + CHECK_EXPECTED_REVS, + BISECT_WRITE } cmdmode = 0; - int no_checkout = 0; + int no_checkout = 0, res = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), @@ -210,10 +275,14 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("reset the bisection state"), BISECT_RESET), OPT_CMDMODE(0, "check-expected-revs", &cmdmode, N_("check for expected revs"), CHECK_EXPECTED_REVS), + OPT_CMDMODE(0, "bisect-write", &cmdmode, + N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() }; + struct bisect_terms terms; + bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, git_bisect_helper_usage, 0); @@ -222,24 +291,38 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) usage_with_options(git_bisect_helper_usage, options); switch (cmdmode) { + int nolog; case NEXT_ALL: return bisect_next_all(prefix, no_checkout); case WRITE_TERMS: if (argc != 2) die(_("--write-terms requires two arguments")); - return write_terms(argv[0], argv[1]); + res = write_terms(argv[0], argv[1]); + break; case BISECT_CLEAN_STATE: if (argc != 0) die(_("--bisect-clean-state requires no arguments")); - return bisect_clean_state(); + res = bisect_clean_state(); + break; case BISECT_RESET: if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); - return bisect_reset(argc ? argv[0] : NULL); + res = bisect_reset(argc ? argv[0] : NULL); + break; case CHECK_EXPECTED_REVS: - return check_expected_revs(argv, argc); + res = check_expected_revs(argv, argc); + break; + case BISECT_WRITE: + if (argc != 4 && argc != 5) + die(_("--bisect-write requires either 4 or 5 arguments")); + nolog = (argc == 5) && !strcmp(argv[4], "nolog"); + strbuf_addstr(&terms.term_good, argv[2]); + strbuf_addstr(&terms.term_bad, argv[3]); + res = bisect_write(argv[0], argv[1], &terms, nolog); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } - return 0; + bisect_terms_release(&terms); + return res; } diff --git a/git-bisect.sh b/git-bisect.sh index 4f6545e..b9896a4 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -145,7 +145,7 @@ bisect_start() { 0) state=$TERM_BAD ; bad_seen=1 ;; *) state=$TERM_GOOD ;; esac - eval="$eval bisect_write '$state' '$rev' 'nolog' &&" + eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" done # # Verify HEAD. @@ -221,23 +221,6 @@ bisect_start() { trap '-' 0 } -bisect_write() { - state="$1" - rev="$2" - nolog="$3" - case "$state" in - "$TERM_BAD") - tag="$state" ;; - "$TERM_GOOD"|skip) - tag="$state"-"$rev" ;; - *) - die "$(eval_gettext "Bad bisect_write argument: \$state")" ;; - esac - git update-ref "refs/bisect/$tag" "$rev" || exit - echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG" - test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" -} - bisect_skip() { all='' for arg in "$@" @@ -263,7 +246,7 @@ bisect_state() { 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip) rev=$(git rev-parse --verify $(bisect_head)) || die "$(gettext "Bad rev input: $(bisect_head)")" - bisect_write "$state" "$rev" + 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 @@ -276,7 +259,7 @@ bisect_state() { done for rev in $hash_list do - bisect_write "$state" "$rev" + git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit done git bisect--helper --check-expected-revs $hash_list ;; *,"$TERM_BAD") @@ -413,7 +396,7 @@ bisect_replay () { cmd="bisect_start $rev" eval "$cmd" ;; "$TERM_GOOD"|"$TERM_BAD"|skip) - bisect_write "$command" "$rev" ;; + git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) bisect_terms $rev ;; *) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 07/13] bisect--helper: `bisect_reset` shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (8 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 09/13] bisect--helper: `bisect_write` shell function in C Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 21:57 ` [PATCH v12 11/13] bisect--helper: `bisect_next_check` & bisect_voc " Pranit Bauva ` (3 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement `bisect_reset` shell function in C and add a `--bisect-reset` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `bisect_reset` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired but its implementation will be called by some other method. Note: --bisect-clean-state subcommand has not been retired as there are still a function namely `bisect_start()` which still uses this subcommand. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 28 ++-------------------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3fffa78..409e268 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,6 +4,8 @@ #include "bisect.h" #include "refs.h" #include "dir.h" +#include "argv-array.h" +#include "run-command.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -13,11 +15,13 @@ static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") static GIT_PATH_FUNC(git_path_head_name, "head-name") static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") +static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD") 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"), + N_("git bisect--helper --bisect-reset [<commit>]"), NULL }; @@ -125,12 +129,47 @@ static int bisect_clean_state(void) return result; } +static int bisect_reset(const char *commit) +{ + struct strbuf branch = STRBUF_INIT; + + if (!commit) { + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { + printf("We are not bisecting.\n"); + return 0; + } + strbuf_rtrim(&branch); + } else { + struct object_id oid; + if (get_oid(commit, &oid)) + return error(_("'%s' is not a valid commit"), commit); + strbuf_addstr(&branch, commit); + } + + if (!file_exists(git_path_bisect_head())) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("Could not check out original HEAD '%s'. Try " + "'git bisect reset <commit>'."), branch.buf); + strbuf_release(&branch); + argv_array_clear(&argv); + return -1; + } + argv_array_clear(&argv); + } + + strbuf_release(&branch); + return bisect_clean_state(); +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, - BISECT_CLEAN_STATE + BISECT_CLEAN_STATE, + BISECT_RESET } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -140,6 +179,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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, "bisect-reset", &cmdmode, + N_("reset the bisection state"), BISECT_RESET), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -162,6 +203,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 0) die(_("--bisect-clean-state requires no arguments")); return bisect_clean_state(); + case BISECT_RESET: + if (argc > 1) + die(_("--bisect-reset requires either zero or one arguments")); + return bisect_reset(argc ? argv[0] : NULL); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index bbc57d2..18580b7 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -409,35 +409,11 @@ bisect_visualize() { eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES") } -bisect_reset() { - test -s "$GIT_DIR/BISECT_START" || { - gettextln "We are not bisecting." - return - } - case "$#" in - 0) branch=$(cat "$GIT_DIR/BISECT_START") ;; - 1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || { - invalid="$1" - die "$(eval_gettext "'\$invalid' is not a valid commit")" - } - branch="$1" ;; - *) - usage ;; - esac - - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- - then - die "$(eval_gettext "Could not check out original HEAD '\$branch'. -Try 'git bisect reset <commit>'.")" - fi - git bisect--helper --bisect-clean-state || exit -} - bisect_replay () { file="$1" test "$#" -eq 1 || die "$(gettext "No logfile given")" test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")" - bisect_reset + git bisect--helper --bisect-reset || exit while read git bisect command rev do test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue @@ -627,7 +603,7 @@ case "$#" in visualize|view) bisect_visualize "$@" ;; reset) - bisect_reset "$@" ;; + git bisect--helper --bisect-reset "$@" ;; replay) bisect_replay "$@" ;; log) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v12 11/13] bisect--helper: `bisect_next_check` & bisect_voc shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (9 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 07/13] bisect--helper: `bisect_reset` " Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-12 18:11 ` Junio C Hamano 2016-08-10 21:57 ` [PATCH v12 02/13] bisect: rewrite `check_term_format` " Pranit Bauva ` (2 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement `bisect_next_check` shell function in C and add `bisect-next-check` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also reimplement `bisect_voc` shell function in C and call it from `bisect_next_check` implementation in C. Using `--bisect-next-check` is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 60 ++------------------------- 2 files changed, 106 insertions(+), 57 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 5c4350f..b6e9973 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -6,6 +6,7 @@ #include "dir.h" #include "argv-array.h" #include "run-command.h" +#include "prompt.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -24,6 +25,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), + N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), NULL }; @@ -293,6 +295,95 @@ static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) return 0; } +static int mark_good(const char *refname, const struct object_id *oid, + int flag, void *cb_data) +{ + int *m_good = (int *)cb_data; + *m_good = 0; + return 1; +} + +static char *bisect_voc(char *revision_type) +{ + if (!strcmp(revision_type, "bad")) + return "bad|new"; + if (!strcmp(revision_type, "good")) + return "good|old"; + + return NULL; +} + +static int bisect_next_check(const struct bisect_terms *terms, + const char *current_term) +{ + int missing_good = 1, missing_bad = 1; + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); + char *good_glob = xstrfmt("%s-*", terms->term_good.buf); + char *bad_syn, *good_syn; + + if (ref_exists(bad_ref)) + missing_bad = 0; + free(bad_ref); + + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", + (void *) &missing_good); + free(good_glob); + + if (!missing_good && !missing_bad) + return 0; + + if (!current_term) + return -1; + + if (missing_good && !missing_bad && current_term && + !strcmp(current_term, terms->term_good.buf)) { + char *yesno; + /* + * have bad (or new) but not good (or old). We could bisect + * although this is less optimum. + */ + fprintf(stderr, N_("Warning: bisecting only with a %s commit\n"), + terms->term_bad.buf); + if (!isatty(0)) + return 0; + /* + * TRANSLATORS: Make sure to include [Y] and [n] in your + * translation. The program will only accept English input + * at this point. + */ + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); + if (starts_with(yesno, "N") || starts_with(yesno, "n")) + return -1; + + return 0; + } + bad_syn = xstrdup(bisect_voc("bad")); + good_syn = xstrdup(bisect_voc("good")); + if (!is_empty_or_missing_file(git_path_bisect_start())) { + error(_("You need to give me at least one %s and " + "%s revision. You can use \"git bisect %s\" " + "and \"git bisect %s\" for that. \n"), + bad_syn, good_syn, bad_syn, good_syn); + free(bad_syn); + free(good_syn); + return -1; + } + else { + error(_("You need to start by \"git bisect start\". You " + "then need to give me at least one %s and %s " + "revision. You can use \"git bisect %s\" and " + "\"git bisect %s\" for that.\n"), + good_syn, bad_syn, bad_syn, good_syn); + free(bad_syn); + free(good_syn); + return -1; + } + free(bad_syn); + free(good_syn); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -302,7 +393,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_RESET, CHECK_EXPECTED_REVS, BISECT_WRITE, - CHECK_AND_SET_TERMS + CHECK_AND_SET_TERMS, + BISECT_NEXT_CHECK } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -320,6 +412,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), + OPT_CMDMODE(0, "bisect-next-check", &cmdmode, + N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -370,6 +464,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[2]); res = check_and_set_terms(&terms, argv[0]); break; + case BISECT_NEXT_CHECK: + if (argc != 2 && argc != 3) + die(_("--bisect-next-check requires 2 or 3 arguments")); + strbuf_addstr(&terms.term_good, argv[0]); + strbuf_addstr(&terms.term_bad, argv[1]); + res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index a41e69b..c2d6319 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -271,59 +271,14 @@ bisect_state() { bisect_auto_next } -bisect_next_check() { - missing_good= missing_bad= - git show-ref -q --verify refs/bisect/$TERM_BAD || missing_bad=t - test -n "$(git for-each-ref "refs/bisect/$TERM_GOOD-*")" || missing_good=t - - case "$missing_good,$missing_bad,$1" in - ,,*) - : have both $TERM_GOOD and $TERM_BAD - ok - ;; - *,) - # do not have both but not asked to fail - just report. - false - ;; - t,,"$TERM_GOOD") - # have bad (or new) but not good (or old). we could bisect although - # this is less optimum. - eval_gettextln "Warning: bisecting only with a \$TERM_BAD commit." >&2 - if test -t 0 - then - # TRANSLATORS: Make sure to include [Y] and [n] in your - # translation. The program will only accept English input - # at this point. - gettext "Are you sure [Y/n]? " >&2 - read yesno - case "$yesno" in [Nn]*) exit 1 ;; esac - fi - : bisect without $TERM_GOOD... - ;; - *) - bad_syn=$(bisect_voc bad) - good_syn=$(bisect_voc good) - if test -s "$GIT_DIR/BISECT_START" - then - - eval_gettextln "You need to give me at least one \$bad_syn and one \$good_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - else - eval_gettextln "You need to start by \"git bisect start\". -You then need to give me at least one \$good_syn and one \$bad_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - fi - exit 1 ;; - esac -} - bisect_auto_next() { - bisect_next_check && bisect_next || : + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || : } bisect_next() { case "$#" in 0) ;; *) usage ;; esac bisect_autostart - bisect_next_check $TERM_GOOD + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit # Perform all bisection computation, display and checkout git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout) @@ -355,7 +310,7 @@ bisect_next() { } bisect_visualize() { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit if test $# = 0 then @@ -409,7 +364,7 @@ bisect_replay () { } bisect_run () { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit while true do @@ -482,13 +437,6 @@ get_terms () { fi } -bisect_voc () { - case "$1" in - bad) echo "bad|new" ;; - good) echo "good|old" ;; - esac -} - bisect_terms () { get_terms if ! test -s "$GIT_DIR/BISECT_TERMS" -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v12 11/13] bisect--helper: `bisect_next_check` & bisect_voc shell function in C 2016-08-10 21:57 ` [PATCH v12 11/13] bisect--helper: `bisect_next_check` & bisect_voc " Pranit Bauva @ 2016-08-12 18:11 ` Junio C Hamano 2016-08-12 18:49 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-12 18:11 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static char *bisect_voc(char *revision_type) > +{ > + if (!strcmp(revision_type, "bad")) > + return "bad|new"; > + if (!strcmp(revision_type, "good")) > + return "good|old"; > + > + return NULL; > +} I think you can return "const char *" from the above function. Then you do not have to do xstrdup() on the return values to store in bad_syn and good_syn, and you do not have to free(3) them. > +static int bisect_next_check(const struct bisect_terms *terms, > + const char *current_term) > +{ > + .... > + fprintf(stderr, N_("Warning: bisecting only with a %s commit\n"), > + terms->term_bad.buf); Hmph, is this N_() and not _()? > + .... > + } > + bad_syn = xstrdup(bisect_voc("bad")); > + good_syn = xstrdup(bisect_voc("good")); > + .... > + free(bad_syn); > + free(good_syn); ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v12 11/13] bisect--helper: `bisect_next_check` & bisect_voc shell function in C 2016-08-12 18:11 ` Junio C Hamano @ 2016-08-12 18:49 ` Junio C Hamano 2016-08-13 6:32 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-12 18:49 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Junio C Hamano <gitster@pobox.com> writes: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +static int bisect_next_check(const struct bisect_terms *terms, >> + const char *current_term) >> +{ >> + .... >> + fprintf(stderr, N_("Warning: bisecting only with a %s commit\n"), >> + terms->term_bad.buf); > > Hmph, is this N_() and not _()? I recall you saying that you are not familiar with i18n. As it is a good skill to have outside the context of this project, let's do a quick primer. GSoC is a learning experience ;-). There is a runtime function "gettext()" that takes a string, which is typically a printf format string, and gives another string. You feed your message in the original language, meant to be used in the C locale, and the function gives it translated into the end-user's language, specified by environment variables like $LC_MESSAGES. Since it is too cumbersome to write gettext() all the time, _() exists as a short-hand for it. So running this printf(_("Hello, world\n")); would look up "Hello world\n" in database for the end-user's language, and shows the translated message instead. By scanning the source text, you can extract these constant strings passed to gettext() or _(). This is done by the i18n coordinator with the "msgmerge" program. By doing so, we learn "Hello, world\n" must be translated, and then ask i18n team to translate it to their language. The result is what we have in the l10n database. They are in po/*.po files in our source tree. Sometimes, the message you want to be translated may be in a variable, e.g. void greeting(const char *message) { printf(_(message)); } int main(int ac, char **av) { int i; const char *message[] = { "Hello, world\n", "Goodbye, everybody\n", }; for (i = 0; i < ARRAY_SIZE(message); i++) greeting(message[i]); } And scanning the source would not find "Hello, world\n" or "Goodby, everybody\n" must be translated. We do not want to do this: ... const char *message[] = { *BAD* _("Hello, world\n"), *BAD* _("Goodbye, everybody\n"), }; ... because we do *NOT* want to call gettext() there (we call the function at runtime inside greeting() instead). We use N_() to mark such messages, like so: ... const char *message[] = { *GOOD* N_("Hello, world\n"), *GOOD* N_("Goodbye, everybody\n"), }; ... The N_() macro is a no-op at runtime. It just is there so that "msgmerge" can notice that the constant string there is something that needs to be thrown into the l10n database. As a concrete example: > @@ -24,6 +25,7 @@ static const char * const git_bisect_helper_usage[] = { > N_("git bisect--helper --bisect-reset [<commit>]"), > N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), > N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), > + N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), > NULL > }; this is such a use of N_(). You want to keep untranslated message in the git_bisect_helper_usage[] array, to be given to gettext(), or more likely its short-hand _(), when these usage strings are used, and make sure these strings will be in the l10n database so that translators will give you translations to them to be used at runtime. > + /* > + * have bad (or new) but not good (or old). We could bisect > + * although this is less optimum. > + */ > + fprintf(stderr, N_("Warning: bisecting only with a %s commit\n"), > + terms->term_bad.buf); This one wants to call gettext() function to give fprintf() the translated warning message. It should be _(). > + /* > + * TRANSLATORS: Make sure to include [Y] and [n] in your > + * translation. The program will only accept English input > + * at this point. > + */ > + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); Just like this one. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v12 11/13] bisect--helper: `bisect_next_check` & bisect_voc shell function in C 2016-08-12 18:49 ` Junio C Hamano @ 2016-08-13 6:32 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-13 6:32 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Sat, Aug 13, 2016 at 12:19 AM, Junio C Hamano <gitster@pobox.com> wrote: > Junio C Hamano <gitster@pobox.com> writes: > >> Pranit Bauva <pranit.bauva@gmail.com> writes: >> >>> +static int bisect_next_check(const struct bisect_terms *terms, >>> + const char *current_term) >>> +{ >>> + .... >>> + fprintf(stderr, N_("Warning: bisecting only with a %s commit\n"), >>> + terms->term_bad.buf); >> >> Hmph, is this N_() and not _()? > > I recall you saying that you are not familiar with i18n. As it is a > good skill to have outside the context of this project, let's do a > quick primer. GSoC is a learning experience ;-). True! :) > There is a runtime function "gettext()" that takes a string, which > is typically a printf format string, and gives another string. You > feed your message in the original language, meant to be used in the > C locale, and the function gives it translated into the end-user's > language, specified by environment variables like $LC_MESSAGES. > > Since it is too cumbersome to write gettext() all the time, _() > exists as a short-hand for it. So running this > > printf(_("Hello, world\n")); > > would look up "Hello world\n" in database for the end-user's > language, and shows the translated message instead. > > By scanning the source text, you can extract these constant strings > passed to gettext() or _(). This is done by the i18n coordinator > with the "msgmerge" program. By doing so, we learn "Hello, world\n" > must be translated, and then ask i18n team to translate it to their > language. The result is what we have in the l10n database. They > are in po/*.po files in our source tree. > > Sometimes, the message you want to be translated may be in a > variable, e.g. > > void greeting(const char *message) > { > printf(_(message)); > } > > int main(int ac, char **av) > { > int i; > const char *message[] = { > "Hello, world\n", > "Goodbye, everybody\n", > }; > for (i = 0; i < ARRAY_SIZE(message); i++) > greeting(message[i]); > } > > And scanning the source would not find "Hello, world\n" or "Goodby, > everybody\n" must be translated. We do not want to do this: > > ... > const char *message[] = { > *BAD* _("Hello, world\n"), > *BAD* _("Goodbye, everybody\n"), > }; > ... > > because we do *NOT* want to call gettext() there (we call the > function at runtime inside greeting() instead). We use N_() > to mark such messages, like so: > > ... > const char *message[] = { > *GOOD* N_("Hello, world\n"), > *GOOD* N_("Goodbye, everybody\n"), > }; > ... > > The N_() macro is a no-op at runtime. It just is there so that > "msgmerge" can notice that the constant string there is something > that needs to be thrown into the l10n database. > > As a concrete example: > >> @@ -24,6 +25,7 @@ static const char * const git_bisect_helper_usage[] = { >> N_("git bisect--helper --bisect-reset [<commit>]"), >> N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), >> N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), >> + N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), >> NULL >> }; > > this is such a use of N_(). You want to keep untranslated message > in the git_bisect_helper_usage[] array, to be given to gettext(), or > more likely its short-hand _(), when these usage strings are used, > and make sure these strings will be in the l10n database so that > translators will give you translations to them to be used at > runtime. > >> + /* >> + * have bad (or new) but not good (or old). We could bisect >> + * although this is less optimum. >> + */ >> + fprintf(stderr, N_("Warning: bisecting only with a %s commit\n"), >> + terms->term_bad.buf); > > This one wants to call gettext() function to give fprintf() the > translated warning message. It should be _(). > >> + /* >> + * TRANSLATORS: Make sure to include [Y] and [n] in your >> + * translation. The program will only accept English input >> + * at this point. >> + */ >> + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); > > Just like this one. Thanks a lot for taking time to explain this. I had searched a bit but couldn't find the difference between _() and N_()! Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v12 02/13] bisect: rewrite `check_term_format` shell function in C 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (10 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 11/13] bisect--helper: `bisect_next_check` & bisect_voc " Pranit Bauva @ 2016-08-10 21:57 ` Pranit Bauva 2016-08-10 22:19 ` [PATCH v12 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 21:57 UTC (permalink / raw) To: git Reimplement the `check_term_format` shell function in C and add a `--check-term-format` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-term-format` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and its implementation will be called by some other method/subcommand. For eg. In conversion of write_terms() of git-bisect.sh, the subcommand will be removed and instead check_term_format() will be called in its C implementation while a new subcommand will be introduced for write_terms(). Helped-by: Johannes Schindelein <Johannes.Schindelein@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 31 ++----------------------- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 8111c91..a47f1f2 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -2,19 +2,73 @@ #include "cache.h" #include "parse-options.h" #include "bisect.h" +#include "refs.h" static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), + N_("git bisect--helper --check-term-format <term> <orig_term>"), 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", 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - enum { NEXT_ALL = 1 } cmdmode = 0; + enum { + NEXT_ALL = 1, + CHECK_TERM_FMT + } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), + OPT_CMDMODE(0, "check-term-format", &cmdmode, + N_("check format of the term"), CHECK_TERM_FMT), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -29,6 +83,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); + case CHECK_TERM_FMT: + if (argc != 2) + die(_("--check-term-format requires two arguments")); + return check_term_format(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 5d1cb00..7d7965d 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -564,38 +564,11 @@ write_terms () { then die "$(gettext "please use two different terms")" fi - check_term_format "$TERM_BAD" bad - check_term_format "$TERM_GOOD" good + git bisect--helper --check-term-format "$TERM_BAD" bad || exit + git bisect--helper --check-term-format "$TERM_GOOD" good || exit 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 -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v12 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (11 preceding siblings ...) 2016-08-10 21:57 ` [PATCH v12 02/13] bisect: rewrite `check_term_format` " Pranit Bauva @ 2016-08-10 22:19 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-10 22:19 UTC (permalink / raw) To: Git List; +Cc: Junio C Hamano, Christian Couder, Lars Schneider Here[1] is the link for interdiff. Sorry could not send a cover patch or put it in here. I am under a proxy which blocks IMAP/SMTP connections and gmail wraps the lines. [1]: http://paste.ubuntu.com/22794990/ Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v13 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-08-10 21:57 ` [PATCH v12 " Pranit Bauva ` (12 preceding siblings ...) 2016-08-10 22:19 ` [PATCH v12 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 09/13] bisect--helper: `bisect_write` shell function in C Pranit Bauva ` (13 more replies) 13 siblings, 14 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git `--next-all` is meant to be used as a subcommand to support multiple "operation mode" though the current implementation does not contain any other subcommand along side with `--next-all` but further commits will include some more subcommands. Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3324229..8111c91 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -10,11 +10,11 @@ static const char * const git_bisect_helper_usage[] = { int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - int next_all = 0; + enum { NEXT_ALL = 1 } 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -23,9 +23,14 @@ 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); + default: + die("BUG: unknown subcommand '%d'", cmdmode); + } + return 0; } -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 09/13] bisect--helper: `bisect_write` shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 04/13] bisect--helper: `bisect_clean_state` " Pranit Bauva ` (12 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement the `bisect_write` shell function in C and add a `bisect-write` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--bisect-write` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Note: bisect_write() uses two variables namely TERM_GOOD and TERM_BAD from the global shell script thus we need to pass it to the subcommand using the arguments. We then store them in a struct bisect_terms and pass the memory address around functions. This patch also introduces new methods namely bisect_state_init() and bisect_terms_release() for easy memory management for the struct bisect_terms. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 97 ++++++++++++++++++++++++++++++++++++++++++++---- git-bisect.sh | 25 ++----------- 2 files changed, 94 insertions(+), 28 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 711be75..5364960 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -19,9 +19,27 @@ static const char * const git_bisect_helper_usage[] = { 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 <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), NULL }; +struct bisect_terms { + struct strbuf term_good; + struct strbuf term_bad; +}; + +static void bisect_terms_init(struct bisect_terms *terms) +{ + strbuf_init(&terms->term_good, 0); + strbuf_init(&terms->term_bad, 0); +} + +static void bisect_terms_release(struct bisect_terms *terms) +{ + strbuf_release(&terms->term_good); + strbuf_release(&terms->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -149,6 +167,52 @@ static int check_expected_revs(const char **revs, int rev_nr) return 0; } +static int bisect_write(const char *state, const char *rev, + const struct bisect_terms *terms, int nolog) +{ + struct strbuf tag = STRBUF_INIT; + struct strbuf commit_name = STRBUF_INIT; + struct object_id oid; + struct commit *commit; + struct pretty_print_context pp = {0}; + FILE *fp; + + if (!strcmp(state, terms->term_bad.buf)) + strbuf_addf(&tag, "refs/bisect/%s", state); + else if (one_of(state, terms->term_good.buf, "skip", NULL)) + strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev); + else + return error(_("Bad bisect_write argument: %s"), state); + + if (get_oid(rev, &oid)) { + strbuf_release(&tag); + return error(_("couldn't get the oid of the rev '%s'"), rev); + } + + if (update_ref(NULL, tag.buf, oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + strbuf_release(&tag); + return -1; + } + strbuf_release(&tag); + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) + return error_errno(_("couldn't open the file '%s'"), git_path_bisect_log()); + + commit = lookup_commit_reference(oid.hash); + format_commit_message(commit, "%s", &commit_name, &pp); + fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash), + commit_name.buf); + strbuf_release(&commit_name); + + if (!nolog) + fprintf(fp, "git bisect %s %s\n", state, rev); + + fclose(fp); + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -156,9 +220,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) WRITE_TERMS, BISECT_CLEAN_STATE, BISECT_RESET, - CHECK_EXPECTED_REVS + CHECK_EXPECTED_REVS, + BISECT_WRITE } cmdmode = 0; - int no_checkout = 0; + int no_checkout = 0, res = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), @@ -170,10 +235,14 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("reset the bisection state"), BISECT_RESET), OPT_CMDMODE(0, "check-expected-revs", &cmdmode, N_("check for expected revs"), CHECK_EXPECTED_REVS), + OPT_CMDMODE(0, "bisect-write", &cmdmode, + N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() }; + struct bisect_terms terms; + bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, git_bisect_helper_usage, 0); @@ -182,24 +251,38 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) usage_with_options(git_bisect_helper_usage, options); switch (cmdmode) { + int nolog; case NEXT_ALL: return bisect_next_all(prefix, no_checkout); case WRITE_TERMS: if (argc != 2) die(_("--write-terms requires two arguments")); - return write_terms(argv[0], argv[1]); + res = write_terms(argv[0], argv[1]); + break; case BISECT_CLEAN_STATE: if (argc != 0) die(_("--bisect-clean-state requires no arguments")); - return bisect_clean_state(); + res = bisect_clean_state(); + break; case BISECT_RESET: if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); - return bisect_reset(argc ? argv[0] : NULL); + res = bisect_reset(argc ? argv[0] : NULL); + break; case CHECK_EXPECTED_REVS: - return check_expected_revs(argv, argc); + res = check_expected_revs(argv, argc); + break; + case BISECT_WRITE: + if (argc != 4 && argc != 5) + die(_("--bisect-write requires either 4 or 5 arguments")); + nolog = (argc == 5) && !strcmp(argv[4], "nolog"); + strbuf_addstr(&terms.term_good, argv[2]); + strbuf_addstr(&terms.term_bad, argv[3]); + res = bisect_write(argv[0], argv[1], &terms, nolog); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } - return 0; + bisect_terms_release(&terms); + return res; } diff --git a/git-bisect.sh b/git-bisect.sh index c3e43248..dfdec33 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -144,7 +144,7 @@ bisect_start() { 0) state=$TERM_BAD ; bad_seen=1 ;; *) state=$TERM_GOOD ;; esac - eval="$eval bisect_write '$state' '$rev' 'nolog' &&" + eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" done # # Verify HEAD. @@ -220,23 +220,6 @@ bisect_start() { trap '-' 0 } -bisect_write() { - state="$1" - rev="$2" - nolog="$3" - case "$state" in - "$TERM_BAD") - tag="$state" ;; - "$TERM_GOOD"|skip) - tag="$state"-"$rev" ;; - *) - die "$(eval_gettext "Bad bisect_write argument: \$state")" ;; - esac - git update-ref "refs/bisect/$tag" "$rev" || exit - echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG" - test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" -} - bisect_skip() { all='' for arg in "$@" @@ -263,7 +246,7 @@ bisect_state() { bisected_head=$(bisect_head) rev=$(git rev-parse --verify "$bisected_head") || die "$(eval_gettext "Bad rev input: \$bisected_head")" - bisect_write "$state" "$rev" + 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 @@ -276,7 +259,7 @@ bisect_state() { done for rev in $hash_list do - bisect_write "$state" "$rev" + git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit done git bisect--helper --check-expected-revs $hash_list ;; *,"$TERM_BAD") @@ -413,7 +396,7 @@ bisect_replay () { cmd="bisect_start $rev" eval "$cmd" ;; "$TERM_GOOD"|"$TERM_BAD"|skip) - bisect_write "$command" "$rev" ;; + git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) bisect_terms $rev ;; *) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 09/13] bisect--helper: `bisect_write` shell function in C Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-21 11:18 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 10/13] bisect--helper: `check_and_set_terms` " Pranit Bauva ` (11 subsequent siblings) 13 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement `bisect_clean_state` shell function in C and add a `bisect-clean-state` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-clean-state` subcommand is a measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by bisect_reset() and bisect_start(). Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- bisect.c | 43 +++++++++++++++++++++++++++++++++++++++++++ bisect.h | 2 ++ builtin/bisect--helper.c | 14 +++++++++++++- git-bisect.sh | 26 +++----------------------- 4 files changed, 61 insertions(+), 24 deletions(-) diff --git a/bisect.c b/bisect.c index 6f512c2..45d598d 100644 --- a/bisect.c +++ b/bisect.c @@ -430,6 +430,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) { @@ -1040,3 +1046,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(&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; +} diff --git a/bisect.h b/bisect.h index acd12ef..0ae63d4 100644 --- a/bisect.h +++ b/bisect.h @@ -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 diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 30e1031..e50934c 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -5,10 +5,15 @@ #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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") +static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") 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 }; @@ -83,7 +88,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - WRITE_TERMS + WRITE_TERMS, + BISECT_CLEAN_STATE } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -91,6 +97,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -109,6 +117,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 2) die(_("--write-terms requires two arguments")); return write_terms(argv[0], argv[1]); + case BISECT_CLEAN_STATE: + if (argc != 0) + die(_("--bisect-clean-state requires no arguments")); + return bisect_clean_state(); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 9ef6cb8..f1202df 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -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 # @@ -430,27 +430,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 () { -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v13 04/13] bisect--helper: `bisect_clean_state` shell function in C 2016-08-19 20:32 ` [PATCH v13 04/13] bisect--helper: `bisect_clean_state` " Pranit Bauva @ 2016-08-21 11:18 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-21 11:18 UTC (permalink / raw) To: Git List Hey, On Sat, Aug 20, 2016 at 2:02 AM, Pranit Bauva <pranit.bauva@gmail.com> wrote: > Reimplement `bisect_clean_state` shell function in C and add a > `bisect-clean-state` subcommand to `git bisect--helper` to call it from > git-bisect.sh . > > Using `--bisect-clean-state` subcommand is a measure to port shell > function to C so as to use the existing test suite. As more functions > are ported, this subcommand will be retired but its implementation will > be called by bisect_reset() and bisect_start(). > > Mentored-by: Lars Schneider <larsxschneider@gmail.com> > Mentored-by: Christian Couder <chriscool@tuxfamily.org> > Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> > --- > bisect.c | 43 +++++++++++++++++++++++++++++++++++++++++++ > bisect.h | 2 ++ > builtin/bisect--helper.c | 14 +++++++++++++- > git-bisect.sh | 26 +++----------------------- > 4 files changed, 61 insertions(+), 24 deletions(-) > > diff --git a/bisect.c b/bisect.c > index 6f512c2..45d598d 100644 > --- a/bisect.c > +++ b/bisect.c > @@ -430,6 +430,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) > { > @@ -1040,3 +1046,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(&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; > +} > diff --git a/bisect.h b/bisect.h > index acd12ef..0ae63d4 100644 > --- a/bisect.h > +++ b/bisect.h > @@ -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 > diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c > index 30e1031..e50934c 100644 > --- a/builtin/bisect--helper.c > +++ b/builtin/bisect--helper.c > @@ -5,10 +5,15 @@ > #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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") > +static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") > > 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 > }; > > @@ -83,7 +88,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > { > enum { > NEXT_ALL = 1, > - WRITE_TERMS > + WRITE_TERMS, > + BISECT_CLEAN_STATE > } cmdmode = 0; > int no_checkout = 0; > struct option options[] = { > @@ -91,6 +97,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > 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_BOOL(0, "no-checkout", &no_checkout, > N_("update BISECT_HEAD instead of checking out the current commit")), > OPT_END() > @@ -109,6 +117,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > if (argc != 2) > die(_("--write-terms requires two arguments")); > return write_terms(argv[0], argv[1]); > + case BISECT_CLEAN_STATE: > + if (argc != 0) > + die(_("--bisect-clean-state requires no arguments")); > + return bisect_clean_state(); > default: > die("BUG: unknown subcommand '%d'", cmdmode); > } > diff --git a/git-bisect.sh b/git-bisect.sh > index 9ef6cb8..f1202df 100755 > --- a/git-bisect.sh > +++ b/git-bisect.sh > @@ -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 > > # > @@ -430,27 +430,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 () { The change as compared to the v12 is that the function `bisect_clean_state()` has been moved to bisect.c from builtin/bisect--helper.c because in a future function conversion (bisect_next()) I needed bisect_clean_state() in bisect.c because bisect.c uses exit() whose signals can be easily trapped by the shell function and then do the cleanup. But now it would be essential to convert those exit() statements to return statements. This has been done. One can find it here[1]. [1]: https://github.com/pranitbauva1997/git/pull/22 Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v13 10/13] bisect--helper: `check_and_set_terms` shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 09/13] bisect--helper: `bisect_write` shell function in C Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 04/13] bisect--helper: `bisect_clean_state` " Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 07/13] bisect--helper: `bisect_reset` " Pranit Bauva ` (10 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement the `check_and_set_terms` shell function in C and add `check-and-set-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-and-set-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. check_and_set_terms() sets and receives two global variables namely TERM_GOOD and TERM_BAD in the shell script. Luckily the file BISECT_TERMS also contains the value of those variables so its appropriate to evoke the method get_terms() after calling the subcommand so that it retrieves the value of TERM_GOOD and TERM_BAD from the file BISECT_TERMS. The two global variables are passed as arguments to the subcommand. Also introduce bisect_terms_reset() to empty the contents of `term_good` and `term_bad` of `struct bisect_terms`. Also introduce set_terms() to copy the `term_good` and `term_bad` into `struct bisect_terms` and write it out to the file BISECT_TERMS. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 36 ++++----------------------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 5364960..450426c 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -20,6 +20,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-clean-state"), N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), + N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), NULL }; @@ -40,6 +41,12 @@ static void bisect_terms_release(struct bisect_terms *terms) strbuf_release(&terms->term_bad); } +static void bisect_terms_reset(struct bisect_terms *term) +{ + strbuf_reset(&term->term_good); + strbuf_reset(&term->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -213,6 +220,39 @@ static int bisect_write(const char *state, const char *rev, return 0; } +static int set_terms(struct bisect_terms *terms, const char *bad, + const char *good) +{ + bisect_terms_reset(terms); + strbuf_addstr(&terms->term_good, good); + strbuf_addstr(&terms->term_bad, bad); + return write_terms(terms->term_bad.buf, terms->term_good.buf); +} + +static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) +{ + int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms()); + + if (one_of(cmd, "skip", "start", "terms", NULL)) + return 0; + + if (has_term_file && + strcmp(cmd, terms->term_bad.buf) && + strcmp(cmd, terms->term_good.buf)) + return error(_("Invalid command: you're currently in a " + "%s/%s bisect"), terms->term_bad.buf, + terms->term_good.buf); + + if (!has_term_file) { + if (one_of(cmd, "bad", "good", NULL)) + return set_terms(terms, "bad", "good"); + if (one_of(cmd, "new", "old", NULL)) + return set_terms(terms, "new", "old"); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -221,7 +261,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_CLEAN_STATE, BISECT_RESET, CHECK_EXPECTED_REVS, - BISECT_WRITE + BISECT_WRITE, + CHECK_AND_SET_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -237,6 +278,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check for expected revs"), CHECK_EXPECTED_REVS), OPT_CMDMODE(0, "bisect-write", &cmdmode, N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), + OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, + N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -280,6 +323,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[3]); res = bisect_write(argv[0], argv[1], &terms, nolog); break; + case CHECK_AND_SET_TERMS: + if (argc != 3) + die(_("--check-and-set-terms requires 3 arguments")); + strbuf_addstr(&terms.term_good, argv[1]); + strbuf_addstr(&terms.term_bad, argv[2]); + res = check_and_set_terms(&terms, argv[0]); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index dfdec33..bdf2227 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -238,7 +238,8 @@ bisect_skip() { bisect_state() { bisect_autostart state=$1 - check_and_set_terms $state + 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." ;; @@ -390,7 +391,8 @@ bisect_replay () { command="$bisect" fi get_terms - check_and_set_terms "$command" + git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit + get_terms case "$command" in start) cmd="bisect_start $rev" @@ -480,36 +482,6 @@ get_terms () { fi } -check_and_set_terms () { - cmd="$1" - case "$cmd" in - skip|start|terms) ;; - *) - if test -s "$GIT_DIR/BISECT_TERMS" && test "$cmd" != "$TERM_BAD" && test "$cmd" != "$TERM_GOOD" - then - die "$(eval_gettext "Invalid command: you're currently in a \$TERM_BAD/\$TERM_GOOD bisect.")" - fi - case "$cmd" in - bad|good) - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - 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 - TERM_BAD=new - TERM_GOOD=old - git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit - fi - ;; - esac ;; - esac -} - bisect_voc () { case "$1" in bad) echo "bad|new" ;; -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 07/13] bisect--helper: `bisect_reset` shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (2 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 10/13] bisect--helper: `check_and_set_terms` " Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 05/13] t6030: explicitly test for bisection cleanup Pranit Bauva ` (9 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement `bisect_reset` shell function in C and add a `--bisect-reset` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `bisect_reset` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired but its implementation will be called by some other method. Note: --bisect-clean-state subcommand has not been retired as there are still a function namely `bisect_start()` which still uses this subcommand. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 28 ++-------------------------- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index e50934c..9aba094 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -3,17 +3,22 @@ #include "parse-options.h" #include "bisect.h" #include "refs.h" +#include "dir.h" +#include "argv-array.h" +#include "run-command.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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") +static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD") 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"), + N_("git bisect--helper --bisect-reset [<commit>]"), NULL }; @@ -84,12 +89,47 @@ static int write_terms(const char *bad, const char *good) return (res < 0) ? -1 : 0; } +static int bisect_reset(const char *commit) +{ + struct strbuf branch = STRBUF_INIT; + + if (!commit) { + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { + printf("We are not bisecting.\n"); + return 0; + } + strbuf_rtrim(&branch); + } else { + struct object_id oid; + if (get_oid(commit, &oid)) + return error(_("'%s' is not a valid commit"), commit); + strbuf_addstr(&branch, commit); + } + + if (!file_exists(git_path_bisect_head())) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("Could not check out original HEAD '%s'. Try " + "'git bisect reset <commit>'."), branch.buf); + strbuf_release(&branch); + argv_array_clear(&argv); + return -1; + } + argv_array_clear(&argv); + } + + strbuf_release(&branch); + return bisect_clean_state(); +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, - BISECT_CLEAN_STATE + BISECT_CLEAN_STATE, + BISECT_RESET } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -99,6 +139,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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, "bisect-reset", &cmdmode, + N_("reset the bisection state"), BISECT_RESET), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -121,6 +163,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 0) die(_("--bisect-clean-state requires no arguments")); return bisect_clean_state(); + case BISECT_RESET: + if (argc > 1) + die(_("--bisect-reset requires either zero or one arguments")); + return bisect_reset(argc ? argv[0] : NULL); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index f1202df..442397b 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -409,35 +409,11 @@ bisect_visualize() { eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES") } -bisect_reset() { - test -s "$GIT_DIR/BISECT_START" || { - gettextln "We are not bisecting." - return - } - case "$#" in - 0) branch=$(cat "$GIT_DIR/BISECT_START") ;; - 1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || { - invalid="$1" - die "$(eval_gettext "'\$invalid' is not a valid commit")" - } - branch="$1" ;; - *) - usage ;; - esac - - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- - then - die "$(eval_gettext "Could not check out original HEAD '\$branch'. -Try 'git bisect reset <commit>'.")" - fi - git bisect--helper --bisect-clean-state || exit -} - bisect_replay () { file="$1" test "$#" -eq 1 || die "$(gettext "No logfile given")" test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")" - bisect_reset + git bisect--helper --bisect-reset || exit while read git bisect command rev do test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue @@ -627,7 +603,7 @@ case "$#" in visualize|view) bisect_visualize "$@" ;; reset) - bisect_reset "$@" ;; + git bisect--helper --bisect-reset "$@" ;; replay) bisect_replay "$@" ;; log) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 05/13] t6030: explicitly test for bisection cleanup 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (3 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 07/13] bisect--helper: `bisect_reset` " Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 02/13] bisect: rewrite `check_term_format` shell function in C Pranit Bauva ` (8 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Add test to explicitly check that 'git bisect reset' is working as expected. This is already covered implicitly by the test suite. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- I faced this problem while converting `bisect_clean_state` and the tests where showing breakages but it wasn't clear as to where exactly are they breaking. This will patch will help in that. Also I tested the test coverage of the test suite before this patch and it covers this (I did this by purposely changing names of files in git-bisect.sh and running the test suite). --- t/t6030-bisect-porcelain.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index 5e5370f..18e7998 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -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 -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 02/13] bisect: rewrite `check_term_format` shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (4 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 05/13] t6030: explicitly test for bisection cleanup Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` " Pranit Bauva ` (7 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement the `check_term_format` shell function in C and add a `--check-term-format` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-term-format` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and its implementation will be called by some other method/subcommand. For eg. In conversion of write_terms() of git-bisect.sh, the subcommand will be removed and instead check_term_format() will be called in its C implementation while a new subcommand will be introduced for write_terms(). Helped-by: Johannes Schindelein <Johannes.Schindelein@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 31 ++----------------------- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 8111c91..a47f1f2 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -2,19 +2,73 @@ #include "cache.h" #include "parse-options.h" #include "bisect.h" +#include "refs.h" static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), + N_("git bisect--helper --check-term-format <term> <orig_term>"), 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", 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - enum { NEXT_ALL = 1 } cmdmode = 0; + enum { + NEXT_ALL = 1, + CHECK_TERM_FMT + } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), + OPT_CMDMODE(0, "check-term-format", &cmdmode, + N_("check format of the term"), CHECK_TERM_FMT), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -29,6 +83,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); + case CHECK_TERM_FMT: + if (argc != 2) + die(_("--check-term-format requires two arguments")); + return check_term_format(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index ae3cb01..a727c59 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -564,38 +564,11 @@ write_terms () { then die "$(gettext "please use two different terms")" fi - check_term_format "$TERM_BAD" bad - check_term_format "$TERM_GOOD" good + git bisect--helper --check-term-format "$TERM_BAD" bad || exit + git bisect--helper --check-term-format "$TERM_GOOD" good || exit 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 -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (5 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 02/13] bisect: rewrite `check_term_format` shell function in C Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 12/13] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva ` (6 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement `is_expected_rev` & `check_expected_revs` shell function in C and add a `--check-expected-revs` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--check-expected-revs` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired but its implementation will be called by some other method. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 33 ++++++++++++++++++++++++++++++++- git-bisect.sh | 20 ++------------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 9aba094..711be75 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -123,13 +123,40 @@ static int bisect_reset(const char *commit) return bisect_clean_state(); } +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) >= 0) { + strbuf_trim(&actual_hex); + res = !strcmp(actual_hex.buf, expected_hex); + } + strbuf_release(&actual_hex); + return res; +} + +static int 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()); + return 0; + } + } + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, BISECT_CLEAN_STATE, - BISECT_RESET + BISECT_RESET, + CHECK_EXPECTED_REVS } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -141,6 +168,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("cleanup the bisection state"), BISECT_CLEAN_STATE), OPT_CMDMODE(0, "bisect-reset", &cmdmode, N_("reset the bisection state"), BISECT_RESET), + 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() @@ -167,6 +196,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); return bisect_reset(argc ? argv[0] : NULL); + case CHECK_EXPECTED_REVS: + return check_expected_revs(argv, argc); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 442397b..c3e43248 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -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.")" ;; *) -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 12/13] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (6 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 08/13] bisect--helper: `is_expected_rev` & `check_expected_revs` " Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 11/13] bisect--helper: `bisect_next_check` & bisect_voc " Pranit Bauva ` (5 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement the `get_terms` and `bisect_terms` shell function in C and add `bisect-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++-- git-bisect.sh | 35 ++-------------------------- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index ada3220d..e1cf956 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -23,6 +23,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), + N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), NULL }; @@ -344,6 +345,52 @@ static int bisect_next_check(const struct bisect_terms *terms, return 0; } +static int get_terms(struct bisect_terms *terms) +{ + FILE *fp; + int res; + fp = fopen(git_path_bisect_terms(), "r"); + if (!fp) + return -1; + + bisect_terms_reset(terms); + res = strbuf_getline(&terms->term_bad, fp) == EOF || + strbuf_getline(&terms->term_good, fp) == EOF; + + fclose(fp); + return res ? -1 : 0; +} + +static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) +{ + int i; + + if (get_terms(terms)) { + fprintf(stderr, N_("no terms defined\n")); + return -1; + } + if (argc == 0) { + printf(N_("Your current terms are %s for the old state\nand " + "%s for the new state.\n"), terms->term_good.buf, + terms->term_bad.buf); + return 0; + } + + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "--term-good")) + printf(N_("%s\n"), terms->term_good.buf); + else if (!strcmp(argv[i], "--term-bad")) + printf(N_("%s\n"), terms->term_bad.buf); + else + printf(N_("invalid argument %s for 'git bisect " + "terms'.\nSupported options are: " + "--term-good|--term-old and " + "--term-bad|--term-new."), argv[i]); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -354,7 +401,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) CHECK_EXPECTED_REVS, BISECT_WRITE, CHECK_AND_SET_TERMS, - BISECT_NEXT_CHECK + BISECT_NEXT_CHECK, + BISECT_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -374,6 +422,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_CMDMODE(0, "bisect-next-check", &cmdmode, N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), + OPT_CMDMODE(0, "bisect-terms", &cmdmode, + N_("print out the bisect terms"), BISECT_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -382,7 +432,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, - git_bisect_helper_usage, 0); + git_bisect_helper_usage, PARSE_OPT_KEEP_UNKNOWN); if (!cmdmode) usage_with_options(git_bisect_helper_usage, options); @@ -431,6 +481,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[1]); res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); break; + case BISECT_TERMS: + if (argc > 1) + die(_("--bisect-terms requires 0 or 1 argument")); + res = bisect_terms(&terms, argv, argc); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index fe6c9d0..d6c8b5a 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -355,7 +355,7 @@ bisect_replay () { "$TERM_GOOD"|"$TERM_BAD"|skip) git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) - bisect_terms $rev ;; + git bisect--helper --bisect-terms $rev || exit;; *) die "$(gettext "?? what are you talking about?")" ;; esac @@ -437,37 +437,6 @@ get_terms () { fi } -bisect_terms () { - get_terms - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - die "$(gettext "no terms defined")" - fi - case "$#" in - 0) - gettextln "Your current terms are $TERM_GOOD for the old state -and $TERM_BAD for the new state." - ;; - 1) - arg=$1 - case "$arg" in - --term-good|--term-old) - printf '%s\n' "$TERM_GOOD" - ;; - --term-bad|--term-new) - printf '%s\n' "$TERM_BAD" - ;; - *) - die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'. -Supported options are: --term-good|--term-old and --term-bad|--term-new.")" - ;; - esac - ;; - *) - usage ;; - esac -} - case "$#" in 0) usage ;; @@ -498,7 +467,7 @@ case "$#" in run) bisect_run "$@" ;; terms) - bisect_terms "$@" ;; + git bisect--helper --bisect-terms "$@" || exit;; *) usage ;; esac -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 11/13] bisect--helper: `bisect_next_check` & bisect_voc shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (7 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 12/13] bisect--helper: `get_terms` & `bisect_terms` " Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva ` (4 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement `bisect_next_check` shell function in C and add `bisect-next-check` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also reimplement `bisect_voc` shell function in C and call it from `bisect_next_check` implementation in C. Using `--bisect-next-check` is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 60 ++------------------------- 2 files changed, 106 insertions(+), 57 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 450426c..ada3220d 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -6,6 +6,7 @@ #include "dir.h" #include "argv-array.h" #include "run-command.h" +#include "prompt.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -21,6 +22,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), + N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), NULL }; @@ -253,6 +255,95 @@ static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) return 0; } +static int mark_good(const char *refname, const struct object_id *oid, + int flag, void *cb_data) +{ + int *m_good = (int *)cb_data; + *m_good = 0; + return 1; +} + +static char *bisect_voc(char *revision_type) +{ + if (!strcmp(revision_type, "bad")) + return "bad|new"; + if (!strcmp(revision_type, "good")) + return "good|old"; + + return NULL; +} + +static int bisect_next_check(const struct bisect_terms *terms, + const char *current_term) +{ + int missing_good = 1, missing_bad = 1; + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); + char *good_glob = xstrfmt("%s-*", terms->term_good.buf); + char *bad_syn, *good_syn; + + if (ref_exists(bad_ref)) + missing_bad = 0; + free(bad_ref); + + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", + (void *) &missing_good); + free(good_glob); + + if (!missing_good && !missing_bad) + return 0; + + if (!current_term) + return -1; + + if (missing_good && !missing_bad && current_term && + !strcmp(current_term, terms->term_good.buf)) { + char *yesno; + /* + * have bad (or new) but not good (or old). We could bisect + * although this is less optimum. + */ + fprintf(stderr, N_("Warning: bisecting only with a %s commit\n"), + terms->term_bad.buf); + if (!isatty(0)) + return 0; + /* + * TRANSLATORS: Make sure to include [Y] and [n] in your + * translation. The program will only accept English input + * at this point. + */ + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); + if (starts_with(yesno, "N") || starts_with(yesno, "n")) + return -1; + + return 0; + } + bad_syn = xstrdup(bisect_voc("bad")); + good_syn = xstrdup(bisect_voc("good")); + if (!is_empty_or_missing_file(git_path_bisect_start())) { + error(_("You need to give me at least one %s and " + "%s revision. You can use \"git bisect %s\" " + "and \"git bisect %s\" for that. \n"), + bad_syn, good_syn, bad_syn, good_syn); + free(bad_syn); + free(good_syn); + return -1; + } + else { + error(_("You need to start by \"git bisect start\". You " + "then need to give me at least one %s and %s " + "revision. You can use \"git bisect %s\" and " + "\"git bisect %s\" for that.\n"), + good_syn, bad_syn, bad_syn, good_syn); + free(bad_syn); + free(good_syn); + return -1; + } + free(bad_syn); + free(good_syn); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -262,7 +353,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_RESET, CHECK_EXPECTED_REVS, BISECT_WRITE, - CHECK_AND_SET_TERMS + CHECK_AND_SET_TERMS, + BISECT_NEXT_CHECK } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -280,6 +372,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), + OPT_CMDMODE(0, "bisect-next-check", &cmdmode, + N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -330,6 +424,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[2]); res = check_and_set_terms(&terms, argv[0]); break; + case BISECT_NEXT_CHECK: + if (argc != 2 && argc != 3) + die(_("--bisect-next-check requires 2 or 3 arguments")); + strbuf_addstr(&terms.term_good, argv[0]); + strbuf_addstr(&terms.term_bad, argv[1]); + res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index bdf2227..fe6c9d0 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -271,59 +271,14 @@ bisect_state() { bisect_auto_next } -bisect_next_check() { - missing_good= missing_bad= - git show-ref -q --verify refs/bisect/$TERM_BAD || missing_bad=t - test -n "$(git for-each-ref "refs/bisect/$TERM_GOOD-*")" || missing_good=t - - case "$missing_good,$missing_bad,$1" in - ,,*) - : have both $TERM_GOOD and $TERM_BAD - ok - ;; - *,) - # do not have both but not asked to fail - just report. - false - ;; - t,,"$TERM_GOOD") - # have bad (or new) but not good (or old). we could bisect although - # this is less optimum. - eval_gettextln "Warning: bisecting only with a \$TERM_BAD commit." >&2 - if test -t 0 - then - # TRANSLATORS: Make sure to include [Y] and [n] in your - # translation. The program will only accept English input - # at this point. - gettext "Are you sure [Y/n]? " >&2 - read yesno - case "$yesno" in [Nn]*) exit 1 ;; esac - fi - : bisect without $TERM_GOOD... - ;; - *) - bad_syn=$(bisect_voc bad) - good_syn=$(bisect_voc good) - if test -s "$GIT_DIR/BISECT_START" - then - - eval_gettextln "You need to give me at least one \$bad_syn and one \$good_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - else - eval_gettextln "You need to start by \"git bisect start\". -You then need to give me at least one \$good_syn and one \$bad_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - fi - exit 1 ;; - esac -} - bisect_auto_next() { - bisect_next_check && bisect_next || : + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || : } bisect_next() { case "$#" in 0) ;; *) usage ;; esac bisect_autostart - bisect_next_check $TERM_GOOD + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit # Perform all bisection computation, display and checkout git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout) @@ -355,7 +310,7 @@ bisect_next() { } bisect_visualize() { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit if test $# = 0 then @@ -409,7 +364,7 @@ bisect_replay () { } bisect_run () { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit while true do @@ -482,13 +437,6 @@ get_terms () { fi } -bisect_voc () { - case "$1" in - bad) echo "bad|new" ;; - good) echo "good|old" ;; - esac -} - bisect_terms () { get_terms if ! test -s "$GIT_DIR/BISECT_TERMS" -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (8 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 11/13] bisect--helper: `bisect_next_check` & bisect_voc " Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 03/13] bisect--helper: `write_terms` shell function in C Pranit Bauva ` (3 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git is_empty_file() can help to refactor a lot of code. This will be very helpful in porting "git bisect" to C. Suggested-by: Torsten Bögershausen <tboegi@web.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/am.c | 20 ++------------------ cache.h | 3 +++ wrapper.c | 13 +++++++++++++ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 739b34d..9e1e9d6 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -30,22 +30,6 @@ #include "mailinfo.h" /** - * Returns 1 if the file is empty or does not exist, 0 otherwise. - */ -static int is_empty_file(const char *filename) -{ - struct stat st; - - if (stat(filename, &st) < 0) { - if (errno == ENOENT) - return 1; - die_errno(_("could not stat %s"), filename); - } - - return !st.st_size; -} - -/** * Returns the length of the first line of msg. */ static int linelen(const char *msg) @@ -1324,7 +1308,7 @@ static int parse_mail(struct am_state *state, const char *mail) goto finish; } - if (is_empty_file(am_path(state, "patch"))) { + if (is_empty_or_missing_file(am_path(state, "patch"))) { printf_ln(_("Patch is empty. Was it split wrong?")); die_user_resolve(state); } @@ -1896,7 +1880,7 @@ static void am_run(struct am_state *state, int resume) resume = 0; } - if (!is_empty_file(am_path(state, "rewritten"))) { + if (!is_empty_or_missing_file(am_path(state, "rewritten"))) { assert(state->rebasing); copy_notes_for_rebase(state); run_post_rewrite_hook(state); diff --git a/cache.h b/cache.h index b780a91..49f214b 100644 --- a/cache.h +++ b/cache.h @@ -1916,4 +1916,7 @@ void sleep_millisec(int millisec); */ void safe_create_dir(const char *dir, int share); +/* Return 1 if the file is empty or does not exists, 0 otherwise. */ +extern int is_empty_or_missing_file(const char *filename); + #endif /* CACHE_H */ diff --git a/wrapper.c b/wrapper.c index e7f1979..78f6431 100644 --- a/wrapper.c +++ b/wrapper.c @@ -679,3 +679,16 @@ void sleep_millisec(int millisec) { poll(NULL, 0, millisec); } + +int is_empty_or_missing_file(const char *filename) +{ + struct stat st; + + if (stat(filename, &st) < 0) { + if (errno == ENOENT) + return 1; + die_errno(_("could not stat %s"), filename); + } + + return !st.st_size; +} -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 03/13] bisect--helper: `write_terms` shell function in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (9 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 06/13] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-19 20:32 ` [PATCH v13 13/13] bisect--helper: `bisect_start` shell function partially " Pranit Bauva ` (2 subsequent siblings) 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement the `write_terms` shell function in C and add a `write-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also remove the subcommand `--check-term-format` as it can now be called from inside the function write_terms() C implementation. Also `|| exit` is added when calling write-terms subcommand from git-bisect.sh so as to exit whenever there is an error. Using `--write-terms` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and its implementation will be called by some other method. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 36 +++++++++++++++++++++++++++++------- git-bisect.sh | 22 +++++++--------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index a47f1f2..30e1031 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,9 +4,11 @@ #include "bisect.h" #include "refs.h" +static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") + static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), - N_("git bisect--helper --check-term-format <term> <orig_term>"), + N_("git bisect--helper --write-terms <bad_term> <good_term>"), NULL }; @@ -57,18 +59,38 @@ static int check_term_format(const char *term, const char *orig_term) return 0; } +static int write_terms(const char *bad, const char *good) +{ + FILE *fp; + 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - CHECK_TERM_FMT + WRITE_TERMS } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), - OPT_CMDMODE(0, "check-term-format", &cmdmode, - N_("check format of the term"), CHECK_TERM_FMT), + OPT_CMDMODE(0, "write-terms", &cmdmode, + N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -83,10 +105,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); - case CHECK_TERM_FMT: + case WRITE_TERMS: if (argc != 2) - die(_("--check-term-format requires two arguments")); - return check_term_format(argv[0], argv[1]); + die(_("--write-terms requires two arguments")); + return write_terms(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index a727c59..9ef6cb8 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -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" fi && echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit # @@ -557,18 +557,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 - git bisect--helper --check-term-format "$TERM_BAD" bad || exit - git bisect--helper --check-term-format "$TERM_GOOD" good || exit - printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS" -} - check_and_set_terms () { cmd="$1" case "$cmd" in @@ -582,13 +570,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 ;; -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v13 13/13] bisect--helper: `bisect_start` shell function partially in C 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (10 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 03/13] bisect--helper: `write_terms` shell function in C Pranit Bauva @ 2016-08-19 20:32 ` Pranit Bauva 2016-08-21 11:14 ` [PATCH v13 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-19 20:32 UTC (permalink / raw) To: git Reimplement the `bisect_start` shell function partially in C and add `bisect-start` subcommand to `git bisect--helper` to call it from git-bisect.sh . The last part is not converted because it calls another shell function. `bisect_start` shell function will be completed after the `bisect_next` shell function is ported in C. Using `--bisect-start` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 252 +++++++++++++++++++++++++++++++++++++++++++++-- git-bisect.sh | 133 +------------------------ 2 files changed, 246 insertions(+), 139 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index e1cf956..ff1dfc7 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -7,6 +7,7 @@ #include "argv-array.h" #include "run-command.h" #include "prompt.h" +#include "quote.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -14,6 +15,8 @@ static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK") static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD") +static GIT_PATH_FUNC(git_path_head_name, "head-name") +static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), @@ -24,6 +27,8 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), + N_("git bisect--helper --bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]" + "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"), NULL }; @@ -303,7 +308,7 @@ static int bisect_next_check(const struct bisect_terms *terms, * have bad (or new) but not good (or old). We could bisect * although this is less optimum. */ - fprintf(stderr, N_("Warning: bisecting only with a %s commit\n"), + fprintf(stderr, _("Warning: bisecting only with a %s commit\n"), terms->term_bad.buf); if (!isatty(0)) return 0; @@ -366,11 +371,11 @@ static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) int i; if (get_terms(terms)) { - fprintf(stderr, N_("no terms defined\n")); + fprintf(stderr, _("no terms defined\n")); return -1; } if (argc == 0) { - printf(N_("Your current terms are %s for the old state\nand " + printf(_("Your current terms are %s for the old state\nand " "%s for the new state.\n"), terms->term_good.buf, terms->term_bad.buf); return 0; @@ -378,11 +383,11 @@ static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "--term-good")) - printf(N_("%s\n"), terms->term_good.buf); + printf(_("%s\n"), terms->term_good.buf); else if (!strcmp(argv[i], "--term-bad")) - printf(N_("%s\n"), terms->term_bad.buf); + printf(_("%s\n"), terms->term_bad.buf); else - printf(N_("invalid argument %s for 'git bisect " + printf(_("invalid argument %s for 'git bisect " "terms'.\nSupported options are: " "--term-good|--term-old and " "--term-bad|--term-new."), argv[i]); @@ -391,6 +396,228 @@ static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) return 0; } +static int bisect_start(struct bisect_terms *terms, int no_checkout, + const char **argv, int argc) +{ + int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0; + int flags, pathspec_pos; + struct string_list revs = STRING_LIST_INIT_DUP; + struct string_list states = STRING_LIST_INIT_DUP; + struct strbuf start_head = STRBUF_INIT; + struct strbuf bisect_names = STRBUF_INIT; + struct strbuf orig_args = STRBUF_INIT; + const char *head; + unsigned char sha1[20]; + FILE *fp; + struct object_id oid; + + if (is_bare_repository()) + no_checkout = 1; + + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "--")) { + has_double_dash = 1; + break; + } + } + + for (i = 0; i < argc; i++) { + char *commit_id = xstrfmt("%s^{commit}", argv[i]); + if (!strcmp(argv[i], "--")) { + has_double_dash = 1; + break; + } else if (!strcmp(argv[i], "--no-checkout")) + no_checkout = 1; + else if (!strcmp(argv[i], "--term-good") || + !strcmp(argv[i], "--term-old")) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[++i]); + } else if (skip_prefix(argv[i], "--term-good=", &argv[i])) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[i]); + } else if (skip_prefix(argv[i], "--term-old=", &argv[i])) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[i]); + } else if (!strcmp(argv[i], "--term-bad") || + !strcmp(argv[i], "--term-new")) { + must_write_terms = 1; + strbuf_reset(&terms->term_bad); + strbuf_addstr(&terms->term_bad, argv[++i]); + } else if (skip_prefix(argv[i], "--term-bad=", &argv[i])) { + must_write_terms = 1; + strbuf_reset(&terms->term_bad); + strbuf_addstr(&terms->term_bad, argv[i]); + } else if (skip_prefix(argv[i], "--term-new=", &argv[i])) { + must_write_terms = 1; + strbuf_reset(&terms->term_good); + strbuf_addstr(&terms->term_good, argv[i]); + } else if (starts_with(argv[i], "--") && + !one_of(argv[i], "--term-good", "--term-bad", NULL)) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("unrecognised option: '%s'"), argv[i]); + } else if (get_oid(argv[i], &oid) && has_double_dash) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + free(commit_id); + die(_("'%s' does not appear to be a valid revision"), argv[i]); + } else { + free(commit_id); + string_list_append(&revs, oid_to_hex(&oid)); + } + } + pathspec_pos = i; + + /* + * The user ran "git bisect start <sha1> <sha1>", hence did not + * explicitly specify the terms, but we are already starting to + * set references named with the default terms, and won't be able + * to change afterwards. + */ + must_write_terms |= !!revs.nr; + for (i = 0; i < revs.nr; i++) { + if (bad_seen) + string_list_append(&states, terms->term_good.buf); + else { + bad_seen = 1; + string_list_append(&states, terms->term_bad.buf); + } + } + + /* + * Verify HEAD + */ + head = resolve_ref_unsafe("HEAD", 0, sha1, &flags); + if (!head) { + if (get_sha1("HEAD", sha1)) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("Bad HEAD - I need a HEAD")); + } + } + if (!is_empty_or_missing_file(git_path_bisect_start())) { + /* Reset to the rev from where we started */ + strbuf_read_file(&start_head, git_path_bisect_start(), 0); + strbuf_trim(&start_head); + if (!no_checkout) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", start_head.buf, + "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("checking out '%s' failed. Try 'git " + "bisect start <valid-branch>'."), + start_head.buf); + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + } else { + if (starts_with(head, "refs/heads/") || + !get_oid_hex(head, &oid) || ref_exists(head)) { + /* + * This error message should only be triggered by + * cogito usage, and cogito users should understand + * it relates to cg-seek. + */ + if (!is_empty_or_missing_file(git_path_head_name())) { + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("won't bisect on cg-seek'ed tree")); + } + if (starts_with(head, "refs/heads/")) { + strbuf_reset(&start_head); + strbuf_addstr(&start_head, head + 11); + } + else { + strbuf_reset(&start_head); + strbuf_addstr(&start_head, sha1_to_hex(sha1)); + } + } else { + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + die(_("Bad HEAD - strange symbolic ref")); + } + } + + /* + * 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". + */ + + /* + * Write new start state + */ + write_file(git_path_bisect_start(), "%s\n", start_head.buf); + + if (no_checkout) { + get_oid(start_head.buf, &oid); + if (update_ref(NULL, "BISECT_HEAD", oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + strbuf_release(&start_head); + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + strbuf_release(&start_head); + + if (pathspec_pos < argc - 1) + sq_quote_argv(&bisect_names, argv + pathspec_pos, 0); + write_file(git_path_bisect_names(), "%s\n", bisect_names.buf); + strbuf_release(&bisect_names); + + for (i = 0; i < states.nr; i++) { + if (bisect_write(states.items[i].string, + revs.items[i].string, terms, 1)) { + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + return -1; + } + } + string_list_clear(&revs, 0); + string_list_clear(&states, 0); + + if (must_write_terms) + if (write_terms(terms->term_bad.buf, terms->term_good.buf)) + return -1; + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) + return -1; + + if (fprintf(fp, "git bisect start") < 1) + return -1; + + sq_quote_argv(&orig_args, argv, 0); + if (fprintf(fp, "%s", orig_args.buf) < 0) { + strbuf_release(&orig_args); + return -1; + } + strbuf_release(&orig_args); + if (fprintf(fp, "\n") < 1) { + fclose(fp); + return -1; + } + fclose(fp); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -402,7 +629,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_WRITE, CHECK_AND_SET_TERMS, BISECT_NEXT_CHECK, - BISECT_TERMS + BISECT_TERMS, + BISECT_START } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -424,6 +652,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), OPT_CMDMODE(0, "bisect-terms", &cmdmode, N_("print out the bisect terms"), BISECT_TERMS), + OPT_CMDMODE(0, "bisect-start", &cmdmode, + N_("start the bisect session"), BISECT_START), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -432,7 +662,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, - git_bisect_helper_usage, PARSE_OPT_KEEP_UNKNOWN); + git_bisect_helper_usage, + PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN); if (!cmdmode) usage_with_options(git_bisect_helper_usage, options); @@ -486,6 +717,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) die(_("--bisect-terms requires 0 or 1 argument")); res = bisect_terms(&terms, argv, argc); break; + case BISECT_START: + strbuf_addstr(&terms.term_good, "good"); + strbuf_addstr(&terms.term_bad, "bad"); + res = bisect_start(&terms, no_checkout, argv, argc); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index d6c8b5a..f0896b3 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -71,122 +71,7 @@ bisect_autostart() { } bisect_start() { - # - # Check for one bad and then some good revisions. - # - has_double_dash=0 - for arg; do - case "$arg" in --) has_double_dash=1; break ;; esac - done - orig_args=$(git rev-parse --sq-quote "$@") - bad_seen=0 - eval='' - must_write_terms=0 - revs='' - if test "z$(git rev-parse --is-bare-repository)" != zfalse - then - mode=--no-checkout - else - mode='' - fi - while [ $# -gt 0 ]; do - arg="$1" - case "$arg" in - --) - shift - break - ;; - --no-checkout) - mode=--no-checkout - shift ;; - --term-good|--term-old) - shift - must_write_terms=1 - TERM_GOOD=$1 - shift ;; - --term-good=*|--term-old=*) - must_write_terms=1 - TERM_GOOD=${1#*=} - shift ;; - --term-bad|--term-new) - shift - must_write_terms=1 - TERM_BAD=$1 - shift ;; - --term-bad=*|--term-new=*) - must_write_terms=1 - TERM_BAD=${1#*=} - shift ;; - --*) - die "$(eval_gettext "unrecognised option: '\$arg'")" ;; - *) - 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" - shift - ;; - esac - done - - for rev in $revs - do - # The user ran "git bisect start <sha1> - # <sha1>", hence did not explicitly specify - # the terms, but we are already starting to - # set references named with the default terms, - # and won't be able to change afterwards. - must_write_terms=1 - - case $bad_seen in - 0) state=$TERM_BAD ; bad_seen=1 ;; - *) state=$TERM_GOOD ;; - esac - eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" - done - # - # Verify HEAD. - # - head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) || - head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) || - die "$(gettext "Bad HEAD - I need a HEAD")" - - # - # Check if we are bisecting. - # - start_head='' - if test -s "$GIT_DIR/BISECT_START" - then - # Reset to the rev from where we started. - start_head=$(cat "$GIT_DIR/BISECT_START") - if test "z$mode" != "z--no-checkout" - then - git checkout "$start_head" -- || - die "$(eval_gettext "Checking out '\$start_head' failed. Try 'git bisect reset <valid-branch>'.")" - fi - else - # Get rev from where we start. - case "$head" in - refs/heads/*|$_x40) - # This error message should only be triggered by - # cogito usage, and cogito users should understand - # it relates to cg-seek. - [ -s "$GIT_DIR/head-name" ] && - die "$(gettext "won't bisect on cg-seek'ed tree")" - start_head="${head#refs/heads/}" - ;; - *) - die "$(gettext "Bad HEAD - strange symbolic ref")" - ;; - esac - fi - - # - # Get rid of any old bisect state. - # - git bisect--helper --bisect-clean-state || exit + git bisect--helper --bisect-start $@ || exit # # Change state. @@ -197,24 +82,10 @@ bisect_start() { # trap 'git bisect--helper --bisect-clean-state' 0 trap 'exit 255' 1 2 3 15 - - # - # Write new start state. - # - echo "$start_head" >"$GIT_DIR/BISECT_START" && { - test "z$mode" != "z--no-checkout" || - git update-ref --no-deref BISECT_HEAD "$start_head" - } && - git rev-parse --sq-quote "$@" >"$GIT_DIR/BISECT_NAMES" && - eval "$eval true" && - if test $must_write_terms -eq 1 - then - git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" - fi && - echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit # # Check if we can proceed to the next bisect state. # + get_terms bisect_auto_next trap '-' 0 -- https://github.com/git/git/pull/281 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v13 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (11 preceding siblings ...) 2016-08-19 20:32 ` [PATCH v13 13/13] bisect--helper: `bisect_start` shell function partially " Pranit Bauva @ 2016-08-21 11:14 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva 13 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-21 11:14 UTC (permalink / raw) To: Git List Hey everyone, On Sat, Aug 20, 2016 at 2:02 AM, Pranit Bauva <pranit.bauva@gmail.com> wrote: > `--next-all` is meant to be used as a subcommand to support multiple > "operation mode" though the current implementation does not contain any > other subcommand along side with `--next-all` but further commits will > include some more subcommands. > > Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> > Mentored-by: Lars Schneider <larsxschneider@gmail.com> > Mentored-by: Christian Couder <chriscool@tuxfamily.org> > Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> > --- > builtin/bisect--helper.c | 17 +++++++++++------ > 1 file changed, 11 insertions(+), 6 deletions(-) > > diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c > index 3324229..8111c91 100644 > --- a/builtin/bisect--helper.c > +++ b/builtin/bisect--helper.c > @@ -10,11 +10,11 @@ static const char * const git_bisect_helper_usage[] = { > > int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > { > - int next_all = 0; > + enum { NEXT_ALL = 1 } 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_BOOL(0, "no-checkout", &no_checkout, > N_("update BISECT_HEAD instead of checking out the current commit")), > OPT_END() > @@ -23,9 +23,14 @@ 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); > + default: > + die("BUG: unknown subcommand '%d'", cmdmode); > + } > + return 0; > } This is the same series (except for patch 04/13) and rebased on v2.10-rc0. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v14 01/27] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL 2016-08-19 20:32 ` [PATCH v13 " Pranit Bauva ` (12 preceding siblings ...) 2016-08-21 11:14 ` [PATCH v13 01/13] bisect--helper: use OPT_CMDMODE instead of OPT_BOOL Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 18/27] bisect--helper: `bisect_state` & `bisect_head` shell function in C Pranit Bauva ` (28 more replies) 13 siblings, 29 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git `--next-all` is meant to be used as a subcommand to support multiple "operation mode" though the current implementation does not contain any other subcommand along side with `--next-all` but further commits will include some more subcommands. Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3324229..8111c91 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -10,11 +10,11 @@ static const char * const git_bisect_helper_usage[] = { int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - int next_all = 0; + enum { NEXT_ALL = 1 } 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -23,9 +23,14 @@ 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); + default: + die("BUG: unknown subcommand '%d'", cmdmode); + } + return 0; } -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 18/27] bisect--helper: `bisect_state` & `bisect_head` shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 16/27] bisect--helper: retire `--next-all` subcommand Pranit Bauva ` (27 subsequent siblings) 28 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement the `bisect_state` shell function in C and also add a subcommand `--bisect-state` to `git-bisect--helper` to call it from git-bisect.sh . Using `--bisect-state` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. `bisect_head` is called from `bisect_state` thus its not required to introduce another subcommand. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ git-bisect.sh | 57 +++----------------------------- 2 files changed, 89 insertions(+), 52 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index a139592..861c07d 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -31,6 +31,8 @@ static const char * const git_bisect_helper_usage[] = { 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 }; @@ -790,6 +792,79 @@ static int bisect_autostart(struct bisect_terms *terms) return 0; } +static char *bisect_head(void) +{ + if (is_empty_or_missing_file(git_path_bisect_head())) + return "HEAD"; + else + return "BISECT_HEAD"; +} + +static int bisect_state(struct bisect_terms *terms, const char **argv, + int argc) +{ + const char *state = argv[0]; + + get_terms(terms); + if (check_and_set_terms(terms, state)) + return -1; + + if (!argc) + die(_("Please call `--bisect-state` with at least one argument")); + + if (argc == 1 && one_of(state, terms->term_good.buf, + terms->term_bad.buf, "skip", NULL)) { + const char *bisected_head = xstrdup(bisect_head()); + const char *hex[1]; + unsigned char sha1[20]; + + if (get_sha1(bisected_head, sha1)) + die(_("Bad rev input: %s"), bisected_head); + if (bisect_write(state, sha1_to_hex(sha1), terms, 0)) + return -1; + + *hex = xstrdup(sha1_to_hex(sha1)); + if (check_expected_revs(hex, 1)) + return -1; + return bisect_auto_next(terms, NULL); + } + + if ((argc == 2 && !strcmp(state, terms->term_bad.buf)) || + one_of(state, terms->term_good.buf, "skip", NULL)) { + int i; + struct string_list hex = STRING_LIST_INIT_DUP; + + for (i = 1; i < argc; i++) { + unsigned char sha1[20]; + + if (get_sha1(argv[i], sha1)) { + string_list_clear(&hex, 0); + die(_("Bad rev input: %s"), argv[i]); + } + string_list_append(&hex, sha1_to_hex(sha1)); + } + for (i = 0; i < hex.nr; i++) { + const char **hex_string = (const char **) &hex.items[i].string; + if(bisect_write(state, *hex_string, terms, 0)) { + string_list_clear(&hex, 0); + return -1; + } + if (check_expected_revs(hex_string, 1)) { + string_list_clear(&hex, 0); + return -1; + } + } + string_list_clear(&hex, 0); + return bisect_auto_next(terms, NULL); + } + + if (!strcmp(state, terms->term_bad.buf)) + die(_("'git bisect %s' can take only one argument."), + terms->term_bad.buf); + + return -1; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -804,6 +879,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_NEXT, BISECT_AUTO_NEXT, BISECT_AUTOSTART, + BISECT_STATE } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -829,6 +905,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("verify the next bisection state then find the next bisection state"), BISECT_AUTO_NEXT), OPT_CMDMODE(0, "bisect-autostart", &cmdmode, N_("start the bisection if BISECT_START empty or missing"), BISECT_AUTOSTART), + OPT_CMDMODE(0, "bisect-state", &cmdmode, + N_("mark the state of ref (or refs)"), BISECT_STATE), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -909,6 +987,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, "bad"); res = bisect_autostart(&terms); break; + case BISECT_STATE: + if (argc == 0) + die(_("--bisect-state requires at least 1 argument")); + get_terms(&terms); + res = bisect_state(&terms, argv, argc); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index cd56551..a9eebbb 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -39,16 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" TERM_BAD=bad TERM_GOOD=good -bisect_head() -{ - if test -f "$GIT_DIR/BISECT_HEAD" - then - echo BISECT_HEAD - else - echo HEAD - fi -} - bisect_skip() { all='' for arg in "$@" @@ -61,44 +51,7 @@ bisect_skip() { esac all="$all $revs" done - eval bisect_state 'skip' $all -} - -bisect_state() { - git bisect--helper --bisect-autostart - state=$1 - get_terms - 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 || exit + eval git bisect--helper --bisect-state 'skip' $all } bisect_visualize() { @@ -184,8 +137,8 @@ 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" ) + # We have to use a subshell because "--bisect-state" can exit. + ( git bisect--helper --bisect-state $state >"$GIT_DIR/BISECT_RUN" ) res=$? cat "$GIT_DIR/BISECT_RUN" @@ -200,7 +153,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 +'git bisect--helper --bisect-state \$state' exited with error code \$res" >&2 exit $res fi @@ -241,7 +194,7 @@ case "$#" in 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) -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 16/27] bisect--helper: retire `--next-all` subcommand 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 18/27] bisect--helper: `bisect_state` & `bisect_head` shell function in C Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 15/27] bisect--helper: retire `--bisect-clean-state` subcommand Pranit Bauva ` (26 subsequent siblings) 28 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git The `--next-all` subcommand is no longer used in the shell script and the function `bisect_next_all()` is called from the C implementation of `bisect_next()`. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index e351794..8cbcc3b 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -20,7 +20,6 @@ static GIT_PATH_FUNC(git_path_head_name, "head-name") static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") 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-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), @@ -764,8 +763,7 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout, int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { - NEXT_ALL = 1, - WRITE_TERMS, + WRITE_TERMS = 1, BISECT_RESET, CHECK_EXPECTED_REVS, BISECT_WRITE, @@ -778,8 +776,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) } cmdmode = 0; int no_checkout = 0, res = 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-reset", &cmdmode, @@ -816,8 +812,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { int nolog; - case NEXT_ALL: - return bisect_next_all(prefix, no_checkout); case WRITE_TERMS: if (argc != 2) die(_("--write-terms requires two arguments")); -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 15/27] bisect--helper: retire `--bisect-clean-state` subcommand 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 18/27] bisect--helper: `bisect_state` & `bisect_head` shell function in C Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 16/27] bisect--helper: retire `--next-all` subcommand Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-26 20:56 ` Junio C Hamano 2016-08-23 11:53 ` [PATCH v14 02/27] bisect: rewrite `check_term_format` shell function in C Pranit Bauva ` (25 subsequent siblings) 28 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git The `bisect-clean-state` subcommand is no longer used in the shell script while the C code uses `bisect_clean_state()` thus remove the subcommand. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index ef7b3a1..e351794 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -22,7 +22,6 @@ static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") 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"), N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), @@ -767,7 +766,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) enum { NEXT_ALL = 1, WRITE_TERMS, - BISECT_CLEAN_STATE, BISECT_RESET, CHECK_EXPECTED_REVS, BISECT_WRITE, @@ -784,8 +782,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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, "bisect-reset", &cmdmode, N_("reset the bisection state"), BISECT_RESET), OPT_CMDMODE(0, "check-expected-revs", &cmdmode, @@ -827,11 +823,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) die(_("--write-terms requires two arguments")); res = write_terms(argv[0], argv[1]); break; - case BISECT_CLEAN_STATE: - if (argc != 0) - die(_("--bisect-clean-state requires no arguments")); - res = bisect_clean_state(); - break; case BISECT_RESET: if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v14 15/27] bisect--helper: retire `--bisect-clean-state` subcommand 2016-08-23 11:53 ` [PATCH v14 15/27] bisect--helper: retire `--bisect-clean-state` subcommand Pranit Bauva @ 2016-08-26 20:56 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-26 20:56 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > The `bisect-clean-state` subcommand is no longer used in the shell > script while the C code uses `bisect_clean_state()` thus remove the > subcommand. Good. ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v14 02/27] bisect: rewrite `check_term_format` shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (2 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 15/27] bisect--helper: retire `--bisect-clean-state` subcommand Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 25/27] bisect--helper: retire `--bisect-autostart` subcommand Pranit Bauva ` (24 subsequent siblings) 28 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement the `check_term_format` shell function in C and add a `--check-term-format` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-term-format` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and its implementation will be called by some other method/subcommand. For eg. In conversion of write_terms() of git-bisect.sh, the subcommand will be removed and instead check_term_format() will be called in its C implementation while a new subcommand will be introduced for write_terms(). Helped-by: Johannes Schindelein <Johannes.Schindelein@gmx.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 31 ++----------------------- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 8111c91..a47f1f2 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -2,19 +2,73 @@ #include "cache.h" #include "parse-options.h" #include "bisect.h" +#include "refs.h" static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), + N_("git bisect--helper --check-term-format <term> <orig_term>"), 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", 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; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { - enum { NEXT_ALL = 1 } cmdmode = 0; + enum { + NEXT_ALL = 1, + CHECK_TERM_FMT + } cmdmode = 0; int no_checkout = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), + OPT_CMDMODE(0, "check-term-format", &cmdmode, + N_("check format of the term"), CHECK_TERM_FMT), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -29,6 +83,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) switch (cmdmode) { case NEXT_ALL: return bisect_next_all(prefix, no_checkout); + case CHECK_TERM_FMT: + if (argc != 2) + die(_("--check-term-format requires two arguments")); + return check_term_format(argv[0], argv[1]); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index ae3cb01..a727c59 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -564,38 +564,11 @@ write_terms () { then die "$(gettext "please use two different terms")" fi - check_term_format "$TERM_BAD" bad - check_term_format "$TERM_GOOD" good + git bisect--helper --check-term-format "$TERM_BAD" bad || exit + git bisect--helper --check-term-format "$TERM_GOOD" good || exit 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 -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 25/27] bisect--helper: retire `--bisect-autostart` subcommand 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (3 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 02/27] bisect: rewrite `check_term_format` shell function in C Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 10/27] bisect--helper: `check_and_set_terms` shell function in C Pranit Bauva ` (23 subsequent siblings) 28 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git The `--bisect-autostart` subcommand is no longer used in the shell script and the function `bisect_autostart()` is called from the C implementation. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 8982f29..a89e2f7 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -27,7 +27,6 @@ static const char * const git_bisect_helper_usage[] = { "[--no-checkout] [<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>...]"), N_("git bisect--helper --bisect-replay <filename>"), @@ -974,7 +973,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_START, BISECT_NEXT, BISECT_AUTO_NEXT, - BISECT_AUTOSTART, BISECT_STATE, BISECT_LOG, BISECT_REPLAY @@ -993,8 +991,6 @@ 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 find the next bisection state"), BISECT_AUTO_NEXT), - OPT_CMDMODE(0, "bisect-autostart", &cmdmode, - N_("start the bisection if BISECT_START empty or missing"), BISECT_AUTOSTART), OPT_CMDMODE(0, "bisect-state", &cmdmode, N_("mark the state of ref (or refs)"), BISECT_STATE), OPT_CMDMODE(0, "bisect-log", &cmdmode, @@ -1050,13 +1046,6 @@ 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) - die(_("--bisect-autostart requires 0 arguments")); - strbuf_addstr(&terms.term_good, "good"); - strbuf_addstr(&terms.term_bad, "bad"); - res = bisect_autostart(&terms); - break; case BISECT_STATE: if (argc == 0) die(_("--bisect-state requires at least 1 argument")); -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 10/27] bisect--helper: `check_and_set_terms` shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (4 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 25/27] bisect--helper: retire `--bisect-autostart` subcommand Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 05/27] t6030: explicitly test for bisection cleanup Pranit Bauva ` (22 subsequent siblings) 28 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement the `check_and_set_terms` shell function in C and add `check-and-set-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-and-set-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. check_and_set_terms() sets and receives two global variables namely TERM_GOOD and TERM_BAD in the shell script. Luckily the file BISECT_TERMS also contains the value of those variables so its appropriate to evoke the method get_terms() after calling the subcommand so that it retrieves the value of TERM_GOOD and TERM_BAD from the file BISECT_TERMS. The two global variables are passed as arguments to the subcommand. Also introduce bisect_terms_reset() to empty the contents of `term_good` and `term_bad` of `struct bisect_terms`. Also introduce set_terms() to copy the `term_good` and `term_bad` into `struct bisect_terms` and write it out to the file BISECT_TERMS. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 36 ++++----------------------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 5364960..450426c 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -20,6 +20,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-clean-state"), N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), + N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), NULL }; @@ -40,6 +41,12 @@ static void bisect_terms_release(struct bisect_terms *terms) strbuf_release(&terms->term_bad); } +static void bisect_terms_reset(struct bisect_terms *term) +{ + strbuf_reset(&term->term_good); + strbuf_reset(&term->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -213,6 +220,39 @@ static int bisect_write(const char *state, const char *rev, return 0; } +static int set_terms(struct bisect_terms *terms, const char *bad, + const char *good) +{ + bisect_terms_reset(terms); + strbuf_addstr(&terms->term_good, good); + strbuf_addstr(&terms->term_bad, bad); + return write_terms(terms->term_bad.buf, terms->term_good.buf); +} + +static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) +{ + int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms()); + + if (one_of(cmd, "skip", "start", "terms", NULL)) + return 0; + + if (has_term_file && + strcmp(cmd, terms->term_bad.buf) && + strcmp(cmd, terms->term_good.buf)) + return error(_("Invalid command: you're currently in a " + "%s/%s bisect"), terms->term_bad.buf, + terms->term_good.buf); + + if (!has_term_file) { + if (one_of(cmd, "bad", "good", NULL)) + return set_terms(terms, "bad", "good"); + if (one_of(cmd, "new", "old", NULL)) + return set_terms(terms, "new", "old"); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -221,7 +261,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_CLEAN_STATE, BISECT_RESET, CHECK_EXPECTED_REVS, - BISECT_WRITE + BISECT_WRITE, + CHECK_AND_SET_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -237,6 +278,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check for expected revs"), CHECK_EXPECTED_REVS), OPT_CMDMODE(0, "bisect-write", &cmdmode, N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), + OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, + N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -280,6 +323,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[3]); res = bisect_write(argv[0], argv[1], &terms, nolog); break; + case CHECK_AND_SET_TERMS: + if (argc != 3) + die(_("--check-and-set-terms requires 3 arguments")); + strbuf_addstr(&terms.term_good, argv[1]); + strbuf_addstr(&terms.term_bad, argv[2]); + res = check_and_set_terms(&terms, argv[0]); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index dfdec33..bdf2227 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -238,7 +238,8 @@ bisect_skip() { bisect_state() { bisect_autostart state=$1 - check_and_set_terms $state + 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." ;; @@ -390,7 +391,8 @@ bisect_replay () { command="$bisect" fi get_terms - check_and_set_terms "$command" + git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit + get_terms case "$command" in start) cmd="bisect_start $rev" @@ -480,36 +482,6 @@ get_terms () { fi } -check_and_set_terms () { - cmd="$1" - case "$cmd" in - skip|start|terms) ;; - *) - if test -s "$GIT_DIR/BISECT_TERMS" && test "$cmd" != "$TERM_BAD" && test "$cmd" != "$TERM_GOOD" - then - die "$(eval_gettext "Invalid command: you're currently in a \$TERM_BAD/\$TERM_GOOD bisect.")" - fi - case "$cmd" in - bad|good) - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - 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 - TERM_BAD=new - TERM_GOOD=old - git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit - fi - ;; - esac ;; - esac -} - bisect_voc () { case "$1" in bad) echo "bad|new" ;; -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 05/27] t6030: explicitly test for bisection cleanup 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (5 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 10/27] bisect--helper: `check_and_set_terms` shell function in C Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C Pranit Bauva ` (21 subsequent siblings) 28 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Add test to explicitly check that 'git bisect reset' is working as expected. This is already covered implicitly by the test suite. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- I faced this problem while converting `bisect_clean_state` and the tests where showing breakages but it wasn't clear as to where exactly are they breaking. This will patch will help in that. Also I tested the test coverage of the test suite before this patch and it covers this (I did this by purposely changing names of files in git-bisect.sh and running the test suite). --- t/t6030-bisect-porcelain.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index 5e5370f..18e7998 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -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 -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (6 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 05/27] t6030: explicitly test for bisection cleanup Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-24 21:12 ` Junio C Hamano 2016-08-23 11:53 ` [PATCH v14 26/27] bisect--helper: retire `--bisect-auto-next` subcommand Pranit Bauva ` (20 subsequent siblings) 28 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement `bisect_reset` shell function in C and add a `--bisect-reset` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `bisect_reset` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired but its implementation will be called by some other method. Note: --bisect-clean-state subcommand has not been retired as there are still a function namely `bisect_start()` which still uses this subcommand. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 28 ++-------------------------- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index e50934c..9aba094 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -3,17 +3,22 @@ #include "parse-options.h" #include "bisect.h" #include "refs.h" +#include "dir.h" +#include "argv-array.h" +#include "run-command.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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") +static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD") 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"), + N_("git bisect--helper --bisect-reset [<commit>]"), NULL }; @@ -84,12 +89,47 @@ static int write_terms(const char *bad, const char *good) return (res < 0) ? -1 : 0; } +static int bisect_reset(const char *commit) +{ + struct strbuf branch = STRBUF_INIT; + + if (!commit) { + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { + printf("We are not bisecting.\n"); + return 0; + } + strbuf_rtrim(&branch); + } else { + struct object_id oid; + if (get_oid(commit, &oid)) + return error(_("'%s' is not a valid commit"), commit); + strbuf_addstr(&branch, commit); + } + + if (!file_exists(git_path_bisect_head())) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("Could not check out original HEAD '%s'. Try " + "'git bisect reset <commit>'."), branch.buf); + strbuf_release(&branch); + argv_array_clear(&argv); + return -1; + } + argv_array_clear(&argv); + } + + strbuf_release(&branch); + return bisect_clean_state(); +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, - BISECT_CLEAN_STATE + BISECT_CLEAN_STATE, + BISECT_RESET } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -99,6 +139,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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, "bisect-reset", &cmdmode, + N_("reset the bisection state"), BISECT_RESET), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -121,6 +163,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 0) die(_("--bisect-clean-state requires no arguments")); return bisect_clean_state(); + case BISECT_RESET: + if (argc > 1) + die(_("--bisect-reset requires either zero or one arguments")); + return bisect_reset(argc ? argv[0] : NULL); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index f1202df..442397b 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -409,35 +409,11 @@ bisect_visualize() { eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES") } -bisect_reset() { - test -s "$GIT_DIR/BISECT_START" || { - gettextln "We are not bisecting." - return - } - case "$#" in - 0) branch=$(cat "$GIT_DIR/BISECT_START") ;; - 1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || { - invalid="$1" - die "$(eval_gettext "'\$invalid' is not a valid commit")" - } - branch="$1" ;; - *) - usage ;; - esac - - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- - then - die "$(eval_gettext "Could not check out original HEAD '\$branch'. -Try 'git bisect reset <commit>'.")" - fi - git bisect--helper --bisect-clean-state || exit -} - bisect_replay () { file="$1" test "$#" -eq 1 || die "$(gettext "No logfile given")" test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")" - bisect_reset + git bisect--helper --bisect-reset || exit while read git bisect command rev do test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue @@ -627,7 +603,7 @@ case "$#" in visualize|view) bisect_visualize "$@" ;; reset) - bisect_reset "$@" ;; + git bisect--helper --bisect-reset "$@" ;; replay) bisect_replay "$@" ;; log) -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C 2016-08-23 11:53 ` [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C Pranit Bauva @ 2016-08-24 21:12 ` Junio C Hamano 2016-08-26 13:46 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-24 21:12 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static int bisect_reset(const char *commit) > +{ > + struct strbuf branch = STRBUF_INIT; > + > + if (!commit) { > + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { Hmm, tricky but correct to do the "< 1" comparison. If the file does not exist, you'd get -1; if it fails to read it, you'd get -1; if it turns out to be empty, you'd get 0. > + printf("We are not bisecting.\n"); > + return 0; > + } > + strbuf_rtrim(&branch); > + } else { > + struct object_id oid; > + if (get_oid(commit, &oid)) > + return error(_("'%s' is not a valid commit"), commit); The original is rev-parse --quiet --verify "$1^{commit}" Unless the caller of this function already appended "^{commit}" to whatever the user gave "bisect--helper bisect-reset", this conversion is not equivalent. If you said git bisect reset HEAD: get_oid() would tell you that the top-level tree object of the current commit exists in the object store, but the original is meant to catch "That's not a commit, it's a tree!" before attempting to run "git checkout" on it. I think get_sha1_committish() is what you want to use here. > + strbuf_addstr(&branch, commit); > + } Also this version fails to catch "bisect reset a b c" as an error, I suspect. > @@ -627,7 +603,7 @@ case "$#" in > visualize|view) > bisect_visualize "$@" ;; > reset) > - bisect_reset "$@" ;; > + git bisect--helper --bisect-reset "$@" ;; > replay) > bisect_replay "$@" ;; > log) > > -- > https://github.com/git/git/pull/287 ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C 2016-08-24 21:12 ` Junio C Hamano @ 2016-08-26 13:46 ` Pranit Bauva 2016-08-26 16:29 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-26 13:46 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Thu, Aug 25, 2016 at 2:42 AM, Junio C Hamano <gitster@pobox.com> wrote: > > Pranit Bauva <pranit.bauva@gmail.com> writes: > > > +static int bisect_reset(const char *commit) > > +{ > > + struct strbuf branch = STRBUF_INIT; > > + > > + if (!commit) { > > + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { > > Hmm, tricky but correct to do the "< 1" comparison. If the file > does not exist, you'd get -1; if it fails to read it, you'd get -1; > if it turns out to be empty, you'd get 0. > > > + printf("We are not bisecting.\n"); > > + return 0; > > + } > > + strbuf_rtrim(&branch); > > + } else { > > + struct object_id oid; > > + if (get_oid(commit, &oid)) > > + return error(_("'%s' is not a valid commit"), commit); > > The original is > > rev-parse --quiet --verify "$1^{commit}" > > Unless the caller of this function already appended "^{commit}" to > whatever the user gave "bisect--helper bisect-reset", this > conversion is not equivalent. If you said > > git bisect reset HEAD: > > get_oid() would tell you that the top-level tree object of the > current commit exists in the object store, but the original is > meant to catch "That's not a commit, it's a tree!" before attempting > to run "git checkout" on it. > > I think get_sha1_committish() is what you want to use here. > > > + strbuf_addstr(&branch, commit); > > + } > Ya! get_sha1_committish() would be better. Thanks! > > Also this version fails to catch "bisect reset a b c" as an error, I > suspect. It didn't when I tried it right now. Could you please elaborate on why you think it can fail? There might be a thing which I haven't tested. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C 2016-08-26 13:46 ` Pranit Bauva @ 2016-08-26 16:29 ` Junio C Hamano 2016-08-27 10:52 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-26 16:29 UTC (permalink / raw) To: Pranit Bauva; +Cc: Git List Pranit Bauva <pranit.bauva@gmail.com> writes: >> Also this version fails to catch "bisect reset a b c" as an error, I >> suspect. > > It didn't when I tried it right now. Could you please elaborate on why > you think it can fail? There might be a thing which I haven't tested. My bad. I just compared your bisect_reset() implementation that had if (no specific commit) { reset to the branch } else { reset to the commit } with the original case $# in 0) reset to the branch ;; 1) reset to the commit ;; *) give usage and die ;; esac and took the difference and reacted "ah, excess parameters are not diagnosed in this function". Your caller does complain about excess parameters without giving usage, and that is what I missed. I am not sure if you intended to change the behaviour in this case to avoid giving the usage string; I tend to think it is a good change, but I didn't see it mentioned in the proposed commit log, which also contributed to my not noticing the test in the caller. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C 2016-08-26 16:29 ` Junio C Hamano @ 2016-08-27 10:52 ` Pranit Bauva 2016-08-29 17:06 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-27 10:52 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Fri, Aug 26, 2016 at 9:59 PM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >>> Also this version fails to catch "bisect reset a b c" as an error, I >>> suspect. >> >> It didn't when I tried it right now. Could you please elaborate on why >> you think it can fail? There might be a thing which I haven't tested. > > My bad. I just compared your bisect_reset() implementation that had > > if (no specific commit) { > reset to the branch > } else { > reset to the commit > } > > with the original > > case $# in > 0) reset to the branch ;; > 1) reset to the commit ;; > *) give usage and die ;; > esac > > and took the difference and reacted "ah, excess parameters are not > diagnosed in this function". > > Your caller does complain about excess parameters without giving > usage, and that is what I missed. > > I am not sure if you intended to change the behaviour in this case > to avoid giving the usage string; I tend to think it is a good > change, but I didn't see it mentioned in the proposed commit log, > which also contributed to my not noticing the test in the caller. I could include this in the commit message. Its not really something which we would want to test in the function because to the function, we are not passing the raw arguments. Since we are removing that check from the function but including it in cmd_bisect__helper(), I will talk about it in the commit message. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C 2016-08-27 10:52 ` Pranit Bauva @ 2016-08-29 17:06 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-29 17:06 UTC (permalink / raw) To: Pranit Bauva; +Cc: Git List Pranit Bauva <pranit.bauva@gmail.com> writes: >> with the original >> >> case $# in >> 0) reset to the branch ;; >> 1) reset to the commit ;; >> *) give usage and die ;; >> esac >> >> and took the difference and reacted "ah, excess parameters are not >> diagnosed in this function". >> >> Your caller does complain about excess parameters without giving >> usage, and that is what I missed. >> >> I am not sure if you intended to change the behaviour in this case >> to avoid giving the usage string; I tend to think it is a good >> change, but I didn't see it mentioned in the proposed commit log, >> which also contributed to my not noticing the test in the caller. > > I could include this in the commit message. Nah, it was something anybody could notice with 2 more minutes of reading and pondering from the patch text alone. Not worth spending more time on the log message on this one. Thanks. ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v14 26/27] bisect--helper: retire `--bisect-auto-next` subcommand 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (7 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 07/27] bisect--helper: `bisect_reset` shell function in C Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 12/27] bisect--helper: `get_terms` & `bisect_terms` shell function in C Pranit Bauva ` (19 subsequent siblings) 28 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git The `--bisect-auto-next` subcommand is no longer used in the shell script and the function `bisect_auto_next` is called from the C implementation. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index a89e2f7..7577b69e 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -26,7 +26,6 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]" "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"), N_("git bisect--helper --bisect-next"), - N_("git bisect--helper --bisect-auto-next"), N_("git bisect--helper --bisect-state (bad|new) [<rev>]"), N_("git bisect--helper --bisect-state (good|old) [<rev>...]"), N_("git bisect--helper --bisect-replay <filename>"), @@ -972,7 +971,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_TERMS, BISECT_START, BISECT_NEXT, - BISECT_AUTO_NEXT, BISECT_STATE, BISECT_LOG, BISECT_REPLAY @@ -989,8 +987,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("start the bisect session"), BISECT_START), OPT_CMDMODE(0, "bisect-next", &cmdmode, N_("find the next bisection commit"), BISECT_NEXT), - OPT_CMDMODE(0, "bisect-auto-next", &cmdmode, - N_("verify the next bisection state then find the next bisection state"), BISECT_AUTO_NEXT), OPT_CMDMODE(0, "bisect-state", &cmdmode, N_("mark the state of ref (or refs)"), BISECT_STATE), OPT_CMDMODE(0, "bisect-log", &cmdmode, @@ -1040,12 +1036,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) get_terms(&terms); res = bisect_next(&terms, prefix); break; - case BISECT_AUTO_NEXT: - if (argc) - die(_("--bisect-auto-next requires 0 arguments")); - get_terms(&terms); - res = bisect_auto_next(&terms, prefix); - break; case BISECT_STATE: if (argc == 0) die(_("--bisect-state requires at least 1 argument")); -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 12/27] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (8 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 26/27] bisect--helper: retire `--bisect-auto-next` subcommand Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-25 18:05 ` Junio C Hamano 2016-08-23 11:53 ` [PATCH v14 09/27] bisect--helper: `bisect_write` " Pranit Bauva ` (18 subsequent siblings) 28 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement the `get_terms` and `bisect_terms` shell function in C and add `bisect-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++-- git-bisect.sh | 35 ++-------------------------- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index aca3908..44adf6b 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -23,6 +23,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), + N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), NULL }; @@ -344,6 +345,52 @@ static int bisect_next_check(const struct bisect_terms *terms, return 0; } +static int get_terms(struct bisect_terms *terms) +{ + FILE *fp; + int res; + fp = fopen(git_path_bisect_terms(), "r"); + if (!fp) + return -1; + + bisect_terms_reset(terms); + res = strbuf_getline(&terms->term_bad, fp) == EOF || + strbuf_getline(&terms->term_good, fp) == EOF; + + fclose(fp); + return res ? -1 : 0; +} + +static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) +{ + int i; + + if (get_terms(terms)) { + fprintf(stderr, _("no terms defined\n")); + return -1; + } + if (argc == 0) { + printf(_("Your current terms are %s for the old state\nand " + "%s for the new state.\n"), terms->term_good.buf, + terms->term_bad.buf); + return 0; + } + + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "--term-good")) + printf("%s\n", terms->term_good.buf); + else if (!strcmp(argv[i], "--term-bad")) + printf("%s\n", terms->term_bad.buf); + else + printf(_("invalid argument %s for 'git bisect " + "terms'.\nSupported options are: " + "--term-good|--term-old and " + "--term-bad|--term-new."), argv[i]); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -354,7 +401,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) CHECK_EXPECTED_REVS, BISECT_WRITE, CHECK_AND_SET_TERMS, - BISECT_NEXT_CHECK + BISECT_NEXT_CHECK, + BISECT_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -374,6 +422,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_CMDMODE(0, "bisect-next-check", &cmdmode, N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), + OPT_CMDMODE(0, "bisect-terms", &cmdmode, + N_("print out the bisect terms"), BISECT_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -382,7 +432,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, - git_bisect_helper_usage, 0); + git_bisect_helper_usage, PARSE_OPT_KEEP_UNKNOWN); if (!cmdmode) usage_with_options(git_bisect_helper_usage, options); @@ -431,6 +481,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[1]); res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); break; + case BISECT_TERMS: + if (argc > 1) + die(_("--bisect-terms requires 0 or 1 argument")); + res = bisect_terms(&terms, argv, argc); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index fe6c9d0..d6c8b5a 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -355,7 +355,7 @@ bisect_replay () { "$TERM_GOOD"|"$TERM_BAD"|skip) git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) - bisect_terms $rev ;; + git bisect--helper --bisect-terms $rev || exit;; *) die "$(gettext "?? what are you talking about?")" ;; esac @@ -437,37 +437,6 @@ get_terms () { fi } -bisect_terms () { - get_terms - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - die "$(gettext "no terms defined")" - fi - case "$#" in - 0) - gettextln "Your current terms are $TERM_GOOD for the old state -and $TERM_BAD for the new state." - ;; - 1) - arg=$1 - case "$arg" in - --term-good|--term-old) - printf '%s\n' "$TERM_GOOD" - ;; - --term-bad|--term-new) - printf '%s\n' "$TERM_BAD" - ;; - *) - die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'. -Supported options are: --term-good|--term-old and --term-bad|--term-new.")" - ;; - esac - ;; - *) - usage ;; - esac -} - case "$#" in 0) usage ;; @@ -498,7 +467,7 @@ case "$#" in run) bisect_run "$@" ;; terms) - bisect_terms "$@" ;; + git bisect--helper --bisect-terms "$@" || exit;; *) usage ;; esac -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v14 12/27] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-08-23 11:53 ` [PATCH v14 12/27] bisect--helper: `get_terms` & `bisect_terms` shell function in C Pranit Bauva @ 2016-08-25 18:05 ` Junio C Hamano 2016-08-27 9:48 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-25 18:05 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) > +{ > + int i; > + > + if (get_terms(terms)) { > + fprintf(stderr, _("no terms defined\n")); > + return -1; > + } > + if (argc == 0) { > + printf(_("Your current terms are %s for the old state\nand " > + "%s for the new state.\n"), terms->term_good.buf, > + terms->term_bad.buf); > + return 0; > + } > + > + for (i = 0; i < argc; i++) { > + if (!strcmp(argv[i], "--term-good")) > + printf("%s\n", terms->term_good.buf); > + else if (!strcmp(argv[i], "--term-bad")) > + printf("%s\n", terms->term_bad.buf); > + else > + printf(_("invalid argument %s for 'git bisect " > + "terms'.\nSupported options are: " > + "--term-good|--term-old and " > + "--term-bad|--term-new."), argv[i]); > + } The original took only one and gave one answer (and errored out when the user asked for more), but this one loops. I can see either way is OK and do not think of a good reason to favor one over the other; unless there is a strong reason why you need this extended behaviour that allows users to ask multiple questions, I'd say we should keep the original behaviour. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 12/27] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-08-25 18:05 ` Junio C Hamano @ 2016-08-27 9:48 ` Pranit Bauva 2016-08-29 17:15 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-27 9:48 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Thu, Aug 25, 2016 at 11:35 PM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) >> +{ >> + int i; >> + >> + if (get_terms(terms)) { >> + fprintf(stderr, _("no terms defined\n")); >> + return -1; >> + } >> + if (argc == 0) { >> + printf(_("Your current terms are %s for the old state\nand " >> + "%s for the new state.\n"), terms->term_good.buf, >> + terms->term_bad.buf); >> + return 0; >> + } >> + >> + for (i = 0; i < argc; i++) { >> + if (!strcmp(argv[i], "--term-good")) >> + printf("%s\n", terms->term_good.buf); >> + else if (!strcmp(argv[i], "--term-bad")) >> + printf("%s\n", terms->term_bad.buf); >> + else >> + printf(_("invalid argument %s for 'git bisect " >> + "terms'.\nSupported options are: " >> + "--term-good|--term-old and " >> + "--term-bad|--term-new."), argv[i]); >> + } > > The original took only one and gave one answer (and errored out when > the user asked for more), but this one loops. I can see either way > is OK and do not think of a good reason to favor one over the other; > unless there is a strong reason why you need this extended behaviour > that allows users to ask multiple questions, I'd say we should keep > the original behaviour. True! I can just use return error() instead of printf. Also I noticed that this is printing to stdout while the original printed it to stderr. Thanks! Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 12/27] bisect--helper: `get_terms` & `bisect_terms` shell function in C 2016-08-27 9:48 ` Pranit Bauva @ 2016-08-29 17:15 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-29 17:15 UTC (permalink / raw) To: Pranit Bauva; +Cc: Git List Pranit Bauva <pranit.bauva@gmail.com> writes: >>> +static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) >>> +{ >>> + int i; >>> + >>> + if (get_terms(terms)) { >>> + fprintf(stderr, _("no terms defined\n")); >>> + return -1; >>> + } >>> + if (argc == 0) { >>> + printf(_("Your current terms are %s for the old state\nand " >>> + "%s for the new state.\n"), terms->term_good.buf, >>> + terms->term_bad.buf); >>> + return 0; >>> + } >>> + >>> + for (i = 0; i < argc; i++) { >>> + if (!strcmp(argv[i], "--term-good")) >>> + printf("%s\n", terms->term_good.buf); >>> + else if (!strcmp(argv[i], "--term-bad")) >>> + printf("%s\n", terms->term_bad.buf); >>> + else >>> + printf(_("invalid argument %s for 'git bisect " >>> + "terms'.\nSupported options are: " >>> + "--term-good|--term-old and " >>> + "--term-bad|--term-new."), argv[i]); >>> + } >> >> The original took only one and gave one answer (and errored out when >> the user asked for more), but this one loops. I can see either way >> is OK and do not think of a good reason to favor one over the other; >> unless there is a strong reason why you need this extended behaviour >> that allows users to ask multiple questions, I'd say we should keep >> the original behaviour. > > True! I can just use return error() instead of printf. Also I noticed > that this is printing to stdout while the original printed it to > stderr. Thanks! The original you removed because the above can take it over is this in your patch. -bisect_terms () { - get_terms - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - die "$(gettext "no terms defined")" - fi - case "$#" in - 0) - gettextln "Your current terms are $TERM_GOOD for the old state -and $TERM_BAD for the new state." - ;; - 1) - arg=$1 - case "$arg" in - --term-good|--term-old) - printf '%s\n' "$TERM_GOOD" - ;; - --term-bad|--term-new) - printf '%s\n' "$TERM_BAD" - ;; - *) - die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'. -Supported options are: --term-good|--term-old and --term-bad|--term-new.")" - ;; - esac - ;; - *) - usage ;; - esac -} - The fprintf() that says "no terms defined" can be made error(). The "invalid argument" message used to be die in the original, and should be sent to the standard error stream as you noticed. But a bigger difference is that the original made sure that the caller asked one question at a time. "terms --term-good --term-bad" was responded with a "usage". That is no longer true in the rewrite. ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v14 09/27] bisect--helper: `bisect_write` shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (9 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 12/27] bisect--helper: `get_terms` & `bisect_terms` shell function in C Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-24 22:30 ` Junio C Hamano 2016-08-23 11:53 ` [PATCH v14 06/27] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva ` (17 subsequent siblings) 28 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement the `bisect_write` shell function in C and add a `bisect-write` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--bisect-write` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Note: bisect_write() uses two variables namely TERM_GOOD and TERM_BAD from the global shell script thus we need to pass it to the subcommand using the arguments. We then store them in a struct bisect_terms and pass the memory address around functions. This patch also introduces new methods namely bisect_state_init() and bisect_terms_release() for easy memory management for the struct bisect_terms. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 97 ++++++++++++++++++++++++++++++++++++++++++++---- git-bisect.sh | 25 ++----------- 2 files changed, 94 insertions(+), 28 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 711be75..5364960 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -19,9 +19,27 @@ static const char * const git_bisect_helper_usage[] = { 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 <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), NULL }; +struct bisect_terms { + struct strbuf term_good; + struct strbuf term_bad; +}; + +static void bisect_terms_init(struct bisect_terms *terms) +{ + strbuf_init(&terms->term_good, 0); + strbuf_init(&terms->term_bad, 0); +} + +static void bisect_terms_release(struct bisect_terms *terms) +{ + strbuf_release(&terms->term_good); + strbuf_release(&terms->term_bad); +} + /* * Check whether the string `term` belongs to the set of strings * included in the variable arguments. @@ -149,6 +167,52 @@ static int check_expected_revs(const char **revs, int rev_nr) return 0; } +static int bisect_write(const char *state, const char *rev, + const struct bisect_terms *terms, int nolog) +{ + struct strbuf tag = STRBUF_INIT; + struct strbuf commit_name = STRBUF_INIT; + struct object_id oid; + struct commit *commit; + struct pretty_print_context pp = {0}; + FILE *fp; + + if (!strcmp(state, terms->term_bad.buf)) + strbuf_addf(&tag, "refs/bisect/%s", state); + else if (one_of(state, terms->term_good.buf, "skip", NULL)) + strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev); + else + return error(_("Bad bisect_write argument: %s"), state); + + if (get_oid(rev, &oid)) { + strbuf_release(&tag); + return error(_("couldn't get the oid of the rev '%s'"), rev); + } + + if (update_ref(NULL, tag.buf, oid.hash, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { + strbuf_release(&tag); + return -1; + } + strbuf_release(&tag); + + fp = fopen(git_path_bisect_log(), "a"); + if (!fp) + return error_errno(_("couldn't open the file '%s'"), git_path_bisect_log()); + + commit = lookup_commit_reference(oid.hash); + format_commit_message(commit, "%s", &commit_name, &pp); + fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash), + commit_name.buf); + strbuf_release(&commit_name); + + if (!nolog) + fprintf(fp, "git bisect %s %s\n", state, rev); + + fclose(fp); + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -156,9 +220,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) WRITE_TERMS, BISECT_CLEAN_STATE, BISECT_RESET, - CHECK_EXPECTED_REVS + CHECK_EXPECTED_REVS, + BISECT_WRITE } cmdmode = 0; - int no_checkout = 0; + int no_checkout = 0, res = 0; struct option options[] = { OPT_CMDMODE(0, "next-all", &cmdmode, N_("perform 'git bisect next'"), NEXT_ALL), @@ -170,10 +235,14 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("reset the bisection state"), BISECT_RESET), OPT_CMDMODE(0, "check-expected-revs", &cmdmode, N_("check for expected revs"), CHECK_EXPECTED_REVS), + OPT_CMDMODE(0, "bisect-write", &cmdmode, + N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() }; + struct bisect_terms terms; + bisect_terms_init(&terms); argc = parse_options(argc, argv, prefix, options, git_bisect_helper_usage, 0); @@ -182,24 +251,38 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) usage_with_options(git_bisect_helper_usage, options); switch (cmdmode) { + int nolog; case NEXT_ALL: return bisect_next_all(prefix, no_checkout); case WRITE_TERMS: if (argc != 2) die(_("--write-terms requires two arguments")); - return write_terms(argv[0], argv[1]); + res = write_terms(argv[0], argv[1]); + break; case BISECT_CLEAN_STATE: if (argc != 0) die(_("--bisect-clean-state requires no arguments")); - return bisect_clean_state(); + res = bisect_clean_state(); + break; case BISECT_RESET: if (argc > 1) die(_("--bisect-reset requires either zero or one arguments")); - return bisect_reset(argc ? argv[0] : NULL); + res = bisect_reset(argc ? argv[0] : NULL); + break; case CHECK_EXPECTED_REVS: - return check_expected_revs(argv, argc); + res = check_expected_revs(argv, argc); + break; + case BISECT_WRITE: + if (argc != 4 && argc != 5) + die(_("--bisect-write requires either 4 or 5 arguments")); + nolog = (argc == 5) && !strcmp(argv[4], "nolog"); + strbuf_addstr(&terms.term_good, argv[2]); + strbuf_addstr(&terms.term_bad, argv[3]); + res = bisect_write(argv[0], argv[1], &terms, nolog); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } - return 0; + bisect_terms_release(&terms); + return res; } diff --git a/git-bisect.sh b/git-bisect.sh index c3e43248..dfdec33 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -144,7 +144,7 @@ bisect_start() { 0) state=$TERM_BAD ; bad_seen=1 ;; *) state=$TERM_GOOD ;; esac - eval="$eval bisect_write '$state' '$rev' 'nolog' &&" + eval="$eval git bisect--helper --bisect-write '$state' '$rev' '$TERM_GOOD' '$TERM_BAD' 'nolog' &&" done # # Verify HEAD. @@ -220,23 +220,6 @@ bisect_start() { trap '-' 0 } -bisect_write() { - state="$1" - rev="$2" - nolog="$3" - case "$state" in - "$TERM_BAD") - tag="$state" ;; - "$TERM_GOOD"|skip) - tag="$state"-"$rev" ;; - *) - die "$(eval_gettext "Bad bisect_write argument: \$state")" ;; - esac - git update-ref "refs/bisect/$tag" "$rev" || exit - echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG" - test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" -} - bisect_skip() { all='' for arg in "$@" @@ -263,7 +246,7 @@ bisect_state() { bisected_head=$(bisect_head) rev=$(git rev-parse --verify "$bisected_head") || die "$(eval_gettext "Bad rev input: \$bisected_head")" - bisect_write "$state" "$rev" + 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 @@ -276,7 +259,7 @@ bisect_state() { done for rev in $hash_list do - bisect_write "$state" "$rev" + git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit done git bisect--helper --check-expected-revs $hash_list ;; *,"$TERM_BAD") @@ -413,7 +396,7 @@ bisect_replay () { cmd="bisect_start $rev" eval "$cmd" ;; "$TERM_GOOD"|"$TERM_BAD"|skip) - bisect_write "$command" "$rev" ;; + git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) bisect_terms $rev ;; *) -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v14 09/27] bisect--helper: `bisect_write` shell function in C 2016-08-23 11:53 ` [PATCH v14 09/27] bisect--helper: `bisect_write` " Pranit Bauva @ 2016-08-24 22:30 ` Junio C Hamano 2016-08-27 9:33 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-24 22:30 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +struct bisect_terms { > + struct strbuf term_good; > + struct strbuf term_bad; > +}; I think "struct strbuf" is overrated. For things like this, where these fields will never change once it is set (and setting it is done atomically, not incrementally), there is no good reason to use define the fields as strbuf. Only because you chose to use strbuf for these two fields, you have to make unnecessarily copies of argv[] in the command parser, and you have to remember to discard these copies later. I think you can just say "const char *" in this case. > +static int bisect_write(const char *state, const char *rev, > + const struct bisect_terms *terms, int nolog) > +{ > + struct strbuf tag = STRBUF_INIT; > + struct strbuf commit_name = STRBUF_INIT; > + struct object_id oid; > + struct commit *commit; > + struct pretty_print_context pp = {0}; > + FILE *fp; > + > + if (!strcmp(state, terms->term_bad.buf)) > + strbuf_addf(&tag, "refs/bisect/%s", state); > + else if (one_of(state, terms->term_good.buf, "skip", NULL)) > + strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev); > + else > + return error(_("Bad bisect_write argument: %s"), state); OK. > + if (get_oid(rev, &oid)) { > + strbuf_release(&tag); > + return error(_("couldn't get the oid of the rev '%s'"), rev); > + } > + > + if (update_ref(NULL, tag.buf, oid.hash, NULL, 0, > + UPDATE_REFS_MSG_ON_ERR)) { > + strbuf_release(&tag); > + return -1; > + } > + strbuf_release(&tag); > + > + fp = fopen(git_path_bisect_log(), "a"); > + if (!fp) > + return error_errno(_("couldn't open the file '%s'"), git_path_bisect_log()); > + > + commit = lookup_commit_reference(oid.hash); > + format_commit_message(commit, "%s", &commit_name, &pp); > + fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash), > + commit_name.buf); > + strbuf_release(&commit_name); > + > + if (!nolog) > + fprintf(fp, "git bisect %s %s\n", state, rev); > + > + fclose(fp); > + return 0; You seem to be _release()ing tag all over the place. Would it make sense to have a single clean-up label at the end of function, introduce a "int retval" variable and set it to -1 (or whatever) when an error is detected and "goto" to the label? It may make it harder to make such a leak. That is, to end the function more like: finish: if (fp) fclose(fp); strbuf_release(&tag); strbuf_release(&commit_name); return retval; } and have sites with potential errors do something like this: if (update_ref(...)) { retval = -1; goto finish; } > + struct bisect_terms terms; > + bisect_terms_init(&terms); With the type of "struct bisect_terms" members corrected, you do not even need the _init() function. > @@ -182,24 +251,38 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) > usage_with_options(git_bisect_helper_usage, options); > > switch (cmdmode) { > + int nolog; > case NEXT_ALL: > return bisect_next_all(prefix, no_checkout); > case WRITE_TERMS: > if (argc != 2) > die(_("--write-terms requires two arguments")); > - return write_terms(argv[0], argv[1]); > + res = write_terms(argv[0], argv[1]); > + break; > case BISECT_CLEAN_STATE: > if (argc != 0) > die(_("--bisect-clean-state requires no arguments")); > - return bisect_clean_state(); > + res = bisect_clean_state(); > + break; > case BISECT_RESET: > if (argc > 1) > die(_("--bisect-reset requires either zero or one arguments")); > - return bisect_reset(argc ? argv[0] : NULL); > + res = bisect_reset(argc ? argv[0] : NULL); > + break; > case CHECK_EXPECTED_REVS: > - return check_expected_revs(argv, argc); > + res = check_expected_revs(argv, argc); > + break; > + case BISECT_WRITE: > + if (argc != 4 && argc != 5) > + die(_("--bisect-write requires either 4 or 5 arguments")); > + nolog = (argc == 5) && !strcmp(argv[4], "nolog"); > + strbuf_addstr(&terms.term_good, argv[2]); > + strbuf_addstr(&terms.term_bad, argv[3]); Here, terms.term_good = argv[2]; terms.term_bad = argv[3]; and then you do not need bisect_terms_release() at all. > + res = bisect_write(argv[0], argv[1], &terms, nolog); > + break; > default: > die("BUG: unknown subcommand '%d'", cmdmode); > } > - return 0; > + bisect_terms_release(&terms); > + return res; > } ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 09/27] bisect--helper: `bisect_write` shell function in C 2016-08-24 22:30 ` Junio C Hamano @ 2016-08-27 9:33 ` Pranit Bauva 2016-08-27 21:22 ` Junio C Hamano 0 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-27 9:33 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Thu, Aug 25, 2016 at 4:00 AM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +struct bisect_terms { >> + struct strbuf term_good; >> + struct strbuf term_bad; >> +}; > > I think "struct strbuf" is overrated. For things like this, where > these fields will never change once it is set (and setting it is > done atomically, not incrementally), there is no good reason to use > define the fields as strbuf. > > Only because you chose to use strbuf for these two fields, you have > to make unnecessarily copies of argv[] in the command parser, and > you have to remember to discard these copies later. > > I think you can just say "const char *" in this case. Using struct strbuf is not really overrated but in fact required. But yes, for this patch it might seem as overrated. In the shell code initally TERM_GOOD is set to "good" while TERM_BAD is set to "bad". Now there are a lot of instances (one of which is bisect_start() function) where this can change. So if we keep it as "const char *", it would be right to change the value of it after wards. And we cannot keep it as "char []" because we don't know its size before hand. >> +static int bisect_write(const char *state, const char *rev, >> + const struct bisect_terms *terms, int nolog) >> +{ >> + struct strbuf tag = STRBUF_INIT; >> + struct strbuf commit_name = STRBUF_INIT; >> + struct object_id oid; >> + struct commit *commit; >> + struct pretty_print_context pp = {0}; >> + FILE *fp; >> + >> + if (!strcmp(state, terms->term_bad.buf)) >> + strbuf_addf(&tag, "refs/bisect/%s", state); >> + else if (one_of(state, terms->term_good.buf, "skip", NULL)) >> + strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev); >> + else >> + return error(_("Bad bisect_write argument: %s"), state); > > OK. > >> + if (get_oid(rev, &oid)) { >> + strbuf_release(&tag); >> + return error(_("couldn't get the oid of the rev '%s'"), rev); >> + } >> + >> + if (update_ref(NULL, tag.buf, oid.hash, NULL, 0, >> + UPDATE_REFS_MSG_ON_ERR)) { >> + strbuf_release(&tag); >> + return -1; >> + } >> + strbuf_release(&tag); >> + >> + fp = fopen(git_path_bisect_log(), "a"); >> + if (!fp) >> + return error_errno(_("couldn't open the file '%s'"), git_path_bisect_log()); >> + >> + commit = lookup_commit_reference(oid.hash); >> + format_commit_message(commit, "%s", &commit_name, &pp); >> + fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash), >> + commit_name.buf); >> + strbuf_release(&commit_name); >> + >> + if (!nolog) >> + fprintf(fp, "git bisect %s %s\n", state, rev); >> + >> + fclose(fp); >> + return 0; > > You seem to be _release()ing tag all over the place. > > Would it make sense to have a single clean-up label at the end of > function, introduce a "int retval" variable and set it to -1 (or > whatever) when an error is detected and "goto" to the label? It may > make it harder to make such a leak. That is, to end the function > more like: I think I could use goto for this function. > finish: > if (fp) > fclose(fp); > strbuf_release(&tag); > strbuf_release(&commit_name); > return retval; > } > > and have sites with potential errors do something like this: > > if (update_ref(...)) { > retval = -1; > goto finish; > } > >> + struct bisect_terms terms; >> + bisect_terms_init(&terms); > > With the type of "struct bisect_terms" members corrected, you do not > even need the _init() function. Discussed above. >> @@ -182,24 +251,38 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) >> usage_with_options(git_bisect_helper_usage, options); >> >> switch (cmdmode) { >> + int nolog; >> case NEXT_ALL: >> return bisect_next_all(prefix, no_checkout); >> case WRITE_TERMS: >> if (argc != 2) >> die(_("--write-terms requires two arguments")); >> - return write_terms(argv[0], argv[1]); >> + res = write_terms(argv[0], argv[1]); >> + break; >> case BISECT_CLEAN_STATE: >> if (argc != 0) >> die(_("--bisect-clean-state requires no arguments")); >> - return bisect_clean_state(); >> + res = bisect_clean_state(); >> + break; >> case BISECT_RESET: >> if (argc > 1) >> die(_("--bisect-reset requires either zero or one arguments")); >> - return bisect_reset(argc ? argv[0] : NULL); >> + res = bisect_reset(argc ? argv[0] : NULL); >> + break; >> case CHECK_EXPECTED_REVS: >> - return check_expected_revs(argv, argc); >> + res = check_expected_revs(argv, argc); >> + break; >> + case BISECT_WRITE: >> + if (argc != 4 && argc != 5) >> + die(_("--bisect-write requires either 4 or 5 arguments")); >> + nolog = (argc == 5) && !strcmp(argv[4], "nolog"); >> + strbuf_addstr(&terms.term_good, argv[2]); >> + strbuf_addstr(&terms.term_bad, argv[3]); > > Here, > > terms.term_good = argv[2]; > terms.term_bad = argv[3]; > > and then you do not need bisect_terms_release() at all. Discussed above. >> + res = bisect_write(argv[0], argv[1], &terms, nolog); >> + break; >> default: >> die("BUG: unknown subcommand '%d'", cmdmode); >> } >> - return 0; >> + bisect_terms_release(&terms); >> + return res; >> } ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 09/27] bisect--helper: `bisect_write` shell function in C 2016-08-27 9:33 ` Pranit Bauva @ 2016-08-27 21:22 ` Junio C Hamano 2016-08-30 6:42 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-27 21:22 UTC (permalink / raw) To: Pranit Bauva; +Cc: Git List Pranit Bauva <pranit.bauva@gmail.com> writes: >>> +struct bisect_terms { >>> + struct strbuf term_good; >>> + struct strbuf term_bad; >>> +}; >> >> I think "struct strbuf" is overrated. ... >> I think you can just say "const char *" in this case. > > Using struct strbuf is not really overrated but in fact required. But Nothing is required. I can make your life easier by requiring you to never use struct strbuf as a structure field type, though. You will almost never need it unless you are building something exotic anyway. Step back and think what "strbuf" is good for. It holds a pointer to a piece of memory the field owns, over-allocates and knows the size of allocation and usage. That is good if you need to (1) frequently find out the length of the string; without a separate .len member you would have to run strlen(). (2) incrementally add to the string in-place; as it overallocates, appending to the string would not have to involve realloc() every time and the cost of it is amortized. (3) make complex operations like splicing another string in, trimming substring out, etc. You need to do none of the above to these fields. term.term_good is either taken from an argv[] element, or you read from a line from a file and set it. You may do some trivial computation and set it to the result, like "the other field uses 'old', so this one need to be set to 'new'". The user of the field either has the string and sets it there, or reads the field's value as a whole string. No string manipulation in the field in-place is needed. > yes, for this patch it might seem as overrated. In the shell code > initally TERM_GOOD is set to "good" while TERM_BAD is set to "bad". > Now there are a lot of instances (one of which is bisect_start() > function) where this can change. So if we keep it as "const char *", > it would be right to change the value of it after wards. And we cannot > keep it as "char []" because we don't know its size before hand. You are not making sense. Nobody would suggest to use a fixed-size char array in this structure. That wouldn't even work in the case where you are stuffing what comes in an argv[] element in there, e.g. terms.term_good = argv[3]; And you can of course support changing the value of the field without using strbuf. Just update the pointer to point at the piece of memory that holds the new value. In short, I do not see any good reason why the term_good field has to be anything other than "char *term_good" or "const char *term_good". Now, what you need to consider choosing between the two depends on where these strings can come from. If they are known to be always unchanging between the time you set it til the end of the program (e.g. using an element in argv[]), you can just assign without making any copy and you can use "const char *term_good". All other cases, the structure needs to take the ownership, and you would need to make a copy if you don't have the ownership, e.g. terms.term_good = xstrdup(argv[3]); You may be reading from a file, a line at a time and you may have a line's content in a strbuf. You do not (yet) own the buffer after reading it, e.g. strbuf_getline(&buf, fp); terms.term_good = strbuf_detach(&buf, NULL); Of course, if you need to take ownership of the memory, you would need to free(3) it as needed, which means the pattern to set the field would become free(terms.term_good); terms.term_good = ... some new value ...; Using strbuf as a local variable is good. It gives a higher level of abstraction when you are actually performing string operations. In most applications, however, a field in a struct is where the result of a step of computation is kept, not a scratch-pad to perform steps of computation in. When you are ready to update the value of a field, you _have_ a completed string, and you can just use "char *" field to point at it. There is no need for strbuf in the field. Don't look at the data structure used in trailer.[ch] as a model; it is an example of a terribly bad implementation taste, a pattern that should not be followed. Print it, not read it and burn it as a good symbolic gesture. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 09/27] bisect--helper: `bisect_write` shell function in C 2016-08-27 21:22 ` Junio C Hamano @ 2016-08-30 6:42 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-30 6:42 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Sun, Aug 28, 2016 at 2:52 AM, Junio C Hamano <gitster@pobox.com> wrote: > > Pranit Bauva <pranit.bauva@gmail.com> writes: > > >>> +struct bisect_terms { > >>> + struct strbuf term_good; > >>> + struct strbuf term_bad; > >>> +}; > >> > >> I think "struct strbuf" is overrated. ... > >> I think you can just say "const char *" in this case. > > > > Using struct strbuf is not really overrated but in fact required. But > > Nothing is required. > > I can make your life easier by requiring you to never use struct > strbuf as a structure field type, though. You will almost never > need it unless you are building something exotic anyway. > > Step back and think what "strbuf" is good for. It holds a pointer > to a piece of memory the field owns, over-allocates and knows the > size of allocation and usage. That is good if you need to > > (1) frequently find out the length of the string; without a > separate .len member you would have to run strlen(). > > (2) incrementally add to the string in-place; as it overallocates, > appending to the string would not have to involve realloc() > every time and the cost of it is amortized. > > (3) make complex operations like splicing another string in, > trimming substring out, etc. > > You need to do none of the above to these fields. term.term_good is > either taken from an argv[] element, or you read from a line from a > file and set it. You may do some trivial computation and set it to > the result, like "the other field uses 'old', so this one need to be > set to 'new'". The user of the field either has the string and sets > it there, or reads the field's value as a whole string. No string > manipulation in the field in-place is needed. > > > yes, for this patch it might seem as overrated. In the shell code > > initally TERM_GOOD is set to "good" while TERM_BAD is set to "bad". > > Now there are a lot of instances (one of which is bisect_start() > > function) where this can change. So if we keep it as "const char *", > > it would be right to change the value of it after wards. And we cannot > > keep it as "char []" because we don't know its size before hand. > > You are not making sense. Nobody would suggest to use a fixed-size > char array in this structure. That wouldn't even work in the case > where you are stuffing what comes in an argv[] element in there, > e.g. > > terms.term_good = argv[3]; > > And you can of course support changing the value of the field > without using strbuf. Just update the pointer to point at the piece > of memory that holds the new value. > > In short, I do not see any good reason why the term_good field has > to be anything other than "char *term_good" or "const char *term_good". > > Now, what you need to consider choosing between the two depends on > where these strings can come from. If they are known to be always > unchanging between the time you set it til the end of the program > (e.g. using an element in argv[]), you can just assign without > making any copy and you can use "const char *term_good". All other > cases, the structure needs to take the ownership, and you would need > to make a copy if you don't have the ownership, e.g. > > terms.term_good = xstrdup(argv[3]); > > You may be reading from a file, a line at a time and you may have a > line's content in a strbuf. You do not (yet) own the buffer after > reading it, e.g. > > strbuf_getline(&buf, fp); > terms.term_good = strbuf_detach(&buf, NULL); > > Of course, if you need to take ownership of the memory, you would > need to free(3) it as needed, which means the pattern to set the > field would become > > free(terms.term_good); > terms.term_good = ... some new value ...; > > Using strbuf as a local variable is good. It gives a higher level > of abstraction when you are actually performing string operations. > In most applications, however, a field in a struct is where the > result of a step of computation is kept, not a scratch-pad to > perform steps of computation in. When you are ready to update the > value of a field, you _have_ a completed string, and you can just > use "char *" field to point at it. There is no need for strbuf in > the field. > > Don't look at the data structure used in trailer.[ch] as a model; it > is an example of a terribly bad implementation taste, a pattern that > should not be followed. Print it, not read it and burn it as a good > symbolic gesture. Thanks for explaining when to use strbuf. I am convinced that the thing I am aiming for can be done with the help of "const char *". Though I will have to use strbuf in get_terms() and detach the string buffer from there as you have mentioned previously. Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v14 06/27] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (10 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 09/27] bisect--helper: `bisect_write` " Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 11/27] bisect--helper: `bisect_next_check` & bisect_voc shell function in C Pranit Bauva ` (16 subsequent siblings) 28 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git is_empty_file() can help to refactor a lot of code. This will be very helpful in porting "git bisect" to C. Suggested-by: Torsten Bögershausen <tboegi@web.de> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/am.c | 20 ++------------------ cache.h | 3 +++ wrapper.c | 13 +++++++++++++ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 739b34d..9e1e9d6 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -30,22 +30,6 @@ #include "mailinfo.h" /** - * Returns 1 if the file is empty or does not exist, 0 otherwise. - */ -static int is_empty_file(const char *filename) -{ - struct stat st; - - if (stat(filename, &st) < 0) { - if (errno == ENOENT) - return 1; - die_errno(_("could not stat %s"), filename); - } - - return !st.st_size; -} - -/** * Returns the length of the first line of msg. */ static int linelen(const char *msg) @@ -1324,7 +1308,7 @@ static int parse_mail(struct am_state *state, const char *mail) goto finish; } - if (is_empty_file(am_path(state, "patch"))) { + if (is_empty_or_missing_file(am_path(state, "patch"))) { printf_ln(_("Patch is empty. Was it split wrong?")); die_user_resolve(state); } @@ -1896,7 +1880,7 @@ static void am_run(struct am_state *state, int resume) resume = 0; } - if (!is_empty_file(am_path(state, "rewritten"))) { + if (!is_empty_or_missing_file(am_path(state, "rewritten"))) { assert(state->rebasing); copy_notes_for_rebase(state); run_post_rewrite_hook(state); diff --git a/cache.h b/cache.h index b780a91..49f214b 100644 --- a/cache.h +++ b/cache.h @@ -1916,4 +1916,7 @@ void sleep_millisec(int millisec); */ void safe_create_dir(const char *dir, int share); +/* Return 1 if the file is empty or does not exists, 0 otherwise. */ +extern int is_empty_or_missing_file(const char *filename); + #endif /* CACHE_H */ diff --git a/wrapper.c b/wrapper.c index e7f1979..78f6431 100644 --- a/wrapper.c +++ b/wrapper.c @@ -679,3 +679,16 @@ void sleep_millisec(int millisec) { poll(NULL, 0, millisec); } + +int is_empty_or_missing_file(const char *filename) +{ + struct stat st; + + if (stat(filename, &st) < 0) { + if (errno == ENOENT) + return 1; + die_errno(_("could not stat %s"), filename); + } + + return !st.st_size; +} -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* [PATCH v14 11/27] bisect--helper: `bisect_next_check` & bisect_voc shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (11 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 06/27] wrapper: move is_empty_file() and rename it as is_empty_or_missing_file() Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-24 22:40 ` Junio C Hamano 2016-08-23 11:53 ` [PATCH v14 17/27] bisect--helper: `bisect_autostart` " Pranit Bauva ` (15 subsequent siblings) 28 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement `bisect_next_check` shell function in C and add `bisect-next-check` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also reimplement `bisect_voc` shell function in C and call it from `bisect_next_check` implementation in C. Using `--bisect-next-check` is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 60 ++------------------------- 2 files changed, 106 insertions(+), 57 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 450426c..aca3908 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -6,6 +6,7 @@ #include "dir.h" #include "argv-array.h" #include "run-command.h" +#include "prompt.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -21,6 +22,7 @@ static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --bisect-reset [<commit>]"), N_("git bisect--helper --bisect-write <state> <revision> <TERM_GOOD> <TERM_BAD> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <TERM_GOOD> <TERM_BAD>"), + N_("git bisect--helper --bisect-next-check [<term>] <TERM_GOOD> <TERM_BAD"), NULL }; @@ -253,6 +255,95 @@ static int check_and_set_terms(struct bisect_terms *terms, const char *cmd) return 0; } +static int mark_good(const char *refname, const struct object_id *oid, + int flag, void *cb_data) +{ + int *m_good = (int *)cb_data; + *m_good = 0; + return 1; +} + +static char *bisect_voc(char *revision_type) +{ + if (!strcmp(revision_type, "bad")) + return "bad|new"; + if (!strcmp(revision_type, "good")) + return "good|old"; + + return NULL; +} + +static int bisect_next_check(const struct bisect_terms *terms, + const char *current_term) +{ + int missing_good = 1, missing_bad = 1; + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); + char *good_glob = xstrfmt("%s-*", terms->term_good.buf); + char *bad_syn, *good_syn; + + if (ref_exists(bad_ref)) + missing_bad = 0; + free(bad_ref); + + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", + (void *) &missing_good); + free(good_glob); + + if (!missing_good && !missing_bad) + return 0; + + if (!current_term) + return -1; + + if (missing_good && !missing_bad && current_term && + !strcmp(current_term, terms->term_good.buf)) { + char *yesno; + /* + * have bad (or new) but not good (or old). We could bisect + * although this is less optimum. + */ + fprintf(stderr, _("Warning: bisecting only with a %s commit\n"), + terms->term_bad.buf); + if (!isatty(0)) + return 0; + /* + * TRANSLATORS: Make sure to include [Y] and [n] in your + * translation. The program will only accept English input + * at this point. + */ + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); + if (starts_with(yesno, "N") || starts_with(yesno, "n")) + return -1; + + return 0; + } + bad_syn = xstrdup(bisect_voc("bad")); + good_syn = xstrdup(bisect_voc("good")); + if (!is_empty_or_missing_file(git_path_bisect_start())) { + error(_("You need to give me at least one %s and " + "%s revision. You can use \"git bisect %s\" " + "and \"git bisect %s\" for that. \n"), + bad_syn, good_syn, bad_syn, good_syn); + free(bad_syn); + free(good_syn); + return -1; + } + else { + error(_("You need to start by \"git bisect start\". You " + "then need to give me at least one %s and %s " + "revision. You can use \"git bisect %s\" and " + "\"git bisect %s\" for that.\n"), + good_syn, bad_syn, bad_syn, good_syn); + free(bad_syn); + free(good_syn); + return -1; + } + free(bad_syn); + free(good_syn); + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -262,7 +353,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_RESET, CHECK_EXPECTED_REVS, BISECT_WRITE, - CHECK_AND_SET_TERMS + CHECK_AND_SET_TERMS, + BISECT_NEXT_CHECK } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -280,6 +372,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE), OPT_CMDMODE(0, "check-and-set-terms", &cmdmode, N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), + OPT_CMDMODE(0, "bisect-next-check", &cmdmode, + N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -330,6 +424,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) strbuf_addstr(&terms.term_bad, argv[2]); res = check_and_set_terms(&terms, argv[0]); break; + case BISECT_NEXT_CHECK: + if (argc != 2 && argc != 3) + die(_("--bisect-next-check requires 2 or 3 arguments")); + strbuf_addstr(&terms.term_good, argv[0]); + strbuf_addstr(&terms.term_bad, argv[1]); + res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index bdf2227..fe6c9d0 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -271,59 +271,14 @@ bisect_state() { bisect_auto_next } -bisect_next_check() { - missing_good= missing_bad= - git show-ref -q --verify refs/bisect/$TERM_BAD || missing_bad=t - test -n "$(git for-each-ref "refs/bisect/$TERM_GOOD-*")" || missing_good=t - - case "$missing_good,$missing_bad,$1" in - ,,*) - : have both $TERM_GOOD and $TERM_BAD - ok - ;; - *,) - # do not have both but not asked to fail - just report. - false - ;; - t,,"$TERM_GOOD") - # have bad (or new) but not good (or old). we could bisect although - # this is less optimum. - eval_gettextln "Warning: bisecting only with a \$TERM_BAD commit." >&2 - if test -t 0 - then - # TRANSLATORS: Make sure to include [Y] and [n] in your - # translation. The program will only accept English input - # at this point. - gettext "Are you sure [Y/n]? " >&2 - read yesno - case "$yesno" in [Nn]*) exit 1 ;; esac - fi - : bisect without $TERM_GOOD... - ;; - *) - bad_syn=$(bisect_voc bad) - good_syn=$(bisect_voc good) - if test -s "$GIT_DIR/BISECT_START" - then - - eval_gettextln "You need to give me at least one \$bad_syn and one \$good_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - else - eval_gettextln "You need to start by \"git bisect start\". -You then need to give me at least one \$good_syn and one \$bad_syn revision. -(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2 - fi - exit 1 ;; - esac -} - bisect_auto_next() { - bisect_next_check && bisect_next || : + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || : } bisect_next() { case "$#" in 0) ;; *) usage ;; esac bisect_autostart - bisect_next_check $TERM_GOOD + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit # Perform all bisection computation, display and checkout git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout) @@ -355,7 +310,7 @@ bisect_next() { } bisect_visualize() { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit if test $# = 0 then @@ -409,7 +364,7 @@ bisect_replay () { } bisect_run () { - bisect_next_check fail + git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit while true do @@ -482,13 +437,6 @@ get_terms () { fi } -bisect_voc () { - case "$1" in - bad) echo "bad|new" ;; - good) echo "good|old" ;; - esac -} - bisect_terms () { get_terms if ! test -s "$GIT_DIR/BISECT_TERMS" -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v14 11/27] bisect--helper: `bisect_next_check` & bisect_voc shell function in C 2016-08-23 11:53 ` [PATCH v14 11/27] bisect--helper: `bisect_next_check` & bisect_voc shell function in C Pranit Bauva @ 2016-08-24 22:40 ` Junio C Hamano 2016-08-27 9:35 ` Pranit Bauva 0 siblings, 1 reply; 320+ messages in thread From: Junio C Hamano @ 2016-08-24 22:40 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > +static int mark_good(const char *refname, const struct object_id *oid, > + int flag, void *cb_data) > +{ > + int *m_good = (int *)cb_data; > + *m_good = 0; > + return 1; > +} > + > +static char *bisect_voc(char *revision_type) > +{ > + if (!strcmp(revision_type, "bad")) > + return "bad|new"; > + if (!strcmp(revision_type, "good")) > + return "good|old"; > + > + return NULL; > +} > + > +static int bisect_next_check(const struct bisect_terms *terms, > + const char *current_term) > +{ > + int missing_good = 1, missing_bad = 1; > + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); > + char *good_glob = xstrfmt("%s-*", terms->term_good.buf); > + char *bad_syn, *good_syn; > + > + if (ref_exists(bad_ref)) > + missing_bad = 0; > + free(bad_ref); > + > + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", > + (void *) &missing_good); > + free(good_glob); > + > + if (!missing_good && !missing_bad) > + return 0; > + > + if (!current_term) > + return -1; > + > + if (missing_good && !missing_bad && current_term && > + !strcmp(current_term, terms->term_good.buf)) { > + char *yesno; > + /* > + * have bad (or new) but not good (or old). We could bisect > + * although this is less optimum. > + */ > + fprintf(stderr, _("Warning: bisecting only with a %s commit\n"), > + terms->term_bad.buf); > + if (!isatty(0)) > + return 0; > + /* > + * TRANSLATORS: Make sure to include [Y] and [n] in your > + * translation. The program will only accept English input > + * at this point. > + */ > + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); > + if (starts_with(yesno, "N") || starts_with(yesno, "n")) > + return -1; > + > + return 0; > + } > + bad_syn = xstrdup(bisect_voc("bad")); > + good_syn = xstrdup(bisect_voc("good")); > + if (!is_empty_or_missing_file(git_path_bisect_start())) { > + error(_("You need to give me at least one %s and " > + "%s revision. You can use \"git bisect %s\" " > + "and \"git bisect %s\" for that. \n"), > + bad_syn, good_syn, bad_syn, good_syn); > + free(bad_syn); > + free(good_syn); > + return -1; > + } > + else { > + error(_("You need to start by \"git bisect start\". You " > + "then need to give me at least one %s and %s " > + "revision. You can use \"git bisect %s\" and " > + "\"git bisect %s\" for that.\n"), > + good_syn, bad_syn, bad_syn, good_syn); > + free(bad_syn); > + free(good_syn); > + return -1; > + } > + free(bad_syn); > + free(good_syn); > + > + return 0; This one looks OK, but I think the same "Wouldn't it become cleaner to have a 'finish:' label at the end and jump there?" comment applies to this implementation, too. ^ permalink raw reply [flat|nested] 320+ messages in thread
* Re: [PATCH v14 11/27] bisect--helper: `bisect_next_check` & bisect_voc shell function in C 2016-08-24 22:40 ` Junio C Hamano @ 2016-08-27 9:35 ` Pranit Bauva 0 siblings, 0 replies; 320+ messages in thread From: Pranit Bauva @ 2016-08-27 9:35 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git List Hey Junio, On Thu, Aug 25, 2016 at 4:10 AM, Junio C Hamano <gitster@pobox.com> wrote: > Pranit Bauva <pranit.bauva@gmail.com> writes: > >> +static int mark_good(const char *refname, const struct object_id *oid, >> + int flag, void *cb_data) >> +{ >> + int *m_good = (int *)cb_data; >> + *m_good = 0; >> + return 1; >> +} >> + >> +static char *bisect_voc(char *revision_type) >> +{ >> + if (!strcmp(revision_type, "bad")) >> + return "bad|new"; >> + if (!strcmp(revision_type, "good")) >> + return "good|old"; >> + >> + return NULL; >> +} >> + >> +static int bisect_next_check(const struct bisect_terms *terms, >> + const char *current_term) >> +{ >> + int missing_good = 1, missing_bad = 1; >> + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad.buf); >> + char *good_glob = xstrfmt("%s-*", terms->term_good.buf); >> + char *bad_syn, *good_syn; >> + >> + if (ref_exists(bad_ref)) >> + missing_bad = 0; >> + free(bad_ref); >> + >> + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/", >> + (void *) &missing_good); >> + free(good_glob); >> + >> + if (!missing_good && !missing_bad) >> + return 0; >> + >> + if (!current_term) >> + return -1; >> + >> + if (missing_good && !missing_bad && current_term && >> + !strcmp(current_term, terms->term_good.buf)) { >> + char *yesno; >> + /* >> + * have bad (or new) but not good (or old). We could bisect >> + * although this is less optimum. >> + */ >> + fprintf(stderr, _("Warning: bisecting only with a %s commit\n"), >> + terms->term_bad.buf); >> + if (!isatty(0)) >> + return 0; >> + /* >> + * TRANSLATORS: Make sure to include [Y] and [n] in your >> + * translation. The program will only accept English input >> + * at this point. >> + */ >> + yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO); >> + if (starts_with(yesno, "N") || starts_with(yesno, "n")) >> + return -1; >> + >> + return 0; >> + } >> + bad_syn = xstrdup(bisect_voc("bad")); >> + good_syn = xstrdup(bisect_voc("good")); >> + if (!is_empty_or_missing_file(git_path_bisect_start())) { >> + error(_("You need to give me at least one %s and " >> + "%s revision. You can use \"git bisect %s\" " >> + "and \"git bisect %s\" for that. \n"), >> + bad_syn, good_syn, bad_syn, good_syn); >> + free(bad_syn); >> + free(good_syn); >> + return -1; >> + } >> + else { >> + error(_("You need to start by \"git bisect start\". You " >> + "then need to give me at least one %s and %s " >> + "revision. You can use \"git bisect %s\" and " >> + "\"git bisect %s\" for that.\n"), >> + good_syn, bad_syn, bad_syn, good_syn); >> + free(bad_syn); >> + free(good_syn); >> + return -1; >> + } >> + free(bad_syn); >> + free(good_syn); >> + >> + return 0; > > This one looks OK, but I think the same "Wouldn't it become cleaner > to have a 'finish:' label at the end and jump there?" comment > applies to this implementation, too. For this goto can simply things. Will do! Regards, Pranit Bauva ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v14 17/27] bisect--helper: `bisect_autostart` shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (12 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 11/27] bisect--helper: `bisect_next_check` & bisect_voc shell function in C Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-26 21:09 ` Junio C Hamano 2016-08-23 11:53 ` [PATCH v14 04/27] bisect--helper: `bisect_clean_state` " Pranit Bauva ` (14 subsequent siblings) 28 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement the `bisect_autostart` shell function in C and add the C implementation from `bisect_next()` which was previously left uncovered. Also add a subcommand `--bisect-autostart` to `git bisect--helper` be called from `bisect_state()` from git-bisect.sh . Using `--bisect-autostart` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by `bisect_state()`. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- builtin/bisect--helper.c | 40 ++++++++++++++++++++++++++++++++++++++++ git-bisect.sh | 23 +---------------------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 8cbcc3b..a139592 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -30,6 +30,7 @@ static const char * const git_bisect_helper_usage[] = { "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"), N_("git bisect--helper --bisect-next"), N_("git bisect--helper --bisect-auto-next"), + N_("git bisect--helper --bisect-autostart"), NULL }; @@ -38,6 +39,8 @@ struct bisect_terms { struct strbuf term_bad; }; +static int bisect_autostart(struct bisect_terms *terms); + static void bisect_terms_init(struct bisect_terms *terms) { strbuf_init(&terms->term_good, 0); @@ -410,6 +413,7 @@ static int bisect_next(struct bisect_terms *terms, const char *prefix) { int res, no_checkout; + bisect_autostart(terms); /* 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 @@ -760,6 +764,32 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout, return bisect_auto_next(terms, NULL); } +static int bisect_autostart(struct bisect_terms *terms) +{ + if (is_empty_or_missing_file(git_path_bisect_start())) { + const char *yesno; + const char *argv[] = {NULL}; + fprintf(stderr, _("You need to start by \"git bisect " + "start\"\n")); + + if (!isatty(0)) + return 1; + + /* + * TRANSLATORS: Make sure to include [Y] and [n] in your + * translation. THe program will only accept English input + * at this point. + */ + yesno = git_prompt(_("Do you want me to do it for you " + "[Y/n]? "), PROMPT_ECHO); + if (starts_with(yesno, "n") || starts_with(yesno, "N")) + exit(0); + + return bisect_start(terms, 0, argv, 0); + } + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -773,6 +803,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_START, BISECT_NEXT, BISECT_AUTO_NEXT, + BISECT_AUTOSTART, } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -796,6 +827,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 find the next bisection state"), BISECT_AUTO_NEXT), + OPT_CMDMODE(0, "bisect-autostart", &cmdmode, + N_("start the bisection if BISECT_START empty or missing"), BISECT_AUTOSTART), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -869,6 +902,13 @@ 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) + die(_("--bisect-autostart requires 0 arguments")); + strbuf_addstr(&terms.term_good, "good"); + strbuf_addstr(&terms.term_bad, "bad"); + res = bisect_autostart(&terms); + break; default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index d574c44..cd56551 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -49,27 +49,6 @@ bisect_head() fi } -bisect_autostart() { - test -s "$GIT_DIR/BISECT_START" || { - gettextln "You need to start by \"git bisect start\"" >&2 - if test -t 0 - then - # TRANSLATORS: Make sure to include [Y] and [n] in your - # translation. The program will only accept English input - # at this point. - gettext "Do you want me to do it for you [Y/n]? " >&2 - read yesno - case "$yesno" in - [Nn]*) - exit ;; - esac - git bisect--helper --bisect-start - else - exit 1 - fi - } -} - bisect_skip() { all='' for arg in "$@" @@ -86,7 +65,7 @@ bisect_skip() { } bisect_state() { - bisect_autostart + git bisect--helper --bisect-autostart state=$1 get_terms git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v14 17/27] bisect--helper: `bisect_autostart` shell function in C 2016-08-23 11:53 ` [PATCH v14 17/27] bisect--helper: `bisect_autostart` " Pranit Bauva @ 2016-08-26 21:09 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-26 21:09 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > @@ -410,6 +413,7 @@ static int bisect_next(struct bisect_terms *terms, const char *prefix) > { > int res, no_checkout; > > + bisect_autostart(terms); > /* 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 This change is extremely hard to reason about. Why wasn't auto-start done at the very beginning of this function before? Why does this start calling it all of a sudden? Before autostart was rewritten in C in this step, who made sure the shell version of autostart was called before bisect_next is called (which must have become unnecessary with this step, and I fully expected a removal of such a call in the shell version, but I didn't find such a change in this patch)? Or is this series simply broken by lossage of autostart feature in "bisect next" between the step where bisect_next is rewritten in C and this step? ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v14 04/27] bisect--helper: `bisect_clean_state` shell function in C 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (13 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 17/27] bisect--helper: `bisect_autostart` " Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-24 20:58 ` Junio C Hamano 2016-08-23 11:53 ` [PATCH v14 24/27] bisect--helper: retire `--check-and-set-terms` subcommand Pranit Bauva ` (13 subsequent siblings) 28 siblings, 1 reply; 320+ messages in thread From: Pranit Bauva @ 2016-08-23 11:53 UTC (permalink / raw) To: git Reimplement `bisect_clean_state` shell function in C and add a `bisect-clean-state` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-clean-state` subcommand is a measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by bisect_reset() and bisect_start(). Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> --- bisect.c | 43 +++++++++++++++++++++++++++++++++++++++++++ bisect.h | 2 ++ builtin/bisect--helper.c | 14 +++++++++++++- git-bisect.sh | 26 +++----------------------- 4 files changed, 61 insertions(+), 24 deletions(-) diff --git a/bisect.c b/bisect.c index 6f512c2..45d598d 100644 --- a/bisect.c +++ b/bisect.c @@ -430,6 +430,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) { @@ -1040,3 +1046,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(&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; +} diff --git a/bisect.h b/bisect.h index acd12ef..0ae63d4 100644 --- a/bisect.h +++ b/bisect.h @@ -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 diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 30e1031..e50934c 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -5,10 +5,15 @@ #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 GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG") +static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") 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 }; @@ -83,7 +88,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, - WRITE_TERMS + WRITE_TERMS, + BISECT_CLEAN_STATE } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -91,6 +97,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) 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_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -109,6 +117,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 2) die(_("--write-terms requires two arguments")); return write_terms(argv[0], argv[1]); + case BISECT_CLEAN_STATE: + if (argc != 0) + die(_("--bisect-clean-state requires no arguments")); + return bisect_clean_state(); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index 9ef6cb8..f1202df 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -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 # @@ -430,27 +430,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 () { -- https://github.com/git/git/pull/287 ^ permalink raw reply related [flat|nested] 320+ messages in thread
* Re: [PATCH v14 04/27] bisect--helper: `bisect_clean_state` shell function in C 2016-08-23 11:53 ` [PATCH v14 04/27] bisect--helper: `bisect_clean_state` " Pranit Bauva @ 2016-08-24 20:58 ` Junio C Hamano 0 siblings, 0 replies; 320+ messages in thread From: Junio C Hamano @ 2016-08-24 20:58 UTC (permalink / raw) To: Pranit Bauva; +Cc: git Pranit Bauva <pranit.bauva@gmail.com> writes: > Reimplement `bisect_clean_state` shell function in C and add a > `bisect-clean-state` subcommand to `git bisect--helper` to call it from > git-bisect.sh . > > Using `--bisect-clean-state` subcommand is a measure to port shell > function to C so as to use the existing test suite. As more functions > are ported, this subcommand will be retired but its implementation will > be called by bisect_reset() and bisect_start(). > > Mentored-by: Lars Schneider <larsxschneider@gmail.com> > Mentored-by: Christian Couder <chriscool@tuxfamily.org> > Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> > --- This seems to be where this round diverges from the previous one. This patch in this round has more stuff that used to be in builtin/bisect--helper.c in the previous one in bisect.c. Because I am not sure if the distinction would make that much of a difference (after all, I do not think of a good reason why many bisect internals need to be exposed to anything other than the eventual builtin/bisect.c that retires git-bisect.sh), I am OK with the change to this patch between the previous round and this round. ^ permalink raw reply [flat|nested] 320+ messages in thread
* [PATCH v14 24/27] bisect--helper: retire `--check-and-set-terms` subcommand 2016-08-23 11:53 ` [PATCH v14 01/27] " Pranit Bauva ` (14 preceding siblings ...) 2016-08-23 11:53 ` [PATCH v14 04/27] bisect--helper: `bisect_clean_state` " Pranit Bauva @ 2016-08-23 11:53 ` Pranit Bauva 2016-08-23 11:53 ` [PATCH v14 08/27] bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C Pranit Bauva ` (12 subsequent siblings)