From 95fda0bd3bcc4b1e9d3dd7cf113068f07401781d Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Mon, 19 Feb 2024 17:55:30 +0100 Subject: [PATCH 1/9] test(parser): add more unit tests for parsing --- src/commit.pest | 2 +- src/parser.rs | 10 ++++++++ ... the end (stdio input adds a newline).snap | 24 +++++++++++++++++++ ...s__subject with whitespace at the end.snap | 24 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/snapshots/commitguard__parser__tests__body and footer missing with newline at the end (stdio input adds a newline).snap create mode 100644 src/snapshots/commitguard__parser__tests__subject with whitespace at the end.snap diff --git a/src/commit.pest b/src/commit.pest index 7ae43e2..61036d9 100644 --- a/src/commit.pest +++ b/src/commit.pest @@ -1,4 +1,4 @@ -commit = { SOI ~ header ~ body? ~ footer? ~ EOI } +commit = { SOI ~ header ~ body? ~ footer? ~ NEWLINE* ~ EOI } header = ${ commit_type ~ scope_with_braces? ~ "!"? ~ ":" ~ WHITE_SPACE+ ~ subject } diff --git a/src/parser.rs b/src/parser.rs index b41af0f..6fe6381 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -169,6 +169,16 @@ mod tests { commit: String::from("feat(nice): add cool feature"), want_err: false, }, + TestConfig { + name: String::from("body and footer missing with newline at the end (stdio input adds a newline)"), + commit: String::from("feat(nice): add cool feature\n"), + want_err: false, + }, + TestConfig { + name: String::from("subject with whitespace at the end"), + commit: String::from("feat(nice): add cool feature \t "), + want_err: false, + }, TestConfig { name: String::from("footer missing"), commit: String::from("feat(nice): add cool feature\n\nsome body"), diff --git a/src/snapshots/commitguard__parser__tests__body and footer missing with newline at the end (stdio input adds a newline).snap b/src/snapshots/commitguard__parser__tests__body and footer missing with newline at the end (stdio input adds a newline).snap new file mode 100644 index 0000000..c1b07ab --- /dev/null +++ b/src/snapshots/commitguard__parser__tests__body and footer missing with newline at the end (stdio input adds a newline).snap @@ -0,0 +1,24 @@ +--- +source: src/parser.rs +expression: result +--- +header: + input: "feat(nice): add cool feature" + start: 0 + end: 28 +body: ~ +footer: ~ +commit_type: + input: feat + start: 0 + end: 4 +scope: + input: nice + start: 5 + end: 9 +subject: + input: add cool feature + start: 12 + end: 28 +raw: "feat(nice): add cool feature\n" + diff --git a/src/snapshots/commitguard__parser__tests__subject with whitespace at the end.snap b/src/snapshots/commitguard__parser__tests__subject with whitespace at the end.snap new file mode 100644 index 0000000..96add45 --- /dev/null +++ b/src/snapshots/commitguard__parser__tests__subject with whitespace at the end.snap @@ -0,0 +1,24 @@ +--- +source: src/parser.rs +expression: result +--- +header: + input: "feat(nice): add cool feature \t " + start: 0 + end: 31 +body: ~ +footer: ~ +commit_type: + input: feat + start: 0 + end: 4 +scope: + input: nice + start: 5 + end: 9 +subject: + input: "add cool feature \t " + start: 12 + end: 31 +raw: "feat(nice): add cool feature \t " + From dff7938c0db889222a31bdc5be0c253d87db7fb4 Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Mon, 19 Feb 2024 18:19:18 +0100 Subject: [PATCH 2/9] wip(cli): start with clap and reading from stdin --- Cargo.lock | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 32 +++++++++++--- src/parser.rs | 5 +++ 4 files changed, 146 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aeb5921..ca14b9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,54 @@ dependencies = [ "version_check", ] +[[package]] +name = "anstream" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b09b5178381e0874812a9b157f7fe84982617e48f71f4e3235482775e5b540" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "async-trait" version = "0.1.77" @@ -105,10 +153,57 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "commitguard" version = "0.1.0" dependencies = [ + "clap", "config", "insta", "miette", @@ -235,6 +330,12 @@ dependencies = [ "ahash", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.4" @@ -576,6 +677,12 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" +[[package]] +name = "strsim" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" + [[package]] name = "supports-color" version = "2.1.0" @@ -695,6 +802,12 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 4065069..39f673b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ keywords = ["cli", "commitlint", "git", "lint", "commit"] categories = ["command-line-utilities", "development-tools"] [dependencies] +clap = { version = "4.5.1", features = ["derive", "string"] } config = "0.13.4" miette = { version = "5.10.0", features = ["fancy"] } pest = "2.7.6" diff --git a/src/main.rs b/src/main.rs index bfe0812..0cc29f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,36 @@ mod parser; mod rules; -use std::process::ExitCode; +use std::{env::current_dir, io::{stdin, Read}, path::PathBuf, process::ExitCode}; +use clap::Parser; use miette::GraphicalReportHandler; use parser::parse_commit; +/// Commit lint +#[derive(clap::Parser, Debug)] +#[command(version, about, long_about = None)] +struct Cli { + /// Path to the config file + #[arg(short, long, default_value = "commitlint.config")] + config_path: PathBuf, + + /// Current working directory + #[arg(long, default_value = current_dir().unwrap_or_else(|_e| PathBuf::from("/")).into_os_string())] + cwd: PathBuf, +} + fn main() -> ExitCode { - let commit_message = - "feat(nice): add cool feature\n\nsome body\n\nsecond body line\n\nsome footer"; + // let commit_message = + // "feat(nice): add cool feature\n\nsome body\n\nsecond body line\n\nsome footer"; - let commit = parse_commit(&commit_message); - println!("{:#?}", commit); + // let commit = parse_commit(&commit_message); + // println!("{:#?}", commit); + + // read commit from stdin + let mut buffer = String::new(); + stdin().read_to_string(&mut buffer).unwrap_or_else(|_e| 0); + let commit = parse_commit(&buffer); let lint_result = rules::run(&commit); let report_handler = GraphicalReportHandler::new(); @@ -42,6 +61,9 @@ fn main() -> ExitCode { lint_result.errors_len() ); + let args = Cli::parse(); + println!("{:?}", args); + if lint_result.has_errors() { return ExitCode::FAILURE; } diff --git a/src/parser.rs b/src/parser.rs index 6fe6381..484a4b1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -209,6 +209,11 @@ mod tests { commit: String::from("Added a cool new feature"), want_err: true, }, + TestConfig { + name: String::from("no text"), + commit: String::new(), + want_err: true, + }, ]; for test_config in test_configs { From 9e5ccf4890985bbb57ecccaf9461d1974f1bcd62 Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Mon, 19 Feb 2024 19:19:35 +0100 Subject: [PATCH 3/9] fix: apply clippy suggestions --- src/main.rs | 2 +- src/parser.rs | 6 +++--- src/rules/mod.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0cc29f9..10f864b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ fn main() -> ExitCode { // read commit from stdin let mut buffer = String::new(); - stdin().read_to_string(&mut buffer).unwrap_or_else(|_e| 0); + stdin().read_to_string(&mut buffer).unwrap_or(0); let commit = parse_commit(&buffer); let lint_result = rules::run(&commit); diff --git a/src/parser.rs b/src/parser.rs index 484a4b1..a351857 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -84,12 +84,12 @@ pub fn parse_commit(commit_msg: &str) -> Commit { println!("{:#?}", pairs); let mut commit = Commit { - header: CommitSpan::new(&"", 0, 0), + header: CommitSpan::new("", 0, 0), body: None, footer: None, - commit_type: CommitSpan::new(&"", 0, 0), + commit_type: CommitSpan::new("", 0, 0), scope: None, - subject: CommitSpan::new(&"", 0, 0), + subject: CommitSpan::new("", 0, 0), raw: String::from(""), }; diff --git a/src/rules/mod.rs b/src/rules/mod.rs index e2aef71..1882c36 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -183,7 +183,7 @@ pub fn run(commit: &Commit) -> LintResult { warnings: None, }; for rule in rules { - if let Some(report) = rule.run(&commit) { + if let Some(report) = rule.run(commit) { match report.severity() { Some(miette::Severity::Error) => { if lint_result.errors.is_none() { From 6d4c8b39ffcf87af474616b3ae920c27a61336ae Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Wed, 21 Feb 2024 21:31:33 +0100 Subject: [PATCH 4/9] build: add more checks and fix clippy hints --- .github/workflows/ci.yml | 19 +++++----- src/main.rs | 7 +++- src/parser.rs | 75 ++++++++++++++++++++-------------------- src/rules/mod.rs | 4 +-- 4 files changed, 56 insertions(+), 49 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 31000a2..94f311c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,22 +1,23 @@ -name: Rust +name: CI on: push: - branches: [ "main" ] + branches: ["*"] pull_request: - branches: [ "main" ] + branches: ["main"] env: CARGO_TERM_COLOR: always jobs: build: - runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose + - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose + - name: Lint + run: cargo clippy && cargo fmt --check + - name: Run tests + run: cargo test --verbose diff --git a/src/main.rs b/src/main.rs index 10f864b..8f98e19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,12 @@ mod parser; mod rules; -use std::{env::current_dir, io::{stdin, Read}, path::PathBuf, process::ExitCode}; +use std::{ + env::current_dir, + io::{stdin, Read}, + path::PathBuf, + process::ExitCode, +}; use clap::Parser; use miette::GraphicalReportHandler; diff --git a/src/parser.rs b/src/parser.rs index a351857..2d8ecde 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,3 +1,5 @@ +use std::fmt; + use serde::Serialize; use pest::{Parser, Span}; @@ -28,10 +30,6 @@ impl<'a> CommitSpan<'a> { } } - pub fn to_string(&self) -> String { - self.input.to_string() - } - pub fn start(&self) -> usize { self.start } @@ -41,6 +39,12 @@ impl<'a> CommitSpan<'a> { } } +impl fmt::Display for CommitSpan<'_> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.input) + } +} + #[derive(Debug, Serialize)] pub struct Commit<'a> { /// The complete header of the commit message including the type, scope and subject @@ -94,44 +98,39 @@ pub fn parse_commit(commit_msg: &str) -> Commit { }; for pair in pairs { - match pair.as_rule() { - Rule::commit => { - commit.raw = pair.as_str().to_string(); - - for inner_pair in pair.into_inner() { - match inner_pair.as_rule() { - Rule::header => { - commit.header = CommitSpan::from(inner_pair.as_span()); - - for header_pair in inner_pair.into_inner() { - match header_pair.as_rule() { - Rule::commit_type => { - commit.commit_type = CommitSpan::from(header_pair.as_span()) - } - Rule::scope => { - commit.scope = Some(CommitSpan::from(header_pair.as_span())) - } - Rule::subject => { - commit.subject = CommitSpan::from(header_pair.as_span()) - } - _ => {} + if let Rule::commit = pair.as_rule() { + commit.raw = pair.as_str().to_string(); + + for inner_pair in pair.into_inner() { + match inner_pair.as_rule() { + Rule::header => { + commit.header = CommitSpan::from(inner_pair.as_span()); + + for header_pair in inner_pair.into_inner() { + match header_pair.as_rule() { + Rule::commit_type => { + commit.commit_type = CommitSpan::from(header_pair.as_span()) } + Rule::scope => { + commit.scope = Some(CommitSpan::from(header_pair.as_span())) + } + Rule::subject => { + commit.subject = CommitSpan::from(header_pair.as_span()) + } + _ => {} } } - Rule::body => commit.body = Some(CommitSpan::from(inner_pair.as_span())), - Rule::footer => { - commit.footer = Some(CommitSpan::from(inner_pair.as_span())) - } - Rule::commit_type => { - commit.commit_type = CommitSpan::from(inner_pair.as_span()) - } - Rule::scope => commit.scope = Some(CommitSpan::from(inner_pair.as_span())), - Rule::subject => commit.subject = CommitSpan::from(inner_pair.as_span()), - _ => {} } + Rule::body => commit.body = Some(CommitSpan::from(inner_pair.as_span())), + Rule::footer => commit.footer = Some(CommitSpan::from(inner_pair.as_span())), + Rule::commit_type => { + commit.commit_type = CommitSpan::from(inner_pair.as_span()) + } + Rule::scope => commit.scope = Some(CommitSpan::from(inner_pair.as_span())), + Rule::subject => commit.subject = CommitSpan::from(inner_pair.as_span()), + _ => {} } } - _ => {} } } @@ -170,7 +169,9 @@ mod tests { want_err: false, }, TestConfig { - name: String::from("body and footer missing with newline at the end (stdio input adds a newline)"), + name: String::from( + "body and footer missing with newline at the end (stdio input adds a newline)", + ), commit: String::from("feat(nice): add cool feature\n"), want_err: false, }, diff --git a/src/rules/mod.rs b/src/rules/mod.rs index 1882c36..b735ffe 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -92,8 +92,8 @@ struct RulesDetails { scope_enum: EnumOpts, #[serde(rename = "scope-max-length")] scope_max_length: LengthOpts, - #[serde(rename = "scope-case")] - scope_case: CaseOpts, + // #[serde(rename = "scope-case")] + // scope_case: CaseOpts, } /// Config From 94439df0e11c0bdde2417984e5b7ebd7f2c72dec Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Wed, 21 Feb 2024 21:35:34 +0100 Subject: [PATCH 5/9] build: update checkout action --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 94f311c..dd37b08 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Build run: cargo build --verbose - name: Lint From 4a5c9c845e273c2af15c165ddab670b4e05b60f0 Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Wed, 6 Mar 2024 21:23:46 +0100 Subject: [PATCH 6/9] test(parser): add new tests --- src/parser.rs | 10 +++++++ ...d__parser__tests__multiple body lines.snap | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/snapshots/commitguard__parser__tests__multiple body lines.snap diff --git a/src/parser.rs b/src/parser.rs index 2d8ecde..73cbd60 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -185,6 +185,11 @@ mod tests { commit: String::from("feat(nice): add cool feature\n\nsome body"), want_err: false, }, + TestConfig { + name: String::from("multiple body lines"), + commit: String::from("feat(nice): add cool feature\n\nsome body\nnext body line\n\nthe real footer"), + want_err: false, + }, TestConfig { name: String::from("breaking change after type"), commit: String::from("feat!: add cool feature\n\nsome body"), @@ -195,6 +200,11 @@ mod tests { commit: String::from("feat(nice)!: add cool feature\n\nsome body"), want_err: false, }, + TestConfig { + name: String::from("only one newline after header"), + commit: String::from("feat(nice): add cool feature\nsome body"), + want_err: true, + }, TestConfig { name: String::from("type missing"), commit: String::from("add cool feature\n\nsome body\n\nsome footer"), diff --git a/src/snapshots/commitguard__parser__tests__multiple body lines.snap b/src/snapshots/commitguard__parser__tests__multiple body lines.snap new file mode 100644 index 0000000..c554543 --- /dev/null +++ b/src/snapshots/commitguard__parser__tests__multiple body lines.snap @@ -0,0 +1,30 @@ +--- +source: src/parser.rs +expression: result +--- +header: + input: "feat(nice): add cool feature" + start: 0 + end: 28 +body: + input: "\n\nsome body\nnext body line" + start: 28 + end: 54 +footer: + input: "\n\nthe real footer" + start: 54 + end: 71 +commit_type: + input: feat + start: 0 + end: 4 +scope: + input: nice + start: 5 + end: 9 +subject: + input: add cool feature + start: 12 + end: 28 +raw: "feat(nice): add cool feature\n\nsome body\nnext body line\n\nthe real footer" + From 29f96ae65c00589516d94476853fb0e2d50f2191 Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Wed, 6 Mar 2024 21:24:00 +0100 Subject: [PATCH 7/9] docs: update README with the usage section --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 361b4ac..455855b 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,8 @@ cargo install commitguard ## Usage -> Not quite yet, but it will be something like this: - ```sh -echo "foo" | commitguard +echo "feat(myscope): add new feature" | commitguard ``` ## Todos/Ideas: From d8dfbf17584dce318c5d6a1b0bb395aa45f60fc7 Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Wed, 6 Mar 2024 21:26:43 +0100 Subject: [PATCH 8/9] fix(parser): fix format --- src/parser.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index 73cbd60..3efeb2e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -187,7 +187,9 @@ mod tests { }, TestConfig { name: String::from("multiple body lines"), - commit: String::from("feat(nice): add cool feature\n\nsome body\nnext body line\n\nthe real footer"), + commit: String::from( + "feat(nice): add cool feature\n\nsome body\nnext body line\n\nthe real footer", + ), want_err: false, }, TestConfig { From 6775f2fe95140e9a323632163a36735149c90bd6 Mon Sep 17 00:00:00 2001 From: Matthias Zaunseder Date: Wed, 6 Mar 2024 21:55:43 +0100 Subject: [PATCH 9/9] feat: use CLI args to get the config path --- src/main.rs | 17 ++++++----------- src/rules/mod.rs | 9 +++------ 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8f98e19..f795497 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,8 +17,8 @@ use parser::parse_commit; #[command(version, about, long_about = None)] struct Cli { /// Path to the config file - #[arg(short, long, default_value = "commitlint.config")] - config_path: PathBuf, + #[arg(short, long, default_value = "commitguard.config")] + config_name: String, /// Current working directory #[arg(long, default_value = current_dir().unwrap_or_else(|_e| PathBuf::from("/")).into_os_string())] @@ -26,18 +26,16 @@ struct Cli { } fn main() -> ExitCode { - // let commit_message = - // "feat(nice): add cool feature\n\nsome body\n\nsecond body line\n\nsome footer"; - - // let commit = parse_commit(&commit_message); - // println!("{:#?}", commit); + let args = Cli::parse(); // read commit from stdin let mut buffer = String::new(); stdin().read_to_string(&mut buffer).unwrap_or(0); let commit = parse_commit(&buffer); - let lint_result = rules::run(&commit); + let config_path = args.cwd.join(args.config_name); + let lint_result = rules::run(&commit, config_path); + let report_handler = GraphicalReportHandler::new(); if lint_result.has_warnings() { @@ -66,9 +64,6 @@ fn main() -> ExitCode { lint_result.errors_len() ); - let args = Cli::parse(); - println!("{:?}", args); - if lint_result.has_errors() { return ExitCode::FAILURE; } diff --git a/src/rules/mod.rs b/src/rules/mod.rs index b735ffe..6cbc30c 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -1,4 +1,4 @@ -use std::path; +use std::path::PathBuf; use config::Config; use serde::Deserialize; @@ -139,10 +139,7 @@ impl LintResult { } } -pub fn run(commit: &Commit) -> LintResult { - let pwd = std::env::current_dir().unwrap_or_else(|_e| path::PathBuf::from("/")); - println!("Current directory: {}", pwd.display()); - +pub fn run(commit: &Commit, config_path: PathBuf) -> LintResult { let settings = Config::builder() .add_source(config::File::from_str( r#" @@ -156,7 +153,7 @@ pub fn run(commit: &Commit) -> LintResult { config::FileFormat::Toml, )) // Source can be `commitlint.config.toml` or `commitlint.config.json`` - .add_source(config::File::from(pwd.join("commitguard.config")).required(false)) + .add_source(config::File::from(config_path).required(false)) .build() .unwrap();