Skip to content

Commit

Permalink
bisect: add --force flag to force checkout
Browse files Browse the repository at this point in the history
This change adds a `--force` flag to `git bisect run` to force the checkout
that happens on each step. Forced checkouts are useful if the command that
`git bisect run` runs modifies the repository state, causing local changes,
such as in package lock files. A developer can run `git bisect run --force`
to ensure that the the repository is reset between each invocation.
  • Loading branch information
kevmo314 committed Jan 7, 2024
1 parent a26002b commit 0c087d1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
13 changes: 8 additions & 5 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ static int is_expected_rev(const struct object_id *oid)
}

enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
int no_checkout)
int no_checkout, int force_checkout)
{
struct commit *commit;
struct pretty_print_context pp = {0};
Expand All @@ -728,8 +728,11 @@ enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
struct child_process cmd = CHILD_PROCESS_INIT;

cmd.git_cmd = 1;
strvec_pushl(&cmd.args, "checkout", "-q",
oid_to_hex(bisect_rev), "--", NULL);
strvec_pushl(&cmd.args, "checkout", "-q");

Check failure on line 731 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-musl (alpine)

bisect.c:731:17: missing sentinel in function call [-Werror=format=]

Check failure on line 731 in bisect.c

View workflow job for this annotation

GitHub Actions / pedantic (fedora)

bisect.c:731:17: missing sentinel in function call [-Werror=format=]

Check failure on line 731 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-asan-ubsan (ubuntu-latest)

bisect.c:731:43: missing sentinel in function call [-Werror,-Wsentinel]

Check failure on line 731 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-gcc (ubuntu-20.04)

bisect.c:731:3: missing sentinel in function call [-Werror=format=]

Check failure on line 731 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-gcc-default (ubuntu-latest)

bisect.c:731:17: missing sentinel in function call [-Werror=format=]

Check failure on line 731 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-leaks (ubuntu-latest)

bisect.c:731:17: missing sentinel in function call [-Werror=format=]

Check failure on line 731 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-sha256 (ubuntu-latest)

bisect.c:731:43: missing sentinel in function call [-Werror,-Wsentinel]

Check failure on line 731 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-TEST-vars (ubuntu-20.04)

bisect.c:731:3: missing sentinel in function call [-Werror=format=]
if (force_checkout) {
strvec_pushl(&cmd.args, "--force");

Check failure on line 733 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-musl (alpine)

bisect.c:733:25: missing sentinel in function call [-Werror=format=]

Check failure on line 733 in bisect.c

View workflow job for this annotation

GitHub Actions / pedantic (fedora)

bisect.c:733:25: missing sentinel in function call [-Werror=format=]

Check failure on line 733 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-asan-ubsan (ubuntu-latest)

bisect.c:733:37: missing sentinel in function call [-Werror,-Wsentinel]

Check failure on line 733 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-gcc (ubuntu-20.04)

bisect.c:733:4: missing sentinel in function call [-Werror=format=]

Check failure on line 733 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-gcc-default (ubuntu-latest)

bisect.c:733:25: missing sentinel in function call [-Werror=format=]

Check failure on line 733 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-leaks (ubuntu-latest)

bisect.c:733:25: missing sentinel in function call [-Werror=format=]

Check failure on line 733 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-sha256 (ubuntu-latest)

bisect.c:733:37: missing sentinel in function call [-Werror,-Wsentinel]

Check failure on line 733 in bisect.c

View workflow job for this annotation

GitHub Actions / linux-TEST-vars (ubuntu-20.04)

bisect.c:733:4: missing sentinel in function call [-Werror=format=]
}
strvec_pushl(&cmd.args, oid_to_hex(bisect_rev), "--", NULL);
if (run_command(&cmd))
/*
* Errors in `run_command()` itself, signaled by res < 0,
Expand Down Expand Up @@ -850,7 +853,7 @@ static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int
handle_skipped_merge_base(mb);
} else {
printf(_("Bisecting: a merge base must be tested\n"));
res = bisect_checkout(mb, no_checkout);
res = bisect_checkout(mb, no_checkout, 0);
if (!res)
/* indicate early success */
res = BISECT_INTERNAL_SUCCESS_MERGE_BASE;
Expand Down Expand Up @@ -1104,7 +1107,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
/* Clean up objects used, as they will be reused. */
repo_clear_commit_marks(r, ALL_REV_FLAGS);

res = bisect_checkout(bisect_rev, no_checkout);
res = bisect_checkout(bisect_rev, no_checkout, 0);
cleanup:
release_revisions(&revs);
strvec_clear(&rev_argv);
Expand Down
2 changes: 1 addition & 1 deletion bisect.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ void read_bisect_terms(const char **bad, const char **good);
int bisect_clean_state(void);

enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
int no_checkout);
int no_checkout, int force_checkout);

#endif
20 changes: 14 additions & 6 deletions builtin/bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ static int do_bisect_run(const char *command)
return run_command(&cmd);
}

static int verify_good(const struct bisect_terms *terms, const char *command)
static int verify_good(const struct bisect_terms *terms, const char *command, int force_checkout)
{
int rc;
enum bisect_error res;
Expand All @@ -1189,13 +1189,13 @@ static int verify_good(const struct bisect_terms *terms, const char *command)
if (read_ref(no_checkout ? "BISECT_HEAD" : "HEAD", &current_rev))
return -1;

res = bisect_checkout(&good_rev, no_checkout);
res = bisect_checkout(&good_rev, no_checkout, force_checkout);
if (res != BISECT_OK)
return -1;

rc = do_bisect_run(command);

res = bisect_checkout(&current_rev, no_checkout);
res = bisect_checkout(&current_rev, no_checkout, force_checkout);
if (res != BISECT_OK)
return -1;

Expand All @@ -1209,6 +1209,7 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
const char *new_state;
int temporary_stdout_fd, saved_stdout;
int is_first_run = 1;
int force_checkout = 0;

if (bisect_next_check(terms, NULL))
return BISECT_FAILED;
Expand All @@ -1218,8 +1219,15 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
return BISECT_FAILED;
}

sq_quote_argv(&command, argv);
strbuf_ltrim(&command);
if (argv[0] == "--force") {

Check failure on line 1222 in builtin/bisect.c

View workflow job for this annotation

GitHub Actions / win build

builtin/bisect.c:1222:21: comparison with string literal results in unspecified behavior [-Werror=address]

Check failure on line 1222 in builtin/bisect.c

View workflow job for this annotation

GitHub Actions / linux32 (daald/ubuntu32:xenial)

builtin/bisect.c:1222:14: comparison with string literal results in unspecified behavior [-Werror=address]
force_checkout = 1;
sq_quote_argv(&command, argv[1]);

Check failure on line 1224 in builtin/bisect.c

View workflow job for this annotation

GitHub Actions / win build

builtin/bisect.c:1224:45: passing argument 2 of 'sq_quote_argv' from incompatible pointer type [-Werror=incompatible-pointer-types]

Check failure on line 1224 in builtin/bisect.c

View workflow job for this annotation

GitHub Actions / linux32 (daald/ubuntu32:xenial)

builtin/bisect.c:1224:27: passing argument 2 of 'sq_quote_argv' from incompatible pointer type [-Werror=incompatible-pointer-types]
strbuf_ltrim(&command);
} else {
sq_quote_argv(&command, argv);
strbuf_ltrim(&command);
}

while (1) {
res = do_bisect_run(command.buf);

Expand All @@ -1231,7 +1239,7 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
* missing or non-executable script.
*/
if (is_first_run && (res == 126 || res == 127)) {
int rc = verify_good(terms, command.buf);
int rc = verify_good(terms, command.buf, force_checkout);
is_first_run = 0;
if (rc < 0 || 128 <= rc) {
error(_("unable to verify %s on good"
Expand Down

0 comments on commit 0c087d1

Please sign in to comment.