forked from conwnet/github1s
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-diff.ts
More file actions
91 lines (75 loc) · 3.26 KB
/
quick-diff.ts
File metadata and controls
91 lines (75 loc) · 3.26 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
/**
* @file GitHub1s quickDiffProvider for pull/commit change files
* @author netcon
*/
import * as vscode from 'vscode';
import router from '@/router';
import { Repository } from '@/repository';
import { emptyFileUri } from '@/providers';
import adapterManager from '@/adapters/manager';
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 routeState = await router.getState();
const currentScheme = adapterManager.getCurrentScheme();
const repository = Repository.getInstance(currentScheme, routeState.repo);
const codeReviewFiles = await repository.getCodeReviewChangedFiles(codeReviewId);
const changedFile = codeReviewFiles?.find((changedFile) => changedFile.path === uri.path.slice(1));
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;
}
const originalAuthority = `${routeState.repo}+${codeReview!.targetSha}`;
const originalPath = changedFile.previousPath ? `/${changedFile.previousPath}` : uri.path;
return uri.with({ authority: originalAuthority, path: originalPath });
};
// get the original source uri when the `routerState.pageType` is `PageType.COMMIT`
const getOriginalResourceForCommit = async (uri: vscode.Uri, commitSha: string) => {
const routeState = await router.getState();
const currentScheme = adapterManager.getCurrentScheme();
const repository = Repository.getInstance(currentScheme, routeState.repo);
const commitFiles = await repository.getCommitChangedFiles(commitSha);
const changedFile = commitFiles?.find((changedFile) => changedFile.path === uri.path.slice(1));
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;
}
const originalAuthority = `${routeState.repo}+${parentCommitSha}`;
const originalPath = changedFile.previousPath ? `/${changedFile.previousPath}` : uri.path;
return uri.with({ authority: originalAuthority, path: originalPath });
};
export class GitHub1sQuickDiffProvider implements vscode.QuickDiffProvider {
provideOriginalResource(uri: vscode.Uri, _token: vscode.CancellationToken): vscode.ProviderResult<vscode.Uri> {
if (uri.scheme !== adapterManager.getCurrentScheme()) {
return null;
}
return router.getState().then(async (routerState) => {
// only the file belong to current authority could be provided a quick diff
if (uri.authority && uri.authority !== (await router.getAuthority())) {
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;
});
}
}