问题
I am setting up an environment where I can push changes done locally on my workstation to "Hub" repository (which is a remote) and then a post-update hook will automatically update my staging web site, which is the "Prime". I'm doing this so that I can see my changes get automatically reflected in my staging site when I push to my remote hub.
This set up is based on this article. My server is Windows 2012 and I'm using MSysGit on both the server and my workstation (Windows 7).
Everything is working fine when I do everything manually -- push from my local repo to the hub and then manually fetch/merge into the prime. My problem is when I try to have the fetch/merge be done by a post-update hook.
When I commit a change to my hub, the output I get is:
c:\Code4X\GIT\stage>git push stage master
Counting objects: 11, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 518 bytes, done.
Total 6 (delta 5), reused 0 (delta 0)
remote:
remote: **** Pulling changes into Live [Stage's post-update hook]
remote:
remote: hooks/post-update: line 12: cd: /c/websites/myproject.staging: No
such file or directory
To w:/stage.git
ce1e2ea..93e11f4 master -> master
My post-update hook is:
#!/bin/sh
echo
echo "**** Pulling changes into Live [Stage's post-update hook]"
echo
cd /c/websites/myproject.staging || exit
unset GIT_DIR
git pull stage master
exec git update-server-info
I'm able to fire up a bash cmd shell and cd to the directory without problem, so I'm not sure what the problem is.
I've also tried:
- including git\cmd in my PATH
- enclosing the path I'm trying to cd to in quotes (")
- having my shebang point to #!/c/progra~1/git/bin/sh
Hopefully it's something obvious. Any thoughts as to what the problem is?
回答1:
I just got through doing something somewhat similar on one of my projects. It looks like you have the right idea.
Try using relative paths instead.
#!/bin/sh
unset GIT_DIR
cd ..
# You are now in the project root
cd ../project.prime
git pull stage master
I figured I'd let you know what I'm doing, as this may help you out. This script will force update the working copy every time the repo gets updated:
#!/bin/sh
unset GIT_DIR
cd ..
git checkout -f
回答2:
The article you're using as a reference appears to assume that the hub and prime live on the same host. Perhaps you have those on separate hosts, thereby meaning the the post-update hook is running where there really is no /c/websites/myproject.staging (it executes on the remote receiving the push)?
Furthermore, while the author's advice to avoid pushing to a non-bare repository is generally valid, scripting a pull in this manner is roughly equivalent to doing so, so perhaps change that to chain the push to a remote on hub (that points to prime) within post-update? That will allow it to work with hub and prime residing on separate hosts and remove the need navigate directories in the hook altogether.
回答3:
I found the answer here: precede any git command run in post-hook with "env -i". Thus:
cd /path/to/other/repo && env -i git pull
The reason, as detailed in the post, is that the post-hook command runs with the GIT_DIR environment variable set, so any git commands that are run look at your old repo, not the one you're in. "env -i" is one way of unsetting that variable.
来源:https://stackoverflow.com/questions/13942116/post-update-git-hook-not-finding-directory