Git - store repository root in the cache along with the workspace folder (#294943)

This commit is contained in:
Ladislau Szomoru
2026-02-12 16:48:03 +01:00
committed by GitHub
parent b5b4ab1415
commit ba54343db9

View File

@@ -9,6 +9,7 @@ import { Remote, RepositoryAccessDetails } from './api/git';
import { isDescendant } from './util';
export interface RepositoryCacheInfo {
repositoryPath: string; // path of the local repository clone
workspacePath: string; // path of the workspace folder or workspace file
lastTouchedTime?: number; // timestamp when the repository was last touched
}
@@ -18,8 +19,8 @@ function isRepositoryCacheInfo(obj: unknown): obj is RepositoryCacheInfo {
return false;
}
const rec = obj as Record<string, unknown>;
return typeof rec.workspacePath === 'string' &&
(rec.lastOpenedTime === undefined || typeof rec.lastOpenedTime === 'number');
return typeof rec.workspacePath === 'string' && typeof rec.repositoryPath === 'string' &&
(rec.lastTouchedTime === undefined || typeof rec.lastTouchedTime === 'number');
}
export class RepositoryCache {
@@ -47,18 +48,18 @@ export class RepositoryCache {
this._recentRepositories = new Map<string, number>();
for (const [_, inner] of this.lru) {
for (const [repositoryPath, repositoryDetails] of inner) {
if (!repositoryDetails.lastTouchedTime) {
for (const [, repositoryDetails] of inner) {
if (!repositoryDetails.repositoryPath || !repositoryDetails.lastTouchedTime) {
continue;
}
// Check whether the repository exists with a more recent access time
const repositoryLastAccessTime = this._recentRepositories.get(repositoryPath);
const repositoryLastAccessTime = this._recentRepositories.get(repositoryDetails.repositoryPath);
if (repositoryLastAccessTime && repositoryDetails.lastTouchedTime <= repositoryLastAccessTime) {
continue;
}
this._recentRepositories.set(repositoryPath, repositoryDetails.lastTouchedTime);
this._recentRepositories.set(repositoryDetails.repositoryPath, repositoryDetails.lastTouchedTime);
}
}
}
@@ -99,6 +100,7 @@ export class RepositoryCache {
}
foldersLru.set(folderPathOrWorkspaceFile, {
repositoryPath: rootPath,
workspacePath: folderPathOrWorkspaceFile,
lastTouchedTime: Date.now()
}); // touch entry
@@ -110,7 +112,7 @@ export class RepositoryCache {
// If the current workspace is a workspace file, use that. Otherwise, find the workspace folder that contains the rootUri
let folderPathOrWorkspaceFile: string | undefined;
try {
if (this._workspaceFile) {
if (this._workspaceFile && this._workspaceFile.scheme === 'file') {
folderPathOrWorkspaceFile = this._workspaceFile.fsPath;
} else if (this._workspaceFolders && this._workspaceFolders.length) {
const sorted = [...this._workspaceFolders].sort((a, b) => b.uri.fsPath.length - a.uri.fsPath.length);
@@ -126,7 +128,6 @@ export class RepositoryCache {
} catch {
return;
}
}
update(addedRemotes: Remote[], removedRemotes: Remote[], rootPath: string): void {
@@ -203,7 +204,6 @@ export class RepositoryCache {
this.lru.set(repo, inner);
}
}
} catch {
this._logger.warn('[CachedRepositories][load] Failed to load cached repositories from global state.');
}