From 80bd8b8796f24ba990b8f1d0871f9043bbb55050 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 29 Oct 2024 20:43:38 +0100 Subject: [PATCH] fix(cli): edit option accepts argument Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- src/args.rs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/args.rs b/src/args.rs index 3c76cec..de7e96d 100644 --- a/src/args.rs +++ b/src/args.rs @@ -26,7 +26,7 @@ pub struct Args { /// Read last commit from the specified file or fallbacks to ./.git/COMMIT_EDITMSG #[arg(short = 'e', long)] - pub edit: bool, + pub edit: Option, /// Lower end of the commit range to lint #[arg(short = 'f', long)] @@ -53,10 +53,12 @@ impl Args { pub fn read(&self) -> Result, Error> { // Check first whether or not the --edit option was supplied. When running from tooling such as // `pre-commit`, stdin exists, so this needs to come first. - if self.edit { - let msg = std::fs::read_to_string("./.git/COMMIT_EDITMSG") - .expect("Failed to read './.git/COMMIT_EDITMSG'"); - return Ok(vec![Message::new(msg)]); + if let Some(edit) = self.edit.clone() { + if edit != "false" { + let msg = std::fs::read_to_string(edit.clone()) + .expect(format!("Failed to read commit message from {}", edit).as_str()); + return Ok(vec![Message::new(msg)]); + } } // Otherwise, check for stdin and use the incoming text buffer from there if so. @@ -68,18 +70,24 @@ impl Args { return Ok(vec![Message::new(buffer)]); } - // And if none of the above, we're expecting to be reading directly from Git... - let config = ReadCommitMessageOptions { - from: self.from.clone(), - path: self.cwd.clone(), - to: self.to.clone(), - }; + if self.from.is_some() || self.from.is_some() { + // Reading directly from Git if from or to is specified. + let config = ReadCommitMessageOptions { + from: self.from.clone(), + path: self.cwd.clone(), + to: self.to.clone(), + }; - let messages = git::read(config) - .iter() - .map(|s| Message::new(s.to_string())) - .collect(); + let messages = git::read(config) + .iter() + .map(|s| Message::new(s.to_string())) + .collect(); - Ok(messages) + return Ok(messages); + } + + let msg = std::fs::read_to_string("./.git/COMMIT_EDITMSG") + .expect("Failed to read commit message from ./.git/COMMIT_EDITMSG"); + return Ok(vec![Message::new(msg)]); } }