Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: Use git_exec when check staged files
  • Loading branch information
alt-art committed Oct 9, 2022
commit 9cc0093583ba9a6802a888d8ba52815c4c72f989
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ serde_json = "1.0.79"
clap = { version = "3.1.6", features = ["derive"] }
dirs = "4.0.0"
anyhow = "1.0.56"
colored = "2.0.0"

[dev-dependencies]
assert_fs = "1.0.7"
Expand Down
8 changes: 8 additions & 0 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ pub fn commit(commit_message: &str) -> Result<()> {
.ok_or_else(|| anyhow!("Signal terminated"))?,
);
}

pub fn check_staged_files() -> Result<()> {
let output = git_exec(&["diff", "--cached", "--quiet"])?;
if output.status.code() == Some(0) {
return Err(anyhow!("You have not added anything please do `git add`"));
}
Ok(())
}
21 changes: 4 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ mod commit;
mod commit_message;
mod config;

use anyhow::anyhow;
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

use commit::{check_staged_files, commit, commit_as_hook};
use commit_message::make_message_commit;

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

if Command::new("git")
.args(["diff", "--cached", "--quiet"])
.output()
.expect("failed to execute process")
.status
.code()
== Some(0)
{
return Err(anyhow!(
"You have not added anything please do `git add`".red()
));
}
check_staged_files()?;

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

if opt.hook {
commit::commit_as_hook(&commit_message)?;
commit_as_hook(&commit_message)?;
return Ok(());
}

commit::commit(&commit_message)?;
commit(&commit_message)?;
Ok(())
}