Skip to content

Commit b0b1af1

Browse files
authored
Git - extract code into ParentRepositoriesManager (microsoft#184891)
1 parent 4144b0a commit b0b1af1

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

extensions/git/src/commands.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,10 +3469,10 @@ export class CommandCenter {
34693469

34703470
const allRepositoriesLabel = l10n.t('All Repositories');
34713471
const allRepositoriesQuickPickItem: QuickPickItem = { label: allRepositoriesLabel };
3472-
const repositoriesQuickPickItems: QuickPickItem[] = Array.from(this.model.parentRepositories.keys())
3472+
const repositoriesQuickPickItems: QuickPickItem[] = this.model.parentRepositories
34733473
.sort(compareRepositoryLabel).map(r => new RepositoryItem(r));
34743474

3475-
const items = this.model.parentRepositories.size === 1 ? [...repositoriesQuickPickItems] :
3475+
const items = this.model.parentRepositories.length === 1 ? [...repositoriesQuickPickItems] :
34763476
[...repositoriesQuickPickItems, { label: '', kind: QuickPickItemKind.Separator }, allRepositoriesQuickPickItem];
34773477

34783478
const repositoryItem = await window.showQuickPick(items, { title, placeHolder });
@@ -3482,7 +3482,7 @@ export class CommandCenter {
34823482

34833483
if (repositoryItem === allRepositoriesQuickPickItem) {
34843484
// All Repositories
3485-
parentRepositories.push(...this.model.parentRepositories.keys());
3485+
parentRepositories.push(...this.model.parentRepositories);
34863486
} else {
34873487
// One Repository
34883488
parentRepositories.push((repositoryItem as RepositoryItem).path);

extensions/git/src/model.ts

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ class UnsafeRepositoryMap extends RepositoryMap<string> {
6868
}
6969
}
7070

71-
/**
72-
* Key - normalized path used in user interface
73-
* Value - value indicating whether the repository should be opened
74-
*/
75-
class ParentRepositoryMap extends RepositoryMap {
76-
updateContextKey(): void {
77-
commands.executeCommand('setContext', 'git.parentRepositoryCount', this.size);
78-
}
79-
}
80-
8171
export interface ModelChangeEvent {
8272
repository: Repository;
8373
uri: Uri;
@@ -128,6 +118,47 @@ class ClosedRepositoriesManager {
128118
}
129119
}
130120

121+
class ParentRepositoriesManager {
122+
123+
/**
124+
* Key - normalized path used in user interface
125+
* Value - value indicating whether the repository should be opened
126+
*/
127+
private _repositories = new Set<string>;
128+
get repositories(): string[] {
129+
return [...this._repositories.values()];
130+
}
131+
132+
constructor(private readonly globalState: Memento) { }
133+
134+
addRepository(repository: string): void {
135+
this._repositories.add(repository);
136+
this.onDidChangeRepositories();
137+
}
138+
139+
deleteRepository(repository: string): boolean {
140+
const result = this._repositories.delete(repository);
141+
if (result) {
142+
this.onDidChangeRepositories();
143+
}
144+
145+
return result;
146+
}
147+
148+
hasRepository(repository: string): boolean {
149+
return this._repositories.has(repository);
150+
}
151+
152+
openRepository(repository: string): void {
153+
this.globalState.update(`parentRepository:${repository}`, true);
154+
this.deleteRepository(repository);
155+
}
156+
157+
private onDidChangeRepositories(): void {
158+
commands.executeCommand('setContext', 'git.parentRepositoryCount', this._repositories.size);
159+
}
160+
}
161+
131162
export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePublisherRegistry, IPostCommitCommandsProviderRegistry, IPushErrorHandlerRegistry {
132163

133164
private _onDidOpenRepository = new EventEmitter<Repository>();
@@ -200,9 +231,9 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
200231
return this._unsafeRepositories;
201232
}
202233

203-
private _parentRepositories = new ParentRepositoryMap();
204-
get parentRepositories(): ParentRepositoryMap {
205-
return this._parentRepositories;
234+
private _parentRepositoriesManager: ParentRepositoriesManager;
235+
get parentRepositories(): string[] {
236+
return this._parentRepositoriesManager.repositories;
206237
}
207238

208239
private _closedRepositoriesManager: ClosedRepositoriesManager;
@@ -225,6 +256,7 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
225256
constructor(readonly git: Git, private readonly askpass: Askpass, private globalState: Memento, readonly workspaceState: Memento, private logger: LogOutputChannel, private telemetryReporter: TelemetryReporter) {
226257
// Repositories managers
227258
this._closedRepositoriesManager = new ClosedRepositoriesManager(workspaceState);
259+
this._parentRepositoriesManager = new ParentRepositoriesManager(globalState);
228260

229261
workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders, this, this.disposables);
230262
window.onDidChangeVisibleTextEditors(this.onDidChangeVisibleTextEditors, this, this.disposables);
@@ -260,7 +292,7 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
260292
await initialScanFn();
261293
}
262294

263-
if (this._parentRepositories.size !== 0 &&
295+
if (this.parentRepositories.length !== 0 &&
264296
parentRepositoryConfig === 'prompt') {
265297
// Parent repositories notification
266298
this.showParentRepositoryNotification();
@@ -497,13 +529,13 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
497529
if (isRepositoryOutsideWorkspace) {
498530
this.logger.trace(`Repository in parent folder: ${repositoryRoot}`);
499531

500-
if (!this._parentRepositories.has(repositoryRoot)) {
532+
if (!this._parentRepositoriesManager.hasRepository(repositoryRoot)) {
501533
// Show a notification if the parent repository is opened after the initial scan
502534
if (this.state === 'initialized' && parentRepositoryConfig === 'prompt') {
503535
this.showParentRepositoryNotification();
504536
}
505537

506-
this._parentRepositories.set(repositoryRoot);
538+
this._parentRepositoriesManager.addRepository(repositoryRoot);
507539
}
508540

509541
return;
@@ -547,11 +579,8 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
547579
}
548580

549581
async openParentRepository(repoPath: string): Promise<void> {
550-
// Mark the repository to be opened from the parent folders
551-
this.globalState.update(`parentRepository:${repoPath}`, true);
552-
553582
await this.openRepository(repoPath);
554-
this.parentRepositories.delete(repoPath);
583+
this._parentRepositoriesManager.openRepository(repoPath);
555584
}
556585

557586
private async getRepositoryRoot(repoPath: string): Promise<{ repositoryRoot: string; unsafeRepositoryMatch: RegExpMatchArray | null }> {
@@ -907,7 +936,7 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
907936
}
908937

909938
private async showParentRepositoryNotification(): Promise<void> {
910-
const message = this.parentRepositories.size === 1 ?
939+
const message = this.parentRepositories.length === 1 ?
911940
l10n.t('A git repository was found in the parent folders of the workspace or the open file(s). Would you like to open the repository?') :
912941
l10n.t('Git repositories were found in the parent folders of the workspace or the open file(s). Would you like to open the repositories?');
913942

@@ -925,7 +954,7 @@ export class Model implements IBranchProtectionProviderRegistry, IRemoteSourcePu
925954
await config.update('openRepositoryInParentFolders', choice === always ? 'always' : 'never', true);
926955

927956
if (choice === always) {
928-
for (const parentRepository of [...this.parentRepositories.keys()]) {
957+
for (const parentRepository of this.parentRepositories) {
929958
await this.openParentRepository(parentRepository);
930959
}
931960
}

0 commit comments

Comments
 (0)