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: '.'
4

1 回答 1

5

我对 Atlassian 一无所知,但从错误输出中可以清楚地看出,您正在绊倒我在现在找不到的答案中提到的一个钩子陷阱:

  • 在一个 git 钩子中,环境变量GIT_DIR被设置(.--barerepos 中,.git在 non-bare 中)。这仅在您cd到某个其他目录之前有效,通常在从不知道$GIT_DIR指向某个现在不合适的地方的挂钩脚本运行的子进程中。

(该git clone步骤有效,因为它不是在寻找 git 目录,它只是在创建一个新目录。)

快速简便的解决方法是unset GIT_DIR.

于 2013-11-19T19:10:59.707 回答