Skip to content

Instantly share code, notes, and snippets.

@Rahul91
Last active January 7, 2022 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rahul91/4049ae946192059fa686c9b1aa306b61 to your computer and use it in GitHub Desktop.
Save Rahul91/4049ae946192059fa686c9b1aa306b61 to your computer and use it in GitHub Desktop.
commit-msg hook to detect commit message length.
#!/usr/bin/python3
import sys
class bcolors:
OKCYAN = '\033[96m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def main():
messageFile = sys.argv[1]
try:
file = open(messageFile, 'r')
message = file.read()
finally:
file.close()
is_valid = len(message.split(" ")) > 5
if not is_valid:
print(f"{bcolors.WARNING}WARNING: Commit message less than 5 words.{bcolors.ENDC}")
print(f"{bcolors.OKCYAN}Try commiting again with verbose commit message{bcolors.ENDC}\n")
print(f"{bcolors.FAIL}Error: Commit Failed.{bcolors.ENDC}")
sys.exit(0 if is_valid else 1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment