From 1d4130b8b39bf3bcbc933eaae2f3fb366530ecc2 Mon Sep 17 00:00:00 2001 From: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:35:48 +0200 Subject: [PATCH] Git - fix repository detection when rev-parse returns a UNC path on Windows (#230207) --- extensions/git/src/git.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 5a036628a57..2a729ae08fb 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -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; }