@tyhld/conductor 0.12.167 → 0.12.168

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tyhld/conductor",
3
- "version": "0.12.167",
3
+ "version": "0.12.168",
4
4
  "description": "采配くん管制の見守りアプリ(conductor-agent)。各PCで常駐し、中央(devlog-tracker)へ定期的に生存報告(heartbeat)を送る常駐CLI。",
5
5
  "type": "module",
6
6
  "bin": {
@@ -493,10 +493,33 @@ DENY** です。読み込み先は公式の説明と道具の実体で確かめ
493
493
  ★前の番人の試験 43本が番人へ渡す命令 5000件ほどを直す前と後で測り比べ、判定が変わった物は0件でした(変わったのは新しい試験の、秘密に届く形だけ)。
494
494
  固定する試験: `test_stage_config_foreach.py`(CC1 の実測の5つの形・ふだんの命令・空振り確認つき)。
495
495
 
496
+ ### ★名指しが字面に無い形(一覧のファイル・標準入力)と git commit の名指しを塞ぐ(便 16b2f958・えふさん決定 2026-11-02)
497
+
498
+ 便 9faa1627 の「分かっている限界」に書いた形で、CC1 が 0.12.166 で実測した穴です(どれも前から `allow`)。
499
+ 囲い(サンドボックス)が秘密の読み取りを OS の層で塞いでいない今、番人がいちばん手前の守りなので塞ぎました。
500
+ **止める側を足すだけ**で、秘密に届かない形は1つも変えていません。
501
+
502
+ | 形 | 前 | 後 |
503
+ | --- | --- | --- |
504
+ | `--pathspec-from-file=<一覧>`(`git add`・`git stage`・`git rm --cached`・`git commit` など・`-` なら `<` の行き先)の一覧に秘密の行がある | 通っていた | **`danger`**(`… listed for git add` など) |
505
+ | `git hash-object --stdin-paths`・`git update-index --stdin`・`git checkout-index --stdin` の `< <一覧>` に秘密の行がある | 通っていた | **`danger`** |
506
+ | 一覧が読めない(無い・ふつうのファイルでない・256KiB より大きい・変数・字が壊れている・行で読むのに NUL がある・標準入力がパイプやヒアドキュメント)・一覧のファイル自体が秘密 | 通っていた | **`danger`**(`path list for git … cannot be verified for secrets (…)`=止める側に倒す) |
507
+ | `git commit` の名指し(`--only`・`-o`・`-i`・`--include`・`--` の後ろも)が秘密 | 通っていた(追跡済みなら記録に入る) | **`danger`**(`… sent by git commit`) |
508
+ | 秘密の無い一覧・秘密でない名指し・名指しなしの `git commit -m x`・説明文の中の秘密の字 | 通る | **通る(変えない)** |
509
+
510
+ - 一覧は1行ずつ(`--pathspec-file-nul`・`-z` なら NUL 区切り)、`"…"` の C の書き方は解いてから今の秘密の判定(`secret_reason`)にかけます。
511
+ pathspec の頭(`:(top)`・`:/`)は外して測り、除く指定(`:!`・`:^`・`:(exclude)`)の行は測りません。
512
+ - ★番人は秘密のファイルを読みません(一覧のファイル自体が秘密なら、中身を読まずに止める)。
513
+ - `git commit` の値を取る旗(`-m`・`-F`・`-C`・`-c`・`-t`・`--author` など)の値は名指しではないので、説明文に秘密の名前を書いても止めません。
514
+
515
+ ★**関門・作法(discipline)・本番 DB・git grep の旗の断りの判定には混ぜていません**(危険の判定だけ)。
516
+ ★前の番人の試験 44本が番人へ渡す命令を直す前と後で測り比べ、判定が変わった物は0件でした(変わったのは新しい試験の、秘密に届く形だけ)。
517
+ 固定する試験: `test_path_list_commit.py`(CC1 の実測の3つの形・読めない一覧・ふだんの命令・空振り確認つき)。
518
+
496
519
  ### 分かっている限界
497
- - **名指しの中身が字面に無い形は測れません**(変えていない):`git add --pathspec-from-file=<一覧>`・
498
- `git hash-object -w --stdin-paths` の一覧の中身、追跡済みの秘密を `git commit <秘密>`/`git commit -a` で入れる形
499
- (`-a` は作法で止まる)。登録済みの別名を後から `git x` で呼ぶ形は、登録の時に測る(登録済みの物は測れない)。
520
+ - **名指しの中身が字面に無い形は、一覧のファイル・`<` の行き先までは測ります**(便 16b2f958)。
521
+ 台本のファイルの中で `git add` する形・パイプで流し込む中身(止める側に倒す)・登録済みの別名を後から `git x` で呼ぶ形
522
+ (登録の時に測る)は測れません。`git commit -a` は作法で止まります。
500
523
  - **`curl … | bash` のように、殻へ流れ込む中身が字面に無い形は測れません**(変えていない)。
501
524
  測るのは `echo`/`printf` の字が殻へ流れ込む形だけです。
502
525
  - `ssh` の命令の中のパスは、送り先ではなく**この機の作業場所**を基準に測ります(止める側に倒れる読み方)。
@@ -29,11 +29,13 @@
29
29
 
30
30
  ★このモジュールは標準ライブラリだけで動き、製品固有の名前を持たない(切り出し可能)。
31
31
  """
32
+ import codecs
32
33
  import fnmatch
33
34
  import glob
34
35
  import os
35
36
  import re
36
37
  import shlex
38
+ import stat
37
39
  import urllib.parse
38
40
 
39
41
  # ═════════════════════════════════════════════════════════════════════════════
@@ -4081,6 +4083,198 @@ def _git_stage_values(sub, rest):
4081
4083
  return paths
4082
4084
 
4083
4085
 
4086
+ # ─────────────────────────────────────────────────────────────────────────────
4087
+ # 名指しが字面に無い形(一覧のファイル・標準入力)と git commit の名指し(便 16b2f958)
4088
+ #
4089
+ # 【塞いだ穴(便 9faa1627 の「分かっている限界」・CC1 が 0.12.166 で実測)】
4090
+ # `git add --pathspec-from-file=<一覧>`・`git hash-object -w --stdin-paths < <一覧>` は、
4091
+ # 一覧のファイルの中に秘密の名前を書けば記録に入った。追跡済みの秘密は
4092
+ # `git commit <秘密> -m x` で記録に入った。どれも allow だった。
4093
+ # 【どう塞ぐか】番人が一覧のファイルを読み、1行ずつ秘密の判定(secret_reason)にかける。
4094
+ # ★一覧が読めない(無い・ふつうのファイルでない・大きすぎる・変数・文字が壊れている・
4095
+ # 標準入力が `<` のファイルでない)ときは確かめられないので止める側に倒す。
4096
+ # ★一覧のファイル自体が秘密なら、中身を読まずに止める(番人も秘密は読まない)。
4097
+ # ★秘密の無い一覧・秘密でない名指し・名指しなしの git commit は今までどおり。
4098
+ # ─────────────────────────────────────────────────────────────────────────────
4099
+
4100
+ #: 一覧のファイルとして読む大きさの上限(これより大きければ確かめられない=止める)。
4101
+ _PATH_LIST_MAX_BYTES = 256 * 1024
4102
+
4103
+ #: 標準入力から名前の一覧を読む旗(git の下位の命令 → 旗)。
4104
+ _GIT_STDIN_LIST_FLAGS = {
4105
+ 'hash-object': '--stdin-paths',
4106
+ 'update-index': '--stdin',
4107
+ 'checkout-index': '--stdin',
4108
+ }
4109
+
4110
+
4111
+ def _long_flag_is(a, full):
4112
+ """`a` が長い旗 `full` か(git の省略形 `--pathspec-fr` も拾う・`=値` は見ない)。"""
4113
+ name = a.partition('=')[0]
4114
+ return a.startswith('--') and len(name) > 3 and full.startswith(name)
4115
+
4116
+
4117
+ def _git_path_lists(sub, rest):
4118
+ """名前の一覧を読む形なら [(読む先, NUL 区切りか)] を返す。読む先が `-` なら標準入力。"""
4119
+ out = []
4120
+ flags = []
4121
+ for a in rest:
4122
+ if a == '--':
4123
+ break
4124
+ flags.append(a)
4125
+ nul = any(_long_flag_is(a, '--pathspec-file-nul') for a in flags)
4126
+ for v in _flag_values(rest, ('--pathspec-from-file',), ''):
4127
+ out.append((v, nul))
4128
+ stdin_flag = _GIT_STDIN_LIST_FLAGS.get(sub)
4129
+ if stdin_flag and any(a == stdin_flag for a in flags):
4130
+ z = sub != 'hash-object' and any(
4131
+ a == '-z' or (a.startswith('-') and not a.startswith('--') and 'z' in a[1:])
4132
+ for a in flags)
4133
+ out.append(('-', z))
4134
+ return out
4135
+
4136
+
4137
+ def _stdin_source(args):
4138
+ """命令の標準入力の行き先(`< ファイル`)を返す。ファイルでない(パイプ・ヒアドキュメント)なら None。"""
4139
+ src = None
4140
+ for k, t in enumerate(args):
4141
+ if t in ('<', '0<') and k + 1 < len(args):
4142
+ src = args[k + 1]
4143
+ elif re.fullmatch(r'0?<[^<(&].*', t):
4144
+ src = t.lstrip('0')[1:]
4145
+ return src
4146
+
4147
+
4148
+ def _pathspec_body(entry):
4149
+ """pathspec の魔法の頭(`:(top)`・`:/`)を外した名前を返す。除く指定(`:!`・`:^`・exclude)は None。"""
4150
+ if not entry.startswith(':'):
4151
+ return entry
4152
+ m = re.match(r':\(([^)]*)\)', entry)
4153
+ if m:
4154
+ if any(w.strip() == 'exclude' for w in m.group(1).split(',')):
4155
+ return None
4156
+ return entry[m.end():]
4157
+ k = 1
4158
+ while k < len(entry) and entry[k] in '/!^':
4159
+ if entry[k] in '!^':
4160
+ return None
4161
+ k += 1
4162
+ if k < len(entry) and entry[k] == ':':
4163
+ k += 1
4164
+ return entry[k:]
4165
+
4166
+
4167
+ def _read_path_list(path, nul, where):
4168
+ """一覧のファイルを読み、(名前の一覧, 読めない理由) を返す。★秘密のファイルは読まない。"""
4169
+ if not path or any(c in path for c in '$`'):
4170
+ return None, 'list path is not fixed'
4171
+ sr = secret_reason(path, None if where is _UNKNOWN_DIR else where)
4172
+ if sr:
4173
+ return None, sr
4174
+ p = os.path.expanduser(path)
4175
+ if not os.path.isabs(p):
4176
+ if where is _UNKNOWN_DIR or not where:
4177
+ return None, 'list path is relative to an unknown folder'
4178
+ p = os.path.join(where, p)
4179
+ try:
4180
+ st = os.stat(p)
4181
+ except OSError:
4182
+ return None, 'list file cannot be read'
4183
+ if not stat.S_ISREG(st.st_mode):
4184
+ return None, 'list is not a regular file'
4185
+ if st.st_size > _PATH_LIST_MAX_BYTES:
4186
+ return None, 'list file too large to verify for secrets'
4187
+ try:
4188
+ with open(p, 'rb') as f:
4189
+ data = f.read(_PATH_LIST_MAX_BYTES + 1)
4190
+ except OSError:
4191
+ return None, 'list file cannot be read'
4192
+ if len(data) > _PATH_LIST_MAX_BYTES:
4193
+ return None, 'list file too large to verify for secrets'
4194
+ if nul:
4195
+ raw = data.split(b'\0')
4196
+ elif b'\0' in data:
4197
+ return None, 'list file has NUL bytes but is read as lines'
4198
+ else:
4199
+ raw = data.split(b'\n')
4200
+ names = []
4201
+ for r in raw:
4202
+ if not nul:
4203
+ r = r.rstrip(b'\r')
4204
+ if r.startswith(b'"') and r.endswith(b'"') and len(r) >= 2:
4205
+ try:
4206
+ r = codecs.escape_decode(r[1:-1])[0]
4207
+ except ValueError:
4208
+ return None, 'list line cannot be decoded'
4209
+ try:
4210
+ names.append(r.decode('utf-8'))
4211
+ except UnicodeDecodeError:
4212
+ return None, 'list line cannot be decoded'
4213
+ return names, None
4214
+
4215
+
4216
+ def _git_list_danger(sub, rest, args, cwd, where):
4217
+ """一覧のファイル・標準入力から名前を読む形で、秘密の行がある/確かめられないなら理由を返す。
4218
+
4219
+ ★`--pathspec-from-file=<一覧>` は git の作業場所(`-C` の後)から、`< <一覧>` は殻の作業場所
4220
+ (cwd)から読む。一覧の中の名前は git の作業場所から見る。
4221
+ """
4222
+ base = cwd if where is _UNKNOWN_DIR else where
4223
+ for src, nul in _git_path_lists(sub, rest):
4224
+ if src == '-':
4225
+ src = _stdin_source(args)
4226
+ if src is None:
4227
+ return f'path list for git {sub} cannot be verified for secrets (stdin is not a file)'
4228
+ names, why = _read_path_list(src, nul, cwd)
4229
+ else:
4230
+ names, why = _read_path_list(src, nul, where)
4231
+ if why:
4232
+ return f'path list for git {sub} cannot be verified for secrets ({why})'
4233
+ for n in names:
4234
+ body = _pathspec_body(n.strip())
4235
+ if not body:
4236
+ continue
4237
+ sr = secret_reason(body, base)
4238
+ if sr:
4239
+ return f'{sr} listed for git {sub}'
4240
+ return None
4241
+
4242
+
4243
+ def _git_commit_paths(rest):
4244
+ """git commit の名指し(旗でも旗の値でもない語・`--` の後ろ)を返す。
4245
+
4246
+ ★値を取る旗(`-m 説明`・`--author x` など)の値は名指しではない=説明文の字では止めない。
4247
+ """
4248
+ paths = []
4249
+ k = 0
4250
+ while k < len(rest):
4251
+ a = rest[k]
4252
+ k += 1
4253
+ if a == '--':
4254
+ paths += rest[k:]
4255
+ break
4256
+ if a in ('<', '0<', '>', '>>', '2>', '&>', '2>>'):
4257
+ k += 1 # リダイレクトの行き先は名指しではない
4258
+ continue
4259
+ if re.fullmatch(r'[0-9&]*[<>].*', a):
4260
+ continue
4261
+ if a.startswith('--'):
4262
+ if '=' not in a and any(_long_flag_is(a, L) for L in _COMMIT_VALUE_LONG):
4263
+ k += 1 # 値は次の語
4264
+ continue
4265
+ if a.startswith('-') and a != '-':
4266
+ for j, ch in enumerate(a[1:], 1):
4267
+ if ch in _COMMIT_VALUE_SHORT:
4268
+ if j == len(a) - 1:
4269
+ k += 1
4270
+ break
4271
+ if ch in _COMMIT_OPTVALUE_SHORT:
4272
+ break
4273
+ continue
4274
+ paths.append(a)
4275
+ return paths
4276
+
4277
+
4084
4278
  def _gh_file_values(args):
4085
4279
  """gh の後ろの語から、(命令の名前, 読むファイルの一覧) を返す。当てはまらなければ ('', [])."""
4086
4280
  path = []
@@ -4135,17 +4329,24 @@ def _vcs_file_danger(tokens, cwd=None):
4135
4329
  where = cwd
4136
4330
  if verb == 'git':
4137
4331
  sub, rest = _git_subcommand(args)
4332
+ got = _git_cwd(args, cwd)
4333
+ where = cwd if got is _UNKNOWN_DIR else got
4334
+ # 一覧のファイル・標準入力から名前を読む形(便 16b2f958)。
4335
+ hit = _git_list_danger(sub, rest, args, cwd, got)
4336
+ if hit:
4337
+ return hit
4138
4338
  spec = _GIT_FILE_FLAGS.get(sub)
4139
4339
  staged = _git_stage_values(sub, rest)
4140
4340
  if spec:
4141
4341
  vals = _flag_values(rest, *spec)
4342
+ if sub == 'commit':
4343
+ # git commit の名指し(便 16b2f958)。pathspec の頭(`:(top)`)は外し、除く指定は見ない。
4344
+ vals = vals + [b for b in map(_pathspec_body, _git_commit_paths(rest)) if b]
4142
4345
  elif staged is not None:
4143
4346
  vals = staged # 記録に入れる名指し(便 9faa1627)
4144
4347
  else:
4145
4348
  return None
4146
4349
  label = f'git {sub}'
4147
- got = _git_cwd(args, cwd)
4148
- where = cwd if got is _UNKNOWN_DIR else got
4149
4350
  elif verb == 'gh':
4150
4351
  label, vals = _gh_file_values(args)
4151
4352
  if not label:
@@ -0,0 +1,270 @@
1
+ #!/usr/bin/env python3
2
+ """名指しが字面に無い持ち出しの形と、git commit の名指しを塞いだ検査(便 16b2f958)。
3
+
4
+ 実行: python3 scripts/hooks/test_path_list_commit.py
5
+ 1件でも期待と違えば exit 1。
6
+
7
+ 【なぜ要るか(便 9faa1627 の「分かっている限界」・CC1 が 0.12.166 で実測)】
8
+ 次の形が allow で、秘密のファイルを記録に入れたあと、ふだんの push で GitHub へ持ち出せた。
9
+ ① `git add --pathspec-from-file=<一覧>` … 一覧のファイルの中に秘密の名前を書けば入る
10
+ ② `git hash-object -w --stdin-paths < <一覧>` … 同じ
11
+ ③ `git commit <秘密> -m x` … 追跡済みの秘密を名指しで記録に入れる
12
+ えふさん決定(2026-11-02):塞ぐ。守りを弱めない。止める側を足すだけ。
13
+
14
+ 【ここで固定すること】
15
+ (a) 止める(danger)… 上の①②③と、その書き方の違い・一覧が読めない形
16
+ (b) 通る … 秘密の無い一覧・秘密でない git commit の名指し・名指しなしの git commit
17
+ (c) 変えない … 前から danger/discipline の形はそのまま
18
+ (d) 部品の単体
19
+ (e) 空振り確認(足した判定を外すと (a) が落ちる)
20
+ ★この台本は秘密の形の名前を素の字で置かない(組み立てて使う)。一覧のファイルは使い捨ての
21
+ フォルダに作り、本物の秘密には1バイトも触らない。
22
+ """
23
+ import os
24
+ import shutil
25
+ import sys
26
+ import tempfile
27
+
28
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
29
+
30
+ import policy # noqa: E402
31
+
32
+ failures = []
33
+
34
+
35
+ def check(label, got, want):
36
+ ok = got == want
37
+ if not ok:
38
+ failures.append(f'{label}: got={got!r} want={want!r}')
39
+ print(f'{"OK " if ok else "NG "} {label:58} {str(got)[:46]}')
40
+
41
+
42
+ # ── 使い捨ての作り物(作業場所の根に秘密の形のファイルを置く=実地と同じ形)──────
43
+ ROOT = tempfile.mkdtemp(prefix='path-list-')
44
+ SITE = os.path.join(ROOT, 'site')
45
+ os.makedirs(os.path.join(SITE, 'src'))
46
+ os.makedirs(os.path.join(SITE, 'a-folder'))
47
+ NPMRC = '.npm' + 'rc'
48
+ ENV = '.en' + 'v'
49
+ DEV_VARS = '.dev' + '.vars'
50
+ for name in (NPMRC, ENV, DEV_VARS):
51
+ with open(os.path.join(SITE, name), 'w') as f:
52
+ f.write('DUMMY=placeholder\n')
53
+ SSH_KEY = '~/.ss' + 'h/id_' + 'rsa'
54
+
55
+
56
+ def make_list(name, body):
57
+ """一覧のファイルを使い捨てのフォルダに作り、絶対パスを返す。"""
58
+ p = os.path.join(ROOT, name)
59
+ with open(p, 'wb') as f:
60
+ f.write(body if isinstance(body, bytes) else body.encode('utf-8'))
61
+ return p
62
+
63
+
64
+ L_SECRET = make_list('secret.txt', f'{NPMRC}\n')
65
+ L_MIXED = make_list('mixed.txt', f'src/a.ts\npackage.json\n{ENV}\n')
66
+ L_NUL = make_list('nul.txt', f'src/a.ts\0{NPMRC}\0'.encode())
67
+ L_QUOTED = make_list('quoted.txt', '"\\056npm' + 'rc"\n')
68
+ L_MAGIC = make_list('magic.txt', f':(top){NPMRC}\n')
69
+ L_MAGIC2 = make_list('magic2.txt', f':/{DEV_VARS}\n')
70
+ L_KEY = make_list('key.txt', f'{SSH_KEY}\n')
71
+ L_CRLF = make_list('crlf.txt', f'src/a.ts\r\n{NPMRC}\r\n')
72
+ L_BIG = make_list('big.txt', b'a\n' * (policy._PATH_LIST_MAX_BYTES // 2 + 1))
73
+ L_NULBYTES = make_list('nulbytes.txt', f'src/a.ts\0{NPMRC}\n'.encode())
74
+ L_BADUTF = make_list('badutf.txt', b'src/\xff\xfe.ts\n')
75
+ L_CLEAN = make_list('clean.txt', 'src/a.ts\npackage.json\n\n')
76
+ L_CLEAN_NUL = make_list('clean-nul.txt', b'src/a.ts\0package.json\0')
77
+ L_EXCL = make_list('excl.txt', f'src\n:!{NPMRC}\n:(exclude){ENV}\n')
78
+ L_EXAMPLE = make_list('example.txt', f'{ENV}.example\n')
79
+ REL = os.path.join(SITE, 'rel-list.txt')
80
+ with open(REL, 'w') as f:
81
+ f.write(f'{NPMRC}\n')
82
+ L_MISSING = os.path.join(ROOT, 'missing.txt')
83
+ L_DIR = os.path.join(SITE, 'a-folder')
84
+
85
+
86
+ def kind(cmd):
87
+ r = policy.decide_bash(cmd, SITE)
88
+ return r['decision'] + ':' + r['reason'].split(':', 1)[0]
89
+
90
+
91
+ # ═════════════════════════════════════════════════════════════════════════════
92
+ # (a) 止める(danger)
93
+ # ═════════════════════════════════════════════════════════════════════════════
94
+ print('── (a1) 一覧のファイルに秘密の行 ' + '─' * 35)
95
+ DANGER_LIST = [
96
+ ('l01 add --pathspec-from-file=一覧(CC1 の実測)', f'git add --pathspec-from-file={L_SECRET}'),
97
+ ('l02 hash-object -w --stdin-paths < 一覧(CC1 の実測)', f'git hash-object -w --stdin-paths < {L_SECRET}'),
98
+ ('l03 旗と値を分けた形', f'git add --pathspec-from-file {L_SECRET}'),
99
+ ('l04 旗の省略形', f'git add --pathspec-fr={L_SECRET}'),
100
+ ('l05 git stage', f'git stage --pathspec-from-file={L_SECRET}'),
101
+ ('l06 git rm --cached', f'git rm --cached --pathspec-from-file={L_SECRET}'),
102
+ ('l07 git commit の一覧', f'git commit --pathspec-from-file={L_SECRET} -m x'),
103
+ ('l08 一覧が - で < の行き先', f'git add --pathspec-from-file=- < {L_SECRET}'),
104
+ ('l09 秘密がふつうの行に混ざる', f'git add --pathspec-from-file={L_MIXED}'),
105
+ ('l10 --pathspec-file-nul の一覧', f'git add --pathspec-from-file={L_NUL} --pathspec-file-nul'),
106
+ ('l11 C の書き方で隠した名前', f'git add --pathspec-from-file={L_QUOTED}'),
107
+ ('l12 pathspec の頭 :(top)', f'git add --pathspec-from-file={L_MAGIC}'),
108
+ ('l13 pathspec の頭 :/', f'git add --pathspec-from-file={L_MAGIC2}'),
109
+ ('l14 鍵の置き場の行', f'git add -f --pathspec-from-file={L_KEY}'),
110
+ ('l15 行末が CRLF', f'git add --pathspec-from-file={L_CRLF}'),
111
+ ('l16 hash-object(-w なし)の --stdin-paths', f'git hash-object --stdin-paths < {L_SECRET}'),
112
+ ('l17 update-index --add --stdin', f'git update-index --add --stdin < {L_SECRET}'),
113
+ ('l18 update-index -z --stdin', f'git update-index -z --add --stdin < {L_NUL}'),
114
+ ('l19 checkout-index --stdin', f'git checkout-index --stdin < {L_SECRET}'),
115
+ ('l20 -C 場所の相対の一覧', f'git -C {SITE} add --pathspec-from-file=rel-list.txt'),
116
+ ('l21 つないだ後ろ', f'git status && git add --pathspec-from-file={L_SECRET}'),
117
+ ('l22 包みの中', f'bash -c "git add --pathspec-from-file={L_SECRET}"'),
118
+ ('l23 0< の書き方', f'git hash-object -w --stdin-paths 0<{L_SECRET}'),
119
+ ]
120
+ for label, cmd in DANGER_LIST:
121
+ check(label, kind(cmd), 'deny:danger')
122
+
123
+ print('── (a2) 一覧が読めない=止める側に倒す ' + '─' * 28)
124
+ DANGER_UNREADABLE = [
125
+ ('u01 一覧のファイルが無い', f'git add --pathspec-from-file={L_MISSING}'),
126
+ ('u02 一覧がフォルダ', f'git add --pathspec-from-file={L_DIR}'),
127
+ ('u03 一覧が大きすぎる', f'git add --pathspec-from-file={L_BIG}'),
128
+ ('u04 一覧の行に NUL(行で読む)', f'git add --pathspec-from-file={L_NULBYTES}'),
129
+ ('u05 一覧の字が壊れている', f'git add --pathspec-from-file={L_BADUTF}'),
130
+ ('u06 一覧のパスが変数', 'git add --pathspec-from-file="$LIST"'),
131
+ ('u07 一覧のファイル自体が秘密', f'git add --pathspec-from-file={NPMRC}'),
132
+ ('u08 標準入力がパイプ', f'printf x | git hash-object -w --stdin-paths'),
133
+ ('u09 標準入力がパイプ(update-index)', 'printf x | git update-index --add --stdin'),
134
+ ('u10 - で < が無い', 'git add --pathspec-from-file=-'),
135
+ ('u11 標準入力が秘密のファイル', f'git update-index --add --stdin < {ENV}'),
136
+ ('u12 一覧が /dev/stdin', 'git add --pathspec-from-file=/dev/stdin'),
137
+ ]
138
+ for label, cmd in DANGER_UNREADABLE:
139
+ check(label, kind(cmd), 'deny:danger')
140
+
141
+ print('── (a3) git commit の名指し ' + '─' * 40)
142
+ DANGER_COMMIT = [
143
+ ('c01 git commit 秘密 -m x(CC1 の実測)', f'git commit {NPMRC} -m x'),
144
+ ('c02 -m の後ろの名指し', f'git commit -m x {NPMRC}'),
145
+ ('c03 --only', f'git commit --only {NPMRC} -m x'),
146
+ ('c04 -o', f'git commit -o {ENV} -m x'),
147
+ ('c05 -i', f'git commit -i {NPMRC} -m x'),
148
+ ('c06 --include', f'git commit --include {DEV_VARS} -m x'),
149
+ ('c07 -- の後ろ', f'git commit -m x -- {NPMRC}'),
150
+ ('c08 秘密でない物と並べる', f'git commit -m x src/a.ts {ENV}'),
151
+ ('c09 まとめた短い旗 -vm', f'git commit -vm x {NPMRC}'),
152
+ ('c10 --message=…', f'git commit --message=x {NPMRC}'),
153
+ ('c11 --author の値を飛ばす', f'git commit --author "A <a@example.com>" -m x {NPMRC}'),
154
+ ('c12 下のフォルダの秘密', f'git commit -m x src/{ENV}'),
155
+ ('c13 -C 場所', f'git -C {SITE} commit -m x {NPMRC}'),
156
+ ('c14 --amend と名指し', f'git commit --amend --no-edit {NPMRC}'),
157
+ ('c15 包みの中', f'bash -c "git commit -m x {NPMRC}"'),
158
+ ('c16 pathspec の頭 :(top)', f"git commit -m x ':(top){NPMRC}'"),
159
+ ]
160
+ for label, cmd in DANGER_COMMIT:
161
+ check(label, kind(cmd), 'deny:danger')
162
+
163
+ # ═════════════════════════════════════════════════════════════════════════════
164
+ # (b) 通る(ふだんの職人の命令)
165
+ # ═════════════════════════════════════════════════════════════════════════════
166
+ print('── (b) 秘密の無い一覧・ふだんの git commit は通る ' + '─' * 18)
167
+ PASS = [
168
+ ('n01 秘密の無い一覧', f'git add --pathspec-from-file={L_CLEAN}'),
169
+ ('n02 秘密の無い一覧(nul)', f'git add --pathspec-from-file={L_CLEAN_NUL} --pathspec-file-nul'),
170
+ ('n03 秘密の無い --stdin-paths', f'git hash-object -w --stdin-paths < {L_CLEAN}'),
171
+ ('n04 秘密の無い update-index --stdin', f'git update-index --add --stdin < {L_CLEAN}'),
172
+ ('n05 除く指定の行だけが秘密', f'git add --pathspec-from-file={L_EXCL}'),
173
+ ('n06 雛形の行', f'git add --pathspec-from-file={L_EXAMPLE}'),
174
+ ('n07 名指しなしの git commit', 'git commit -m x'),
175
+ ('n08 秘密でない名指し', 'git commit -m x src/a.ts'),
176
+ ('n09 --only で秘密でない名指し', 'git commit --only src/a.ts -m x'),
177
+ ('n10 -- の後ろの秘密でない名指し', 'git commit -m x -- src/a.ts package.json'),
178
+ ('n11 説明文に秘密の字(名指しではない)', f'git commit -m "git commit {NPMRC} を止める"'),
179
+ ('n12 --message の値に秘密の字', f'git commit --message "{ENV} の穴" src/a.ts'),
180
+ ('n13 --amend --no-edit', 'git commit --amend --no-edit'),
181
+ ('n14 -F で本文のファイル', f'git commit -F {L_CLEAN}'),
182
+ ('n15 --author の値', 'git commit --author "A <a@example.com>" -m x'),
183
+ ('n16 除く指定の秘密', f"git commit -m x -- src ':(exclude){NPMRC}'"),
184
+ ('n17 -m の値が - で始まる', 'git commit -m "-x" src/a.ts'),
185
+ ('n18 雛形の名指し', f'git commit -m x {ENV}.example'),
186
+ ('n19 git add -u', 'git add -u'),
187
+ ('n20 名指しの git add', 'git add src/a.ts'),
188
+ ('n21 update-index --refresh', 'git update-index --refresh'),
189
+ ]
190
+ for label, cmd in PASS:
191
+ check(label, kind(cmd), 'allow:no gate, no danger')
192
+
193
+ # ═════════════════════════════════════════════════════════════════════════════
194
+ # (c) 変えない(前から danger/discipline の形)
195
+ # ═════════════════════════════════════════════════════════════════════════════
196
+ print('── (c) 前の判定は変えない ' + '─' * 41)
197
+ check('k01 git commit -a -m x(作法のまま)', kind('git commit -a -m x'), 'deny:discipline')
198
+ check('k02 git commit -am x(作法のまま)', kind('git commit -am x'), 'deny:discipline')
199
+ check('k03 git add -A(作法のまま)', kind('git add -A'), 'deny:discipline')
200
+ check('k04 git commit -F 秘密(前から danger)', kind(f'git commit -F {NPMRC}'), 'deny:danger')
201
+ check('k05 git add 秘密(前から danger)', kind(f'git add -f {NPMRC}'), 'deny:danger')
202
+ check('k06 hash-object -w --stdin < 秘密(前から danger)',
203
+ kind(f'git hash-object -w --stdin < {ENV}'), 'deny:danger')
204
+
205
+ # ═════════════════════════════════════════════════════════════════════════════
206
+ # (d) 部品の単体
207
+ # ═════════════════════════════════════════════════════════════════════════════
208
+ print('── (d) 部品の単体 ' + '─' * 48)
209
+ PL = policy._git_path_lists
210
+ check('d01 --pathspec-from-file=', PL('add', ['--pathspec-from-file=a']), [('a', False)])
211
+ check('d02 旗と値を分けた形・nul', PL('add', ['--pathspec-file-nul', '--pathspec-from-file', 'a']),
212
+ [('a', True)])
213
+ check('d03 -- の後ろは旗でない', PL('add', ['--', '--pathspec-from-file=a']), [])
214
+ check('d04 --stdin-paths', PL('hash-object', ['-w', '--stdin-paths']), [('-', False)])
215
+ check('d05 update-index -z --stdin', PL('update-index', ['-z', '--stdin']), [('-', True)])
216
+ check('d06 hash-object --stdin は一覧でない', PL('hash-object', ['-w', '--stdin']), [])
217
+ check('d07 ほかの命令', PL('status', ['-s']), [])
218
+ SS = policy._stdin_source
219
+ check('d10 < の行き先', SS(['-w', '<', 'a']), 'a')
220
+ check('d11 <a の形', SS(['<a']), 'a')
221
+ check('d12 < が無い', SS(['-w']), None)
222
+ PB = policy._pathspec_body
223
+ check('d20 頭なし', PB('a/b'), 'a/b')
224
+ check('d21 :(top)', PB(':(top)a'), 'a')
225
+ check('d22 :/', PB(':/a'), 'a')
226
+ check('d23 :! は除く', PB(':!a'), None)
227
+ check('d24 :(exclude) は除く', PB(':(exclude)a'), None)
228
+ check('d25 :(top,exclude) は除く', PB(':(top,exclude)a'), None)
229
+ CP = policy._git_commit_paths
230
+ check('d30 名指し', CP(['-m', 'x', 'a', 'b']), ['a', 'b'])
231
+ check('d31 -m の値は名指しでない', CP(['-m', 'a']), [])
232
+ check('d32 -mabc は1語', CP(['-mabc', 'a']), ['a'])
233
+ check('d33 --author の値', CP(['--author', 'A', 'a']), ['a'])
234
+ check('d34 --auth の省略形', CP(['--auth', 'A', 'a']), ['a'])
235
+ check('d35 -- の後ろ', CP(['-m', 'x', '--', '-a']), ['-a'])
236
+ check('d36 リダイレクトの行き先', CP(['-m', 'x', '>', 'o', '2>&1']), [])
237
+ check('d37 -S鍵 は値を続けて書く', CP(['-Skey', 'a']), ['a'])
238
+ RL = policy._read_path_list
239
+ check('d40 一覧を読む', RL(L_CLEAN, False, ROOT), (['src/a.ts', 'package.json', '', ''], None))
240
+ check('d41 秘密の一覧は読まない', RL(NPMRC, False, SITE)[0], None)
241
+
242
+ # ═════════════════════════════════════════════════════════════════════════════
243
+ # (e) 空振り確認(試験の中で部品を外す)
244
+ # ═════════════════════════════════════════════════════════════════════════════
245
+ print('── (e) 空振り確認 ' + '─' * 48)
246
+ _real_list = policy._git_list_danger
247
+ policy._git_list_danger = lambda *a, **k: None # ①② を外す
248
+ check('e1 一覧を測らないと add が通る', kind(f'git add --pathspec-from-file={L_SECRET}'),
249
+ 'allow:no gate, no danger')
250
+ check('e2 一覧を測らないと --stdin-paths が通る',
251
+ kind(f'git hash-object -w --stdin-paths < {L_SECRET}'), 'allow:no gate, no danger')
252
+ policy._git_list_danger = _real_list
253
+ check('e3 戻すと止まる', kind(f'git add --pathspec-from-file={L_SECRET}'), 'deny:danger')
254
+ _real_cp = policy._git_commit_paths
255
+ policy._git_commit_paths = lambda rest: [] # ③ を外す
256
+ check('e4 名指しを測らないと commit が通る', kind(f'git commit {NPMRC} -m x'),
257
+ 'allow:no gate, no danger')
258
+ policy._git_commit_paths = _real_cp
259
+ check('e5 戻すと止まる', kind(f'git commit {NPMRC} -m x'), 'deny:danger')
260
+
261
+ shutil.rmtree(ROOT, ignore_errors=True)
262
+ check('z1 作り物を片付けた', os.path.exists(ROOT), False)
263
+
264
+ print()
265
+ if failures:
266
+ print(f'NG {len(failures)} 件')
267
+ for f in failures:
268
+ print(f' - {f}')
269
+ sys.exit(1)
270
+ print('すべて期待どおり')