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
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
use std::ffi::{OsStr, OsString};
use std::future::Future;
use std::path::Path;
use std::process::Stdio;
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{Context, Result};
use prek_consts::env_vars::{EnvVars, EnvVarsRead};
use prek_identify::parse_shebang;
use tracing::{instrument, trace};

use crate::cli::reporter::HookInstallReporter;
use crate::cli::run::HookRunReporter;
use crate::config::Language;
use crate::fs::PathClean;
use crate::git::GitCommandExt;
use crate::hook::{Hook, InstallInfo, InstalledHook, Repo};
use crate::hook_entry::PreparedHookEntry;
use crate::hooks::{self, HookOutput};
use crate::process::{Cmd, with_command_env};
use crate::run::run_by_batch;
use crate::store::{CacheBucket, Store, ToolBucket};

mod bun;
mod conda;
mod coursier;
mod dart;
mod deno;
mod docker;
mod docker_image;
mod dotnet;
mod fail;
mod golang;
mod haskell;
mod julia;
mod lua;
mod mise;
mod node;
mod perl;
mod php;
mod pygrep;
mod python;
mod r;
mod ruby;
mod rust;
mod script;
mod swift;
mod system;
pub(crate) mod version;

// Backend futures are awaited in place rather than spawned. Requiring `Send` here would impose a
// stronger contract than callers need and rejects the borrowed async closures used by backends.
#[async_trait::async_trait(?Send)]
trait LanguageBackend: Sync {
    /// Provisions the environment required by `hook` and returns the prepared hook.
    ///
    /// `install_cwd` is the cloned repository for remote hooks and an empty temporary directory
    /// for local hooks. Installer arguments that depend on the process working directory should
    /// resolve from it rather than the user's work tree.
    async fn install(
        &self,
        store: &Store,
        hook: Arc<Hook>,
        install_cwd: &Path,
        reporter: &HookInstallReporter,
    ) -> Result<InstalledHook>;

    /// Checks whether the installed environment described by `info` can be reused.
    async fn check_health(&self, info: &InstallInfo) -> Result<()>;

    /// Builds the language-specific base environment for hook commands.
    ///
    /// The default leaves the caller's environment unchanged.
    fn execution_environment(
        &self,
        _store: &Store,
        _hook: &InstalledHook,
    ) -> Result<ExecutionEnvironment> {
        Ok(ExecutionEnvironment::default())
    }

    /// Resolves the configured entry after the execution environment is prepared.
    ///
    /// This is used for normal hook runs; `prek exec` executes its supplied command directly.
    fn prepare_hook_entry(
        &self,
        store: &Store,
        hook: &InstalledHook,
        environment: &ExecutionEnvironment,
    ) -> Result<PreparedHookEntry> {
        let repo_path = hook.repo_path().unwrap_or(hook.work_dir());
        Ok(hook
            .entry
            .resolve(repo_path, environment.path(hook), hook.work_dir(), store)?)
    }

    /// Applies asynchronous or working-directory-dependent changes to `environment`.
    ///
    /// This is called after [`Self::execution_environment`] and before command resolution. `cwd`
    /// is the hook work directory for a normal run and the caller's working directory for
    /// `prek exec`.
    async fn prepare_execution_environment(
        &self,
        _hook: &InstalledHook,
        _cwd: &Path,
        _environment: &mut ExecutionEnvironment,
    ) -> Result<()> {
        Ok(())
    }

    /// Runs the hook for `filenames`, reports progress, and returns its exit code and output.
    ///
    /// The default prepares the environment, resolves the entry, and executes filename batches
    /// from the hook work directory.
    async fn run(
        &self,
        store: &Store,
        hook: &InstalledHook,
        filenames: &[&Path],
        reporter: &HookRunReporter,
    ) -> Result<(i32, Vec<u8>)> {
        let progress = reporter.on_run_start(hook, filenames.len());

        let mut environment = self.execution_environment(store, hook)?;
        self.prepare_execution_environment(hook, hook.work_dir(), &mut environment)
            .await?;
        let entry = self.prepare_hook_entry(store, hook, &environment)?;
        let run = async |batch: &[&Path]| {
            let output = environment
                .command(hook, hook.work_dir(), &entry)?
                .args(&hook.args)
                .file_args(batch)
                .stdin(Stdio::null())
                .pty_output_with_sink(reporter.output_sink(progress))
                .await?;

            reporter.on_run_progress(progress, batch.len() as u64);

            anyhow::Ok(output)
        };

        let output = run_by_batch(hook, filenames, &entry, run).await?;

        reporter.on_run_complete(progress);

        Ok(output)
    }
}

/// Process environment shared by normal hook runs and `prek exec`.
///
/// Language defaults are applied before the hook's configured `env`.
#[derive(Debug, Default)]
pub(crate) struct ExecutionEnvironment {
    path: Option<OsString>,
    patch: EnvironmentPatch,
}

#[derive(Debug, Default)]
struct EnvironmentPatch {
    set: Vec<(OsString, OsString)>,
    remove: Vec<OsString>,
}

impl EnvironmentPatch {
    fn set(&mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) {
        self.set
            .push((key.as_ref().to_os_string(), value.as_ref().to_os_string()));
    }

    fn remove(&mut self, key: impl AsRef<OsStr>) {
        self.remove.push(key.as_ref().to_os_string());
    }

    fn apply(&self, cmd: &mut Cmd) {
        for key in &self.remove {
            cmd.env_remove(key);
        }
        cmd.envs(self.set.iter().map(|(key, value)| (key, value)));
    }
}

impl ExecutionEnvironment {
    pub(crate) fn new() -> Self {
        Self::default()
    }

    pub(crate) fn set_path(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
        let path = path.as_ref().to_os_string();
        self.patch.set(EnvVars::PATH, &path);
        self.path = Some(path);
        self
    }

    pub(crate) fn env(&mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> &mut Self {
        self.patch.set(key, value);
        self
    }

    pub(crate) fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<OsStr>,
        V: AsRef<OsStr>,
    {
        for (key, value) in vars {
            self.env(key, value);
        }
        self
    }

    pub(crate) fn env_remove(&mut self, key: impl AsRef<OsStr>) -> &mut Self {
        self.patch.remove(key);
        self
    }

    pub(crate) fn command(
        &self,
        hook: &InstalledHook,
        cwd: &Path,
        command: &[OsString],
    ) -> Result<Cmd> {
        let command = resolve_command(command.to_vec(), self.path(hook), cwd);
        let (program, args) = command.split_first().context("Command cannot be empty")?;
        let mut cmd = Cmd::new(program);
        cmd.current_dir(cwd)
            .preserve_current_worktree(cwd)
            .args(args)
            .check(false);
        self.patch.apply(&mut cmd);
        cmd.envs(&hook.env);
        Ok(cmd)
    }

    /// Effective PATH after applying the hook's configured environment.
    pub(crate) fn path<'a>(&'a self, hook: &'a InstalledHook) -> Option<&'a OsStr> {
        hook.env
            .iter()
            .find_map(|(key, value)| {
                if is_path_env(key) {
                    Some(OsStr::new(value))
                } else {
                    None
                }
            })
            .or(self.language_path())
    }

    /// PATH supplied by the language backend before hook-specific overrides.
    fn language_path(&self) -> Option<&OsStr> {
        self.path.as_deref()
    }
}

fn is_path_env(key: impl AsRef<OsStr>) -> bool {
    let key = key.as_ref();
    #[cfg(windows)]
    {
        key.to_string_lossy().eq_ignore_ascii_case(EnvVars::PATH)
    }
    #[cfg(not(windows))]
    {
        key == OsStr::new(EnvVars::PATH)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ShellSupport {
    Supported,
    Unsupported(&'static str),
}

// `pre-commit` language support:
// bun: install requested version, support env, support additional deps
// conda: only system version, support env, support additional deps
// coursier: only system version, support env, support additional deps
// dart: only system version, support env, support additional deps
// docker_image: only system version, no env, no additional deps
// docker: only system version, support env, no additional deps
// dotnet: install requested version, support env, support additional deps
// fail: only system version, no env, no additional deps
// golang: install requested version, support env, support additional deps
// haskell: only system version, support env, support additional deps
// lua: only system version, support env, support additional deps
// mise: install requested version, support env, support additional deps
// node: install requested version, support env, support additional deps (delegated to nodeenv)
// perl: only system version, support env, support additional deps
// php: only system version, support env, support additional deps
// pygrep: only system version, no env, no additional deps
// python: install requested version, support env, support additional deps (delegated to virtualenv)
// r: only system version, support env, support additional deps
// ruby: install requested version, support env, support additional deps (delegated to rbenv)
// rust: install requested version, support env, support additional deps (delegated to rustup and cargo)
// script: only system version, no env, no additional deps
// swift: only system version, support env, no additional deps
// system: only system version, no env, no additional deps

impl Language {
    fn backend(self) -> &'static dyn LanguageBackend {
        match self {
            Self::Bun => &bun::Bun,
            Self::Conda => &conda::Conda,
            Self::Coursier => &coursier::Coursier,
            Self::Dart => &dart::Dart,
            Self::Deno => &deno::Deno,
            Self::Docker => &docker::Docker,
            Self::DockerImage => &docker_image::DockerImage,
            Self::Dotnet => &dotnet::Dotnet,
            Self::Fail => &fail::Fail,
            Self::Golang => &golang::Golang,
            Self::Haskell => &haskell::Haskell,
            Self::Julia => &julia::Julia,
            Self::Lua => &lua::Lua,
            Self::Mise => &mise::Mise,
            Self::Node => &node::Node,
            Self::Perl => &perl::Perl,
            Self::Php => &php::Php,
            Self::Pygrep => &pygrep::Pygrep,
            Self::Python => &python::Python,
            Self::R => &r::R,
            Self::Ruby => &ruby::Ruby,
            Self::Rust => &rust::Rust,
            Self::Script => &script::Script,
            Self::Swift => &swift::Swift,
            Self::System => &system::System,
        }
    }

    pub(crate) fn can_modify_files(self) -> bool {
        !matches!(self, Self::Fail | Self::Pygrep)
    }

    pub(crate) fn supports_install_env(self) -> bool {
        match self {
            Self::Bun
            | Self::Conda
            | Self::Coursier
            | Self::Dart
            | Self::Deno
            | Self::Docker
            | Self::Dotnet
            | Self::Golang
            | Self::Haskell
            | Self::Julia
            | Self::Lua
            | Self::Mise
            | Self::Node
            | Self::Perl
            | Self::Php
            | Self::Pygrep
            | Self::Python
            | Self::R
            | Self::Ruby
            | Self::Rust
            | Self::Swift => true,
            Self::DockerImage | Self::Fail | Self::Script | Self::System => false,
        }
    }

    pub(crate) fn shell_support(self) -> ShellSupport {
        match self {
            Self::Bun
            | Self::Conda
            | Self::Coursier
            | Self::Deno
            | Self::Dotnet
            | Self::Golang
            | Self::Haskell
            | Self::Lua
            | Self::Mise
            | Self::Node
            | Self::Perl
            | Self::Php
            | Self::Python
            | Self::Ruby
            | Self::Script
            | Self::Swift
            | Self::System => ShellSupport::Supported,
            Self::R => ShellSupport::Unsupported(
                "`entry` must use the R backend's `Rscript -e <expr>` or `Rscript <file>` forms",
            ),
            Self::Dart => ShellSupport::Unsupported(
                "`--packages` injection requires the resolved argv to contain `dart` directly",
            ),
            Self::Docker | Self::DockerImage => ShellSupport::Unsupported(
                "`entry` participates in container image or entrypoint selection",
            ),
            Self::Fail => ShellSupport::Unsupported("`entry` is the failure message body"),
            Self::Julia | Self::Rust => ShellSupport::Unsupported(
                "`entry` participates in install/runtime package resolution and is split before execution",
            ),
            Self::Pygrep => ShellSupport::Unsupported("`entry` is the regex pattern"),
        }
    }

    pub(crate) fn tool_buckets(self) -> &'static [ToolBucket] {
        match self {
            Self::Bun => &[ToolBucket::Bun],
            Self::Deno => &[ToolBucket::Deno],
            Self::Dotnet => &[ToolBucket::Dotnet],
            Self::Golang => &[ToolBucket::Go],
            Self::Mise => &[ToolBucket::Mise],
            Self::Node => &[ToolBucket::Node],
            Self::Python | Self::Pygrep => &[ToolBucket::Uv, ToolBucket::Python],
            Self::Ruby => &[ToolBucket::Ruby],
            Self::Rust => &[ToolBucket::Rustup],
            Self::Conda
            | Self::Coursier
            | Self::Dart
            | Self::Docker
            | Self::DockerImage
            | Self::Fail
            | Self::Haskell
            | Self::Julia
            | Self::Lua
            | Self::Perl
            | Self::Php
            | Self::R
            | Self::Script
            | Self::Swift
            | Self::System => &[],
        }
    }

    pub(crate) fn cache_buckets(self) -> &'static [CacheBucket] {
        match self {
            Self::Coursier => &[CacheBucket::Coursier],
            Self::Deno => &[CacheBucket::Deno],
            Self::Golang => &[CacheBucket::Go],
            Self::Node => &[CacheBucket::Npm],
            Self::Python | Self::Pygrep => &[CacheBucket::Uv, CacheBucket::Python],
            Self::Rust => &[CacheBucket::Cargo],
            Self::Bun
            | Self::Conda
            | Self::Dart
            | Self::Docker
            | Self::DockerImage
            | Self::Dotnet
            | Self::Fail
            | Self::Haskell
            | Self::Julia
            | Self::Lua
            | Self::Mise
            | Self::Perl
            | Self::Php
            | Self::R
            | Self::Ruby
            | Self::Script
            | Self::Swift
            | Self::System => &[],
        }
    }

    /// Return whether the language allows specifying the version, e.g. we can install a specific
    /// requested language version.
    /// See <https://pre-commit.com/#overriding-language-version>
    pub(crate) fn supports_language_version(self) -> bool {
        match self {
            Self::Bun
            | Self::Deno
            | Self::Dotnet
            | Self::Golang
            | Self::Mise
            | Self::Node
            | Self::Python
            | Self::Ruby
            | Self::Rust => true,
            Self::Conda
            | Self::Coursier
            | Self::Dart
            | Self::Docker
            | Self::DockerImage
            | Self::Fail
            | Self::Haskell
            | Self::Julia
            | Self::Lua
            | Self::Perl
            | Self::Php
            | Self::Pygrep
            | Self::R
            | Self::Script
            | Self::Swift
            | Self::System => false,
        }
    }

    /// Whether the language supports installing dependencies.
    ///
    /// For example, Python and Node.js support installing dependencies, while
    /// System and Fail do not.
    pub(crate) fn supports_dependency(self) -> bool {
        match self {
            Self::Bun
            | Self::Conda
            | Self::Coursier
            | Self::Dart
            | Self::Deno
            | Self::Dotnet
            | Self::Golang
            | Self::Haskell
            | Self::Julia
            | Self::Lua
            | Self::Mise
            | Self::Node
            | Self::Perl
            | Self::Php
            | Self::Python
            | Self::R
            | Self::Ruby
            | Self::Rust => true,
            Self::Docker
            | Self::DockerImage
            | Self::Fail
            | Self::Pygrep
            | Self::Script
            | Self::Swift
            | Self::System => false,
        }
    }

    pub(crate) fn ensure_exec_supported(self, hook: &Hook) -> Result<()> {
        match hook.repo() {
            Repo::Meta => anyhow::bail!("`prek exec` does not support meta hooks"),
            Repo::Builtin => anyhow::bail!("`prek exec` does not support builtin hooks"),
            Repo::Remote { .. } | Repo::Local => {}
        }

        match self {
            Self::Bun
            | Self::Conda
            | Self::Coursier
            | Self::Dart
            | Self::Deno
            | Self::Dotnet
            | Self::Golang
            | Self::Haskell
            | Self::Lua
            | Self::Mise
            | Self::Node
            | Self::Perl
            | Self::Php
            | Self::Python
            | Self::R
            | Self::Ruby
            | Self::Rust
            | Self::Script
            | Self::Swift
            | Self::System => Ok(()),
            Self::Docker | Self::DockerImage | Self::Fail | Self::Julia | Self::Pygrep => {
                anyhow::bail!("`prek exec` does not support hooks with language `{self}`")
            }
        }
    }

    pub(crate) fn install<'a>(
        &'a self,
        store: &'a Store,
        hook: Arc<Hook>,
        install_cwd: &'a Path,
        reporter: &'a HookInstallReporter,
    ) -> impl Future<Output = Result<InstalledHook>> + 'a {
        let env = hook
            .env
            .iter()
            .map(|(key, value)| (key.into(), value.into()))
            .collect();
        let install = self.backend().install(store, hook, install_cwd, reporter);
        with_command_env(env, install)
    }

    pub(crate) fn check_health<'a>(
        &'a self,
        info: &'a InstallInfo,
    ) -> impl Future<Output = Result<()>> + 'a {
        self.backend().check_health(info)
    }

    #[instrument(skip_all, fields(hook_id=%hook.id, language=%hook.language))]
    pub(crate) async fn run<'a, 'p>(
        &'a self,
        store: &'a Store,
        hook: &'a InstalledHook,
        filenames: &'a [&'p Path],
        reporter: &'a HookRunReporter,
    ) -> Result<HookOutput>
    where
        'p: 'a,
    {
        match hook.repo() {
            Repo::Meta => {
                hooks::MetaHooks::from_str(&hook.id)
                    .unwrap()
                    .run(store, hook, filenames, reporter)
                    .await
            }
            Repo::Builtin => {
                hooks::BuiltinHooks::from_str(&hook.id)
                    .unwrap()
                    .run(store, hook, filenames, reporter)
                    .await
            }
            // Fast path for hooks implemented in Rust
            Repo::Remote { .. } if hooks::check_fast_path(hook) => {
                hooks::run_fast_path(store, hook, filenames, reporter).await
            }
            Repo::Remote { .. } | Repo::Local => {
                let (exit_status, output) =
                    self.backend().run(store, hook, filenames, reporter).await?;
                if self.can_modify_files() {
                    Ok(HookOutput::unknown(exit_status, output))
                } else {
                    Ok(HookOutput::unchanged(exit_status, output))
                }
            }
        }
    }

    #[instrument(skip_all, fields(hook_id=%hook.id, language=%hook.language))]
    pub(crate) async fn exec(
        &self,
        store: &Store,
        hook: &InstalledHook,
        cwd: &Path,
        command: &[OsString],
    ) -> Result<std::process::ExitStatus> {
        self.ensure_exec_supported(hook)?;

        let mut environment = self.backend().execution_environment(store, hook)?;
        self.backend()
            .prepare_execution_environment(hook, cwd, &mut environment)
            .await?;
        environment
            .command(hook, cwd, command)?
            .status()
            .await
            .map_err(Into::into)
    }
}

/// Try to extract metadata from the given hook.
pub(crate) async fn extract_metadata(hook: &mut Hook) -> Result<()> {
    match hook.language {
        Language::Python => python::extract_metadata(hook).await,
        Language::Golang => golang::extract_go_mod_metadata(hook).await,
        Language::Bun
        | Language::Conda
        | Language::Coursier
        | Language::Dart
        | Language::Deno
        | Language::Docker
        | Language::DockerImage
        | Language::Dotnet
        | Language::Fail
        | Language::Haskell
        | Language::Julia
        | Language::Lua
        | Language::Mise
        | Language::Node
        | Language::Perl
        | Language::Php
        | Language::Pygrep
        | Language::R
        | Language::Ruby
        | Language::Rust
        | Language::Script
        | Language::Swift
        | Language::System => Ok(()),
    }
}

/// Resolve the actual process invocation, honoring shebangs and PATH lookups.
pub(crate) fn resolve_command(
    mut cmds: Vec<OsString>,
    paths: Option<&OsStr>,
    cwd: &Path,
) -> Vec<OsString> {
    let Some(candidate) = cmds.first() else {
        return cmds;
    };

    let env_path = if paths.is_none() {
        EnvVars.var_os(EnvVars::PATH)
    } else {
        None
    };
    let paths = paths.or(env_path.as_deref());

    let resolved_binary = which::which_in(candidate, paths, cwd).unwrap_or_else(|_| {
        let candidate = Path::new(candidate);
        let has_parent = candidate
            .parent()
            .is_some_and(|parent| !parent.as_os_str().is_empty());
        // `which_in` only returns executable files. Resolve an explicit relative
        // path against `cwd` so its shebang can still be inspected.
        if candidate.is_absolute() || has_parent {
            cwd.join(candidate).clean()
        } else {
            candidate.into()
        }
    });

    let Ok(shebang_argv) = parse_shebang(&resolved_binary) else {
        cmds[0] = resolved_binary.into_os_string();
        return cmds;
    };

    let mut shebang_argv = shebang_argv
        .into_iter()
        .map(OsString::from)
        .collect::<Vec<_>>();
    trace!("Found shebang: {:?}", shebang_argv);
    let interpreter = shebang_argv[0].as_os_str();
    #[cfg(windows)]
    let interpreter = {
        let interpreter_path = Path::new(interpreter);
        if !interpreter_path.exists()
            && (interpreter_path.has_root() || interpreter_path.components().count() > 1)
        {
            // Git for Windows behavior: if a shebang points to a Unix-style absolute
            // interpreter path (e.g. `/bin/sh`) that does not exist on Windows,
            // fall back to PATH lookup of its basename (`sh`).
            interpreter_path.file_name().unwrap_or(interpreter)
        } else {
            interpreter
        }
    };

    // Resolve the interpreter path, converting "python3" to "python3.exe" on Windows.
    if let Ok(path) = which::which_in(interpreter, paths, cwd) {
        shebang_argv[0] = path.into_os_string();
    }
    shebang_argv.push(resolved_binary.into_os_string());
    shebang_argv.extend(cmds.drain(1..));
    shebang_argv
}

#[cfg(test)]
mod tests {
    #[cfg(unix)]
    use std::ffi::OsStr;
    use std::ffi::OsString;
    #[cfg(unix)]
    use std::os::unix::ffi::OsStringExt;
    use std::path::Path;

    use tempfile::tempdir;

    use super::resolve_command;
    use crate::fs::{CWD, PathClean, make_executable};

    fn write_file(path: &Path, contents: &str) {
        fs_err::write(path, contents).expect("write test file");
    }

    #[test]
    fn resolve_command_passthrough_when_not_found() {
        let cmd = OsString::from("__prek_nonexistent_command__");
        let resolved = resolve_command(vec![cmd.clone()], None, &CWD);
        assert_eq!(resolved, vec![cmd]);
    }

    #[test]
    fn resolve_command_resolves_missing_relative_path_from_cwd() {
        let cmd = OsString::from("./__prek_nonexistent_command__");
        let paths = OsString::new();
        let resolved = resolve_command(vec![cmd.clone()], Some(paths.as_os_str()), &CWD);
        assert_eq!(resolved, vec![CWD.join(cmd).clean().into_os_string()]);
    }

    #[test]
    fn resolve_command_passthrough_when_empty() {
        assert_eq!(
            resolve_command(Vec::new(), None, &CWD),
            Vec::<OsString>::new()
        );
    }

    #[cfg(unix)]
    #[test]
    fn resolve_command_preserves_non_utf8_arguments() {
        let cmd = OsString::from("__prek_nonexistent_command__");
        let arg = OsString::from_vec(vec![b'f', b'o', 0x80]);

        assert_eq!(
            resolve_command(vec![cmd.clone(), arg.clone()], Some(OsStr::new("")), &CWD,),
            vec![cmd, arg]
        );
    }

    #[test]
    fn resolve_command_resolves_shebang_and_preserves_arguments() {
        let dir = tempdir().expect("create temp dir");
        let script_path = dir.path().join("hook-script");
        write_file(
            &script_path,
            "#!/usr/bin/env -S prek-test-interpreter --from-shebang\necho hi\n",
        );

        #[cfg(windows)]
        let interpreter_path = dir.path().join("prek-test-interpreter.exe");
        #[cfg(not(windows))]
        let interpreter_path = dir.path().join("prek-test-interpreter");

        write_file(&interpreter_path, "");
        make_executable(&interpreter_path).expect("set executable bit");

        let paths = OsString::from(dir.path().as_os_str());
        let script = script_path.into_os_string();
        let resolved = resolve_command(
            vec![script.clone(), OsString::from("--from-entry")],
            Some(paths.as_os_str()),
            &CWD,
        );

        assert_eq!(
            resolved,
            vec![
                interpreter_path.into_os_string(),
                OsString::from("--from-shebang"),
                script,
                OsString::from("--from-entry"),
            ]
        );
    }

    #[test]
    fn resolve_command_resolves_relative_shebang_from_cwd() {
        let dir = tempdir().expect("create temp dir");
        let script_path = dir.path().join("hook-script");
        write_file(
            &script_path,
            "#!/usr/bin/env -S prek-test-interpreter --from-shebang\necho hi\n",
        );

        #[cfg(windows)]
        let interpreter_path = dir.path().join("prek-test-interpreter.exe");
        #[cfg(not(windows))]
        let interpreter_path = dir.path().join("prek-test-interpreter");

        write_file(&interpreter_path, "");
        make_executable(&interpreter_path).expect("set executable bit");

        let paths = OsString::from(dir.path().as_os_str());
        let resolved = resolve_command(
            vec![OsString::from("./hook-script")],
            Some(paths.as_os_str()),
            dir.path(),
        );

        assert_eq!(
            resolved,
            vec![
                interpreter_path.into_os_string(),
                OsString::from("--from-shebang"),
                script_path.into_os_string(),
            ]
        );
    }

    #[cfg(windows)]
    #[test]
    fn resolve_command_windows_rewrites_bin_sh_to_path_sh() {
        let dir = tempdir().expect("create temp dir");
        let script_path = dir.path().join("legacy-hook");
        write_file(&script_path, "#!/bin/sh\necho legacy\n");

        let sh_path = dir.path().join("sh.exe");
        write_file(&sh_path, "");

        let paths = OsString::from(dir.path().as_os_str());
        let resolved = resolve_command(
            vec![script_path.as_os_str().to_owned()],
            Some(paths.as_os_str()),
            &CWD,
        );

        assert_eq!(resolved[0].as_os_str(), sh_path.as_os_str());
        assert_eq!(resolved[1].as_os_str(), script_path.as_os_str());
    }

    #[cfg(windows)]
    #[test]
    fn resolve_command_windows_keeps_existing_absolute_interpreter_path() {
        let dir = tempdir().expect("create temp dir");

        let interp_dir = dir.path().join("bin");
        fs_err::create_dir_all(&interp_dir).expect("create interpreter dir");
        let interp_path = interp_dir.join("sh.exe");
        write_file(&interp_path, "");
        let shebang_interpreter = interp_path.to_string_lossy().replace('\\', "/");

        let script_path = dir.path().join("legacy-hook");
        write_file(
            &script_path,
            &format!("#!{shebang_interpreter}\necho legacy\n"),
        );

        let paths = OsString::from(dir.path().as_os_str());
        let resolved = resolve_command(
            vec![script_path.as_os_str().to_owned()],
            Some(paths.as_os_str()),
            &CWD,
        );

        let resolved_interp = Path::new(&resolved[0]);
        assert_eq!(resolved_interp, interp_path.as_path());
        assert_eq!(resolved[1].as_os_str(), script_path.as_os_str());
    }
}