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

Re: Getting diff in commit-msg hook?

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 23, 2009, 02:35 UTC
Message-ID
<7vhc2lx4mc.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<loom.20090222T205429-30@post.gmane.org>
Gerd Knops <gerti-git@bitart.com> writes:
Show 11 quoted lines
> When I commit certain files, I would like to send a diff via email to
> interested parties.
>
> So I added a commit-msg hook, which checks the list of files for that commit.
> That part works fine. However in the email I would like to include a
> diff of the file(s) in question.
>
> Now when I run "git diff -- '$file'", I get no output, probably because the
> file is in some semi-committed state.
>
> So how can I obtain a diff for a file in a commit-msg hook?

How are you getting the list of files for the commit in that commit-msg hook? I think you are doing something like

	"git diff --cached --name-only"

The new state you are committing is in the index, and "the diff for the commit" is "what is the difference between the state of the previous commit and the state of this commit records", so

	" git diff --cached"
should give you what you want (and you do not need to do that per path.

Having said all that, I imagine you would realize that you do not want to do this inside any of the hooks triggered by "git commit", as you learn to work with git more. I suspect you would probably find a hook triggered when you push into another repository is more appropriate place for doing this kind of thing.

Previous: Gerd KnopsNext: Gerd Knops
Message 2 of 3 in “Getting diff in commit-msg hook?”
  1. Gerd KnopsFeb 22, 2009
  2. Junio C HamanoFeb 23, 2009
  3. Gerd KnopsFeb 23, 2009

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.