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

Add an option to force capitalizing the subject line #270

Open
novadev94 opened this issue Mar 9, 2022 · 6 comments
Open

Add an option to force capitalizing the subject line #270

novadev94 opened this issue Mar 9, 2022 · 6 comments

Comments

@novadev94
Copy link

This convention is well-known in the community and deemed a good practice in several articles related to Git commit messages, ex. https://cbea.ms/git-commit/#capitalize.

However, it's currently not possible to get it configured, due to Python stock regex module re limitations - we can't have a regex pattern that matches a Unicode uppercase character.

There're 2 possible approaches to this:

  • Implement this as a rule and maybe a default one.

  • Switch to regex module, which has richer regex support with full backward compatibility with the re module.

@sigmavirus24
Copy link
Collaborator

I don't think this must be done with regular expressions but it would be good to add even if it's off by default

@Bearwolves
Copy link

Bearwolves commented Apr 11, 2022

@novadev94 : Why is that not possible?

We are using following regex to check for uppercase character in the description from the conventional commit perspective https://www.conventionalcommits.org/en/v1.0.0/ . I think that's what you are calling subject.

[general]
...
contrib=contrib-title-conventional-commits

[title-match-regex]
# python-style regex that the commit-msg title must match
# Note that the regex can contradict with other rules if not used correctly
# (e.g. title-must-not-contain-word).
regex=^[a-z]+[(a-z)]*[!]*: [A-Z]+.*

[ignore-by-title]
# Ignore certain rules for commits of which the title matches a regex
# E.g. Match commit titles that start with "Release"
regex=^((?:Bump version from (.*) to)|(?:Bump version to)|(?:CHANGELOG\.md))(.*)

# Ignore certain rules, you can reference them by their id or by their full name
# Use 'all' to ignore all rules
ignore=contrib-title-conventional-commits, title-match-regex, body-min-length

What do I miss? :(

One thing to keep in mind, this new rule must not conflict with the contrib rules i.e. the conventional commits.

@novadev94
Copy link
Author

@Bearwolves Yes, I should've called them "title" instead of "subject line" to match gitlint convention.
That conventional commit regex pattern has the same mentioned problem where [A-Z] only matches capital English letters, and doesn't match Ö for example.
That's a non-problem if we force commit messages to be English only, but imo that'd be too restrictive.

@Bearwolves
Copy link

@novadev94 : Got your point. Supporting other language/character shall be allowed, agreed!
Thanks for detailing!

@jorisroovers jorisroovers added the enhancement User-facing feature enhancements label Apr 15, 2022
@jorisroovers
Copy link
Owner

We can add this as a contrib rule.

I’ve quickly implemented this a user-defined rule. Can you please verify this works as expected?

# filename: captilized_title.py
from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle

class CapitalizedTitle(LineRule):
    name = "capitalized-title"
    id = "UL1"
    target = CommitMessageTitle

    def validate(self, title, _commit):
        if (len(title) > 0 and not title[0].isupper()):
            return [ RuleViolation(self.id, "Title not capitalized", title) ]

        return []

Use this .gitlint config:

[general]
extra-path=my/path/captilized_title.py

This would need unit tests and docs. Happy to accept a PR if you’re up for it: https://jorisroovers.com/gitlint/contributing/#contrib-rules

@jorisroovers jorisroovers added this to the 0.18.0 milestone Apr 16, 2022
@jorisroovers jorisroovers added contrib-rule and removed enhancement User-facing feature enhancements labels Aug 4, 2022
@jorisroovers
Copy link
Owner

Bump @novadev94 :-)

@jorisroovers jorisroovers removed this from the 0.18.0 milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

4 participants