Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/prek/src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ pub(crate) async fn init_template_dir(

if template_dir.is_empty() || !is_same_file(&directory, &template_dir)? {
warn_user!(
"git config `init.templateDir` not set to the target directory, try `{}`",
"`init.templateDir` does not point to the target directory. Run `{}` to set it",
format!(
"git config --global init.templateDir '{}'",
directory.display()
Expand Down
15 changes: 6 additions & 9 deletions crates/prek/src/cli/run/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl UnstagedChangesRestorer {
eprintln!(
"{}",
format!(
"Unstaged changes detected, stashing unstaged changes to `{}`",
"Unstaged changes detected. Temporarily saving them to `{}`",
patch_path.user_display()
)
.yellow()
Expand Down Expand Up @@ -198,7 +198,7 @@ impl UnstagedChangesRestorer {
error!("{e}");
eprintln!(
"{}",
"Stashed changes conflicted with changes made by hook, rolling back the hook changes".red().bold()
"Hook changes conflicted with the saved unstaged changes. Reverting the hook changes".red().bold()
);

// Discard any changes made by hooks, and try applying the patch again.
Expand All @@ -208,12 +208,9 @@ impl UnstagedChangesRestorer {

eprintln!(
"{}",
format!(
"Restored working tree changes from `{}`",
patch.user_display()
)
.yellow()
.bold()
format!("Restored unstaged changes from `{}`", patch.user_display())
.yellow()
.bold()
);

Ok(())
Expand All @@ -225,7 +222,7 @@ impl Drop for UnstagedChangesRestorer {
if let Err(err) = self.restore() {
eprintln!(
"{}",
format!("Failed to restore working tree changes: {err}").red()
format!("Failed to restore unstaged changes: {err}").red()
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/prek/src/cli/run/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ pub(crate) async fn run(

// Check if we have unresolved merge conflict files and fail fast.
if should_stash && git::has_unmerged_paths().await? {
anyhow::bail!("You have unmerged paths. Resolve them before running prek");
anyhow::bail!(
"Found unresolved merge conflicts. Resolve the conflicts, stage the files with `git add`, and try again"
);
}

let workspace_root = Workspace::find_root(config.as_deref(), &CWD)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/prek/src/cli/try_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async fn prepare_repo<'a>(

// If repo is a local repo with uncommitted changes, create a shadow repo to commit the changes.
if is_local && git::has_diff("HEAD", repo_path).await? {
warn_user!("Creating temporary repo with uncommitted changes...");
warn_user!("Local repository has uncommitted changes. Creating a temporary copy...");
let shadow = clone_and_commit(repo_path, &head_rev, tmp_dir).await?;
let head_rev = get_head_rev(&shadow).await?;
Ok(PreparedRepo {
Expand Down
27 changes: 13 additions & 14 deletions crates/prek/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1633,13 +1633,12 @@ pub(crate) fn read_config(path: &Path) -> Result<Config, Error> {
if !mutable_revs.is_empty() {
warn_user!(
"{}",
indoc::formatdoc! { r#"
The following repos have mutable `rev` fields (moving tag / branch):
indoc::formatdoc! { r"
The following repositories use mutable `rev` values (branches or moving tags):
{}
Mutable references are never updated after first install and are not supported.
See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details.
hint: `prek update` often fixes this",
"#,
`prek` does not automatically detect changes to these references after the first install.
Use a tag or commit SHA for each `rev`, or run `prek update` to select the latest eligible tag.
See https://prek.j178.dev/reference/configuration/#rev for details.",
mutable_revs
}
);
Expand Down Expand Up @@ -1683,10 +1682,10 @@ where
if version > cur_version {
let hint = InstallSource::detect()
.map(|s| format!("To update, run `{}`.", s.update_instructions()))
.unwrap_or("Please consider updating prek".to_string());
.unwrap_or("Upgrade `prek` and try again.".to_string());

return Err(serde::de::Error::custom(format!(
"Required minimum prek version `{version}` is greater than current version `{cur_version}`; {hint}",
"`prek` version `{version}` or newer is required, but version `{cur_version}` is installed. {hint}",
)));
}

Expand All @@ -1701,8 +1700,8 @@ mod tests {

/// Filter to replace dynamic version in snapshots
const VERSION_FILTER: (&str, &str) = (
r"current version `\d+\.\d+\.\d+(?:-[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)*)?`",
"current version `[CURRENT_VERSION]`",
r"but version `\d+\.\d+\.\d+(?:-[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)*)?` is installed",
"but version `[CURRENT_VERSION]` is installed",
);

#[test]
Expand Down Expand Up @@ -2472,13 +2471,13 @@ mod tests {
let err = serde_saphyr::from_str::<Config>(yaml).unwrap_err();
insta::with_settings!({ filters => vec![VERSION_FILTER] }, {
insta::assert_snapshot!(err, @"
error: line 8 column 23: Required minimum prek version `10.0.0` is greater than current version `[CURRENT_VERSION]`; Please consider updating prek
error: line 8 column 23: `prek` version `10.0.0` or newer is required, but version `[CURRENT_VERSION]` is installed. Upgrade `prek` and try again.
--> <input>:8:23
|
6 | entry: echo test
7 | language: system
8 | minimum_prek_version: '10.0.0'
| ^ Required minimum prek version `10.0.0` is greater than current version `[CURRENT_VERSION]`; Please consider updating prek
| ^ `prek` version `10.0.0` or newer is required, but version `[CURRENT_VERSION]` is installed. Upgrade `prek` and try again.
");
});

Expand All @@ -2493,11 +2492,11 @@ mod tests {
let err = serde_saphyr::from_str::<Manifest>(yaml).unwrap_err();
insta::with_settings!({ filters => vec![VERSION_FILTER] }, {
insta::assert_snapshot!(err, @"
error: line 1 column 3: Required minimum prek version `10.0.0` is greater than current version `[CURRENT_VERSION]`; Please consider updating prek
error: line 1 column 3: `prek` version `10.0.0` or newer is required, but version `[CURRENT_VERSION]` is installed. Upgrade `prek` and try again.
--> <input>:1:3
|
1 | - id: test-hook
| ^ Required minimum prek version `10.0.0` is greater than current version `[CURRENT_VERSION]`; Please consider updating prek
| ^ `prek` version `10.0.0` or newer is required, but version `[CURRENT_VERSION]` is installed. Upgrade `prek` and try again.
2 | name: Test Hook
3 | entry: echo test
|
Expand Down
12 changes: 7 additions & 5 deletions crates/prek/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ pub(crate) enum Error {
)]
MissingConfigFile,

#[error("Hook `{hook}` not present in repo `{repo}`")]
#[error(
"Hook `{hook}` was not found in repository `{repo}`. Check the hook ID or choose a `rev` that includes it"
)]
HookNotFound { hook: String, repo: String },

#[error(transparent)]
Expand Down Expand Up @@ -1043,14 +1045,14 @@ impl Workspace {
.collect::<Vec<_>>();
match non_staged.as_slice() {
[filename] => anyhow::bail!(
"prek configuration file is not staged, run `{}` to stage it",
format!("git add {}", filename.user_display()).cyan()
"Configuration file `{}` is not staged. Stage it with `git add` and try again",
filename.user_display().cyan()
),
_ => anyhow::bail!(
"The following configuration files are not staged, `git add` them first:\n{}",
"The following configuration files are not staged. Stage them with `git add` and try again:\n{}",
non_staged
.iter()
.map(|p| format!(" {}", p.user_display()))
.map(|p| format!(" - `{}`", p.user_display()))
.collect::<Vec<_>>()
.join("\n")
),
Expand Down
14 changes: 7 additions & 7 deletions crates/prek/tests/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ fn init_template_dir() -> anyhow::Result<()> {
prek installed at `.git/hooks/pre-commit`

----- stderr -----
warning: git config `init.templateDir` not set to the target directory, try `git config --global init.templateDir '.git'`
warning: `init.templateDir` does not point to the target directory. Run `git config --global init.templateDir '.git'` to set it
"#);

insta::with_settings!(
Expand Down Expand Up @@ -1257,7 +1257,7 @@ fn init_template_dir() -> anyhow::Result<()> {
prek installed at `temp-dir/hooks/pre-commit`

----- stderr -----
warning: git config `init.templateDir` not set to the target directory, try `git config --global init.templateDir 'temp-dir'`
warning: `init.templateDir` does not point to the target directory. Run `git config --global init.templateDir 'temp-dir'` to set it
"#);
insta::with_settings!(
{ filters => context.filters() },
Expand Down Expand Up @@ -1288,7 +1288,7 @@ fn init_template_dir() -> anyhow::Result<()> {
prek installed at `subdir2/hooks/pre-commit` with specified config `non-exist-config`

----- stderr -----
warning: git config `init.templateDir` not set to the target directory, try `git config --global init.templateDir 'subdir2'`
warning: `init.templateDir` does not point to the target directory. Run `git config --global init.templateDir 'subdir2'` to set it
");
insta::with_settings!(
{ filters => context.filters() },
Expand Down Expand Up @@ -1327,7 +1327,7 @@ fn util_init_template_dir() {
prek installed at `.git/hooks/pre-commit`

----- stderr -----
warning: git config `init.templateDir` not set to the target directory, try `git config --global init.templateDir '.git'`
warning: `init.templateDir` does not point to the target directory. Run `git config --global init.templateDir '.git'` to set it
"#);

insta::with_settings!(
Expand Down Expand Up @@ -1364,7 +1364,7 @@ fn init_template_dir_non_git_repo() {
prek installed at `.git/hooks/pre-commit`

----- stderr -----
warning: git config `init.templateDir` not set to the target directory, try `git config --global init.templateDir '.git'`
warning: `init.templateDir` does not point to the target directory. Run `git config --global init.templateDir '.git'` to set it
"#);

context.write_pre_commit_config(indoc::indoc! {"
Expand All @@ -1384,7 +1384,7 @@ fn init_template_dir_non_git_repo() {
prek installed at `.git/hooks/pre-push` with specified config `[TEMP_DIR]/.pre-commit-config.yaml`

----- stderr -----
warning: git config `init.templateDir` not set to the target directory, try `git config --global init.templateDir '.git'`
warning: `init.templateDir` does not point to the target directory. Run `git config --global init.templateDir '.git'` to set it
");
}

Expand Down Expand Up @@ -1746,7 +1746,7 @@ fn workspace_init_template_dir() -> anyhow::Result<()> {
prek installed at `template/hooks/pre-commit`

----- stderr -----
warning: git config `init.templateDir` not set to the target directory, try `git config --global init.templateDir '[TEMP_DIR]/template'`
warning: `init.templateDir` does not point to the target directory. Run `git config --global init.templateDir '[TEMP_DIR]/template'` to set it
");

// Check that hooks are created in the template directory
Expand Down
22 changes: 11 additions & 11 deletions crates/prek/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ fn config_not_staged() -> Result<()> {
----- stdout -----

----- stderr -----
error: prek configuration file is not staged, run `git add .pre-commit-config.yaml` to stage it
error: Configuration file `.pre-commit-config.yaml` is not staged. Stage it with `git add` and try again
"#);

Ok(())
Expand Down Expand Up @@ -1855,8 +1855,8 @@ fn staged_files_only() -> Result<()> {
Hello, world!

----- stderr -----
Unstaged changes detected, stashing unstaged changes to `[HOME]/patches/[TIME]-[PID].patch`
Restored working tree changes from `[HOME]/patches/[TIME]-[PID].patch`
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");
Expand Down Expand Up @@ -1910,9 +1910,9 @@ fn intent_to_add_file_survives_conflicted_stash_restore() -> Result<()> {
- files were modified by this hook

----- stderr -----
Unstaged changes detected, stashing unstaged changes to `[HOME]/patches/[TIME]-[PID].patch`
Stashed changes conflicted with changes made by hook, rolling back the hook changes
Restored working tree changes from `[HOME]/patches/[TIME]-[PID].patch`
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");
Expand Down Expand Up @@ -2037,7 +2037,7 @@ fn merge_conflicts() -> Result<()> {
----- stdout -----

----- stderr -----
error: You have unmerged paths. Resolve them before running prek
error: Found unresolved merge conflicts. Resolve the conflicts, stage the files with `git add`, and try again
"#);

// Fix the conflict and run again.
Expand Down Expand Up @@ -2715,8 +2715,8 @@ fn minimum_prek_version() {
.filters()
.into_iter()
.chain([(
r"current version `\d+\.\d+\.\d+(?:-[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)*)?`",
"current version `[CURRENT_VERSION]`",
r"but version `\d+\.\d+\.\d+(?:-[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)*)?` is installed",
"but version `[CURRENT_VERSION]` is installed",
)])
.collect::<Vec<_>>();

Expand All @@ -2727,11 +2727,11 @@ fn minimum_prek_version() {

----- stderr -----
error: Failed to parse `.pre-commit-config.yaml`
caused by: error: line 1 column 23: Required minimum prek version `10.0.0` is greater than current version `[CURRENT_VERSION]`; Please consider updating prek
caused by: error: line 1 column 23: `prek` version `10.0.0` or newer is required, but version `[CURRENT_VERSION]` is installed. Upgrade `prek` and try again.
--> <input>:1:23
|
1 | minimum_prek_version: 10.0.0
| ^ Required minimum prek version `10.0.0` is greater than current version `[CURRENT_VERSION]`; Please consider updating prek
| ^ `prek` version `10.0.0` or newer is required, but version `[CURRENT_VERSION]` is installed. Upgrade `prek` and try again.
2 | repos:
3 | - repo: local
|
Expand Down
2 changes: 1 addition & 1 deletion crates/prek/tests/try_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ fn try_repo_uncommitted_changes() -> Result<()> {
Uncommitted Hook.........................................................Passed

----- stderr -----
warning: Creating temporary repo with uncommitted changes...
warning: Local repository has uncommitted changes. Creating a temporary copy...
"#);

Ok(())
Expand Down
25 changes: 25 additions & 0 deletions crates/prek/tests/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ fn validate_config() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn mutable_revision_warning_has_actionable_guidance() {
let context = TestContext::new();
context.write_pre_commit_config(indoc::indoc! {r"
repos:
- repo: https://example.com/hooks
rev: main
hooks: []
"});

cmd_snapshot!(context.filters(), context.validate_config().arg(PRE_COMMIT_CONFIG_YAML), @r"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
warning: The following repositories use mutable `rev` values (branches or moving tags):
https://example.com/hooks: main
`prek` does not automatically detect changes to these references after the first install.
Use a tag or commit SHA for each `rev`, or run `prek update` to select the latest eligible tag.
See https://prek.j178.dev/reference/configuration/#rev for details.
success: All configs are valid
");
}

#[test]
fn invalid_config_error() {
let context = TestContext::new();
Expand Down
20 changes: 10 additions & 10 deletions crates/prek/tests/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ fn config_not_staged() -> Result<()> {
----- stdout -----

----- stderr -----
error: The following configuration files are not staged, `git add` them first:
.pre-commit-config.yaml
nested/project4/.pre-commit-config.yaml
project2/.pre-commit-config.yaml
project3/.pre-commit-config.yaml
project3/project5/.pre-commit-config.yaml
error: The following configuration files are not staged. Stage them with `git add` and try again:
- `.pre-commit-config.yaml`
- `nested/project4/.pre-commit-config.yaml`
- `project2/.pre-commit-config.yaml`
- `project3/.pre-commit-config.yaml`
- `project3/project5/.pre-commit-config.yaml`
");

// Run from a subdirectory
Expand All @@ -400,9 +400,9 @@ fn config_not_staged() -> Result<()> {
----- stdout -----

----- stderr -----
error: The following configuration files are not staged, `git add` them first:
.pre-commit-config.yaml
project5/.pre-commit-config.yaml
error: The following configuration files are not staged. Stage them with `git add` and try again:
- `.pre-commit-config.yaml`
- `project5/.pre-commit-config.yaml`
");

cmd_snapshot!(context.filters(), context.run().current_dir(cwd.join("project2")), @r"
Expand All @@ -411,7 +411,7 @@ fn config_not_staged() -> Result<()> {
----- stdout -----

----- stderr -----
error: prek configuration file is not staged, run `git add .pre-commit-config.yaml` to stage it
error: Configuration file `.pre-commit-config.yaml` is not staged. Stage it with `git add` and try again
");

Ok(())
Expand Down
Loading