0

I'm trying to implement a work-flow using git. Here's what I did:

  • I have a project on my local (git initialized off course)
  • I have added a remote pointing to xyz
  • I created a directory xyz on the server
  • Initialized git in xyz
  • Executed git config receive.denyCurrentBranch='ignore' withing xyz -Afterwards, I created post-receive file in .gt/hooks and added the following lines:

    #!/bin/bash
    GIT_WORK_TREE=/path/to/xyz
    LOGFILE=./post-receive.log
    cd /path/to/xyz/
    echo "Checking out $PWD"
    echo "Run: checkout dev"
    git checkout dev
    echo "Run: git clean -df & git checkout dev"
    git clean -df & git checkout dev
    echo "Run: git submodule update --init "
    echo "Checking out sumodules"
    git submodule update --init 
    chmod 755 -R /path/to/xyz
    

When I push using git push xyz-branch dev --force, I get the following errors:

remote: Checking out /path/to/xyz
remote: Run: checkout dev
remote: fatal: Not a git repository: '.'
remote: Run: git clean -df & git checkout dev
remote: fatal: Not a git repository: '.'
remote: Run: git submodule update --init 
remote: Checking out sumodules
remote: fatal: Not a git repository: '.'
remote: Setting access rights on files and folders
remote: Run: chmod 755 -R /path/to/xyz/

I don't know should I blame the docs or myself for mot understanding the cause of the error.

Update

I was doing many things wrong.Thanks to a twitter friend who pointed out this SO question and I solved my problem. I think it's OK if I explain it as an answer.

Community
  • 1
  • 1
Junaid Qadir Shekhanzai
  • 1,386
  • 1
  • 20
  • 38
  • Doesn't `fatal: Not a git repository: '.'` speak for itself? Add a `pwd` after `cd /path/to/xyz/` to see where it ended up. (On a side note, you can use `set -x` instead of all the `echo "Run: ...` lines.) – Biffen Jun 18 '14 at 12:32
  • ...and on an other side note, you should probably use `set -e` so that if something fails *in* the hook, the hook fails and thus the Git command fails. – Biffen Jun 18 '14 at 12:43
  • on the 4th line I'm changing to the directory where i have initialized git but still i get `fatal: Not a git repository: '.'` error when `checkout dev` is run – Junaid Qadir Shekhanzai Jun 18 '14 at 12:57
  • And are you *sure* there's a Git repo there? What if you run `file .git` after the `cd` (or is it a bare repo?)? I also noticed that `cd` isn't using the `GIT_WORK_TREE` variable; shouldn't it? – Biffen Jun 18 '14 at 13:01
  • yes there is a git repo, non-bare one. well, i'm not sure how to utilize `GIT_WORK_TREE' or `GIT_DIR` – Junaid Qadir Shekhanzai Jun 18 '14 at 13:11
  • Then may I suggest a Bash scripting tutorial? The quick answer is `cd "$GIT_WORK_TREE"`, but you should probably know such things *before* attempting to write a Git hook. And what about `file .git`? – Biffen Jun 18 '14 at 13:55
  • Well, I think we learn by doing. :) yes .git is there. I have ssh as well as cpanel access to my site, so i can see whats in the directories. Let me try and update you – Junaid Qadir Shekhanzai Jun 18 '14 at 14:20
  • @Biffen `file .git` returns `remote: .git: directory` so it's seeing the .git dir and I'm in the xyz dir. – Junaid Qadir Shekhanzai Jun 18 '14 at 14:33
  • Can you make *absolutely* sure the path is correct, and that it's not a matter of absolute vs relative? Did you put the `file` command in the script? Can you "manually" run `git status` in the repo? – Biffen Jun 18 '14 at 15:29

1 Answers1

1

Thanks to my twitter friend (dotNet and Azure guru) Ilija for pointing me to the right direction. I was able to achieve my goal after reading through the SO post I've mentioned in the OP.

Here's what I did:

#!/bin/bash
export GIT_WORK_TREE=/path/to/xyz
export GIT_DIR=/path/to/xyz/.git
cd $GIT_WORK_TREE 
echo "Checking out dev"
git checkout dev
git clean -df & git checkout dev
unset GIT_WORK_TREE
unset GIT_DIR
echo "Checking out sumodules"
mkdir "codeigniter"
git submodule update --init 
echo "clean"
echo "Setting access rights on files and folders"
echo "Run: chmod 755 -R $GIT_WORK_TREE"
cd "$GIT_WORK_TREE"
chmod 755 -R "$GIT_WORK_TREE"
echo "Done."

export was the the key to solving my issue.

Please note two unset commands. I ran them as my repo has a submodule. To have the submodule fetch smooth.

Junaid Qadir Shekhanzai
  • 1,386
  • 1
  • 20
  • 38