prek 0.5.4

A fast Git hook manager written in Rust, designed as a drop-in alternative to pre-commit, reimagined.
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
use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use insta::assert_snapshot;
use prek_consts::PRE_COMMIT_CONFIG_YAML;
use prek_consts::env_vars::EnvVars;

use crate::common::{TestEnv, cmd_snapshot};

#[test]
fn run_in_non_git_repo() {
    let context = TestEnv::new();

    cmd_snapshot!(context, context.run().env(EnvVars::LC_ALL, "fr_FR.UTF-8"), @r#"
    success: false
    exit_code: 2
    ----- stdout -----

    ----- stderr -----
    error: Not in a Git repository. Change to a Git repository, or run `git init` to create one.
    "#);
}

#[test]
fn run_preserves_git_discovery_errors() {
    let context = TestEnv::new()
        .with_config("repos: []")
        .with_filter(
            r"Command `[^`]*git(?:\.exe)? rev-parse --absolute-git-dir --git-common-dir --git-path hooks --show-toplevel`",
            "Command `[GIT] rev-parse --absolute-git-dir --git-common-dir --git-path hooks --show-toplevel`",
        )
        .init_git()
        .with_file("invalid.gitconfig", "[invalid\n");

    cmd_snapshot!(context, context.run().env("GIT_CONFIG_GLOBAL", context.child("invalid.gitconfig").path()), @r#"
    success: false
    exit_code: 2
    ----- stdout -----

    ----- stderr -----
    error: Command `[GIT] rev-parse --absolute-git-dir --git-common-dir --git-path hooks --show-toplevel` exited with an error:

    [status]
    exit status: 128

    [stderr]
    fatal: bad config line 1 in file [TEMP_DIR]/invalid.gitconfig
    "#);
    cmd_snapshot!(context, context.run().env(EnvVars::GIT_DIR, "missing"), @r#"
    success: false
    exit_code: 2
    ----- stdout -----

    ----- stderr -----
    error: Command `[GIT] rev-parse --absolute-git-dir --git-common-dir --git-path hooks --show-toplevel` exited with an error:

    [status]
    exit status: 128

    [stderr]
    fatal: not a git repository: 'missing'
    "#);
}

#[test]
fn staged_files_only() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: trailing-whitespace
                name: trailing-whitespace
                language: system
                entry: python3 -c 'print(open("file.txt", "rt").read())'
                verbose: true
                types: [text]
       "#})
        .with_file("file.txt", "Hello, world!")
        .init_git();

    // Non-staged files should be stashed and restored.
    context.write_file("file.txt", "Hello world again!");

    cmd_snapshot!(context, context.run(), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    trailing-whitespace......................................................Passed
    - hook id: trailing-whitespace
    - duration: [TIME]

      Hello, world!

    ----- stderr -----
    Unstaged changes detected. Temporarily saving them to `[HOME]/patches/[TIME]-[PID].patch`
    Restored unstaged changes from `[HOME]/patches/[TIME]-[PID].patch`
    ");

    let content = context.read("file.txt");
    assert_snapshot!(content, @"Hello world again!");
}

#[test]
fn intent_to_add_file_survives_conflicted_stash_restore() -> Result<()> {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: rewrite-python
                name: rewrite-python
                language: system
                entry: python3 -c 'open("test.py", "w").write("a = 1\n")'
                files: ^test\.py$
       "#})
        .init_git();

    context.git().add(PRE_COMMIT_CONFIG_YAML);

    context.write_file("intent.txt", "preserve me\n");
    context
        .git()
        .command()
        .arg("add")
        .arg("--intent-to-add")
        .arg("intent.txt")
        .assert()
        .success();

    context.write_file("test.py", "a=1\n");
    context.git().add("test.py");
    context.write_file("test.py", "a=1\nb = 2\n");

    cmd_snapshot!(context, context.run(), @r#"
    success: false
    exit_code: 1
    ----- stdout -----
    rewrite-python...........................................................Failed
    - hook id: rewrite-python
    - files were modified by this hook

    ----- stderr -----
    Unstaged changes detected. Temporarily saving them to `[HOME]/patches/[TIME]-[PID].patch`
    Hook changes conflicted with the saved unstaged changes. Reverting the hook changes
    Restored unstaged changes from `[HOME]/patches/[TIME]-[PID].patch`
    "#);

    assert_eq!(context.read("intent.txt"), "preserve me\n");
    assert_eq!(context.read("test.py"), "a=1\nb = 2\n");

    let output = context
        .git()
        .command()
        .arg("diff")
        .arg("--diff-filter=A")
        .arg("--name-only")
        .arg("--")
        .arg("intent.txt")
        .output()?;
    assert!(output.status.success(), "{output:?}");
    assert_eq!(String::from_utf8(output.stdout)?, "intent.txt\n");

    Ok(())
}

#[test]
fn restore_intent_and_unstaged_changes_from_subdirectory() {
    let context = TestEnv::new()
        .with_file(
            format!("project/{PRE_COMMIT_CONFIG_YAML}"),
            indoc::indoc! {r#"
            repos:
              - repo: local
                hooks:
                  - id: check-staged
                    name: check staged
                    language: system
                    entry: python3 -c 'assert open("tracked.txt").read() == "staged\n"'
                    files: ^tracked\.txt$
        "#},
        )
        .with_file("project/tracked.txt", "staged\n")
        .with_file("project/nested/.gitkeep", "")
        .init_git();
    context.git().run(["config", "diff.relative", "true"]);
    context.write_file("project/tracked.txt", "unstaged\n");
    context.write_file("project/intent.txt", "intent\n");
    context.write_file("outside.txt", "outside\n");
    context.git().run(["add", "--intent-to-add", "."]);

    cmd_snapshot!(context, context.run().current_dir(context.child("project/nested")), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    check staged.............................................................Passed

    ----- stderr -----
    Unstaged changes detected. Temporarily saving them to `[HOME]/patches/[TIME]-[PID].patch`
    Restored unstaged changes from `[HOME]/patches/[TIME]-[PID].patch`
    "#);

    assert_eq!(context.read("project/tracked.txt"), "unstaged\n");
    assert_eq!(context.read("project/intent.txt"), "intent\n");
    assert_eq!(context.read("outside.txt"), "outside\n");
    cmd_snapshot!(context, context.git().command().args(["diff", "--name-only", "--diff-filter=A"]), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    outside.txt
    project/intent.txt

    ----- stderr -----
    "#);
}

#[cfg(unix)]
#[test]
fn restore_on_interrupt() -> Result<()> {
    // The hook will sleep for 3 seconds.
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: trailing-whitespace
                name: trailing-whitespace
                language: system
                entry: python3 -c 'import time; open("out.txt", "wt").write(open("file.txt", "rt").read()); time.sleep(10)'
                verbose: true
                types: [text]
   "#})
        .with_file("file.txt", "Hello, world!")
        .init_git();

    // Non-staged files should be stashed and restored.
    context.write_file("file.txt", "Hello world again!");

    let mut child = context.run().spawn()?;
    let child_id = child.id();

    // Send an interrupt signal to the process.
    let handle = std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_secs(1));
        #[allow(clippy::cast_possible_wrap)]
        unsafe {
            libc::kill(child_id as i32, libc::SIGINT)
        };
    });

    handle.join().unwrap();
    child.wait()?;

    let content = context.read("out.txt");
    assert_snapshot!(content, @"Hello, world!");

    let content = context.read("file.txt");
    assert_snapshot!(content, @"Hello world again!");

    Ok(())
}

/// When in merge conflict, runs on files that have conflicts fixed.
#[test]
fn merge_conflicts() {
    let context = TestEnv::new()
        .with_file("file.txt", "Hello, world!")
        .init_git();

    // Create a merge conflict.
    context.git().commit("Initial commit");

    context.git().branch("feature").checkout("feature");
    context.write_file("file.txt", "Hello, world again!");
    context.git().add(".").commit("Feature commit");

    context.git().checkout("master");
    context.write_file("file.txt", "Hello, world from master!");
    context.git().add(".").commit("Master commit");

    context
        .git()
        .command()
        .arg("merge")
        .arg("feature")
        .assert()
        .code(1);

    context.write_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: trailing-whitespace
                name: trailing-whitespace
                language: system
                entry: python3 -c 'import sys; print(sorted(sys.argv[1:]))'
                verbose: true
    "});

    // Abort on merge conflicts.
    cmd_snapshot!(context, context.run(), @r#"
    success: false
    exit_code: 2
    ----- stdout -----

    ----- stderr -----
    error: Found unresolved merge conflicts. Resolve the conflicts, stage the files with `git add`, and try again
    "#);

    // Fix the conflict and run again.
    context.git().add(".");
    cmd_snapshot!(context, context.run(), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    trailing-whitespace......................................................Passed
    - hook id: trailing-whitespace
    - duration: [TIME]

      ['.pre-commit-config.yaml', 'file.txt']

    ----- stderr -----
    ");
}

#[test]
fn run_last_commit() {
    // file2 starts with issues but is intentionally absent from the last commit.
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r"
        repos:
          - repo: https://github.com/pre-commit/pre-commit-hooks
            rev: v5.0.0
            hooks:
              - id: trailing-whitespace
              - id: end-of-file-fixer
    "})
        .with_file("file1.txt", "Hello, world!\n")
        .with_file("file2.txt", "Initial content with trailing spaces   \n")
        .init_git();

    context.git().commit("Initial commit");

    // Modify files and make second commit with trailing whitespace
    context.write_file("file1.txt", "Hello, world!   \n"); // trailing whitespace
    context.write_file("file3.txt", "New file"); // missing newline
    // Note: file2.txt is NOT modified in this commit, so it should be filtered out by --last-commit
    context.git().add(".").commit("Second commit with issues");

    // Run with --last-commit should only check files from the last commit
    // This should only process file1.txt and file3.txt, NOT file2.txt
    cmd_snapshot!(context, context.run().arg("--last-commit"), @r"
    success: false
    exit_code: 1
    ----- stdout -----
    trim trailing whitespace.................................................Failed
    - hook id: trailing-whitespace
    - description: trims trailing whitespace
    - exit code: 1
    - files were modified by this hook

      Fixing file1.txt
    fix end of files.........................................................Failed
    - hook id: end-of-file-fixer
    - description: ensures that a file is either empty, or ends with one newline
    - exit code: 1
    - files were modified by this hook

      Fixing file3.txt

    ----- stderr -----
    ");

    // Now reset the files to their problematic state for comparison
    context.write_file("file1.txt", "Hello, world!   \n"); // trailing whitespace
    context.write_file("file3.txt", "New file"); // missing newline

    // Run with --all-files should check ALL files including file2.txt
    // This demonstrates that file2.txt was indeed filtered out in the previous test
    cmd_snapshot!(context, context.run().arg("--all-files"), @r"
    success: false
    exit_code: 1
    ----- stdout -----
    trim trailing whitespace.................................................Failed
    - hook id: trailing-whitespace
    - description: trims trailing whitespace
    - exit code: 1
    - files were modified by this hook

      Fixing file1.txt
      Fixing file2.txt
    fix end of files.........................................................Failed
    - hook id: end-of-file-fixer
    - description: ensures that a file is either empty, or ends with one newline
    - exit code: 1
    - files were modified by this hook

      Fixing file3.txt

    ----- stderr -----
    ");
}

/// Test `git commit -a` works without `.git/index.lock exists` error.
#[test]
fn git_commit_a() {
    let context = TestEnv::new()
        .with_filter("7c8398204bbc95c33a6d2543f86a27621647cf78", "[HASH]")
        .with_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: echo
                name: echo
                language: system
                entry: echo
                verbose: true
    "})
        .with_file("file.txt", "Hello, world!\n")
        .init_git();

    cmd_snapshot!(context, context.install(), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Installed Git hook at `.git/hooks/pre-commit`

    ----- stderr -----
    "#);

    context.git().add(".").commit("Initial commit");

    // Edit the file
    context.write_file("file.txt", "Hello, world again!\n");

    let mut commit = context.git().command();
    commit.arg("commit").arg("-a").arg("-m").arg("Update file");

    cmd_snapshot!(context, commit, @r"
    success: true
    exit_code: 0
    ----- stdout -----
    [master COMMIT] Update file
     1 file changed, 1 insertion(+), 1 deletion(-)

    ----- stderr -----
    echo.....................................................................Passed
    - hook id: echo
    - duration: [TIME]

      file.txt
    ");
}

#[cfg(unix)]
#[test]
fn git_commit_a_currently_fails_when_hook_writes_to_temp_git_index() {
    // Repro for #1786 documenting the current behavior. `git commit -a`
    // exports `GIT_INDEX_FILE=.git/index.lock` to the hook process. If the
    // hook inherits that env var and then runs a git command that writes to an
    // index in a different repository, Git writes those entries into the
    // parent repo's temporary index instead.
    //
    // The important detail is that the temp repo stages `file.txt`, matching a tracked
    // path in the parent repo. `prek` treats the post-hook diff as a best-effort
    // snapshot, so the commit continues until Git tries to build trees from the
    // corrupted temporary index and fails with `invalid object ... for 'file.txt'`.
    let context = TestEnv::new()
        .with_filter(
            r"invalid object 100644 [0-9a-f]{40}",
            "invalid object 100644 [HASH]",
        )
        .with_file(
            "hook.sh",
            indoc::indoc! {r#"
        set -eu
        tmpdir="$(mktemp -d)"
        trap 'rm -rf "$tmpdir"' EXIT
        cd "$tmpdir"
        git init >/dev/null 2>&1
        printf 'hook version\n' > file.txt
        git add file.txt
    "#},
        )
        .with_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: write-temp-index
                name: write-temp-index
                language: system
                entry: sh hook.sh
                pass_filenames: false
                always_run: true
                verbose: true
    "})
        .with_file("file.txt", "Hello, world!\n")
        .init_git();

    cmd_snapshot!(context, context.install(), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Installed Git hook at `.git/hooks/pre-commit`

    ----- stderr -----
    "#);

    context.git().add(".").commit("Initial commit");

    // `git commit` does not set `GIT_INDEX_FILE`; `git commit -a` does.
    // The repro only triggers on the `-a` path.
    context.write_file("file.txt", "Hello again!\n");

    let mut commit = context.git().command();
    commit.arg("commit").arg("-a").arg("-m").arg("Update file");

    cmd_snapshot!(context, commit, @r"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    write-temp-index.........................................................Passed
    - hook id: write-temp-index
    - duration: [TIME]
    error: invalid object 100644 [HASH] for 'file.txt'
    error: Error building trees
    "
    );
}

#[test]
fn run_with_tree_object_as_ref() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: echo-files
                name: echo files
                entry: echo
                language: system
                pass_filenames: true
    "})
        .with_file("file1.txt", "hello")
        .init_git();

    context.git().commit("Initial commit");

    // Create some changes and stage them
    context.write_file("file2.txt", "world");
    context.git().add("file2.txt");

    // Get the tree object from the staged changes
    let tree_output = context
        .git()
        .command()
        .arg("write-tree")
        .output()
        .expect("Failed to run git write-tree");
    let tree_sha = String::from_utf8_lossy(&tree_output.stdout)
        .trim()
        .to_string();

    // Run prek with tree object as to-ref (should work with .. syntax)
    cmd_snapshot!(context, context.run()
        .arg("--from-ref").arg("HEAD")
        .arg("--to-ref").arg(&tree_sha), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    echo files...............................................................Passed

    ----- stderr -----
    ");
}