(cwd: string)
| 176 | } |
| 177 | |
| 178 | async function getGitRepositoryInfoAsync(cwd: string): Promise<GitRepositoryInfo | null> { |
| 179 | const topLevel = await runGitAsync(cwd, ["rev-parse", "--show-toplevel"]); |
| 180 | if (!topLevel.ok) return null; |
| 181 | const rawRepoRoot = topLevel.stdout.trim(); |
| 182 | if (!rawRepoRoot) return null; |
| 183 | let gitCwd: string; |
| 184 | try { |
| 185 | gitCwd = await realpath(resolve(cwd)); |
| 186 | } catch { |
| 187 | return null; |
| 188 | } |
| 189 | let repoRoot: string; |
| 190 | try { |
| 191 | repoRoot = await realpath(rawRepoRoot); |
| 192 | } catch { |
| 193 | return null; |
| 194 | } |
| 195 | |
| 196 | const [gitDir, gitCommonDir] = await Promise.all([ |
| 197 | runGitAsync(cwd, ["rev-parse", "--git-dir"]), |
| 198 | runGitAsync(cwd, ["rev-parse", "--git-common-dir"]), |
| 199 | ]); |
| 200 | |
| 201 | return { |
| 202 | repoRoot, |
| 203 | gitDir: gitDir.ok && gitDir.stdout.trim() ? resolveGitPath(gitCwd, gitDir.stdout.trim()) : resolve(repoRoot, ".git"), |
| 204 | gitCommonDir: gitCommonDir.ok && gitCommonDir.stdout.trim() |
| 205 | ? resolveGitPath(gitCwd, gitCommonDir.stdout.trim()) |
| 206 | : gitDir.ok && gitDir.stdout.trim() |
| 207 | ? resolveGitPath(gitCwd, gitDir.stdout.trim()) |
| 208 | : resolve(repoRoot, ".git"), |
| 209 | }; |
| 210 | } |
| 211 | |
| 212 | function isWithinPath(candidate: string, root: string): boolean { |
| 213 | const rel = relative(root, candidate); |
no test coverage detected