@hjxarthuratlas/claw-git-snapshot 0.0.0-stage → 1.0.3

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,171 @@
1
+ #!/bin/sh
2
+ # git-snapshot — 多仓库快照: 密钥扫描 → 一次提交 → 推送(失败重试一次) → 失败时通知。
3
+ # 适用于「仓库由 agent 持续改动、由定时任务统一留痕」的场景(如 OpenClaw 工作区与技能库)。
4
+ #
5
+ # 用法:
6
+ # git-snapshot 有改动就提交并推送(定时任务调用)
7
+ # git-snapshot --dry-run 只列出会提交什么, 不动暂存区
8
+ # git-snapshot --scan 只扫各仓库已暂存的内容(手工提交前跑; 命中不发声)
9
+ # git-snapshot --version
10
+ #
11
+ # 退出码: 0 成功或无改动; 1 有仓库失败(提交留在本地, 或被密钥扫描拦下); 2 环境错误。
12
+ # 密钥扫描只拦高信号模式, 防的是事故, 不是完整的防泄漏方案。
13
+ #
14
+ # 配置(环境差异一律经配置, 脚本不含特定环境的值):
15
+ # 查找顺序 GIT_SNAPSHOT_CONFIG 指向的文件 > $XDG_CONFIG_HOME/git-snapshot/config(无该变量则 ~/.config/…)
16
+ # 每行一个指令, # 开头为注释; 路径含空格用引号包住:
17
+ # repo <路径> 要快照的仓库, 可重复
18
+ # notify-command <命令> 失败时调用的通知命令(可执行文件路径); 缺省不通知
19
+ # notify-event <事件> 传给通知命令的 --event
20
+ # notify-title <标题> 传给通知命令的 --title
21
+ # push-timeout <秒> 推送超时, 默认 180
22
+ # retry-delay <秒> 推送重试前等待, 默认 10
23
+ # tz <IANA 时区> 提交信息里的日期所用时区, 默认 UTC
24
+ # secret-regex <正则> 追加的高信号密钥模式(ERE), 可重复
25
+ # 兼容环境变量: GIT_SNAPSHOT_REPOS(空格分隔的仓库列表)、GIT_SNAPSHOT_NOTIFY(通知命令), 优先于配置文件。
26
+ set -u
27
+
28
+ VERSION="1.0.3"
29
+ PROGRAM=$(basename "$0")
30
+
31
+ # 高信号密钥模式: Telegram token、sk- 密钥、GitHub token、带口令的 URL、JWT、Bark key。
32
+ SECRET_RE='(^|[^0-9])[0-9]{8,10}:[A-Za-z0-9_-]{35}|sk-[A-Za-z0-9]{20,}|ghp_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{60,}|[a-z][a-z0-9+.-]*://[^/[:space:]:@]+:[^/[:space:]:@]+@|[0-9a-f]{32}\.[A-Za-z0-9]{16}|api\.day\.app/[A-Za-z0-9_-]{15,}|eyJ[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{10,}'
33
+ PRIVATE_KEY_BEGIN='BEGIN [A-Z ]*PRIVATE KEY'
34
+ PRIVATE_KEY_END='END [A-Z ]*PRIVATE KEY'
35
+ # 命中里含这些词的当作占位符, 不算泄漏(经 awk -v 传入, 不能含反斜杠转义, 用字符类)。
36
+ PLACEHOLDER_RE='<|xxx|[*][*][*]|pwd|pass|your|redact|placeholder|example|[.][.][.]'
37
+
38
+ REPOS=""
39
+ NOTIFY=""
40
+ EVENT="backup.failed"
41
+ TITLE="Git 快照失败"
42
+ PUSH_TIMEOUT=180
43
+ RETRY_DELAY=10
44
+ TZ_NAME="UTC"
45
+
46
+ config_file() {
47
+ if [ -n "${GIT_SNAPSHOT_CONFIG:-}" ]; then printf '%s' "$GIT_SNAPSHOT_CONFIG"; return 0; fi
48
+ printf '%s/git-snapshot/config' "${XDG_CONFIG_HOME:-$HOME/.config}"
49
+ }
50
+
51
+ load_config() {
52
+ f=$(config_file)
53
+ [ -f "$f" ] || return 0
54
+ while IFS= read -r line || [ -n "$line" ]; do
55
+ line=$(printf '%s' "$line" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//')
56
+ case "$line" in ""|\#*) continue ;; esac
57
+ key=${line%%[[:space:]]*}
58
+ value=$(printf '%s' "$line" | sed -E 's/^[^[:space:]]+[[:space:]]+//; s/^"(.*)"$/\1/')
59
+ case "$key" in
60
+ repo) REPOS="$REPOS $value" ;;
61
+ notify-command) NOTIFY=$value ;;
62
+ notify-event) EVENT=$value ;;
63
+ notify-title) TITLE=$value ;;
64
+ push-timeout) PUSH_TIMEOUT=$value ;;
65
+ retry-delay) RETRY_DELAY=$value ;;
66
+ tz) TZ_NAME=$value ;;
67
+ secret-regex) SECRET_RE="$SECRET_RE|$value" ;;
68
+ *) echo "未知配置项: $key ($f)" >&2; exit 2 ;;
69
+ esac
70
+ done < "$f"
71
+ }
72
+ load_config
73
+ # 兼容环境变量优先于配置文件。
74
+ REPOS="${GIT_SNAPSHOT_REPOS:-$REPOS}"
75
+ NOTIFY="${GIT_SNAPSHOT_NOTIFY:-$NOTIFY}"
76
+
77
+ MODE=snapshot
78
+ case "${1:-}" in
79
+ "") MODE=snapshot ;;
80
+ --dry-run) MODE=dry ;;
81
+ --scan) MODE=scan ;;
82
+ --version) echo "git-snapshot $VERSION"; exit 0 ;;
83
+ -h|--help) sed -n '2,/^set -u/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//'; exit 0 ;;
84
+ *) echo "用法: $PROGRAM [--dry-run | --scan]" >&2; exit 2 ;;
85
+ esac
86
+ command -v git >/dev/null 2>&1 || { echo "缺少 git" >&2; exit 2; }
87
+ if [ "$MODE" = snapshot ]; then
88
+ command -v timeout >/dev/null 2>&1 || { echo "缺少 timeout" >&2; exit 2; }
89
+ fi
90
+ [ -n "$REPOS" ] || { echo "未配置仓库(repo 指令或 GIT_SNAPSHOT_REPOS), 配置文件: $(config_file)" >&2; exit 2; }
91
+ ERR=$(mktemp) || exit 2
92
+ trap 'rm -f "$ERR"' EXIT
93
+
94
+ # 输出命中的「文件:行」, 只出位置, 不出匹配内容; 有命中返回 1。
95
+ # 查的是整个暂存区(含已提交的文件), 不只是本次改动的文件。
96
+ scan() {
97
+ r=$1
98
+ tmp=$(mktemp) || return 2
99
+ git -C "$r" grep --cached -I -l -E "$SECRET_RE" 2>/dev/null \
100
+ | while IFS= read -r f; do
101
+ # -n 出行号, -h 不出路径: 记录形如「行号:匹配内容」, 只在第一个冒号处切分。
102
+ git -C "$r" grep --cached -I -n -o -h -E "$SECRET_RE" -- "$f" 2>/dev/null \
103
+ | awk '{ c=index($0, ":"); print substr($0, 1, c-1) "\t" substr($0, c+1) }' \
104
+ | awk -F'\t' -v p="$PLACEHOLDER_RE" 'tolower($2) ~ p { next } { print $1 }' \
105
+ | sort -u | sed "s#^#$f:#"
106
+ done > "$tmp"
107
+ # 私钥块: 头尾标记同时出现才算, 只提到头部的不算。
108
+ git -C "$r" grep --cached -I -l -E "$PRIVATE_KEY_BEGIN" 2>/dev/null \
109
+ | while IFS= read -r f; do
110
+ if git -C "$r" grep --cached -I -q -E "$PRIVATE_KEY_END" -- "$f" 2>/dev/null; then
111
+ echo "$f:private-key-block"
112
+ fi
113
+ done >> "$tmp"
114
+ sort -u -o "$tmp" "$tmp"
115
+ if [ -s "$tmp" ]; then cat "$tmp"; rm -f "$tmp"; return 1; fi
116
+ rm -f "$tmp"
117
+ return 0
118
+ }
119
+
120
+ # 推送当前分支; 失败时等 RETRY_DELAY 秒重试一次。
121
+ push_with_retry() {
122
+ r=$1
123
+ if timeout "$PUSH_TIMEOUT" git -C "$r" push -q origin HEAD 2>"$ERR"; then return 0; fi
124
+ sleep "$RETRY_DELAY"
125
+ timeout "$PUSH_TIMEOUT" git -C "$r" push -q origin HEAD 2>"$ERR"
126
+ }
127
+
128
+ rc=0
129
+ failures=""
130
+ for r in $REPOS; do
131
+ name=$(basename "$r")
132
+ if [ ! -d "$r/.git" ]; then
133
+ echo "FAIL $name: 不是 git 仓库"; failures="$failures|$name: 不是 git 仓库"; rc=1; continue
134
+ fi
135
+ if [ "$MODE" = snapshot ]; then
136
+ git -C "$r" add -A || { echo "FAIL $name: git add 失败"; failures="$failures|$name: git add 失败"; rc=1; continue; }
137
+ fi
138
+ staged=$(git -C "$r" diff --cached --name-only)
139
+ if [ "$MODE" = scan ] && [ -z "$staged" ]; then echo "OK $name: 暂存区为空"; continue; fi
140
+ if [ -z "$(git -C "$r" status --porcelain)" ]; then echo "OK $name: 无改动"; continue; fi
141
+ if [ "$MODE" = dry ]; then echo "WOULD $name:"; git -C "$r" status --short | head -20; continue; fi
142
+ if hits=$(scan "$r"); then
143
+ :
144
+ else
145
+ echo "BLOCK $name: 密钥扫描命中, 未提交(文件:行):"
146
+ printf '%s\n' "$hits" | sed 's/^/ /'
147
+ [ "$MODE" = snapshot ] && git -C "$r" reset -q
148
+ failures="$failures|$name: 密钥扫描命中 $(printf '%s\n' "$hits" | wc -l | tr -d ' ') 处"
149
+ rc=1
150
+ continue
151
+ fi
152
+ if [ "$MODE" = scan ]; then echo "OK $name: 暂存内容无命中"; continue; fi
153
+ if ! git -C "$r" commit -q -m "快照 $(TZ="$TZ_NAME" date +%F): $name"; then
154
+ echo "FAIL $name: git commit 失败"; failures="$failures|$name: 提交失败"; rc=1; continue
155
+ fi
156
+ sha=$(git -C "$r" rev-parse --short HEAD)
157
+ if push_with_retry "$r"; then
158
+ echo "OK $name: 已提交并推送 $sha"
159
+ else
160
+ echo "FAIL $name: 提交 $sha 已留本地, 推送失败: $(tail -1 "$ERR" 2>/dev/null | cut -c1-120)"
161
+ failures="$failures|$name: 推送失败, $sha 留本地"
162
+ rc=1
163
+ fi
164
+ done
165
+
166
+ # 快照失败才通知; 手工 --scan 命中不发声。
167
+ if [ "$MODE" = snapshot ] && [ -n "$failures" ] && [ -n "$NOTIFY" ] && [ -x "$NOTIFY" ]; then
168
+ body=$(printf '%s' "$failures" | tr '|' '\n' | sed '/^$/d' | tr '\n' ';' | cut -c1-300)
169
+ "$NOTIFY" --event "$EVENT" --title "$TITLE" --body "$body" >/dev/null 2>&1 || echo "WARN 通知命令发送失败"
170
+ fi
171
+ exit $rc
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@hjxarthuratlas/claw-git-snapshot",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.3",
4
+ "description": "多仓库快照:密钥扫描 + 一次提交 + 推送重试 + 失败通知。单个 POSIX shell 脚本。",
5
+ "license": "MIT",
6
+ "repository": "git@github.com:HJXArthurAtlas/claw-git-snapshot.git",
7
+ "bin": {
8
+ "git-snapshot": "bin/git-snapshot"
9
+ },
10
+ "files": [
11
+ "bin"
12
+ ],
13
+ "engines": {
14
+ "node": ">=18"
15
+ },
16
+ "keywords": [
17
+ "git",
18
+ "snapshot",
19
+ "backup",
20
+ "secret-scan",
21
+ "agent"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ }
26
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.