prek 0.3.11

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
use std::hash::{DefaultHasher, Hash, Hasher};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::Result;
use etcetera::BaseStrategy;
use futures::StreamExt;
use prek_consts::env_vars::EnvVars;
use rustc_hash::{FxHashMap, FxHashSet};
use seahash::SeaHasher;
use thiserror::Error;
use tracing::{debug, warn};

use crate::config::{RemoteRepo, RemoteRepoKey};
use crate::fs::LockedFile;
use crate::git::{self, TerminalPrompt};
use crate::hook::InstallInfo;
use crate::run::CONCURRENCY;
use crate::warn_user;
use crate::workspace::{HookInitReporter, WorkspaceCache};

struct PendingClone<'a> {
    repo: &'a RemoteRepo,
}

enum FirstClonePass<'a> {
    Ready {
        repo: &'a RemoteRepo,
        temp: tempfile::TempDir,
        progress: Option<usize>,
    },
    AuthFailed {
        repo: &'a RemoteRepo,
        error: git::Error,
        progress: Option<usize>,
    },
}

#[derive(Debug, Error)]
pub enum Error {
    #[error("Home directory not found")]
    HomeNotFound,
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error("Failed to clone repo `{repo}`")]
    CloneRepo {
        repo: String,
        #[source]
        error: git::Error,
    },
    #[error(transparent)]
    Serde(#[from] serde_json::Error),
}

/// Expand a path starting with `~` to the user's home directory.
fn expand_tilde(path: PathBuf) -> PathBuf {
    if let Ok(stripped) = path.strip_prefix("~") {
        if let Some(home) = std::env::home_dir() {
            return home.join(stripped);
        }
    }
    path
}

pub(crate) const REPO_MARKER: &str = ".prek-repo.json";

/// A store for managing repos.
#[derive(Debug)]
pub struct Store {
    path: PathBuf,
}

impl Store {
    pub(crate) fn from_path(path: impl Into<PathBuf>) -> Self {
        Self { path: path.into() }
    }

    /// Create a store from environment variables or default paths.
    pub(crate) fn from_settings() -> Result<Self, Error> {
        let path = if let Some(path) = EnvVars::var_os(EnvVars::PREK_HOME) {
            Some(expand_tilde(PathBuf::from(path)))
        } else {
            etcetera::choose_base_strategy()
                .map(|path| path.cache_dir().join("prek"))
                .ok()
        };

        let Some(path) = path else {
            return Err(Error::HomeNotFound);
        };
        let store = Store::from_path(path).init()?;

        Ok(store)
    }

    pub(crate) fn path(&self) -> &Path {
        self.path.as_ref()
    }

    /// Initialize the store.
    pub(crate) fn init(self) -> Result<Self, Error> {
        fs_err::create_dir_all(&self.path)?;
        fs_err::create_dir_all(self.repos_dir())?;
        fs_err::create_dir_all(self.hooks_dir())?;
        fs_err::create_dir_all(self.scratch_path())?;

        match fs_err::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(self.path.join("README")) {
            Ok(mut f) => f.write_all(b"This directory is maintained by the prek project.\nLearn more: https://github.com/j178/prek\n")?,
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => (),
            Err(err) => return Err(err.into()),
        }
        Ok(self)
    }

    async fn clone_repo_to_temp(
        &self,
        repo: &RemoteRepo,
        terminal_prompt: TerminalPrompt,
    ) -> Result<tempfile::TempDir, git::Error> {
        let temp = tempfile::tempdir_in(self.scratch_path())?;
        debug!(
            target = %temp.path().display(),
            %repo,
            ?terminal_prompt,
            "Cloning repo"
        );
        git::clone_repo(&repo.repo, &repo.rev, temp.path(), terminal_prompt).await?;
        Ok(temp)
    }

    async fn persist_cloned_repo(
        &self,
        repo: &RemoteRepo,
        temp: tempfile::TempDir,
    ) -> Result<PathBuf, Error> {
        let target = self.repo_path(repo);

        // TODO: add windows retry
        fs_err::tokio::remove_dir_all(&target).await.ok();
        fs_err::tokio::rename(temp, &target).await?;

        let content = serde_json::to_string_pretty(&repo)?;
        fs_err::tokio::write(target.join(REPO_MARKER), content).await?;

        Ok(target)
    }

    /// Clone remote repositories into the store.
    ///
    /// The first pass runs in parallel with terminal prompts disabled. Repositories that fail
    /// with an authentication error are retried afterwards, sequentially, with terminal prompts
    /// enabled so the user can provide credentials for one repository at a time.
    pub(crate) async fn clone_repos<'a>(
        &self,
        repos: impl IntoIterator<Item = &'a RemoteRepo>,
        reporter: Option<&dyn HookInitReporter>,
    ) -> Result<FxHashMap<RemoteRepoKey<'a>, PathBuf>, Error> {
        let mut cloned = FxHashMap::default();
        let mut pending = Vec::new();

        for repo in repos {
            let target = self.repo_path(repo);
            if target.join(REPO_MARKER).try_exists()? {
                cloned.insert(repo.key(), target);
                continue;
            }

            pending.push(PendingClone { repo });
        }

        let mut auth_failed = Vec::new();
        let mut tasks = futures::stream::iter(pending)
            .map(async |pending| {
                let progress =
                    reporter.map(|reporter| reporter.on_clone_start(&format!("{}", pending.repo)));
                match self
                    .clone_repo_to_temp(pending.repo, TerminalPrompt::Disabled)
                    .await
                {
                    Ok(temp) => Ok(FirstClonePass::Ready {
                        repo: pending.repo,
                        temp,
                        progress,
                    }),
                    Err(err) if git::is_auth_error(&err) => {
                        warn!(
                            repo = %pending.repo.repo,
                            ?err,
                            "Clone failed with authentication error and terminal prompts disabled"
                        );
                        Ok(FirstClonePass::AuthFailed {
                            repo: pending.repo,
                            error: err,
                            progress,
                        })
                    }
                    Err(err) => Err(Error::CloneRepo {
                        repo: pending.repo.repo.clone(),
                        error: err,
                    }),
                }
            })
            .buffer_unordered(*CONCURRENCY);

        while let Some(result) = tasks.next().await {
            match result? {
                FirstClonePass::Ready {
                    repo,
                    temp,
                    progress,
                } => {
                    let path = self.persist_cloned_repo(repo, temp).await?;
                    if let (Some(reporter), Some(progress)) = (reporter, progress) {
                        reporter.on_clone_complete(progress);
                    }
                    cloned.insert(repo.key(), path);
                }
                FirstClonePass::AuthFailed {
                    repo,
                    error,
                    progress,
                } => {
                    if let (Some(reporter), Some(progress)) = (reporter, progress) {
                        reporter.on_clone_complete(progress);
                    }
                    auth_failed.push((repo, error));
                }
            }
        }

        if EnvVars::is_under_ci() {
            // CI cannot answer interactive credential prompts, so surface the original auth
            // failure instead of attempting the prompt-enabled retry path.
            if let Some((repo, error)) = auth_failed.into_iter().next() {
                return Err(Error::CloneRepo {
                    repo: repo.repo.clone(),
                    error,
                });
            }

            return Ok(cloned);
        }

        if !auth_failed.is_empty() {
            // Tear down the shared MultiProgress before warning/prompt output so progress redraws
            // do not overwrite terminal messages or git credential prompts.
            reporter.map(HookInitReporter::on_complete);
        }

        for (repo, _error) in auth_failed {
            warn_user!(
                "Authentication may be required to clone repository `{}`. Retrying with terminal prompts enabled.",
                repo.repo
            );
            let temp = self
                .clone_repo_to_temp(repo, TerminalPrompt::Enabled)
                .await
                .map_err(|error| Error::CloneRepo {
                    repo: repo.repo.clone(),
                    error,
                })?;
            let path = self.persist_cloned_repo(repo, temp).await?;
            cloned.insert(repo.key(), path);
        }

        Ok(cloned)
    }

    /// Clone a single remote repository into the store.
    pub(crate) async fn clone_repo(
        &self,
        repo: &RemoteRepo,
        reporter: Option<&dyn HookInitReporter>,
    ) -> Result<PathBuf, Error> {
        let repo_key = repo.key();
        let cloned = self.clone_repos(std::iter::once(repo), reporter).await?;
        cloned
            .get(&repo_key)
            .cloned()
            .ok_or_else(|| Error::CloneRepo {
                repo: repo.repo.clone(),
                error: git::Error::Io(std::io::Error::other("repo was not cloned")),
            })
    }

    /// Returns installed hooks in the store.
    pub(crate) async fn installed_hooks(&self) -> Vec<Arc<InstallInfo>> {
        let Ok(dirs) = fs_err::read_dir(self.hooks_dir()) else {
            return vec![];
        };

        let mut tasks = futures::stream::iter(dirs)
            .map(async |entry| {
                let path = match entry {
                    Ok(entry) => entry.path(),
                    Err(err) => {
                        warn!(%err, "Failed to read hook dir");
                        return None;
                    }
                };
                let info = match InstallInfo::from_env_path(&path).await {
                    Ok(info) => info,
                    Err(err) => {
                        warn!(%err, path = %path.display(), "Skipping invalid installed hook");
                        return None;
                    }
                };
                Some(info)
            })
            .buffer_unordered(*CONCURRENCY);

        let mut hooks = Vec::new();
        while let Some(hook) = tasks.next().await {
            if let Some(hook) = hook {
                hooks.push(Arc::new(hook));
            }
        }

        hooks
    }

    pub(crate) async fn lock_async(&self) -> Result<LockedFile, std::io::Error> {
        LockedFile::acquire(self.path.join(".lock"), "store").await
    }

    /// Returns the path to where a remote repo would be stored.
    pub(crate) fn repo_path(&self, repo: &RemoteRepo) -> PathBuf {
        // TODO: remove legacy path support in next breaking release
        if let Some(legacy_path) = self.legacy_repo_path(repo) {
            return legacy_path;
        }

        self.repos_dir().join(Self::repo_key(repo))
    }

    /// Returns the store key (directory name) for a remote repo.
    pub(crate) fn repo_key(repo: &RemoteRepo) -> String {
        let mut hasher = SeaHasher::new();
        repo.repo.hash(&mut hasher);
        repo.rev.hash(&mut hasher);
        to_hex(hasher.finish())
    }

    fn legacy_repo_key(repo: &RemoteRepo) -> String {
        let mut hasher = DefaultHasher::new();
        repo.repo.hash(&mut hasher);
        repo.rev.hash(&mut hasher);
        to_hex(hasher.finish())
    }

    fn legacy_repo_path(&self, repo: &RemoteRepo) -> Option<PathBuf> {
        let path = self.repos_dir().join(Self::legacy_repo_key(repo));
        path.join(REPO_MARKER).is_file().then_some(path)
    }

    pub(crate) fn repos_dir(&self) -> PathBuf {
        self.path.join("repos")
    }

    pub(crate) fn hooks_dir(&self) -> PathBuf {
        self.path.join("hooks")
    }

    pub(crate) fn patches_dir(&self) -> PathBuf {
        self.path.join("patches")
    }

    pub(crate) fn tools_dir(&self) -> PathBuf {
        self.path.join("tools")
    }

    pub(crate) fn cache_dir(&self) -> PathBuf {
        self.path.join("cache")
    }

    /// The path to the tool directory in the store.
    pub(crate) fn tools_path(&self, tool: ToolBucket) -> PathBuf {
        self.tools_dir().join(tool.as_ref())
    }

    pub(crate) fn cache_path(&self, tool: CacheBucket) -> PathBuf {
        self.cache_dir().join(tool.as_ref())
    }

    /// Scratch path for temporary files.
    pub(crate) fn scratch_path(&self) -> PathBuf {
        self.path.join("scratch")
    }

    pub(crate) fn log_file(&self) -> PathBuf {
        self.path.join("prek.log")
    }

    pub(crate) fn config_tracking_file(&self) -> PathBuf {
        self.path.join("config-tracking.json")
    }

    /// Get all tracked config files.
    ///
    /// Seed `config-tracking.json` from the workspace discovery cache if it doesn't exist.
    /// This is a one-time upgrade helper: it only does work when tracking is empty.
    pub(crate) fn tracked_configs(&self) -> Result<FxHashSet<PathBuf>, Error> {
        let tracking_file = self.config_tracking_file();
        match fs_err::read_to_string(&tracking_file) {
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
            Err(e) => return Err(e.into()),
            Ok(content) => {
                let tracked = serde_json::from_str(&content).unwrap_or_else(|e| {
                    warn!("Failed to parse config tracking file: {e}, resetting");
                    FxHashSet::default()
                });
                return Ok(tracked);
            }
        }

        let cached = WorkspaceCache::cached_config_paths(self);
        if cached.is_empty() {
            return Ok(FxHashSet::default());
        }

        debug!(
            count = cached.len(),
            "Bootstrapping config tracking from workspace cache"
        );
        self.update_tracked_configs(&cached)?;

        Ok(cached)
    }

    /// Track new config files for GC.
    pub(crate) fn track_configs<'a>(
        &self,
        config_paths: impl Iterator<Item = &'a Path>,
    ) -> Result<(), Error> {
        let mut tracked = self.tracked_configs()?;
        for config_path in config_paths {
            tracked.insert(config_path.to_path_buf());
        }

        let tracking_file = self.config_tracking_file();
        let content = serde_json::to_string_pretty(&tracked)?;
        fs_err::write(&tracking_file, content)?;

        Ok(())
    }

    /// Update the tracked configs file.
    pub(crate) fn update_tracked_configs(&self, configs: &FxHashSet<PathBuf>) -> Result<(), Error> {
        let tracking_file = self.config_tracking_file();
        let content = serde_json::to_string_pretty(configs)?;
        fs_err::write(&tracking_file, content)?;

        Ok(())
    }
}

#[derive(Copy, Clone, Eq, Hash, PartialEq, strum::EnumIter, strum::AsRefStr, strum::Display)]
#[strum(serialize_all = "lowercase")]
pub(crate) enum ToolBucket {
    Uv,
    Python,
    Node,
    Go,
    Ruby,
    Rustup,
    Bun,
    Dotnet,
    Deno,
}

#[derive(Copy, Clone, Eq, Hash, PartialEq, strum::AsRefStr, strum::Display)]
#[strum(serialize_all = "lowercase")]
pub(crate) enum CacheBucket {
    Uv,
    Go,
    Python,
    Cargo,
    Deno,
    Prek,
}

/// Convert a u64 to a hex string.
fn to_hex(num: u64) -> String {
    hex::encode(num.to_le_bytes())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn remote_repo() -> RemoteRepo {
        RemoteRepo::new(
            "https://github.com/pre-commit/pre-commit-hooks".to_string(),
            "v6.0.0".to_string(),
            vec![],
        )
    }

    #[test]
    fn repo_path_prefers_existing_legacy_repo_dir() {
        let temp = tempfile::tempdir().expect("create temp dir");
        let store = Store::from_path(temp.path()).init().expect("init store");
        let repo = remote_repo();
        let legacy_path = store.repos_dir().join(Store::legacy_repo_key(&repo));

        fs_err::create_dir_all(&legacy_path).expect("create legacy repo dir");
        fs_err::write(legacy_path.join(REPO_MARKER), "{}").expect("write repo marker");

        assert_eq!(store.repo_path(&repo), legacy_path);
    }

    #[test]
    fn repo_path_uses_stable_key_without_legacy_marker() {
        let temp = tempfile::tempdir().expect("create temp dir");
        let store = Store::from_path(temp.path()).init().expect("init store");
        let repo = remote_repo();
        let legacy_path = store.repos_dir().join(Store::legacy_repo_key(&repo));

        fs_err::create_dir_all(&legacy_path).expect("create legacy repo dir");

        assert_eq!(
            store.repo_path(&repo),
            store.repos_dir().join(Store::repo_key(&repo))
        );
    }
}