@matheusbsbs / matheus-bagliano / commits / ef24c7ca05
Fork 0

bisect--helper: add "--next-exit" to output bisect results

The goal of this patch is to port more shell code from the "bisect_next" function in "git-bisect.sh" to C code in "builtin-bisect--helper.c". So we port the code that interprets the bisection result and stops or continues (by checking out the next revision) the bisection process. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Apr 19, 2009 at 11:56 UTC ef24c7ca055f03c673c2b67a3a7c92bcc32f62f0
3 files changed +115 -3
bisect.c
+105
@@ -6,6 +6,7 @@
6 #include "list-objects.h"
7 #include "quote.h"
8 #include "sha1-lookup.h"
9 +#include "run-command.h"
10 #include "bisect.h"
11
12 static unsigned char (*skipped_sha1)[20];
@@ -16,6 +17,12 @@ static const char **rev_argv;
17 static int rev_argv_nr;
18 static int rev_argv_alloc;
19
20 +static const unsigned char *current_bad_sha1;
21 +
22 +static const char *argv_diff_tree[] = {"diff-tree", "--pretty", NULL, NULL};
23 +static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
24 +static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
25 +
26 /* bits #0-15 in revision.h */
27
28 #define COUNTED (1u<<16)
@@ -403,6 +410,7 @@ static int register_ref(const char *refname, const unsigned char *sha1,
410 {
411 if (!strcmp(refname, "bad")) {
412 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
413 + current_bad_sha1 = sha1;
414 rev_argv[rev_argv_nr++] = xstrdup(sha1_to_hex(sha1));
415 } else if (!prefixcmp(refname, "good-")) {
416 const char *hex = sha1_to_hex(sha1);
@@ -560,3 +568,100 @@ int bisect_next_vars(const char *prefix)
568
569 return show_bisect_vars(&info, reaches, all);
570 }
571 +
572 +static void exit_if_skipped_commits(struct commit_list *tried,
573 + const unsigned char *bad)
574 +{
575 + if (!tried)
576 + return;
577 +
578 + printf("There are only 'skip'ped commits left to test.\n"
579 + "The first bad commit could be any of:\n");
580 + print_commit_list(tried, "%s\n", "%s\n");
581 + if (bad)
582 + printf("%s\n", sha1_to_hex(bad));
583 + printf("We cannot bisect more!\n");
584 + exit(2);
585 +}
586 +
587 +static void mark_expected_rev(char *bisect_rev_hex)
588 +{
589 + int len = strlen(bisect_rev_hex);
590 + const char *filename = git_path("BISECT_EXPECTED_REV");
591 + int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
592 +
593 + if (fd < 0)
594 + die("could not create file '%s': %s",
595 + filename, strerror(errno));
596 +
597 + bisect_rev_hex[len] = '\n';
598 + write_or_die(fd, bisect_rev_hex, len + 1);
599 + bisect_rev_hex[len] = '\0';
600 +
601 + if (close(fd) < 0)
602 + die("closing file %s: %s", filename, strerror(errno));
603 +}
604 +
605 +static int bisect_checkout(char *bisect_rev_hex)
606 +{
607 + int res;
608 +
609 + mark_expected_rev(bisect_rev_hex);
610 +
611 + argv_checkout[2] = bisect_rev_hex;
612 + res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
613 + if (res)
614 + exit(res);
615 +
616 + argv_show_branch[1] = bisect_rev_hex;
617 + return run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
618 +}
619 +
620 +/*
621 + * We use the convention that exiting with an exit code 10 means that
622 + * the bisection process finished successfully.
623 + * In this case the calling shell script should exit 0.
624 + */
625 +int bisect_next_exit(const char *prefix)
626 +{
627 + struct rev_info revs;
628 + struct commit_list *tried;
629 + int reaches = 0, all = 0, nr;
630 + const unsigned char *bisect_rev;
631 + char bisect_rev_hex[41];
632 +
633 + bisect_common(&revs, prefix, &reaches, &all);
634 +
635 + revs.commits = filter_skipped(revs.commits, &tried, 0);
636 +
637 + if (!revs.commits) {
638 + /*
639 + * We should exit here only if the "bad"
640 + * commit is also a "skip" commit.
641 + */
642 + exit_if_skipped_commits(tried, NULL);
643 +
644 + printf("%s was both good and bad\n",
645 + sha1_to_hex(current_bad_sha1));
646 + exit(1);
647 + }
648 +
649 + bisect_rev = revs.commits->item->object.sha1;
650 + memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41);
651 +
652 + if (!hashcmp(bisect_rev, current_bad_sha1)) {
653 + exit_if_skipped_commits(tried, current_bad_sha1);
654 + printf("%s is first bad commit\n", bisect_rev_hex);
655 + argv_diff_tree[2] = bisect_rev_hex;
656 + run_command_v_opt(argv_diff_tree, RUN_GIT_CMD);
657 + /* This means the bisection process succeeded. */
658 + exit(10);
659 + }
660 +
661 + nr = all - reaches - 1;
662 + printf("Bisecting: %d revisions left to test after this "
663 + "(roughly %d steps)\n", nr, estimate_bisect_steps(all));
664 +
665 + return bisect_checkout(bisect_rev_hex);
666 +}
667 +
bisect.h
+1
@@ -29,6 +29,7 @@ struct rev_list_info {
29 extern int show_bisect_vars(struct rev_list_info *info, int reaches, int all);
30
31 extern int bisect_next_vars(const char *prefix);
32 +extern int bisect_next_exit(const char *prefix);
33
34 extern int estimate_bisect_steps(int all);
35
builtin-bisect--helper.c
+9 -3
@@ -5,23 +5,29 @@
5
6 static const char * const git_bisect_helper_usage[] = {
7 "git bisect--helper --next-vars",
8 + "git bisect--helper --next-exit",
9 NULL
10 };
11
12 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
13 {
14 int next_vars = 0;
15 + int next_exit = 0;
16 struct option options[] = {
17 OPT_BOOLEAN(0, "next-vars", &next_vars,
18 "output next bisect step variables"),
19 + OPT_BOOLEAN(0, "next-exit", &next_exit,
20 + "output bisect result and exit instuctions"),
21 OPT_END()
22 };
23
24 argc = parse_options(argc, argv, options, git_bisect_helper_usage, 0);
25
22 - if (!next_vars)
26 + if ((next_vars && next_exit) || (!next_vars && !next_exit))
27 usage_with_options(git_bisect_helper_usage, options);
28
25 - /* next-vars */
26 - return bisect_next_vars(prefix);
29 + if (next_vars)
30 + return bisect_next_vars(prefix);
31 + else /* next-exit */
32 + return bisect_next_exit(prefix);
33 }