Skill Tracer
Skills › gstack › gstack-upgrade › v1.38.1.0.sh

v1.38.1.0.sh

104 lines · gstack-upgrade/migrations/v1.38.1.0.sh at 3f79d81

1#!/usr/bin/env bash
2# Migration: v1.38.1.0 — add root-level design + test-plan patterns to
3# .brain-allowlist, .brain-privacy-map.json, and .gitattributes (#1452).
4#
5# Why a migration: gstack-artifacts-init regenerates these files but also
6# does `git commit + push` on ~/.gstack/, which would clobber user state on
7# upgrade. Instead, we do targeted per-file in-place repairs.
8#
9# Per-file independent — if one file is missing we still repair the others.
11# Idempotent: each insertion is gated on `not already present` so re-running
12# the migration is a no-op.
14# No `set -e` — we intentionally tolerate per-file failures so other repairs
15# still run. `set -u` is fine.
16set -u
18_gstack_migration_dir="${BASH_SOURCE[0]//\\//}"; _gstack_migration_dir="${_gstack_migration_dir%/*}"
19. "${_gstack_migration_dir}/../../bin/gstack-state-root.sh" 2>/dev/null || { echo "$0: cannot resolve the gstack state root: ${_gstack_migration_dir}/../../bin/gstack-state-root.sh is missing. fix: reinstall with ./setup or /gstack-upgrade (docs/state-root.md)" >&2; exit 1; }
20gstack_state_root_select; GSTACK_HOME="$_gstack_sr_root"
21ALLOWLIST="${GSTACK_HOME}/.brain-allowlist"
22PRIVACY="${GSTACK_HOME}/.brain-privacy-map.json"
23GITATTRS="${GSTACK_HOME}/.gitattributes"
25MIGRATION_DIR="${GSTACK_HOME}/.migrations"
26DONE="${MIGRATION_DIR}/v1.38.1.0.done"
28mkdir -p "${MIGRATION_DIR}" 2>/dev/null || true
29if [ -f "${DONE}" ]; then
30 exit 0
31fi
33NEW_PATTERNS=(
34 'projects/*/*-design-*.md'
35 'projects/*/*-test-plan-*.md'
38added_any=0
40# ----- .brain-allowlist ---------------------------------------------------
41if [ -f "${ALLOWLIST}" ]; then
42 for PATTERN in "${NEW_PATTERNS[@]}"; do
43 if ! grep -Fq -- "${PATTERN}" "${ALLOWLIST}" 2>/dev/null; then
44 # Insert before USER ADDITIONS marker. BSD sed (-i.bak) compat for macOS;
45 # the backup file is removed afterward.
46 if grep -q '^# ---- USER ADDITIONS BELOW' "${ALLOWLIST}" 2>/dev/null; then
47 sed -i.bak "/^# ---- USER ADDITIONS BELOW/i\\
48${PATTERN}
49" "${ALLOWLIST}" && rm -f "${ALLOWLIST}.bak"
50 added_any=1
51 else
52 # Marker missing — append at end of file as a fallback. User may have
53 # custom-edited the file; better to add than skip silently.
54 printf '%s\n' "${PATTERN}" >> "${ALLOWLIST}"
55 added_any=1
56 fi
57 fi
58 done
59fi
61# ----- .brain-privacy-map.json -------------------------------------------
62# Uses jq to preserve JSON validity. Skips with a warning if jq is missing.
63if [ -f "${PRIVACY}" ]; then
64 if command -v jq >/dev/null 2>&1; then
65 for PATTERN in "${NEW_PATTERNS[@]}"; do
66 if ! jq -e --arg p "${PATTERN}" 'map(select(.pattern == $p)) | length > 0' "${PRIVACY}" >/dev/null 2>&1; then
67 if jq --arg p "${PATTERN}" '. += [{"pattern": $p, "class": "artifact"}]' "${PRIVACY}" > "${PRIVACY}.tmp" 2>/dev/null; then
68 mv "${PRIVACY}.tmp" "${PRIVACY}"
69 added_any=1
70 else
71 rm -f "${PRIVACY}.tmp"
72 echo " [v1.38.1.0] WARN: jq failed to patch ${PRIVACY}; skipping pattern ${PATTERN}." >&2
73 fi
74 fi
75 done
76 else
77 echo " [v1.38.1.0] WARN: jq not found; skipping privacy-map repair. Install jq and re-run gstack-upgrade, or run gstack-artifacts-init manually." >&2
78 fi
79fi
81# ----- .gitattributes -----------------------------------------------------
82if [ -f "${GITATTRS}" ]; then
83 for PATTERN in "${NEW_PATTERNS[@]}"; do
84 RULE="${PATTERN} merge=union"
85 if ! grep -Fq -- "${RULE}" "${GITATTRS}" 2>/dev/null; then
86 printf '%s\n' "${RULE}" >> "${GITATTRS}"
87 added_any=1
88 fi
89 done
90fi
92# Mark done. Even if no patches were applied (already-current install), we
93# write the touchfile so the migration runs once.
94touch "${DONE}"
96if [ "${added_any}" = "1" ]; then
97 echo " [v1.38.1.0] allowlist/privacy-map/gitattributes patched for root-level design + test-plan artifacts (idempotent)" >&2
98fi
100# NEVER `git commit + push` from this migration. The user controls when the
101# patches ship into their federated artifacts repo (next gstack-brain-sync
102# --once or a manual commit).
104exit 0