Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git - avoid opening a repository multiple times if symbolic links are used #187435

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions extensions/git/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,9 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
@sequentialize
async openRepository(repoPath: string, openIfClosed = false): Promise<void> {
this.logger.trace(`Opening repository: ${repoPath}`);
if (this.getRepositoryExact(repoPath)) {
this.logger.trace(`Repository for path ${repoPath} already exists`);
const existingRepository = await this.getRepositoryExact(repoPath);
if (existingRepository) {
this.logger.trace(`Repository for path ${repoPath} already exists: ${existingRepository.root})`);
return;
}

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

if (this.getRepositoryExact(repositoryRoot)) {
this.logger.trace(`Repository for path ${repositoryRoot} already exists`);
const existingRepository = await this.getRepositoryExact(repositoryRoot);
if (existingRepository) {
this.logger.trace(`Repository for path ${repositoryRoot} already exists: ${existingRepository.root}`);
return;
}

Expand Down Expand Up @@ -763,9 +765,12 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
return liveRepository && liveRepository.repository;
}

private getRepositoryExact(repoPath: string): Repository | undefined {
const openRepository = this.openRepositories
.find(r => pathEquals(r.repository.root, repoPath));
private async getRepositoryExact(repoPath: string): Promise<Repository | undefined> {
const repoPathCanonical = await fs.promises.realpath(repoPath, { encoding: 'utf8' });
const openRepository = this.openRepositories.find(async r => {
const rootPathCanonical = await fs.promises.realpath(r.repository.root, { encoding: 'utf8' });
return pathEquals(rootPathCanonical, repoPathCanonical);
});
return openRepository?.repository;
}

Expand Down