blob: e453fd1b29173d2896bc05932c9903ee8f89a3fb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#!/usr/bin/env bash
# git-worktree-gate — one definition of safe Git state for startup and wrap.
#
# Modes:
# strict [DIR] Require an entirely empty worktree.
# sync-safe [DIR] Permit untracked inbox/ deliveries, but nothing else.
# certify [DIR] Strict-check, then record the verified HEAD in the git dir.
# verify [DIR] Strict-check and require the recorded HEAD to still match.
#
# Ignored files are deliberately outside Git's clean-worktree contract.
set -u
mode="${1:-}"
repo="${2:-.}"
usage() {
echo "usage: git-worktree-gate {strict|sync-safe|certify|verify} [DIR]" >&2
exit 2
}
case "$mode" in
strict|sync-safe|certify|verify) ;;
*) usage ;;
esac
root="$(git -C "$repo" rev-parse --show-toplevel 2>/dev/null)" || {
echo "git-worktree-gate: $repo is not inside a Git worktree" >&2
exit 2
}
gitdir="$(git -C "$root" rev-parse --absolute-git-dir 2>/dev/null)" || {
echo "git-worktree-gate: cannot resolve the Git directory for $root" >&2
exit 2
}
certificate="$gitdir/ai-wrap-clean"
quote_path() {
printf '%q' "$1"
}
operation_in_progress() {
local marker
for marker in MERGE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_LOG; do
[ -e "$gitdir/$marker" ] && {
printf '%s' "$marker"
return 0
}
done
for marker in rebase-merge rebase-apply sequencer; do
[ -d "$gitdir/$marker" ] && {
printf '%s' "$marker"
return 0
}
done
return 1
}
describe_entry() {
local xy="$1" path="$2" original="${3:-}"
local index="${xy:0:1}" worktree="${xy:1:1}" label=""
if [ "$xy" = "??" ]; then
label="untracked; add and commit it, move it outside the repository, or remove it if unwanted"
elif [ "$xy" = "!!" ]; then
label="ignored"
elif [[ "$xy" = *U* || "$xy" = "AA" || "$xy" = "DD" ]]; then
label="unmerged; resolve the conflict and commit the result"
elif [ "$index" != " " ] && [ "$worktree" != " " ]; then
label="staged and unstaged changes; review both layers, then commit or restore them"
elif [ "$index" != " " ]; then
label="staged change; commit it or unstage and restore it"
else
label="unstaged tracked change; commit it or restore it"
fi
printf ' %s ' "$xy"
quote_path "$path"
if [ -n "$original" ]; then
printf ' (from '
quote_path "$original"
printf ')'
fi
printf ' — %s\n' "$label"
}
check_state() {
local policy="$1" xy path original="" blocked=0 op=""
local status_tmp="" status_err="" status_detail=""
local -a report=()
if op="$(operation_in_progress)"; then
report+=(" Git operation in progress: $op — finish or abort it")
blocked=1
fi
status_tmp="$(mktemp "$gitdir/ai-worktree-status.tmp.XXXXXX")" || {
echo "wrap blocked: cannot allocate a Git-state check file" >&2
return 1
}
status_err="$(mktemp "$gitdir/ai-worktree-status.err.XXXXXX")" || {
rm -f "$status_tmp"
echo "wrap blocked: cannot allocate a Git-state error file" >&2
return 1
}
if ! git -C "$root" status --porcelain=v1 -z \
--untracked-files=all --ignore-submodules=none \
>"$status_tmp" 2>"$status_err"; then
status_detail="$(head -1 "$status_err")"
[ -n "$status_detail" ] || status_detail="unknown Git error"
report+=(" git status failed — $status_detail")
blocked=1
else
while IFS= read -r -d '' entry; do
xy="${entry:0:2}"
path="${entry:3}"
original=""
if [[ "${xy:0:1}" = "R" || "${xy:0:1}" = "C" ]]; then
IFS= read -r -d '' original || true
fi
if [ "$policy" = "sync-safe" ] \
&& [ "$xy" = "??" ] \
&& [[ "$path" = inbox/* ]]; then
continue
fi
report+=("$(describe_entry "$xy" "$path" "$original")")
blocked=1
done <"$status_tmp"
fi
rm -f "$status_tmp" "$status_err"
if [ "$blocked" -ne 0 ]; then
if [ "$policy" = "sync-safe" ]; then
echo "sync blocked: rulesets has changes other than untracked inbox deliveries" >&2
else
echo "wrap blocked: Git worktree is not completely clean" >&2
fi
printf '%s\n' "${report[@]}" >&2
return 1
fi
return 0
}
case "$mode" in
strict)
check_state strict
;;
sync-safe)
check_state sync-safe
;;
certify)
check_state strict || exit 1
head="$(git -C "$root" rev-parse HEAD 2>/dev/null)" || {
echo "wrap blocked: cannot resolve HEAD" >&2
exit 1
}
tmp="$(mktemp "$gitdir/ai-wrap-clean.tmp.XXXXXX")" || exit 1
chmod 600 "$tmp"
{
printf 'head=%s\n' "$head"
printf 'root=%s\n' "$root"
} >"$tmp"
mv "$tmp" "$certificate"
;;
verify)
check_state strict || exit 1
[ -f "$certificate" ] || {
echo "wrap blocked: no clean-tree certificate exists; rerun the final wrap verification" >&2
exit 1
}
certified_head="$(sed -n 's/^head=//p' "$certificate" | head -1)"
certified_root="$(sed -n 's/^root=//p' "$certificate" | head -1)"
current_head="$(git -C "$root" rev-parse HEAD 2>/dev/null)" || exit 1
[ "$certified_root" = "$root" ] || {
echo "wrap blocked: clean-tree certificate belongs to a different worktree" >&2
exit 1
}
[ -n "$certified_head" ] && [ "$certified_head" = "$current_head" ] || {
echo "wrap blocked: HEAD changed after clean-tree certification; rerun the final wrap verification" >&2
exit 1
}
;;
esac
|