destructive_command_guard 0.4.3

A Claude Code hook that blocks destructive commands before they execute
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! Core git patterns - protections against destructive git commands.
//!
//! This includes patterns for:
//! - Work destruction (reset --hard, checkout --, restore)
//! - History rewriting (push --force, branch -D)
//! - Stash destruction (stash drop, stash clear)

use crate::packs::{DestructivePattern, Pack, PatternSuggestion, SafePattern};
use crate::{destructive_pattern, safe_pattern};

/// Create the core git pack.
#[must_use]
pub fn create_pack() -> Pack {
    Pack {
        id: "core.git".to_string(),
        name: "Core Git",
        description: "Protects against destructive git commands that can lose uncommitted work, \
                      rewrite history, or destroy stashes",
        keywords: &["git"],
        safe_patterns: create_safe_patterns(),
        destructive_patterns: create_destructive_patterns(),
        keyword_matcher: None,
        safe_regex_set: None,
        safe_regex_set_is_complete: false,
    }
}

fn create_safe_patterns() -> Vec<SafePattern> {
    vec![
        // Branch creation is safe
        safe_pattern!(
            "checkout-new-branch",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*checkout\s+-b\s+"
        ),
        safe_pattern!(
            "checkout-orphan",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*checkout\s+--orphan\s+"
        ),
        // restore --staged only affects index, not working tree
        safe_pattern!(
            "restore-staged-long",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*restore\s+--staged\s+(?!.*--worktree)(?!.*-W\b)"
        ),
        safe_pattern!(
            "restore-staged-short",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*restore\s+-S\s+(?!.*--worktree)(?!.*-W\b)"
        ),
        // clean dry-run just previews, doesn't delete
        safe_pattern!(
            "clean-dry-run-short",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*clean\s+-[a-z]*n[a-z]*"
        ),
        safe_pattern!(
            "clean-dry-run-long",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*clean\s+--dry-run"
        ),
    ]
}

#[allow(clippy::too_many_lines)]
fn create_destructive_patterns() -> Vec<DestructivePattern> {
    // Severity levels:
    // - Critical: Most dangerous, irreversible, high-confidence detections
    // - High: Dangerous but more context-dependent (default)
    // - Medium: Warn by default
    // - Low: Log only

    vec![
        // checkout -- discards uncommitted changes
        destructive_pattern!(
            "checkout-discard",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*checkout\s+--\s+",
            "git checkout -- discards uncommitted changes permanently. Use 'git stash' first.",
            High,
            "git checkout -- <path> discards all uncommitted changes to the specified files \
             in your working directory. These changes are permanently lost - they cannot be \
             recovered because they were never committed.\n\n\
             Safer alternatives:\n\
             - git stash: Save changes temporarily, restore later with 'git stash pop'\n\
             - git diff <path>: Review what would be lost before discarding\n\n\
             Preview changes first:\n  git diff -- <path>",
            &const {
                [
                    PatternSuggestion::new(
                        "git stash",
                        "Save changes temporarily, restore later with 'git stash pop'",
                    ),
                    PatternSuggestion::new(
                        "git diff -- {path}",
                        "Review what would be lost before discarding",
                    ),
                ]
            }
        ),
        destructive_pattern!(
            "checkout-ref-discard",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*checkout\s+(?!-b\b)(?!--orphan\b)[^\s]+\s+--\s+",
            "git checkout <ref> -- <path> overwrites working tree. Use 'git stash' first.",
            High,
            "git checkout <ref> -- <path> replaces your working tree files with versions from \
             another commit or branch. Any uncommitted changes to those files are permanently \
             lost - they cannot be recovered.\n\n\
             Safer alternatives:\n\
             - git stash: Save changes first, then checkout, then restore with 'git stash pop'\n\
             - git show <ref>:<path>: View the file content without overwriting\n\n\
             Preview what would change:\n  git diff HEAD <ref> -- <path>",
            &const {
                [
                    PatternSuggestion::new(
                        "git stash",
                        "Save changes first, then checkout, then restore with 'git stash pop'",
                    ),
                    PatternSuggestion::new(
                        "git show {ref}:{path}",
                        "View the file content without overwriting",
                    ),
                    PatternSuggestion::new(
                        "git diff HEAD {ref} -- {path}",
                        "Preview what would change before overwriting",
                    ),
                ]
            }
        ),
        // restore without --staged affects working tree
        destructive_pattern!(
            "restore-worktree",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*restore\s+(?!--staged\b)(?!-S\b)",
            "git restore discards uncommitted changes. Use 'git stash' or 'git diff' first.",
            High,
            "git restore <path> discards uncommitted changes in your working directory, \
             reverting files to their last committed state. Changes that were never \
             committed are permanently lost.\n\n\
             Safer alternatives:\n\
             - git restore --staged <path>: Only unstage, keeps working directory changes\n\
             - git stash: Save all changes temporarily\n\
             - git diff <path>: Review what would be lost\n\n\
             Preview changes first:\n  git diff <path>",
            &const {
                [
                    PatternSuggestion::new(
                        "git restore --staged {path}",
                        "Only unstage, keeps working directory changes intact",
                    ),
                    PatternSuggestion::new(
                        "git stash",
                        "Save all changes temporarily, restore later with 'git stash pop'",
                    ),
                    PatternSuggestion::new(
                        "git diff {path}",
                        "Review what would be lost before discarding",
                    ),
                ]
            }
        ),
        destructive_pattern!(
            "restore-worktree-explicit",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*restore\s+.*(?:--worktree|-W\b)",
            "git restore --worktree/-W discards uncommitted changes permanently.",
            High,
            "git restore --worktree (or -W) explicitly targets your working directory, \
             discarding uncommitted changes. Even when combined with --staged, the worktree \
             changes are permanently lost.\n\n\
             Safer alternatives:\n\
             - git restore --staged <path>: Only unstage, keeps working directory\n\
             - git stash: Save changes first\n\n\
             Preview changes first:\n  git diff <path>",
            &const {
                [
                    PatternSuggestion::new(
                        "git restore --staged {path}",
                        "Only unstage, keeps working directory changes intact",
                    ),
                    PatternSuggestion::new(
                        "git stash",
                        "Save all changes temporarily before discarding",
                    ),
                    PatternSuggestion::new(
                        "git diff {path}",
                        "Review what would be lost before discarding",
                    ),
                ]
            }
        ),
        // reset --hard destroys uncommitted work (CRITICAL - extremely common mistake)
        destructive_pattern!(
            "reset-hard",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*reset\s+--hard",
            "git reset --hard destroys uncommitted changes. Use 'git stash' first.",
            Critical,
            "git reset --hard discards ALL uncommitted changes in your working directory \
             AND staging area. This is one of the most dangerous git commands because \
             changes that were never committed cannot be recovered by any means.\n\n\
             What gets destroyed:\n\
             - All modified files revert to the target commit\n\
             - All staged changes are lost\n\
             - Untracked files remain (use git clean to remove those)\n\n\
             Safer alternatives:\n\
             - git reset --soft <ref>: Move HEAD but keep all changes staged\n\
             - git reset --mixed <ref>: Move HEAD, unstage changes, keep working dir (default)\n\
             - git stash: Save changes before resetting\n\n\
             Preview what would be lost:\n  git status && git diff",
            &const {
                [
                    PatternSuggestion::new(
                        "git stash",
                        "Save all uncommitted changes before reset",
                    ),
                    PatternSuggestion::new(
                        "git reset --soft HEAD~1",
                        "Undo commit but keep all changes staged",
                    ),
                    PatternSuggestion::new(
                        "git reset --mixed HEAD~1",
                        "Undo commit, unstage changes, but keep working directory",
                    ),
                    PatternSuggestion::new(
                        "git checkout -- {file}",
                        "Reset a specific file only, preserving other changes",
                    ),
                ]
            }
        ),
        destructive_pattern!(
            "reset-merge",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*reset\s+--merge",
            "git reset --merge can lose uncommitted changes.",
            High,
            "git reset --merge resets the index and updates files in the working tree that \
             differ between the target commit and HEAD, but keeps changes that are not staged. \
             However, if there are uncommitted changes in files that need to be updated, \
             those changes will be lost.\n\n\
             Safer alternatives:\n\
             - git stash: Save uncommitted changes before reset\n\
             - git merge --abort: If in the middle of a merge, abort safely\n\n\
             Preview what would change:\n  git status && git diff",
            &const {
                [
                    PatternSuggestion::new("git stash", "Save uncommitted changes before reset"),
                    PatternSuggestion::new(
                        "git merge --abort",
                        "Abort the current merge safely without losing changes",
                    ),
                    PatternSuggestion::new(
                        "git status && git diff",
                        "Preview what would change before resetting",
                    ),
                ]
            }
        ),
        // clean -f deletes untracked files (CRITICAL - permanently removes files)
        destructive_pattern!(
            "clean-force",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*clean\s+(?:-[a-z]*f|--force\b)",
            "git clean -f/--force removes untracked files permanently. Review with 'git clean -n' first.",
            Critical,
            "git clean -f permanently deletes untracked files from your working directory. \
             These are files that have never been committed to git, so they cannot be \
             recovered from git history. If you haven't backed them up elsewhere, they \
             are gone forever.\n\n\
             Common dangerous combinations:\n\
             - git clean -fd: Also removes untracked directories\n\
             - git clean -xf: Also removes ignored files (build artifacts, .env, etc.)\n\n\
             Safer alternatives:\n\
             - git clean -n: Dry-run, shows what would be deleted\n\
             - git clean -i: Interactive mode, choose what to delete\n\n\
             ALWAYS preview first:\n  git clean -n -d",
            &const {
                [
                    PatternSuggestion::new(
                        "git clean -n",
                        "Dry run first (shows what would be deleted)",
                    ),
                    PatternSuggestion::new("git clean -nd", "Dry run including directories"),
                    PatternSuggestion::new(
                        "git clean -i",
                        "Interactive mode, choose what to delete",
                    ),
                    PatternSuggestion::new(
                        "git stash --include-untracked",
                        "Stash instead of delete (recoverable)",
                    ),
                ]
            }
        ),
        // force push can destroy remote history (CRITICAL - affects shared history)
        destructive_pattern!(
            "push-force-long",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*push\s+.*--force(?![-a-z])",
            "Force push can destroy remote history. Use --force-with-lease if necessary.",
            Critical,
            "git push --force overwrites remote history with your local history. This can \
             permanently destroy commits that others have already pulled, causing data loss \
             for your entire team. Collaborators may lose work, and recovering requires \
             manual intervention from everyone affected.\n\n\
             What can go wrong:\n\
             - Commits others pushed are deleted from remote\n\
             - Team members get diverged histories\n\
             - CI/CD pipelines may reference deleted commits\n\n\
             Safer alternative:\n\
             - git push --force-with-lease: Only forces if remote matches your last fetch\n\n\
             Check remote state first:\n  git fetch && git log origin/<branch>..HEAD",
            &const {
                [
                    PatternSuggestion::new(
                        "git push --force-with-lease",
                        "Fails if remote has new commits you haven't fetched",
                    ),
                    PatternSuggestion::new(
                        "git push --force-with-lease --force-if-includes",
                        "Even safer: also checks that your local ref includes the remote ref",
                    ),
                    PatternSuggestion::new(
                        "git fetch && git log origin/{branch}..HEAD",
                        "Preview what you're about to overwrite on the remote",
                    ),
                ]
            }
        ),
        destructive_pattern!(
            "push-force-short",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*push\s+.*-f\b",
            "Force push (-f) can destroy remote history. Use --force-with-lease if necessary.",
            Critical,
            "git push -f (short for --force) overwrites remote history with your local history. \
             This can permanently destroy commits that others have already pulled, causing data \
             loss for your entire team.\n\n\
             What can go wrong:\n\
             - Commits others pushed are deleted from remote\n\
             - Team members get diverged histories\n\
             - CI/CD pipelines may reference deleted commits\n\n\
             Safer alternative:\n\
             - git push --force-with-lease: Only forces if remote matches your last fetch\n\n\
             Check remote state first:\n  git fetch && git log origin/<branch>..HEAD",
            &const {
                [
                    PatternSuggestion::new(
                        "git push --force-with-lease",
                        "Fails if remote has new commits you haven't fetched",
                    ),
                    PatternSuggestion::new(
                        "git push --force-with-lease --force-if-includes",
                        "Even safer: also checks that your local ref includes the remote ref",
                    ),
                    PatternSuggestion::new(
                        "git fetch && git log origin/{branch}..HEAD",
                        "Preview what you're about to overwrite on the remote",
                    ),
                ]
            }
        ),
        // branch -D/-f force deletes or overwrites without checks (Medium: recoverable via reflog)
        destructive_pattern!(
            "branch-force-delete",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*branch\s+.*(?:-D\b|--force\b|-f\b)",
            "git branch -D/--force deletes branches without checks. Recoverable via 'git reflog'.",
            Medium,
            "git branch -D force-deletes a branch without checking if it has been merged. \
             If the branch contains unmerged commits, you may lose access to that work. \
             However, the commits still exist in git's object database and can be recovered \
             using reflog (for a limited time, typically 90 days).\n\n\
             Safer alternatives:\n\
             - git branch -d <branch>: Safe delete, fails if branch is not fully merged\n\
             - Merge the branch first, then delete with -d\n\n\
             Recovery if needed:\n\
               git reflog  # Find the commit hash\n\
               git checkout -b <branch> <commit-hash>",
            &const {
                [
                    PatternSuggestion::new(
                        "git branch -d {branch}",
                        "Safe delete: only works if branch is fully merged",
                    ),
                    PatternSuggestion::new(
                        "git branch -v {branch}",
                        "Show branch info (last commit) before deleting",
                    ),
                    PatternSuggestion::new(
                        "git log {branch} --oneline -10",
                        "Review branch commits before deleting",
                    ),
                ]
            }
        ),
        // stash destruction (Medium: single stash, recoverable via fsck/unreachable objects)
        destructive_pattern!(
            "stash-drop",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*stash\s+drop",
            "git stash drop deletes a single stash. Recoverable via `git fsck` (unreachable objects).",
            Medium,
            "git stash drop removes a specific stash entry from your stash list. The stashed \
             changes become unreferenced but remain in git's object database temporarily. \
             They can often be recovered using git fsck, but this is not guaranteed and \
             becomes harder over time as git garbage collects.\n\n\
             Safer alternatives:\n\
             - git stash pop: Apply and drop in one step (only drops if apply succeeds)\n\
             - git stash apply: Apply without dropping, verify first\n\n\
             Recovery if needed:\n\
               git fsck --unreachable | grep commit\n\
               git show <commit-hash>  # Inspect each to find your stash",
            &const {
                [
                    PatternSuggestion::new(
                        "git stash pop",
                        "Apply and drop atomically (only drops if apply succeeds)",
                    ),
                    PatternSuggestion::new(
                        "git stash apply",
                        "Apply without dropping, verify changes first",
                    ),
                    PatternSuggestion::new(
                        "git stash show stash@{0}",
                        "Preview stash contents before dropping",
                    ),
                    PatternSuggestion::new(
                        "git stash list",
                        "Review all stashes before dropping any",
                    ),
                ]
            }
        ),
        // stash clear destroys ALL stashes (CRITICAL)
        destructive_pattern!(
            "stash-clear",
            r"(?:^|[^[:alnum:]_-])git\s+(?:\S+\s+)*stash\s+clear",
            "git stash clear permanently deletes ALL stashed changes.",
            Critical,
            "git stash clear removes ALL stash entries at once. Unlike git stash drop, \
             which removes one at a time, this command wipes your entire stash list. \
             All stashed changes become unreferenced and are very difficult to recover.\n\n\
             What gets destroyed:\n\
             - All entries in 'git stash list' are removed\n\
             - Multiple sets of saved work-in-progress may be lost\n\n\
             Safer alternatives:\n\
             - git stash drop stash@{n}: Remove one specific stash at a time\n\
             - git stash list: Review what would be lost first\n\
             - git stash show stash@{n}: Inspect each stash before deciding\n\n\
             Recovery (difficult, not guaranteed):\n\
               git fsck --unreachable | grep commit",
            &const {
                [
                    PatternSuggestion::new(
                        "git stash drop stash@{n}",
                        "Remove one specific stash at a time",
                    ),
                    PatternSuggestion::new("git stash list", "Review all stashes before clearing"),
                    PatternSuggestion::new(
                        "git stash show stash@{n}",
                        "Inspect each stash before deciding to delete",
                    ),
                ]
            }
        ),
    ]
}

#[cfg(test)]
mod tests {
    //! Unit tests for core.git pack using the `test_helpers` framework.
    //!
    //! This module serves as an example of how to use the pack testing
    //! infrastructure. See `docs/pack-testing-guide.md` for details.

    use super::*;
    use crate::packs::Severity;
    use crate::packs::test_helpers::*;

    // =========================================================================
    // Pack Creation Tests
    // =========================================================================

    #[test]
    fn test_pack_creation() {
        let pack = create_pack();

        assert_eq!(pack.id, "core.git");
        assert_eq!(pack.name, "Core Git");
        assert!(!pack.description.is_empty());
        assert!(pack.keywords.contains(&"git"));

        // Validate patterns
        assert_patterns_compile(&pack);
        assert_all_patterns_have_reasons(&pack);
        assert_unique_pattern_names(&pack);
    }

    // =========================================================================
    // Critical Severity Pattern Tests
    // =========================================================================

    #[test]
    fn test_reset_hard_critical() {
        let pack = create_pack();

        assert_blocks_with_severity(&pack, "git reset --hard", Severity::Critical);
        assert_blocks_with_pattern(&pack, "git reset --hard", "reset-hard");
        assert_blocks(&pack, "git reset --hard HEAD", "destroys uncommitted");
        assert_blocks(&pack, "git reset --hard HEAD~1", "destroys uncommitted");
        assert_blocks(
            &pack,
            "git reset --hard origin/main",
            "destroys uncommitted",
        );
    }

    #[test]
    fn test_clean_force_critical() {
        let pack = create_pack();

        assert_blocks_with_severity(&pack, "git clean -f", Severity::Critical);
        assert_blocks_with_pattern(&pack, "git clean -f", "clean-force");
        assert_blocks(&pack, "git clean -fd", "removes untracked files");
        assert_blocks(&pack, "git clean -xf", "removes untracked files");
    }

    #[test]
    fn test_push_force_critical() {
        let pack = create_pack();

        assert_blocks_with_severity(&pack, "git push --force", Severity::Critical);
        assert_blocks_with_severity(&pack, "git push -f", Severity::Critical);
        assert_blocks(
            &pack,
            "git push origin main --force",
            "destroy remote history",
        );
        assert_blocks(
            &pack,
            "git push --force origin main",
            "destroy remote history",
        );
    }

    #[test]
    fn test_stash_clear_critical() {
        let pack = create_pack();

        assert_blocks_with_severity(&pack, "git stash clear", Severity::Critical);
        assert_blocks_with_pattern(&pack, "git stash clear", "stash-clear");
    }

    // =========================================================================
    // High Severity Pattern Tests
    // =========================================================================

    #[test]
    fn test_checkout_discard_high() {
        let pack = create_pack();

        assert_blocks_with_severity(&pack, "git checkout -- file.txt", Severity::High);
        assert_blocks_with_pattern(&pack, "git checkout -- file.txt", "checkout-discard");
        assert_blocks(&pack, "git checkout -- .", "discards uncommitted changes");
    }

    #[test]
    fn test_restore_worktree_high() {
        let pack = create_pack();

        assert_blocks_with_severity(&pack, "git restore file.txt", Severity::High);
        assert_blocks(
            &pack,
            "git restore --worktree file.txt",
            "discards uncommitted",
        );
    }

    #[test]
    fn test_branch_force_medium() {
        // Branch force delete is Medium severity (recoverable via reflog)
        let pack = create_pack();

        assert_blocks_with_severity(&pack, "git branch -D feature", Severity::Medium);
        assert_blocks_with_pattern(&pack, "git branch -D feature", "branch-force-delete");
        assert_blocks_with_pattern(&pack, "git branch --force feature", "branch-force-delete");
        assert_blocks_with_pattern(&pack, "git branch -f feature", "branch-force-delete");
    }

    #[test]
    fn test_stash_drop_medium() {
        // Stash drop is Medium severity (recoverable via fsck)
        let pack = create_pack();

        assert_blocks_with_severity(&pack, "git stash drop", Severity::Medium);
        assert_blocks(&pack, "git stash drop stash@{0}", "Recoverable");
    }

    // =========================================================================
    // Safe Pattern Tests
    // =========================================================================

    #[test]
    fn test_safe_checkout_new_branch() {
        let pack = create_pack();

        assert_safe_pattern_matches(&pack, "git checkout -b feature");
        assert_safe_pattern_matches(&pack, "git checkout -b feature/new-thing");
        assert_allows(&pack, "git checkout -b fix-123");
    }

    #[test]
    fn test_safe_checkout_orphan() {
        let pack = create_pack();

        assert_safe_pattern_matches(&pack, "git checkout --orphan gh-pages");
        assert_allows(&pack, "git checkout --orphan new-root");
    }

    #[test]
    fn test_safe_restore_staged() {
        let pack = create_pack();

        assert_allows(&pack, "git restore --staged file.txt");
        assert_allows(&pack, "git restore -S file.txt");
    }

    #[test]
    fn test_safe_clean_dry_run() {
        let pack = create_pack();

        assert_allows(&pack, "git clean -n");
        assert_allows(&pack, "git clean -dn");
        assert_allows(&pack, "git clean --dry-run");
    }

    // =========================================================================
    // Specificity Tests (False Positive Prevention)
    // =========================================================================

    #[test]
    fn test_specificity_safe_git_commands() {
        let pack = create_pack();

        test_batch_allows(
            &pack,
            &[
                "git status",
                "git log",
                "git log --oneline",
                "git diff",
                "git diff --cached",
                "git show HEAD",
                "git branch",
                "git branch -a",
                "git remote -v",
                "git fetch",
                "git pull",
                "git push", // Without --force
                "git add .",
                "git commit -m 'message'",
                "git branch -d feature", // Safe delete with -d
            ],
        );
    }

    #[test]
    fn test_specificity_unrelated_commands() {
        let pack = create_pack();

        assert_no_match(&pack, "ls -la");
        assert_no_match(&pack, "cargo build");
        assert_no_match(&pack, "npm install");
        assert_no_match(&pack, "docker run");
    }

    #[test]
    fn test_specificity_substring_not_matched() {
        let pack = create_pack();

        // "git" as substring should not trigger
        assert_no_match(&pack, "cat .gitignore");
        assert_no_match(&pack, "echo digit");
    }

    // =========================================================================
    // Performance Tests
    // =========================================================================

    #[test]
    fn test_performance_normal_commands() {
        let pack = create_pack();

        assert_matches_within_budget(&pack, "git reset --hard");
        assert_matches_within_budget(&pack, "git push --force origin main");
        assert_matches_within_budget(&pack, "git checkout -b feature/new");
    }

    #[test]
    fn test_performance_pathological_inputs() {
        let pack = create_pack();

        let long_flags = format!("git {}", "-".repeat(500));
        assert_matches_within_budget(&pack, &long_flags);

        let many_spaces = format!("git{}status", " ".repeat(100));
        assert_matches_within_budget(&pack, &many_spaces);
    }
}