-
Notifications
You must be signed in to change notification settings - Fork 225
feat: use tree-sitter for gitcommit #2081
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
Open
Tijs-B
wants to merge
2
commits into
Automattic:master
Choose a base branch
from
Tijs-B:push-xttpxwvuyukp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "harper-git-commit" | ||
| version = "0.68.0" | ||
| edition = "2024" | ||
| description = "The language checker for developers." | ||
| license = "Apache-2.0" | ||
| repository = "https://github.com/automattic/harper" | ||
|
|
||
| [dependencies] | ||
| harper-core = { path = "../harper-core", version = "0.68.0" } | ||
| harper-tree-sitter = { path = "../harper-tree-sitter", version = "0.68.0" } | ||
| tree-sitter-gitcommit = { git = "https://github.com/gbprod/tree-sitter-gitcommit", rev = "a716678c0f00645fed1e6f1d0eb221481dbd6f6d" } | ||
| tree-sitter = "0.25.10" | ||
|
|
||
| [dev-dependencies] | ||
| paste = "1.0.15" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use harper_core::Token; | ||
| use harper_core::parsers::{self, Markdown, MarkdownOptions, Parser}; | ||
| use harper_tree_sitter::TreeSitterMasker; | ||
| use tree_sitter::Node; | ||
|
|
||
| pub struct GitCommitParser { | ||
| /// Used to grab the text nodes, and parse them as markdown. | ||
| inner: parsers::Mask<TreeSitterMasker, Markdown>, | ||
| } | ||
|
|
||
| impl GitCommitParser { | ||
| fn node_condition(n: &Node) -> bool { | ||
| matches!(n.kind(), "subject" | "message_line" | "breaking_change") | ||
| } | ||
|
|
||
| pub fn new(markdown_options: MarkdownOptions) -> Self { | ||
| Self { | ||
| inner: parsers::Mask::new( | ||
| TreeSitterMasker::new(tree_sitter_gitcommit::language(), Self::node_condition), | ||
| Markdown::new(markdown_options), | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Parser for GitCommitParser { | ||
| fn parse(&self, source: &[char]) -> Vec<Token> { | ||
| self.inner.parse(source) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| use harper_core::linting::{LintGroup, Linter}; | ||
| use harper_core::parsers::MarkdownOptions; | ||
| use harper_core::spell::FstDictionary; | ||
| use harper_core::{Dialect, Document}; | ||
| use harper_git_commit::GitCommitParser; | ||
|
|
||
| /// Creates a unit test checking that the linting of a git commit document (in | ||
| /// `tests_sources`) produces the expected number of lints. | ||
| macro_rules! create_test { | ||
| ($filename:ident.txt, $correct_expected:expr) => { | ||
| paste::paste! { | ||
| #[test] | ||
| fn [<lints_ $filename _correctly>](){ | ||
| let source = include_str!( | ||
| concat!( | ||
| "./test_sources/", | ||
| concat!(stringify!($filename), ".txt") | ||
| ) | ||
| ); | ||
|
|
||
| let dict = FstDictionary::curated(); | ||
| let document = Document::new(source, &GitCommitParser::new(MarkdownOptions::default()), &dict); | ||
|
|
||
| let mut linter = LintGroup::new_curated(dict, Dialect::American); | ||
| let lints = linter.lint(&document); | ||
|
|
||
| dbg!(&lints); | ||
| assert_eq!(lints.len(), $correct_expected); | ||
|
|
||
| // Make sure that all generated tokens span real characters | ||
| for token in document.tokens(){ | ||
| assert!(token.span.try_get_content(document.get_source()).is_some()); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| create_test!(simple_commit.txt, 1); | ||
| create_test!(complex_verbose_commit.txt, 2); | ||
| create_test!(conventional_commit.txt, 3); |
26 changes: 26 additions & 0 deletions
26
harper-git-commit/tests/test_sources/complex_verbose_commit.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| This is the the subject | ||
|
|
||
| This is a first line without typos | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good tests! |
||
| # This is a comment with a typooo that should be ignored | ||
| This is a line below the comment with typooos | ||
|
|
||
| # Please enter the commit message for your changes. Lines starting | ||
| # with '#' will be ignored, and an empty message aborts the commit. | ||
| # | ||
| # On branch main | ||
| # | ||
| # Initial commit | ||
| # | ||
| # Changes to be committed: | ||
| #new file: myfile.txt | ||
| # | ||
| # ------------------------ >8 ------------------------ | ||
| # Do not modify or remove the line above. | ||
| # Everything below it will be ignored. | ||
| diff --git a/myfile.txt b/myfile.txt | ||
| new file mode 100644 | ||
| index 0000000..485c415 | ||
| --- /dev/null | ||
| +++ b/myfile.txt | ||
| @@ -0,0 +1 @@ | ||
| +some textt in the file that was added (the typo should be ignored) | ||
17 changes: 17 additions & 0 deletions
17
harper-git-commit/tests/test_sources/conventional_commit.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| feat(stuff): use session-based authentiation | ||
|
|
||
| BREAKING CHANGE: JWT authentication removed. API clients mustt now use | ||
| session cookies instead of Authorization headers with bearer tokens. | ||
|
|
||
| Sessions expire after 24 hours of inactvity. | ||
|
|
||
| Closes: #247 | ||
| Reviewed-by: John Doe <[email protected]> | ||
|
|
||
| # Please enter the commit message for your changes. Lines starting | ||
| # with '#' will be ignored, and an empty message aborts the commit. | ||
| # | ||
| # On branch main | ||
| # Changes to be committed: | ||
| # modified: myfile.txt | ||
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| A simple commit with a typo: comit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, Harper can't include git dependencies.
crates.iodoesn't allow it. Would it be possible for you to upload the crate tocrates.ioand reference that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The crate is already on crates.io, but it does not have the newest commits in the repo unfortunately. I could ask for a release or use the released version there; however, the version there does not have separate tree-sitter nodes for comments in between actual message lines, meaning that I can't remove those comments from the mask.
I'll ask upstream for a new release and version bump on
crates.io.Sorry, I'm not really knowledgeable about the rust packaging ecosystem, and didn't know that git dependencies aren't allowed when publishing on
crates.io.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely no worries. Hopefully they'll release an update.