UNPKG

pivotal-flow

Version:

🔀 A command-line tool that helps you manage & automate your workflow to use with PivotalTracker.

63 lines (62 loc) • 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const path_1 = require("path"); const fs_1 = require("fs"); const console_1 = require("./console"); /** * Get root directory of the project (where the .git folder exists) */ exports.getRootDirectory = () => child_process_1.execSync('git rev-parse --show-toplevel') .toString() .trim(); /** * Get current branch name as a string. */ exports.getCurrentBranch = () => { const branchName = child_process_1.execSync('git rev-parse --abbrev-ref HEAD') .toString() .trim(); return branchName; }; /** * Detect if currently in detached-HEAD state. * * @see https://stackoverflow.com/a/52222248/4101408 * @see https://www.git-tower.com/learn/git/faq/detached-head-when-checkout-commit */ exports.inDetachedHeadState = () => { try { child_process_1.execSync('git symbolic-ref -q HEAD'); return false; } catch (error) { return true; } }; /** * Get count of how many times a particular ref is checked out. */ exports.getCheckoutCount = ( /** The git `ref` to check */ ref = '') => { const count = child_process_1.execSync(`git reflog --date=local | grep -o '${ref}' | wc -l`, { encoding: 'utf-8' }).trim(); console_1.debugLogObject('checkout count', { count, ref }); return parseInt(count, 10); }; /** * Check if the checked out branch is a new branch. */ exports.isNewBranch = (prevHead, currentHead, branch) => prevHead === currentHead && exports.getCheckoutCount(branch || '') === 1; exports.checkoutNewBranch = (branchName) => child_process_1.execSync(`git checkout -b ${branchName}`); /** * Get contents of commit message file */ exports.getCommitMessage = (filename) => fs_1.readFileSync(filename, { encoding: 'utf8' }); /** * Write the final commit message to the filename provided. */ exports.writeCommitMessage = (filename, message) => { const gitRootDirectory = exports.getRootDirectory(); return fs_1.writeFileSync(path_1.resolve(gitRootDirectory, filename), message, { encoding: 'utf8' }); };