UNPKG

git-commit-range

Version:
95 lines (75 loc) 3.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _execa = _interopRequireDefault(require("execa")); var _isGitRepository = _interopRequireDefault(require("is-git-repository")); var _os = require("os"); var _path = _interopRequireDefault(require("path")); var _pathIsAbsolute = _interopRequireDefault(require("path-is-absolute")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } var cwd = process.cwd(); var getCommitRange = function getCommitRange() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var path = options.path, from = options.from, to = options.to, include = options.include, _options$includeMerge = options.includeMerges, includeMerges = _options$includeMerge === void 0 ? true : _options$includeMerge, _short = options["short"], type = options.type; var commits = []; var thisFrom = from || ''; var thisTo = to || ''; // cannot use `inclue || true` // because if you set include to false // it will automatically change to true var thisInclude = include === false ? include : true; var thisShort = _short === true ? _short : false; var thisPath = path || cwd; var getCommits; thisPath = (0, _pathIsAbsolute["default"])(thisPath) ? thisPath : _path["default"].join(cwd, thisPath); if (!(0, _isGitRepository["default"])(thisPath)) { return []; } var format = type === 'text' ? 'B' : 'H'; var mergeFlag = includeMerges ? '' : '--no-merges'; try { var commitRangeExec; if ((0, _os.platform)() === 'win32') { if (!thisFrom) { commitRangeExec = "pushd ".concat(thisPath, " & git --no-pager log --format=format:\"%").concat(format, "{{gitCommitRangeEnd}}\" ").concat(mergeFlag); } else { commitRangeExec = "pushd ".concat(thisPath, " & git --no-pager log ").concat(thisFrom, "...").concat(thisTo, " --format=format:\"%").concat(format, "{{gitCommitRangeEnd}}\" ").concat(mergeFlag); } } else { if (!thisFrom) { // eslint-disable-line commitRangeExec = "(cd ".concat(thisPath, " ; git --no-pager log --format=format:\"%").concat(format, "{{gitCommitRangeEnd}}\" ").concat(mergeFlag, ")"); } else { commitRangeExec = "(cd ".concat(thisPath, " ; git --no-pager log ").concat(thisFrom, "...").concat(thisTo, " --format=format:\"%").concat(format, "{{gitCommitRangeEnd}}\" ").concat(mergeFlag, ")"); } } getCommits = _execa["default"].shellSync(commitRangeExec).stdout; getCommits = getCommits.split('{{gitCommitRangeEnd}}').map(function (s) { return s.trim(); }).filter(Boolean); getCommits.forEach(function (commithash) { if (thisShort) { return commits.push(commithash.substring(0, 7)); } return commits.push(commithash); }); if (!thisInclude) { commits.shift(); commits.pop(); } return commits; } catch (err) { return []; } }; var _default = getCommitRange; exports["default"] = _default; module.exports = exports["default"];