Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Last active October 8, 2021 23:02
Show Gist options
  • Save tbranyen/962852 to your computer and use it in GitHub Desktop.
Save tbranyen/962852 to your computer and use it in GitHub Desktop.
git profanity check
#!/usr/bin/env node
// Copyright 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
// 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();
@cowboy
Copy link

cowboy commented May 9, 2011

This was definitely not created for a specific reason.

@miketaylr
Copy link

Quit cursing in the fscking commit message, jeeze.

@tbranyen
Copy link
Author

tbranyen commented May 9, 2011

Updated it with a new regex generation snippet (by cowboy) to eliminate all forEach statements, so much nicer! Thanks!

@factoidforrest
Copy link

i think this is broken with modern nodegit. What nodegit version is this supposed to have?

@factoidforrest
Copy link

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

@tbranyen
Copy link
Author

tbranyen commented Oct 8, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment