Skip to content

Instantly share code, notes, and snippets.

@nikhilw
Last active April 17, 2023 11:36
Show Gist options
  • Save nikhilw/7492c3d0426543f983acedbf1fc9f600 to your computer and use it in GitHub Desktop.
Save nikhilw/7492c3d0426543f983acedbf1fc9f600 to your computer and use it in GitHub Desktop.

Revisions

  1. nikhilw revised this gist Apr 17, 2023. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion commit-msg
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,8 @@
    # NOTES:
    # 1. This is one of the three implementation, this one is in Python. if you do not have python installed, there are 'node' and 'scala' counterparts.
    # 2. To use this file, place it in .git/hooks/ in your repository.
    # 3. You can also use this script to along with 'husky' npm module.
    # 3. You can also use this script with the 'husky' npm module.

    import sys
    import os
    import re
  2. nikhilw created this gist Apr 5, 2023.
    30 changes: 30 additions & 0 deletions commit-msg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/usr/bin/env python3

    # NOTES:
    # 1. This is one of the three implementation, this one is in Python. if you do not have python installed, there are 'node' and 'scala' counterparts.
    # 2. To use this file, place it in .git/hooks/ in your repository.
    # 3. You can also use this script to along with 'husky' npm module.
    import sys
    import os
    import re

    filename = sys.argv[1] or os.getenv('GIT_PARAMS')
    with open(filename, 'r') as f:
    msg = f.read()

    msg = re.sub(r'\r\n|\r|\n', ' ', msg)
    maxLen = 50 if len(msg) > 50 else len(msg)
    msg = msg[0:maxLen]
    matchTest = re.search('([A-Z]+-?\d+|Adhoc)\: (.+: )?(Added|Removed|BugFix|Modified|Feature|Merged|Refactored|Release): (.+){15,}$', msg)

    exitCode = 0 if (not (matchTest is None)) else 1

    if (exitCode == 0):
    print("SUCCESS: Commit ACCEPTED.")
    else:
    print("ERROR: Commit REJECTED. REASON: Your commit message: '" + msg + "' does not follow the commit message convention.");
    print("Convention: '[JIRAID]: [Optional clientId]: [One Primary Change Type: Added|Removed|BugFix|Modified|Feature|Merged|Refactored|Release]: [Message describing the change. If absolutely necessary, you can add multiple Change types here.]");
    print("Sample: IT-123: Added: git hook to validate commit message.");


    exit(exitCode)