Skip to content

Commit 1b70fcf

Browse files
author
Hoopher
committed
new: 🎉 first commit
0 parents  commit 1b70fcf

File tree

8 files changed

+269
-0
lines changed

8 files changed

+269
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.vscode
2+
dist
3+
commit_linter/__pycache__
4+
commit_linter/commit_linter.egg*
5+
commit_linter.egg-info
6+
build

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Hoopher
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
![pypi](https://img.shields.io/pypi/v/commit-linter.svg)
2+
![python](https://img.shields.io/pypi/pyversions/commit-linter.svg)
3+
![license](https://img.shields.io/github/license/Hoopher/commit-linter.svg)
4+
![last-commit](https://img.shields.io/github/last-commit/Hoopher/commit-linter.svg)
5+
![downloads](https://img.shields.io/pypi/dm/commit-linter?style=flat-square)
6+
![PyPI - Wheel](https://img.shields.io/pypi/wheel/commit-linter)
7+
![PyPI - Implementation](https://img.shields.io/pypi/implementation/commit-linter)
8+
<!-- ![PyPI - Status](https://img.shields.io/pypi/status/commit-linter) -->
9+
![PyPI - Format](https://img.shields.io/pypi/format/commit-linter)
10+
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
11+
12+
# commit-linter
13+
14+
### An easy to use tool to that lints your commit messages according to commit conventions .
15+
16+
commit-linter is a Python tool that helps you standardize your commit messages to a known [commit conventions](https://www.conventionalcommits.org/en/). the tool also add emojies to your commits.
17+
18+
## Installation
19+
20+
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install commit-linter.
21+
22+
```sh
23+
pip install commit-linter
24+
```
25+
26+
## Usage
27+
28+
in your git repository just execute
29+
30+
```sh
31+
commit-linter
32+
```
33+
or
34+
```sh
35+
commit-linter install
36+
```
37+
if you want to remove the hook, just execute
38+
```sh
39+
commit-linter remove
40+
```
41+
42+
## Contributing
43+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
44+
45+
46+
## License
47+
This Tool is Under [MIT](https://choosealicense.com/licenses/mit/) License.

commit_linter/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__author__ = "Hoopher"
2+
__email__ = "Unknown@no-email.com"
3+
4+
__license__ = "MIT"
5+
__version__ = "1.0.0"
6+
__title__ = "Commit Messages Linter"
7+
__description__ = "simple git hooks scripts for a better git experience that enforces you to use the known commit messages conventions"

commit_linter/hooks/commit-msg

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
import re, sys, os
3+
4+
examples = """fix: navbar not responsive on mobile
5+
test: prepared test cases for user authentication
6+
chore: moved to semantic versioning
7+
feat(auth): added social login using twitter
8+
"""
9+
10+
types = """build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
11+
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
12+
docs: Documentation only changes
13+
feat: A new feature
14+
fix: A bug fix
15+
perf: A code change that improves performance
16+
refactor: A code change that neither fixes a bug nor adds a feature
17+
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
18+
test: Adding missing tests or correcting existing tests
19+
"""
20+
21+
def main():
22+
emojies = {"new": ':tada:', "build" : ':construction:' , "ci" : ':green_heart', "docs": ':books:',
23+
"feature" : ':sparkles:' ,"fix" : ':bug:', "perf": ':racehorse:', "refactor" : ':hammer:'
24+
, "style" : 1, "test": ':heavy_check_mark:', "chore" : ':arrow_up:' , "revert" : ':rewind:'}
25+
pattern = r'(new|build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert)(\([\w\-]+\))?:\s.*'
26+
filename = sys.argv[1]
27+
ss = open(filename, 'r').read()
28+
m = re.match(pattern, ss)
29+
if m == None:
30+
print("\nCOMMIT FAILED!")
31+
print("\nPlease enter commit message in the conventional format and try to commit again. Examples:")
32+
print("\n" + examples)
33+
print("\nCHECK COMMIT CONVENTIONS BELOW!\n" + types)
34+
print("\nCheck the Conventional Commits at https://www.conventionalcommits.org/\n" )
35+
sys.exit(1)
36+
else:
37+
commitPrefix = ss.split(':')[0]
38+
commitPostfix = ss.split(':')[1]
39+
if '(' in commitPrefix:
40+
commitEmojie = emojies.get(commitPrefix.split('(')[0])
41+
newCommit = open(filename, 'w')
42+
newCommit.write(commitPrefix + ": " + commitEmojie + commitPostfix)
43+
else:
44+
commitEmojie = emojies.get(commitPrefix)
45+
newCommit = open(filename, 'w')
46+
newCommit.write(commitPrefix + ": " + commitEmojie + commitPostfix)
47+
48+
if __name__ == "__main__":
49+
main()

commit_linter/hooks/pre-receive

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import re
4+
import subprocess
5+
6+
#######################################################################
7+
############## THIS HOOK IS FOR SERVER SIDE ONLY ##############
8+
############## CUSTOMIZE IT TO YOUR NEEDS ##############
9+
#######################################################################
10+
11+
examples = """fix: navbar not responsive on mobile
12+
test: prepared test cases for user authentication
13+
chore: moved to semantic versioning
14+
feat(auth): added social login using twitter
15+
"""
16+
17+
types = """build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
18+
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
19+
docs: Documentation only changes
20+
feat: A new feature
21+
fix: A bug fix
22+
perf: A code change that improves performance
23+
refactor: A code change that neither fixes a bug nor adds a feature
24+
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
25+
test: Adding missing tests or correcting existing tests
26+
"""
27+
28+
29+
pttrn = r'(:(\w+):) (\w+)(\([\w\-]+\))?:\s.*'
30+
31+
#Format: "oldref newref branch"
32+
line = sys.stdin.read()
33+
(base, commit, ref) = line.strip().split()
34+
revs = base + "..." + commit
35+
proc = subprocess.Popen(['git', 'rev-list', '--oneline', '--no-merges',
36+
'--first-parent', revs], stdout=subprocess.PIPE)
37+
lines = proc.stdout.readlines()
38+
print(lines)
39+
40+
if lines:
41+
for line in lines:
42+
rev = str(line)
43+
rev = rev[:-3]
44+
rev = rev.split(' ', 1)[1]
45+
match = re.match(pttrn, rev)
46+
if match == None:
47+
print("\nCOMMIT FAILED AT ==>" + rev)
48+
print("\nPlease enter commit message in the conventional format and try to commit again. Check Examples:\n" + examples)
49+
print("\nCHECK COMMIT CONVENTIONS BELOW!\n" + types)
50+
sys.exit(1)

commit_linter/main.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import stat
4+
import shutil
5+
import pkg_resources
6+
import sys
7+
8+
def enforce():
9+
githooks_path = pkg_resources.resource_filename(
10+
__name__, "hooks/commit-msg")
11+
user_repository = os.path.join(os.getcwd(), ".git/hooks")
12+
13+
if os.path.exists(user_repository+"/commit-msg"):
14+
print("your repo already enforced with commit-msg hook")
15+
else:
16+
shutil.copy(githooks_path, user_repository)
17+
st = os.stat(user_repository + '/commit-msg')
18+
os.chmod(user_repository + '/commit-msg', st.st_mode | stat.S_IEXEC)
19+
print("Your Repo has been enforced with git commit-msg hook")
20+
21+
def unenforce():
22+
user_repository = os.path.join(os.getcwd(), ".git/hooks")
23+
if os.path.exists(user_repository+"/commit-msg"):
24+
os.remove(user_repository+"/commit-msg")
25+
print("The hook has been removed")
26+
else:
27+
print("The hook already does not exist")
28+
29+
def main():
30+
if not os.path.isdir('.git'):
31+
print('error: .git directory not found in the current path. this is not a git repository?')
32+
return
33+
githooks_path = pkg_resources.resource_filename(
34+
__name__, "hooks/commit-msg")
35+
36+
if shutil.which('git') == None:
37+
print('error: git not found on path. please install git and then run pip install --upgrade commit-linter')
38+
return
39+
if not os.path.exists(githooks_path):
40+
print('error: git hooks template not found on %s, please run pip install commit-linter' % githooks_path)
41+
return
42+
else:
43+
if len(sys.argv) > 1 :
44+
if sys.argv[1] == "install":
45+
enforce()
46+
elif sys.argv[1] == "remove":
47+
unenforce()
48+
else:
49+
print("unknown command please either use:")
50+
print("commit-linter install ==> will install hooks to your repo")
51+
print("commit-linter remove ==> will remove hooks from your repo")
52+
else:
53+
print("enforcing by default")
54+
enforce()
55+
56+
57+
if __name__ == "__main__":
58+
main()

setup.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
from setuptools import setup, find_packages
3+
from commit_linter import __version__, __description__, __author__, __email__, __license__
4+
5+
with open("README.md", "r", encoding="utf-8") as fh:
6+
long_description = fh.read()
7+
8+
setup(
9+
name="commit-linter",
10+
version=__version__,
11+
license=__license__,
12+
author=__author__,
13+
author_email=__email__,
14+
description=__description__,
15+
long_description=long_description,
16+
long_description_content_type="text/markdown",
17+
url="https://github.com/Hoopher/commit-linter",
18+
packages=find_packages(),
19+
classifiers=[
20+
"Programming Language :: Python :: 3",
21+
"License :: OSI Approved :: MIT License",
22+
"Operating System :: OS Independent",
23+
],
24+
python_requires='>=3.6',
25+
include_package_data=True,
26+
package_data={'': ['hooks/*']},
27+
entry_points={
28+
'console_scripts': [
29+
'commit-linter = commit_linter.main:main'],
30+
}
31+
)

0 commit comments

Comments
 (0)