All of lore.kernel.org
 help / color / mirror / Atom feed
* how-to get commit content with pre-receive hook ?
@ 2017-04-06 14:03 Eric Belhomme
  2017-04-06 18:38 ` Ævar Arnfjörð Bjarmason
  2017-04-06 20:13 ` Igor Djordjevic
  0 siblings, 2 replies; 4+ messages in thread
From: Eric Belhomme @ 2017-04-06 14:03 UTC (permalink / raw)
  To: git

Hello the list,

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 ?

Regards,

-- 
Eric


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how-to get commit content with pre-receive hook ?
  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
  1 sibling, 1 reply; 4+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2017-04-06 18:38 UTC (permalink / raw)
  To: Eric Belhomme; +Cc: Git Mailing List

On Thu, Apr 6, 2017 at 4:03 PM, Eric Belhomme <rico-ml@ricozome.net> 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 ?
>
> Regards,

As documented in githooks(5), the hook gets a list of these on stdin:

    <old-value> SP <new-value> SP <ref-name> LF

That means that you can use any git command to inspect that ref range, e.g.:

    git log -p <old>..<new>
    git diff <old>..<new>

Or (pseudocode):

    git show <commit>:<some path> for $(git rev-list <old>..<new>)

This is no different than how you'd inspect the content in your git
repo, e.g. to get the README.md content of the master branch of
git.git:

    $ git ls-tree master|grep README
    100644 blob f17af66a97c8097ab91f074478c4a5cb90425725    README.md
    $ git cat-file blob f17af66a97c8097ab91f074478c4a5cb90425725|wc
         61     397    3001

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how-to get commit content with pre-receive hook ?
  2017-04-06 18:38 ` Ævar Arnfjörð Bjarmason
@ 2017-04-06 18:48   ` Stefan Beller
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Beller @ 2017-04-06 18:48 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: Eric Belhomme, Git Mailing List

>>
>> But I don't know how to get the *content* of the file being submitted to run
>> my syntax check rules against it !
>>

>     git log -p <old>..<new>
>     git diff <old>..<new>

or in case of the tool requiring a full worktree

    mkdir /tmp/test
    GIT_WORKTREE=/tmp/test git checkout -f <new>
    and then perform the check on that full tree?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how-to get commit content with pre-receive hook ?
  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 20:13 ` Igor Djordjevic
  1 sibling, 0 replies; 4+ messages in thread
From: Igor Djordjevic @ 2017-04-06 20:13 UTC (permalink / raw)
  To: Eric Belhomme, git

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-04-06 20:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.