6

Is there a way to change the file that is being pushed to the server using a server-side pre-receive hook?

Say I want to add something to the end of a file like:

//End of Org

each time someone pushes to my repo.

Is there a way you can change the file coming in using git hooks?

Frank S. Thomas
  • 4,569
  • 2
  • 25
  • 47
beast
  • 61
  • 1
  • 2

2 Answers2

4

I would rather use a filter driver which can operate on the content of each file in order to check if your line is there and add it if not, during the checkout step.

alt text

That would be:

  • a smudge script
  • able to be replicated when your repo is cloned (as opposed to hooks which are not copied over when cloning a repo unless you use a template directory)
Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • How do I get the filter driver copied to each repo when a clone/pull is done? – beast Sep 01 '10 at 21:13
  • @beast: a filter driver is a `.gitattributes` file, managed like any other files, and pushed/pulled like any other file. The smudge script it references can also be versioned. – VonC Sep 01 '10 at 21:34
  • right so I have the .gitattributes file setup and in the repo. I have a *.c filter=test in the .gitattributes file. How do I get so that [filter "test"] clean = cat smudge = cat is copied to each repo clone/pull so that I don't have to do git config filter.test.smudge cat each time I clone the repo? If you cannot tell I am fairly new to git and trying to get the same config across multiple computers. – beast Sep 01 '10 at 21:54
  • @beast: You need to replay the setup for your config (unless you are cloning on the same server, in which case a `git config --global` is enough). I usually keep the config commands in a version file, for me to replay after a clone. – VonC Sep 02 '10 at 02:29
  • @VonC could you check this! https://stackoverflow.com/questions/45931921/how-to-add-the-pre-receive-hook-in-server-side-in-gitlab –  Aug 29 '17 at 06:21
3

Just for completeness sake: it should be rather obvious from the name that what you are asking for, is simply impossible. The pre-receive hook cannot change any files, because it hasn't received any yet!

Jörg W Mittag
  • 337,159
  • 71
  • 413
  • 614