Git 工具模块提供 git 远程 URL 规范化、仓库根目录查找、裸仓库安全检测等功能。测试重点是纯函数的 URL 规范化和需要文件系统 mock 的仓库发现逻辑。
| 文件 | 关键导出 |
|---|---|
src/utils/git.ts |
normalizeGitRemoteUrl, findGitRoot, findCanonicalGitRoot, getIsGit, isAtGitRoot, getRepoRemoteHash, isCurrentDirectoryBareGitRepo, gitExe, getGitState, stashToCleanState, preserveGitStateForIssue |
'git@github.com:owner/repo.git' → 'github.com/owner/repo''git@github.com:owner/repo' → 'github.com/owner/repo''git@gitlab.com:group/subgroup/repo.git' → 'gitlab.com/group/subgroup/repo''https://github.com/owner/repo.git' → 'github.com/owner/repo''https://github.com/owner/repo' → 'github.com/owner/repo''http://github.com/owner/repo.git' → 'github.com/owner/repo''ssh://git@github.com/owner/repo' → 'github.com/owner/repo''ssh://user@host/path' → 'host/path''http://local_proxy@127.0.0.1:16583/git/owner/repo' → 'github.com/owner/repo''http://user@127.0.0.1:8080/git/ghe.company.com/owner/repo' → 'ghe.company.com/owner/repo''' → null' ' → null'not-a-url' → null'git@GitHub.com:Owner/Repo.git' → 'github.com/owner/repo'/project/src/utils/ → /project/(假设 /project/.git 存在)/project/ → /project/.git → null.git 为文件时也识别| 依赖 | Mock 方式 | 说明 |
|---|---|---|
statSync |
mock module | findGitRoot 中的 .git 检测 |
readFileSync |
mock module | worktree .git 文件读取 |
realpathSync |
mock module | 路径解析 |
execFileNoThrow |
mock module | git 命令执行 |
whichSync |
mock module | gitExe 中的 git 路径查找 |
getCwd |
mock module | 当前工作目录 |
getRemoteUrl |
mock module | getRepoRemoteHash 依赖 |
| 临时目录 | mkdtemp |
集成测试中创建临时 git 仓库 |