{"thread":{"id":"45620","subject":"how-to get commit content with pre-receive hook ?","startedAt":"2017-04-06T14:11:50Z","lastAt":"2017-04-06T20:14:06Z","messageCount":4,"participants":["Eric Belhomme","Ævar Arnfjörð Bjarmason","Stefan Beller","Igor Djordjevic"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"316278","messageId":"955c152a-b278-ecfd-8bc7-671c3aa81dc6@ricozome.net","threadId":"45620","inReplyTo":null,"subject":"how-to get commit content with pre-receive hook ?","fromName":"Eric Belhomme","fromEmail":"rico-ml@ricozome.net","sentAt":"2017-04-06T14:03:19Z","receivedAt":"2017-04-06T14:11:50Z","isPatch":false,"sender":{"key":"rico-ml@ricozome.net","avatar":null},"body":"Hello the list,\n\nUntil now I ever had a quite \"basic\" Git usage, but now I'm working on a \nproject based on Git hooks feature.. and I'm a very beginner with Git \nhooks !\n\nMy need consist doing a syntax check on submitted files before a 'git \npush'. So the right hook is 'pre-receive' and I'm already able to \nidentify the files I want to check using 'git show'.\n\nBut I don't know how to get the *content* of the file being submitted to \nrun my syntax check rules against it !\n\nI googled but most examples using pre-receive I found are doing sanity \ncheck on enveloppe but never on actual content of the file !\n\nCould someone here put me on the rails ?\n\nRegards,\n\n-- \nEric\n\n"},{"id":"316296","messageId":"CACBZZX7xA92e5n=E8fdGHiBHaqzRjP_MVWsaE2v=DcVmCO2r0w@mail.gmail.com","threadId":"45620","inReplyTo":"955c152a-b278-ecfd-8bc7-671c3aa81dc6@ricozome.net","subject":"Re: how-to get commit content with pre-receive hook ?","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-04-06T18:38:19Z","receivedAt":"2017-04-06T18:38:45Z","isPatch":false,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"On Thu, Apr 6, 2017 at 4:03 PM, Eric Belhomme <rico-ml@ricozome.net> wrote:\n> Until now I ever had a quite \"basic\" Git usage, but now I'm working on a\n> project based on Git hooks feature.. and I'm a very beginner with Git hooks\n> !\n>\n> My need consist doing a syntax check on submitted files before a 'git push'.\n> So the right hook is 'pre-receive' and I'm already able to identify the\n> files I want to check using 'git show'.\n>\n> But I don't know how to get the *content* of the file being submitted to run\n> my syntax check rules against it !\n>\n> I googled but most examples using pre-receive I found are doing sanity check\n> on enveloppe but never on actual content of the file !\n>\n> Could someone here put me on the rails ?\n>\n> Regards,\n\nAs documented in githooks(5), the hook gets a list of these on stdin:\n\n    <old-value> SP <new-value> SP <ref-name> LF\n\nThat means that you can use any git command to inspect that ref range, e.g.:\n\n    git log -p <old>..<new>\n    git diff <old>..<new>\n\nOr (pseudocode):\n\n    git show <commit>:<some path> for $(git rev-list <old>..<new>)\n\nThis is no different than how you'd inspect the content in your git\nrepo, e.g. to get the README.md content of the master branch of\ngit.git:\n\n    $ git ls-tree master|grep README\n    100644 blob f17af66a97c8097ab91f074478c4a5cb90425725    README.md\n    $ git cat-file blob f17af66a97c8097ab91f074478c4a5cb90425725|wc\n         61     397    3001\n"},{"id":"316297","messageId":"CAGZ79kYByeaX0qhRhy0RDq5hvuyEhhDcgWS7GABi6Nr+k-Ekdw@mail.gmail.com","threadId":"45620","inReplyTo":"CACBZZX7xA92e5n=E8fdGHiBHaqzRjP_MVWsaE2v=DcVmCO2r0w@mail.gmail.com","subject":"Re: how-to get commit content with pre-receive hook ?","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2017-04-06T18:48:35Z","receivedAt":"2017-04-06T18:48:41Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":">>\n>> But I don't know how to get the *content* of the file being submitted to run\n>> my syntax check rules against it !\n>>\n\n>     git log -p <old>..<new>\n>     git diff <old>..<new>\n\nor in case of the tool requiring a full worktree\n\n    mkdir /tmp/test\n    GIT_WORKTREE=/tmp/test git checkout -f <new>\n    and then perform the check on that full tree?\n"},{"id":"316299","messageId":"0dec12a8-b3d2-41e5-4563-4bec2a6df7be@gmail.com","threadId":"45620","inReplyTo":"955c152a-b278-ecfd-8bc7-671c3aa81dc6@ricozome.net","subject":"Re: how-to get commit content with pre-receive hook ?","fromName":"Igor Djordjevic","fromEmail":"igor.d.djordjevic@gmail.com","sentAt":"2017-04-06T20:13:55Z","receivedAt":"2017-04-06T20:14:06Z","isPatch":false,"sender":{"key":"igor.d.djordjevic@gmail.com","avatar":null},"body":"Hello Eric,\n\nOn 06/04/2017 16:03, Eric Belhomme wrote:\n> Until now I ever had a quite \"basic\" Git usage, but now I'm working \n> on a project based on Git hooks feature.. and I'm a very beginner \n> with Git hooks !\n> \n> My need consist doing a syntax check on submitted files before a 'git \n> push'. So the right hook is 'pre-receive' and I'm already able to \n> identify the files I want to check using 'git show'.\n> \n> But I don't know how to get the *content* of the file being submitted \n> to run my syntax check rules against it !\n> \n> I googled but most examples using pre-receive I found are doing \n> sanity check on enveloppe but never on actual content of the file !\n> \n> Could someone here put me on the rails ?\n\nI`m yet another beginner with both Git hooks and Bash scripting, but \nI`ve managed to patch something up that might give you an idea, just \ndrop it inside your \"pre-receive\" hook file:\n\n\twhile read oldrev newrev\n\tdo\n\t\tfor commit in $(git rev-list --reverse $oldrev..$newrev)\n\t\tdo\n\t\t\tfor file in $(git diff-tree --no-commit-id --name-only -r $commit)\n\t\t\tdo\n\t\t\t\techo \"$(git show $commit:$file)\" >&1\n\t\t\tdone\n\t\tdone\n\tdone\n\texit 1\n\nThis should reject any push attempt, returning back *content* of each \nchanged file for each new commit.\n\nFeel free to adapt as needed, like processing/checking file content \ninstead of sending it over, allowing or rejecting the push \naccordingly, and also handling corner cases (for example, \"oldrev\" \nvalue of 0000... in case of a new ref creation).\n\nI guess there might be better ways (comments welcome), but this \nshould at least get you going... and I`ve learned something new as \nwell ;)\n\nRegards,\nBuga\n"}]}