{"thread":{"id":"42637","subject":"[RFC/PATCH] bisect--helper: `check_and_set_terms` shell function in C","startedAt":"2016-06-17T13:36:17Z","lastAt":"2016-06-17T13:36:17Z","messageCount":1,"participants":["Pranit Bauva"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"289443","messageId":"20160617133457.10336-1-pranit.bauva@gmail.com","threadId":"42637","inReplyTo":null,"subject":"[RFC/PATCH] bisect--helper: `check_and_set_terms` shell function in C","fromName":"Pranit Bauva","fromEmail":"pranit.bauva@gmail.com","sentAt":"2016-06-17T13:34:57Z","receivedAt":"2016-06-17T13:36:17Z","isPatch":true,"sender":{"key":"pranit.bauva@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2959938?v=4"},"body":"Reimplement the `check_and_set_terms` shell function in C and add\n`check-and-set-terms` subcommand to `git bisect--helper` to call it from\ngit-bisect.sh\n\nUsing `--check-and-set-terms` subcommand is a temporary measure to port\nshell function in C so as to use the existing test suite. As more\nfunctions are ported, this subcommand will be retired and will be called\nby some other methods.\n\ncheck_and_set_terms() sets and receives two global variables namely\nTERM_GOOD and TERM_BAD in the shell script. Luckily the file BISECT_TERMS\nalso contains the value of those variables so its appropriate to evoke the\nmethod get_terms() after calling the subcommand so that it retrieves the\nvalue of TERM_GOOD and TERM_BAD from the file BISECT_TERMS. The two\nglobal variables are passed as arguments to the subcommand. A struct is\nalso introduced to be used as a C counter part of the global variables.\n\nMentored-by: Lars Schneider <larsxschneider@gmail.com>\nMentored-by: Christian Couder <chriscool@tuxfamily.org>\nSigned-off-by: Pranit Bauva <pranit.bauva@gmail.com>\n---\nCollecting reviews. This patch is based on my previous series. I will also\ninclude this the next time I reroll the series.\n\n builtin/bisect--helper.c | 44 +++++++++++++++++++++++++++++++++++++++++++-\n git-bisect.sh            | 36 ++++--------------------------------\n 2 files changed, 47 insertions(+), 33 deletions(-)\n\ndiff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c\nindex eebfcf0..69cbc93 100644\n--- a/builtin/bisect--helper.c\n+++ b/builtin/bisect--helper.c\n@@ -26,6 +26,11 @@ static const char * const git_bisect_helper_usage[] = {\n \tNULL\n };\n \n+static struct bisect_term {\n+\tchar term_good[10];\n+\tchar term_bad[10];\n+} terms;\n+\n /*\n  * Check whether the string `term` belongs to the set of strings\n  * included in the variable arguments.\n@@ -242,6 +247,36 @@ static int bisect_write(const char *state, const char *rev,\n \treturn 0;\n }\n \n+static int check_and_set_terms(const char *cmd, const char *term_good,\n+\t\t\t       const char *term_bad)\n+{\n+\tif (one_of(cmd, \"skip\", \"start\", \"terms\", NULL))\n+\t\treturn 0;\n+\n+\tif (!is_empty_file(git_path_bisect_write_terms()) &&\n+\t    strcmp(cmd, term_bad) && strcmp(cmd, term_good))\n+\t\treturn error(_(\"Invalid command: you're currently in a\"\n+\t\t\t\t\"'%s' '%s' bisect\"), term_bad, term_good);\n+\n+\tif (one_of(cmd, \"bad\", \"good\", NULL)) {\n+\t\tif (is_empty_file(git_path_bisect_write_terms())) {\n+\t\t\tstrcpy(terms.term_bad, \"bad\");\n+\t\t\tstrcpy(terms.term_good, \"good\");\n+\t\t\treturn write_terms(terms.term_bad, terms.term_good);\n+\t\t}\n+\t}\n+\n+\tif (one_of(cmd, \"new\", \"old\", NULL)) {\n+\t\tif (is_empty_file(git_path_bisect_write_terms())) {\n+\t\t\tstrcpy(terms.term_bad, \"new\");\n+\t\t\tstrcpy(terms.term_good, \"old\");\n+\t\t\treturn write_terms(terms.term_bad, terms.term_good);\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n int cmd_bisect__helper(int argc, const char **argv, const char *prefix)\n {\n \tenum {\n@@ -250,7 +285,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)\n \t\tBISECT_CLEAN_STATE,\n \t\tBISECT_RESET,\n \t\tCHECK_EXPECTED_REVS,\n-\t\tBISECT_WRITE\n+\t\tBISECT_WRITE,\n+\t\tCHECK_AND_SET_TERMS\n \t} cmdmode = 0;\n \tint no_checkout = 0;\n \tstruct option options[] = {\n@@ -266,6 +302,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)\n \t\t\t N_(\"check for expected revs\"), CHECK_EXPECTED_REVS),\n \t\tOPT_CMDMODE(0, \"bisect-write\", &cmdmode,\n \t\t\t N_(\"write out the bisection state in BISECT_LOG\"), BISECT_WRITE),\n+\t\tOPT_CMDMODE(0, \"check-and-set-terms\", &cmdmode,\n+\t\t\t N_(\"check and set terms in a bisection state\"), CHECK_AND_SET_TERMS),\n \t\tOPT_BOOL(0, \"no-checkout\", &no_checkout,\n \t\t\t N_(\"update BISECT_HEAD instead of checking out the current commit\")),\n \t\tOPT_END()\n@@ -300,6 +338,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)\n \t\t\tdie(_(\"--bisect-write requires either 4 or 5 arguments\"));\n \t\tnolog = (argc == 5) && !strcmp(argv[4], \"nolog\");\n \t\treturn bisect_write(argv[0], argv[1], argv[2], argv[3], nolog);\n+\tcase CHECK_AND_SET_TERMS:\n+\t\tif (argc != 3)\n+\t\t\tdie(_(\"--check-and-set-terms requires 3 arguments\"));\n+\t\treturn check_and_set_terms(argv[0], argv[1], argv[2]);\n \tdefault:\n \t\tdie(\"BUG: unknown subcommand '%d'\", cmdmode);\n \t}\ndiff --git a/git-bisect.sh b/git-bisect.sh\nindex b9896a4..63ae742 100755\n--- a/git-bisect.sh\n+++ b/git-bisect.sh\n@@ -239,7 +239,8 @@ bisect_skip() {\n bisect_state() {\n \tbisect_autostart\n \tstate=$1\n-\tcheck_and_set_terms $state\n+\tgit bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD\n+\tget_terms\n \tcase \"$#,$state\" in\n \t0,*)\n \t\tdie \"$(gettext \"Please call 'bisect_state' with at least one argument.\")\" ;;\n@@ -390,7 +391,8 @@ bisect_replay () {\n \t\t\tcommand=\"$bisect\"\n \t\tfi\n \t\tget_terms\n-\t\tcheck_and_set_terms \"$command\"\n+\t\tgit bisect--helper --check-and-set-terms \"$command\" \"$TERM_GOOD\" \"$TERM_BAD\" || exit\n+\t\tget_terms\n \t\tcase \"$command\" in\n \t\tstart)\n \t\t\tcmd=\"bisect_start $rev\"\n@@ -480,36 +482,6 @@ get_terms () {\n \tfi\n }\n \n-check_and_set_terms () {\n-\tcmd=\"$1\"\n-\tcase \"$cmd\" in\n-\tskip|start|terms) ;;\n-\t*)\n-\t\tif test -s \"$GIT_DIR/BISECT_TERMS\" && test \"$cmd\" != \"$TERM_BAD\" && test \"$cmd\" != \"$TERM_GOOD\"\n-\t\tthen\n-\t\t\tdie \"$(eval_gettext \"Invalid command: you're currently in a \\$TERM_BAD/\\$TERM_GOOD bisect.\")\"\n-\t\tfi\n-\t\tcase \"$cmd\" in\n-\t\tbad|good)\n-\t\t\tif ! test -s \"$GIT_DIR/BISECT_TERMS\"\n-\t\t\tthen\n-\t\t\t\tTERM_BAD=bad\n-\t\t\t\tTERM_GOOD=good\n-\t\t\t\tgit bisect--helper --write-terms \"$TERM_BAD\" \"$TERM_GOOD\" || exit\n-\t\t\tfi\n-\t\t\t;;\n-\t\tnew|old)\n-\t\t\tif ! test -s \"$GIT_DIR/BISECT_TERMS\"\n-\t\t\tthen\n-\t\t\t\tTERM_BAD=new\n-\t\t\t\tTERM_GOOD=old\n-\t\t\t\tgit bisect--helper --write-terms \"$TERM_BAD\" \"$TERM_GOOD\" || exit\n-\t\t\tfi\n-\t\t\t;;\n-\t\tesac ;;\n-\tesac\n-}\n-\n bisect_voc () {\n \tcase \"$1\" in\n \tbad) echo \"bad|new\" ;;\n-- \n2.9.0\n\n"}]}