9

Is there a good way I can share the code in .git/hooks?

The only thing I can think of is to create command line tools, and call those tools from each script in .git/hooks/*

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    I'm doing it exactly as you've mentioned. On the other hand I'm trying to use as many "remote side" hooks as possible - then there is no problem with sharing them. – Marcin Pietraszek Sep 12 '18 at 04:06
  • Thanks, what do you mean by "remote side" hooks? – Alexander Mills Sep 12 '18 at 04:07
  • You can either set up a client-side hook (which is difficult to maintain, since there is no easy/default way to share them) or a server-side hook: a hook that resides in the remote repository, that is usually activated via `git push` actions. The advantage is that server side hooks do not need to be deployed to every developers machine. Alas, server side hooks can of course only react to remote repository interactions. Read more about hooks [here](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). – kowsky Sep 12 '18 at 05:10
  • Well a pre-commit hook won't work server side, right? – Alexander Mills Sep 12 '18 at 05:13
  • No, but you can validate commits prior to accepting them on remote. In other words you could reject them if something is iffy. That will force committer to fix them prior to pushing to remote. – Marcin Pietraszek Sep 12 '18 at 05:17
  • As of git 2.9, there's the core.hooksPath setting; [linking this SO](https://stackoverflow.com/q/427207/6069586) – JWCS Dec 02 '21 at 22:43

3 Answers3

7

Create a git repo on github. Clone it to your fs. Symlink the hooks files to .git/hooks/*, or better yet, use:

git config core.hooksPath .githooks

which will tell .git to use the .githooks folder in your project to look up the hooks, instead of .git/hooks

3

You can either:

  • version your pre-commit script in your own repo (git add --chmod=+x) and describe in the README of said repo what a user needs to do to activate it (for instance a symbolic link from .git/hooks/pre-commit to your script)
  • or setup a git repo template, provided all the users of that repo have access to a common shared path.
Calum Halpin
  • 1,945
  • 1
  • 10
  • 20
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

My suggestion: https://githooks.com/

There are a few ways to manage hooks. I ended up creating one for myself and made it public weeks ago: https://github.com/lovato/hooks4git

In my approach, there are no global scripts (like other tools or even git properly configured). Once the tool is installed and activated on your repo, you commit your scripts along with your code.

In any approach, you can link files to an external repo, and have all your scripts shared, without actually needing to touch .git/hooks folder directly.

Lovato
  • 2,250
  • 1
  • 16
  • 22