(startPath: string, indexRoot: string)
| 146 | * common dir): the #155 borrowed-worktree case, warned about, not refused. |
| 147 | */ |
| 148 | export function nestedRepositoryBelow(startPath: string, indexRoot: string): NestedRepository | null { |
| 149 | // git needs an existing directory to run in: a file, or a sub-path that does |
| 150 | // not exist yet, belongs to the repository of its nearest existing directory. |
| 151 | let dir = path.resolve(startPath); |
| 152 | while (!isDirectory(dir)) { |
| 153 | const up = path.dirname(dir); |
| 154 | if (up === dir) return null; |
| 155 | dir = up; |
| 156 | } |
| 157 | const repoRoot = gitWorktreeRoot(dir); |
| 158 | if (!repoRoot) return null; |
| 159 | |
| 160 | // The on-disk spelling of both ends (`realpathSync.native` normalizes case on |
| 161 | // macOS and Windows), so `relPath` matches the paths the index stored. |
| 162 | const rel = path.relative(nativeRealpath(indexRoot), nativeRealpath(repoRoot)); |
| 163 | if (!rel || rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) return null; |
| 164 | |
| 165 | const repoCommon = gitCommonDir(repoRoot); |
| 166 | if (!repoCommon) return null; |
| 167 | const indexCommon = gitCommonDir(realpath(indexRoot)); |
| 168 | if (indexCommon && sameDirectory(repoCommon, indexCommon)) return null; |
| 169 | return { root: repoRoot, relPath: rel.split(path.sep).join('/') }; |
| 170 | } |
| 171 | |
| 172 | function isDirectory(p: string): boolean { |
| 173 | try { |
no test coverage detected