-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
git-repository-provider.ts
106 lines (93 loc) · 3.85 KB
/
git-repository-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (C) 2017 TypeFox and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
import { Git, Repository } from '../common';
import { injectable, inject } from "inversify";
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { Event, Emitter } from '@theia/core';
@injectable()
export class GitRepositoryProvider {
protected _selectedRepository: Repository | undefined;
protected containingRepository: Repository | undefined;
protected _allRepositories: Repository[] = [];
protected readonly onDidChangeRepositoryEmitter = new Emitter<Repository | undefined>();
constructor(
@inject(Git) protected readonly git: Git,
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService
) {
this.refresh();
}
/**
* Returns with the previously selected repository, or if no repository has been selected yet,
* it picks the first available repository from the backend and sets it as the selected one and returns with that.
* If no repositories are available, returns `undefined`.
*/
get selectedRepository(): Repository | undefined {
return this._selectedRepository;
}
/**
* Sets or un-sets the repository.
*/
set selectedRepository(repository: Repository | undefined) {
this._selectedRepository = repository;
this.onDidChangeRepositoryEmitter.fire(repository);
}
get onDidChangeRepository(): Event<Repository | undefined> {
return this.onDidChangeRepositoryEmitter.event;
}
/**
* Returns with all know repositories.
*/
get allRepositories(): Repository[] {
return this._allRepositories;
}
async refresh(): Promise<void> {
const root = await this.workspaceService.root;
if (!root) {
return;
}
await this.refreshContainingRepository(root.uri);
await this.refreshAllRepositories(root.uri);
}
protected async refreshAllRepositories(workspaceUri: string): Promise<void> {
const repositories = await this.git.repositories(workspaceUri, { shallow: false });
this._allRepositories = repositories;
this.refreshSelectedRepository();
}
protected async refreshContainingRepository(workspaceUri: string): Promise<void> {
const repositories = await this.git.repositories(workspaceUri, { shallow: true });
const containingRepository = <Repository | undefined>repositories[0];
if (Repository.equal(this.containingRepository, containingRepository)) {
return;
}
this.remove(this.containingRepository);
this.containingRepository = containingRepository;
this.add(containingRepository);
this.refreshSelectedRepository();
}
protected refreshSelectedRepository(): void {
const selectedRepository = this._selectedRepository;
if (!selectedRepository || !this.exists(selectedRepository)) {
this.selectedRepository = this._allRepositories[0];
}
}
protected add(repository: Repository | undefined): void {
if (!!repository && !this.exists(repository)) {
this._allRepositories.unshift(repository);
}
}
protected remove(repository: Repository | undefined): void {
if (repository) {
const index = this._allRepositories.findIndex(repository2 => Repository.equal(repository, repository2));
if (index !== -1) {
this._allRepositories.splice(index, 1);
}
}
}
protected exists(repository: Repository): boolean {
return this._allRepositories.some(repository2 => Repository.equal(repository, repository2));
}
}