Add git_staged CLI functionality.

This commit is contained in:
Bauke 2022-11-20 12:39:08 +01:00
parent da0e38e3bb
commit 651699c40a
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 33 additions and 3 deletions

1
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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)),