UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

121 lines (120 loc) 3.26 kB
import type { IGitCommitInfo } from './types'; /** * 判断指定路径(默认 `process.cwd()`)是否在 git 仓库中。 * * 实现方式:从给定目录起向上递归查找 `.git`(目录或文件,后者用于 worktree / submodule 场景)。 * 不会执行任何 git 命令,**纯文件系统判断**,零副作用、不会刷屏报错。 * * 结果会按绝对路径缓存,重复调用近乎零开销。 * * @param root 起始目录,默认为 `process.cwd()` * @returns 是否在 git 仓库内 * * @example * ```ts * if (isInGitRepo()) { * const branch = getGitCurBranch(); * } * ``` */ export declare function isInGitRepo(root?: string): boolean; /** * 获取当前分支 * @returns {string} 分支名称 * * @example * * getGitCurBranch() * * // => master */ export declare function getGitCurBranch(root?: string, useCache?: boolean): string; /** * 获取提交信息 * @param {string} root 根路径 * @param {boolean} mergeCommit 是否包含 merge 的提交 * @param {boolean} splitMessage 是否去掉提交信息的前缀 * @returns {string} 提交信息 * * @example * ```ts * getGitCommitMessage() * // '优化一部分文档' * ``` */ export declare function getGitCommitMessage(root?: string, mergeCommit?: boolean, splitMessage?: boolean, useCache?: boolean): string; /** * 获取提交信息 * @param {string} root 根路径 * @param {boolean} mergeCommit 是否包含 merge 的提交 * @param {boolean} splitMessage 是否去掉提交信息的前缀 * @returns {Object} 提交对象 * * @example * ```ts * getGitCommitInfo() * { * author: 'novlan1', * message: ' 优化一部分文档', * hash: '0cb71f9', * date: '2022-10-02 10:34:31 +0800', * timeStamp: '1664678071', * branch: 'master' * } * ``` */ export declare function getGitCommitInfo(root?: string, mergeCommit?: boolean, splitMessage?: boolean, useCache?: boolean): IGitCommitInfo; /** * 获取最新tag * @returns {string} 最新tag * @example * ```ts * getGitLastTag(); * // 'v1.2.3' * * // 指定仓库路径 * getGitLastTag('/path/to/repo'); * ``` */ export declare function getGitLastTag(root?: string): string; /** * 获取tag到head的提交数目 * @param {string} tag git标签 * @returns {string} tag至今的提交数目 * @example * ```ts * getGitCommitsBeforeTag('v1.0.0'); * // '12' * * // 指定仓库路径 * getGitCommitsBeforeTag('v1.0.0', '/path/to/repo'); * ``` */ export declare function getGitCommitsBeforeTag(tag: string, root?: string): string; /** * 获取打标签的时间 * @private * @param {string} tag git标签 * @returns {string} 标签时间 * @example * ```ts * getGitTagTime('v1.0.0'); * // '2023-09-12 10:30:45 +0800' * ``` */ export declare function getGitTagTime(tag: string, root?: string): string; /** * 获取当前用户 * @param isPriorGit - 是否优先使用git用户信息 * @returns user * @example * ```ts * // 默认优先 process.env.VUE_APP_AUTHOR,其次 git config user.name * getGitAuthor(); * // 'novlan1' * * // 优先使用 git 配置中的 user.name * getGitAuthor(true); * ``` */ export declare function getGitAuthor(isPriorGit?: boolean, root?: string, useCache?: boolean): string;