]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'mr/bisect-in-c-2'
authorJunio C Hamano <gitster@pobox.com>
Sun, 4 Oct 2020 19:49:08 +0000 (12:49 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 4 Oct 2020 19:49:08 +0000 (12:49 -0700)
Rewrite of the "git bisect" script in C continues.

* mr/bisect-in-c-2:
  bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C
  bisect: call 'clear_commit_marks_all()' in 'bisect_next_all()'
  bisect--helper: reimplement `bisect_autostart` shell function in C
  bisect--helper: introduce new `write_in_file()` function
  bisect--helper: use '-res' in 'cmd_bisect__helper' return
  bisect--helper: BUG() in cmd_*() on invalid subcommand

1  2 
builtin/bisect--helper.c
git-bisect.sh

diff --combined builtin/bisect--helper.c
index 2f8ef0ea307d920efda0374e6b4a94c72fa2bd38,64f16d5d92ffb0c1433e92735b3e3b5516f9b124..7512b880f0d67fbe5dba8de93271324b8338d7d9
@@@ -8,6 -8,7 +8,7 @@@
  #include "run-command.h"
  #include "prompt.h"
  #include "quote.h"
+ #include "revision.h"
  
  static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
  static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
@@@ -27,11 -28,19 +28,19 @@@ static const char * const git_bisect_he
        N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
        N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
        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>]"
 +      N_("git bisect--helper --bisect-start [--term-{new,bad}=<term> --term-{old,good}=<term>]"
                                            " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
+       N_("git bisect--helper --bisect-next"),
+       N_("git bisect--helper --bisect-auto-next"),
+       N_("git bisect--helper --bisect-autostart"),
        NULL
  };
  
+ struct add_bisect_ref_data {
+       struct rev_info *revs;
+       unsigned int object_flags;
+ };
  struct bisect_terms {
        char *term_good;
        char *term_bad;
@@@ -55,6 -64,8 +64,8 @@@ static void set_terms(struct bisect_ter
  static const char vocab_bad[] = "bad|new";
  static const char vocab_good[] = "good|old";
  
+ static int bisect_autostart(struct bisect_terms *terms);
  /*
   * Check whether the string `term` belongs to the set of strings
   * included in the variable arguments.
@@@ -74,6 -85,52 +85,52 @@@ static int one_of(const char *term, ...
        return res;
  }
  
+ static int write_in_file(const char *path, const char *mode, const char *format, va_list args)
+ {
+       FILE *fp = NULL;
+       int res = 0;
+       if (strcmp(mode, "w") && strcmp(mode, "a"))
+               BUG("write-in-file does not support '%s' mode", mode);
+       fp = fopen(path, mode);
+       if (!fp)
+               return error_errno(_("cannot open file '%s' in mode '%s'"), path, mode);
+       res = vfprintf(fp, format, args);
+       if (res < 0) {
+               int saved_errno = errno;
+               fclose(fp);
+               errno = saved_errno;
+               return error_errno(_("could not write to file '%s'"), path);
+       }
+       return fclose(fp);
+ }
+ static int write_to_file(const char *path, const char *format, ...)
+ {
+       int res;
+       va_list args;
+       va_start(args, format);
+       res = write_in_file(path, "w", format, args);
+       va_end(args);
+       return res;
+ }
+ static int append_to_file(const char *path, const char *format, ...)
+ {
+       int res;
+       va_list args;
+       va_start(args, format);
+       res = write_in_file(path, "a", format, args);
+       va_end(args);
+       return res;
+ }
  static int check_term_format(const char *term, const char *orig_term)
  {
        int res;
  
  static int write_terms(const char *bad, const char *good)
  {
-       FILE *fp = NULL;
        int res;
  
        if (!strcmp(bad, good))
        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 = write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad, good);
  
-       res = fprintf(fp, "%s\n%s\n", bad, good);
-       res |= fclose(fp);
-       return (res < 0) ? -1 : 0;
+       return res;
  }
  
  static int is_expected_rev(const char *expected_hex)
@@@ -421,6 -473,142 +473,142 @@@ finish
        return res;
  }
  
+ static int add_bisect_ref(const char *refname, const struct object_id *oid,
+                         int flags, void *cb)
+ {
+       struct add_bisect_ref_data *data = cb;
+       add_pending_oid(data->revs, refname, oid, data->object_flags);
+       return 0;
+ }
+ static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
+ {
+       int res = 0;
+       struct add_bisect_ref_data cb = { revs };
+       char *good = xstrfmt("%s-*", terms->term_good);
+       /*
+        * We cannot use terms->term_bad directly in
+        * for_each_glob_ref_in() and we have to append a '*' to it,
+        * otherwise for_each_glob_ref_in() will append '/' and '*'.
+        */
+       char *bad = xstrfmt("%s*", terms->term_bad);
+       /*
+        * It is important to reset the flags used by revision walks
+        * as the previous call to bisect_next_all() in turn
+        * sets up a revision walk.
+        */
+       reset_revision_walk();
+       init_revisions(revs, NULL);
+       setup_revisions(0, NULL, revs, NULL);
+       for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb);
+       cb.object_flags = UNINTERESTING;
+       for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb);
+       if (prepare_revision_walk(revs))
+               res = error(_("revision walk setup failed\n"));
+       free(good);
+       free(bad);
+       return res;
+ }
+ static int bisect_skipped_commits(struct bisect_terms *terms)
+ {
+       int res;
+       FILE *fp = NULL;
+       struct rev_info revs;
+       struct commit *commit;
+       struct pretty_print_context pp = {0};
+       struct strbuf commit_name = STRBUF_INIT;
+       res = prepare_revs(terms, &revs);
+       if (res)
+               return res;
+       fp = fopen(git_path_bisect_log(), "a");
+       if (!fp)
+               return error_errno(_("could not open '%s' for appending"),
+                                 git_path_bisect_log());
+       if (fprintf(fp, "# only skipped commits left to test\n") < 0)
+               return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
+       while ((commit = get_revision(&revs)) != NULL) {
+               strbuf_reset(&commit_name);
+               format_commit_message(commit, "%s",
+                                     &commit_name, &pp);
+               fprintf(fp, "# possible first %s commit: [%s] %s\n",
+                       terms->term_bad, oid_to_hex(&commit->object.oid),
+                       commit_name.buf);
+       }
+       /*
+        * Reset the flags used by revision walks in case
+        * there is another revision walk after this one.
+        */
+       reset_revision_walk();
+       strbuf_release(&commit_name);
+       fclose(fp);
+       return 0;
+ }
+ static int bisect_successful(struct bisect_terms *terms)
+ {
+       struct object_id oid;
+       struct commit *commit;
+       struct pretty_print_context pp = {0};
+       struct strbuf commit_name = STRBUF_INIT;
+       char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
+       int res;
+       read_ref(bad_ref, &oid);
+       commit = lookup_commit_reference_by_name(bad_ref);
+       format_commit_message(commit, "%s", &commit_name, &pp);
+       res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
+                           terms->term_bad, oid_to_hex(&commit->object.oid),
+                           commit_name.buf);
+       strbuf_release(&commit_name);
+       free(bad_ref);
+       return res;
+ }
+ static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
+ {
+       enum bisect_error res;
+       if (bisect_autostart(terms))
+               return BISECT_FAILED;
+       if (bisect_next_check(terms, terms->term_good))
+               return BISECT_FAILED;
+       /* Perform all bisection computation */
+       res = bisect_next_all(the_repository, prefix);
+       if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
+               res = bisect_successful(terms);
+               return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
+       } else if (res == BISECT_ONLY_SKIPPED_LEFT) {
+               res = bisect_skipped_commits(terms);
+               return res ? res : BISECT_ONLY_SKIPPED_LEFT;
+       }
+       return res;
+ }
+ static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
+ {
+       if (bisect_next_check(terms, NULL))
+               return BISECT_OK;
+       return bisect_next(terms, prefix);
+ }
  static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
  {
        int no_checkout = 0;
                        terms->term_bad = xstrdup(arg);
                } else if (starts_with(arg, "--")) {
                        return error(_("unrecognized option: '%s'"), arg);
 -              } else {
 -                      char *commit_id = xstrfmt("%s^{commit}", arg);
 -                      if (get_oid(commit_id, &oid) && has_double_dash)
 -                              die(_("'%s' does not appear to be a valid "
 -                                    "revision"), arg);
 -
 +              } else if (!get_oidf(&oid, "%s^{commit}", arg)) {
                        string_list_append(&revs, oid_to_hex(&oid));
 -                      free(commit_id);
 +              } else if (has_double_dash) {
 +                      die(_("'%s' does not appear to be a valid "
 +                            "revision"), arg);
 +              } else {
 +                      break;
                }
        }
        pathspec_pos = i;
@@@ -623,6 -812,38 +811,38 @@@ finish
        return res;
  }
  
+ static inline int file_is_not_empty(const char *path)
+ {
+       return !is_empty_or_missing_file(path);
+ }
+ static int bisect_autostart(struct bisect_terms *terms)
+ {
+       int res;
+       const char *yesno;
+       if (file_is_not_empty(git_path_bisect_start()))
+               return 0;
+       fprintf_ln(stderr, _("You need to start by \"git bisect "
+                         "start\"\n"));
+       if (!isatty(STDIN_FILENO))
+               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);
+       res = tolower(*yesno) == 'n' ?
+               -1 : bisect_start(terms, empty_strvec, 0);
+       return res;
+ }
  int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
  {
        enum {
                CHECK_AND_SET_TERMS,
                BISECT_NEXT_CHECK,
                BISECT_TERMS,
-               BISECT_START
+               BISECT_START,
+               BISECT_AUTOSTART,
+               BISECT_NEXT,
+               BISECT_AUTO_NEXT
        } cmdmode = 0;
        int res = 0, nolog = 0;
        struct option options[] = {
                         N_("print out the bisect terms"), BISECT_TERMS),
                OPT_CMDMODE(0, "bisect-start", &cmdmode,
                         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 checkout the next bisection commit"), BISECT_AUTO_NEXT),
+               OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
+                        N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
                OPT_BOOL(0, "no-log", &nolog,
                         N_("no log for BISECT_WRITE")),
                OPT_END()
                set_terms(&terms, "bad", "good");
                res = bisect_start(&terms, argv, argc);
                break;
+       case BISECT_NEXT:
+               if (argc)
+                       return error(_("--bisect-next requires 0 arguments"));
+               get_terms(&terms);
+               res = bisect_next(&terms, prefix);
+               break;
+       case BISECT_AUTO_NEXT:
+               if (argc)
+                       return error(_("--bisect-auto-next requires 0 arguments"));
+               get_terms(&terms);
+               res = bisect_auto_next(&terms, prefix);
+               break;
+       case BISECT_AUTOSTART:
+               if (argc)
+                       return error(_("--bisect-autostart does not accept arguments"));
+               set_terms(&terms, "bad", "good");
+               res = bisect_autostart(&terms);
+               break;
        default:
-               return error("BUG: unknown subcommand '%d'", cmdmode);
+               BUG("unknown subcommand %d", cmdmode);
        }
        free_terms(&terms);
  
         * Handle early success
         * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
         */
-       if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE)
+       if ((res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) || (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND))
                res = BISECT_OK;
  
-       return abs(res);
+       return -res;
  }
diff --combined git-bisect.sh
index 2f60fefcfaa12c3a4579818d4cd616232340108f,f7f39c705ad72819954b8faa6e6d76ec1c9f9b1f..ea7e684ebb55481b8118adaab6dd2324cf7bf3e8
@@@ -3,8 -3,8 +3,8 @@@
  USAGE='[help|start|bad|good|new|old|terms|skip|next|reset|visualize|view|replay|log|run]'
  LONG_USAGE='git bisect help
        print this long help message.
 -git bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]
 -               [--no-checkout] [<bad> [<good>...]] [--] [<pathspec>...]
 +git bisect start [--term-{new,bad}=<term> --term-{old,good}=<term>]
 +               [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<pathspec>...]
        reset bisect state and start bisection.
  git bisect (bad|new) [<rev>]
        mark <rev> a known-bad revision/
@@@ -49,27 -49,6 +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
-                       bisect_start
-               else
-                       exit 1
-               fi
-       }
- }
  bisect_start() {
        git bisect--helper --bisect-start $@ || exit
  
@@@ -86,8 -65,7 +65,7 @@@
        #
        # Check if we can proceed to the next bisect state.
        #
-       get_terms
-       bisect_auto_next
+       git bisect--helper --bisect-auto-next || exit
  
        trap '-' 0
  }
@@@ -108,7 -86,7 +86,7 @@@ bisect_skip() 
  }
  
  bisect_state() {
-       bisect_autostart
+       git bisect--helper --bisect-autostart || exit
        state=$1
        git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
        get_terms
        *)
                usage ;;
        esac
-       bisect_auto_next
- }
- bisect_auto_next() {
-       git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || :
- }
- bisect_next() {
-       case "$#" in 0) ;; *) usage ;; esac
-       bisect_autostart
-       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
-       res=$?
-       # Check if we should exit because bisection is finished
-       if test $res -eq 10
-       then
-               bad_rev=$(git show-ref --hash --verify refs/bisect/$TERM_BAD)
-               bad_commit=$(git show-branch $bad_rev)
-               echo "# first $TERM_BAD commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
-               exit 0
-       elif test $res -eq 2
-       then
-               echo "# only skipped commits left to test" >>"$GIT_DIR/BISECT_LOG"
-               good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/$TERM_GOOD-*")
-               for skipped in $(git rev-list refs/bisect/$TERM_BAD --not $good_revs)
-               do
-                       skipped_commit=$(git show-branch $skipped)
-                       echo "# possible first $TERM_BAD commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
-               done
-               exit $res
-       fi
-       # Check for an error in the bisection process
-       test $res -ne 0 && exit $res
-       return 0
+       git bisect--helper --bisect-auto-next
  }
  
  bisect_visualize() {
@@@ -234,7 -174,7 +174,7 @@@ bisect_replay () 
                esac
        done <"$file"
        IFS="$oIFS"
-       bisect_auto_next
+       git bisect--helper --bisect-auto-next || exit
  }
  
  bisect_run () {
@@@ -331,7 -271,7 +271,7 @@@ case "$#" i
                bisect_skip "$@" ;;
        next)
                # Not sure we want "next" at the UI level anymore.
-               bisect_next "$@" ;;
+               git bisect--helper --bisect-next "$@" || exit ;;
        visualize|view)
                bisect_visualize "$@" ;;
        reset)