I have a central git repo set up using gitolite.
I want to set up a hook such that whenever a user pushes to the repo, it performs a pull elsewhere followed by some automated testing.
So far, I only want to it perform the pull.
In the hooks directory I created the following script names post-update:
#!/bin/sh
cd /home/git/www/epicac
git pull
When I invoke this script using ./post-update, it does exactly what I want.
However, whenever it's invoked automatically as I hook, I get: fatal: Not a git repository: '.'
Any idea why this might be happening?
The post-commit hook is called immediately after the commit-msg hook. It can't change the outcome of the git commit operation, so it's used primarily for notification purposes. The script takes no parameters and its exit status does not affect the commit in any way.
Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.
If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook.
No. Hooks are per-repository and are never pushed.
You have various diagnostics to run as suggested in this SO answer.
In particular, check out the the value of GIT_DIR
and GIT_WORK_TREE
.
While the hook is running,
GIT_DIR
and (if the worktree can't be inferred fromGIT_DIR
)GIT_WORK_TREE
are set.
That means your pull won't run with the repository in the directory you changed to.
See also blog post Using Git Inside a Git Hook:
Eventually we got our linux guru over and he noticed that the environment under which the git user runs is totally different when inside a hook.
Gitolite does a bunch of things to the env, but the one that was screwing us up was the setting of theGIT_DIR
.
After we figured that out, the solution was as easy as:
ENV.delete 'GIT_DIR'
in our ruby script that is triggered by the '
post-receive
' hook.
Same deal in Git Tip: Auto update working tree via post-receive hook, but with an elegant way out of this:
The solution?
It turns out thepost-receive
hook starts out with theGIT_DIR
environment variable set to therepo/.git
folder, so no matter what path you 'cd' into it will always try to run any following git commands there.
Fixing this is simply a matter of unsetting theGIT_DIR
(thanks to Ulrich Petri for the elegantenv -i
solution):
#!/bin/sh
cd ..
env -i git reset --hard
How about specifying the --git-dir
.
#!/bin/sh
cd /home/git/www/epicac
git --git-dir=.git pull
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With