Git - fix repository detection when rev-parse returns a UNC path on Windows (#230207)

This commit is contained in:
Ladislau Szomoru
2024-10-01 10:35:48 +02:00
committed by GitHub
parent 744fdb0983
commit 1d4130b8b3

View File

@@ -484,6 +484,17 @@ export class Git {
// Keep trailing spaces which are part of the directory name
const repositoryRootPath = path.normalize(result.stdout.trimStart().replace(/[\r\n]+$/, ''));
// Handle symbolic links and UNC paths
// Git 2.31 added the `--path-format` flag to rev-parse which
// allows us to get the relative path of the repository root
if (!pathEquals(pathInsidePossibleRepository, repositoryRootPath) &&
!isDescendant(repositoryRootPath, pathInsidePossibleRepository) &&
!isDescendant(pathInsidePossibleRepository, repositoryRootPath) &&
this.compareGitVersionTo('2.31.0') !== -1) {
const relativePathResult = await this.exec(pathInsidePossibleRepository, ['rev-parse', '--path-format=relative', '--show-toplevel',]);
return path.resolve(pathInsidePossibleRepository, relativePathResult.stdout.trimStart().replace(/[\r\n]+$/, ''));
}
if (isWindows) {
// On Git 2.25+ if you call `rev-parse --show-toplevel` on a mapped drive, instead of getting the mapped
// drive path back, you get the UNC path for the mapped drive. So we will try to normalize it back to the
@@ -520,17 +531,6 @@ export class Git {
}
}
// Handle symbolic links
// Git 2.31 added the `--path-format` flag to rev-parse which
// allows us to get the relative path of the repository root
if (!pathEquals(pathInsidePossibleRepository, repositoryRootPath) &&
!isDescendant(repositoryRootPath, pathInsidePossibleRepository) &&
!isDescendant(pathInsidePossibleRepository, repositoryRootPath) &&
this.compareGitVersionTo('2.31.0') !== -1) {
const relativePathResult = await this.exec(pathInsidePossibleRepository, ['rev-parse', '--path-format=relative', '--show-toplevel',]);
return path.resolve(pathInsidePossibleRepository, relativePathResult.stdout.trimStart().replace(/[\r\n]+$/, ''));
}
return repositoryRootPath;
}