git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Igor Djordjevic <igor.d.djordjevic@gmail.com>
To: Eric Belhomme <rico-ml@ricozome.net>, git@vger.kernel.org
Subject: Re: how-to get commit content with pre-receive hook ?
Date: Thu, 6 Apr 2017 22:13:55 +0200	[thread overview]
Message-ID: <0dec12a8-b3d2-41e5-4563-4bec2a6df7be@gmail.com> (raw)
In-Reply-To: <955c152a-b278-ecfd-8bc7-671c3aa81dc6@ricozome.net>

Hello Eric,

On 06/04/2017 16:03, Eric Belhomme wrote:
> Until now I ever had a quite "basic" Git usage, but now I'm working 
> on a project based on Git hooks feature.. and I'm a very beginner 
> with Git hooks !
> 
> My need consist doing a syntax check on submitted files before a 'git 
> push'. So the right hook is 'pre-receive' and I'm already able to 
> identify the files I want to check using 'git show'.
> 
> But I don't know how to get the *content* of the file being submitted 
> to run my syntax check rules against it !
> 
> I googled but most examples using pre-receive I found are doing 
> sanity check on enveloppe but never on actual content of the file !
> 
> Could someone here put me on the rails ?

I`m yet another beginner with both Git hooks and Bash scripting, but 
I`ve managed to patch something up that might give you an idea, just 
drop it inside your "pre-receive" hook file:

	while read oldrev newrev
	do
		for commit in $(git rev-list --reverse $oldrev..$newrev)
		do
			for file in $(git diff-tree --no-commit-id --name-only -r $commit)
			do
				echo "$(git show $commit:$file)" >&1
			done
		done
	done
	exit 1

This should reject any push attempt, returning back *content* of each 
changed file for each new commit.

Feel free to adapt as needed, like processing/checking file content 
instead of sending it over, allowing or rejecting the push 
accordingly, and also handling corner cases (for example, "oldrev" 
value of 0000... in case of a new ref creation).

I guess there might be better ways (comments welcome), but this 
should at least get you going... and I`ve learned something new as 
well ;)

Regards,
Buga

      parent reply	other threads:[~2017-04-06 20:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-06 14:03 how-to get commit content with pre-receive hook ? Eric Belhomme
2017-04-06 18:38 ` Ævar Arnfjörð Bjarmason
2017-04-06 18:48   ` Stefan Beller
2017-04-06 20:13 ` Igor Djordjevic [this message]

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0dec12a8-b3d2-41e5-4563-4bec2a6df7be@gmail.com \
    --to=igor.d.djordjevic@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=rico-ml@ricozome.net \
    /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.
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 NNTP newsgroup(s).