From: Andrew Sayers <andrew-git@pileofstuff.org>
To: jaseem abid <jaseemabid@gmail.com>
Cc: git mailing list <git@vger.kernel.org>
Subject: Re: Arguments to git hooks
Date: Mon, 07 May 2012 23:17:51 +0100 [thread overview]
Message-ID: <4FA84A0F.6060608@pileofstuff.org> (raw)
In-Reply-To: <CAH-tXsB4PBS_YjW4DCjT6ORmNPomQ8XMPbKx3hxVNH=FyB2u3g@mail.gmail.com>
On 06/05/12 19:35, jaseem abid wrote:
> 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
next prev parent reply other threads:[~2012-05-07 22:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-06 18:35 Arguments to git hooks jaseem abid
2012-05-06 19:12 ` Marcus Karlsson
2012-05-06 19:18 ` jaseem abid
2012-05-06 21:11 ` Marcus Karlsson
2012-05-06 21:40 ` jaseem abid
2012-05-07 7:21 ` Jeff King
2012-05-07 7:15 ` Jeff King
2012-05-07 22:17 ` Andrew Sayers [this message]
2012-05-14 20:34 ` jaseem abid
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4FA84A0F.6060608@pileofstuff.org \
--to=andrew-git@pileofstuff.org \
--cc=git@vger.kernel.org \
--cc=jaseemabid@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).