Last active
November 16, 2022 08:26
-
-
Save schlagelk/2f8b3b510e7a32a7b0437be315d83e33 to your computer and use it in GitHub Desktop.
A Git hook written in Swift using NLP to enforce commit message formatting. Checks if the first word of a message is a verb and has an uppercased first letter. chmod +x swibang and then mv to .git/hooks/commit-msg in your repo. Requires Xcode/Command Line tools, and just for fun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xcrun swift | |
import NaturalLanguage | |
let str = try! String(contentsOfFile: CommandLine.arguments[1]).trimmingCharacters(in: .whitespacesAndNewlines) | |
print("Analyzing commit message: '\(str)'") | |
if !str.first!.isUppercase { | |
print("\u{001B}[0;31mUppercase the first word of your commit message bro wtf") | |
exit(1) | |
} | |
let tagger = NLTagger(tagSchemes: [.lexicalClass]) | |
tagger.string = str | |
let range = str.startIndex ..< str.endIndex | |
tagger.enumerateTags(in: range, unit: .word, scheme: .lexicalClass) { (tag, _) -> Bool in | |
if let tag = tag, tag == .verb { | |
print("\u{001B}[0;32mCommit message first word was a verb and had an uppercased first letter - your teammates will thank you") | |
exit(0) | |
} | |
print("\u{001B}[0;31mCommit message first word was not a verb - what kinda message is that?") | |
exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment