long-git-cli
Version:
A CLI tool for Git tag management.
135 lines • 6.09 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.commitCommand = commitCommand;
const simple_git_1 = require("simple-git");
const git_remote_1 = require("../utils/git-remote");
const git_1 = require("../utils/git");
const constants_1 = require("../constants");
const prompt = __importStar(require("../utils/prompt"));
const message_1 = require("../utils/message");
const git = (0, simple_git_1.simpleGit)();
/**
* 交互式规范化 commit 并 push
*/
async function commitCommand() {
try {
// 0. 检查是否在 Git 仓库中
await (0, git_1.checkGitRepository)();
// 1. 检查分支保护
const branch = await (0, git_1.getCurrentBranch)();
if (constants_1.PROTECTED_BRANCHES.includes(branch)) {
(0, message_1.printError)(`当前分支为 "${branch}",禁止在主分支(${constants_1.PROTECTED_BRANCHES.join("、")})上 commit!`);
return;
}
// 2. pull(不加 --rebase)
try {
await (0, git_1.pullWithLoading)();
}
catch (e) {
(0, message_1.printErrorMessage)("", e);
return;
}
// 3. 检查暂存区文件
let stagedFiles = await (0, git_1.checkHasStagedFiles)();
// 检查未暂存的修改和未跟踪文件
const unstagedChanges = await (0, git_1.checkHasUnstagedChanges)();
if (stagedFiles.length === 0 && !unstagedChanges.hasChanges) {
(0, message_1.printWarning)("没有需要提交的内容。");
return;
}
if (stagedFiles.length === 0) {
// 没有暂存文件,自动 add .
await (0, git_1.addAllFiles)();
stagedFiles = await (0, git_1.checkHasStagedFiles)();
}
else {
if (stagedFiles.length > 0) {
// 暂存区有文件,检查是否有未add的文件
if (unstagedChanges.hasChanges) {
(0, message_1.printWarning)("检测到还有未 add 的文件:");
unstagedChanges.notAdded.forEach((f) => console.log(" " + f));
unstagedChanges.modified.forEach((f) => console.log(" " + f));
const addMore = await prompt.confirm("是否将这些文件也一并 add?");
if (addMore) {
await (0, git_1.addAllFiles)();
stagedFiles = await (0, git_1.checkHasStagedFiles)();
}
}
}
}
// 如果暂存区没有文件,直接进入注释输入和确认流程
// 4. 交互式收集 commit 信息
const type = await prompt.select("请选择提交类型(必选):", constants_1.commitTypes);
const scope = await prompt.input("影响范围(可选,如模块名,直接回车跳过):");
const subject = await prompt.input("简短说明(必填,建议50字以内):", (input) => !!input.trim() || "提交说明不能为空");
const body = await prompt.input("详细描述(可选,直接回车跳过):");
// const footer = await prompt.input(
// "脚注(如 BREAKING CHANGE/关联 issue,可选,直接回车跳过):"
// );
// 5. 生成规范化 commit message
const message = (0, git_1.formatCommitMessage)(type, subject, scope, body);
// 6. 打印将要提交的文件和注释,询问用户是否 commit 并 push
(0, message_1.printInfo)("\n🚩 即将提交以下内容:");
if (stagedFiles.length > 0) {
(0, message_1.printFileList)("提交文件:", stagedFiles);
}
else {
(0, message_1.printWarning)("没有文件将被提交(空 commit)");
}
(0, message_1.printInfo)("\n提交注释:");
console.log("\x1b[33m%s\x1b[0m", message);
const confirmCommit = await prompt.confirm("请确认是否 commit 并 push?");
if (!confirmCommit) {
(0, message_1.printWarning)("❌ 已终止提交。");
return;
}
// 7. commit
await git.commit(message);
(0, message_1.printSuccess)("commit 成功!");
// 8. push
await (0, git_1.pushCurrentBranch)();
(0, message_1.printSuccess)("push 成功!");
// 9. 远程仓库友好提示
const remoteInfo = await (0, git_remote_1.detectRemoteType)();
if (remoteInfo) {
(0, git_remote_1.showRemoteSpecificTips)(remoteInfo.type);
}
}
catch (error) {
(0, message_1.printErrorMessage)("提交或推送失败:", error);
}
}
//# sourceMappingURL=commit.js.map