Compare commits
2 Commits
4ba723d5cc
...
651699c40a
Author | SHA1 | Date |
---|---|---|
Bauke | 651699c40a | |
Bauke | da0e38e3bb |
|
@ -453,6 +453,7 @@ dependencies = [
|
||||||
"assert_cmd",
|
"assert_cmd",
|
||||||
"clap",
|
"clap",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
|
"globset",
|
||||||
"hooked-config",
|
"hooked-config",
|
||||||
"insta",
|
"insta",
|
||||||
"owo-colors",
|
"owo-colors",
|
||||||
|
|
|
@ -16,6 +16,7 @@ path = "source/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
color-eyre = "0.6.2"
|
color-eyre = "0.6.2"
|
||||||
|
globset = "0.4.9"
|
||||||
owo-colors = "3.5.0"
|
owo-colors = "3.5.0"
|
||||||
subprocess = "0.2.9"
|
subprocess = "0.2.9"
|
||||||
supports-color = "1.3.0"
|
supports-color = "1.3.0"
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::{io::Read, process::exit};
|
||||||
|
|
||||||
use {
|
use {
|
||||||
color_eyre::{eyre::eyre, Result},
|
color_eyre::{eyre::eyre, Result},
|
||||||
|
globset::{Glob, GlobSetBuilder},
|
||||||
hooked_config::{Config, ExitAction},
|
hooked_config::{Config, ExitAction},
|
||||||
owo_colors::{OwoColorize, Style},
|
owo_colors::{OwoColorize, Style},
|
||||||
subprocess::{Exec, Redirection},
|
subprocess::{Exec, Redirection},
|
||||||
|
@ -14,16 +15,17 @@ use crate::utilities::plural;
|
||||||
|
|
||||||
/// The `run` subcommand.
|
/// The `run` subcommand.
|
||||||
pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
|
pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
|
||||||
let (success_style, warn_style, error_style) =
|
let (success_style, warn_style, error_style, skipped_style) =
|
||||||
if let Some(_support) = supports_color::on(Stream::Stdout) {
|
if let Some(_support) = supports_color::on(Stream::Stdout) {
|
||||||
let shared_style = Style::new().bold();
|
let shared_style = Style::new().bold();
|
||||||
(
|
(
|
||||||
shared_style.green(),
|
shared_style.green(),
|
||||||
shared_style.yellow(),
|
shared_style.yellow(),
|
||||||
shared_style.red(),
|
shared_style.red(),
|
||||||
|
shared_style.blue(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
(Style::new(), Style::new(), Style::new())
|
(Style::new(), Style::new(), Style::new(), Style::new())
|
||||||
};
|
};
|
||||||
|
|
||||||
if hook_type == "pre-commit" {
|
if hook_type == "pre-commit" {
|
||||||
|
@ -34,9 +36,35 @@ pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
|
||||||
plural(hook_count, "hook", None)
|
plural(hook_count, "hook", None)
|
||||||
);
|
);
|
||||||
|
|
||||||
for hook in config.pre_commit {
|
'hook_loop: for hook in config.pre_commit {
|
||||||
let hook_name = hook.name.unwrap_or_else(|| "Unnamed Hook".to_string());
|
let hook_name = hook.name.unwrap_or_else(|| "Unnamed Hook".to_string());
|
||||||
|
|
||||||
|
if !hook.git_staged.is_empty() {
|
||||||
|
let globs = {
|
||||||
|
let mut builder = GlobSetBuilder::new();
|
||||||
|
for glob in hook.git_staged {
|
||||||
|
builder.add(Glob::new(&glob)?);
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.build()?
|
||||||
|
};
|
||||||
|
|
||||||
|
let staged_files = Exec::cmd("git")
|
||||||
|
.args(&["diff", "--name-only", "--cached"])
|
||||||
|
.capture()?
|
||||||
|
.stdout_str();
|
||||||
|
for line in staged_files.lines() {
|
||||||
|
if globs.is_match(line) {
|
||||||
|
println!(
|
||||||
|
"\t{} {}",
|
||||||
|
"≫".style(skipped_style),
|
||||||
|
hook_name.style(skipped_style)
|
||||||
|
);
|
||||||
|
continue 'hook_loop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let command = match (hook.task.command, hook.task.script) {
|
let command = match (hook.task.command, hook.task.script) {
|
||||||
(Some(command), _) => Ok(Exec::shell(command)),
|
(Some(command), _) => Ok(Exec::shell(command)),
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,10 @@ use crate::{ExitAction, Task};
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct PreCommit {
|
pub struct PreCommit {
|
||||||
|
/// List of globs to check against staged files.
|
||||||
|
#[serde(default)]
|
||||||
|
pub git_staged: Vec<String>,
|
||||||
|
|
||||||
/// Display name for this hook.
|
/// Display name for this hook.
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ template = "test.sh"
|
||||||
[[pre_commit]]
|
[[pre_commit]]
|
||||||
name = "Pre Commit 1"
|
name = "Pre Commit 1"
|
||||||
command = "exit 0"
|
command = "exit 0"
|
||||||
|
git_staged = ["*.txt"]
|
||||||
on_failure = "continue"
|
on_failure = "continue"
|
||||||
|
|
||||||
[[pre_commit]]
|
[[pre_commit]]
|
||||||
|
|
|
@ -8,6 +8,7 @@ use insta::assert_snapshot;
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize() {
|
fn test_serialize() {
|
||||||
let pre_commit_command = PreCommit {
|
let pre_commit_command = PreCommit {
|
||||||
|
git_staged: vec!["*.txt".to_string()],
|
||||||
name: Some("Command Test".to_string()),
|
name: Some("Command Test".to_string()),
|
||||||
on_failure: ExitAction::Continue,
|
on_failure: ExitAction::Continue,
|
||||||
task: Task {
|
task: Task {
|
||||||
|
@ -17,6 +18,7 @@ fn test_serialize() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let pre_commit_script = PreCommit {
|
let pre_commit_script = PreCommit {
|
||||||
|
git_staged: vec![],
|
||||||
name: Some("Script Test".to_string()),
|
name: Some("Script Test".to_string()),
|
||||||
on_failure: ExitAction::Stop,
|
on_failure: ExitAction::Stop,
|
||||||
task: Task {
|
task: Task {
|
||||||
|
|
|
@ -10,6 +10,7 @@ Config {
|
||||||
},
|
},
|
||||||
pre_commit: [
|
pre_commit: [
|
||||||
PreCommit {
|
PreCommit {
|
||||||
|
git_staged: [],
|
||||||
name: None,
|
name: None,
|
||||||
on_failure: Stop,
|
on_failure: Stop,
|
||||||
task: Task {
|
task: Task {
|
||||||
|
|
|
@ -12,6 +12,9 @@ Config {
|
||||||
},
|
},
|
||||||
pre_commit: [
|
pre_commit: [
|
||||||
PreCommit {
|
PreCommit {
|
||||||
|
git_staged: [
|
||||||
|
"*.txt",
|
||||||
|
],
|
||||||
name: Some(
|
name: Some(
|
||||||
"Pre Commit 1",
|
"Pre Commit 1",
|
||||||
),
|
),
|
||||||
|
@ -24,6 +27,7 @@ Config {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
PreCommit {
|
PreCommit {
|
||||||
|
git_staged: [],
|
||||||
name: Some(
|
name: Some(
|
||||||
"Pre Commit 2",
|
"Pre Commit 2",
|
||||||
),
|
),
|
||||||
|
|
|
@ -7,11 +7,13 @@ config = 'Hooked.toml'
|
||||||
directory = 'hooks'
|
directory = 'hooks'
|
||||||
|
|
||||||
[[pre_commit]]
|
[[pre_commit]]
|
||||||
|
git_staged = ['*.txt']
|
||||||
name = 'Command Test'
|
name = 'Command Test'
|
||||||
on_failure = 'continue'
|
on_failure = 'continue'
|
||||||
command = 'exit 0'
|
command = 'exit 0'
|
||||||
|
|
||||||
[[pre_commit]]
|
[[pre_commit]]
|
||||||
|
git_staged = []
|
||||||
name = 'Script Test'
|
name = 'Script Test'
|
||||||
on_failure = 'stop'
|
on_failure = 'stop'
|
||||||
script = 'test.sh'
|
script = 'test.sh'
|
||||||
|
|
Loading…
Reference in New Issue