git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4 2/2] bisect: add --reset-when-found to leave when done

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 1, 2026, 19:54 UTC
Message-ID
<xmqqwlu97h1k.fsf@gitster.g>
In-Reply-To
<f5f370df1bab91872e32398386935d71d48a831b.1785577445.git.gitgitgadget@gmail.com>
"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 8 quoted lines
> @@ -1211,6 +1212,7 @@ int bisect_clean_state(void)
>  	unlink_or_warn(git_path_bisect_run());
>  	unlink_or_warn(git_path_bisect_terms());
>  	unlink_or_warn(git_path_bisect_first_parent());
> +	unlink_or_warn(git_path_bisect_reset_when_found());
>  	/*
>  	 * Cleanup BISECT_START last to support the --no-checkout option
>  	 * introduced in the commit 4796e823a.
OK.  If we
Show 12 quoted lines
> +static int bisect_reset_when_found(struct bisect_terms *terms)
> +{
> +	struct strbuf value = STRBUF_INIT;
> +	enum reset_when_found_mode mode;
> +	char *commit = NULL;
> +	int res;
> +
> +	if (strbuf_read_file(&value, git_path_bisect_reset_when_found(), 0) < 0) {
> +		res = error_errno(_("could not read '%s'"),
> +				  git_path_bisect_reset_when_found());
> +		goto cleanup;
> +	}

We expect that the caller calls this function only when we are doing "--reset-when-found"; otherwise we would give an error message from here even though we do not cause any damage otherwise.

We also expect that the callers refrain from calling this function when bisect_next() that they eventually reach would not want to immediately reset.

The defer_reset arrangement looks somewhat ugly even though what it achieves may be a worthy thing to do. Is the only code path that passes defer_reset==true down the call chain the bisect_run() codepath, to give that single caller a chance to close files that bisect_reset() would remove by calling bisect_clean_state()?

I am wondering if the result of solving it slightly differently may give us cleaner and easier to follow code, namely, we stop calling bisect_clean_state() from bisect_reset(). Of course you would need to find different place to call bisect_clean_state() to compensate, if we go that route, but how many code paths do we have that depends on bisect_reset() calling biesct_clean_state()?

Among existing callers of bisect_reset():
 - Does replay have to call reset?  Just like start does, isn't it
   sufficient to call clean_state?
 - cmd_bisect__reset() calls reset and returns, but it can call
   clean fater reset returns, if we need to make reset not to call
   clean.
Show 15 quoted lines
> +	strbuf_trim(&value);
> +	if (parse_reset_when_found(value.buf, &mode)) {
> +		res = -1;
> +		goto cleanup;
> +	}
> +
> +	if (mode == RESET_WHEN_FOUND_TO_FOUND)
> +		commit = xstrfmt("refs/bisect/%s", terms->term_bad);
> +	res = bisect_reset(commit, 1);
> +
> +cleanup:
> +	free(commit);
> +	strbuf_release(&value);
> +	return res;
> +}

OK, "commit" is NULL unless mode specifies TO_FOUND in which case we jump to the bad commit we found. bisect_reset() knows that commit==NULL means we go back to where we started. OK.

Show 10 quoted lines
> @@ -697,6 +760,9 @@ static enum bisect_error bisect_next(struct bisect_terms *terms, const char *pre
>  
>  	if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
>  		res = bisect_successful(terms);
> +		if (!res && !defer_reset &&
> +		    !is_empty_or_missing_file(git_path_bisect_reset_when_found()))
> +			res = bisect_reset_when_found(terms);
>  		return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
>  	} else if (res == BISECT_ONLY_SKIPPED_LEFT) {
>  		res = bisect_skipped_commits(terms);
This is the first ugliness I mentioned earlier.
Show 22 quoted lines
> @@ -1311,7 +1415,7 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
>  		saved_stdout = dup(1);
>  		dup2(temporary_stdout_fd, 1);
>  
> -		res = bisect_state(terms, 1, &new_state);
> +		res = bisect_state(terms, 1, &new_state, true);
>  
>  		fflush(stdout);
>  		dup2(saved_stdout, 1);
> @@ -1327,7 +1431,11 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
>  			res = BISECT_OK;
>  		} else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
>  			printf(_("bisect found first '%s' commit\n"), terms->term_bad);
> -			res = BISECT_OK;
> +			if (!is_empty_or_missing_file(git_path_bisect_reset_when_found()) &&
> +			    bisect_reset_when_found(terms))
> +				res = BISECT_FAILED;
> +			else
> +				res = BISECT_OK;
>  		} else if (res) {
>  			error(_("bisect run failed: 'git bisect %s'"
>  				" exited with error code %d"), new_state, res);
And these are the second one, that made the first one needed.
Another thing that I find a bit iffy is that earlier we said:
    We expect that the caller calls this function only when we are doing
    "--reset-when-found"; otherwise we would give an error message from
    here even though we do not cause any damage otherwise.
    We also expect that the callers refrain from calling this function
    when bisect_next() that they eventually reach would not want to
    immediately reset.

but the way the callers see if "--reset-when-found" is in effect looks quite ad-hoc. Instead of sprinkling "do we have that file in the filesystem and what does it say?" all over the place, I wonder if it is simpler to reason about if we do these checks upfront and store the parsed result in a variable, so that places that say !is_empty_or_missing_file(...) etc. do not have to? After all, we do not keep calling get_terms() in the middle of operation, and instead use values from "struct bisect_terms" that somebody else prepared much earlier before terms->term_good and terms->term_bad are used, right? Shouldn't it be handled pretty much the same way?

Previous: Harald Nordgren via GitGitGadgetNext: Junio C Hamano
Message 29 of 42 in “bisect: add --auto-reset to leave when done”
  1. 0/3 bisect: add --auto-reset to leave when doneHarald Nordgren via GitGitGadget, Jul 16, 2026
  2. 1/3 bisect: read run output from the open descriptorHarald Nordgren via GitGitGadget, Jul 16, 2026
  3. 2/3 bisect: let bisect_reset() optionally check out quietlyHarald Nordgren via GitGitGadget, Jul 16, 2026
  4. 3/3 bisect: add --auto-reset to leave when doneHarald Nordgren via GitGitGadget, Jul 16, 2026
  5. Junio C HamanoJul 16, 2026
  6. Harald NordgrenJul 16, 2026
  7. Junio C HamanoJul 17, 2026
  8. Harald NordgrenJul 17, 2026
  9. Junio C HamanoJul 17, 2026
  10. 0/3 bisect: add --auto-reset to leave when doneHarald Nordgren via GitGitGadget, Jul 17, 2026
  11. 1/3 bisect: read run output from the open descriptorHarald Nordgren via GitGitGadget, Jul 17, 2026
  12. Junio C HamanoJul 17, 2026
  13. Johannes SixtJul 18, 2026
  14. 2/3 bisect: let bisect_reset() optionally check out quietlyHarald Nordgren via GitGitGadget, Jul 17, 2026
  15. 3/3 bisect: add --auto-reset to leave when doneHarald Nordgren via GitGitGadget, Jul 17, 2026
  16. Johannes SixtJul 18, 2026
  17. Junio C HamanoJul 20, 2026
  18. 0/2 bisect: add --auto-reset to leave when doneHarald Nordgren via GitGitGadget, Jul 20, 2026
  19. 1/2 bisect: let bisect_reset() optionally check out quietlyHarald Nordgren via GitGitGadget, Jul 20, 2026
  20. 2/2 bisect: add --reset-when-found to leave when doneHarald Nordgren via GitGitGadget, Jul 20, 2026
  21. Johannes SixtJul 23, 2026
  22. Junio C HamanoJul 23, 2026
  23. Harald NordgrenAug 1, 2026
  24. Junio C HamanoJul 20, 2026
  25. 0/2 bisect: add --reset-when-found to leave when doneHarald Nordgren via GitGitGadget, Aug 1, 2026
  26. 1/2 bisect: let bisect_reset() optionally check out quietlyHarald Nordgren via GitGitGadget, Aug 1, 2026
  27. Junio C HamanoAug 1, 2026
  28. 2/2 bisect: add --reset-when-found to leave when doneHarald Nordgren via GitGitGadget, Aug 1, 2026
  29. Junio C HamanoAug 1, 2026
  30. Junio C HamanoAug 1, 2026
  31. 0/2 bisect: add --reset-when-found to leave when doneHarald Nordgren via GitGitGadget, Aug 2, 2026
  32. 1/2 bisect: let bisect_reset() optionally check out quietlyHarald Nordgren via GitGitGadget, Aug 2, 2026
  33. 2/2 bisect: add --reset-when-found to leave when doneHarald Nordgren via GitGitGadget, Aug 2, 2026
  34. Junio C HamanoAug 2, 2026
  35. 0/2 bisect: add --reset-when-found to leave when doneHarald Nordgren via GitGitGadget, Aug 2, 2026
  36. 1/2 bisect: let bisect_reset() optionally check out quietlyHarald Nordgren via GitGitGadget, Aug 2, 2026
  37. 2/2 bisect: add --reset-when-found to leave when doneHarald Nordgren via GitGitGadget, Aug 2, 2026
  38. Junio C HamanoAug 3, 2026
  39. Harald NordgrenAug 6, 2026
  40. Junio C HamanoAug 6, 2026
  41. Harald NordgrenAug 6, 2026
  42. Junio C HamanoAug 6, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.