@sassywares/skills 0.17.1 → 0.17.2

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.
@@ -0,0 +1,200 @@
1
+ # generated_by: sassy-setup@0.17.2
2
+ # lib/branch.sh — the --branch mode: resolve commit.branch into the one
3
+ # decision the entry skills make before they start.
4
+ # Sourced by detect.sh. Never sets shell options; reads the paths it set.
5
+
6
+ # --- Branch decision -------------------------------------------------------
7
+ # Read-only. Resolves commit.branch into the single decision the entry skills
8
+ # (sassy-work, sassy-debug) make before they start: cut a branch,
9
+ # reuse the one checked out, or commit where you stand.
10
+ #
11
+ # Every base_map VALUE counts as a base branch, not just the remote default.
12
+ # tap2's day-to-day base is `staging`, so comparing only against origin/HEAD
13
+ # declared staging "not a base" and let phase work commit onto it.
14
+ mode_branch() {
15
+ policy="${POLICY_ARG:-infer}"
16
+ case "$policy" in
17
+ infer|current|new) ;;
18
+ *) echo "unknown --policy: $policy (expected infer, current, or new)" >&2; exit 2 ;;
19
+ esac
20
+ configured="$(yflat "commit${US}branch" 2>/dev/null || true)"
21
+ note=""
22
+ if [[ -n "$configured" && "$configured" != "current" && "$configured" != "new" ]]; then
23
+ # A soft config error must not become a silently wrong branch decision.
24
+ note="commit.branch '$configured' is not current|new; using new"
25
+ configured=""
26
+ fi
27
+ if [[ "$policy" == "infer" ]]; then
28
+ policy="${configured:-new}"
29
+ fi
30
+
31
+ # base_heads_in DIR — every branch DIR's config treats as a base: each
32
+ # base_map value, the repos[<dir>] rung, and the remote default. A workspace
33
+ # has no base_map of its own, so repos[] is what names its bases; reading
34
+ # base_map alone reported every nested repo as on-branch and let runs commit
35
+ # onto staging, which is the bug this whole mode exists to stop.
36
+ base_heads_in() {
37
+ local dir="$1" r v out=""
38
+ if [[ -f "$dir/.sassywares/config.yaml" ]]; then
39
+ out="$(yaml_flat "$dir/.sassywares/config.yaml" 2>/dev/null | awk -F'\t' -v us="$US" \
40
+ '{n=split($1,a,us); if (n==3 && a[1]=="git" && a[2]=="base_map") print $2}' | sort -u || true)"
41
+ fi
42
+ v="$(yflat "repos${US}$(basename "$dir")" 2>/dev/null || true)"
43
+ [[ -n "$v" ]] && out="$out"$'\n'"$v"
44
+ r="$(remote_name "$dir")"
45
+ if [[ -n "$r" ]]; then
46
+ v="$(git -C "$dir" symbolic-ref --short "refs/remotes/$r/HEAD" 2>/dev/null | sed "s|^$r/||" || true)"
47
+ [[ -n "$v" ]] && out="$out"$'\n'"$v"
48
+ fi
49
+ out="$(printf '%s\n' "$out" | grep -v '^$' | sort -u || true)"
50
+ # Probing through gh costs a network call, so only when nothing else said.
51
+ [[ -n "$out" ]] || out="$(detect_base "$dir" 2>/dev/null || true)"
52
+ printf '%s\n' "$out" | grep -v '^$' || true
53
+ }
54
+
55
+ # in_base_heads DIR BRANCH — membership without a pipe: `… | grep -q` trips
56
+ # pipefail, because grep exits on the first match and SIGPIPEs the producer,
57
+ # so a branch that IS a base reported as if it were not.
58
+ in_base_heads() {
59
+ local dir="$1" want="$2" b
60
+ while IFS= read -r b; do
61
+ [[ "$b" == "$want" ]] && return 0
62
+ done < <(base_heads_in "$dir")
63
+ return 1
64
+ }
65
+
66
+ # repo_base DIR — the base a PR from DIR targets, for a brand-new branch name.
67
+ repo_base() {
68
+ local dir="$1" v="" r
69
+ if [[ -f "$dir/.sassywares/config.yaml" ]]; then
70
+ v="$(yflat_in "$dir/.sassywares/config.yaml" "git${US}base_map${US}*" 2>/dev/null || true)"
71
+ fi
72
+ [[ -n "$v" ]] || v="$(yflat "repos${US}$(basename "$dir")" 2>/dev/null || true)"
73
+ if [[ -z "$v" ]]; then
74
+ v="$(detect_base "$dir" 2>/dev/null || true)"
75
+ [[ -n "$v" ]] && printf '%s' "$v" && return 0
76
+ fi
77
+ printf '%s' "$v"
78
+ }
79
+
80
+ # classify DIR [HEAD] — on-base, on-branch, detached, or unknown.
81
+ classify() {
82
+ local dir="$1" h="$2"
83
+ if [[ -z "$h" ]]; then printf 'detached'; return 0; fi
84
+ if in_base_heads "$dir" "$h"; then printf 'on-base'; else printf 'on-branch'; fi
85
+ }
86
+
87
+ # start_point DIR HEAD — the upstream ref a new branch is cut from. Prints
88
+ # "none" when there is no upstream ref: cutting from local HEAD would carry
89
+ # commits stranded on the base, which is the failure this prevents.
90
+ start_point() {
91
+ local dir="$1" h="$2" r
92
+ r="$(remote_name "$dir")"
93
+ if [[ -n "$r" && -n "$h" ]] && git -C "$dir" rev-parse --verify --quiet "refs/remotes/$r/$h" >/dev/null 2>&1; then
94
+ printf '%s/%s' "$r" "$h"
95
+ else
96
+ printf 'none'
97
+ fi
98
+ }
99
+
100
+ # unpublished DIR HEAD — commits on the local base that are not upstream.
101
+ # Unknown on error, never 0: an unreadable count must not read as "clean".
102
+ unpublished() {
103
+ local dir="$1" h="$2" r n
104
+ r="$(remote_name "$dir")"
105
+ [[ -n "$r" && -n "$h" ]] || { printf 'unknown'; return 0; }
106
+ git -C "$dir" rev-parse --verify --quiet "refs/remotes/$r/$h" >/dev/null 2>&1 || { printf 'unknown'; return 0; }
107
+ n="$(git -C "$dir" rev-list --count "$r/$h..$h" 2>/dev/null || true)"
108
+ [[ -n "$n" ]] && printf '%s' "$n" || printf 'unknown'
109
+ }
110
+
111
+ # decide STATE POLICY START UNPUBLISHED — the action, fail closed.
112
+ decide() {
113
+ local st="$1" pol="$2" start="$3" unp="$4"
114
+ case "$st:$pol" in
115
+ on-branch:*) printf 'reuse'; return 0 ;;
116
+ on-base:new) ;;
117
+ on-base:current) printf 'commit-here'; return 0 ;;
118
+ detached:new) ;;
119
+ *) printf 'refuse'; return 0 ;;
120
+ esac
121
+ # Cutting needs a known-clean upstream base.
122
+ [[ "$start" == "none" ]] && { printf 'refuse'; return 0; }
123
+ [[ "$unp" == "0" ]] || { printf 'stop-and-ask'; return 0; }
124
+ printf 'create'
125
+ }
126
+
127
+ repo_value="$(yflat "git${US}repo" 2>/dev/null || true)"
128
+
129
+ [[ -n "$note" ]] && printf 'note: %s\n' "$note"
130
+ printf 'topology: %s\n' "$TOPOLOGY"
131
+ printf 'config: %s\n' "${CONFIG:-none}"
132
+ printf 'configured: %s\n' "${configured:-none}"
133
+ printf 'policy: %s\n' "$policy"
134
+
135
+ if [[ "$TOPOLOGY" == "none" || "$repo_value" == "none" ]]; then
136
+ if [[ "$repo_value" == "none" ]]; then
137
+ printf 'state: no-git\naction: refuse\nreason: git.repo is none\n'
138
+ else
139
+ printf 'state: no-git\naction: refuse\nreason: not a git work tree\n'
140
+ fi
141
+ exit 0
142
+ fi
143
+
144
+ if [[ "$TOPOLOGY" == "workspace" ]]; then
145
+ # One policy for the whole run, resolved per repo: a run cannot follow two
146
+ # branch policies, so a nested repo that disagrees is reported as drift.
147
+ while IFS= read -r dir; do
148
+ [[ -n "$dir" ]] || continue
149
+ d="$ROOT/$dir"
150
+ h="$(git -C "$d" branch --show-current 2>/dev/null || true)"
151
+ st="$(classify "$d" "$h")"
152
+ sp="$(start_point "$d" "$h")"
153
+ base="$(repo_base "$d")"
154
+ if [[ "$st" == "on-base" ]]; then unp="$(unpublished "$d" "$h")"; else unp=n/a; fi
155
+ printf 'repo %s: head=%s state=%s base=%s start-point=%s unpublished=%s action=%s\n' \
156
+ "$dir" "${h:-none}" "$st" "${base:-none}" "$sp" "$unp" "$(decide "$st" "$policy" "$sp" "$unp")"
157
+ own=""
158
+ if [[ -f "$d/.sassywares/config.yaml" ]]; then
159
+ own="$(yflat_in "$d/.sassywares/config.yaml" "commit${US}branch" 2>/dev/null || true)"
160
+ fi
161
+ [[ -n "$own" && "$own" != "$policy" ]] && printf 'drift %s: commit.branch=%s\n' "$dir" "$own"
162
+ done < <(nested_repos)
163
+ exit 0
164
+ fi
165
+
166
+ head="${HEAD_ARG:-$(git -C "$ROOT" branch --show-current 2>/dev/null || true)}"
167
+ actual="$(git -C "$ROOT" branch --show-current 2>/dev/null || true)"
168
+ [[ -n "$HEAD_ARG" && "$HEAD_ARG" != "$actual" ]] && printf 'checked-out: %s\n' "${actual:-none}"
169
+ printf 'head: %s\n' "${head:-none}"
170
+
171
+ state="$(classify "$ROOT" "$head")"
172
+ printf 'state: %s\n' "$state"
173
+ start="$(start_point "$ROOT" "$head")"
174
+ unp=n/a
175
+ [[ "$state" == "on-base" ]] && unp="$(unpublished "$ROOT" "$head")"
176
+ printf 'action: %s\n' "$(decide "$state" "$policy" "$start" "$unp")"
177
+ printf 'start-point: %s\n' "$start"
178
+ printf 'unpublished: %s\n' "$unp"
179
+
180
+ # pr-base is provisional for a branch that does not exist yet: the name the
181
+ # skill picks can match a base_map family (release/*, hotfix/*) and retarget
182
+ # the PR. Pass --name once the name is known to get the final answer.
183
+ pr_src=config
184
+ if [[ -n "$NAME_ARG" ]]; then
185
+ pr_base="$(base_for_head "$NAME_ARG" 2>/dev/null || true)"
186
+ elif [[ "$state" == "on-base" || "$state" == "detached" ]]; then
187
+ pr_base="$(repo_base "$ROOT")"
188
+ else
189
+ pr_base="$(base_for_head "$head" 2>/dev/null || true)"
190
+ fi
191
+ if [[ -z "$pr_base" ]]; then
192
+ pr_base="$(detect_base "$ROOT" 2>/dev/null || true)"
193
+ pr_src=probed
194
+ fi
195
+ [[ -n "$pr_base" ]] || pr_src=unknown
196
+ printf 'pr-base: %s\n' "$pr_base"
197
+ printf 'pr-base-source: %s\n' "$pr_src"
198
+ printf 'pr-base-final: %s\n' "$( [[ -n "$NAME_ARG" ]] && printf yes || printf no )"
199
+ exit 0
200
+ }