Repository navigation
Expand file tree
/
Copy pathfiles.ts
More file actions
128 lines (111 loc) · 4.35 KB
/
Copy pathfiles.ts
File metadata and controls
128 lines (111 loc) · 4.35 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
/**
* @file source control change file list
* @author netcon
*/
import * as vscode from 'vscode';
import * as adapterTypes from '@/adapters/types';
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 repository = Repository.getCurrentInstance();
const baseRootUri = router.buildUri({ ref: codeReview.targetSha });
const headRootUri = router.buildUri({ ref: codeReview.sourceSha }, baseRootUri);
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: baseRootUri.with({ path: baseFilePath }),
headFileUri: headRootUri.with({ path: headFilePath }),
status: changedFile.status,
};
});
};
export const getCommitChangedFiles = async (
commit: adapterTypes.Commit,
repository = Repository.getCurrentInstance(),
) => {
const { scheme, repo } = repository;
// 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 parentCommitSha = commit?.parents?.[0] || '';
const headRootUri = router.buildUri({ scheme, repo, ref: commit.sha });
const baseRootUri = router.buildUri({ ref: parentCommitSha }, headRootUri);
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: baseRootUri.with({ path: baseFilePath }),
headFileUri: headRootUri.with({ path: headFilePath }),
status: commitFile.status,
};
});
};
export const getChangedFiles = async (): Promise<VSCodeChangedFile[]> => {
const routerState = router.getState();
const repository = Repository.getCurrentInstance();
// code review page
if (routerState.pageType === adapterTypes.PageType.CodeReview) {
const codeReview = await repository.getCodeReviewItem(routerState.codeReviewId);
return codeReview ? getCodeReviewChangedFiles(codeReview) : [];
}
// commit page
else if (routerState.pageType === adapterTypes.PageType.Commit) {
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 { ref: baseCommitSha } = router.parseUri(baseFileUri);
const { ref: headCommitSha } = router.parseUri(headFileUri);
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);
return {
title: 'Diff',
command: 'vscode.diff',
arguments: [baseFileUri, headFileUri, title],
};
};