Repository: swellaby/rusty-hook
Pull request #122 (2020-09-27 17:45:59)

Inline git arguments
## Changes
<!-- Summary of changes included in this PR -->
- [x] Add the ability to have inline git arguments in hook commands

## Related Issues
<!-- List any related/impacted issues -->
- Closes https://github.com/swellaby/rusty-hook/issues/113


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 34f626d..ba80d5c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -164,9 +164,9 @@ dependencies = [
 
 [[package]]
 name = "proc-macro2"
-version = "1.0.21"
+version = "1.0.23"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c"
+checksum = "51ef7cd2518ead700af67bf9d1a658d90b6037d77110fd9c0445429d0ba1c6c9"
 dependencies = [
  "unicode-xid",
 ]
@@ -204,9 +204,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
 
 [[package]]
 name = "syn"
-version = "1.0.41"
+version = "1.0.42"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b"
+checksum = "9c51d92969d209b54a98397e1b91c8ae82d8c87a7bb87df0b29aa2ad81454228"
 dependencies = [
  "proc-macro2",
  "quote",
diff --git a/README.md b/README.md
index e1edd05..fa72c35 100644
--- a/README.md
+++ b/README.md
@@ -72,6 +72,14 @@ verbose = true
 ### Hooks
 Under the `[hooks]` table, you can add an entry for any and every git hook you want to run by adding a key using the name of the [git hook][git hooks], and then specify the command/script you want to run for that hook. Whenever that git hook is triggered, `rusty-hook` will run your specified command!
 
+#### Using git arguments
+In git hook commands, any instance of `%rh!` will be replaced by the arguments that git passes to this hook.  
+
+```toml
+[hooks]
+pre-push = "echo %rh!"
+```
+
 ### Logging
 Under the `[logging]` table, you can control whether to log the output of running your specified hook commands. By default `rusty-hook` will log the results of your hook script, but you can disable this behavior by setting the `verbose` key to `false`:
 
diff --git a/src/main.rs b/src/main.rs
index c0b3abe..5e8c0e6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -44,14 +44,14 @@ fn init() {
     };
 }
 
-fn run(hook: String, args: String) {
+fn run(hook: String, args: Option<String>) {
     if let Err(err) = rusty_hook::run(
         nias::get_command_runner(),
         nias::get_file_existence_checker(),
         nias::get_file_reader(),
         nias::get_conditional_logger(),
         &hook,
-        &args,
+        args,
     ) {
         match err {
             Some(e) if e == rusty_hook::NO_CONFIG_FILE_FOUND => {
@@ -71,6 +71,6 @@ fn main() {
     match opts {
         RustyHookOpts::Init => init(),
         RustyHookOpts::Version => println!(env!("CARGO_PKG_VERSION")),
-        RustyHookOpts::Run { hook, args } => run(hook, args.unwrap_or_default()),
+        RustyHookOpts::Run { hook, args } => run(hook, args),
     }
 }
diff --git a/src/rusty_hook.rs b/src/rusty_hook.rs
index f584431..f89e9e7 100644
--- a/src/rusty_hook.rs
+++ b/src/rusty_hook.rs
@@ -1,7 +1,6 @@
-use std::collections::HashMap;
-
 pub use config::NO_CONFIG_FILE_FOUND;
 pub use git::NO_CONFIG_FILE_FOUND_ERROR_CODE;
+use std::collections::HashMap;
 
 #[path = "config.rs"]
 mod config;
@@ -62,7 +61,7 @@ pub fn run<F, G, H, I>(
     read_file: H,
     log: I,
     hook_name: &str,
-    _args: &str,
+    args: Option<String>,
 ) -> Result<(), Option<String>>
 where
     F: Fn(
@@ -97,7 +96,7 @@ where
         };
 
     let log_details = config::get_log_setting(&config_file_contents);
-    let script = match config::get_hook_script(&config_file_contents, &hook_name) {
+    let mut script = match config::get_hook_script(&config_file_contents, &hook_name) {
         Ok(script) => script,
         Err(err) => {
             if err == config::MISSING_CONFIG_KEY {
@@ -113,6 +112,10 @@ where
     );
     log(&message, log_details);
 
+    if let Some(args) = args {
+        script = script.replace("%rh!", &args)
+    }
+
     match run_command(&script, Some(&root_directory_path), log_details, None) {
         Err(e) => Err(e),
         Ok(_) => Ok(()),


Test patch:
diff --git a/src/rusty_hook_test.rs b/src/rusty_hook_test.rs
index ce3bae4..d7467ad 100644
--- a/src/rusty_hook_test.rs
+++ b/src/rusty_hook_test.rs
@@ -106,7 +106,14 @@ mod run_tests {
         let read_file = |_file_path: &str| panic!("");
         let file_exists = |_path: &str| panic!("");
         let log = |_path: &str, _should_log: bool| panic!("");
-        let result = run(run_command, file_exists, read_file, log, "", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "",
+            Some("".into()),
+        );
         assert_eq!(result, Err(Some(String::from(exp_err))));
     }
 
@@ -117,7 +124,14 @@ mod run_tests {
         let read_file = |_file_path: &str| Err(());
         let file_exists = |_path: &str| Ok(false);
         let log = |_path: &str, _should_log: bool| panic!("");
-        let result = run(run_command, file_exists, read_file, log, "", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "",
+            Some("".into()),
+        );
         assert_eq!(result, Err(Some(String::from(exp_err))));
     }
 
@@ -128,7 +142,14 @@ mod run_tests {
         let read_file = |_file_path: &str| Err(());
         let file_exists = |_path: &str| Ok(true);
         let log = |_path: &str, _should_log: bool| panic!("");
-        let result = run(run_command, file_exists, read_file, log, "", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "",
+            Some("".into()),
+        );
         assert_eq!(result, Err(Some(String::from(exp_err))));
     }
 
@@ -141,7 +162,14 @@ mod run_tests {
         let read_file = |_file_path: &str| Ok(String::from(contents));
         let file_exists = |_path: &str| Ok(true);
         let log = |_path: &str, _should_log: bool| panic!("");
-        let result = run(run_command, file_exists, read_file, log, "pre-push", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "pre-push",
+            Some("".into()),
+        );
         assert_eq!(result, Ok(()));
     }
 
@@ -153,7 +181,14 @@ mod run_tests {
         let read_file = |_file_path: &str| Ok(String::from(contents));
         let file_exists = |_path: &str| Ok(true);
         let log = |_path: &str, _should_log: bool| panic!("");
-        let result = run(run_command, file_exists, read_file, log, "pre-push", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "pre-push",
+            Some("".into()),
+        );
         assert_eq!(result, Err(Some(String::from(exp_err))));
     }
 
@@ -181,7 +216,14 @@ mod run_tests {
                 panic!("")
             }
         };
-        let result = run(run_command, file_exists, read_file, log, "pre-commit", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "pre-commit",
+            Some("".into()),
+        );
         assert_eq!(result, Ok(()));
     }
 
@@ -209,7 +251,14 @@ mod run_tests {
                 panic!("")
             }
         };
-        let result = run(run_command, file_exists, read_file, log, "pre-commit", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "pre-commit",
+            Some("".into()),
+        );
         assert_eq!(result, Ok(()));
     }
 
@@ -225,7 +274,14 @@ mod run_tests {
         let read_file = |_file_path: &str| Ok(String::from(contents));
         let file_exists = |_path: &str| Ok(true);
         let log = |_path: &str, _should_log: bool| ();
-        let result = run(run_command, file_exists, read_file, log, "pre-commit", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "pre-commit",
+            Some("".into()),
+        );
         assert_eq!(result, Ok(()));
     }
 
@@ -250,7 +306,14 @@ mod run_tests {
         let read_file = |_file_path: &str| Ok(String::from(contents));
         let file_exists = |_path: &str| Ok(true);
         let log = |_path: &str, _should_log: bool| ();
-        let result = run(run_command, file_exists, read_file, log, "pre-commit", "");
+        let result = run(
+            run_command,
+            file_exists,
+            read_file,
+            log,
+            "pre-commit",
+            Some("".into()),
+        );
         assert_eq!(result, Err(Some(String::from(exp_err))));
     }
 }