git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Should commit-msg hook receive the washed message?
@ 2024-07-05 20:12 Sean Allred
  2024-07-05 21:35 ` Eric Sunshine
  0 siblings, 1 reply; 4+ messages in thread
From: Sean Allred @ 2024-07-05 20:12 UTC (permalink / raw)
  To: Git mailing list

From githooks.txt:
> This hook is invoked by linkgit:git-commit[1] and
> linkgit:git-merge[1], and can be bypassed with the `--no-verify`
> option. It takes a single parameter, the name of the file that holds
> the proposed commit log message. Exiting with a non-zero status causes
> the command to abort.

Of course the actual 'proposed commit log message' doesn't include the
comments included when running a commit, e.g.

    git -c commit.status=true commit

but the execution of the `commit-msg` happens before `cleanup_message`
is called on COMMIT_EDITMSG.

This seems like a bug to me; is there something I'm missing? I would
propose adding a call to `cleanup_message` (with the appropriate
arguments) inside `prepare_to_commit` right before `commit-msg` is
invoked.

It's causing us quite a bit of grief (e.g. with external tools that
invoke hooks incorrectly [1] + some other internal workarounds for
things like patch scissors).

Thanks,
-Sean

[1]: https://lore.kernel.org/git/17df67804ef7a3c8.df629cdadcf4ea15.524a056283063601@EPIC94403/

-- 
Sean Allred

^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: Should commit-msg hook receive the washed message?
@ 2024-07-05 22:07 brianmlyles
  0 siblings, 0 replies; 4+ messages in thread
From: brianmlyles @ 2024-07-05 22:07 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git mailing list, Jeff King, Phillip Wood, Sean Allred

> Eric Sunshine <sunshine@sunshineco.com> wrote:
> 
> The idea you proposed in a different thread[2] of exposing
> cleanup_message() functionality as a user-facing utility which a hook
> can call on an as-needed basis may make more sense(?).
> 
> [2]: https://lore.kernel.org/git/m034onpng4.fsf@epic96565.epic.com/

I don't think that would suffice for our use case. Specifically, the
commit-msg hook has no way to know if it's looking at a message that
will be washed, or will not. For example, let's say a commit-msg hook
aims to enforce a 72-character hard-wrap policy. If a line starts with
`#`, what is the hook supposed to do?

- If the commit was created using `git commit` and the user's normal
  editor is invoked, the hook should *not* evaluate any line starting
  with `#` because that line will be removed prior to creating the
  commit as part of the message washing process.
- If the commit was created using `git commit -m`, the hook *should*
  evaluate any line starting with `#` because that line will not be
  removed by washing.

This challenge also exists for the patch scissors and any content
following it. That could have been added by git because the user called
`git commit -v` (most likely), but it also could in theory have been
added by the user, in which case the patch (or patch-like content) ends
up in the final commit message as well. The hook has no way to know
whether the commit was initiated via `git commit -v` or not. The
real-world use case here could be a commit-msg hook that adds a trailer:
if the patch will be removed during washing, then using `git
interpret-trailers` to add the trailer (which places it *before* the
patch) is fine. If the patch will not be removed during washing, then 
`git interpret-trailers` ends up putting the trailer in the middle of
the final message between the subject/body and the patch, then the patch
isn't removed and thus it's not a valid trailer.

Ultimately, the hook doesn't know what will happen to the message after
it runs, and thus doesn't know if a given line is guaranteed to be part
of the final commit message. This presents these edge case challenges
when attempting to implement the hook.

If the hook either were always called with the final proposed commit
message, or if it knew that the message would be washed after being
executed, then we'd have options for handling these edge cases.

-- 
Thanks,
Brian Lyles

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-07-06  6:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-05 20:12 Should commit-msg hook receive the washed message? Sean Allred
2024-07-05 21:35 ` Eric Sunshine
2024-07-06  6:37   ` Jeff King
  -- strict thread matches above, loose matches on Subject: below --
2024-07-05 22:07 brianmlyles

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).