4

So I have written a bash script within Atlassian-Stash for post-receive events. In this script, after a commit has been made, it creates a codecollaborator code review. To create a code review, it needs commit title, commit user and git SHA for any changes and uploading the changes to the code review. To get these informations, I clone'd the directory to --depth=1 (even without --depth=1) and work with git log (options).

The problem I am seeing is that if I run the script manually, it works just fine. However, if it runs after a commit has been made, it errors out after it clones the directory saying it is not a git directory. If I cd into the directory after the script exits, I am able to run git log (and other git commands).

Things I tried to troubleshoot are 1. Permissions issues (running it as root), so I am not seeing any permissions issues. 2. troubleshooting it with bash -xv and until that point everything looks good. 3. I also put it status checks with $? 4. I tried to move .git to git-backup, wait 3 seconds and moved it back, still the same issue. 5. I ran ls -ltra to make sure that it has all the files and .git directory.

Now, I am out of options. Has anyone ran into this kind of problem before?

Anyone know where I might be doing something wrong or missing something?

I tried to be as descriptive as possible, if the question does not make sense or need a sample script, please let me know.

Adding the script and its error output below.

#!/bin/bash -xv

CCollabExe='/usr/local/bin/ccollab'
CCollabUrl='--url http://***:8080'
CCollabUser='--user ******'
CCollabPassword='--password ******'
CCollabConnection="${CCollabExe} ${CCollabUrl} ${CCollabUser} ${CCollabPassword}"
CCollabStuff='/home/stash/repositories/tmp'
CloneDir="${CCollabStuff}/ClonnedDir"
StashUser='******'
StashPass='******'
RepoURLlinkGit="http://${StashUser}:${StashPass}@******:7990/scm/t/test1.git"

unset SSH_ASKPASS

# Test function to check if a varibale is empty
CheckIfVarEmpty () {
  local Variable="$1"
  if [[ -z ${Variable} ]] ; then
     echo "Variable $1 '\${Variable}' is empty, exiting"
     echo "Lets try to go back in the git dir" && cd ${CloneDir} && git log -10
     cd /root && cd ${CloneDir}
     [[ -d .git ]] && cp -rp .git git-backup && rm -rf .git && echo "sleeping 3" && sleep 3 && mv git-backup .git
     git log -10
     exit 0
  fi
}

#Create a new CCollab temp dir, clone the directory and get commit title, user and SHA info
   rm -rf ${CCollabStuff} && mkdir ${CCollabStuff} && cd ${CCollabStuff}
   git clone ${RepoURLlinkGit} ${CloneDir}
   cd ${CloneDir}

# below is where its erroring out.

   CommitTitle=$(git log  --pretty=format:"%s" -1)
   CheckIfVarEmpty ${CommitTitle}
   CommitUser=$(git log  --pretty=format:"%an" -1)
   CheckIfVarEmpty ${CommitUser}
   CommitSHA=$(git log  --pretty=format:"%h" -2)
   CheckIfVarEmpty ${CommitSHA}
   CommitSHA1=$(echo $CommitSHA | awk -F' ' '{ print $1 }')
   CommitSHA2=$(echo $CommitSHA | awk -F' ' '{ print $2 }')
   echo "=========="

Error out is:

remote:   rm -rf ${CCollabStuff} && mkdir ${CCollabStuff} && cd ${CCollabStuff}
remote: + rm -rf /home/stash/repositories/tmp
remote: + mkdir /home/stash/repositories/tmp
remote: + cd /home/stash/repositories/tmp
remote:   git clone ${RepoURLlinkGit} ${CloneDir}
remote: + git clone http://******:******@******:7990/scm/t/test1.git /home/stash/repositories/tmp/ClonnedDir
remote: Cloning into '/home/stash/repositories/tmp/ClonnedDir'...
remote:   cd ${CloneDir}
remote: + cd /home/stash/repositories/tmp/ClonnedDir
remote:   CommitTitle=$(git log  --pretty=format:"%s" -1)
remote: git log  --pretty=format:"%s" -1
remote: ++ git log --pretty=format:%s -1
remote: fatal: Not a git repository: '.'
Murtaza Pitalwala
  • 839
  • 1
  • 8
  • 14
  • Yes please, a copy of the script would be a start. – tripleee Nov 19 '13 at 17:49
  • tripleee, I have added the snippet of the code that is erroring out. – Murtaza Pitalwala Nov 19 '13 at 18:17
  • Please replace line `CommitTitle=$(git log --pretty=format:"%s" -1)` with `CommitTitle=$(pwd>&2;git log --pretty=format:"%s" -1)` and show as a result. – ArturFH Nov 19 '13 at 18:53
  • Here it is. remote: + cd /home/stash/repositories/tmp/ClonnedDir remote: CommitTitle=$(pwd; ls -altr ; git log --pretty=format:"%s" -1) remote: pwd; ls -altr ; git log --pretty=format:"%s" -1 remote: ++ pwd remote: ++ ls -altr remote: ++ git log --pretty=format:%s -1 remote: fatal: Not a git repository: '.' remote: + CommitTitle='/home/stash/repositories/tmp/ClonnedDir remote: total 52 remote: drwxr-xr-x 3 root root 4096 Nov 19 10:56 .. remote: drwxr-xr-x 8 root root 4096 Nov 19 10:56 .git remote: -rw-r--r-- 1 root root 10 Nov 19 10:56 file9 – Murtaza Pitalwala Nov 19 '13 at 19:00

1 Answers1

5

I know nothing about Atlassian but it's clear from the error output that you're tripping over one of the hook traps I noted in an answer I can't find now:

  • In a git hook, the environment variable GIT_DIR is set (to . in --bare repos, to .git in non-bare ones). This is valid only until you cd to some other directory, often in a sub-process run from the hook script that has no idea that $GIT_DIR is pointing off to some now-inappropriate place.

(The git clone step works because it is not looking for a git directory, it's just creating a new one.)

The quick and easy fix is unset GIT_DIR.

torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    Thanks torek. I had no idea about that, neither would I have imagined to look at (possibly an issue with) git. "unset GIT_DIR" did the trick and moved on to the next error. Thanks very much for saving the day – Murtaza Pitalwala Nov 19 '13 at 19:49