holy-agents-kits 0.2.0 → 0.3.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.
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env python3
2
+ """逐路径完整性核对:目标分支是否把源分支该迁的内容都迁了。
3
+
4
+ 这是选择性搬运里唯一能发现「静默缺口」的机械检查 —— 编译与测试都发现不了
5
+ 「少迁了一个文件」。典型缺口:报告文档迁了、配套的执行脚本没迁。
6
+
7
+ 比对逻辑:
8
+ - 源分支有、目标分支也有 → 内容是否逐字节相同
9
+ 相同 → 已完整迁移
10
+ 不同 → 差异(可能是有意取舍,需人工判定)
11
+ - 源分支有、目标分支没有 → 缺失(该迁却漏迁)
12
+ - 源分支没有、目标分支有 → 目标侧独有(正常,不报)
13
+
14
+ 用法:
15
+ python parity_check.py --target main --source feature/old
16
+ python parity_check.py --target main --source feature/old --prefix src api
17
+ python parity_check.py --target main --source feature/old --prefix docs --show-diff
18
+ python parity_check.py --target main --source feature/old --exclude config/local.json
19
+
20
+ stdout 是核对结果(结果);stderr 是过程日志。
21
+ 退出码:0 = 缺失清单为空;1 = 有缺失;2 = 用法错误。
22
+ """
23
+ import argparse
24
+ import fnmatch
25
+ import hashlib
26
+ import os
27
+ import subprocess
28
+ import sys
29
+
30
+
31
+ def run(args, cwd):
32
+ return subprocess.run(args, capture_output=True, cwd=cwd, text=True, errors="replace")
33
+
34
+
35
+ def run_bytes(args, cwd):
36
+ return subprocess.run(args, capture_output=True, cwd=cwd)
37
+
38
+
39
+ def out(args, cwd):
40
+ r = run(args, cwd)
41
+ if r.returncode != 0:
42
+ raise SystemExit(f"命令失败: {' '.join(args)}\n{r.stderr.strip()}")
43
+ return r.stdout
44
+
45
+
46
+ def list_paths(rev, prefix, cwd):
47
+ """某 revision 下、给定前缀下的文件路径集合。"""
48
+ args = ["git", "ls-tree", "-r", "--name-only", rev]
49
+ if prefix:
50
+ args += ["--"] + list(prefix)
51
+ return {p for p in out(args, cwd).splitlines() if p.strip()}
52
+
53
+
54
+ def blob(rev, path, cwd):
55
+ """某 revision 下某路径的原始字节;路径不存在返回 None。"""
56
+ r = run_bytes(["git", "show", f"{rev}:{path}"], cwd)
57
+ if r.returncode != 0:
58
+ return None
59
+ return r.stdout
60
+
61
+
62
+ def digest(data):
63
+ return hashlib.sha256(data).hexdigest()[:12]
64
+
65
+
66
+ def matches(path, patterns):
67
+ if not patterns:
68
+ return False
69
+ for pat in patterns:
70
+ if pat.endswith("/"):
71
+ if path.startswith(pat):
72
+ return True
73
+ elif path == pat or fnmatch.fnmatch(path, pat):
74
+ return True
75
+ return False
76
+
77
+
78
+ def main():
79
+ ap = argparse.ArgumentParser(description="逐路径完整性核对:目标分支 vs 源分支")
80
+ ap.add_argument("--target", required=True, help="目标分支(迁入的一边)")
81
+ ap.add_argument("--source", required=True, help="源分支(迁过去的一边)")
82
+ ap.add_argument("--repo", default=None,
83
+ help="仓库根目录(默认取当前目录所属 git 仓库的根)")
84
+ ap.add_argument("--prefix", nargs="*", default=None,
85
+ help="只核对这些路径前缀(不给则核对全部)")
86
+ ap.add_argument("--exclude", nargs="*", default=[],
87
+ help="跳过这些路径(已知刻意不迁的)")
88
+ ap.add_argument("--show-diff", action="store_true",
89
+ help="对内容不同的路径,打印差异统计")
90
+ args = ap.parse_args()
91
+
92
+ repo = os.path.abspath(args.repo) if args.repo else os.getcwd()
93
+ if run(["git", "rev-parse", "--git-dir"], repo).returncode != 0:
94
+ raise SystemExit(f"不是 git 仓库: {repo}")
95
+ repo = out(["git", "rev-parse", "--show-toplevel"], repo).strip()
96
+
97
+ for rev in (args.target, args.source):
98
+ if run(["git", "rev-parse", "--verify", rev], repo).returncode != 0:
99
+ raise SystemExit(f"分支不存在: {rev}")
100
+
101
+ src_paths = list_paths(args.source, args.prefix, repo)
102
+ tgt_paths = list_paths(args.target, args.prefix, repo)
103
+ print(f"源分支 {args.source}: {len(src_paths)} 个路径", file=sys.stderr)
104
+ print(f"目标分支 {args.target}: {len(tgt_paths)} 个路径", file=sys.stderr)
105
+
106
+ excluded, missing, identical, differing = [], [], [], []
107
+
108
+ for p in sorted(src_paths):
109
+ if matches(p, args.exclude):
110
+ excluded.append(p)
111
+ continue
112
+ if p not in tgt_paths:
113
+ missing.append(p)
114
+ continue
115
+ a = blob(args.source, p, repo)
116
+ b = blob(args.target, p, repo)
117
+ if a is None:
118
+ continue # 目标分支上是目录或已删除
119
+ if a == b:
120
+ identical.append(p)
121
+ else:
122
+ differing.append(p)
123
+
124
+ print()
125
+ print(f"逐字节相同: {len(identical)}")
126
+ print(f"内容不同 : {len(differing)} (可能是有意取舍,需人工判定)")
127
+ print(f"目标分支缺失: {len(missing)} (该迁却漏迁)")
128
+ if excluded:
129
+ print(f"按排除规则跳过: {len(excluded)}")
130
+
131
+ if differing:
132
+ print("\n--- 内容不同的路径 ---")
133
+ for p in differing:
134
+ a = blob(args.source, p, repo)
135
+ b = blob(args.target, p, repo)
136
+ print(f" {p} (source {digest(a)} / target {digest(b)}, "
137
+ f"{len(a.splitlines())} vs {len(b.splitlines())} 行)")
138
+ if args.show_diff:
139
+ d = run(["git", "diff", "--stat", args.target, args.source, "--", p], repo)
140
+ if d.returncode == 0 and d.stdout.strip():
141
+ print(" " + d.stdout.strip().replace("\n", "\n "))
142
+
143
+ if missing:
144
+ print("\n--- 目标分支缺失(该迁却漏迁)---")
145
+ for p in missing:
146
+ print(f" {p}")
147
+ print("\n补迁命令示例:")
148
+ print(f" git checkout {args.source} -- " + " ".join(missing[:3]) +
149
+ (" ..." if len(missing) > 3 else ""))
150
+
151
+ if excluded:
152
+ print("\n--- 按排除规则跳过(刻意不迁)---")
153
+ for p in excluded:
154
+ print(f" {p}")
155
+
156
+ return 1 if missing else 0
157
+
158
+
159
+ if __name__ == "__main__":
160
+ sys.exit(main())