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

Re: pre-receive hook ; how to detect file and append code ?

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 22, 2012, 18:02 UTC
Message-ID
<7v395nql6m.fsf@alter.siamese.dyndns.org>
In-Reply-To
<20120622183709.2f0beaa6@shiva.selfip.org>
"J. Bakshi" <joydeep.bakshi@infoservices.in> writes:
> How can I detect a specific file during push, if it is modified or not ?
> And how can I then append a block of code into that file; if modified
> during push ? I think pre-receive hook is the best for this operation.

The purpose of the "pre-receive" hook is to either approve or reject a proposed change made by the push. When it runs, the repository already has received all the objects necessary to inspect the proposed change locally (from the receiving repository's point of view).

A proposed change comes in the form of <old> <new> <refname> that says "The pusher saw <old> at <refname> when he started this push, and it wants to update the ref to <new>". So any of the following standard techniques (note: not exhaustive) to inspect the changes between <old> and <new> can be used:

    # are log messages sane?
    git log <old>..<new>
    # are the changes sane?
    git diff <old>..<new>
    # does it leave forbidden paths intact
    git diff --name-only <old>..<new>

If your script likes the proposed change, exit with 0. If it does not, exit with non-zero.

That is all it can do. It is not supposed to change <new> to some other value, and there is no interface to do so.

If you want to rewrite the history pushed into the repository after a push is accepted, you would want to run it from post-receive or something. All the usual warning about rewriting published history will apply if you do so, however. After all, the history is already published immediately before post-recieve (or post-update) runs.

Previous: Thomas AdamNext: J. Bakshi
Message 3 of 4 in “pre-receive hook ; how to detect file and append code ?”
  1. J. BakshiJun 22, 2012
  2. Thomas AdamJun 22, 2012
  3. Junio C HamanoJun 22, 2012
  4. J. BakshiJun 25, 2012

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.