Repository navigation
Expand file tree
/
Copy pathfiles.ts
More file actions
152 lines (134 loc) · 5.15 KB
/
Copy pathfiles.ts
File metadata and controls
152 lines (134 loc) · 5.15 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
/**
* @file source control change file list
* @author netcon
*/
import * as vscode from 'vscode';
import * as queryString from 'query-string';
import * as adapterTypes from '@/adapters/types';
import adapterManager from '@/adapters/manager';
import router from '@/router';
import { basename } from '@/helpers/util';
import { emptyFileUri } from '@/providers';
import { Repository } from '@/repository';
interface VSCodeChangedFile {
baseFileUri: vscode.Uri;
headFileUri: vscode.Uri;
status: adapterTypes.FileChangeStatus;
}
// get the change files of a codeReview
export const getCodeReviewChangedFiles = async (
codeReview: adapterTypes.CodeReview & { sourceSha: string; targetSha: string }
) => {
const scheme = adapterManager.getCurrentScheme();
const { repo } = await router.getState();
const baseRootUri = vscode.Uri.parse('').with({
scheme: scheme,
authority: `${repo}+${codeReview.targetSha}`,
path: '/',
});
const headRootUri = baseRootUri.with({
authority: `${repo}+${codeReview.sourceSha}`,
});
const repository = Repository.getInstance(scheme, repo);
const changedFiles = await repository.getCodeReviewChangedFiles(codeReview.id);
return changedFiles.map((changedFile) => {
// the `previous_filename` field only exists in `RENAMED` file,
// fallback to `filename` otherwise
const baseFilePath = changedFile.previousPath || changedFile.path;
const headFilePath = changedFile.path;
return {
baseFileUri: vscode.Uri.joinPath(baseRootUri, baseFilePath),
headFileUri: vscode.Uri.joinPath(headRootUri, headFilePath),
status: changedFile.status,
};
});
};
export const getCommitChangedFiles = async (commit: adapterTypes.Commit) => {
const currentAdapter = adapterManager.getCurrentAdapter();
const scheme = currentAdapter.scheme;
const { 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];
const baseRootUri = vscode.Uri.parse('').with({
scheme: currentAdapter.scheme,
authority: `${repo}+${baseRef || 'HEAD'}`,
path: '/',
});
const headRootUri = baseRootUri.with({
authority: `${repo}+${commit.sha || 'HEAD'}`,
});
const repository = Repository.getInstance(scheme, repo);
const changedFiles = await repository.getCommitChangedFiles(commit.sha);
return changedFiles.map((commitFile) => {
// the `previous_filename` field only exists in `RENAMED` file,
// fallback to `filename` otherwise
const baseFilePath = commitFile.previousPath || commitFile.path;
const headFilePath = commitFile.path;
return {
baseFileUri: vscode.Uri.joinPath(baseRootUri, baseFilePath),
headFileUri: vscode.Uri.joinPath(headRootUri, headFilePath),
status: commitFile.status,
};
});
};
export const getChangedFiles = async (): Promise<VSCodeChangedFile[]> => {
const routerState = await router.getState();
const scheme = adapterManager.getCurrentScheme();
// code review page
if (routerState.pageType === adapterTypes.PageType.CodeReview) {
const repository = Repository.getInstance(scheme, routerState.repo);
const codeReview = await repository.getCodeReviewItem(routerState.codeReviewId);
return codeReview ? getCodeReviewChangedFiles(codeReview) : [];
}
// commit page
else if (routerState.pageType === adapterTypes.PageType.Commit) {
const repository = Repository.getInstance(scheme, routerState.repo);
const commit = await repository.getCommitItem(routerState.commitSha);
return commit ? getCommitChangedFiles(commit) : [];
}
return [];
};
// get the title of the diff editor
export const getChangedFileDiffTitle = (
baseFileUri: vscode.Uri,
headFileUri: vscode.Uri,
status: adapterTypes.FileChangeStatus
) => {
const baseFileName = basename(baseFileUri.path);
const headFileName = basename(headFileUri.path);
const [_repo, baseCommitSha] = baseFileUri.authority.split('+');
const [__repo, headCommitSha] = headFileUri.authority.split('+');
const baseFileLabel = `${baseFileName} (${baseCommitSha?.slice(0, 7)})`;
const headFileLabel = `${headFileName} (${headCommitSha?.slice(0, 7)})`;
if (status === adapterTypes.FileChangeStatus.Added) {
return `${headFileName} (added in ${headCommitSha?.slice(0, 7)})`;
}
if (status === adapterTypes.FileChangeStatus.Removed) {
return `${baseFileName} (deleted from ${baseCommitSha?.slice(0, 7)})`;
}
return `${baseFileLabel} ⟷ ${headFileLabel}`;
};
export const getChangedFileDiffCommand = (changedFile: VSCodeChangedFile): vscode.Command => {
let baseFileUri = changedFile.baseFileUri;
let headFileUri = changedFile.headFileUri;
const status = changedFile.status;
if (status === adapterTypes.FileChangeStatus.Added) {
baseFileUri = emptyFileUri;
}
if (status === adapterTypes.FileChangeStatus.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],
};
};