-
-
Save tbranyen/962852 to your computer and use it in GitHub Desktop.
#!/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(); |
Quit cursing in the fscking commit message, jeeze.
Updated it with a new regex generation snippet (by cowboy) to eliminate all forEach statements, so much nicer! Thanks!
i think this is broken with modern nodegit. What nodegit version is this supposed to have?
Yeah. That module changed API completely since this was written. Honestly, just use the command line. You can do this with git log -S swearword
much more easily
@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.
This was definitely not created for a specific reason.