git克隆不同的存储库在git hooks中创建麻烦

Jat*_*oot 7 git commit pre-commit pre-commit-hook githooks

我们有一个特定的要求,我们必须将所有到达的文件推送到某个分支.

我们计划通过git hooks专门实现commit-msg hook来实现这一点.

在这样做的同时,我们将分支克隆到临时位置(/ tmp /),然后在git commit-msg钩子中,尝试将到达的文件提交到某个分支.

但现在发生的是我们在/ tmp /中看到所有文件都被删除了.

粗体commit-msg脚本如下: -

#!/bin/bash
#
#!/usr/bin/env bash

#git config credential.helper store
REPOSRC="https://<USER>:<PASS>@<REPO_URL>"
LOCALREPO="<LOCAL_REPO_DIR>"

echo "Pulling code to temporarry location";

cd /tmp && git clone "${REPOSRC}" || (cd "${LOCALREPO}"; git pull;)

#here when I navigate to /tmp/<LOCALREPO> all files are listed as DELETED

git diff --cached --name-status | while read st file; do

    echo "file == $file and status == $st";

                    if [ "$st" == "A" ]; then
                        cd "${LOCALREPO}" && git add "$file" && git commit "$file" -m "$COMMIT_MSG" && git push origin "$branch"
                    else
                        cd "${LOCALREPO}" && git commit "$file" -m "$COMMIT_MSG" && git push origin "$branch"
                    fi
done
Run Code Online (Sandbox Code Playgroud)

这可能是什么原因?

编辑:

GIT_INDEX_FILE显示启动提交的索引文件的路径,而不是/ tmp / path.有没有办法改变这个变量?索引文件也会输出类似next-index-32419.lock的内容.问候

Von*_*onC 5

每当一个钩子改变文件夹时,检查以下值是很重要的:

  • GIT_DIR (在你的情况下应该引用正确的.git repo文件夹)
  • GIT_WORK_TREE(应不引用预期文件夹)

这就是为什么,对于任何Git命令,你需要替换git为:

git --work-tree=$(pwd) ...
Run Code Online (Sandbox Code Playgroud)

(或/path/to/your/working/tree)