Last active
October 8, 2021 23:02
-
-
Save tbranyen/962852 to your computer and use it in GitHub Desktop.
git profanity check
This file contains 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 node | |
// Copyright 2011, Tim Branyen @tbranyen <[email protected]> | |
// Last Updated: Oct 2021 - Supports NodeGit 0.27.0 | |
// Dual licensed under the MIT and GPL licenses. | |
// Script to detect cursewords in commit messages and provide the | |
// offending commit sha's. | |
// vim: ft=javascript | |
const { Repository } = require('nodegit'); | |
const curses = ['init', 'inappropriate', 'swear', 'words']; | |
const reCurse = new RegExp('\\b(?:' + curses.join('|') + ')\\b', 'gi'); | |
let path = './.git'; | |
let branch = 'master'; | |
// Set git path | |
if (process.argv.length < 3) { | |
console.log('No path passed as argument, defaulting to ./.git'); | |
} | |
else { | |
path = process.argv[2]; | |
// Set repo branch | |
if (process.argv.length < 4) { | |
console.log('No branch passed as argument, defaulting to master'); | |
} | |
else { | |
branch = process.argv[3]; | |
} | |
} | |
async function main() { | |
// Open repository | |
const repo = await Repository.open(path); | |
// Open branch | |
const branchCommit = await repo.getBranchCommit(branch); | |
// Iterate history | |
const history = branchCommit.history(); | |
history.on('commit', commit => { | |
// Check commit messages first | |
if (reCurse.test(commit.message())) { | |
console.log('Curse detected in commit', commit.sha(), commit.message()); | |
} | |
}); | |
history.start(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@factoidforrest this Gist was written 11 years ago about when NodeGit was first created. We used this script for some code that was to be handed off to a client and wanted to ensure clean commit messages.
git log -S swearword
searches files within committed files, not the actual message. It also only works on the currently checked out branch, so it doesn't really seem to be comparable.I've updated this Gist to support modern NodeGit, which required a few tweaks. The API is pretty darn close for over a decade, just some of the helpers were removed in favor of better alignment with libgit2.