Skip to content

Commit 9cc0093

Browse files
committed
refactor: Use git_exec when check staged files
1 parent e74efd9 commit 9cc0093

File tree

4 files changed

+12
-30
lines changed

4 files changed

+12
-30
lines changed

Cargo.lock

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ serde_json = "1.0.79"
2020
clap = { version = "3.1.6", features = ["derive"] }
2121
dirs = "4.0.0"
2222
anyhow = "1.0.56"
23-
colored = "2.0.0"
2423

2524
[dev-dependencies]
2625
assert_fs = "1.0.7"

src/commit.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ pub fn commit(commit_message: &str) -> Result<()> {
3838
.ok_or_else(|| anyhow!("Signal terminated"))?,
3939
);
4040
}
41+
42+
pub fn check_staged_files() -> Result<()> {
43+
let output = git_exec(&["diff", "--cached", "--quiet"])?;
44+
if output.status.code() == Some(0) {
45+
return Err(anyhow!("You have not added anything please do `git add`"));
46+
}
47+
Ok(())
48+
}

src/main.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ mod commit;
1111
mod commit_message;
1212
mod config;
1313

14-
use anyhow::anyhow;
1514
use anyhow::Result;
1615
use clap::Parser;
17-
use colored::Colorize;
1816
use std::io::Write;
1917
use std::path::PathBuf;
20-
use std::process::Command;
2118

19+
use commit::{check_staged_files, commit, commit_as_hook};
2220
use commit_message::make_message_commit;
2321

2422
const DEFAULT_CONFIG_FILE: &str = include_str!("../commit-default.json");
@@ -46,18 +44,7 @@ fn main() -> Result<()> {
4644
std::env::set_current_dir(current_dir)?;
4745
}
4846

49-
if Command::new("git")
50-
.args(["diff", "--cached", "--quiet"])
51-
.output()
52-
.expect("failed to execute process")
53-
.status
54-
.code()
55-
== Some(0)
56-
{
57-
return Err(anyhow!(
58-
"You have not added anything please do `git add`".red()
59-
));
60-
}
47+
check_staged_files()?;
6148

6249
let opt = Opt::parse();
6350
if opt.init {
@@ -70,10 +57,10 @@ fn main() -> Result<()> {
7057
let commit_message = make_message_commit(pattern)?;
7158

7259
if opt.hook {
73-
commit::commit_as_hook(&commit_message)?;
60+
commit_as_hook(&commit_message)?;
7461
return Ok(());
7562
}
7663

77-
commit::commit(&commit_message)?;
64+
commit(&commit_message)?;
7865
Ok(())
7966
}

0 commit comments

Comments
 (0)