Skip to content
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

feat(cli): add support for --edit and pre-commit #174

Merged
merged 3 commits into from
Sep 22, 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
9 changes: 9 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- id: commitlint
name: Assert Conventional Commit Messages
description: 'Asserts that Conventional Commits have been used for all commit messages according to the rules for this repo.'
entry: commitlint --edit
language: rust
stages: [prepare-commit-msg]
pass_filenames: false
require_serial: true
verbose: true
11 changes: 11 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ impl Args {

/// Read commit messages from stdin.
pub fn read(&self) -> Result<Vec<Message>, Error> {
// Check first whether or not the --edit option was supplied. When running from tooling such as
// `pre-commit`, stdin exists, so this needs to come first.
if self.edit {
let msg = std::fs::read_to_string("./.git/COMMIT_EDITMSG")
.expect("Failed to read './.git/COMMIT_EDITMSG'");
return Ok(vec![Message::new(msg)]);
}

// Otherwise, check for stdin and use the incoming text buffer from there if so.
if self.has_stdin() {
let mut buffer = String::new();
stdin()
Expand All @@ -59,11 +68,13 @@ impl Args {
return Ok(vec![Message::new(buffer)]);
}

// And if none of the above, we're expecting to be reading directly from Git...
let config = ReadCommitMessageOptions {
from: self.from.clone(),
path: self.cwd.clone(),
to: self.to.clone(),
};

let messages = git::read(config)
.iter()
.map(|s| Message::new(s.to_string()))
Expand Down
Loading