在bash脚本中,git无法识别它自己的目录?

Mur*_*ala 4 linux git bash shell bitbucket-server

因此,我在 Atlassian-Stash 中为接收后事件编写了一个 bash 脚本。在此脚本中,提交后,它会创建一个 codecollaborator 代码审查。要创建代码审查,需要提交标题、提交用户和 git SHA 以进行任何更改,并将更改上传到代码审查。为了获取这些信息,我将目录克隆到 --深度 = 1 (即使没有 --深度 = 1)并使用 git log (选项)。

我看到的问题是,如果我手动运行脚本,它就可以正常工作。但是,如果它在提交后运行,则在克隆目录后会出错,表明它不是 git 目录。如果我在脚本退出后 cd 进入该目录,我就可以运行 git log (和其他 git 命令)。

我尝试解决的问题是 1. 权限问题(以 root 身份运行),所以我没有看到任何权限问题。2. 使用 bash -xv 进行故障排除,直到此时一切看起来都很好。3.我还用$?4.我尝试将.git移至git-backup,等待3秒并将其移回来,仍然是同样的问题。5. 我运行 ls -ltra 以确保它具有所有文件和 .git 目录。

现在,我别无选择。以前有人遇到过这种问题吗?

有人知道我哪里可能做错了什么或遗漏了什么吗?

我尝试尽可能具有描述性,如果问题没有意义或需要示例脚本,请告诉我。

添加下面的脚本及其错误输出。

#!/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}
Run Code Online (Sandbox Code Playgroud)

# 下面是错误的地方。

   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 "=========="
Run Code Online (Sandbox Code Playgroud)

错误输出为:

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: '.'
Run Code Online (Sandbox Code Playgroud)

tor*_*rek 5

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

  • 在 git hook 中,GIT_DIR设置环境变量(.在存储库中设置为--bare.git在非裸机中设置为)。这仅在您cd到达其他目录之前有效,通常是在从钩子脚本运行的子进程中,该子进程不知道$GIT_DIR指向某个现在不合适的位置。

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

快速而简单的解决方法是unset GIT_DIR