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

Is there a way to get branch name in a user-defined rule? #108

Closed
and-semakin opened this issue Nov 6, 2019 · 5 comments
Closed

Is there a way to get branch name in a user-defined rule? #108

and-semakin opened this issue Nov 6, 2019 · 5 comments
Labels
enhancement User-facing feature enhancements question

Comments

@and-semakin
Copy link

By convention in our company we should put Jira ticket ID in the beginning of branch name (for example JIRA-123_do_something_great) and also we should put ticket id in commit message. I would like to check if they are the same. For this I need to get branch name inside of validate method of the rule. But I don't see that there is some property in commit or context objects that would help me. Do I miss something or gitlint is not able to help me in this case?

@jorisroovers
Copy link
Owner

So gitlint doesn't provide a way to do this out of the box, but I might have a workaround using the non-public gitlint.git._git function.

from gitlint.rules import CommitRule, RuleViolation
from gitlint.options import IntOption
from gitlint.git import _git

class CheckBranchRule(CommitRule):
    name = "branch-name-match-title"
    id = "UC1"

    def validate(self, commit):
        print("title", commit.message.title)

        # See https://stackoverflow.com/a/12142066/381010
        print _git("rev-parse",  "--abbrev-ref", "HEAD")

        # I'll leave the comparison logic to you :-)

        return [RuleViolation(self.id, "Title and branch name don't match", line_nr=1)]

Caveats:

  • No guarantees about the availability or workings of the gitlint.git._git function in future gitlint releases, although I think it will remain fairly stable.
  • The code above will only work if you're trying to check the latest commit on the branch, it won't work when using gitlint's --commits argument to check multiple commits. This is because a commit might be part of multiple branches, so given a single sha it's not possible to figure out the one branch it's part of. At least not generically - in most cases the commit will only be part of a single branch of course. If you need this, you should still be able to get it to work using git branch --contains commit.sha and some extra parsing and business logic that fits your use-case.

Hope this helps!

@and-semakin
Copy link
Author

Thanks for the quick reply @jorisroovers!
Looks reasonable. Will try it :)

Do you have plans to make branch name (or names) accessible via commit or context objects? For example it may be List[str] which will contain one or multiple branch names which commit belongs to.

@jorisroovers
Copy link
Owner

That did occur to me when writing the reply.
Definitely not opposed to it, it's just a matter of finding/making the time. PRs help - a lot ;-)

@jorisroovers jorisroovers added the enhancement User-facing feature enhancements label Nov 22, 2019
jorisroovers added a commit that referenced this issue Feb 16, 2020
Each GitCommit object now contains a 'branches' property that can be
used as part of rules.

Also added an extra user-defined rule example that demonstrates this.

This implements #108
jorisroovers added a commit that referenced this issue Feb 16, 2020
Each GitCommit object now contains a 'branches' property that can be
used as part of rules.

Also added an extra user-defined rule example that demonstrates this.

This implements #108
jorisroovers added a commit that referenced this issue Feb 16, 2020
Each GitCommit object now contains a 'branches' property that can be
used as part of rules.

Also added an extra user-defined rule example that demonstrates this.

This implements #108
@jorisroovers
Copy link
Owner

Implemented. Will release this as part of the 0.13.0 release, hopefully soon :-)

Also, I think it would be nice to also have a GitContext.current_branch property. Adding that to the plan for 0.14.0 (probably not any time soon).

jorisroovers added a commit that referenced this issue Feb 18, 2020
The GitContext class now contains a current_branch property.

Related to #108
jorisroovers added a commit that referenced this issue Feb 25, 2020
- Behavior Change: Revert Commits are now recognized and ignored by default #99
- --staged flag: gitlint can now detect meta-data (such as author details,
  changed files, etc) of staged/pre-commits. Useful when you use gitlint's
  commit-msg hook or precommit (#105)
- New branch properties on GitCommit and GitContext, useful when writing your
  own user-defined rules: commit.branches and commit.context.current_branch #108
- Python 3.8 support
- Python 3.4 no longer supported. Python 3.4 has reached EOL and an increasing
  of gitlint's dependencies have dropped support which makes it hard to maintain
- Improved Windows support: better unicode handling.
- Bugfixes:
  - Gitlint no longer crashes when acting on empty repositories (this only
    occurred in specific circumstances).
  - Changed files are now better detected in repos that only have a root commit
- Improved performance and memory (gitlint now caches git properties)
- Improved --debug output
- Improved documentation
- Under-the-hood: dependencies updated, unit and integration test improvements,
  migrated from TravisCI to Github Actions.

Full Release details in CHANGELOG.md.
@jorisroovers
Copy link
Owner

Released this yesterday as part of the 0.13.0 release - closing this out now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement User-facing feature enhancements question
Projects
None yet
Development

No branches or pull requests

2 participants