forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitContentProvider.ts
More file actions
69 lines (57 loc) · 2.23 KB
/
Copy pathgitContentProvider.ts
File metadata and controls
69 lines (57 loc) · 2.23 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as pathLib from 'path';
import * as vscode from 'vscode';
import { GitApiImpl } from '../api/api1';
import { fromReviewUri } from '../common/uri';
import { getRepositoryForFile } from '../github/utils';
export class GitContentProvider implements vscode.TextDocumentContentProvider {
private _onDidChange = new vscode.EventEmitter<vscode.Uri>();
get onDidChange(): vscode.Event<vscode.Uri> {
return this._onDidChange.event;
}
private _fallback?: (uri: vscode.Uri) => Promise<string>;
constructor(private gitAPI: GitApiImpl) {}
async provideTextDocumentContent(uri: vscode.Uri, _token: vscode.CancellationToken): Promise<string> {
if (!this._fallback) {
return '';
}
const { path, commit, rootPath } = fromReviewUri(uri.query);
if (!path || !commit) {
return '';
}
const repository = getRepositoryForFile(this.gitAPI, vscode.Uri.file(rootPath));
if (!repository) {
vscode.window.showErrorMessage(`We couldn't find an open repository for ${commit} locally.`);
return '';
}
const absolutePath = pathLib.join(repository.rootUri.fsPath, path).replace(/\\/g, '/');
let content: string;
try {
content = await repository.show(commit, absolutePath);
if (!content) {
throw new Error();
}
} catch (_) {
content = await this._fallback(uri);
if (!content) {
// Content does not exist for the base or modified file for a file deletion or addition.
// Manually check if the commit exists before notifying the user.
try {
await repository.getCommit(commit);
} catch (err) {
vscode.window.showErrorMessage(
`We couldn't find commit ${commit} locally. You may want to sync the branch with remote. Sometimes commits can disappear after a force-push`,
);
}
}
}
return content || '';
}
registerTextDocumentContentFallback(provider: (uri: vscode.Uri) => Promise<string>) {
this._fallback = provider;
}
}