git-yike-logger-hook
Version:
A TypeScript Git hook plugin for automatically generating commit logs with TODO/WIP comment scanning
135 lines • 3.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitUtils = void 0;
const simple_git_1 = require("simple-git");
/**
* Git 操作工具类
*/
class GitUtils {
constructor() {
this.git = (0, simple_git_1.simpleGit)();
}
/**
* 获取当前提交信息
*/
async getCurrentCommitInfo() {
try {
const log = await this.git.log({ maxCount: 1 });
const currentCommit = log.latest;
if (!currentCommit) {
// 如果是首次提交,返回空信息,让调用者处理
return {};
}
return {
commitHash: currentCommit.hash,
message: currentCommit.message,
author: {
name: currentCommit.author_name,
email: currentCommit.author_email
},
timestamp: currentCommit.date
};
}
catch (error) {
// 如果是首次提交,返回空信息
if (error instanceof Error && error.message.includes('does not have any commits yet')) {
return {};
}
throw new Error(`获取提交信息失败: ${error}`);
}
}
/**
* 获取当前分支名称
*/
async getCurrentBranch() {
try {
const branch = await this.git.revparse(['--abbrev-ref', 'HEAD']);
return branch.trim();
}
catch (error) {
console.warn('无法获取分支名称:', error);
return 'unknown';
}
}
/**
* 获取远程仓库信息
*/
async getRemoteInfo() {
try {
const remotes = await this.git.getRemotes(true);
const origin = remotes.find((remote) => remote.name === 'origin');
if (origin) {
return {
name: origin.name,
url: origin.refs.fetch || origin.refs.push
};
}
}
catch (error) {
console.warn('无法获取远程仓库信息:', error);
}
return undefined;
}
/**
* 获取暂存区的文件状态
*/
async getStagedFiles() {
try {
const status = await this.git.status();
return status.staged;
}
catch (error) {
throw new Error(`获取暂存区文件失败: ${error}`);
}
}
/**
* 获取工作区文件状态
*/
async getWorkingDirectoryStatus() {
try {
const status = await this.git.status();
return {
staged: status.staged,
unstaged: status.modified.concat(status.not_added),
untracked: status.untracked || []
};
}
catch (error) {
throw new Error(`获取工作区状态失败: ${error}`);
}
}
/**
* 获取文件变更详情
*/
async getFileChanges() {
try {
const status = await this.git.status();
return {
added: status.created,
modified: status.modified,
deleted: status.deleted,
renamed: status.renamed.map((rename) => ({
from: rename.from,
to: rename.to
}))
};
}
catch (error) {
throw new Error(`获取文件变更失败: ${error}`);
}
}
/**
* 检查是否在 Git 仓库中
*/
async isGitRepository() {
try {
await this.git.status();
return true;
}
catch (error) {
return false;
}
}
}
exports.GitUtils = GitUtils;
//# sourceMappingURL=git-utils.js.map
;