Skip to content

New configuration pre-commit #17

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 3 commits into from
Apr 27, 2023
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
44 changes: 44 additions & 0 deletions commit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"config": {
"subject_separator": ": ",
"scope_prefix": "(",
"scope_suffix": ")",
"pre_commit": "./pre-commit.sh"
},
"commit_types": [
{ "name": "feat", "description": "A new feature" },
{ "name": "fix", "description": "A bug fix" },
{ "name": "docs", "description": "Documentation only changes" },
{
"name": "style",
"description": "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)"
},
{
"name": "refactor",
"description": "A code change that neither fixes a bug nor adds a feature"
},
{
"name": "perf",
"description": "A code change that improves performance"
},
{
"name": "test",
"description": "Adding missing tests or correcting existing tests"
},
{
"name": "chore",
"description": "Other changes that don't modify src or test files"
}
],
"commit_scopes": [
{ "name": "custom", "description": "Custom scope" },
{ "name": "none", "description": "No scope" }
],
"msg": {
"commit_type": "What type of commit you will made?",
"commit_scope": "What scope of commit you will made? (Optional)",
"commit_description": "Write a SHORT, IMPERATIVE tense description of the change:",
"commit_body": "Provide a LONGER description of the change (Optional):",
"commit_footer": "List any ISSUES CLOSED by this change E.g.: #31, #34 (Optional):"
}
}
17 changes: 17 additions & 0 deletions pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/env bash

YELLOW='\e[1;33m'
NC='\e[0m'

if ! cargo fmt -- --check; then
printf "%bHelp: run 'cargo fmt' to fix formatting issues%b" "$YELLOW" "$NC"
exit 1
fi
if ! cargo clippy --all-targets --all-features -- -D warnings; then
printf "%bHelp: run 'cargo clippy' and fix the issues%b" "$YELLOW" "$NC"
exit 1
fi
if ! cargo test; then
printf "%bHelp: run 'cargo test' and fix the issues%b" "$YELLOW" "$NC"
exit 1
fi
18 changes: 18 additions & 0 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,21 @@ pub fn write_cached_commit(commit_message: &str) -> Result<()> {
fs::write(get_git_path()?.join("COMMIT_EDITMSG"), commit_message)?;
Ok(())
}

pub fn pre_commit_check(pre_commit_command: Option<String>, message: &str) -> Result<()> {
if let Some(command) = pre_commit_command {
println!("Running pre-commit command...");
let output = Command::new(command).env("MSG", message).output()?;
let message = String::from_utf8_lossy(&output.stdout);
println!("{message}");
if !output.status.success() {
let error_message = String::from_utf8_lossy(&output.stderr);
return Err(anyhow!(
"Pre-commit command failed with code: {}\n{}",
output.status.code().unwrap_or_default(),
error_message
));
}
}
Ok(())
}
2 changes: 2 additions & 0 deletions src/commit_message/message_build/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn message_builder_config_test() {
subject_separator: ": ".to_owned(),
type_prefix: None,
type_suffix: None,
pre_commit: None,
};

assert_eq!(
Expand Down Expand Up @@ -57,6 +58,7 @@ fn message_builder_test() {
subject_separator: ": ".to_owned(),
type_prefix: None,
type_suffix: None,
pre_commit: None,
};
let mut builder = MessageBuilder::new(config);

Expand Down
1 change: 1 addition & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Config {
pub subject_separator: String,
pub scope_prefix: String,
pub scope_suffix: String,
pub pre_commit: Option<String>,
}

impl Display for Type {
Expand Down
17 changes: 17 additions & 0 deletions src/config/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env::{current_dir, set_current_dir};

use super::*;
use assert_fs::prelude::*;

Expand All @@ -11,6 +13,8 @@ fn select_custom_config_path_test() -> Result<()> {
let selected_config_path = select_custom_config_path(config_path.clone())?;
assert_eq!(config_path.unwrap().to_str(), selected_config_path.to_str());

let last_dir = current_dir()?;
set_current_dir(temp_dir.path())?;
let config_path_default = dirs::config_dir().unwrap().join("commit/commit.json");
let selected_config_path = select_custom_config_path(None)?;
assert_eq!(selected_config_path.to_str(), config_path_default.to_str());
Expand All @@ -20,16 +24,23 @@ fn select_custom_config_path_test() -> Result<()> {
Err(err) => assert_eq!(err.to_string(), "Config file does not exist: "),
_ => unreachable!(),
}
set_current_dir(last_dir)?;
temp_dir.close()?;
Ok(())
}

#[test]
fn get_config_path_test() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let last_dir = current_dir()?;
set_current_dir(temp_dir.path())?;
let config_file = dirs::config_dir()
.ok_or_else(|| anyhow!("Could not find config directory"))?
.join("commit/commit.json");
let config_path = get_config_path();
assert_eq!(config_file.to_str(), config_path?.to_str());
set_current_dir(last_dir)?;
temp_dir.close()?;
Ok(())
}

Expand All @@ -46,16 +57,22 @@ fn get_config_path_content_test() -> Result<()> {
config_file.write_str(expected)?;
let content = get_config_path_content(config_path)?;
assert_eq!(content, expected);
temp_dir.close()?;
Ok(())
}

#[test]
fn get_pattern_test() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let last_dir = current_dir()?;
set_current_dir(temp_dir.path())?;
let pattern = get_pattern(None)?;
assert_eq!(pattern.config.type_prefix, None);
assert_eq!(pattern.config.type_suffix, None);
assert_eq!(pattern.config.subject_separator, ": ");
assert_eq!(pattern.config.scope_prefix, "(");
assert_eq!(pattern.config.scope_suffix, ")");
set_current_dir(last_dir)?;
temp_dir.close()?;
Ok(())
}
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use clap::Parser;
use std::io::Write;
use std::path::PathBuf;

use commit::{check_staged_files, commit, read_cached_commit, write_cached_commit};
use commit::{
check_staged_files, commit, pre_commit_check, read_cached_commit, write_cached_commit,
};
use commit_message::make_message_commit;

const DEFAULT_CONFIG_FILE: &str = include_str!("../commit-default.json");
Expand Down Expand Up @@ -56,9 +58,12 @@ fn main() -> Result<()> {
}

let pattern = config::get_pattern(args.config)?;
let commit_message = make_message_commit(pattern)?;

let commit_message = make_message_commit(pattern.clone())?;
write_cached_commit(&commit_message)?;

pre_commit_check(pattern.config.pre_commit, &commit_message)?;

if args.hook {
return Ok(());
}
Expand Down