claude-dev-env 8.117.2 → 8.117.4

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.
Files changed (77) hide show
  1. package/_shared/pr-loop/scripts/pyproject.toml +1 -1
  2. package/_shared/process-tree/scripts/pyproject.toml +1 -1
  3. package/bin/cde.mjs +4 -3
  4. package/bin/cde.test.mjs +87 -1
  5. package/bin/codex-compat.mjs +4 -3
  6. package/bin/codex-compat.test.mjs +91 -1
  7. package/bin/install-plan.mjs +2 -1
  8. package/bin/install.mjs +24 -7
  9. package/bin/install.plan.test.mjs +15 -0
  10. package/bin/install.test.mjs +61 -0
  11. package/bin/python-probe.mjs +42 -0
  12. package/bin/python-probe.test.mjs +19 -2
  13. package/docs/rule-guides/features-start-with-an-eval.md +3 -3
  14. package/docs/rule-guides/proof-before-pull-request.md +2 -2
  15. package/hooks/blocking/bash_pre_tool_use_dispatcher.py +11 -4
  16. package/hooks/blocking/build_eval_before_eval_write_gate.py +10 -4
  17. package/hooks/blocking/followup_pr_dedupe.py +381 -64
  18. package/hooks/blocking/gate_child_process.py +187 -0
  19. package/hooks/blocking/gate_run_reads.py +116 -35
  20. package/hooks/blocking/merge_review_gate.py +93 -10
  21. package/hooks/blocking/merge_review_gate_parts/create_folders.py +346 -0
  22. package/hooks/blocking/merge_review_gate_parts/create_options.py +86 -0
  23. package/hooks/blocking/merge_review_gate_parts/folder_state.py +107 -0
  24. package/hooks/blocking/merge_review_gate_parts/open_request.py +199 -64
  25. package/hooks/blocking/merge_review_gate_parts/quoted_lines.py +283 -0
  26. package/hooks/blocking/merge_review_gate_parts/wrapped_creates.py +126 -0
  27. package/hooks/blocking/pr_lifecycle_skill_gate.py +61 -18
  28. package/hooks/blocking/pre_tool_use_dispatcher.py +25 -3
  29. package/hooks/blocking/pull_request_proof.py +5 -51
  30. package/hooks/blocking/pull_request_proof_edit_gate.py +19 -5
  31. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +54 -0
  32. package/hooks/blocking/test_build_eval_before_eval_write_gate.py +64 -0
  33. package/hooks/blocking/test_followup_pr_dedupe.py +464 -1
  34. package/hooks/blocking/test_gate_child_process.py +64 -0
  35. package/hooks/blocking/test_gate_run_reads.py +364 -4
  36. package/hooks/blocking/test_merge_review_gate.py +832 -39
  37. package/hooks/blocking/test_pr_lifecycle_skill_gate.py +130 -4
  38. package/hooks/blocking/test_pre_tool_use_dispatcher.py +64 -0
  39. package/hooks/blocking/test_pull_request_proof.py +16 -0
  40. package/hooks/blocking/test_pull_request_proof_edit_gate.py +60 -0
  41. package/hooks/blocking/tests/test_create_folders.py +290 -0
  42. package/hooks/blocking/tests/test_create_options.py +34 -0
  43. package/hooks/blocking/tests/test_folder_state.py +40 -0
  44. package/hooks/blocking/tests/test_open_request.py +350 -44
  45. package/hooks/blocking/tests/test_quoted_lines.py +86 -0
  46. package/hooks/blocking/tests/test_wrapped_creates.py +42 -0
  47. package/hooks/features/bash-dispatchers.md +1 -1
  48. package/hooks/features/conduct-gates.md +7 -7
  49. package/hooks/features/write-edit-blocking.md +3 -3
  50. package/hooks/hooks_constants/build_eval_before_eval_write_gate_constants.py +5 -0
  51. package/hooks/hooks_constants/followup_pr_dedupe_constants.py +34 -1
  52. package/hooks/hooks_constants/gate_fault.py +52 -0
  53. package/hooks/hooks_constants/gate_fault_constants.py +13 -0
  54. package/hooks/hooks_constants/gate_run_reads_constants.py +9 -1
  55. package/hooks/hooks_constants/merge_review_gate_constants.py +94 -1
  56. package/hooks/hooks_constants/piped_pytest_blocker_constants.py +2 -0
  57. package/hooks/hooks_constants/pr_lifecycle_skill_gate_constants.py +6 -1
  58. package/hooks/hooks_constants/pre_tool_use_stdin.py +23 -3
  59. package/hooks/hooks_constants/shell_command_pipeline.py +68 -8
  60. package/hooks/hooks_constants/shell_command_segments.py +2 -2
  61. package/hooks/hooks_constants/shell_command_wrappers.py +3 -2
  62. package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +7 -0
  63. package/hooks/hooks_constants/test_gate_fault.py +41 -0
  64. package/hooks/hooks_constants/test_piped_pytest_blocker_constants.py +5 -0
  65. package/hooks/hooks_constants/test_pr_lifecycle_skill_gate_constants.py +10 -0
  66. package/hooks/hooks_constants/test_pre_tool_use_stdin.py +97 -0
  67. package/hooks/hooks_constants/test_shell_command_pipeline.py +55 -0
  68. package/hooks/hooks_constants/test_shell_command_segments.py +15 -0
  69. package/hooks/hooks_constants/test_shell_command_wrappers.py +6 -0
  70. package/hooks/hooks_constants/transcript_skill_scan_constants.py +6 -0
  71. package/hooks/pyproject.toml +2 -3
  72. package/hooks/test_python_floor.py +197 -0
  73. package/hooks/test_transcript_skill_scan.py +324 -0
  74. package/hooks/transcript_skill_scan.py +233 -2
  75. package/hooks/validators/code_quality_checks.py +119 -17
  76. package/hooks/validators/test_code_quality_checks.py +110 -4
  77. package/package.json +1 -1
@@ -0,0 +1,346 @@
1
+ """Walk a shell command in order and give each pull request create the folder it runs in.
2
+
3
+ ::
4
+
5
+ cd ../a && gh pr create --head x -> ["--head", "x"] in ../a
6
+ (cd ../a && gh pr create) ; gh pr create -> one in ../a, one in the starting folder
7
+ pushd ../a && popd && gh pr create -> the starting folder
8
+ cd "$WORK" && gh pr create -> folder not followed
9
+ { cd ../a; } | true; gh pr create -> folder not followed
10
+
11
+ The walk follows ``cd``, ``chdir``, ``Set-Location``, ``pushd`` and ``popd``,
12
+ also after ``builtin`` or ``command``, and the end of a subshell. A folder
13
+ change in a background job stays in that job. ``bash -c`` and substitutions
14
+ run as child shells, while ``eval`` changes the folder of the shell that runs
15
+ it. A folder change the walk does not model leaves each later create in that
16
+ scope unfollowed: one in a pipeline member, a brace group, ``if``, ``case``,
17
+ a loop or a function body, one after a redirect, or a folder word anywhere
18
+ but the program place. So does a folder it cannot name, such as one a
19
+ variable holds. ``quoted_lines.quote_aware_lines`` cuts each text into the
20
+ lines bash reads, and ``followup_pr_dedupe.create_arguments`` decides what
21
+ counts as a create. Shell variables, aliases and functions that hold a program
22
+ name are not followed. Text nested past ``MAXIMUM_UNWRAP_DEPTH`` that names
23
+ ``gh`` and a create word, or ``pull_request.py``, gives one create with no
24
+ options, unfollowed.
25
+ """
26
+
27
+ from __future__ import annotations
28
+
29
+ import sys
30
+ from dataclasses import dataclass, field
31
+ from pathlib import Path
32
+
33
+ hooks_directory = str(Path(__file__).resolve().parent.parent.parent)
34
+ if hooks_directory not in sys.path:
35
+ sys.path.insert(0, hooks_directory)
36
+
37
+ from blocking.followup_pr_dedupe import create_arguments
38
+ from blocking.merge_review_gate_parts.folder_state import FolderState, state_after_folder_change
39
+ from blocking.merge_review_gate_parts.quoted_lines import quote_aware_lines
40
+ from blocking.merge_review_gate_parts.wrapped_creates import unplaced_creates
41
+ from hooks_constants.merge_review_gate_constants import (
42
+ ALL_BACKGROUND_OPERATORS,
43
+ ALL_CLOSING_CHARACTERS,
44
+ ALL_COMMAND_PREFIX_WORDS,
45
+ ALL_COMPOUND_BODY_WORDS,
46
+ ALL_COMPOUND_CLOSE_WORDS,
47
+ ALL_COMPOUND_OPEN_WORDS,
48
+ ALL_FOLDER_CHANGE_WORDS,
49
+ ALL_PIPE_OPERATORS,
50
+ ALL_UNBALANCED_TOKENIZATION_MODES,
51
+ BRACE_CLOSE_TOKEN,
52
+ BRACE_OPEN_OPERATOR,
53
+ END_OF_OPTIONS_PREFIX,
54
+ IS_POSIX_TOKENIZATION,
55
+ LOOSE_GH_PROGRAM_PATTERN,
56
+ LOOSE_OPEN_WORD_PATTERN,
57
+ LOOSE_PULL_REQUEST_SCRIPT_PATTERN,
58
+ SEGMENT_TOKEN_JOIN,
59
+ SUBSHELL_CLOSE_TOKEN,
60
+ SUBSHELL_OPEN_OPERATOR,
61
+ SUBSTITUTION_KEY_IGNORED_PATTERN,
62
+ )
63
+ from hooks_constants.pytest_invocation import string_exec_inner_command, unquoted_token
64
+ from hooks_constants.shell_command_evaluation_constants import (
65
+ ALL_STRING_EVALUATING_PROGRAM_NAMES,
66
+ MAXIMUM_UNWRAP_DEPTH,
67
+ )
68
+ from hooks_constants.shell_command_pipeline import (
69
+ join_line_continuations,
70
+ operator_aware_tokens,
71
+ scannable_command_lines,
72
+ segments_with_following_operator,
73
+ )
74
+ from hooks_constants.shell_command_wrappers import (
75
+ all_command_substitution_texts,
76
+ evaluated_inner_command,
77
+ quoted_line_breaks_joined,
78
+ segment_program_and_arguments,
79
+ )
80
+
81
+
82
+ @dataclass(frozen=True)
83
+ class PlacedCreate:
84
+ """One create in a shell command and the folder it runs in.
85
+
86
+ Attributes:
87
+ all_arguments: The create's options, as ``create_arguments`` gives them.
88
+ folder: The folder in effect at the create, or None for the gate's own.
89
+ is_folder_followed: False when a folder change before it could not be followed.
90
+ all_command_words: The words of the simple command that runs it, in the order bash
91
+ passes them, or empty when the create was read without them.
92
+ """
93
+
94
+ all_arguments: tuple[str, ...]
95
+ folder: str | None
96
+ is_folder_followed: bool
97
+ all_command_words: tuple[str, ...] = field(default=(), compare=False)
98
+
99
+
100
+ @dataclass
101
+ class _Walk:
102
+ all_creates: list[PlacedCreate]
103
+
104
+
105
+ @dataclass
106
+ class _Scope:
107
+ state: FolderState
108
+ is_posix: bool = IS_POSIX_TOKENIZATION
109
+ previous_operator: str = ""
110
+ brace_depth: int = 0
111
+ compound_depth: int = 0
112
+ all_saved: list[FolderState] = field(default_factory=list)
113
+ all_live_substitution_keys: frozenset[str] = frozenset()
114
+
115
+
116
+ def all_placed_creates(command: str, folder: str | None) -> list[PlacedCreate]:
117
+ """Return each create in a shell command with the folder it runs in, in command order.
118
+
119
+ ::
120
+
121
+ "cd sub && gh pr create --fill", "/w" -> [PlacedCreate(("--fill",), "/w/sub", True)]
122
+ "cd $X && gh pr create --fill", "/w" -> [PlacedCreate(("--fill",), None, False)]
123
+
124
+ Each text is cut into lines and words as bash reads its quotes, so a line
125
+ break inside ``bash -c "..."`` or ``eval '...'`` starts a command while one
126
+ inside ``--body "..."`` does not. ``eval`` runs its words joined by spaces,
127
+ and the words after a ``)`` in a segment, such as a ``case`` arm's command,
128
+ are read for a wrapper too. A text whose quotes do not close is read as
129
+ written and with its quoted line breaks joined, each in POSIX and raw
130
+ words. ``wrapped_creates.unplaced_creates`` gives each create the
131
+ walk did not place, and it comes back unfollowed.
132
+
133
+ Args:
134
+ command: The shell command text.
135
+ folder: The folder the command starts in, or None for the gate's own.
136
+ """
137
+ start = FolderState(folder, True)
138
+ all_creates: list[PlacedCreate] = []
139
+ _walk_text(_Walk(all_creates), command, start, 0)
140
+ all_placed_arguments = [each_create.all_arguments for each_create in all_creates]
141
+ all_creates.extend(
142
+ PlacedCreate(each_arguments, None, False, each_words)
143
+ for each_arguments, each_words in unplaced_creates(command, all_placed_arguments)
144
+ )
145
+ return list(dict.fromkeys(all_creates))
146
+
147
+
148
+ def _walk_text(walk: _Walk, text: str, state: FolderState, depth: int) -> FolderState:
149
+ if depth > MAXIMUM_UNWRAP_DEPTH:
150
+ if _may_hold_create(text):
151
+ walk.all_creates.append(PlacedCreate((), None, False))
152
+ return state
153
+ all_token_lines = _posix_token_lines(text)
154
+ if all_token_lines is None:
155
+ return _walk_unbalanced_text(walk, text, state, depth)
156
+ scope = _Scope(state, all_live_substitution_keys=_substitution_keys(text))
157
+ return _walk_token_lines(walk, all_token_lines, scope, depth)
158
+
159
+
160
+ def _may_hold_create(text: str) -> bool:
161
+ is_gh_create = LOOSE_GH_PROGRAM_PATTERN.search(text) and LOOSE_OPEN_WORD_PATTERN.search(text)
162
+ return bool(is_gh_create or LOOSE_PULL_REQUEST_SCRIPT_PATTERN.search(text))
163
+
164
+
165
+ def _posix_token_lines(text: str) -> list[list[str]] | None:
166
+ all_lines = quote_aware_lines(text)
167
+ if all_lines is None:
168
+ return None
169
+ all_token_lines = [
170
+ each_tokens
171
+ for each_line in all_lines
172
+ if (each_tokens := operator_aware_tokens(each_line, IS_POSIX_TOKENIZATION)) is not None
173
+ ]
174
+ return all_token_lines if len(all_token_lines) == len(all_lines) else None
175
+
176
+
177
+ def _walk_unbalanced_text(
178
+ walk: _Walk, text: str, state: FolderState, depth: int
179
+ ) -> FolderState:
180
+ all_end_states: set[FolderState] = set()
181
+ for each_text in dict.fromkeys([text, quoted_line_breaks_joined(text)]):
182
+ for each_is_posix in ALL_UNBALANCED_TOKENIZATION_MODES:
183
+ all_token_lines = _line_tokens(each_text, each_is_posix)
184
+ scope = _Scope(state, each_is_posix, all_live_substitution_keys=_substitution_keys(each_text))
185
+ all_end_states.add(_walk_token_lines(walk, all_token_lines, scope, depth))
186
+ return state if all_end_states == {state} else FolderState(None, False)
187
+
188
+
189
+ def _line_tokens(text: str, is_posix: bool) -> list[list[str]]:
190
+ return [
191
+ operator_aware_tokens(each_line, is_posix) or []
192
+ for each_line in scannable_command_lines(join_line_continuations(text))
193
+ ]
194
+
195
+
196
+ def _walk_token_lines(
197
+ walk: _Walk, all_token_lines: list[list[str]], scope: _Scope, depth: int
198
+ ) -> FolderState:
199
+ for each_tokens in all_token_lines:
200
+ for each_segment, each_operator in segments_with_following_operator(each_tokens):
201
+ _walk_step(walk, scope, each_segment, each_operator, depth)
202
+ return scope.state
203
+
204
+
205
+ def _walk_step(
206
+ walk: _Walk, scope: _Scope, all_segment_tokens: list[str], operator: str, depth: int
207
+ ) -> None:
208
+ _walk_segment(walk, scope, all_segment_tokens, operator, depth)
209
+ _restore_closed_subshells(scope, _closing_count(all_segment_tokens))
210
+ if operator == SUBSHELL_OPEN_OPERATOR:
211
+ scope.all_saved.append(scope.state)
212
+ scope.brace_depth += operator == BRACE_OPEN_OPERATOR
213
+ scope.previous_operator = operator
214
+
215
+
216
+ def _restore_closed_subshells(scope: _Scope, closing_count: int) -> None:
217
+ if closing_count == 0 or not scope.all_saved:
218
+ return
219
+ kept_count = max(0, len(scope.all_saved) - closing_count)
220
+ scope.state = scope.all_saved[kept_count]
221
+ del scope.all_saved[kept_count:]
222
+
223
+
224
+ def _walk_segment(
225
+ walk: _Walk, scope: _Scope, all_segment_tokens: list[str], operator: str, depth: int
226
+ ) -> None:
227
+ all_tokens = _without_closing_tokens(all_segment_tokens)
228
+ while all_tokens[:1] == [BRACE_CLOSE_TOKEN]:
229
+ all_tokens = all_tokens[1:]
230
+ scope.brace_depth = max(0, scope.brace_depth - 1)
231
+ first_word = unquoted_token(all_tokens[0]).lower() if all_tokens else ""
232
+ scope.compound_depth += first_word in ALL_COMPOUND_OPEN_WORDS
233
+ is_piped = scope.previous_operator in ALL_PIPE_OPERATORS or operator in ALL_PIPE_OPERATORS
234
+ is_modeled = (
235
+ not is_piped
236
+ and scope.brace_depth == 0
237
+ and scope.compound_depth == 0
238
+ and first_word not in ALL_COMPOUND_BODY_WORDS
239
+ )
240
+ next_state = _segment_step(walk, scope, all_tokens, depth, is_modeled)
241
+ if operator not in ALL_BACKGROUND_OPERATORS:
242
+ scope.state = next_state
243
+ if first_word in ALL_COMPOUND_CLOSE_WORDS:
244
+ scope.compound_depth = max(0, scope.compound_depth - 1)
245
+
246
+
247
+ def _closing_count(all_segment_tokens: list[str]) -> int:
248
+ all_closing_tokens = all_segment_tokens[len(_without_closing_tokens(all_segment_tokens)) :]
249
+ return sum(each_token.count(SUBSHELL_CLOSE_TOKEN) for each_token in all_closing_tokens)
250
+
251
+
252
+ def _without_closing_tokens(all_segment_tokens: list[str]) -> list[str]:
253
+ all_tokens = list(all_segment_tokens)
254
+ while all_tokens and SUBSHELL_CLOSE_TOKEN in all_tokens[-1] and not all_tokens[-1].strip(ALL_CLOSING_CHARACTERS):
255
+ all_tokens.pop()
256
+ return all_tokens
257
+
258
+
259
+ def _segment_step(
260
+ walk: _Walk, scope: _Scope, all_tokens: list[str], depth: int, is_modeled: bool
261
+ ) -> FolderState:
262
+ state = scope.state
263
+ all_readings = _wrapper_readings(all_tokens)
264
+ for each_reading in all_readings:
265
+ all_arguments = create_arguments(each_reading)
266
+ if all_arguments is not None:
267
+ walk.all_creates.append(PlacedCreate(tuple(all_arguments), state.folder, state.is_followed, tuple(each_reading)))
268
+ break
269
+ for each_child_text in dict.fromkeys(
270
+ each_text for each_reading in all_readings for each_text in _child_shell_texts(each_reading, scope)
271
+ ):
272
+ _walk_text(walk, each_child_text, state, depth + 1)
273
+ next_state = _state_after_evaluation(walk, scope, all_readings, depth)
274
+ next_state = state_after_folder_change(_without_command_prefixes(all_tokens), next_state)
275
+ if (next_state != state and not is_modeled) or _has_unmodeled_folder_word(all_tokens):
276
+ return FolderState(None, False)
277
+ return next_state
278
+
279
+
280
+ def _state_after_evaluation(
281
+ walk: _Walk, scope: _Scope, all_readings: list[list[str]], depth: int
282
+ ) -> FolderState:
283
+ for each_reading in all_readings:
284
+ evaluated_text = _evaluated_text(each_reading, scope.is_posix)
285
+ if evaluated_text is None:
286
+ continue
287
+ evaluated_state = _walk_text(walk, evaluated_text, scope.state, depth + 1)
288
+ is_same_shell = (
289
+ segment_program_and_arguments(each_reading)[0] in ALL_STRING_EVALUATING_PROGRAM_NAMES
290
+ )
291
+ return evaluated_state if is_same_shell else scope.state
292
+ return scope.state
293
+
294
+
295
+ def _wrapper_readings(all_tokens: list[str]) -> list[list[str]]:
296
+ if SUBSHELL_CLOSE_TOKEN not in all_tokens:
297
+ return [all_tokens]
298
+ last_close_index = len(all_tokens) - 1 - all_tokens[::-1].index(SUBSHELL_CLOSE_TOKEN)
299
+ all_after_pattern = all_tokens[last_close_index + 1 :]
300
+ return [all_tokens, all_after_pattern] if all_after_pattern else [all_tokens]
301
+
302
+
303
+ def _evaluated_text(all_tokens: list[str], is_posix: bool) -> str | None:
304
+ program, all_arguments = segment_program_and_arguments(all_tokens)
305
+ if is_posix and program.lower() in ALL_STRING_EVALUATING_PROGRAM_NAMES:
306
+ return SEGMENT_TOKEN_JOIN.join(all_arguments)
307
+ return evaluated_inner_command(all_tokens)
308
+
309
+
310
+ def _without_command_prefixes(all_tokens: list[str]) -> list[str]:
311
+ all_remaining = list(all_tokens)
312
+ while (
313
+ len(all_remaining) > 1
314
+ and unquoted_token(all_remaining[0]).lower() in ALL_COMMAND_PREFIX_WORDS
315
+ ):
316
+ all_remaining = all_remaining[1:]
317
+ return all_remaining
318
+
319
+
320
+ def _has_unmodeled_folder_word(all_tokens: list[str]) -> bool:
321
+ all_command_tokens = _without_command_prefixes(all_tokens)
322
+ program = segment_program_and_arguments(all_command_tokens)[0]
323
+ if program in ALL_FOLDER_CHANGE_WORDS:
324
+ return False
325
+ return any(each_token.lower() in ALL_FOLDER_CHANGE_WORDS for each_token in all_command_tokens)
326
+
327
+
328
+ def _child_shell_texts(all_tokens: list[str], scope: _Scope) -> list[str]:
329
+ string_exec_text = string_exec_inner_command(all_tokens)
330
+ if string_exec_text is not None:
331
+ string_exec_text = string_exec_text.removeprefix(END_OF_OPTIONS_PREFIX)
332
+ all_substitution_texts = [
333
+ each_text
334
+ for each_text in all_command_substitution_texts(SEGMENT_TOKEN_JOIN.join(all_tokens))
335
+ if _substitution_key(each_text) in scope.all_live_substitution_keys
336
+ ]
337
+ return [*([string_exec_text] if string_exec_text is not None else []), *all_substitution_texts]
338
+
339
+
340
+ def _substitution_keys(text: str) -> frozenset[str]:
341
+ return frozenset(_substitution_key(each_text) for each_text in all_command_substitution_texts(text))
342
+
343
+
344
+ def _substitution_key(text: str) -> str:
345
+ return SUBSTITUTION_KEY_IGNORED_PATTERN.sub("", text)
346
+
@@ -0,0 +1,86 @@
1
+ """Read the values one create's flags give, the way gh reads them.
2
+
3
+ ::
4
+
5
+ ["--head", "a", "-dHb"], "--head", "H" -> ["a", "b"]
6
+ ["--title", "-Hx", "--head", "a"], "--head", "H" -> ["a"]
7
+ ["--title", "--repo=x/y"], "--repo", "R" -> []
8
+
9
+ A word that is the value of another option is that option's value, not a
10
+ flag. A short-flag cluster such as ``-dHb`` gives the rest of the cluster
11
+ to its first letter that takes a value. Reading stops at ``--``.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import sys
17
+ from pathlib import Path
18
+
19
+ hooks_directory = str(Path(__file__).resolve().parent.parent.parent)
20
+ if hooks_directory not in sys.path:
21
+ sys.path.insert(0, hooks_directory)
22
+
23
+ from hooks_constants.merge_review_gate_constants import (
24
+ ALL_VALUELESS_LONG_CREATE_OPTIONS,
25
+ ALL_VALUELESS_SHORT_CREATE_LETTERS,
26
+ LONG_OPTION_PREFIX,
27
+ OPTION_AND_VALUE_WIDTH,
28
+ OPTION_VALUE_SEPARATOR,
29
+ SHORT_OPTION_PREFIX,
30
+ )
31
+ from hooks_constants.pytest_invocation import unquoted_token
32
+
33
+
34
+ def all_option_values(all_words: list[str], long_option: str, short_letter: str) -> list[str]:
35
+ """Return each value one option is given, in command order.
36
+
37
+ ::
38
+
39
+ ["-H", "a", "--head=b"], "--head", "H" -> ["a", "b"]
40
+ ["--body", "-H", "x"], "--head", "H" -> []
41
+
42
+ Args:
43
+ all_words: The create's words after the program.
44
+ long_option: The long spelling, such as ``--head``.
45
+ short_letter: The short letter, such as ``H``.
46
+ """
47
+ all_values: list[str] = []
48
+ index = 0
49
+ while index < len(all_words) and unquoted_token(all_words[index]) != LONG_OPTION_PREFIX:
50
+ following = all_words[index + 1] if index + 1 < len(all_words) else None
51
+ flag_value, width = _option_reading(
52
+ unquoted_token(all_words[index]), following, long_option, short_letter
53
+ )
54
+ if flag_value is not None:
55
+ all_values.append(unquoted_token(flag_value))
56
+ index += width
57
+ return all_values
58
+
59
+
60
+ def _option_reading(
61
+ word: str, following: str | None, long_option: str, short_letter: str
62
+ ) -> tuple[str | None, int]:
63
+ if not word.startswith(SHORT_OPTION_PREFIX) or word == SHORT_OPTION_PREFIX:
64
+ return None, 1
65
+ if not word.startswith(LONG_OPTION_PREFIX):
66
+ return _short_cluster_reading(word[len(SHORT_OPTION_PREFIX) :], following, short_letter)
67
+ option_name, separator, inline_value = word.partition(OPTION_VALUE_SEPARATOR)
68
+ if option_name == long_option:
69
+ return (inline_value, 1) if separator else (following, OPTION_AND_VALUE_WIDTH)
70
+ if separator or option_name in ALL_VALUELESS_LONG_CREATE_OPTIONS:
71
+ return None, 1
72
+ return None, OPTION_AND_VALUE_WIDTH
73
+
74
+
75
+ def _short_cluster_reading(
76
+ all_letters: str, following: str | None, short_letter: str
77
+ ) -> tuple[str | None, int]:
78
+ for each_index, each_letter in enumerate(all_letters):
79
+ if each_letter in ALL_VALUELESS_SHORT_CREATE_LETTERS:
80
+ continue
81
+ inline_value = all_letters[each_index + 1 :].removeprefix(OPTION_VALUE_SEPARATOR)
82
+ flag_value, width = (
83
+ (inline_value, 1) if inline_value else (following, OPTION_AND_VALUE_WIDTH)
84
+ )
85
+ return (flag_value if each_letter == short_letter else None), width
86
+ return None, 1
@@ -0,0 +1,107 @@
1
+ """The folder state the create walk carries: the folder, whether it is followed, and the pushed folders.
2
+
3
+ ::
4
+
5
+ cd sub from /w -> /w/sub
6
+ pushd ../a from /w -> /w/../a, with /w pushed
7
+ popd after that pushd -> /w
8
+ cd "$X" from /w -> folder not followed
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import os
14
+ import sys
15
+ from dataclasses import dataclass
16
+ from pathlib import Path
17
+
18
+ hooks_directory = str(Path(__file__).resolve().parent.parent.parent)
19
+ if hooks_directory not in sys.path:
20
+ sys.path.insert(0, hooks_directory)
21
+
22
+ from hooks_constants.merge_review_gate_constants import (
23
+ ALL_CHANGE_FOLDER_PROGRAMS,
24
+ ALL_HOME_DEFAULT_PROGRAMS,
25
+ ALL_PATH_PARAMETER_NAMES,
26
+ ALL_POP_FOLDER_PROGRAMS,
27
+ ALL_PUSH_FOLDER_PROGRAMS,
28
+ ALL_UNFOLLOWED_FOLDER_CHARACTERS,
29
+ HOME_FOLDER,
30
+ OPTION_PREFIX,
31
+ PREVIOUS_FOLDER_ARGUMENT,
32
+ )
33
+ from hooks_constants.pytest_invocation import unquoted_token
34
+ from hooks_constants.shell_command_wrappers import segment_program_and_arguments
35
+
36
+
37
+ @dataclass(frozen=True)
38
+ class FolderState:
39
+ """The folder a shell is in at one point in a command.
40
+
41
+ Attributes:
42
+ folder: The folder in effect, or None for the gate's own.
43
+ is_followed: False once a folder change could not be followed.
44
+ all_pushed: The ``pushd`` stack, each entry a folder and whether it was followed.
45
+ """
46
+
47
+ folder: str | None
48
+ is_followed: bool
49
+ all_pushed: tuple[tuple[str | None, bool], ...] = ()
50
+
51
+
52
+ def state_after_folder_change(all_tokens: list[str], state: FolderState) -> FolderState:
53
+ """Return the state after one simple command, when it changes the folder.
54
+
55
+ ::
56
+
57
+ ["cd", "sub"], /w -> /w/sub
58
+ ["cd", "-"], /w -> folder not followed
59
+ ["popd"], nothing pushed -> folder not followed
60
+ ["git", "status"], /w -> /w
61
+
62
+ Args:
63
+ all_tokens: The command's tokens, with ``builtin`` and ``command`` removed.
64
+ state: The state before the command.
65
+ """
66
+ program, all_arguments = segment_program_and_arguments(all_tokens)
67
+ if program in ALL_POP_FOLDER_PROGRAMS:
68
+ return _state_after_pop(state)
69
+ is_push = program in ALL_PUSH_FOLDER_PROGRAMS
70
+ if not is_push and program not in ALL_CHANGE_FOLDER_PROGRAMS:
71
+ return state
72
+ all_pushed = (
73
+ (*state.all_pushed, (state.folder, state.is_followed)) if is_push else state.all_pushed
74
+ )
75
+ target = _folder_target(all_arguments)
76
+ if target is None:
77
+ target = HOME_FOLDER if program in ALL_HOME_DEFAULT_PROGRAMS else PREVIOUS_FOLDER_ARGUMENT
78
+ folder, is_followed = _followed_folder(target, state)
79
+ return FolderState(folder, is_followed, all_pushed)
80
+
81
+
82
+ def _state_after_pop(state: FolderState) -> FolderState:
83
+ if not state.all_pushed:
84
+ return FolderState(None, False)
85
+ popped_folder, is_popped_followed = state.all_pushed[-1]
86
+ return FolderState(popped_folder, is_popped_followed, state.all_pushed[:-1])
87
+
88
+
89
+ def _folder_target(all_arguments: list[str]) -> str | None:
90
+ is_path_value = False
91
+ for each_argument in all_arguments:
92
+ word = unquoted_token(each_argument)
93
+ if is_path_value or not word.startswith(OPTION_PREFIX) or word == PREVIOUS_FOLDER_ARGUMENT:
94
+ return word
95
+ is_path_value = word.lower() in ALL_PATH_PARAMETER_NAMES
96
+ return None
97
+
98
+
99
+ def _followed_folder(target: str, state: FolderState) -> tuple[str | None, bool]:
100
+ if target == PREVIOUS_FOLDER_ARGUMENT or ALL_UNFOLLOWED_FOLDER_CHARACTERS & set(target):
101
+ return None, False
102
+ target_path = Path(os.path.expanduser(target))
103
+ if target_path.is_absolute():
104
+ return str(target_path), True
105
+ if not state.is_followed:
106
+ return None, False
107
+ return (str(Path(state.folder) / target_path) if state.folder else str(target_path)), True