git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: how-to get commit content with pre-receive hook ?

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Apr 6, 2017, 18:38 UTC
Message-ID
<CACBZZX7xA92e5n=E8fdGHiBHaqzRjP_MVWsaE2v=DcVmCO2r0w@mail.gmail.com>
In-Reply-To
<955c152a-b278-ecfd-8bc7-671c3aa81dc6@ricozome.net>
On Thu, Apr 6, 2017 at 4:03 PM, Eric Belhomme <rico-ml@ricozome.net> wrote:
Show 17 quoted lines
> 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
Previous: Eric BelhommeNext: Stefan Beller
Message 2 of 4 in “how-to get commit content with pre-receive hook ?”
  1. Eric BelhommeApr 6, 2017
  2. Ævar Arnfjörð BjarmasonApr 6, 2017
  3. Stefan BellerApr 6, 2017
  4. Igor DjordjevicApr 6, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.