Skip to content

Instantly share code, notes, and snippets.

@chrisgfortes
Last active December 17, 2019 02:03
Show Gist options
  • Save chrisgfortes/c574898ac9a4ab603779bb6f62e4409e to your computer and use it in GitHub Desktop.
Save chrisgfortes/c574898ac9a4ab603779bb6f62e4409e to your computer and use it in GitHub Desktop.
Get git branch parent name in node.js
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