Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support init-templatedir #101

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ pub(crate) async fn install(
overwrite: bool,
allow_missing_config: bool,
printer: Printer,
git_dir: Option<&str>,
) -> Result<ExitStatus> {
if git::has_hooks_path_set().await? {
if git_dir.is_none() && git::has_hooks_path_set().await? {
writeln!(
printer.stderr(),
indoc::indoc! {"
Expand All @@ -35,7 +36,12 @@ pub(crate) async fn install(

let hook_types = get_hook_types(config.clone(), hook_types);

let hooks_path = git::get_git_common_dir().await?.join("hooks");
let hooks_path = if let Some(dir) = git_dir {
Path::new(dir).join("hooks")
} else {
git::get_git_common_dir().await?.join("hooks")
};

fs_err::create_dir_all(&hooks_path)?;

let project = Project::from_config_file(config);
Expand Down
16 changes: 15 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub(crate) enum Command {
Clean,
/// Install hook script in a directory intended for use with `git config init.templateDir`.
#[command(name = "init-templatedir")]
InitTemplateDir,
InitTemplateDir(InitTemplateDirArgs),
/// Try the pre-commit hooks in the current repo.
TryRepo(Box<RunArgs>),

Expand Down Expand Up @@ -339,3 +339,17 @@ pub(crate) struct GenerateShellCompletionArgs {
#[arg(value_enum)]
pub shell: clap_complete::Shell,
}

#[derive(Debug, Args)]
pub(crate) struct InitTemplateDirArgs {
/// The directory in which to write the hook script.
pub(crate) directory: String,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) directory: String,
pub(crate) directory: PathBuf,


/// Assume cloned repos should have a `pre-commit` config.
#[arg(long, default_value_t = false)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[arg(long, default_value_t = false)]
#[arg(long)]

pub(crate) no_allow_missing_config: bool,

/// Which hook type to install.
#[arg(long, short = 't', default_value_t = HookType::PreCommit)]
pub(crate) hook_type: HookType,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[arg(long, short = 't', default_value_t = HookType::PreCommit)]
pub(crate) hook_type: HookType,
#[arg(short = 't', long = "hook-type", value_name = "HOOK_TYPE", value_enum)]
pub(crate) hook_types: Vec<HookType>,

}
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
args.overwrite,
args.allow_missing_config,
printer,
None,
)
.await
}
Expand Down Expand Up @@ -241,6 +242,20 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
clap_complete::generate(args.shell, &mut command, bin_name, &mut std::io::stdout());
Ok(ExitStatus::Success)
}
Command::InitTemplateDir(args) => {
show_settings!(args);

cli::install(
cli.globals.config,
vec![args.hook_type],
false,
true,
!args.no_allow_missing_config,
printer,
Some(&args.directory),
)
.await
}
_ => {
writeln!(printer.stderr(), "Command not implemented yet")?;
Ok(ExitStatus::Failure)
Expand Down