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

Re: Arguments to git hooks

From
ASAndrew Sayers <andrew-git@pileofstuff.org>
Date
May 7, 2012, 22:17 UTC
Message-ID
<4FA84A0F.6060608@pileofstuff.org>
In-Reply-To
<CAH-tXsB4PBS_YjW4DCjT6ORmNPomQ8XMPbKx3hxVNH=FyB2u3g@mail.gmail.com>
On 06/05/12 19:35, jaseem abid wrote:
Show 9 quoted lines
> Hello all,
> 
> I am trying to write a hook '.git/hooks/commit-msg' to be run before
> every commit.
> 
> How can I pass arguments to the script? Now by default the only arg I
> am getting is `.git/COMMIT_EDITMSG'`. I would love to get the list of
> files I tried to commit also into the script so that I can run a lint
> program on it before committing it. How can I get this done?

First, a standard warning - consider using a pre-receive hook instead of a pre-commit hook. A lot of git's power comes from making commits as cheap as possible, so rules like "no committing until your code is pretty" tend to stifle people. For example, I often commit changes before running lint-type operations, then use `git add -p` and `git checkout -p` to selectively accept/reject individual changes. When I'm done, I `git commit --amend` to pretend the original commit never happened. A pre-receive hook gives you most of the same guarantees as a pre-commit hook with almost none of the cost.

Having said that, there are situations where pre-commit hooks are a good idea (like catching "DO NOT COMMIT" comments). I've played with this a little before, and never found a very satisfactory solution. Here are some important cases:

# git status will sometimes tell you the file that will be committed: # edit foo git add foo git commit

# git status will sometimes need a bit of careful parsing: # edit foo # edit bar git add foo git commit

# git status sometimes tells you the right file but the wrong contents: # edit foo git add foo # edit foo again git commit

# but often git status will tell you the wrong file altogether: # edit foo # edit bar git add foo git commit bar

The best solution I've found is a `git commit` wrapper that does something like `CHANGES="$(git commit $@ --dry-run -v)"` to get a reliable diff, then starts work from there.

	- Andrew
Previous: Jeff KingNext: jaseem abid
Message 8 of 9 in “Arguments to git hooks”
  1. jaseem abidMay 6, 2012
  2. Marcus KarlssonMay 6, 2012
  3. jaseem abidMay 6, 2012
  4. Marcus KarlssonMay 6, 2012
  5. jaseem abidMay 6, 2012
  6. Jeff KingMay 7, 2012
  7. Jeff KingMay 7, 2012
  8. Andrew SayersMay 7, 2012
  9. jaseem abidMay 14, 2012

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.