Skip to content

Commit

Permalink
feat(check): check from stdin if not a tty
Browse files Browse the repository at this point in the history
This is usefull to use convco in a commit-msg git hook.

```
printf '%s' "$1" | convco check
```

Refs: #100
  • Loading branch information
hdevalke committed Jan 12, 2023
1 parent 8ab9c3f commit f53b02c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ctrlc = "3.2.3"
dialoguer = "0.10.2"
git2 = { version = "0.15.0", default-features = false, features = [ "zlib-ng-compat" ] }
handlebars = "4.3.5"
is-terminal = "0.4.2"
regex = "1.7.0"
semver = "1.0.14"
serde = { version = "1.0.147", features = ["derive"] }
Expand Down
32 changes: 22 additions & 10 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::io::{stdin, Read};

use conventional::Config;
use git2::{Commit, Repository};
use is_terminal::IsTerminal;

use crate::{
cli::CheckCommand,
Expand Down Expand Up @@ -45,16 +48,6 @@ impl Command for CheckCommand {
if self.first_parent {
config.first_parent = true;
}
let repo = Repository::open_from_env()?;
let mut revwalk = repo.revwalk()?;
if config.first_parent {
revwalk.simplify_first_parent()?;
}
if self.rev.contains("..") {
revwalk.push_range(self.rev.as_str())?;
} else {
revwalk.push_ref(self.rev.as_str())?;
}

let mut total = 0;
let mut fail = 0;
Expand All @@ -71,6 +64,25 @@ impl Command for CheckCommand {

let Config { merges, .. } = config;

if !stdin().is_terminal() {
let mut stdin = stdin().lock();
let mut commit_msg = String::new();
stdin.read_to_string(&mut commit_msg)?;
parser.parse(&commit_msg)?;
return Ok(());
}

let repo = Repository::open_from_env()?;
let mut revwalk = repo.revwalk()?;
if config.first_parent {
revwalk.simplify_first_parent()?;
}
if self.rev.contains("..") {
revwalk.push_range(self.rev.as_str())?;
} else {
revwalk.push_ref(self.rev.as_str())?;
}

for commit in revwalk
.flatten()
.flat_map(|oid| repo.find_commit(oid).ok())
Expand Down

0 comments on commit f53b02c

Please sign in to comment.