t-comm
Version:
专业、稳定、纯粹的工具库
84 lines (83 loc) • 3.04 kB
TypeScript
/**
* 一站式 git 仓库信息(贴近 CI / 构建上报场景的完整字段)
*/
export interface IGitFullInfo {
/** 项目标识,如 `pmd-mobile/pixui/pubgm-official`(remoteUrl 解析失败时为 null) */
project: string | null;
/** 原始 remote URL */
remoteUrl: string | null;
/** 当前分支 */
branch: string | null;
/** 最近一次提交 sha(40 位) */
commitSha: string | null;
/** 最近一次提交作者姓名 */
authorName: string | null;
/** 最近一次提交作者邮箱 */
authorEmail: string | null;
/** 当前 git 登录用户名(仓库级优先 → 全局) */
userName: string | null;
/** 当前 git 登录用户邮箱 */
userEmail: string | null;
/** user.name / user.email 来源 */
userScope: 'local' | 'global' | 'none';
}
/**
* 解析 `git config --show-origin --get key` 的输出,
* 格式:`file:<配置文件路径>\t<值>`
*
* 通过配置文件路径判断来源:
* - 含 `.git/` 或 `config.worktree` → `'local'`
* - 其它(~/.gitconfig、/etc/gitconfig 等) → `'global'`
*/
export declare function parseConfigWithOrigin(output: string): {
value: string;
scope: 'local' | 'global';
} | null;
/**
* 将 git remote URL 解析成 `group/subgroup/project` 形式。
*
* 支持以下格式:
* - `git@git.woa.com:pmd-mobile/pixui/pubgm-official.git`
* - `ssh://git@git.woa.com:22/pmd-mobile/pixui/pubgm-official.git`
* - `https://git.woa.com/pmd-mobile/pixui/pubgm-official.git`
* - `http://git.woa.com/pmd-mobile/pixui/pubgm-official`
*
* @example
* ```ts
* parseGitProject('git@git.woa.com:pmd-mobile/pixui/pubgm-official.git');
* // => 'pmd-mobile/pixui/pubgm-official'
* ```
*/
export declare function parseGitProject(remoteUrl?: string | null): string | null;
/**
* 一次性收集仓库的完整 git 信息(remote / 分支 / 最近一次提交作者 / 当前登录用户)。
*
* 与 `getGitCommitInfo` 的差异:
* - 提供 **40 位 commitSha**(`getGitCommitInfo.hash` 是 short id)
* - 提供 **authorEmail**
* - 提供 **当前 git 登录用户**(`user.name` / `user.email`,**仓库级优先 → 全局 → 环境变量**)并标记来源 `userScope`
* - 提供 **project** 字段(自动解析 remoteUrl 为 `group/sub/project`)
*
* 任意一项失败均不影响其它字段。
*
* @param root git 仓库根目录,默认 `process.cwd()`
*
* @example
* ```ts
* import { getGitFullInfo } from 't-comm';
*
* const info = getGitFullInfo();
* // {
* // project: 'pmd-mobile/pixui/pubgm-official',
* // remoteUrl: 'git@git.woa.com:pmd-mobile/pixui/pubgm-official.git',
* // branch: 'develop',
* // commitSha: '33c75cb8e78a7388c357749234d46b731daead09',
* // authorName: 'mobilehelper',
* // authorEmail: 'mobilehelper@tencent.com',
* // userName: 'novlan1',
* // userEmail: 'novlan1@tencent.com',
* // userScope: 'global',
* // }
* ```
*/
export declare function getGitFullInfo(root?: string): IGitFullInfo;