mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
133 lines • 4.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitSwitchManager = void 0;
exports.isGitRepository = isGitRepository;
exports.isWorkingDirectoryClean = isWorkingDirectoryClean;
exports.branchOrCommitExists = branchOrCommitExists;
const child_process_1 = require("child_process");
const debug_logger_1 = require("./debug-logger");
const errors_1 = require("./errors");
/**
* 检查当前是否在 Git 仓库中
*/
function isGitRepository(projectRoot) {
try {
(0, child_process_1.execSync)('git rev-parse --git-dir', { cwd: projectRoot, stdio: 'ignore' });
return true;
}
catch (_a) {
return false;
}
}
/**
* 检查工作区是否干净(没有未提交的更改)
*/
function isWorkingDirectoryClean(projectRoot) {
try {
const output = (0, child_process_1.execSync)('git status --porcelain', {
cwd: projectRoot,
encoding: 'utf8',
}).trim();
return output === '';
}
catch (error) {
debug_logger_1.logger.warn(`检查工作区状态失败: ${error.message}`);
return false;
}
}
/**
* 获取当前分支名
*/
function getCurrentBranch(projectRoot) {
try {
return (0, child_process_1.execSync)('git rev-parse --abbrev-ref HEAD', {
cwd: projectRoot,
encoding: 'utf8',
}).trim();
}
catch (error) {
throw new errors_1.HandledError(`获取当前分支失败: ${error.message}`);
}
}
/**
* 检查分支或提交是否存在
*/
function branchOrCommitExists(projectRoot, ref) {
try {
(0, child_process_1.execSync)(`git rev-parse --verify ${ref}`, {
cwd: projectRoot,
stdio: 'ignore',
});
return true;
}
catch (_a) {
return false;
}
}
/**
* 切换到指定分支或提交
*/
function checkoutBranch(projectRoot, ref) {
try {
debug_logger_1.logger.info(`正在切换到: ${ref}`);
(0, child_process_1.execSync)(`git checkout ${ref}`, {
cwd: projectRoot,
stdio: 'inherit',
});
}
catch (error) {
throw new errors_1.HandledError(`切换分支失败: ${error.message}`);
}
}
/**
* Git 切换管理器,用于安全地切换分支并在完成后恢复
*/
class GitSwitchManager {
constructor(projectRoot) {
this.hasSwitched = false;
this.projectRoot = projectRoot;
this.originalBranch = getCurrentBranch(projectRoot);
this.currentBranchName = this.originalBranch;
}
/**
* 获取原始分支名
*/
getOriginalBranch() {
return this.originalBranch;
}
/**
* 获取当前 GitSwitchManager 检出的分支名
*/
getCurrentBranch() {
return this.currentBranchName;
}
/**
* 切换到目标分支或提交
*/
switchTo(target) {
if (target === this.currentBranchName ||
(target === 'HEAD' && this.currentBranchName === this.originalBranch && !this.hasSwitched)) {
debug_logger_1.logger.info(`目标 ${target} 与当前分支 ${this.currentBranchName} 相同,无需切换`);
return;
}
if (!branchOrCommitExists(this.projectRoot, target)) {
throw new errors_1.HandledError(`分支或提交 '${target}' 不存在`);
}
checkoutBranch(this.projectRoot, target);
this.currentBranchName = target; // Update current branch after successful checkout
this.hasSwitched = true;
}
/**
* 恢复到原始分支
*/
restore() {
if (this.hasSwitched && this.currentBranchName !== this.originalBranch) {
debug_logger_1.logger.info(`恢复到原始分支: ${this.originalBranch}`);
checkoutBranch(this.projectRoot, this.originalBranch);
this.currentBranchName = this.originalBranch;
this.hasSwitched = false; // Reset switch status after restoring
}
}
}
exports.GitSwitchManager = GitSwitchManager;
//# sourceMappingURL=git-helper.js.map