Last active
December 17, 2019 02:03
-
-
Save chrisgfortes/c574898ac9a4ab603779bb6f62e4409e to your computer and use it in GitHub Desktop.
Get git branch parent name in node.js
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
const { promisify } = require('util'); | |
const { exec } = require('child_process'); | |
const run = promisify(exec); | |
const normalizeString = string => (string || '').trim(); | |
function getBranchParent () { | |
return run(`git show-branch | grep '*' | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n1`) | |
.then(resp => { | |
const commit = normalizeString(resp.stdout); | |
const branchNameMatch = /\[(.*)\]/g.exec(commit); | |
const parentBranch = ( | |
branchNameMatch | |
? branchNameMatch[1].replace(/\^|~./g, '') | |
: 'HEAD' | |
); | |
return parentBranch; | |
}); | |
} | |
// bash: git show-branch | grep '*' | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment