Title: git hook not behaving consistently between linux and windows
Tags: git;raspberry-pi;hook;git-post-receive
Question: After setting a local GIT server up I tried to set a post-receive hook to deploy my code after each push. It worked like a breeze on my windows 10 computer but not on my linux server. To work on the matter further I installed a raspberry pi to check if it was related to linux, and I met the same behavior;
#!/bin/bash
TARGET="C:/GIT/repoPROJECT/Test"
GIT_DIR="C:/GIT/repoGIT/Test"
BRANCH="master"
while read oldrev newrev ref
# only checking out the master (or whatever branch you would like to deploy)
if [ "$ref" = "refs/heads/$BRANCH" ];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
(repoGIT/Test is a bare git repo, the other is a clone. I push to the bare and set the hook there)
with the same configuration, authorizations, this wouldn't work on my linux (raspberry) but would work perfectly on my windows computer. I checked that my hook is fired and it is (it creates a folder).
In the end this one worked:
#!/bin/bash
unset $(git rev-parse --local-env-vars)
TARGET="/home/pi/GIT/repoPROJECT/Test"
GIT_DIR="/home/pi/GIT/repoGIT/Test"
BRANCH="origin"
cd $TARGET
git pull
but I'm unsure as to why I had to use a different script, why I had to switch from master to origin when I set the project in the exact same way (when experimenting with the top script).
I assume it's related to the OS, but I'd like to understand so I can deal with such problems in the future.
(I can't recall all of the errors I had, but sometimes it'd be master is not a git repository, sometimes the script would be stuck in a loop.
note that I tried unsetting the var on the top script but it still didn't work)
Comment: I'm still experimenting and it seems my read while read oldrev newrev ref just hangs on my raspberry.
So I don't know, I tried other ways, pulling seems to be troubles as well as it creates merging issues (even though this is just sort of copying the latest modification?) so I've thoguht about using reset --hard before each pull in the post-receive hook but it's really not satisfactory: I can't wrap my head around the fact that that script that worked in seconds on my windows just won't work on linux no matter what I do.
I've tried removing the CR/LF from my file, unsetting variables...

Comment: with terminal when I try to checkout adding the same arguments I have no message, nothing is updated. when I simply try checkout it says my branch is already up to date (but when I use git log I can see I have one less push still

Comment: my project isn't updated, the hook doesn't do its job even though it is fired. that script that works on windows doesn't on linux. $branch is used on the top script,t the one that works on windows but not on linux.
when I run it as is (top one) on linux (with correct paths), the script hangs, waiting for my input (from the terminal)
When set it as a hook (post-receive) nothing happens

Comment: it seems to be waiting for the ref variable value from terminal input, if I remove the condition (and while loop ) I get "already on master".
I don't get why this is behaving completely differently even thoguh it's the same script.

Comment: echoing the values of the variable returns empty strings, before and during the loop. the end of it is never reached.

Comment: could it be that since those values aren't passed and the script still detects that there's changes on my repoGIT it loops endlessly waiting for the right branch value in ref. the sad part would be that even if it did reach the checkout part even this alone doesn't seem like it'd work. unless it doesn't because I try it outside of the hook context..?

Comment: Ok,I think I found the problem; I created a test project only on the raspberry and tried the same setup with a bare git linking a work directory and a deploy directory. same script. this time it works fine; so the problem is that when I push from windows on a linux hosted bare repo the hook doesn't receive the data it should (ref etc). I've yet to find what to do to solve this.

Comment: *doesn't work on my linux (raspberry)* In what way it doesn't work? Is there an error? What is the output? *why I had to switch from master to origin* In the second script `BRANCH` is not used at all so it could be anything — `origin`, `master`, whatever.

Comment: *the script hangs, waiting for my input* Of course — `read oldrev newrev ref` waits for input. *When set it as a hook (post-receive) nothing happens* That part you have to investigate. Insert debugging `echo` in the script — before the loop, inside the loop (like `echo $oldrev $newrev $ref`), after the loop — the output should appear at the terminal where you run `git push`.

Comment: *echoing the values of the variable returns empty strings, before and during the loop. the end of it is never reached.* Very strange!


Here is another answer: I don't entirely understand how I ended up in this situation, but there's what I found:
First; I was experimenting with GIT there's probably things I don't know regarding how it is supposed to be used and this is the main reason I had several problems and wasn't sure about what was causing them.
I was trying to set a local GIT server so I could work on my computer, push my code on a linux server, and then have my deployed project update accordingly.
What I first tried was to create a bare repo, push my code there from my IDE and add a post-receive hook to that repo so my deployed repo could benefit from the changes I'd push.
I think the fact that I was going through a mounted folder was part of the problem as this setup worked with a few test fodlers and files locally.
Now the problem that this created is that my code would indeed be pushed to the bare repo, the post-receive hook would be fired, but the variables ref etc used in the script would be empty for some reason. I've also had a few funky results while trying to solve this where my remote/branches would use my machine's context instead of the server's.
Anyways, this didn't work. I managed to reproduce that behavior with my own computer and a raspberry pi: different gear, same setup. the hook would be fired but the script would be stuck, variables remained empty. (this is the part I don't understand to this day, because the remote paths were good, everything seemed fine, pushing would work, pulling etc.)
Now, what ended up solving it was setting this in a different way:
1) creating the bare repo, empty (server side)
2) creating a clone of that empty repo, still on the server (probably doesn't matter).
3) putting my code inside that new repo, and pushing it to the bare one so it's up to date.
4) creating a new clone of the bare repo (for my deployed project)
5) adding the hook to the bare repo, post-receive, target is the deployed repo, git dir is the bare one. (+chmod 777 and +x on that file)
(from there, changing anything and pushing from the first clone should fire the hook and have the deployed repo checkout correctly)
6) then, I went back on my IDE, cloned the bare repo, tried to push something new, and that's when things finally worked as expected.
I can only assume cloning repos as I was setting my project created the right links between the different parts of the setup, which means the way I first did it was missing something so it couldn't work properly.
I'd appreciate it if someone with more experience on the matter could provide a more detailed explanation. In the mean time my problem is solved.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 10, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:03/29/19', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<git><raspberry-pi><hook><git-post-receive>', 'Title': 'git hook not behaving consistently between linux and windows', 'ViewCount': 111, 'id': 55426593, 'user_AboutMe': None, 'user_AccountId': 15634388.0, 'user_DisplayName': 'fenswtich', 'user_Reputation': 41.0, 'user_Views': 26.0, 'user_id': 11279766.0}}