{"thread":{"id":"30449","subject":"Arguments to git hooks","startedAt":"2012-05-06T18:35:51Z","lastAt":"2012-05-14T20:34:19Z","messageCount":9,"participants":["jaseem abid","Marcus Karlsson","Jeff King","Andrew Sayers"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"190932","messageId":"CAH-tXsB4PBS_YjW4DCjT6ORmNPomQ8XMPbKx3hxVNH=FyB2u3g@mail.gmail.com","threadId":"30449","inReplyTo":null,"subject":"Arguments to git hooks","fromName":"jaseem abid","fromEmail":"jaseemabid@gmail.com","sentAt":"2012-05-06T18:35:51Z","receivedAt":"2012-05-06T18:35:51Z","isPatch":false,"sender":{"key":"jaseemabid@gmail.com","avatar":"https://gravatar.com/avatar/8b0432c96e4d3c8a9a96c9961ee842df7b7a869744da9c25187a51a992eabd81?d=mp&s=160"},"body":"Hello all,\n\nI am trying to write a hook '.git/hooks/commit-msg' to be run before\nevery commit.\n\nHow can I pass arguments to the script? Now by default the only arg I\nam getting is `.git/COMMIT_EDITMSG'`. I would love to get the list of\nfiles I tried to commit also into the script so that I can run a lint\nprogram on it before committing it. How can I get this done?\n\nV/r,\n\n--\nJaseem Abid\nhttp://jaseemabid.github.com\n"},{"id":"190933","messageId":"20120506191202.GB5725@moj","threadId":"30449","inReplyTo":"CAH-tXsB4PBS_YjW4DCjT6ORmNPomQ8XMPbKx3hxVNH=FyB2u3g@mail.gmail.com","subject":"Re: Arguments to git hooks","fromName":"Marcus Karlsson","fromEmail":"mk@acc.umu.se","sentAt":"2012-05-06T19:12:02Z","receivedAt":"2012-05-06T19:12:02Z","isPatch":false,"sender":{"key":"mk@acc.umu.se","avatar":null},"body":"On Mon, May 07, 2012 at 12:05:51AM +0530, jaseem abid wrote:\n> Hello all,\n> \n> I am trying to write a hook '.git/hooks/commit-msg' to be run before\n> every commit.\n> \n> How can I pass arguments to the script? Now by default the only arg I\n> am getting is `.git/COMMIT_EDITMSG'`. I would love to get the list of\n> files I tried to commit also into the script so that I can run a lint\n> program on it before committing it. How can I get this done?\n> \n> V/r,\n\nAs far as I can see that's the only argument that git-commit passes on\nto the commit-msg hook. Can't you just call something like git-status\nfrom the hook or do you need the information passed specifically as an\nargument?\n\n\tMarcus\n"},{"id":"190934","messageId":"CAH-tXsBpfXkr-qWdSBhPu6CcSZbm3f=3i1xJVtk6OkHz1NKCfA@mail.gmail.com","threadId":"30449","inReplyTo":"20120506191202.GB5725@moj","subject":"Re: Arguments to git hooks","fromName":"jaseem abid","fromEmail":"jaseemabid@gmail.com","sentAt":"2012-05-06T19:18:35Z","receivedAt":"2012-05-06T19:18:35Z","isPatch":false,"sender":{"key":"jaseemabid@gmail.com","avatar":"https://gravatar.com/avatar/8b0432c96e4d3c8a9a96c9961ee842df7b7a869744da9c25187a51a992eabd81?d=mp&s=160"},"body":"On Mon, May 7, 2012 at 12:42 AM, Marcus Karlsson <mk@acc.umu.se> wrote:\n> As far as I can see that's the only argument that git-commit passes on\n> to the commit-msg hook. Can't you just call something like git-status\n> from the hook or do you need the information passed specifically as an\n> argument?\n\nI can always get last commit from `.git/COMMIT_EDITMSG'` - a fixed\nfile. Isn't passing that as an argument a bit pointless? I want files\nI tried to commit specifically passed in as an argument.\n\nAre you suggesting me to run some plumbing command, parse the result\nand get the required data ? Is there an easier way to get this done?\n\n\n-- \nJaseem Abid\nhttp://jaseemabid.github.com\n"},{"id":"190941","messageId":"20120506211120.GC5725@moj","threadId":"30449","inReplyTo":"CAH-tXsBpfXkr-qWdSBhPu6CcSZbm3f=3i1xJVtk6OkHz1NKCfA@mail.gmail.com","subject":"Re: Arguments to git hooks","fromName":"Marcus Karlsson","fromEmail":"mk@acc.umu.se","sentAt":"2012-05-06T21:11:21Z","receivedAt":"2012-05-06T21:11:21Z","isPatch":false,"sender":{"key":"mk@acc.umu.se","avatar":null},"body":"On Mon, May 07, 2012 at 12:48:35AM +0530, jaseem abid wrote:\n> On Mon, May 7, 2012 at 12:42 AM, Marcus Karlsson <mk@acc.umu.se> wrote:\n> > As far as I can see that's the only argument that git-commit passes on\n> > to the commit-msg hook. Can't you just call something like git-status\n> > from the hook or do you need the information passed specifically as an\n> > argument?\n> \n> I can always get last commit from `.git/COMMIT_EDITMSG'` - a fixed\n> file. Isn't passing that as an argument a bit pointless?\n\nThat's a good question. I don't know. If someone else knows the reason I\nsure would like to find out.\n\n> I want files I tried to commit specifically passed in as an argument.\n\nThere are many different reasons to use hooks. If everything that a hook\ncould potentially need was passed to it as an argument then a lot of\ndata would often end up unused. Better to supply as little as possible\nand only do more work when it's actually needed.\n\n> Are you suggesting me to run some plumbing command, parse the result\n> and get the required data ? Is there an easier way to get this done?\n\nI wouldn't go as deep as the plumbing, I think git status --porcelain\nshould work just fine, the output is fairly easy to parse.\n\n\tMarcus\n"},{"id":"190945","messageId":"CAH-tXsDe7Hsacz19egdeW2sDV3SoPkJpJLPAmk_pCoEiR3wvcQ@mail.gmail.com","threadId":"30449","inReplyTo":"20120506211120.GC5725@moj","subject":"Re: Arguments to git hooks","fromName":"jaseem abid","fromEmail":"jaseemabid@gmail.com","sentAt":"2012-05-06T21:40:07Z","receivedAt":"2012-05-06T21:40:07Z","isPatch":false,"sender":{"key":"jaseemabid@gmail.com","avatar":"https://gravatar.com/avatar/8b0432c96e4d3c8a9a96c9961ee842df7b7a869744da9c25187a51a992eabd81?d=mp&s=160"},"body":"On Mon, May 7, 2012 at 2:41 AM, Marcus Karlsson <mk@acc.umu.se> wrote:\n> There are many different reasons to use hooks. If everything that a hook\n> could potentially need was passed to it as an argument then a lot of\n> data would often end up unused. Better to supply as little as possible\n> and only do more work when it's actually needed.\n\nThat seems like a good reason not to pass all of the data, but isn't\nthe file names the most primary thing somebody can ask for?\n\n>> Are you suggesting me to run some plumbing command, parse the result\n>> and get the required data ? Is there an easier way to get this done?\n>\n> I wouldn't go as deep as the plumbing, I think git status --porcelain\n> should work just fine, the output is fairly easy to parse.\n\nI have always wondered why 'git status --porcelain' is giving a\n'plumbing' style output. Any docs on this somewhere?\n\n-- \nJaseem Abid\nhttp://jaseemabid.github.com\n"},{"id":"190963","messageId":"20120507071541.GA19874@sigill.intra.peff.net","threadId":"30449","inReplyTo":"CAH-tXsB4PBS_YjW4DCjT6ORmNPomQ8XMPbKx3hxVNH=FyB2u3g@mail.gmail.com","subject":"Re: Arguments to git hooks","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2012-05-07T07:15:41Z","receivedAt":"2012-05-07T07:15:41Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, May 07, 2012 at 12:05:51AM +0530, jaseem abid wrote:\n\n> I am trying to write a hook '.git/hooks/commit-msg' to be run before\n> every commit.\n> \n> How can I pass arguments to the script? Now by default the only arg I\n> am getting is `.git/COMMIT_EDITMSG'`. I would love to get the list of\n> files I tried to commit also into the script so that I can run a lint\n> program on it before committing it. How can I get this done?\n\nIt sounds like you want the \"pre-commit\" hook rather than \"commit-msg\".\n\nBut that aside, the solution is to use plumbing commands to examine the\nstate. You probably want \"git diff-index --cached --name-only\" to get\nthe list of files that are being committed.\n\n-Peff\n"},{"id":"190964","messageId":"20120507072123.GB19874@sigill.intra.peff.net","threadId":"30449","inReplyTo":"CAH-tXsDe7Hsacz19egdeW2sDV3SoPkJpJLPAmk_pCoEiR3wvcQ@mail.gmail.com","subject":"Re: Arguments to git hooks","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2012-05-07T07:21:23Z","receivedAt":"2012-05-07T07:21:23Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, May 07, 2012 at 03:10:07AM +0530, jaseem abid wrote:\n\n> On Mon, May 7, 2012 at 2:41 AM, Marcus Karlsson <mk@acc.umu.se> wrote:\n> > There are many different reasons to use hooks. If everything that a hook\n> > could potentially need was passed to it as an argument then a lot of\n> > data would often end up unused. Better to supply as little as possible\n> > and only do more work when it's actually needed.\n> \n> That seems like a good reason not to pass all of the data, but isn't\n> the file names the most primary thing somebody can ask for?\n\nYes, although keep in mind that the list of files does not necessarily\nfit onto the command line. We would have to feed it over stdin, and then\nyou would have parsing/quoting issues.\n\nBut you can get the exact same list from \"git diff-index --cached\n--name-only\", and it is exactly as hard to parse as stdin would be (and\nyou can even decide to use \"-z\" to eliminate the quoting issues).\n\nSo asking the user to call the plumbing command is more efficient and\nmore flexible, but not actually any harder to use.\n\n> > I wouldn't go as deep as the plumbing, I think git status --porcelain\n> > should work just fine, the output is fairly easy to parse.\n> \n> I have always wondered why 'git status --porcelain' is giving a\n> 'plumbing' style output. Any docs on this somewhere?\n\nIt is plumbing. The porcelain is meant to be \"output suitable for\nreading by porcelains\". Although I was the person who named \"git status\n--porcelain\", it is not a convention I think is particularly good; I\nnamed it to be consistent with other git commands which have a\n\"--porcelain\" mode.\n\n-Peff\n"},{"id":"191048","messageId":"4FA84A0F.6060608@pileofstuff.org","threadId":"30449","inReplyTo":"CAH-tXsB4PBS_YjW4DCjT6ORmNPomQ8XMPbKx3hxVNH=FyB2u3g@mail.gmail.com","subject":"Re: Arguments to git hooks","fromName":"Andrew Sayers","fromEmail":"andrew-git@pileofstuff.org","sentAt":"2012-05-07T22:17:51Z","receivedAt":"2012-05-07T22:17:51Z","isPatch":false,"sender":{"key":"andrew-git@pileofstuff.org","avatar":null},"body":"On 06/05/12 19:35, jaseem abid wrote:\n> Hello all,\n> \n> I am trying to write a hook '.git/hooks/commit-msg' to be run before\n> every commit.\n> \n> How can I pass arguments to the script? Now by default the only arg I\n> am getting is `.git/COMMIT_EDITMSG'`. I would love to get the list of\n> files I tried to commit also into the script so that I can run a lint\n> program on it before committing it. How can I get this done?\n\nFirst, a standard warning - consider using a pre-receive hook instead of\na pre-commit hook.  A lot of git's power comes from making commits as\ncheap as possible, so rules like \"no committing until your code is\npretty\" tend to stifle people.  For example, I often commit changes\nbefore running lint-type operations, then use `git add -p` and `git\ncheckout -p` to selectively accept/reject individual changes.  When I'm\ndone, I `git commit --amend` to pretend the original commit never\nhappened.  A pre-receive hook gives you most of the same guarantees as a\npre-commit hook with almost none of the cost.\n\nHaving said that, there are situations where pre-commit hooks are a good\nidea (like catching \"DO NOT COMMIT\" comments).  I've played with this a\nlittle before, and never found a very satisfactory solution.  Here are\nsome important cases:\n\n# git status will sometimes tell you the file that will be committed:\n# edit foo\ngit add foo\ngit commit\n\n\n# git status will sometimes need a bit of careful parsing:\n# edit foo\n# edit bar\ngit add foo\ngit commit\n\n\n# git status sometimes tells you the right file but the wrong contents:\n# edit foo\ngit add foo\n# edit foo again\ngit commit\n\n\n# but often git status will tell you the wrong file altogether:\n# edit foo\n# edit bar\ngit add foo\ngit commit bar\n\n\nThe best solution I've found is a `git commit` wrapper that does\nsomething like `CHANGES=\"$(git commit $@ --dry-run -v)\"` to get a\nreliable diff, then starts work from there.\n\n\t- Andrew\n"},{"id":"191527","messageId":"CAH-tXsBMYe2q6dZ6COyEH7N9n-mJh4yV8O50tgeZ1PeSEnZHvQ@mail.gmail.com","threadId":"30449","inReplyTo":"4FA84A0F.6060608@pileofstuff.org","subject":"Re: Arguments to git hooks","fromName":"jaseem abid","fromEmail":"jaseemabid@gmail.com","sentAt":"2012-05-14T20:34:19Z","receivedAt":"2012-05-14T20:34:19Z","isPatch":false,"sender":{"key":"jaseemabid@gmail.com","avatar":"https://gravatar.com/avatar/8b0432c96e4d3c8a9a96c9961ee842df7b7a869744da9c25187a51a992eabd81?d=mp&s=160"},"body":"On Tue, May 8, 2012 at 3:47 AM, Andrew Sayers\n<andrew-git@pileofstuff.org> wrote:\n> On 06/05/12 19:35, jaseem abid wrote:\n>> Hello all,\n>>\n>> I am trying to write a hook '.git/hooks/commit-msg' to be run before\n>> every commit.\n>>\n>> How can I pass arguments to the script? Now by default the only arg I\n>> am getting is `.git/COMMIT_EDITMSG'`. I would love to get the list of\n>> files I tried to commit also into the script so that I can run a lint\n>> program on it before committing it. How can I get this done?\n>\n> First, a standard warning - consider using a pre-receive hook instead of\n> a pre-commit hook.\n\nI am trying to get a lint, commit message spell checker, trailing\nwhitespace check in code etc work on my *local machine* before\ncommitting. pre-receive works in the server right? Its also time I\nneed to seriously consider a pre-commit hook to \"Reject commits made\nbetween 4am and 7am with a note to go to bed.\"\n\n> A lot of git's power comes from making commits as\n> cheap as possible, so rules like \"no committing until your code is\n> pretty\" tend to stifle people.\n\nIts ok since I am the only one to use it because I want to make my\ncommits cleaner and better.  There is always --no-verify for skipping\nhooks.\n\n> The best solution I've found is a `git commit` wrapper that does\n> something like `CHANGES=\"$(git commit $@ --dry-run -v)\"` to get a\n> reliable diff, then starts work from there.\n\nIsn't `git commit $@ --dry-run --porcelain` better for parsing or am I\nmissing something ?\n\n-- \nJaseem Abid\nhttp://jaseemabid.github.com\n"}]}