UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

205 lines (202 loc) 5.71 kB
import { _ as __assign } from '../tslib.es6-096fffdd.js'; import { execCommand } from '../node/node-command.mjs'; import '@babel/runtime/helpers/typeof'; var DEFAULT_COMMIT_INFO = { author: 'UNKNOWN', message: 'UNKNOWN', hash: 'UNKNOWN', date: 'UNKNOWN', timeStamp: '0', branch: '' }; var localInfo = { curBranch: '', gotCurBranch: false, commitInfo: __assign({}, DEFAULT_COMMIT_INFO), gotCommitInfo: false, gitAuthor: '', gotGitAuthor: false, commitMessage: '', gotCommitMessage: false, lastRoot: '' }; /** * 获取当前分支 * @returns {string} 分支名称 * * @example * * getGitCurBranch() * * // => master */ function getGitCurBranch(root, useCache) { if (useCache === void 0) { useCache = true; } if (useCache && localInfo.lastRoot === root && localInfo.gotCurBranch) { return localInfo.curBranch; } var res = ''; try { res = execCommand('git symbolic-ref --short -q HEAD', root); } catch (err) { console.log('[getGitCurBranch] error: ', err); } localInfo.curBranch = res; localInfo.gotCurBranch = true; localInfo.lastRoot = root || ''; return res; } /** * 获取提交信息 * @param {string} root 根路径 * @param {boolean} mergeCommit 是否包含 merge 的提交 * @param {boolean} splitMessage 是否去掉提交信息的前缀 * @returns {string} 提交信息 * * @example * ```ts * getGitCommitMessage() * // '优化一部分文档' * ``` */ function getGitCommitMessage(root, mergeCommit, splitMessage, useCache) { if (mergeCommit === void 0) { mergeCommit = false; } if (splitMessage === void 0) { splitMessage = false; } if (useCache === void 0) { useCache = true; } if (useCache && localInfo.lastRoot === root && localInfo.gotCommitMessage) { return localInfo.commitMessage; } var infoMessage = execCommand("git log ".concat(mergeCommit ? '' : '--no-merges', " -1 --format=%s | cat"), root); var result = ''; if (splitMessage) { result = (infoMessage.split(':')[1] || infoMessage.split(':')[1] || '').trim(); } else { result = infoMessage; } localInfo.gotCommitMessage = true; localInfo.commitMessage = result; localInfo.lastRoot = root || ''; return result; } /** * 获取提交信息 * @param {string} root 根路径 * @param {boolean} mergeCommit 是否包含 merge 的提交 * @param {boolean} splitMessage 是否去掉提交信息的前缀 * @returns {Object} 提交对象 * * @example * ```ts * getGitCommitInfo() * { * author: 'novlan1', * message: ' 优化一部分文档', * hash: '0cb71f9', * date: '2022-10-02 10:34:31 +0800', * timeStamp: '1664678071', * branch: 'master' * } * ``` */ function getGitCommitInfo(root, mergeCommit, splitMessage, useCache) { if (mergeCommit === void 0) { mergeCommit = false; } if (splitMessage === void 0) { splitMessage = false; } if (useCache === void 0) { useCache = true; } if (useCache && localInfo.lastRoot === root && localInfo.gotCommitInfo) { return localInfo.commitInfo; } var command = "\n git log ".concat(mergeCommit ? '' : '--no-merges', " -1 --date=iso --pretty=format:'{\"author\": \"%aN\", \"hash\": \"%h\", \"date\": \"%ad\", \"timeStamp\": \"%at\"},' $@ | perl -pe 'BEGIN{print \"[\"}; END{print \"]\n\"}' | perl -pe 's/},]/}]/'"); var stdout = execCommand(command, root); var info = __assign({}, DEFAULT_COMMIT_INFO); try { // 如果提交信息包括双引号,会解析失败 info = JSON.parse(stdout)[0]; } catch (err) { console.warn('[getGitCommitInfo] parse error'); } info.branch = getGitCurBranch(root, false); info.message = getGitCommitMessage(root, mergeCommit, splitMessage, false); localInfo.gotCommitInfo = true; localInfo.commitInfo = info; localInfo.lastRoot = root || ''; return info; } /** * 获取最新tag * @returns {string} 最新tag */ function getGitLastTag(root) { var fakeFirstTag = execCommand('git tag -l', root); if (!fakeFirstTag) return ''; // 不能使用`git tag | head -1`,这个命令不准 var command = 'git describe --abbrev=0'; var tag = execCommand(command, root); return tag || ''; } /** * 获取tag到head的提交数目 * @param {string} tag git标签 * @returns {string} tag至今的提交数目 */ function getGitCommitsBeforeTag(tag, root) { if (!tag) return '0'; return execCommand("git log ".concat(tag, "...HEAD --no-merges --oneline | wc -l"), root); } /** * 获取打标签的时间 * @private * @param {string} tag git标签 * @returns {string} 标签时间 */ function getGitTagTime(tag, root) { if (!tag) return ''; return execCommand("git log -1 --format=%ai ".concat(tag, " | cat"), root); } /** * 获取当前用户 * @param isPriorGit - 是否优先使用git用户信息 * @returns user */ function getGitAuthor(isPriorGit, root, useCache) { if (isPriorGit === void 0) { isPriorGit = false; } if (useCache === void 0) { useCache = true; } if (useCache && localInfo.lastRoot === root && localInfo.gotGitAuthor) { return localInfo.gitAuthor; } var envAuthor = process.env.VUE_APP_AUTHOR; var gitAuthor = ''; try { gitAuthor = execCommand('git config user.name', root); } catch (err) { console.log('[getAuthor] err: ', err); } var result = ''; if (isPriorGit) { result = gitAuthor || envAuthor || ''; } else { result = envAuthor || gitAuthor || ''; } localInfo.gitAuthor = result; localInfo.gotGitAuthor = true; localInfo.lastRoot = root || ''; return result; } export { getGitAuthor, getGitCommitInfo, getGitCommitMessage, getGitCommitsBeforeTag, getGitCurBranch, getGitLastTag, getGitTagTime };