Skip to content

Refatoring repeated code #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
77 changes: 35 additions & 42 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,46 @@ use anyhow::{anyhow, Result};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::{exit, Command};
use std::process::{exit, Command, Output};

pub fn commit_as_hook(commit_message: &str) -> Result<()> {
let output = Command::new("git")
.args(&["rev-parse", "--absolute-git-dir"])
.output();
pub fn git_exec(args: &[&str]) -> Result<Output> {
let output = Command::new("git").args(args).output();
match output {
Ok(output) => {
if !output.status.success() {
return Err(anyhow!("Could not get git directory"));
}
let git_dir = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
let commit_file_path = git_dir.join("COMMIT_EDITMSG");
fs::write(commit_file_path, commit_message)?;
}
Err(e) => {
return Err(anyhow!(
"Failed to run git. Make sure git is installed\nAdditional info: {}",
e
));
}
Ok(output) => Ok(output),
Err(e) => Err(anyhow!(
"Failed to run git. Make sure git is installed\nAdditional info: {}",
e
)),
}
}

pub fn commit_as_hook(commit_message: &str) -> Result<()> {
let output = git_exec(&["rev-parse", "--absolute-git-dir"])?;
if !output.status.success() {
return Err(anyhow!("Could not get git directory"));
}
let git_dir = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
let commit_file_path = git_dir.join("COMMIT_EDITMSG");
fs::write(commit_file_path, commit_message)?;
Ok(())
}

pub fn commit(commit_message: &str) -> Result<()> {
let output = Command::new("git")
.arg("commit")
.arg("-m")
.arg(commit_message)
.output();
match output {
Ok(output) => {
std::io::stdout().write_all(&output.stdout)?;
std::io::stderr().write_all(&output.stderr)?;
exit(
output
.status
.code()
.ok_or_else(|| anyhow!("Could not get exit code"))?,
);
}
Err(e) => {
return Err(anyhow::anyhow!(
"Failed to run git. Make sure git is installed\nAdditional info: {}",
e
));
}
};
let output = git_exec(&["commit", "-m", commit_message])?;
std::io::stdout().write_all(&output.stdout)?;
std::io::stderr().write_all(&output.stderr)?;
exit(
output
.status
.code()
.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(())
}