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

Re: [PATCH] builtin-commit: re-read file index before launching editor

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Nov 9, 2021, 02:32 UTC
Message-ID
<211109.86wnlicaur.gmgdl@evledraar.gmail.com>
In-Reply-To
<pull.1127.git.git.1636423586620.gitgitgadget@gmail.com>
On Tue, Nov 09 2021, Samuel Yvon via GitGitGadget wrote:
Show 102 quoted lines
> From: Samuel Yvon <samuelyvon9@gmail.com>
>
> Changes made within a pre-commit hook are not reflected within
> the editor (for instance, with `git commit --verbose`) because
> the index is re-read after the editor has been used.
>
> This has the consequence of not displaying the actual changes made
> by the pre-commit hook, but committing them anyways.
>
> While it is often argued that the hook's purpose is not to automatically
> write content to the repository, it is acknowledged that using the
> pre-commit to apply mechanical fixes on top of the existing changes
> is a supported use case, along with verifying the content.
>
> I think not seeing these mechanical fixes before commiting is incorrect.
> A developer might expect the commit to look a certain way but if the
> pre-commit goes wrong the invalid changes will go unnoticed until
> committed.
>
> Signed-off-by: Samuel Yvon <samuelyvon9@gmail.com>
> ---
>     builtin-commit: Re-read file index before launching editor
>     
>     Changes made within a pre-commit hook are not reflected within the
>     editor (for instance, with git commit --verbose) because the index is
>     re-read after the editor has been used.
>     
>     This has the consequence of not displaying the actual changes made by
>     the pre-commit hook, but committing them anyways.
>     
>     While it is often argued that the hook's purpose is not to automatically
>     write content to the repository, it is acknowledged that using the
>     pre-commit to apply mechanical fixes on top of the existing changes is a
>     supported use case, along with verifying the content.
>     
>     I think not seeing these mechanical fixes before commiting is incorrect.
>     A developer might expect the commit to look a certain way but if the
>     pre-commit goes wrong the invalid changes will go unnoticed until
>     committed.
>     
>     I had a small exchange in the Google Group before submitting this Pr.
>     Here is a link for cross-referencing:
>     https://groups.google.com/g/git-mentoring/c/FsP83I9mN6c
>     
>     As a side note, I do not know who manages the Github Repo but the
>     following description threw me off a little bit:
>     
>     Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. 
>     
>     
>     since after looking deeper it seems the PRs are not ignored.
>     
>     Signed-off-by: Samuel Yvon samuelyvon9@gmail.com
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1127%2FSamuelYvon%2Fmaint-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1127/SamuelYvon/maint-v1
> Pull-Request: https://github.com/git/git/pull/1127
>
>  builtin/commit.c | 21 +++++++++++----------
>  1 file changed, 11 insertions(+), 10 deletions(-)
>
> diff --git a/builtin/commit.c b/builtin/commit.c
> index 7c9b1e7be3a..e75b11d1c60 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -728,8 +728,17 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  	/* This checks and barfs if author is badly specified */
>  	determine_author_info(author_ident);
>  
> -	if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
> -		return 0;
> +	if (!no_verify && find_hook("pre-commit")) {
> +		if(run_commit_hook(use_editor, index_file, "pre-commit", NULL))
> +			return 0;
> +
> +		/*
> +		 * Re-read the index as pre-commit hook could have updated it,
> +		 * and write it out as a tree.
> +		 */
> +		discard_cache();
> +		read_cache_from(index_file);
> +	}
>  
>  	if (squash_message) {
>  		/*
> @@ -1051,14 +1060,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  		return 0;
>  	}
>  
> -	if (!no_verify && find_hook("pre-commit")) {
> -		/*
> -		 * Re-read the index as pre-commit hook could have updated it,
> -		 * and write it out as a tree.  We must do this before we invoke
> -		 * the editor and after we invoke run_status above.
> -		 */
> -		discard_cache();
> -	}
>  	read_cache_from(index_file);
>  
>  	if (update_main_cache_tree(0)) {
>
> base-commit: 5fbd2fc5997dfa4d4593a862fe729b1e7a89bcf8

This hook logic is actively being changed these days, we're currently at batch 2/3 of 3/3 (or was it 4/4? I forget) to rewrite all these hook interfaces.

I haven't looked carefully at this, but I've got a patch that touches this exact logic here that'll be in-flight before too long: https://lore.kernel.org/git/patch-v5-36.36-fe056098534-20210902T125111Z-avarab@gmail.com/

Anyway, what you have here seems orthagonal, and it looks like these two could be combined, but on top of my linked patch we wouldn't be racy with the new behavior either.

But (and I've got to run) check out the commit message of that patch, perhaps it links to useful past discussions/commits.

I.e. a thing not covered in your commit message is a discussion of if the current behavior was explicitly desired by anyone, or if we just ended up with it by accident.

The code you're moving around has a comment which seems to suggest that what you want *is* the desired behavior, i.e. we re-read it before invoking the editor, so we should have the updated diff, but just don't?

Or maybe I'm misunderstanding it.
Previous: Samuel Yvon via GitGitGadgetNext: samuelyvon9@gmail.com
Message 2 of 18 in “builtin-commit: re-read file index before launching editor”
  1. builtin-commit: re-read file index before launching editorSamuel Yvon via GitGitGadget, Nov 9, 2021
  2. Ævar Arnfjörð BjarmasonNov 9, 2021
  3. samuelyvon9@gmail.comNov 9, 2021
  4. Ævar Arnfjörð BjarmasonNov 9, 2021
  5. Samuel YvonNov 9, 2021
  6. Junio C HamanoNov 9, 2021
  7. Samuel YvonNov 9, 2021
  8. Junio C HamanoNov 11, 2021
  9. Description of github.com/git/git, was Re: [PATCH] builtin-commit: re-read file index before launching editorJohannes Schindelin, Nov 9, 2021
  10. Samuel YvonNov 9, 2021
  11. Junio C HamanoNov 9, 2021
  12. Taylor BlauNov 9, 2021
  13. Samuel YvonNov 9, 2021
  14. Johannes SchindelinNov 10, 2021
  15. builtin-commit: re-read file index before run_statusSamuel Yvon via GitGitGadget, Nov 11, 2021
  16. Junio C HamanoNov 12, 2021
  17. Samuel YvonNov 17, 2021
  18. Junio C HamanoNov 18, 2021

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.