Skip to contentSkip to conversation

post-update to stash after push to non-bare current branch

DISCUSSION Neal Kreitzinger 3 participants 10 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

We use the worktree of git-repos as the webroot for virtual hosts assigned to ports so we can directly test changes to dev and test git-repos. We have some developers who want to develop offline on laptops and push to these non-bare repos so they can test their changes. My plan is to set receive.denyCurrentBranch = warn, and then use the post-update hook on the remote non-bare to do a stash of the worktree and index. My assumption is that post-update hook only executes after a successful push. Correct? I only want to stash the non-bare remote work-tree and index after a successful push to it (effectively doing a git-reset --hard, but also keeping any changes to the worktree/index of the non-bare remote as a safety in case someone does directly make uncommitted changes on the non-bare remote.)

v/r,
neal

#1 lore raw
Quoted 4 lines · Neal Kreitzinger

We use the worktree of git-repos as the webroot for virtual hosts assigned to ports so we can directly test changes to dev and test git-repos. We have some developers who want to develop offline on laptops and push to these non-bare repos so they can test their changes. My plan is to set

Quoted 9 lines

receive.denyCurrentBranch = warn, and then use the post-update hook on the remote non-bare to do a stash of the worktree and index. My assumption is that post-update hook only executes after a successful push. Correct? I only want to stash the non-bare remote work-tree and index after a successful push to it (effectively doing a git-reset --hard, but also keeping any changes to the worktree/index of the non-bare remote as a safety in case someone does directly make uncommitted changes on the non-bare remote.)

If I manually run git-stash on the non-bare remote after pushing to it from a clone (receive.denyCurrentBranch=warn) it works as expected and leaves the worktree and index matching the new HEAD. However, when post-update runs the git-stash is leaves the worktree dirty. (git 1.7.1)

v/r,
neal

#2 lore raw
Quoted 18 lines · Neal Kreitzinger
Quoted 8 lines · Neal Kreitzinger

We use the worktree of git-repos as the webroot for virtual hosts assigned
to ports so we can directly test changes to dev and test git-repos. We have
some developers who want to develop offline on laptops and push to these non-bare repos so they can test their changes. My plan is to set receive.denyCurrentBranch = warn, and then use the post-update hook on the

Quoted 9 lines

remote non-bare to do a stash of the worktree and index. My assumption is that post-update hook only executes after a successful push. Correct? I only want to stash the non-bare remote work-tree and index after a successful push to it (effectively doing a git-reset --hard, but also keeping any changes to the worktree/index of the non-bare remote as a safety
in case someone does directly make uncommitted changes on the non-bare remote.)

Quoted 5 lines

If I manually run git-stash on the non-bare remote after pushing to it from a clone (receive.denyCurrentBranch=warn) it works as expected and leaves the worktree and index matching the new HEAD. However, when post-update runs the git-stash is leaves the worktree dirty. (git 1.7.1)

hooks/post-update was:

git stash save
echo "worktree+index of non-bare remote current branch stashed for safety"

it created the stash and echoed the message. However, as stated earlier, the worktree is still dirty. I then manually run git-reset --hard and that makes it clean (worktree, index, HEAD match). However, if I add that to the hook it still leaves dirty worktree (index matches HEAD, but worktree doesn't match index)

hooks/post-update is:

git stash save
echo "worktree+index of non-bare remote current branch stashed for safety" git reset --hard
echo "git-reset --hard on current remote branch to ensure clean state"

message is echoed, but git-reset --hard does not appear to have really worked. (git 1.7.1)

v/r,
neal

#3 lore raw
Quoted 9 lines · Neal Kreitzinger

hooks/post-update is:

git stash save
echo "worktree+index of non-bare remote current branch stashed for safety" git reset --hard
echo "git-reset --hard on current remote branch to ensure clean state"

message is echoed, but git-reset --hard does not appear to have really worked. (git 1.7.1)

Have you checked where in the filesystem hierarchy that script is run (hint: pwd)?

Also it is unclear why you keep saying "stash". What kind of changes are you expecting to be saved to the stash? Will they be changes that are not source controlled that you would rather not to see? In other words, after running "stash" every time somebody pushes and having accumulated many stash entries, when do you plan to pop these stashed changes?

I would have expect that such a repository to reject a push if the working tree is dirty, and run checkout in post-update, though.

#4 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Neal Kreitzinger

hooks/post-update is:

git stash save
echo "worktree+index of non-bare remote current branch stashed for safety" git reset --hard
echo "git-reset --hard on current remote branch to ensure clean state"

Quoted 6 lines
Quoted 2 lines

message is echoed, but git-reset --hard does not appear to have really worked. (git 1.7.1)

Have you checked where in the filesystem hierarchy that script is run (hint: pwd)?

echo pwd in post-update echoes /path/WORKTREE/.git in git-push stdout.

Quoted 6 lines

Also it is unclear why you keep saying "stash". What kind of changes are you expecting to be saved to the stash? Will they be changes that are not source controlled that you would rather not to see? In other words, after running "stash" every time somebody pushes and having accumulated many stash entries, when do you plan to pop these stashed changes?

good point. rejection of dirty worktree is a cleaner safety.

Quoted 3 lines

I would have expect that such a repository to reject a push if the working tree is dirty, and run checkout in post-update, though.

'git-checkout -f' works manually, but in post-update hook it leaves behind dirty worktree, ie. index and HEAD match, but worktree still matches HEAD@{1}. This is the same undesired result git-stash and git-reset --hard leave behind when executed in post-update hook.

v/r,
neal

#5 lore raw
Quoted 6 lines · Neal Kreitzinger
Quoted 3 lines

Have you checked where in the filesystem hierarchy that script is run (hint: pwd)?

echo pwd in post-update echoes /path/WORKTREE/.git in git-push stdout.
...
'git-checkout -f' works manually, but in post-update hook...

Stronger hint. Did you run "git checkout -f" in /path/WORKTREE/.git to back that "works manually" claim?

#6 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Neal Kreitzinger
Quoted 3 lines

Have you checked where in the filesystem hierarchy that script is run (hint: pwd)?

echo pwd in post-update echoes /path/WORKTREE/.git in git-push stdout.
...
'git-checkout -f' works manually, but in post-update hook...

Stronger hint. Did you run "git checkout -f" in /path/WORKTREE/.git to back that "works manually" claim?

(Manual behavior):
If pwd is WORKTREE/ then git-checkout has correct effect, ie. worktree, index, and HEAD match. If pwd is WORKTREE/.git/ then git-checkout complains 'must be run in a worktree' and has no effect.

(post-update hook behavior):
If pwd is WORKTREE/.git/ then git-push verbage does not complain, and git-checkout exits zero status but has incorrect effect, ie. index and HEAD match, but worktree matches HEAD@{1}. If pwd is WORKTREE/ then git-push complains 'not a git repository', and git-checkout exits non-zero status and has no effect, ie. worktree and index match HEAD@{1}.

Evidence:

(post-update hook script):
MYPWD=pwd
echo $MYPWD
pushd /home/neal/FSNMSTHTML
MYPWD=pwd
echo $MYPWD
git checkout -f HEAD
if [ $? -ne 0 ]; then
echo "error on checkout!"
else
echo "checkout HEAD to non-bare remote current branch after push"
fi
echo $MYPWD
popd
MYPWD=pwd
echo $MYPWD

(git-push verbage):

$ git push origin HEAD
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 462 bytes, done.
Total 5 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
remote: warning: updating the current branch
remote: /home/neal/FSNMSTHTML/.git
remote: ~/FSNMSTHTML ~/FSNMSTHTML/.git
remote: /home/neal/FSNMSTHTML
remote: fatal: Not a git repository: '.'
remote: error on checkout!
remote: /home/neal/FSNMSTHTML
remote: ~/FSNMSTHTML/.git
remote: /home/neal/FSNMSTHTML/.git
To file:///home/neal/FSNMSTHTML
   cee9269..34dc5a9  HEAD -> master

v/r,
neal

#7 lore raw
Quoted 33 lines · Neal Kreitzinger
Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Neal Kreitzinger
Quoted 3 lines

Have you checked where in the filesystem hierarchy that script is run (hint: pwd)?

echo pwd in post-update echoes /path/WORKTREE/.git in git-push stdout. ... 'git-checkout -f' works manually, but in post-update hook...

Stronger hint. Did you run "git checkout -f" in /path/WORKTREE/.git to back that "works manually" claim?

(Manual behavior): If pwd is WORKTREE/ then git-checkout has correct effect, ie. worktree, index, and HEAD match. If pwd is WORKTREE/.git/ then git-checkout complains 'must be run in a worktree' and has no effect.

(post-update hook behavior): If pwd is WORKTREE/.git/ then git-push verbage does not complain, and git-checkout exits zero status but has incorrect effect, ie. index and HEAD match, but worktree matches HEAD@{1}. If pwd is WORKTREE/ then git-push complains 'not a git repository', and git-checkout exits non-zero status and has no effect, ie. worktree and index match HEAD@{1}.

Evidence:

(post-update hook script): MYPWD=pwd echo $MYPWD pushd /home/neal/FSNMSTHTML MYPWD=pwd echo $MYPWD git checkout -f HEAD if [ $? -ne 0 ]; then echo "error on checkout!" else echo "checkout HEAD to non-bare remote current branch after push" fi echo $MYPWD popd MYPWD=pwd echo $MYPWD

Quoted 11 lines

(git-push verbage): $ git push origin HEAD Counting objects: 9, done. Delta compression using up to 8 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (5/5), 462 bytes, done. Total 5 (delta 4), reused 0 (delta 0) Unpacking objects: 100% (5/5), done.
remote: warning: updating the current branch remote:
/home/neal/FSNMSTHTML/.git remote: ~/FSNMSTHTML ~/FSNMSTHTML/.git
remote: /home/neal/FSNMSTHTML remote: fatal: Not a git repository:
'.' remote: error on checkout! remote: /home/neal/FSNMSTHTML remote:
~/FSNMSTHTML/.git remote: /home/neal/FSNMSTHTML/.git To file:///home/neal/FSNMSTHTML cee9269..34dc5a9 HEAD -> master

To get this to work I had to learn more about shell scripting and export the GIT_WORK_TREE git environment variable.

(post-update hook):
export GIT_WORK_TREE=/home/neal/FSNMSTHTML
git checkout -f HEAD

for more info see this stackoverflow thread
http://stackoverflow.com/questions/6635018/reuse-git-work-tree-in-post-receive-hook-to-rm-a-few-files

v/r,
neal

#8 lore raw
Quoted 6 lines · Neal Kreitzinger

To get this to work I had to learn more about shell scripting and export the GIT_WORK_TREE git environment variable.

(post-update hook):
export GIT_WORK_TREE=/home/neal/FSNMSTHTML
git checkout -f HEAD

Hmm, I thought "cd .. && git checkout -f HEAD" (or "git reset --hard") would have worked.

#9 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 3 lines · Neal Kreitzinger

To get this to work I had to learn more about shell scripting and export the GIT_WORK_TREE git environment variable.

Quoted 7 lines
Quoted 3 lines

(post-update hook):
export GIT_WORK_TREE=/home/neal/FSNMSTHTML
git checkout -f HEAD

Hmm, I thought "cd ..&& git checkout -f HEAD" (or "git reset --hard") would have worked.

Ok, I just tried that for the first time and it complained "not a git repository: '.'" because it looks like that caused GIT_DIR to no longer point to WORKTREE/.git but instead to WORKTREE/ because GIT_DIR is set to '.' in the post-update script's environment as indicated in this thread: http://stackoverflow.com/questions/6635018/reuse-git-work-tree-in-post-receive-hook-to-rm-a-few-files

v/r,
neal