Skip to content

Instantly share code, notes, and snippets.

@schlagelk
Last active November 16, 2022 08:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schlagelk/2f8b3b510e7a32a7b0437be315d83e33 to your computer and use it in GitHub Desktop.
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
#!/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