Repository navigation
Expand file tree
/
Copy pathchanges.ts
More file actions
160 lines (140 loc) · 5.4 KB
/
Copy pathchanges.ts
File metadata and controls
160 lines (140 loc) · 5.4 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
/**
* @file source control change file list
* @author netcon
*/
import * as vscode from 'vscode';
import * as queryString from 'query-string';
import repository from '@/repository';
import { FileChangeType, RepositoryCommit, RepositoryPull } from '@/repository/types';
import router from '@/router';
import { basename } from '@/helpers/util';
import { emptyFileUri } from '@/providers';
import { GitHub1sFileSystemProvider } from '@/providers/fileSystemProvider';
import { PageType } from '@/router/types';
import { GitHub1sQuickDiffProvider } from './quickDiffProviders';
export interface ChangedFile {
baseFileUri: vscode.Uri;
headFileUri: vscode.Uri;
status: FileChangeType;
}
// get the change files of a pull
export const getPullChangedFiles = async (pull: RepositoryPull) => {
const { owner, repo } = await router.getState();
const baseRootUri = vscode.Uri.parse('').with({
scheme: GitHub1sFileSystemProvider.scheme,
authority: `${owner}+${repo}+${pull.base.sha}`,
path: '/',
});
const headRootUri = baseRootUri.with({
authority: `${owner}+${repo}+${pull.head.sha}`,
});
const pullFiles = await repository.getPullManager().getPullFiles(pull.number);
return pullFiles.map((pullFile) => {
// the `previous_filename` field only exists in `RENAMED` file,
// fallback to `filename` otherwise
const baseFilePath = pullFile.previous_filename || pullFile.filename;
const headFilePath = pullFile.filename;
return {
baseFileUri: vscode.Uri.joinPath(baseRootUri, baseFilePath),
headFileUri: vscode.Uri.joinPath(headRootUri, headFilePath),
status: pullFile.status,
};
});
};
export const getCommitChangedFiles = async (commit: RepositoryCommit) => {
const { owner, repo } = await router.getState();
// if the commit.parents is more than one element
// the parents[1].sha should be the merge source commitSha
// So we use the parents[0].sha as the parent commitSha
const baseRef = commit?.parents?.[0]?.sha;
const baseRootUri = vscode.Uri.parse('').with({
scheme: GitHub1sFileSystemProvider.scheme,
authority: `${owner}+${repo}+${baseRef || 'HEAD'}`,
path: '/',
});
const headRootUri = baseRootUri.with({
authority: `${owner}+${repo}+${commit.sha || 'HEAD'}`,
});
const commitFiles = await repository.getCommitManager().getCommitFiles(commit.sha);
return commitFiles.map((commitFile) => {
// the `previous_filename` field only exists in `RENAMED` file,
// fallback to `filename` otherwise
const baseFilePath = commitFile.previous_filename || commitFile.filename;
const headFilePath = commitFile.filename;
return {
baseFileUri: vscode.Uri.joinPath(baseRootUri, baseFilePath),
headFileUri: vscode.Uri.joinPath(headRootUri, headFilePath),
status: commitFile.status,
};
});
};
export const getChangedFiles = async (): Promise<ChangedFile[]> => {
const routerState = await router.getState();
// github pull page
if (routerState.pageType === PageType.PULL) {
const pull = await repository.getPullManager().getItem(routerState.pullNumber);
return pull ? getPullChangedFiles(pull) : [];
}
// github commit page
else if (routerState.pageType === PageType.COMMIT) {
const commit = await repository.getCommitManager().getItem(routerState.commitSha);
return commit ? getCommitChangedFiles(commit) : [];
}
return [];
};
// get the title of the diff editor
export const getChangedFileDiffTitle = (baseFileUri: vscode.Uri, headFileUri: vscode.Uri, status: FileChangeType) => {
const baseFileName = basename(baseFileUri.path);
const headFileName = basename(headFileUri.path);
const [_owner, _repo, baseCommitSha] = baseFileUri.authority.split('+');
const [__owner, __repo, headCommitSha] = headFileUri.authority.split('+');
const baseFileLabel = `${baseFileName} (${baseCommitSha?.slice(0, 7)})`;
const headFileLabel = `${headFileName} (${headCommitSha?.slice(0, 7)})`;
if (status === FileChangeType.ADDED) {
return `${headFileName} (added in ${headCommitSha.slice(0, 7)})`;
}
if (status === FileChangeType.REMOVED) {
return `${baseFileName} (deleted from ${baseCommitSha.slice(0, 7)})`;
}
return `${baseFileLabel} ⟷ ${headFileLabel}`;
};
export const getChangedFileCommand = (changedFile: ChangedFile) => {
let baseFileUri = changedFile.baseFileUri;
let headFileUri = changedFile.headFileUri;
const status = changedFile.status;
if (status === FileChangeType.ADDED) {
baseFileUri = emptyFileUri;
}
if (status === FileChangeType.REMOVED) {
headFileUri = emptyFileUri;
}
const title = getChangedFileDiffTitle(baseFileUri, headFileUri, status);
const query = queryString.stringify({
status,
base: baseFileUri.with({ query: '' }).toString(),
head: headFileUri.with({ query: '' }).toString(),
});
return {
title: 'Diff',
command: 'vscode.diff',
arguments: [baseFileUri.with({ query }), headFileUri.with({ query }), title],
};
};
export const updateSourceControlChanges = (() => {
const sourceControl = vscode.scm.createSourceControl('github1s', 'GitHub1s');
const changesGroup = sourceControl.createResourceGroup('changes', 'Changes');
sourceControl.quickDiffProvider = new GitHub1sQuickDiffProvider();
return async () => {
const changedFiles = await getChangedFiles();
changesGroup.resourceStates = changedFiles.map((changedFile) => {
return {
resourceUri: changedFile.headFileUri,
decorations: {
strikeThrough: changedFile.status === FileChangeType.REMOVED,
tooltip: changedFile.status,
},
command: getChangedFileCommand(changedFile),
};
});
};
})();