gtw
Version:
git
64 lines (48 loc) • 1.79 kB
JavaScript
const cp = require("child_process");
const util = require("util");
const exec = util.promisify(cp.exec);
const colors = require("colors");
const check = async () => {
let currentBranch;
const { stdout, stderr } = await exec("git rev-parse --abbrev-ref HEAD");
if (stdout) {
currentBranch = stdout.replace(/[\r\n]/g, "");
if (isRelease(currentBranch) || isFeature(currentBranch)) {
const { stdout: reflog } = await exec(
`git reflog --date=local | grep ${currentBranch}`
);
const preBranch = reflog.match(
new RegExp(`(?<=from ).*?(?= to ${currentBranch})`)
)[0];
const { stdout: originBrachList } = await exec("git branch -r");
const { stdout: status } = await exec("git status");
if (
preBranch !== "master" &&
!isRelease(preBranch) &&
!originBrachList.includes(currentBranch)
) {
const message = `📢: 当前分支为 ${currentBranch} 分支, 源分支为 ${preBranch}`;
console.log(colors.yellow(message));
const { stdout: diff } = await exec("git diff");
if (diff) {
console.log(colors.red("警告💀: 存在文件变动,请仔细检查"));
process.exit(1);
} else if (!isClean(status)) {
console.log(
colors.red(
"警告💀: 本地存在未提交的代码,你确定要切换到此分支吗?要不再确认下?"
)
);
process.exit(1);
}
}
}
}
if (stderr) console.log(stderr);
};
const isRelease = (branch) => /^release\//.test(branch);
const isFeature = (branch) => /^feature\//.test(branch);
const isClean = (status) =>
status.includes("nothing to commit, working tree clean");
check();
module.exports = check;