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
18 changes: 14 additions & 4 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use std::sync::Arc;
use anyhow::{Context, Result};
use bstr::ByteSlice;
use owo_colors::OwoColorize;
use prek_consts::CONFIG_FILE;
use same_file::is_same_file;

use crate::cli::reporter::{HookInitReporter, HookInstallReporter};
use crate::cli::run;
use crate::cli::run::{SelectorSource, Selectors};
use crate::cli::{ExitStatus, HookType};
use crate::config::read_config;
use crate::fs::{CWD, Simplified};
use crate::git::{GIT_ROOT, git_cmd};
use crate::printer::Printer;
Expand Down Expand Up @@ -42,7 +44,7 @@ pub(crate) async fn install(
}

let project = Project::discover(config.as_deref(), &CWD).ok();
let hook_types = get_hook_types(project.as_ref(), hook_types);
let hook_types = get_hook_types(project.as_ref(), config.as_deref(), hook_types);

let hooks_path = if let Some(dir) = git_dir {
dir.join("hooks")
Expand Down Expand Up @@ -111,7 +113,11 @@ pub(crate) async fn install_hooks(
Ok(ExitStatus::Success)
}

fn get_hook_types(project: Option<&Project>, hook_types: Vec<HookType>) -> Vec<HookType> {
fn get_hook_types(
project: Option<&Project>,
config: Option<&Path>,
hook_types: Vec<HookType>,
) -> Vec<HookType> {
let mut hook_types = if hook_types.is_empty() {
if let Some(project) = project {
project
Expand All @@ -120,7 +126,11 @@ fn get_hook_types(project: Option<&Project>, hook_types: Vec<HookType>) -> Vec<H
.clone()
.unwrap_or_default()
} else {
vec![]
let config = config.unwrap_or(Path::new(CONFIG_FILE));
read_config(config)
.ok()
.and_then(|cfg| cfg.default_install_hook_types.clone())
.unwrap_or_default()
}
} else {
hook_types
Expand Down Expand Up @@ -319,7 +329,7 @@ pub(crate) async fn uninstall(
let project = Project::discover(config.as_deref(), &CWD).ok();
let hooks_path = git::get_git_common_dir().await?.join("hooks");

for hook_type in get_hook_types(project.as_ref(), hook_types) {
for hook_type in get_hook_types(project.as_ref(), config.as_deref(), hook_types) {
let hook_path = hooks_path.join(hook_type.as_str());
let legacy_path = hooks_path.join(format!("{}.legacy", hook_type.as_str()));

Expand Down
20 changes: 20 additions & 0 deletions tests/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,26 @@ fn init_template_dir_non_git_repo() {
----- stderr -----
warning: git config `init.templateDir` not set to the target directory, try `git config --global init.templateDir '.git'`
"#);

context.write_pre_commit_config(indoc::indoc! {"
default_install_hook_types:
- pre-commit
- commit-msg
- pre-push
repos:
"});
cmd_snapshot!(context.filters(), context.command().arg("init-template-dir").arg("-c").arg(context.work_dir().join(CONFIG_FILE)).arg(".git"), @r"
success: true
exit_code: 0
----- stdout -----
Overwriting existing hook at `.git/hooks/pre-commit`
prek installed at `.git/hooks/pre-commit` with specified config `[TEMP_DIR]/.pre-commit-config.yaml`
prek installed at `.git/hooks/commit-msg` with specified config `[TEMP_DIR]/.pre-commit-config.yaml`
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'`
");
}

#[test]
Expand Down
Loading