是否可以在服务器端更新挂钩上执行 git 提交

Is it possible do git commit on server side update hook

我的任务要点:收集有关回购的信息并将其放入文件,同时更新挂钩并提交它(与新提交完美结合)。

问题:当我进行提交时 -> 它锁定了 origin 存储库并且在此推送失败之后。我的代码如下所示:

#!/bin/bash

# --- Command line
refname=""
oldrev=""
newrev=""
export GIT_WORK_TREE=$PWD

# --- Safety check
if [ -z "$GIT_DIR" ]; then
        echo "Don't run this script from the command line." >&2
        echo " (if you want, you could supply GIT_DIR then run" >&2
        echo "  [=11=] <ref> <oldrev> <newrev>)" >&2
        exit 1
fi

version="${refname##*_}"
branchName="${refname##*/}"
filePath="_componentVersion/BranchVersion.ps1"

if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
        echo "usage: [=11=] <ref> <oldrev> <newrev>" >&2
        exit 1
fi

# --- Check types
# if $newrev is 0000...0000, it's a commit to delete a ref.
zero="0000000000000000000000000000000000000000"
if [ "$newrev" = "$zero" ]; then
        newrev_type=delete
else
        newrev_type=$(git cat-file -t $newrev)
fi

case "$refname","$newrev_type" in
        refs/heads/*,commit)
                if [ "$oldrev " != "$zero" ]; then
                        version="${refname##*_}"
                        branchName="${refname##*/}"
                        filePath="_componentVersion/BranchVersion.ps1"

                        countOfCommits=$(git rev-list --count START_$version..$newrev)
                        countOfPushes=$(git log --pretty=oneline START_$version..$newrev | grep 'Issue nr: HOOK_$version' | wc -l)
                        countOfPushes=$(($countOfPushes+1))
                        echo git log --pretty=oneline START_$version..$newrev

                        message="
# -----------------------
# Brancht Version Info
# -----------------------

$branch = '$version'
$countOfCommits = $countOfCommits
$countOfPushes = $countOfPushes # push
$commitHash = '$newrev'
                        "

                        # credits go to 
                        # branch commit - here we will do the magic about count of commits and about count of pushes
                        # here we create file for info

                        # Empty the index, not sure if this step is necessary
                        git read-tree --empty

                        # Load the current tree. A commit ref is fine, it'll figure it out.
                        git read-tree "${newrev}"

                        # create blob from stdin
                        BLOB_ID=$(echo "$message" | git hash-object -w --stdin)

                        # update indexes in git
                        git update-index --add --cacheinfo 100644 "$BLOB_ID" "$filePath"

                        # Create a tree from your new index
                        TREE_ID=$(git write-tree)

                        # Commit it.
                        NEW_COMMIT=$(echo "Issue nr: HOOK_$version $message" | git commit-tree "$TREE_ID" -p "$oldrev")

                        # Update the branch
                        git update-ref "$refname" "$NEW_COMMIT" "$oldrev"
                fi

                # Done
                exit 0
                ;;
        *)
                # Other actions except commit to branch / for now - we won't check it
                exit 0
                ;;
esac

# --- Finished
exit 0

我正在使用裸仓库。以及来自 here 的提交示例 执行问题是

remote: error: cannot lock ref 'refs/heads/REL_7.0.0': ref refs/heads/REL_7.0.0 is at 54f2454ddab36eda001e27946733a7b0e981f097 but expected 89a3032e0bfb999273205e32b7f6d57173c4bd7e 

可以创建提交。

不能更新被锁定的引用,其中包括调用更新挂钩的引用。

由于git push 可以推送多个引用名称,因此可能会有额外的锁定引用。一般来说,在 git push 调用的挂钩中更新任何人可能 git pushing 的任何内容都不是一个好主意。换句话说,不要尝试更新任何分支或标记名称。如果要创建新对象,请将它们附加到这两个名称空间之外的某个名称。

(另外:git read-tree --empty 不是必需的,但无论如何使用临时索引文件而不是使用主索引是个好主意。)