簡體   English   中英

是否可以在服務器端更新掛鈎上執行git commit

[英]Is it possible do git commit on server side update hook

我的任務重點:收集有關回購的信息並將其放置在文件中,同時更新鈎子並提交(與新提交完全匹配)。

問題:當我執行提交->時,它鎖定了origin存儲庫,並且在此推送失敗之后。 我的代碼如下所示:

#!/bin/bash

# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"
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 "  $0 <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: $0 <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 https://stackoverflow.com/questions/9670302/commit-directly-to-a-bare-repository
                        # 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

我正在使用裸倉庫。 這里的 Execte問題中提交的示例是

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 push調用的鈎子中的東西不是一個好主意。 換句話說,不要嘗試更新任何分支或標簽名稱。 如果要創建新對象,請將它們附加到這兩個命名空間之外的某個名稱。

(此外: git read-tree --empty不是必需的,但是無論如何都使用臨時索引文件,而不是使用主索引是一個好主意。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM