Compare commits

..

No commits in common. "4f28ced1b1e44bbbccda4e01ba882c756f683ed2" and "54657adaf0884bc47bc24f287a919f21afac2b24" have entirely different histories.

26 changed files with 15 additions and 324 deletions

3
.gitignore vendored
View File

@ -4,6 +4,3 @@ target/
# Code coverage results # Code coverage results
coverage/ coverage/
# mdBook output
hooked-book/book/

127
Cargo.lock generated
View File

@ -35,20 +35,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "assert_cmd"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5c2ca00549910ec251e3bd15f87aeeb206c9456b9a77b43ff6c97c54042a472"
dependencies = [
"bstr",
"doc-comment",
"predicates",
"predicates-core",
"predicates-tree",
"wait-timeout",
]
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -102,9 +88,7 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
dependencies = [ dependencies = [
"lazy_static",
"memchr", "memchr",
"regex-automata",
] ]
[[package]] [[package]]
@ -330,12 +314,6 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690"
[[package]]
name = "difflib"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.5" version = "0.10.5"
@ -346,18 +324,6 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "doc-comment"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]] [[package]]
name = "encode_unicode" name = "encode_unicode"
version = "0.3.6" version = "0.3.6"
@ -450,20 +416,14 @@ dependencies = [
name = "hooked-cli" name = "hooked-cli"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"assert_cmd",
"clap", "clap",
"color-eyre", "color-eyre",
"hooked-config", "hooked-library",
"insta",
"owo-colors",
"subprocess",
"supports-color",
"tera", "tera",
"test-case",
] ]
[[package]] [[package]]
name = "hooked-config" name = "hooked-library"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"color-eyre", "color-eyre",
@ -540,21 +500,6 @@ dependencies = [
"yaml-rust", "yaml-rust",
] ]
[[package]]
name = "is_ci"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.4" version = "1.0.4"
@ -777,33 +722,6 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "predicates"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5aab5be6e4732b473071984b3164dbbfb7a3674d30ea5ff44410b6bcd960c3c"
dependencies = [
"difflib",
"itertools",
"predicates-core",
]
[[package]]
name = "predicates-core"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da1c2388b1513e1b605fcec39a95e0a9e8ef088f71443ef37099fa9ae6673fcb"
[[package]]
name = "predicates-tree"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d86de6de25020a36c6d3643a86d9a6a9f552107c0559c60ea03551b5e16c032"
dependencies = [
"predicates-core",
"termtree",
]
[[package]] [[package]]
name = "proc-macro-error" name = "proc-macro-error"
version = "1.0.4" version = "1.0.4"
@ -887,12 +805,6 @@ dependencies = [
"regex-syntax", "regex-syntax",
] ]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.27" version = "0.6.27"
@ -1004,26 +916,6 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "subprocess"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2e86926081dda636c546d8c5e641661049d7562a68f5488be4a1f7f66f6086"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "supports-color"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4872ced36b91d47bae8a214a683fe54e7078875b399dfa251df346c9b547d1f9"
dependencies = [
"atty",
"is_ci",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.103" version = "1.0.103"
@ -1076,12 +968,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "termtree"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b"
[[package]] [[package]]
name = "test-case" name = "test-case"
version = "2.2.2" version = "2.2.2"
@ -1279,15 +1165,6 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wait-timeout"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.3.2" version = "2.3.2"

View File

@ -1,5 +1,5 @@
[workspace] [workspace]
members = [ members = [
"hooked-cli", "hooked-cli",
"hooked-config" "hooked-library"
] ]

View File

@ -40,22 +40,3 @@ args = [
"--skip-clean", "--skip-clean",
"--target-dir=target/tarpaulin" "--target-dir=target/tarpaulin"
] ]
[tasks.book]
workspace = false
install_crate = "mdbook"
command = "mdbook"
[tasks.clean-book]
extend = "book"
args = ["clean", "hooked-book"]
[tasks.build-book]
extend = "book"
dependencies = ["clean-book"]
args = ["build", "hooked-book"]
[tasks.serve-book]
extend = "book"
dependencies = ["clean-book"]
args = ["serve", "hooked-book"]

View File

@ -13,20 +13,12 @@ path = "source/main.rs"
[dependencies] [dependencies]
color-eyre = "0.6.2" color-eyre = "0.6.2"
owo-colors = "3.5.0"
subprocess = "0.2.9"
supports-color = "1.3.0"
tera = "1.17.1" tera = "1.17.1"
[dependencies.clap] [dependencies.clap]
features = ["derive"] features = ["derive"]
version = "4.0.18" version = "4.0.18"
[dependencies.hooked-config] [dependencies.hooked-library]
path = "../hooked-config" path = "../hooked-library"
version = "0.1.0" version = "0.1.0"
[dev-dependencies]
assert_cmd = "2.0.5"
insta = "1.21.0"
test-case = "2.2.2"

View File

@ -4,10 +4,6 @@ use std::path::PathBuf;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
mod run;
pub use run::hooked_run;
/// CLI arguments struct using [`clap::Parser`]. /// CLI arguments struct using [`clap::Parser`].
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[clap(about, author, version)] #[clap(about, author, version)]
@ -38,11 +34,4 @@ pub enum MainSubcommands {
#[clap(long)] #[clap(long)]
all: bool, all: bool,
}, },
/// Manually run hooks.
Run {
/// The hook type to run.
#[clap(value_parser = crate::HOOK_TYPES)]
hook_type: String,
},
} }

View File

@ -1,84 +0,0 @@
//! The `run` subcommand.
use std::{io::Read, process::exit};
use {
color_eyre::{eyre::eyre, Result},
hooked_config::{Config, ExitAction},
owo_colors::{OwoColorize, Style},
subprocess::{Exec, Redirection},
supports_color::Stream,
};
/// The `run` subcommand.
pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
let (success_style, warn_style, error_style) =
if let Some(_support) = supports_color::on(Stream::Stdout) {
let shared_style = Style::new().bold();
(
shared_style.green(),
shared_style.yellow(),
shared_style.red(),
)
} else {
(Style::new(), Style::new(), Style::new())
};
if hook_type == "pre-commit" {
println!(
"Hooked: Running {} pre-commit hooks.",
config.pre_commit.len()
);
for hook in config.pre_commit {
let hook_name = hook.name.unwrap_or_else(|| "Unnamed Hook".to_string());
let command = match (hook.task.command, hook.task.script) {
(Some(command), _) => Ok(Exec::shell(command)),
(None, Some(script_file)) => {
let script_path = config.general.directory.join(script_file);
let script_path_str = script_path
.to_str()
.ok_or_else(|| eyre!("Failed to convert path to str"))?;
Ok(Exec::shell(script_path_str))
}
(None, None) => Err(eyre!(
"No command or script provided for hook: {}",
hook_name
)),
}?;
let mut process = command
.stderr(Redirection::Merge)
.stdout(Redirection::Pipe)
.popen()?;
let exit_status = process.wait()?;
let output = {
let mut output = String::new();
let mut stdout_file = process.stdout.take().unwrap();
stdout_file.read_to_string(&mut output)?;
output
};
let (stop, print_output, prefix, style) =
match (exit_status.success(), hook.on_failure) {
(true, _) => (false, false, "", success_style),
(false, ExitAction::Continue) => (false, true, "", warn_style),
(false, ExitAction::Stop) => (true, true, "", error_style),
};
println!("\t{} {}", prefix.style(style), hook_name.style(style));
if !output.is_empty() && print_output {
println!("{}", output);
}
if stop {
exit(1);
}
}
}
Ok(())
}

View File

@ -14,7 +14,7 @@ use std::{
use { use {
clap::Parser, clap::Parser,
color_eyre::{eyre::eyre, install, Result}, color_eyre::{eyre::eyre, install, Result},
hooked_config::Config, hooked_library::Config,
tera::{Context, Tera}, tera::{Context, Tera},
}; };
@ -24,7 +24,7 @@ use crate::cli::{Args, MainSubcommands};
pub const DEFAULT_TEMPLATE: &str = include_str!("templates/default.sh"); pub const DEFAULT_TEMPLATE: &str = include_str!("templates/default.sh");
/// All supported hook types. /// All supported hook types.
pub const HOOK_TYPES: [&str; 1] = ["pre-commit"]; pub const HOOK_TYPES: &[&str] = &["pre-commit"];
mod cli; mod cli;
@ -35,13 +35,12 @@ fn main() -> Result<()> {
let config = Config::from_toml_file(args.config)?; let config = Config::from_toml_file(args.config)?;
let git_hooks_dir = PathBuf::from(".git/hooks/"); let git_hooks_dir = PathBuf::from(".git/hooks/");
if !git_hooks_dir.exists() {
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
}
match args.command { match args.command {
MainSubcommands::Install { overwrite } => { MainSubcommands::Install { overwrite } => {
if !git_hooks_dir.exists() {
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
}
for hook_type in HOOK_TYPES { for hook_type in HOOK_TYPES {
let mut context = Context::new(); let mut context = Context::new();
context.insert("hook_type", hook_type); context.insert("hook_type", hook_type);
@ -64,10 +63,6 @@ fn main() -> Result<()> {
} }
MainSubcommands::Uninstall { all } => { MainSubcommands::Uninstall { all } => {
if !git_hooks_dir.exists() {
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
}
for hook_type in HOOK_TYPES { for hook_type in HOOK_TYPES {
let hook_path = git_hooks_dir.join(hook_type); let hook_path = git_hooks_dir.join(hook_type);
if !hook_path.exists() { if !hook_path.exists() {
@ -85,10 +80,6 @@ fn main() -> Result<()> {
} }
} }
} }
MainSubcommands::Run { hook_type } => {
cli::hooked_run(config, hook_type)?;
}
} }
Ok(()) Ok(())

View File

@ -1,21 +0,0 @@
use {assert_cmd::Command, insta::assert_snapshot, test_case::test_case};
#[test_case("pre-commit", "pre-commit.toml", true; "pre-commit")]
fn test_hooked_run(hook_type: &str, config: &str, expect_failure: bool) {
let config = format!("tests/samples/{}", config);
let mut command = Command::cargo_bin("hooked").unwrap();
command
.args(["run", hook_type, "--config", &config])
.env("NO_COLOR", "1");
let assert = if expect_failure {
command.assert().failure().code(1)
} else {
command.assert().success()
};
let stderr = std::str::from_utf8(&assert.get_output().stderr).unwrap();
let stdout = std::str::from_utf8(&assert.get_output().stdout).unwrap();
assert_snapshot!(hook_type, format!("{}{}", stdout, stderr));
}

View File

@ -1,14 +0,0 @@
[general]
directory = "tests/samples"
[[pre_commit]]
script = "simple-echo.bash"
[[pre_commit]]
name = "Exit 1 Continue"
command = "echo 'on_failure=continue' && exit 1"
on_failure = "continue"
[[pre_commit]]
name = "Exit 1 Stop"
command = "echo 'Hey from stdout!' && echo 'Hey from stderr!' 1>&2 && exit 1"

View File

@ -1,3 +0,0 @@
#!/usr/bin/env bash
echo "Hey, $USER!"

View File

@ -1,14 +0,0 @@
---
source: hooked-cli/tests/run.rs
expression: "format!(\"{}{}\", stdout, stderr)"
---
Hooked: Running 3 pre-commit hooks.
✓ Unnamed Hook
⚠ Exit 1 Continue
on_failure=continue
✗ Exit 1 Stop
Hey from stdout!
Hey from stderr!

View File

@ -1,6 +1,6 @@
[package] [package]
name = "hooked-config" name = "hooked-library"
description = "Configuration for Hooked." description = "Git hooks manager."
repository = "https://git.bauke.xyz/Holllo/hooked" repository = "https://git.bauke.xyz/Holllo/hooked"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
version = "0.1.0" version = "0.1.0"

View File

@ -3,7 +3,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// Action to take on hook exit. /// Action to take on hook exit.
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] #[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum ExitAction { pub enum ExitAction {
Continue, Continue,

View File

@ -1,6 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use {color_eyre::Result, hooked_config::Config}; use {color_eyre::Result, hooked_library::Config};
use {insta::assert_debug_snapshot, test_case::test_case}; use {insta::assert_debug_snapshot, test_case::test_case};

View File

@ -1,5 +1,5 @@
use { use {
hooked_config::{Config, ExitAction, PreCommit, Task}, hooked_library::{Config, ExitAction, PreCommit, Task},
toml::to_string_pretty, toml::to_string_pretty,
}; };