Skip to content

Commit d937d4a

Browse files
chriscoolgitster
authored andcommitted
bisect: add "check_good_are_ancestors_of_bad" function
This is a port of the function with the same name that is in "git-bisect.sh". The new function is not used yet but will be in a later patch. We also implement an helper "check_ancestors" function that use "start_command" and "finish_command" to launch "git rev-list $good ^$bad". Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c053766 commit d937d4a

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

bisect.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,81 @@ static void check_merge_bases(void)
750750
free_commit_list(result);
751751
}
752752

753+
/*
754+
* This function runs the command "git rev-list $_good ^$_bad"
755+
* and returns 1 if it produces some output, 0 otherwise.
756+
*/
757+
static int check_ancestors(void)
758+
{
759+
struct argv_array rev_argv = { NULL, 0, 0 };
760+
struct strbuf str = STRBUF_INIT;
761+
int i, result = 0;
762+
struct child_process rls;
763+
FILE *rls_fout;
764+
765+
argv_array_push(&rev_argv, xstrdup("rev-list"));
766+
argv_array_push_sha1(&rev_argv, current_bad_sha1, "^%s");
767+
for (i = 0; i < good_revs.sha1_nr; i++)
768+
argv_array_push_sha1(&rev_argv, good_revs.sha1[i], "%s");
769+
argv_array_push(&rev_argv, NULL);
770+
771+
memset(&rls, 0, sizeof(rls));
772+
rls.argv = rev_argv.argv;
773+
rls.out = -1;
774+
rls.git_cmd = 1;
775+
if (start_command(&rls))
776+
die("Could not launch 'git rev-list' command.");
777+
rls_fout = fdopen(rls.out, "r");
778+
while (strbuf_getline(&str, rls_fout, '\n') != EOF) {
779+
strbuf_trim(&str);
780+
if (*str.buf) {
781+
result = 1;
782+
break;
783+
}
784+
}
785+
fclose(rls_fout);
786+
finish_command(&rls);
787+
788+
return result;
789+
}
790+
791+
/*
792+
* "check_good_are_ancestors_of_bad" checks that all "good" revs are
793+
* ancestor of the "bad" rev.
794+
*
795+
* If that's not the case, we need to check the merge bases.
796+
* If a merge base must be tested by the user, its source code will be
797+
* checked out to be tested by the user and we will exit.
798+
*/
799+
static void check_good_are_ancestors_of_bad(const char *prefix)
800+
{
801+
const char *filename = git_path("BISECT_ANCESTORS_OK");
802+
struct stat st;
803+
int fd;
804+
805+
if (!current_bad_sha1)
806+
die("a bad revision is needed");
807+
808+
/* Check if file BISECT_ANCESTORS_OK exists. */
809+
if (!stat(filename, &st) && S_ISREG(st.st_mode))
810+
return;
811+
812+
/* Bisecting with no good rev is ok. */
813+
if (good_revs.sha1_nr == 0)
814+
return;
815+
816+
if (check_ancestors())
817+
check_merge_bases();
818+
819+
/* Create file BISECT_ANCESTORS_OK. */
820+
fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
821+
if (fd < 0)
822+
warning("could not create file '%s': %s",
823+
filename, strerror(errno));
824+
else
825+
close(fd);
826+
}
827+
753828
/*
754829
* We use the convention that exiting with an exit code 10 means that
755830
* the bisection process finished successfully.

0 commit comments

Comments
 (0)