Skip to content

Commit

Permalink
.gitlint: Remove URLs from lines ending with URLs before linting
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Aug 4, 2020
1 parent 3c03fc2 commit 0144e3d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Empty file added .changelog/3168.trivial.md
Empty file.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target/
/__pycache__/
hfuzz_target
hfuzz_workspace
.gitlint-rules/__pycache__/

# IDE.
.idea/*
Expand Down
4 changes: 1 addition & 3 deletions .gitlint
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ignore-merge-commits=true
ignore-fixup-commits=false
ignore-squash-commits=false
ignore=body-is-missing
extra-path=.gitlint-rules

[title-max-length]
line-length=72
Expand All @@ -13,6 +14,3 @@ line-length=80

[body-min-length]
min-length=20

[title-must-not-contain-word]
words=wip
35 changes: 35 additions & 0 deletions .gitlint-rules/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import re

import gitlint
from gitlint.rules import CommitRule, ConfigurationRule

# Monkey patch gitlint so it allows us to define a user-defined ConfigurationRule.
gitlint.rule_finder.assert_valid_rule_class = lambda *_: True

# URL regular expression.
URL_RE_RAW = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
URL_RE = re.compile(URL_RE_RAW, re.IGNORECASE)
LINE_MATCH_RE = re.compile(r'.*' + URL_RE_RAW + r'$')

class RemoveURLs(CommitRule, ConfigurationRule):
"""Removes all URLs from all lines."""

name = "remove-urls"
id = "UC1"

def apply(self, config, commit):
"""Modify the commit message to remove all URLs."""
if not commit.message.body:
return

# Skip the first line as we shouldn't have URLs in the title.
new_body = [commit.message.body[0]]
new_body += [
URL_RE.sub('', line).strip() if LINE_MATCH_RE.match(line) else line
for line in commit.message.body[1:]
]
commit.message.body = new_body

def validate(self, commit):
"""Implements CommitRule."""
return

0 comments on commit 0144e3d

Please sign in to comment.