Files

69 lines
2.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 共享 Development-Manager 执行器薄入口:核对 deps/development-manager 与 index 钉住的提交一致且无修改,
# 构建(或复用缓存的)dm-server 二进制后执行 `dm-server agent-exec`。结果仅为本地完整性证据(SELF)。
set -euo pipefail
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
root=$(cd -- "$script_dir/.." && pwd)
sub_path="deps/development-manager"
dm="$root/$sub_path"
die() {
echo "agent-exec: $*" >&2
exit 2
}
# 在 DM checkout 内执行 git;清除 pre-commit 可能继承的仓库环境变量,避免读取超级仓的索引或对象库。
dm_git() {
env -u GIT_DIR -u GIT_WORK_TREE -u GIT_INDEX_FILE git -C "$dm" "$@"
}
index_entry=$(git -C "$root" ls-files -s -- "$sub_path") || index_entry=""
if [ -z "$index_entry" ]; then
die "index 中不存在 $sub_path 的 gitlink,拒绝执行"
fi
read -r mode pin _ <<<"$index_entry"
if [ "$mode" != "160000" ]; then
die "$sub_path 在 index 中不是 gitlink(mode=$mode),拒绝执行"
fi
if ! [[ "$pin" =~ ^[0-9a-f]{40}$ ]]; then
die "$sub_path 的 index 钉住值无效:$pin"
fi
remedy="请运行 git submodule update --init deps/development-manager"
if [ ! -e "$dm/.git" ]; then
die "$sub_path 未检出;$remedy"
fi
top=$(dm_git rev-parse --show-toplevel 2>/dev/null) || top=""
real_dm=$(cd -- "$dm" && pwd -P)
if [ -z "$top" ] || [ "$top" != "$real_dm" ]; then
die "$sub_path 不是独立的 checkout;$remedy"
fi
head=$(dm_git rev-parse HEAD 2>/dev/null) || head=""
if [ "$head" != "$pin" ]; then
die "$sub_path 的 HEAD(${head:-未知})与 index 钉住的 $pin 不一致;$remedy"
fi
dirty=$(dm_git status --porcelain --ignore-submodules=all) || die "无法读取 $sub_path 的工作区状态,拒绝执行"
if [ -n "$dirty" ]; then
die "$sub_path 存在未提交的修改,拒绝执行"
fi
cache_dir="${AGENT_EXEC_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/agent-system-dev/agent-exec}"
bin="$cache_dir/dm-server-$pin"
if [ ! -x "$bin" ]; then
command -v go >/dev/null 2>&1 || die "未找到 go,无法构建 dm-server"
mkdir -p "$cache_dir"
tmp=$(mktemp "$cache_dir/.dm-server-$pin.XXXXXX")
trap 'rm -f "$tmp"' EXIT
if ! (cd "$dm" && env -u GIT_DIR -u GIT_WORK_TREE -u GIT_INDEX_FILE GOWORK=off go build -trimpath -o "$tmp" ./cmd/dm-server); then
die "构建 dm-server 失败"
fi
chmod 0755 "$tmp"
mv -f "$tmp" "$bin"
trap - EXIT
fi
exec "$bin" agent-exec "$@"