{"thread":{"id":"35531","subject":"Looking for pre-commit hook to check whitespace errors but not for all files","startedAt":"2013-12-14T15:28:14Z","lastAt":"2013-12-16T19:06:59Z","messageCount":3,"participants":["Paul Menzel","John Keeping","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"232026","messageId":"1387034894.4636.9.camel@mattotaupa","threadId":"35531","inReplyTo":null,"subject":"Looking for pre-commit hook to check whitespace errors but not for all files","fromName":"Paul Menzel","fromEmail":"paulepanter@users.sourceforge.net","sentAt":"2013-12-14T15:28:14Z","receivedAt":"2013-12-14T15:28:14Z","isPatch":false,"sender":{"key":"paulepanter@users.sourceforge.net","avatar":null},"body":"Dear git folks,\n\n\nin coreboot we try to check for whitespace errors before committing. Of\ncourse a pre-commit hook is the way to go, but unfortunately it is not\nso simple (at least for me) as the following requirements exist.\n\n1. Only the files actually committed should be checked. That means\nrunning `git commit -a`, abort that and then running `git commit\nsome/file` should only check `some/file` for whitespace errors.\n\n2. There are certain files that are allowed to have whitespace errors.\nIn our case these are `*.patch` and `*.diff` files which by design seem\nto contain whitespace error.\n\nCurrently the whole tree is checked, which takes a lot of time [1].\n\nI tried to come up with a patch [2], but failed so far. Best would be to\nhave\n\n    $ git diff --check --only-committed-files --exclude \"*patch$\"\n\nwhere I could not find a way for the last to switches.\n\nCurrently, I would use\n\n    $ git diff-index --cached --name-only $against -- | grep -v patch$\n\nand pass that list to some whitespace check program. Unfortunately that\nstill does not fulfill the first requirement.\n\nWhat am I missing to solve this elegantly?\n\n\nThanks,\n\nPaul\n\n\n[1] http://review.coreboot.org/gitweb?p=coreboot.git;a=blob;f=util/lint/lint-stable-003-whitespace;h=1e824a1e76e37ce641b91c75f2f78e0e2abfd37f;hb=HEAD\n[2] http://review.coreboot.org/#/c/3340/\n"},{"id":"232027","messageId":"20131214165914.GJ3163@serenity.lan","threadId":"35531","inReplyTo":"1387034894.4636.9.camel@mattotaupa","subject":"Re: Looking for pre-commit hook to check whitespace errors but not for all files","fromName":"John Keeping","fromEmail":"john@keeping.me.uk","sentAt":"2013-12-14T16:59:15Z","receivedAt":"2013-12-14T16:59:15Z","isPatch":false,"sender":{"key":"john@keeping.me.uk","avatar":"https://avatars.githubusercontent.com/u/1702081?v=4"},"body":"On Sat, Dec 14, 2013 at 04:28:14PM +0100, Paul Menzel wrote:\n> in coreboot we try to check for whitespace errors before committing. Of\n> course a pre-commit hook is the way to go, but unfortunately it is not\n> so simple (at least for me) as the following requirements exist.\n> \n> 1. Only the files actually committed should be checked. That means\n> running `git commit -a`, abort that and then running `git commit\n> some/file` should only check `some/file` for whitespace errors.\n> \n> 2. There are certain files that are allowed to have whitespace errors.\n> In our case these are `*.patch` and `*.diff` files which by design seem\n> to contain whitespace error.\n> \n> Currently the whole tree is checked, which takes a lot of time [1].\n> \n> I tried to come up with a patch [2], but failed so far. Best would be to\n> have\n> \n>     $ git diff --check --only-committed-files --exclude \"*patch$\"\n> \n> where I could not find a way for the last to switches.\n> \n> Currently, I would use\n> \n>     $ git diff-index --cached --name-only $against -- | grep -v patch$\n> \n> and pass that list to some whitespace check program. Unfortunately that\n> still does not fulfill the first requirement.\n> \n> What am I missing to solve this elegantly?\n\nI think you want to combine .gitattributes with something like the\ndiff-index above:\n\n\t$ cat >.gitattributes <<-EOF\n\t*.patch whitespace=false\n\t*.diff whitespace=false\n\tEOF\n\n\t$ git diff-index --check --cached $against --\n"},{"id":"232062","messageId":"xmqq8uvkpqcs.fsf@gitster.dls.corp.google.com","threadId":"35531","inReplyTo":"1387034894.4636.9.camel@mattotaupa","subject":"Re: Looking for pre-commit hook to check whitespace errors but not for all files","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-12-16T19:06:59Z","receivedAt":"2013-12-16T19:06:59Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul Menzel <paulepanter@users.sourceforge.net> writes:\n\n> 1. Only the files actually committed should be checked. That means\n> running `git commit -a`, abort that and then running `git commit\n> some/file` should only check `some/file` for whitespace errors.\n\nDoesn't hooks/pre-commit.sample we ship already gets this right?\n\n\t$ git init foobar && cd foobar\n\t$ mv .git/hooks/pre-commit.sample .git/hooks/pre-commit\n        $ for i in foo bar; do echo $i >$i.txt; done\n        $ git commit -m initial\n        $ for i in foo bar; do echo \"$i \" >$i.txt; done\n        $ git commit -a\n        bar.txt:1: trailing whitespace.\n        +bar\n        foo.txt:1: trailing whitespace.\n        +foo\n        $ git commit foo.txt\n        foo.txt:1: trailing whitespace.\n        +foo\n\n> 2. There are certain files that are allowed to have whitespace errors.\n\nAs John Keeping alraedy pointed out, you can use the attributes\nmechanism to mark what kind of payload each path has to control\nthis kind of thing.\n"}]}