bisect--helper: `check_and_set_terms` shell function in C
PATCH 1/3 bisect--helper: `check_and_set_terms` shell function in C
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_reinit() to release the previous memory of struct bisect_terms and allocate new memory for it thus flushing out the previous contents.
---
This series applies on top of my previous patch series[1].
[1]: http://thread.gmane.org/gmane.comp.version-control.git/298263
builtin/bisect--helper.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 36 ++++---------------------------- 2 files changed, 56 insertions(+), 33 deletions(-)
builtin/bisect--helper.c +52 -1
@@ -45,6 +45,13 @@ static int bisect_terms_release(struct bisect_terms *term)return 0 ; } +static int bisect_terms_reinit (struct bisect_terms *term) +{ +bisect_terms_release (term); +bisect_terms_init (term); +return 0 ; +} +/* * Check whether the string `term` belongs to the set of strings * included in the variable arguments.
@@ -254,6 +261,40 @@ static int bisect_write(const char *state, const char *rev,return 0 ; } +static int check_and_set_terms (const char *cmd,struct bisect_terms *term) +{ +if (one_of (cmd,"skip" ,"start" ,"terms" ,NULL )) +return 0 ; + +if (!is_empty_or_missing_file (git_path_bisect_write_terms ()) && +strcmp (cmd, term->term_bad.buf) &&strcmp (cmd, term->term_good.buf)) +return error (_ ("Invalid command: you're currently in a" +"'%s' '%s' bisect" ), term->term_bad.buf, + term->term_good.buf); + +if (one_of (cmd,"bad" ,"good" ,NULL )) { +if (is_empty_or_missing_file (git_path_bisect_write_terms ())) { +bisect_terms_reinit (term); +strbuf_addstr (&term->term_bad,"bad" ); +strbuf_addstr (&term->term_good,"good" ); +return write_terms (term->term_bad.buf, + term->term_good.buf); + } + } + +if (one_of (cmd,"new" ,"old" ,NULL )) { +if (is_empty_or_missing_file (git_path_bisect_write_terms ())) { +bisect_terms_reinit (term); +strbuf_addstr (&term->term_bad,"new" ); +strbuf_addstr (&term->term_good,"old" ); +return write_terms (term->term_bad.buf, + term->term_good.buf); + } + } + +return 0 ; +} +int cmd_bisect__helper (int argc,const char **argv,const char *prefix) {enum {
@@ -262,7 +303,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[] = {
@@ -278,6 +320,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 ()
@@ -321,6 +365,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)strbuf_addstr (&state.term_bad, argv[3 ]); res =bisect_write (argv[0 ], argv[1 ], &state, nolog);break ; +case CHECK_AND_SET_TERMS : +if (argc !=3 ) +die (_ ("--check-and-set-terms requires 3 arguments" )); +strbuf_addstr (&state.term_good, argv[1 ]); +strbuf_addstr (&state.term_bad, argv[2 ]); + res =check_and_set_terms (argv[0 ], &state); +break ;default :die ("BUG: unknown subcommand '%d'" , cmdmode); }
git-bisect.sh +4 -32
@@ -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 + 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" ;;
Signature · 1 line
2.9.0
PATCH 2/3 bisect--helper: `bisect_next_check` shell function in C
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().
--- builtin/bisect--helper.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 60 +++--------------------------------- 2 files changed, 83 insertions(+), 57 deletions(-)
builtin/bisect--helper.c +79 -1
@@ -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_write_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
@@ -23,6 +24,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-next-check [<term>] <TERM_GOOD> <TERM_BAD" ), NULL };
@@ -295,6 +297,68 @@ static int check_and_set_terms(const char *cmd, struct bisect_terms *term)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 *state,const char *term) +{ +int missing_good =1 , missing_bad =1 ; +char *bad_ref =xstrfmt ("refs/bisect/%s" , state->term_bad.buf); +char *good_ref =xstrfmt ("%s*" , state->term_good.buf); +if (ref_exists (bad_ref)) + missing_bad =0 ; + +for_each_glob_ref_in (mark_good, good_ref,"refs/bisect/" , + (void *) &missing_good); +free (good_ref); +free (bad_ref); + +if (!missing_good && !missing_bad) +return 0 ; + +if (!term) +return -1 ; + +if (missing_good && !missing_bad && term && !strcmp (term, state->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" , + state->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 {
@@ -304,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[] = {
@@ -322,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 ()
@@ -372,6 +439,17 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)strbuf_addstr (&state.term_bad, argv[2 ]); res =check_and_set_terms (argv[0 ], &state);break ; +case BISECT_NEXT_CHECK : +if (argc !=2 && argc !=3 ) +die (_ ("--bisect-next-check requires 2 or 3 arguments" )); +strbuf_addstr (&state.term_good, argv[0 ]); +strbuf_addstr (&state.term_bad, argv[1 ]); +if (argc ==2 ) { + res =bisect_next_check (&state,NULL ); +break ; + } + res =bisect_next_check (&state, argv[2 ]); +break ;default :die ("BUG: unknown subcommand '%d'" , cmdmode); }
git-bisect.sh +4 -56
90 lines not on this page · raw message
Signature · 1 line
2.9.0
PATCH 3/3 bisect--helper: `get_terms` & `bisect_terms` shell function in C
In reply to Pranit BauvaReimplement 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 and will be called by some other methods.
--- builtin/bisect--helper.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 35 ++-------------------------------- 2 files changed, 50 insertions(+), 34 deletions(-)
builtin/bisect--helper.c +48 -1
@@ -359,6 +359,41 @@ static int bisect_next_check(const struct bisect_terms *state, const char *term)return 0 ; } +static int get_terms (struct bisect_terms *term) +{ +FILE *fp; + fp =fopen (git_path_bisect_write_terms (),"r" ); +if (!fp) +return -1 ; + +bisect_terms_reinit (term); +if (strbuf_getline (&term->term_bad, fp) ==EOF ) +return -1 ; +if (strbuf_getline (&term->term_good, fp) ==EOF ) +return -1 ; +return 0 ; +} + +static int bisect_terms (struct bisect_terms *term,const char *arg) +{ +if (get_terms (term)) { +fprintf (stderr,"no terms defined\n" ); +return -1 ; + } +if (!arg) { +printf ("Your current terms are %s for the old state\nand " +"%s for the new state.\n" , term->term_good.buf, + term->term_bad.buf); +return 0 ; + } +if (one_of (arg,"--term-good" ,"--term-old" ,NULL )) +printf ("%s\n" , term->term_good.buf); +if (one_of (arg,"--term-bad" ,"--term-new" ,NULL )) +printf ("%s\n" , term->term_bad.buf); + +return 0 ; +} +int cmd_bisect__helper (int argc,const char **argv,const char *prefix) {enum {
@@ -369,7 +404,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[] = {
@@ -389,6 +425,12 @@ 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_ARGUMENT ("term-bad" ,"handle this in an individual function" ), +OPT_ARGUMENT ("term-good" ,"handle this in an individual function" ), +OPT_ARGUMENT ("term-new" ,"handle this in an individual function" ), +OPT_ARGUMENT ("term-old" ,"handle this in an individual function" ),OPT_BOOL (0 ,"no-checkout" , &no_checkout,N_ ("update BISECT_HEAD instead of checking out the current commit" )),OPT_END ()
@@ -450,6 +492,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) } res =bisect_next_check (&state, argv[2 ]);break ; +case BISECT_TERMS : +if (argc !=0 && argc !=1 ) +die (_ ("--bisect-terms requires 0 or 1 argument" )); + res =bisect_terms (&state, argc ? argv[0 ] :NULL ); +break ;default :die ("BUG: unknown subcommand '%d'" , cmdmode); }
git-bisect.sh +2 -33
@@ -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 ;; *) 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 "$@" ;; *) usage ;; esac
Signature · 1 line
2.9.0