forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompareChangesTreeDataProvider.ts
More file actions
197 lines (163 loc) · 6 KB
/
Copy pathcompareChangesTreeDataProvider.ts
File metadata and controls
197 lines (163 loc) · 6 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as buffer from 'buffer';
import * as vscode from 'vscode';
import { Repository } from '../api/api';
import { getGitChangeType } from '../common/diffHunk';
import Logger from '../common/logger';
import { fromGitHubURI } from '../common/uri';
import { FolderRepositoryManager } from '../github/folderRepositoryManager';
import { GitHubRepository } from '../github/githubRepository';
import { GitHubFileChangeNode } from './treeNodes/fileChangeNode';
import { TreeNode } from './treeNodes/treeNode';
export const GITHUB_FILE_SCHEME = 'githubpr';
export class CompareChangesTreeProvider implements vscode.TreeDataProvider<TreeNode> {
private _view: vscode.TreeView<TreeNode>;
private _onDidChangeTreeData = new vscode.EventEmitter<TreeNode | void>();
readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
private _contentProvider: GitHubContentProvider | undefined;
private _disposables: vscode.Disposable[] = [];
private _gitHubRepository: GitHubRepository | undefined;
get view(): vscode.TreeView<TreeNode> {
return this._view;
}
constructor(
private readonly repository: Repository,
private baseOwner: string,
public baseBranchName: string,
private _compareOwner: string,
private compareBranchName: string,
private compareHasUpstream: boolean,
private folderRepoManager: FolderRepositoryManager,
) {
this._view = vscode.window.createTreeView('github:compareChanges', {
treeDataProvider: this,
});
this._gitHubRepository = this.folderRepoManager.gitHubRepositories.find(
repo => repo.remote.owner === this._compareOwner,
);
this._disposables.push(this._view);
}
updateBaseBranch(branch: string): void {
this.baseBranchName = branch;
this._onDidChangeTreeData.fire();
}
updateBaseOwner(owner: string) {
this.baseOwner = owner;
this._onDidChangeTreeData.fire();
}
async reveal(treeNode: TreeNode, options?: { select?: boolean, focus?: boolean, expand?: boolean }): Promise<void> {
return this._view.reveal(treeNode, options);
}
refresh(): void {
this._onDidChangeTreeData.fire();
}
private async updateHasUpstream(branch: string): Promise<void> {
// Currently, the list of selectable compare branches it those on GitHub,
// plus the current branch which may not be published yet. Check the
// status of the current branch using local git, otherwise assume it is from
// GitHub.
if (this.repository.state.HEAD?.name === branch) {
const compareBranch = await this.repository.getBranch(branch);
this.compareHasUpstream = !!compareBranch.upstream;
} else {
this.compareHasUpstream = true;
}
}
async updateCompareBranch(branch: string): Promise<void> {
await this.updateHasUpstream(branch);
this.compareBranchName = branch;
this._onDidChangeTreeData.fire();
}
get compareOwner(): string {
return this._compareOwner;
}
set compareOwner(owner: string) {
this._gitHubRepository = this.folderRepoManager.gitHubRepositories.find(
repo => repo.remote.owner === owner,
);
if (this._contentProvider && this._gitHubRepository) {
this._contentProvider.gitHubRepository = this._gitHubRepository;
}
this._compareOwner = owner;
this._onDidChangeTreeData.fire();
}
getTreeItem(element: TreeNode): vscode.TreeItem | Thenable<vscode.TreeItem> {
return element.getTreeItem();
}
async getChildren() {
// If no upstream, show error.
if (!this.compareHasUpstream) {
vscode.commands.executeCommand('setContext', 'github:noUpstream', true);
this._view.message = undefined;
return [];
} else {
vscode.commands.executeCommand('setContext', 'github:noUpstream', false);
}
if (!this._gitHubRepository) {
return [];
}
if (!this._contentProvider) {
this._contentProvider = new GitHubContentProvider(this._gitHubRepository);
this._disposables.push(
vscode.workspace.registerTextDocumentContentProvider(GITHUB_FILE_SCHEME, this._contentProvider),
);
}
const { octokit, remote } = await this._gitHubRepository.ensure();
try {
const { data } = await octokit.repos.compareCommits({
repo: remote.repositoryName,
owner: remote.owner,
base: `${this.baseOwner}:${this.baseBranchName}`,
head: `${this.compareOwner}:${this.compareBranchName}`,
});
if (!data.files.length) {
this._view.message = `There are no commits between the base '${this.baseBranchName}' branch and the comparing '${this.compareBranchName}' branch`;
} else {
this._view.message = undefined;
}
return data.files.map(file => {
return new GitHubFileChangeNode(
this,
file.filename,
file.previous_filename,
getGitChangeType(file.status),
data.merge_base_commit.sha,
this.compareBranchName,
);
});
} catch (e) {
Logger.appendLine(`Comparing changes failed: ${e}`);
return [];
}
}
dispose() {
this._disposables.forEach(d => d.dispose());
this._contentProvider = undefined;
}
}
/**
* Provides file contents for documents with GITHUB_FILE_SCHEME (githubpr) scheme. Contents are fetched from GitHub based on
* information in the document's query string.
*/
class GitHubContentProvider {
constructor(public gitHubRepository: GitHubRepository) { }
async provideTextDocumentContent(uri: vscode.Uri, _token: vscode.CancellationToken): Promise<string> {
const params = fromGitHubURI(uri);
if (!params || params.isEmpty) {
return '';
}
const { octokit, remote } = await this.gitHubRepository.ensure();
const fileContent = await octokit.repos.getContent({
owner: remote.owner,
repo: remote.repositoryName,
path: params.fileName,
ref: params.branch,
});
const contents = (fileContent.data as any).content ?? '';
const buff = buffer.Buffer.from(contents, (fileContent.data as any).encoding);
return buff.toString();
}
}