Created
December 10, 2020 11:49
-
-
Save davidalpert/29e90b0aae9632d64ed2294f824ac9a1 to your computer and use it in GitHub Desktop.
git commit message prepare script (current branch prefix, git-mob support)
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 node | |
let { exec, execSync } = require('child_process'), | |
fs = require('fs'); | |
const { stdout } = require('process'); | |
const commitMessage = process.argv[2]; | |
// expect .git/COMMIT_EDITMSG | |
if (/COMMIT_EDITMSG/g.test(commitMessage)) { | |
// opens .git/COMMIT_EDITMSG | |
let contents = fs.readFileSync(commitMessage) || ''; | |
if (process.env.GIT_COMMIT_MSG_PREFIX_WITH_BRANCH_NAME) { | |
let current_branch = execSync("git branch --show-current").toString().trim() | |
if (!contents.toString().startsWith(current_branch)) { | |
contents = current_branch + ' ' + contents | |
} | |
} | |
let mobbers = execSync("git mob-print").toString().trim() | |
if (mobbers && contents.indexOf(mobbers) !== -1) { | |
const commentPos = contents.indexOf('# '); | |
const gitMessage = contents.slice(0, commentPos); | |
const gitComments = contents.slice(commentPos) | |
contents = gitMessage + mobbers + gitComments | |
} | |
fs.writeFileSync(commitMessage, contents); | |
process.exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment