-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21_precommit_hook.py
More file actions
378 lines (307 loc) · 11.5 KB
/
Copy path21_precommit_hook.py
File metadata and controls
378 lines (307 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""Example 21: Pre-commit Hook - Layer 4 Validation
This example demonstrates Layer 4 of the governance system:
Pre-commit validation that blocks commits with .parac/ structure violations.
Layer 4 provides a **safety net at commit time**, preventing violations
from entering version control.
What you'll learn:
1. How the pre-commit hook works
2. What happens when violations are detected
3. How to fix violations before committing
4. How to bypass (when absolutely necessary)
5. Integration with developer workflows
Prerequisites:
pip install paracle-core
paracle init # Installs hook automatically
"""
def example_1_hook_automatic_installation():
"""Example 1: Pre-commit hook installed automatically during init."""
print("=" * 70)
print("Example 1: Automatic Hook Installation")
print("=" * 70 + "\n")
print("When you run 'paracle init', the pre-commit hook is installed:\n")
print("$ cd my-project")
print("$ git init")
print("$ paracle init\n")
print("✅ Creates .parac/ structure")
print("✅ Installs pre-commit hook: .git/hooks/pre-commit")
print("✅ Makes hook executable\n")
print("The hook runs automatically on every 'git commit'")
print()
def example_2_valid_commit():
"""Example 2: Commit with valid .parac/ files succeeds."""
print("=" * 70)
print("Example 2: Valid Commit Succeeds")
print("=" * 70 + "\n")
print("Developer creates file in correct location:\n")
print("# Create database in correct location")
print("$ echo 'data' > .parac/memory/data/costs.db")
print("$ git add .parac/memory/data/costs.db")
print("$ git commit -m 'Add cost tracking database'\n")
print("Hook Output:")
print("─" * 70)
print("🔍 Validating 1 .parac/ file(s)...")
print("✅ All 1 file(s) validated successfully!")
print(" Commit allowed.\n")
print("─" * 70)
print("\n✅ Commit succeeds - file is in correct location\n")
def example_3_blocked_commit():
"""Example 3: Commit with invalid .parac/ files is blocked."""
print("=" * 70)
print("Example 3: Invalid Commit Blocked")
print("=" * 70 + "\n")
print("AI assistant creates file in wrong location:\n")
print("# Copilot creates database in .parac root (WRONG)")
print("$ echo 'data' > .parac/costs.db")
print("$ git add .parac/costs.db")
print("$ git commit -m 'Add database'\n")
print("Hook Output:")
print("─" * 70)
print("🔍 Validating 1 .parac/ file(s)...")
print()
print("=" * 70)
print("❌ COMMIT BLOCKED - .parac/ Structure Violations Found")
print("=" * 70)
print()
print("1. File: .parac/costs.db")
print(" Category: OPERATIONAL_DATA")
print(
" Issue: File placement violation: All databases must be in .parac/memory/data/"
)
print(" ✅ Fix: Move to .parac/memory/data/costs.db")
print()
print("=" * 70)
print("Total violations: 1")
print("=" * 70)
print()
print("To fix these violations:")
print()
print("Option 1: Auto-fix (recommended)")
print(" paracle validate structure --fix")
print()
print("Option 2: Manual fix")
print(" git mv .parac/costs.db .parac/memory/data/costs.db")
print()
print("Option 3: Bypass (NOT RECOMMENDED)")
print(" git commit --no-verify")
print()
print("Validation Summary:")
print(" ✅ Valid files: 0")
print(" ❌ Violations: 1")
print()
print("❌ Commit blocked due to structure violations.")
print(" Fix the issues above and try again.")
print()
print("─" * 70)
print("\n❌ Commit blocked - file is in wrong location\n")
def example_4_auto_fix_workflow():
"""Example 4: Using auto-fix to correct violations."""
print("=" * 70)
print("Example 4: Auto-Fix Workflow")
print("=" * 70 + "\n")
print("After commit is blocked, developer uses auto-fix:\n")
print("# Commit blocked")
print("$ git commit -m 'Add database'")
print("❌ COMMIT BLOCKED\n")
print("# Use auto-fix")
print("$ paracle validate structure --fix\n")
print("Auto-fix Output:")
print("─" * 70)
print("🔧 Fixing .parac/ structure violations...")
print()
print("Moving files:")
print(" .parac/costs.db → .parac/memory/data/costs.db ✅")
print()
print("✅ Fixed 1 violation(s)")
print("─" * 70)
print()
print("# Now commit succeeds")
print("$ git add .parac/memory/data/costs.db")
print("$ git commit -m 'Add cost tracking database'")
print("✅ All 1 file(s) validated successfully!")
print(" Commit allowed.\n")
print("✅ Workflow complete - violation fixed and committed\n")
def example_5_multiple_violations():
"""Example 5: Multiple violations in one commit."""
print("=" * 70)
print("Example 5: Multiple Violations")
print("=" * 70 + "\n")
print("Developer commits multiple files with violations:\n")
print("$ git add .parac/")
print("$ git commit -m 'Add project files'\n")
print("Hook Output:")
print("─" * 70)
print("🔍 Validating 3 .parac/ file(s)...")
print()
print("=" * 70)
print("❌ COMMIT BLOCKED - .parac/ Structure Violations Found")
print("=" * 70)
print()
print("1. File: .parac/costs.db")
print(" Category: OPERATIONAL_DATA")
print(
" Issue: File placement violation: All databases must be in .parac/memory/data/"
)
print(" ✅ Fix: Move to .parac/memory/data/costs.db")
print()
print("2. File: .parac/debug.log")
print(" Category: LOGS")
print(
" Issue: File placement violation: All log files must be in .parac/memory/logs/"
)
print(" ✅ Fix: Move to .parac/memory/logs/debug.log")
print()
print("3. File: .parac/architecture.md")
print(" Category: KNOWLEDGE")
print(
" Issue: File placement violation: Knowledge base files must be in .parac/memory/knowledge/"
)
print(" ✅ Fix: Move to .parac/memory/knowledge/architecture.md")
print()
print("=" * 70)
print("Total violations: 3")
print("=" * 70)
print()
print("Validation Summary:")
print(" ✅ Valid files: 0")
print(" ❌ Violations: 3")
print()
print("─" * 70)
print("\n❌ All violations must be fixed before commit\n")
def example_6_bypass_hook():
"""Example 6: Bypassing the hook (emergency only)."""
print("=" * 70)
print("Example 6: Bypassing Hook (Emergency Only)")
print("=" * 70 + "\n")
print("⚠️ WARNING: Only use --no-verify in emergencies!\n")
print("# Emergency commit (not recommended)")
print("$ git commit -m 'Emergency fix' --no-verify\n")
print("✅ Commit succeeds (hook bypassed)")
print()
print("⚠️ Note: Bypassing the hook:")
print(" - Creates governance violations in version control")
print(" - May break CI/CD pipelines")
print(" - Requires cleanup later")
print()
print("Better approach:")
print(" 1. Fix violations properly")
print(" 2. Or use 'git commit --amend' after fixing")
print()
def example_7_mixed_files():
"""Example 7: Commit with mixed .parac/ and regular files."""
print("=" * 70)
print("Example 7: Mixed Files Commit")
print("=" * 70 + "\n")
print("Developer commits both .parac/ and application files:\n")
print("$ git add .")
print("$ git commit -m 'Feature implementation'\n")
print("Hook Output:")
print("─" * 70)
print("🔍 Validating 2 .parac/ file(s)...")
print("✅ All 2 file(s) validated successfully!")
print(" Commit allowed.")
print("─" * 70)
print()
print("Files committed:")
print(" src/feature.py ✅ (not validated)")
print(" .parac/memory/data/metrics.db ✅ (validated, correct)")
print(" .parac/memory/logs/feature.log ✅ (validated, correct)")
print(" README.md ✅ (not validated)")
print()
print("✅ Hook only validates .parac/ files, allows rest through\n")
def example_8_integration_with_ci():
"""Example 8: Integration with CI/CD pipelines."""
print("=" * 70)
print("Example 8: CI/CD Integration")
print("=" * 70 + "\n")
print("Pre-commit hook works with CI/CD:\n")
print("# GitHub Actions workflow")
print("─" * 70)
print("name: Validate PR")
print()
print("on: [pull_request]")
print()
print("jobs:")
print(" validate:")
print(" runs-on: ubuntu-latest")
print(" steps:")
print(" - uses: actions/checkout@v3")
print(" - name: Validate .parac/ structure")
print(" run: |")
print(" python .parac/tools/hooks/validate-structure.py")
print("─" * 70)
print()
print("Benefits:")
print(" ✅ Catches violations in PRs")
print(" ✅ Prevents bad commits from merging")
print(" ✅ Consistent enforcement across team")
print(" ✅ No manual review needed for structure")
print()
def main():
"""Run all pre-commit hook examples."""
print("\n" + "=" * 70)
print("LAYER 4: PRE-COMMIT VALIDATION")
print("Git Hook that Blocks Commits with Structure Violations")
print("=" * 70 + "\n")
print("Layer 4 adds a safety net at commit time:\n")
print(" • Validates .parac/ files before commit")
print(" • Blocks commits with violations")
print(" • Provides auto-fix suggestions")
print(" • Integrates with developer workflows")
print(" • Works with CI/CD pipelines")
print()
input("Press Enter to see examples...")
print()
# Run examples
example_1_hook_automatic_installation()
input("Press Enter to continue...")
print()
example_2_valid_commit()
input("Press Enter to continue...")
print()
example_3_blocked_commit()
input("Press Enter to continue...")
print()
example_4_auto_fix_workflow()
input("Press Enter to continue...")
print()
example_5_multiple_violations()
input("Press Enter to continue...")
print()
example_6_bypass_hook()
input("Press Enter to continue...")
print()
example_7_mixed_files()
input("Press Enter to continue...")
print()
example_8_integration_with_ci()
print("\n" + "=" * 70)
print("SUMMARY: Layer 4 Pre-commit Validation")
print("=" * 70 + "\n")
print("What Layer 4 Provides:\n")
print(" ✅ Safety net at commit time")
print(" ✅ Prevents violations from entering version control")
print(" ✅ Auto-fix suggestions for quick resolution")
print(" ✅ Seamless developer workflow integration")
print(" ✅ CI/CD pipeline support")
print(" ✅ Team-wide consistency enforcement")
print()
print("Governance Layers So Far:\n")
print(" Layer 1: Automatic Action Logging ✅")
print(" Layer 2: Automatic State Management ✅")
print(" Layer 3: AI Compliance Engine (real-time blocking) ✅")
print(" Layer 4: Pre-commit Validation (commit-time blocking) ✅")
print(" Layer 5: Continuous Monitoring (planned)")
print()
print("Complete Protection:")
print(" • Layer 3 blocks violations during development (AI assistants)")
print(" • Layer 4 blocks violations at commit time (safety net)")
print(" • Layer 5 will monitor and auto-repair 24/7")
print()
print("Next Steps:")
print(" 1. Install hook: paracle init")
print(" 2. Test with: git commit")
print(" 3. Use auto-fix: paracle validate structure --fix")
print(" 4. Integrate with CI/CD")
print()
if __name__ == "__main__":
main()