Skip to content

Commit ccea02b

Browse files
authored
Git - avoid opening a repository multiple times if symbolic links are used (microsoft#187435)
1 parent 060dfba commit ccea02b

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

extensions/git/src/model.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,9 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
493493
@sequentialize
494494
async openRepository(repoPath: string, openIfClosed = false): Promise<void> {
495495
this.logger.trace(`Opening repository: ${repoPath}`);
496-
if (this.getRepositoryExact(repoPath)) {
497-
this.logger.trace(`Repository for path ${repoPath} already exists`);
496+
const existingRepository = await this.getRepositoryExact(repoPath);
497+
if (existingRepository) {
498+
this.logger.trace(`Repository for path ${repoPath} already exists: ${existingRepository.root})`);
498499
return;
499500
}
500501

@@ -524,8 +525,9 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
524525
const { repositoryRoot, unsafeRepositoryMatch } = await this.getRepositoryRoot(repoPath);
525526
this.logger.trace(`Repository root for path ${repoPath} is: ${repositoryRoot}`);
526527

527-
if (this.getRepositoryExact(repositoryRoot)) {
528-
this.logger.trace(`Repository for path ${repositoryRoot} already exists`);
528+
const existingRepository = await this.getRepositoryExact(repositoryRoot);
529+
if (existingRepository) {
530+
this.logger.trace(`Repository for path ${repositoryRoot} already exists: ${existingRepository.root}`);
529531
return;
530532
}
531533

@@ -763,9 +765,12 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
763765
return liveRepository && liveRepository.repository;
764766
}
765767

766-
private getRepositoryExact(repoPath: string): Repository | undefined {
767-
const openRepository = this.openRepositories
768-
.find(r => pathEquals(r.repository.root, repoPath));
768+
private async getRepositoryExact(repoPath: string): Promise<Repository | undefined> {
769+
const repoPathCanonical = await fs.promises.realpath(repoPath, { encoding: 'utf8' });
770+
const openRepository = this.openRepositories.find(async r => {
771+
const rootPathCanonical = await fs.promises.realpath(r.repository.root, { encoding: 'utf8' });
772+
return pathEquals(rootPathCanonical, repoPathCanonical);
773+
});
769774
return openRepository?.repository;
770775
}
771776

0 commit comments

Comments
 (0)