@rt-tools/agent-kit 0.26.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/agents/cargo-writer.md +54 -0
- package/assets/checks/board-epic-link.github.mjs +29 -0
- package/assets/checks/board-epic-plan.github.mjs +86 -0
- package/assets/checks/board-epics.github.mjs +217 -69
- package/assets/checks/board-long-work.github.mjs +5 -1
- package/assets/checks/board-pull-state.github.mjs +163 -0
- package/assets/checks/board-task-dirs.github.mjs +98 -0
- package/assets/checks/board.github.mjs +8 -91
- package/assets/checks/check-board.github.mjs +25 -165
- package/assets/checks/check-glossary.mjs +10 -2
- package/assets/checks/check-lib-layers.mjs +24 -1
- package/assets/checks/check-prose-style.mjs +19 -3
- package/assets/checks/check-reuse.mjs +26 -2
- package/assets/checks/check-schema-drift.mjs +19 -3
- package/assets/checks/check-specs.mjs +8 -1
- package/assets/checks/epic-table.github.mjs +268 -0
- package/assets/checks/lib-boundaries.mjs +2 -2
- package/assets/checks/lib-common.mjs +46 -0
- package/assets/checks/lib-manifests.mjs +18 -13
- package/assets/checks/rt-kit-checks.config.mjs +19 -0
- package/assets/checks/signals.mjs +39 -5
- package/assets/checks/spec-anchors.mjs +48 -4
- package/assets/checks/spec-common.mjs +40 -6
- package/assets/checks/spec-contract.mjs +33 -7
- package/assets/checks/spec-scenarios.mjs +24 -1
- package/assets/checks/specs-for.mjs +214 -0
- package/assets/checks/task-new.github.mjs +237 -6
- package/assets/commands/night.md +115 -0
- package/assets/defaults/gate-map.sh +20 -5
- package/assets/defaults/project.sh +13 -0
- package/assets/defaults/turn-map.md +3 -1
- package/assets/docs/GLOSSARY.md +22 -22
- package/assets/hooks/browser-device-id.sh +4 -0
- package/assets/hooks/browser-guard-device-id.sh +4 -0
- package/assets/hooks/commit-msg.sh +3 -0
- package/assets/hooks/dev-server-guard.sh +64 -31
- package/assets/hooks/epic-over.sh +49 -0
- package/assets/hooks/epic-stop-guard.sh +108 -0
- package/assets/hooks/exam-guard.sh +10 -10
- package/assets/hooks/git-guard-delivery-conflict.sh +56 -1
- package/assets/hooks/git-guard-delivery-draft.sh +10 -6
- package/assets/hooks/git-guard-delivery-epic.sh +167 -0
- package/assets/hooks/git-guard-delivery-signature.sh +32 -2
- package/assets/hooks/git-guard-delivery-tree.sh +50 -0
- package/assets/hooks/git-guard-delivery.sh +71 -27
- package/assets/hooks/git-guard-discard.sh +144 -0
- package/assets/hooks/git-guard-push-tests.sh +36 -16
- package/assets/hooks/handoff-entry-guard.sh +20 -2
- package/assets/hooks/prose-style-guard.sh +31 -8
- package/assets/hooks/qa-dataid-guard.sh +33 -11
- package/assets/hooks/reuse-first-guard.sh +29 -4
- package/assets/hooks/rule-article.sh +3 -0
- package/assets/hooks/rule-source-guard.sh +17 -2
- package/assets/hooks/skill-gate-layers.sh +4 -0
- package/assets/hooks/skill-gate.sh +7 -0
- package/assets/hooks/skill-loaded.sh +4 -0
- package/assets/hooks/sql-guard-parse.sh +3 -0
- package/assets/hooks/sql-guard-request.sh +3 -0
- package/assets/hooks/sql-guard-target.sh +3 -0
- package/assets/hooks/sql-guard-write.sh +21 -2
- package/assets/hooks/stand-login-guard.sh +3 -0
- package/assets/hooks/turn-exit-guard.sh +55 -67
- package/assets/hooks/turn-exit-patterns.sh +28 -1
- package/assets/hooks/turn-exit-verdict.sh +84 -0
- package/assets/hooks/utf8.sh +3 -0
- package/assets/hooks/waiting-turn-guard.sh +11 -1
- package/assets/hooks/write-targets.sh +3 -0
- package/assets/laws/delivery.md +37 -7
- package/assets/laws/verifiability.md +22 -4
- package/assets/patterns/git-workflow-commit.azure.md +1 -1
- package/assets/patterns/git-workflow-commit.github.md +9 -2
- package/assets/patterns/git-workflow-commit.gitlab.md +1 -1
- package/assets/patterns/git-workflow-merge.md +8 -5
- package/assets/patterns/git-workflow-pr.azure.md +1 -1
- package/assets/patterns/git-workflow-pr.github.md +44 -18
- package/assets/patterns/git-workflow-pr.gitlab.md +1 -1
- package/assets/patterns/git-workflow-stack.md +32 -9
- package/assets/patterns/status-report-table.github.md +11 -0
- package/assets/patterns/task-flow-close.md +96 -90
- package/assets/patterns/task-flow-start.md +40 -26
- package/assets/patterns/ts-procedure.md +6 -0
- package/assets/pitfalls/agent-kit.md +19 -0
- package/assets/pitfalls/git-workflow.github.md +44 -0
- package/assets/pitfalls/task-flow.md +25 -23
- package/assets/rules/autonomous-work.md +15 -3
- package/assets/rules/browser-verification.md +8 -0
- package/assets/rules/deploy-flow.github.md +6 -0
- package/assets/rules/doc-style.md +13 -4
- package/assets/rules/git-workflow.azure.md +10 -0
- package/assets/rules/git-workflow.github.md +144 -126
- package/assets/rules/git-workflow.gitlab.md +10 -0
- package/assets/rules/lib-layers.md +11 -0
- package/assets/rules/spec-driven.md +17 -1
- package/assets/rules/status-report.md +7 -0
- package/assets/rules/task-flow.md +78 -54
- package/assets/rules/testing.md +7 -0
- package/assets/rules/turn-conduct.md +30 -3
- package/assets/samples/tasks/_template/plan.md +4 -1
- package/assets/skills/agent-kit.md +40 -25
- package/bin/agent-kit.d.ts.map +1 -1
- package/bin/agent-kit.js +9 -2
- package/bin/agent-kit.js.map +1 -1
- package/lib/cargo-prose.d.ts +41 -0
- package/lib/cargo-prose.d.ts.map +1 -0
- package/lib/cargo-prose.js +88 -0
- package/lib/cargo-prose.js.map +1 -0
- package/lib/cargo.d.ts +12 -1
- package/lib/cargo.d.ts.map +1 -1
- package/lib/cargo.js +8 -2
- package/lib/cargo.js.map +1 -1
- package/lib/commands.d.ts +1 -1
- package/lib/commands.d.ts.map +1 -1
- package/lib/commands.js +59 -2
- package/lib/commands.js.map +1 -1
- package/lib/fate.d.ts +29 -0
- package/lib/fate.d.ts.map +1 -0
- package/lib/fate.js +147 -0
- package/lib/fate.js.map +1 -0
- package/lib/hooks-map.d.ts +5 -0
- package/lib/hooks-map.d.ts.map +1 -1
- package/lib/hooks-map.js +9 -3
- package/lib/hooks-map.js.map +1 -1
- package/lib/override-marks.d.ts +7 -0
- package/lib/override-marks.d.ts.map +1 -1
- package/lib/override-marks.js +14 -0
- package/lib/override-marks.js.map +1 -1
- package/lib/plan.d.ts +2 -0
- package/lib/plan.d.ts.map +1 -1
- package/lib/plan.js +8 -0
- package/lib/plan.js.map +1 -1
- package/lib/proposals.d.ts +25 -0
- package/lib/proposals.d.ts.map +1 -1
- package/lib/proposals.js +43 -1
- package/lib/proposals.js.map +1 -1
- package/lib/ship.d.ts +29 -2
- package/lib/ship.d.ts.map +1 -1
- package/lib/ship.js +34 -0
- package/lib/ship.js.map +1 -1
- package/lib/shipment.d.ts.map +1 -1
- package/lib/shipment.fixture.d.ts +8 -1
- package/lib/shipment.fixture.d.ts.map +1 -1
- package/lib/shipment.fixture.js +9 -0
- package/lib/shipment.fixture.js.map +1 -1
- package/lib/shipment.js +40 -14
- package/lib/shipment.js.map +1 -1
- package/package.json +1 -1
- package/rt-tools-agent-kit-0.28.0.tgz +0 -0
- package/rt-tools-agent-kit-0.26.0.tgz +0 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Delivery conditions about the epic of a task. NOT a guard: it has no `rt-hook:` declaration and
|
|
3
|
+
# it hooks into no agent event. The delivery guard sources it — the same way it sources the task
|
|
4
|
+
# folder conditions and the signature.
|
|
5
|
+
#
|
|
6
|
+
# Why a file of its own. The subject is one — the epic a task hangs on — and it shows at two call
|
|
7
|
+
# sites: the base of a new branch and the base of the request. Together with the command parsing,
|
|
8
|
+
# the signature, the folder and the work queue it would take the guard past the file length limit,
|
|
9
|
+
# and the guard cannot be split by event: it is one whole, and its command parsing is shared.
|
|
10
|
+
#
|
|
11
|
+
# WHAT IS HERE. Two calls, both called from the delivery guard, where `fault`, `main_branch` and
|
|
12
|
+
# the profile calls are already declared. The helper reads neither the input nor the settings.
|
|
13
|
+
|
|
14
|
+
# The branch of an epic by its number: the same form as a task branch — the profile pulls the
|
|
15
|
+
# number out of a name, and the epic branch is the one whose number is the epic's.
|
|
16
|
+
#
|
|
17
|
+
# The branch is looked for among the remote refs, not the local ones: a branch that exists only on
|
|
18
|
+
# somebody's machine is a base nobody else has, and the branches of the tasks are taken from it.
|
|
19
|
+
# Several matched — none is chosen: two branches of one epic is a discrepancy of its own, and a
|
|
20
|
+
# guess here would name a base at random.
|
|
21
|
+
rt_epic_branch() {
|
|
22
|
+
_epic="$1"
|
|
23
|
+
_found=''
|
|
24
|
+
for _ref in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin 2>/dev/null); do
|
|
25
|
+
_name="${_ref#origin/}"
|
|
26
|
+
[ "$(rt_task_branch_number "$_name" 2>/dev/null)" = "$_epic" ] || continue
|
|
27
|
+
_found="${_found}${_found:+ }${_name}"
|
|
28
|
+
done
|
|
29
|
+
printf '%s' "$_found"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
# The base of a new task branch against the branch of its epic.
|
|
33
|
+
#
|
|
34
|
+
# The task branch is taken from the branch of the epic, and a branch taken from the main one leaves
|
|
35
|
+
# the epic half merged before its last task is done: the merge of the epic then carries nothing of
|
|
36
|
+
# that task. The guard used to judge the base against the main branch alone and let this through in
|
|
37
|
+
# silence.
|
|
38
|
+
#
|
|
39
|
+
# The freshness of the main branch is not dropped, it is moved: it is asked of the branch of the
|
|
40
|
+
# epic, and there it is fixed by one merge — while asked of the base of the task branch it made the
|
|
41
|
+
# executor either wait for the epic branch to catch up or take the base from the main one, that is,
|
|
42
|
+
# do exactly what this condition forbids.
|
|
43
|
+
#
|
|
44
|
+
# Arguments: the number of the epic, the ref the branch grows from, the name of the new branch.
|
|
45
|
+
rt_epic_base() {
|
|
46
|
+
_epic="$1"
|
|
47
|
+
_base_ref="$2"
|
|
48
|
+
_branch="$3"
|
|
49
|
+
[ -n "$_epic" ] || return 0
|
|
50
|
+
|
|
51
|
+
_epic_branch="$(rt_epic_branch "$_epic")"
|
|
52
|
+
case "$_epic_branch" in
|
|
53
|
+
'')
|
|
54
|
+
fault "the task belongs to the epic #${_epic}, and no branch of that epic is in the remote. The branch of the epic is taken before the first of its tasks and sent at once — a branch that exists only on one machine is a base nobody else has."
|
|
55
|
+
return 0
|
|
56
|
+
;;
|
|
57
|
+
*' '*)
|
|
58
|
+
fault "the epic #${_epic} has more than one branch in the remote — ${_epic_branch}. There is no telling which of them the task branches from; leave one."
|
|
59
|
+
return 0
|
|
60
|
+
;;
|
|
61
|
+
esac
|
|
62
|
+
|
|
63
|
+
git rev-parse --verify --quiet "origin/${_epic_branch}" >/dev/null 2>&1 || return 0
|
|
64
|
+
|
|
65
|
+
if ! git merge-base --is-ancestor "origin/${_epic_branch}" "$_base_ref" 2>/dev/null; then
|
|
66
|
+
fault "the branch will grow from a base that does not carry the branch of the epic «${_epic_branch}». A task branched from the main one leaves its epic half merged before the epic itself is handed in. Take the base from the epic: git fetch origin && git checkout -b ${_branch} origin/${_epic_branch}."
|
|
67
|
+
return 0
|
|
68
|
+
fi
|
|
69
|
+
|
|
70
|
+
# The main branch is asked of the epic, not of the base: there it is fixed by one merge, and the
|
|
71
|
+
# branches of all its tasks get it at once.
|
|
72
|
+
if git rev-parse --verify --quiet "origin/${main_branch}" >/dev/null 2>&1 \
|
|
73
|
+
&& ! git merge-base --is-ancestor "origin/${main_branch}" "origin/${_epic_branch}" 2>/dev/null; then
|
|
74
|
+
_behind="$(git rev-list --count "origin/${_epic_branch}..origin/${main_branch}" 2>/dev/null)"
|
|
75
|
+
fault "the branch of the epic «${_epic_branch}» does not carry the tip of «${main_branch}» — it has moved ahead by ${_behind:-several} commits. The main branch is merged into the branch of the epic, not bypassed by taking the base from the main one: git checkout ${_epic_branch}, git merge origin/${main_branch}, and send the branch."
|
|
76
|
+
fi
|
|
77
|
+
|
|
78
|
+
return 0
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
# The base of a request about a task of an epic.
|
|
82
|
+
#
|
|
83
|
+
# A request of a task goes into the branch of its epic, and one opened into the main branch takes
|
|
84
|
+
# the task past the epic: the epic is then handed in without it, and the reviewer sees the edit
|
|
85
|
+
# next to everything that lies in the main branch and not in the epic. The guard let any base
|
|
86
|
+
# through and asked only that the main branch be merged in.
|
|
87
|
+
#
|
|
88
|
+
# The freshness asked here is of the epic, not of the main branch: the request goes into the epic,
|
|
89
|
+
# and it is the divergence with the epic that shows in the diff. The main branch reaches the task
|
|
90
|
+
# through the epic — that is the same order as at the creation of the branch.
|
|
91
|
+
#
|
|
92
|
+
# Arguments: the number of the epic and the text of the command.
|
|
93
|
+
rt_epic_pull_base() {
|
|
94
|
+
_epic="$1"
|
|
95
|
+
_cmd="$2"
|
|
96
|
+
[ -n "$_epic" ] || return 0
|
|
97
|
+
|
|
98
|
+
_epic_branch="$(rt_epic_branch "$_epic")"
|
|
99
|
+
case "$_epic_branch" in
|
|
100
|
+
'' | *' '*) return 0 ;;
|
|
101
|
+
esac
|
|
102
|
+
|
|
103
|
+
# The base named by the command. Absent — the hosting takes the default branch of the
|
|
104
|
+
# repository, that is the main one: the very case this condition is about.
|
|
105
|
+
_base=''
|
|
106
|
+
if command -v perl >/dev/null 2>&1; then
|
|
107
|
+
_base="$(printf '%s' "$_cmd" | perl -0ne '
|
|
108
|
+
if (/(?:^|\s)(?:--base|-B)(?:=|\s+)(?:"((?:[^"\\]|\\.)*)"|\x27([^\x27]*)\x27|(\S+))/s) {
|
|
109
|
+
print defined $1 ? $1 : (defined $2 ? $2 : $3);
|
|
110
|
+
}
|
|
111
|
+
' 2>/dev/null)"
|
|
112
|
+
fi
|
|
113
|
+
|
|
114
|
+
if [ "$_base" != "$_epic_branch" ]; then
|
|
115
|
+
fault "the request of a task of the epic #${_epic} goes into the branch of the epic «${_epic_branch}», and the base here is ${_base:-the default branch of the repository}. A request into the main branch takes the task past its epic: the epic is handed in without it. Name the base: --base ${_epic_branch}."
|
|
116
|
+
return 0
|
|
117
|
+
fi
|
|
118
|
+
|
|
119
|
+
if git rev-parse --verify --quiet "origin/${_epic_branch}" >/dev/null 2>&1 \
|
|
120
|
+
&& ! git merge-base --is-ancestor "origin/${_epic_branch}" HEAD 2>/dev/null; then
|
|
121
|
+
_behind="$(git rev-list --count "HEAD..origin/${_epic_branch}" 2>/dev/null)"
|
|
122
|
+
fault "the branch of the epic «${_epic_branch}» has moved ahead by ${_behind:-several} commits and is not merged into this branch. The reviewer would see the edit mixed with someone else's: git fetch origin && git merge origin/${_epic_branch}."
|
|
123
|
+
fi
|
|
124
|
+
|
|
125
|
+
return 0
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
# The request of an epic itself: it goes into the main branch, and it opens only when the folders
|
|
129
|
+
# of all its tasks are taken apart.
|
|
130
|
+
#
|
|
131
|
+
# The folder guard reads the folder of one task — by the name of the branch — and the branch of an
|
|
132
|
+
# epic has none of its own: every folder lying there belongs to a task of the epic whose work is
|
|
133
|
+
# not closed. Merged into the main branch, such an epic carries the folders of unfinished tasks
|
|
134
|
+
# there, and the reader of the main branch has no way to tell them from current work.
|
|
135
|
+
#
|
|
136
|
+
# The epic is recognised by the label of its card, not by the shape of the branch name: the branch
|
|
137
|
+
# of an epic and the branch of a task are named alike, and the label is the only thing that tells
|
|
138
|
+
# them apart. The tree has not named the label — the condition is not judged at all.
|
|
139
|
+
#
|
|
140
|
+
# Arguments: the state of the card of the current branch, as the work queue answered it.
|
|
141
|
+
rt_epic_own_pull() {
|
|
142
|
+
_state="$1"
|
|
143
|
+
[ -n "$epic_label" ] || return 0
|
|
144
|
+
[ -n "$_state" ] || return 0
|
|
145
|
+
[ -n "$tasks_dir" ] || return 0
|
|
146
|
+
|
|
147
|
+
printf '%s' "$_state" | jq -e --arg l "$epic_label" '(.labels // []) | index($l)' >/dev/null 2>&1 || return 0
|
|
148
|
+
|
|
149
|
+
_folders="$(git ls-tree -d --name-only HEAD -- "$tasks_dir" 2>/dev/null)"
|
|
150
|
+
[ -n "$_folders" ] || return 0
|
|
151
|
+
|
|
152
|
+
# What lies in the tasks directory of the branch, one level down: the sample and the index of
|
|
153
|
+
# the directory are not folders of tasks and do not hold the request.
|
|
154
|
+
_left=''
|
|
155
|
+
for _entry in $(git ls-tree -d --name-only "HEAD:${tasks_dir}" 2>/dev/null); do
|
|
156
|
+
case "$_entry" in
|
|
157
|
+
_template | _draft-*) continue ;;
|
|
158
|
+
esac
|
|
159
|
+
_left="${_left}${_left:+, }${_entry}"
|
|
160
|
+
done
|
|
161
|
+
[ -n "$_left" ] || return 0
|
|
162
|
+
|
|
163
|
+
fault "the branch of the epic still carries the folders of its tasks — ${_left}. The request of an epic opens when the last of its folders is taken apart: merged as it is, the epic takes the folders of unfinished work into the main branch, and the reader has no way to tell them from current work."
|
|
164
|
+
|
|
165
|
+
return 0
|
|
166
|
+
}
|
|
167
|
+
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
# The signature of a machine commit for the delivery guard: whose commit it is, whether it is signed
|
|
3
3
|
# with the right mail and what to do if it is not.
|
|
4
4
|
#
|
|
5
|
+
# NOT a guard: it has no `rt-hook:` declaration and hooks into no agent event. The delivery guard
|
|
6
|
+
# sources it — one tier of its verdict, moved out when the guard reached its length limit.
|
|
7
|
+
#
|
|
5
8
|
# There is deliberately no `# rt-hook:` line here: the event and the call pattern are declared by the
|
|
6
9
|
# guard itself, and a helper next to it is not registered as a hook and decides nothing alone. It is
|
|
7
10
|
# called from the guard and uses the guard variables — the command, the declared mail, the name of
|
|
@@ -75,6 +78,33 @@ if printf '%s' "$cmd" | grep -qE "${RT_CMD_BOUND}git([[:space:]]|\$)" \
|
|
|
75
78
|
bot_login="${commit_email%%@*}"
|
|
76
79
|
bot_login="${bot_login##*+}"
|
|
77
80
|
|
|
81
|
+
# The repair named in the refusal is itself a commit, and without this exception the guard
|
|
82
|
+
# refused it together with everything else: the divergence is removed only by a commit,
|
|
83
|
+
# and a commit is refused while the divergence is there. The circle closed, and there was
|
|
84
|
+
# no way out of it by the guard's own means — the second advice, rewriting the whole
|
|
85
|
+
# contribution, demands a clean working tree, and saving the work is a commit too.
|
|
86
|
+
#
|
|
87
|
+
# The repair is recognised not by trusting the line — the note above rightly refuses that —
|
|
88
|
+
# but by the declared mail: the guard looks in the call for exactly the address it declared
|
|
89
|
+
# itself, in both variables at once, next to a rewrite of the last commit. Faking that means
|
|
90
|
+
# putting the right signature, that is doing precisely what the guard demands.
|
|
91
|
+
#
|
|
92
|
+
# The quotes are stripped before matching: the same command is typed with double quotes,
|
|
93
|
+
# with single ones and without any, and a sign counting on one form stays silent on the
|
|
94
|
+
# other two — that is, refuses the repair again and says nothing about why.
|
|
95
|
+
# A push is never a repair, whatever stands in its variables: the divergence has to be
|
|
96
|
+
# removed BEFORE the contribution leaves, and after it only a force push helps. Without
|
|
97
|
+
# this line a compound call carrying both the rewrite and the push would take the whole
|
|
98
|
+
# exception onto the push as well.
|
|
99
|
+
repair=''
|
|
100
|
+
probe="$(printf '%s' "$cmd" | tr -d "\"'")"
|
|
101
|
+
if ! printf '%s' "$probe" | sed -E 's/git[[:space:]]+stash[[:space:]]+push/git stash/g' | grep -qE '(^|[[:space:]])push([[:space:]]|$)' \
|
|
102
|
+
&& printf '%s' "$probe" | grep -qF "GIT_AUTHOR_EMAIL=${commit_email}" \
|
|
103
|
+
&& printf '%s' "$probe" | grep -qF "GIT_COMMITTER_EMAIL=${commit_email}" \
|
|
104
|
+
&& printf '%s' "$probe" | grep -qE '(^|[[:space:]])--amend([[:space:]]|$)'; then
|
|
105
|
+
repair=1
|
|
106
|
+
fi
|
|
107
|
+
|
|
78
108
|
strangers=''
|
|
79
109
|
while IFS="$(printf '\t')" read -r short author email; do
|
|
80
110
|
[ -z "$short" ] && continue
|
|
@@ -112,12 +142,12 @@ $(git log --format='%h%x09%an%x09%ae' "origin/${main_branch}..HEAD" 2>/dev/null)
|
|
|
112
142
|
EOF
|
|
113
143
|
fi
|
|
114
144
|
|
|
115
|
-
[ -n "$unknown" ] \
|
|
145
|
+
[ -n "$unknown" ] && [ -z "$repair" ] \
|
|
116
146
|
&& deny "BLOCKED: a commit of the contribution is signed by an account the tree never declared. Known are the machine account ${commit_email} and the emails of people named by the profile; everything else the hosting attributes to whoever owns that address, and from inside the history the miss is invisible. Diverging: ${unknown}. Rewrite the signature before the push, after it only a force push fixes this:
|
|
117
147
|
the last commit — GIT_AUTHOR_EMAIL=\"${commit_email}\" GIT_COMMITTER_EMAIL=\"${commit_email}\" git commit --amend --no-edit --reset-author
|
|
118
148
|
the whole contribution of the branch — git filter-branch -f --env-filter 'GIT_AUTHOR_EMAIL=\"${commit_email}\"; GIT_COMMITTER_EMAIL=\"${commit_email}\"' origin/${main_branch}..HEAD"
|
|
119
149
|
|
|
120
|
-
[ -n "$strangers" ] \
|
|
150
|
+
[ -n "$strangers" ] && [ -z "$repair" ] \
|
|
121
151
|
&& deny "BLOCKED: a machine commit is signed by an email other than the one the tree declared. The hosting matches a service address by the number in it, and a commit with a foreign number it attributes to an outside person — from inside the miss is invisible, because the name of the account next to it is right. Diverging: ${strangers}. Declared: ${commit_email} — the email is taken from there, not typed from memory. Rewrite the signature before the push, after it only a force push fixes this:
|
|
122
152
|
the last commit — GIT_AUTHOR_NAME=\"${bot_login}\" GIT_AUTHOR_EMAIL=\"${commit_email}\" GIT_COMMITTER_NAME=\"${bot_login}\" GIT_COMMITTER_EMAIL=\"${commit_email}\" git commit --amend --no-edit --reset-author
|
|
123
153
|
the whole contribution of the branch — git filter-branch -f --env-filter 'GIT_AUTHOR_EMAIL=\"${commit_email}\"; GIT_COMMITTER_EMAIL=\"${commit_email}\"' origin/${main_branch}..HEAD"
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# The tree a command runs in, for the delivery guard: the form of a branch name is judged by the
|
|
3
|
+
# profile of that tree, not of the tree the session was started from.
|
|
4
|
+
#
|
|
5
|
+
# NOT a guard: it has no `rt-hook:` declaration and hooks into no agent event. The delivery guard
|
|
6
|
+
# sources it — one tier of its verdict, moved out when the guard reached its length limit.
|
|
7
|
+
#
|
|
8
|
+
# Why exactly so. A session works in a neighbouring tree by the direct word of the owner: the task
|
|
9
|
+
# is created there, the number and the short name are right, and the branch name is lawful by that
|
|
10
|
+
# tree's key. The guard used to compare it with the key of the tree the session stands in and
|
|
11
|
+
# refused — a refusal with no lawful move at all, and the work stopped whole: neither a branch nor a
|
|
12
|
+
# commit. The same refusal caught an edit of an outside file: it was enough for a neighbouring
|
|
13
|
+
# tree's branch name to reach the command text as a line of a document.
|
|
14
|
+
#
|
|
15
|
+
# The directory of execution is taken from the command itself — from the move into it. The directory
|
|
16
|
+
# did not differ from the session's tree — everything goes as before. A tree with no profile of its own is not judged at all: a foreign tree is not accountable
|
|
17
|
+
# to this guard, and a refusal on a lawful name has no bypass.
|
|
18
|
+
#
|
|
19
|
+
# FAIL-OPEN: no move in the command, no profile in the tree it moves to — the form is not judged.
|
|
20
|
+
|
|
21
|
+
# Where the command runs, if it says so itself — the move at the start of the call. Prints the tree
|
|
22
|
+
# root or stays silent.
|
|
23
|
+
rt_delivery_exec_dir() {
|
|
24
|
+
named="$(printf '%s' "$cmd" | sed -nE 's/.*(^|[;&|[:space:]])cd[[:space:]]+([^[:space:];&|]+).*/\2/p' | head -1)"
|
|
25
|
+
named="${named%\'}"; named="${named#\'}"
|
|
26
|
+
named="${named%\"}"; named="${named#\"}"
|
|
27
|
+
[ -z "$named" ] && return 0
|
|
28
|
+
[ -d "$named" ] || return 0
|
|
29
|
+
git -C "$named" rev-parse --show-toplevel 2>/dev/null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
# Is the branch name lawful. The tree of execution answers; where it is the session's own tree, or
|
|
33
|
+
# says nothing of itself, the answer comes from the profile already loaded.
|
|
34
|
+
rt_delivery_branch_form_ok() {
|
|
35
|
+
named="$1"
|
|
36
|
+
other="$(rt_delivery_exec_dir)"
|
|
37
|
+
if [ -n "$other" ] && [ "$other" != "$root" ]; then
|
|
38
|
+
[ -f "$other/.claude/rt-kit/project.sh" ] || return 0
|
|
39
|
+
(
|
|
40
|
+
for profile in "$other/.claude/rt-kit/defaults/project.sh" "$other/.claude/rt-kit/project.sh"; do
|
|
41
|
+
# shellcheck disable=SC1090
|
|
42
|
+
[ -f "$profile" ] && . "$profile" 2>/dev/null
|
|
43
|
+
done
|
|
44
|
+
command -v rt_task_branch_ok >/dev/null 2>&1 || exit 0
|
|
45
|
+
rt_task_branch_ok "$named"
|
|
46
|
+
)
|
|
47
|
+
return $?
|
|
48
|
+
fi
|
|
49
|
+
rt_task_branch_ok "$named"
|
|
50
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
2
|
# rt-hook: PreToolUse Bash|mcp__webstorm__execute_terminal_command|mcp__webstorm__execute_tool
|
|
3
|
-
# Requires: hooks/git-guard-delivery-folder.sh, hooks/git-guard-delivery-conflict.sh, hooks/profile-check.sh, hooks/deny-tail.sh, hooks/guard-note.sh
|
|
3
|
+
# Requires: hooks/git-guard-delivery-folder.sh, hooks/git-guard-delivery-epic.sh, hooks/git-guard-delivery-conflict.sh, hooks/profile-check.sh, hooks/deny-tail.sh, hooks/guard-note.sh
|
|
4
4
|
# Delivery guard. PreToolUse on creating a branch, on the push and on opening a PR.
|
|
5
5
|
#
|
|
6
6
|
# The delivery law demands three things nothing usually checks: an edit starts from a task visible
|
|
@@ -107,6 +107,7 @@ task_move="${RT_TASK_MOVE_CMD:-npm run task:move}"
|
|
|
107
107
|
# a tree names its columns in its own words, and an invented name would match nothing and would
|
|
108
108
|
# silently switch the check off.
|
|
109
109
|
backlog_column="${RT_BOARD_BACKLOG:-}"
|
|
110
|
+
epic_label="${RT_BOARD_EPIC_LABEL:-}"
|
|
110
111
|
task_bot="${RT_TASK_BOT:-}"
|
|
111
112
|
# The identity of the call arrives through the environment, not as a word in the line: from the
|
|
112
113
|
# command text only one thing is visible — whether the token is substituted explicitly. A tree that
|
|
@@ -144,6 +145,12 @@ deny() {
|
|
|
144
145
|
[ -f "$rt_hooks_dir/deny-tail.sh" ] && . "$rt_hooks_dir/deny-tail.sh" 2>/dev/null
|
|
145
146
|
reason="$1"
|
|
146
147
|
command -v rt_deny_tail >/dev/null 2>&1 && reason="$1 $(rt_deny_tail "$2")"
|
|
148
|
+
# A neighbour's conflict travels inside the refusal: the guard says one thing per call, and a
|
|
149
|
+
# second JSON document next to the first is read as plain text — that is, as no refusal at all.
|
|
150
|
+
[ -n "${rt_delivery_neighbour_note:-}" ] && reason="${reason}
|
|
151
|
+
|
|
152
|
+
${rt_delivery_neighbour_note}"
|
|
153
|
+
rt_delivery_said=1
|
|
147
154
|
|
|
148
155
|
jq -n --arg r "$reason" '{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:$r}}' 2>/dev/null \
|
|
149
156
|
|| printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"The delivery guard."}}\n'
|
|
@@ -168,12 +175,26 @@ fault() {
|
|
|
168
175
|
# shellcheck disable=SC1090
|
|
169
176
|
[ -f "$rt_hooks_dir/git-guard-delivery-folder.sh" ] && . "$rt_hooks_dir/git-guard-delivery-folder.sh" 2>/dev/null
|
|
170
177
|
|
|
178
|
+
# The epic of a task: the same technique as with the folder. It is sourced before the branch block —
|
|
179
|
+
# there the base of a new branch is judged, and with an epic it is judged against the branch of the
|
|
180
|
+
# epic. No helper — the epic is not judged, and the base is asked against the main branch as before.
|
|
181
|
+
# shellcheck disable=SC1090
|
|
182
|
+
[ -f "$rt_hooks_dir/git-guard-delivery-epic.sh" ] && . "$rt_hooks_dir/git-guard-delivery-epic.sh" 2>/dev/null
|
|
183
|
+
|
|
171
184
|
# A conflicting PR of one's own: the same technique as with the folder and the signature. The helper
|
|
172
185
|
# is called before all the tiers below and judges not the readiness of this work but the right to
|
|
173
186
|
# take the next one: while what was handed over conflicts, it is fixed by the first action of the
|
|
174
187
|
# turn. No helper — the tier is not judged, and the work goes on.
|
|
175
188
|
# shellcheck disable=SC1090
|
|
189
|
+
# The tree the command runs in: the form of a branch name is judged by its profile, not by the
|
|
190
|
+
# profile of the tree the session was started from. No helper — the form is judged as before.
|
|
191
|
+
# shellcheck disable=SC1090
|
|
192
|
+
[ -f "$rt_hooks_dir/git-guard-delivery-tree.sh" ] && . "$rt_hooks_dir/git-guard-delivery-tree.sh" 2>/dev/null
|
|
193
|
+
command -v rt_delivery_branch_form_ok >/dev/null 2>&1 \
|
|
194
|
+
|| rt_delivery_branch_form_ok() { rt_task_branch_ok "$1"; }
|
|
195
|
+
|
|
176
196
|
[ -f "$rt_hooks_dir/git-guard-delivery-conflict.sh" ] && . "$rt_hooks_dir/git-guard-delivery-conflict.sh" 2>/dev/null
|
|
197
|
+
command -v rt_delivery_note_out >/dev/null 2>&1 && trap rt_delivery_note_out EXIT
|
|
177
198
|
command -v rt_delivery_conflict >/dev/null 2>&1 && rt_delivery_conflict
|
|
178
199
|
|
|
179
200
|
# The refusal on what has piled up. Empty — the calling side goes on.
|
|
@@ -236,9 +257,13 @@ if [ -n "$branch_arg" ]; then
|
|
|
236
257
|
# number at all.
|
|
237
258
|
number_arg="$(rt_task_branch_number "$branch_arg")"
|
|
238
259
|
if [ -n "$number_arg" ]; then
|
|
239
|
-
|
|
260
|
+
rt_delivery_branch_form_ok "$branch_arg" \
|
|
240
261
|
|| deny "BLOCKED: the branch name «${branch_arg}» is not of the form accepted here. The branch number is the same as the number of the task and of the title of the merge request."
|
|
262
|
+
state=''
|
|
241
263
|
check_task "$number_arg" "the branch «${branch_arg}»"
|
|
264
|
+
# The epic of the task: with one, the base is judged against the branch of the epic, and the
|
|
265
|
+
# freshness of the main branch moves there too — see `git-guard-delivery-epic.sh`.
|
|
266
|
+
epic_arg="$(printf '%s' "$state" | jq -r '.epic // empty' 2>/dev/null)"
|
|
242
267
|
|
|
243
268
|
# The delivery conditions already known here are checked here. After the work is done, the
|
|
244
269
|
# base is fixed by a merge with conflict resolution, and the commit signature by rewriting
|
|
@@ -249,7 +274,9 @@ if [ -n "$branch_arg" ]; then
|
|
|
249
274
|
# base fresh — `git checkout -b <branch> origin/<main>` — would be forbidden.
|
|
250
275
|
base_arg="$(printf '%s' "$cmd" | sed -nE 's/.*git[[:space:]]+(checkout([[:space:]]+-[A-Za-z-]+)*[[:space:]]+-b|switch([[:space:]]+-[A-Za-z-]+)*[[:space:]]+-c)[[:space:]]+[^[:space:];&|]+[[:space:]]+([^[:space:];&|-][^[:space:];&|]*).*/\4/p' | head -1)"
|
|
251
276
|
base_ref="${base_arg:-HEAD}"
|
|
252
|
-
if
|
|
277
|
+
if [ -n "$epic_arg" ] && command -v rt_epic_base >/dev/null 2>&1; then
|
|
278
|
+
rt_epic_base "$epic_arg" "$base_ref" "$branch_arg"
|
|
279
|
+
elif git rev-parse --verify --quiet "refs/remotes/origin/${main_branch}" >/dev/null 2>&1 \
|
|
253
280
|
&& git rev-parse --verify --quiet "$base_ref" >/dev/null 2>&1 \
|
|
254
281
|
&& ! git merge-base --is-ancestor "origin/${main_branch}" "$base_ref" 2>/dev/null; then
|
|
255
282
|
behind="$(git rev-list --count "${base_ref}..origin/${main_branch}" 2>/dev/null)"
|
|
@@ -327,6 +354,16 @@ rt_task_branch_ok "$branch" \
|
|
|
327
354
|
|
|
328
355
|
number="$(rt_task_branch_number "$branch")"
|
|
329
356
|
|
|
357
|
+
# The work queue is asked here and not at the end of the block: the epic of the task decides what
|
|
358
|
+
# the base of the request is judged against, and that judging stands above. The call is one — the
|
|
359
|
+
# state stays in `state` and the tail of the block reads the same answer.
|
|
360
|
+
state=''
|
|
361
|
+
check_task "$number" "the request from the branch «${branch}»" yes
|
|
362
|
+
epic_pull="$(printf '%s' "$state" | jq -r '.epic // empty' 2>/dev/null)"
|
|
363
|
+
# The branch of an epic itself: its request goes into the main branch, and it opens only when the
|
|
364
|
+
# folders of all its tasks are taken apart.
|
|
365
|
+
command -v rt_epic_own_pull >/dev/null 2>&1 && rt_epic_own_pull "$state"
|
|
366
|
+
|
|
330
367
|
title=''
|
|
331
368
|
if command -v perl >/dev/null 2>&1; then
|
|
332
369
|
title="$(printf '%s' "$cmd" | perl -0ne '
|
|
@@ -358,7 +395,9 @@ fi
|
|
|
358
395
|
# the remote reference: without it, silence means only "the local reference is not older than the
|
|
359
396
|
# branch", while it reads as "the main branch is merged in". No answer from the network — a silent
|
|
360
397
|
# skip; the waiting limit is set by git variables, an external `timeout` is not on every machine.
|
|
361
|
-
if
|
|
398
|
+
if [ -n "$epic_pull" ] && command -v rt_epic_pull_base >/dev/null 2>&1; then
|
|
399
|
+
rt_epic_pull_base "$epic_pull" "$cmd"
|
|
400
|
+
elif git rev-parse --verify --quiet "refs/remotes/origin/${main_branch}" >/dev/null 2>&1 \
|
|
362
401
|
&& ! git merge-base --is-ancestor "origin/${main_branch}" HEAD 2>/dev/null; then
|
|
363
402
|
behind="$(git rev-list --count "HEAD..origin/${main_branch}" 2>/dev/null)"
|
|
364
403
|
fault "«${main_branch}» has moved ahead by ${behind:-several} commits and is not merged into the branch. A PR from a diverged branch shows the reviewer the edit mixed with someone else, and the checks on it run from a stale base. Merge it in and repeat: git fetch origin && git merge origin/${main_branch} — the order and the resolving of the conflict are in the pattern git-workflow-merge."
|
|
@@ -384,30 +423,36 @@ fi
|
|
|
384
423
|
|
|
385
424
|
# The identity of the call. The hosting client holds two accounts — the logged-in one and the one
|
|
386
425
|
# whose token stands in the call environment; which of them opens the PR is visible from the command
|
|
387
|
-
# only through an explicit substitution.
|
|
388
|
-
# a PR is never its reviewer, and the author cannot be changed.
|
|
426
|
+
# only through an explicit substitution.
|
|
389
427
|
#
|
|
390
|
-
#
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
#
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
#
|
|
400
|
-
#
|
|
401
|
-
#
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
if [ -
|
|
407
|
-
|
|
408
|
-
elif [ "$token_login" != "$task_bot" ]; then
|
|
409
|
-
|
|
428
|
+
# What is judged is not the name of the account but the clash with the reviewer: the author of a PR
|
|
429
|
+
# is never its reviewer, the hosting silently drops such a review request, and the author cannot be
|
|
430
|
+
# changed afterwards. A PR opened by any other account is lawful while its reviewer is somebody
|
|
431
|
+
# else — judging the name instead leaves a machine that holds no token with no way to deliver at all.
|
|
432
|
+
# The value is taken as one word up to the next space: a login has no spaces in it, and an alphabet
|
|
433
|
+
# listed by ranges would silently not recognise a neighbouring one — the value then reads as absent,
|
|
434
|
+
# and the clash with the reviewer goes unnoticed.
|
|
435
|
+
pull_reviewer="$(printf '%s' "$cmd" | sed -n "s/.*--reviewer[= ]\{1,\}['\"]\{0,1\}\([^[:space:]'\"]\{1,\}\).*/\1/p" | head -1)"
|
|
436
|
+
|
|
437
|
+
# Who will open the PR is asked of the hosting by the tree: the hosting, the client and the path to
|
|
438
|
+
# the token are each their own. The answer covers both accounts at once — the substitution reads a
|
|
439
|
+
# file that may not be on the machine, and then the client answers from the logged-in one.
|
|
440
|
+
token_login=''
|
|
441
|
+
command -v rt_pull_token_login >/dev/null 2>&1 && token_login="$(rt_pull_token_login 2>/dev/null)"
|
|
442
|
+
|
|
443
|
+
if [ -n "$token_login" ]; then
|
|
444
|
+
if [ -n "$pull_reviewer" ] && [ "$token_login" = "$pull_reviewer" ]; then
|
|
445
|
+
fault "the request would be opened by the account «${token_login}», and the same one is named as its reviewer. The hosting accepts such a review request and silently does not create it, and the author of a request cannot be changed — it is fixed only by reopening. Open it by another account${pull_token_hint:+, substituting the token: ${pull_token_hint} …}, or name another reviewer."
|
|
446
|
+
elif [ -n "$task_bot" ] && [ "$token_login" != "$task_bot" ]; then
|
|
447
|
+
# Not a refusal: the reviewer is somebody else, and the review will be created. But silence
|
|
448
|
+
# about a foreign account is indistinguishable from a check that did not fire at all.
|
|
449
|
+
printf 'the delivery guard: the request will come out from the account «%s», not the machine one «%s». The reviewer is another one, so the review will be created.\n' "$token_login" "$task_bot" >&2
|
|
410
450
|
fi
|
|
451
|
+
elif [ -n "$pull_token_var" ] \
|
|
452
|
+
&& ! printf '%s' "$cmd" | grep -qE "(^|[;&|(]|&&|\|\||[[:space:]])${pull_token_var}="; then
|
|
453
|
+
# There is nothing to learn the author by — no helper, or an empty answer from it. Then the only
|
|
454
|
+
# lawful call is the one that names the account in its own text.
|
|
455
|
+
fault "who will open the request cannot be learned: the hosting was not asked, and the command carries no substitution of «${pull_token_var}». Opened by the logged-in account, the request may come out from the very person named as its reviewer, and that is fixed only by reopening.${pull_token_hint:+ Substitute the token: ${pull_token_hint} …}"
|
|
411
456
|
fi
|
|
412
457
|
|
|
413
458
|
# The PR body carries the section about the remaining step from the minute it opens: without it the
|
|
@@ -437,7 +482,6 @@ if [ -n "$pull_body_section" ]; then
|
|
|
437
482
|
fi
|
|
438
483
|
fi
|
|
439
484
|
|
|
440
|
-
check_task "$number" "the request from the branch «${branch}»" yes
|
|
441
485
|
|
|
442
486
|
# The task folder is taken apart before the PR opens, not after the approval: the owner merges as
|
|
443
487
|
# soon as he sees green, and no room is left for a closing commit — three times in a row the folder
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# rt-hook: PreToolUse Bash|mcp__webstorm__execute_terminal_command|mcp__webstorm__execute_tool
|
|
3
|
+
# Requires: hooks/deny-tail.sh, hooks/guard-note.sh
|
|
4
|
+
# Guard of the working tree. PreToolUse on a git command that throws uncommitted edits away.
|
|
5
|
+
#
|
|
6
|
+
# `git reset --hard`, `git checkout -- <path>`, `git restore <path>` and `git clean -f` erase what
|
|
7
|
+
# lies in the working tree and is not committed, and git keeps no object of it: `git fsck
|
|
8
|
+
# --lost-found` finds nothing afterwards. In one working copy two sessions work at once, so the
|
|
9
|
+
# erased edits are as often a neighbour's as one's own — seven files of a neighbouring task went
|
|
10
|
+
# that way, and there was nothing to restore them from. The rule `git-workflow` says: a commit is
|
|
11
|
+
# dropped by `--soft`, and `--hard` is taken only to discard edits named before the call.
|
|
12
|
+
#
|
|
13
|
+
# The guard judges two things: the verb with its discarding form in the command, and a non-empty
|
|
14
|
+
# working tree in the directory the command runs in. A clean tree passes: there is nothing to lose.
|
|
15
|
+
# The lawful bypass is the comment `# discard: <reason>` in the same command — the edits are named
|
|
16
|
+
# by whoever throws them away.
|
|
17
|
+
#
|
|
18
|
+
# FAIL-OPEN: not a repository, no git, no parser, broken input — pass. A broken guard must not get
|
|
19
|
+
# in the way of work.
|
|
20
|
+
|
|
21
|
+
RT_GUARD_NAME=git-guard-discard
|
|
22
|
+
|
|
23
|
+
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/utf8.sh" 2>/dev/null || true
|
|
24
|
+
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/hook-input.sh" 2>/dev/null || true
|
|
25
|
+
|
|
26
|
+
rt_hook_read
|
|
27
|
+
input="$RT_HOOK_INPUT"
|
|
28
|
+
[ -z "$input" ] && exit 0
|
|
29
|
+
|
|
30
|
+
tool="$(rt_hook_tool)"
|
|
31
|
+
case "$tool" in
|
|
32
|
+
Bash | mcp__webstorm__execute_terminal_command | mcp__webstorm__execute_tool) ;;
|
|
33
|
+
*) exit 0 ;;
|
|
34
|
+
esac
|
|
35
|
+
|
|
36
|
+
cmd="$(rt_hook_cmd)"
|
|
37
|
+
|
|
38
|
+
# The universal executor of the environment passes the real command as a nested string.
|
|
39
|
+
if [ "$tool" = "mcp__webstorm__execute_tool" ] && command -v perl >/dev/null 2>&1; then
|
|
40
|
+
inner="$(printf '%s' "$cmd" | perl -0ne '
|
|
41
|
+
if (/--command(?:=|\s+)(?:"((?:[^"\\]|\\.)*)"|\x27([^\x27]*)\x27|(.+))/s) {
|
|
42
|
+
print defined $1 ? $1 : (defined $2 ? $2 : $3);
|
|
43
|
+
}
|
|
44
|
+
' 2>/dev/null)"
|
|
45
|
+
[ -n "$inner" ] && cmd="$inner"
|
|
46
|
+
fi
|
|
47
|
+
|
|
48
|
+
# The bypass: the reason stands in the command itself, after the discard mark. An empty reason is
|
|
49
|
+
# no bypass — a mark without words names nothing.
|
|
50
|
+
if printf '%s' "$cmd" | grep -qE '#[[:space:]]*discard:[[:space:]]*[^[:space:]]'; then
|
|
51
|
+
exit 0
|
|
52
|
+
fi
|
|
53
|
+
|
|
54
|
+
command -v awk >/dev/null 2>&1 || exit 0
|
|
55
|
+
|
|
56
|
+
# The verb is looked for in the command position, the same way the main-branch guard does it: the
|
|
57
|
+
# options between `git` and the verb are skipped, and the verb's own arguments are read on to the
|
|
58
|
+
# end of the shell word list of that call. Printed is one line per discarding call: the verb and
|
|
59
|
+
# the sign that makes it discarding.
|
|
60
|
+
#
|
|
61
|
+
# reset — `--hard` among the arguments
|
|
62
|
+
# checkout — `--` among the arguments, or a lone `.` (a path, not a branch)
|
|
63
|
+
# restore — any argument without a leading dash, unless `--staged` alone is asked
|
|
64
|
+
# clean — `-f`, `-fd`, `-df`, `--force` among the arguments
|
|
65
|
+
hits="$(printf '%s\n' "$cmd" | awk '
|
|
66
|
+
function flush() {
|
|
67
|
+
if (verb == "reset" && hard) print "reset --hard"
|
|
68
|
+
if (verb == "checkout" && (dashdash || dot)) print "checkout --"
|
|
69
|
+
if (verb == "restore" && path && !staged) print "restore"
|
|
70
|
+
if (verb == "clean" && force) print "clean -f"
|
|
71
|
+
verb = ""; hard = 0; dashdash = 0; dot = 0; path = 0; staged = 0; force = 0
|
|
72
|
+
}
|
|
73
|
+
{
|
|
74
|
+
for (i = 1; i <= NF; i++) {
|
|
75
|
+
word = $i
|
|
76
|
+
if (word == "&&" || word == "||" || word == ";" || word == "|") { flush(); continue }
|
|
77
|
+
sub(/^.*\//, "", word)
|
|
78
|
+
if (word == "git") {
|
|
79
|
+
flush()
|
|
80
|
+
for (j = i + 1; j <= NF; j++) {
|
|
81
|
+
arg = $j
|
|
82
|
+
if (arg == "-c" || arg == "-C" || arg == "--git-dir" || arg == "--work-tree" \
|
|
83
|
+
|| arg == "--namespace" || arg == "--exec-path") { j++; continue }
|
|
84
|
+
if (substr(arg, 1, 1) == "-") continue
|
|
85
|
+
verb = arg
|
|
86
|
+
break
|
|
87
|
+
}
|
|
88
|
+
i = j
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
if (verb == "") continue
|
|
92
|
+
if (word == "--hard") hard = 1
|
|
93
|
+
if (word == "--") dashdash = 1
|
|
94
|
+
if (word == ".") dot = 1
|
|
95
|
+
if (word == "--staged" || word == "-S") staged = 1
|
|
96
|
+
if (word == "-f" || word == "-fd" || word == "-df" || word == "-fdx" || word == "-fx" \
|
|
97
|
+
|| word == "-xf" || word == "-dfx" || word == "--force") force = 1
|
|
98
|
+
if (substr(word, 1, 1) != "-") path = 1
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
END { flush() }
|
|
102
|
+
' 2>/dev/null)"
|
|
103
|
+
[ -z "$hits" ] && exit 0
|
|
104
|
+
|
|
105
|
+
# The tree is read in the directory the command runs in: a separate working copy has a tree of its
|
|
106
|
+
# own, and the project root would answer for the wrong one.
|
|
107
|
+
workdir="$(rt_hook_cwd)"
|
|
108
|
+
[ -z "$workdir" ] && workdir="${CLAUDE_PROJECT_DIR:-.}"
|
|
109
|
+
cd "$workdir" 2>/dev/null || exit 0
|
|
110
|
+
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || exit 0
|
|
111
|
+
|
|
112
|
+
# `clean` erases what is untracked; the other three erase what is tracked and changed. Each is
|
|
113
|
+
# judged by the part of the tree it can lose.
|
|
114
|
+
if printf '%s\n' "$hits" | grep -q '^clean'; then
|
|
115
|
+
changed="$(git status --porcelain --untracked-files=all 2>/dev/null)"
|
|
116
|
+
else
|
|
117
|
+
changed="$(git status --porcelain --untracked-files=no 2>/dev/null)"
|
|
118
|
+
fi
|
|
119
|
+
[ -z "$changed" ] && exit 0
|
|
120
|
+
|
|
121
|
+
count="$(printf '%s\n' "$changed" | grep -c .)"
|
|
122
|
+
files="$(printf '%s\n' "$changed" | sed 's/^...//' | head -20 | sed 's/^/ /')"
|
|
123
|
+
[ "$count" -gt 20 ] && files="${files}
|
|
124
|
+
… and $((count - 20)) more"
|
|
125
|
+
what="$(printf '%s\n' "$hits" | sort -u | paste -sd ',' - | sed 's/,/, /g')"
|
|
126
|
+
|
|
127
|
+
reason="BLOCKED by git-guard-discard: «git ${what}» throws away uncommitted edits, and the working tree holds ${count} of them:
|
|
128
|
+
${files}
|
|
129
|
+
|
|
130
|
+
Git keeps no object of an uncommitted edit: after this call there is nothing to restore it from. In one working copy two sessions work at once, so these files may be a neighbour's as well as your own. To drop a commit and keep the edits, take \`git reset --soft\`; to keep them aside, commit or stash them by name first. \`--hard\` is taken only when the goal is to throw the edits away, and then they are named before the call — the rule git-workflow."
|
|
131
|
+
|
|
132
|
+
# shellcheck disable=SC1090
|
|
133
|
+
[ -f "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/deny-tail.sh" ] \
|
|
134
|
+
&& . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/deny-tail.sh" 2>/dev/null
|
|
135
|
+
command -v rt_deny_tail >/dev/null 2>&1 || rt_deny_tail() { :; }
|
|
136
|
+
deny_tail_text="$(rt_deny_tail 'the comment `# discard: <reason>` in the same command, the erased files named in the reason')"
|
|
137
|
+
[ -n "$deny_tail_text" ] && reason="${reason}
|
|
138
|
+
|
|
139
|
+
${deny_tail_text}"
|
|
140
|
+
|
|
141
|
+
jq -n --arg r "$reason" '{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:$r}}' 2>/dev/null \
|
|
142
|
+
|| printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"A discarding git command on a dirty working tree is refused. Commit or name the edits first."}}\n'
|
|
143
|
+
|
|
144
|
+
exit 0
|