Skip to content

Commit

Permalink
feat(cli): add support for --edit and pre-commit (#174)
Browse files Browse the repository at this point in the history
* task: add support for --edit and pre-commit

* fix: typo in 'expect' clause for --edit

Co-authored-by: KeisukeYamashita <19yamashita15@gmail.com>

---------

Co-authored-by: KeisukeYamashita <19yamashita15@gmail.com>
  • Loading branch information
cylewitruk and KeisukeYamashita authored Sep 22, 2023
1 parent 1caadf1 commit cfa41fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit cfa41fc

Please sign in to comment.