long-git-cli
Version:
A CLI tool for Git tag management.
141 lines • 5.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkGitRepository = checkGitRepository;
exports.getCurrentBranch = getCurrentBranch;
exports.fetchLatestTags = fetchLatestTags;
exports.pushTags = pushTags;
exports.pushCurrentBranch = pushCurrentBranch;
exports.getLatestTag = getLatestTag;
exports.pullRebaseWithLoading = pullRebaseWithLoading;
exports.pullWithLoading = pullWithLoading;
exports.checkHasStagedFiles = checkHasStagedFiles;
exports.checkHasUnstagedChanges = checkHasUnstagedChanges;
exports.addAllFiles = addAllFiles;
exports.formatCommitMessage = formatCommitMessage;
const simple_git_1 = require("simple-git");
const loading_1 = require("./loading");
const tag_1 = require("../utils/tag");
const git = (0, simple_git_1.simpleGit)();
/** 检查当前目录是否为有效的 Git 仓库 */
async function checkGitRepository() {
try {
await git.status();
}
catch (error) {
throw new Error("傻逼没有git你执行什么,你他妈先关联好仓库行吗?");
}
}
/** 获取当前分支名称 */
async function getCurrentBranch() {
const status = await git.status();
if (!status.current) {
throw new Error("无法获取当前分支名称,请确认已在有效的 git 分支上");
}
return status.current;
}
/** 拉取最新的远程 tag 信息(带 loading) */
async function fetchLatestTags(remoteType) {
if (remoteType === "bitbucket") {
await (0, loading_1.withLoading)(() => git.fetch("origin", "--tags"), "正在拉取最新的远程 tag (bitbucket) ...");
}
else {
await (0, loading_1.withLoading)(() => git.fetch(["--tags"]), "正在拉取最新的远程 tag...");
}
}
/** 推送所有 tag(带 loading) */
async function pushTags() {
await (0, loading_1.withLoading)(() => git.pushTags(), "正在推送 tag 到远程仓库...");
}
/** 推送当前分支(带 loading) */
async function pushCurrentBranch() {
await (0, loading_1.withLoading)(() => git.push(), "正在 push 当前分支...");
}
/** 获取最新的 tag(test-v 格式) */
async function getLatestTag() {
try {
const tags = await git.tags();
const testTags = tags.all.filter((tag) => {
try {
return !!(0, tag_1.parseTag)(tag);
}
catch {
return false;
}
});
if (testTags.length === 0) {
return null;
}
// 按版本号排序,获取最新的
testTags.sort((a, b) => {
const aInfo = (0, tag_1.parseTag)(a);
const bInfo = (0, tag_1.parseTag)(b);
return (0, tag_1.compareVersions)(aInfo, bInfo);
});
return testTags[testTags.length - 1];
}
catch (error) {
throw new Error("获取 tag 列表失败");
}
}
/** 拉取远程并 rebase(带 loading),指定远程和分支名,精准判断冲突 */
async function pullRebaseWithLoading() {
const status = await git.status();
const branch = status.current;
if (!branch)
throw new Error("无法获取当前分支名");
try {
await (0, loading_1.withLoading)(() => git.pull("origin", branch, { "--rebase": null }), "正在拉取远程最新代码并 rebase...");
}
catch (e) {
const msg = e && e.message ? e.message : String(e);
if (/conflict|CONFLICT|冲突/.test(msg)) {
throw new Error("pull --rebase 失败,检测到冲突,请手动解决冲突后再提交!");
}
else {
throw new Error("pull --rebase 失败,原因:" + msg);
}
}
}
/** 拉取远程(带 loading),不加 --rebase */
async function pullWithLoading() {
const status = await git.status();
const branch = status.current;
if (!branch)
throw new Error("无法获取当前分支名");
await (0, loading_1.withLoading)(() => git.pull("origin", branch), "正在拉取远程最新代码...");
}
/** 检查是否有待提交的文件 */
async function checkHasStagedFiles() {
const status = await git.status();
return status.staged;
}
/** 检查是否有未暂存的修改 */
async function checkHasUnstagedChanges() {
const status = await git.status();
return {
notAdded: status.not_added,
modified: status.modified,
hasChanges: status.not_added.length > 0 || status.modified.length > 0,
};
}
/** 自动 add 所有文件 */
async function addAllFiles() {
await git.add(".");
}
/** 生成规范化的 commit message */
function formatCommitMessage(type, subject, scope, body, footer) {
let header = type;
if (scope && scope.trim()) {
header += `(${scope.trim()})`;
}
header += `: ${subject.trim()}`;
let message = header;
if (body && body.trim()) {
message += `\n\n${body.trim()}`;
}
if (footer && footer.trim()) {
message += `\n\n${footer.trim()}`;
}
return message;
}
//# sourceMappingURL=git.js.map