UNPKG

git-branches

Version:

Get a current repositories branches list

51 lines (42 loc) 1.45 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.parser = parser; exports.default = branches; var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _path = require('path'); var _path2 = _interopRequireDefault(_path); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function parser(fetch_head) { var branches = []; fetch_head.split("\n").map(function (head) { if (head == "") return head; var branch = /branch\s+\'[\w\d\-]+\'\s+/.exec(head); if (!branch) return head; var branch_name = /\'[\w\d\-]+\'/.exec(branch[0].trim()); if (branch_name) branches.push(branch_name[0].replace(/\'/ig, "").trim()); return head; }); return branches; } function branches(root) { return new Promise(function (res, rej) { if (!root) root = process.cwd(); var git_root = _path2.default.join(root, '.git'); _fs2.default.stat(git_root, function (err, stats) { if (err) return rej(err); if (!stats.isDirectory()) return rej(new Error('no gitconfig to be found at ' + git_root)); var fetch = _path2.default.join(git_root, 'FETCH_HEAD'); _fs2.default.readFile(fetch, function (err, data) { if (err) { if (err.code == "ENOENT") res(["master"]);else rej(err); return; } var branches = parser(data + ""); res(branches); }); }); }); }