Repository navigation
Expand file tree
/
Copy pathquick-diff.ts
More file actions
82 lines (69 loc) · 2.61 KB
/
Copy pathquick-diff.ts
File metadata and controls
82 lines (69 loc) · 2.61 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
/**
* @file GitHub1s quickDiffProvider for pull/commit change files
* @author netcon
*/
import * as vscode from 'vscode';
import router from '@/router';
import { getAdapter } from '@/adapters';
import { Repository } from '@/repository';
import { emptyFileUri } from '@/providers';
import * as adapterTypes from '@/adapters/types';
// get the original source uri when the `routerState.pageType` is `PageType.PULL`
const getOriginalResourceForPull = async (uri: vscode.Uri, codeReviewId: string): Promise<vscode.Uri | null> => {
const repository = Repository.getCurrentInstance();
const codeReviewFiles = await repository.getCodeReviewChangedFiles(codeReviewId);
const changedFile = codeReviewFiles?.find((changedFile) => changedFile.path === uri.path);
if (
!changedFile ||
changedFile.status === adapterTypes.FileChangeStatus.Added ||
changedFile.status === adapterTypes.FileChangeStatus.Removed
) {
return null;
}
const codeReview = await repository.getCodeReviewItem(codeReviewId);
if (!codeReview?.targetSha) {
return null;
}
return router.buildUri({
ref: codeReview.targetSha,
path: changedFile.previousPath || uri.path,
});
};
// get the original source uri when the `routerState.pageType` is `PageType.COMMIT`
const getOriginalResourceForCommit = async (uri: vscode.Uri, commitSha: string) => {
const repository = Repository.getCurrentInstance();
const commitFiles = await repository.getCommitChangedFiles(commitSha);
const changedFile = commitFiles?.find((changedFile) => changedFile.path === uri.path);
if (
!changedFile ||
changedFile.status === adapterTypes.FileChangeStatus.Added ||
changedFile.status === adapterTypes.FileChangeStatus.Removed
) {
return null;
}
const commit = await repository.getCommitItem(commitSha);
const parentCommitSha = commit?.parents?.[0];
if (!parentCommitSha) {
return emptyFileUri;
}
return router.buildUri({
ref: parentCommitSha,
path: changedFile.previousPath || uri.path,
});
};
export class GitHub1sQuickDiffProvider implements vscode.QuickDiffProvider {
provideOriginalResource(uri: vscode.Uri, _token: vscode.CancellationToken): vscode.ProviderResult<vscode.Uri> {
const routerState = router.getState();
// only the file belong to current workspace could be provided a quick diff
if (uri.scheme !== getAdapter().scheme || uri.authority) {
return null;
}
if (routerState.pageType === adapterTypes.PageType.CodeReview) {
return getOriginalResourceForPull(uri, routerState.codeReviewId);
}
if (routerState.pageType === adapterTypes.PageType.Commit) {
return getOriginalResourceForCommit(uri, routerState.commitSha);
}
return null;
}
}