Skip to content

Commit c234e90

Browse files
fix(cli): edit option accepts argument (#376)
* fix(cli): edit option accepts argument Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> * fix(cli): remove redundunt return in last line of the func Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> * fix(cli): refactor based on coderabbitai Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --------- Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
1 parent 893e009 commit c234e90

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/args.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct Args {
2626

2727
/// Read last commit from the specified file or fallbacks to ./.git/COMMIT_EDITMSG
2828
#[arg(short = 'e', long)]
29-
pub edit: bool,
29+
pub edit: Option<String>,
3030

3131
/// Lower end of the commit range to lint
3232
#[arg(short = 'f', long)]
@@ -53,10 +53,12 @@ impl Args {
5353
pub fn read(&self) -> Result<Vec<Message>, Error> {
5454
// Check first whether or not the --edit option was supplied. When running from tooling such as
5555
// `pre-commit`, stdin exists, so this needs to come first.
56-
if self.edit {
57-
let msg = std::fs::read_to_string("./.git/COMMIT_EDITMSG")
58-
.expect("Failed to read './.git/COMMIT_EDITMSG'");
59-
return Ok(vec![Message::new(msg)]);
56+
if let Some(edit) = self.edit.as_deref() {
57+
if edit != "false" {
58+
let msg = std::fs::read_to_string(edit)
59+
.expect(format!("Failed to read commit message from {}", edit).as_str());
60+
return Ok(vec![Message::new(msg)]);
61+
}
6062
}
6163

6264
// Otherwise, check for stdin and use the incoming text buffer from there if so.
@@ -68,18 +70,30 @@ impl Args {
6870
return Ok(vec![Message::new(buffer)]);
6971
}
7072

71-
// And if none of the above, we're expecting to be reading directly from Git...
72-
let config = ReadCommitMessageOptions {
73-
from: self.from.clone(),
74-
path: self.cwd.clone(),
75-
to: self.to.clone(),
76-
};
73+
if self.from.is_some() || self.to.is_some() {
74+
// Reading directly from Git if from or to is specified.
75+
let config = ReadCommitMessageOptions {
76+
from: self.from.clone(),
77+
path: self.cwd.clone(),
78+
to: self.to.clone(),
79+
};
7780

78-
let messages = git::read(config)
79-
.iter()
80-
.map(|s| Message::new(s.to_string()))
81-
.collect();
81+
let messages = git::read(config)
82+
.iter()
83+
.map(|s| Message::new(s.to_string()))
84+
.collect();
8285

83-
Ok(messages)
86+
return Ok(messages);
87+
}
88+
89+
let default_path = std::path::PathBuf::from(".git").join("COMMIT_EDITMSG");
90+
let msg = std::fs::read_to_string(&default_path).expect(
91+
format!(
92+
"Failed to read commit message from {}",
93+
default_path.display()
94+
)
95+
.as_str(),
96+
);
97+
Ok(vec![Message::new(msg)])
8498
}
8599
}

0 commit comments

Comments
 (0)