test: let print workflows own their worktrees
This commit is contained in:
@@ -24,7 +24,9 @@ jobs:
|
||||
|
||||
REPO_NAME="${{ github.event.repository.name }}"
|
||||
HEAD_SHA="${{ github.event.workflow_run.head_sha }}"
|
||||
REPOSITORY_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/repository.git"
|
||||
WORKTREE_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/worktrees/print2"
|
||||
WORKTREE_LOCK="${WORKSPACE_ROOT}/${REPO_NAME}/worktree-admin.lock"
|
||||
|
||||
case "$HEAD_SHA" in
|
||||
''|*[!0-9a-fA-F]*)
|
||||
@@ -32,12 +34,43 @@ jobs:
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
if [ ! -f "$WORKTREE_DIR/.git" ]; then
|
||||
echo "Prepare 未创建 Print Two Worktree: $WORKTREE_DIR" >&2
|
||||
|
||||
if [ ! -d "$REPOSITORY_DIR" ] || \
|
||||
[ "$(git --git-dir="$REPOSITORY_DIR" rev-parse --is-bare-repository)" != "true" ]; then
|
||||
echo "Prepare 未创建共享 bare 仓库: $REPOSITORY_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! git -C "$WORKTREE_DIR" cat-file -e "${HEAD_SHA}^{commit}"; then
|
||||
echo "Worktree 共享仓库中不存在上游提交: $HEAD_SHA" >&2
|
||||
if ! git --git-dir="$REPOSITORY_DIR" cat-file -e "${HEAD_SHA}^{commit}"; then
|
||||
echo "共享仓库中不存在上游提交: $HEAD_SHA" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f "$WORKTREE_DIR/.git" ]; then
|
||||
if ! command -v flock >/dev/null 2>&1; then
|
||||
echo "首次创建 Worktree 需要 flock" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec 9>"$WORKTREE_LOCK"
|
||||
flock 9
|
||||
|
||||
if [ ! -f "$WORKTREE_DIR/.git" ]; then
|
||||
if [ -e "$WORKTREE_DIR" ]; then
|
||||
echo "Worktree 路径已存在但不是 linked worktree: $WORKTREE_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$(dirname "$WORKTREE_DIR")"
|
||||
echo "首次创建 Print Two Worktree: $WORKTREE_DIR"
|
||||
git --git-dir="$REPOSITORY_DIR" worktree add --detach "$WORKTREE_DIR" "$HEAD_SHA"
|
||||
fi
|
||||
|
||||
flock -u 9
|
||||
fi
|
||||
|
||||
COMMON_DIR=$(git -C "$WORKTREE_DIR" rev-parse --path-format=absolute --git-common-dir)
|
||||
if [ "$COMMON_DIR" != "$(realpath "$REPOSITORY_DIR")" ]; then
|
||||
echo "Print Two Worktree 不属于共享仓库: $WORKTREE_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user