This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
get-repo-pipeline-manager.js
227 lines (214 loc) · 8.5 KB
/
get-repo-pipeline-manager.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import createDOMPurify from 'dompurify';
import ActionPipelineManager from './action-pipeline';
import {GitError} from './git-shell-out-strategy';
import {deleteFileOrFolder, getCommitMessagePath, getCommitMessageEditors, destroyFilePatchPaneItems} from './helpers';
const DOMPurify = createDOMPurify();
// Note: Middleware that catches errors should re-throw the errors so that they propogate
// and other middleware in the pipeline can be made aware of the errors.
// Ultimately, the views are responsible for catching the errors and handling them accordingly
export default function({confirm, notificationManager, workspace}) {
const pipelineManager = new ActionPipelineManager({
actionNames: ['PUSH', 'PULL', 'FETCH', 'COMMIT', 'CHECKOUT'],
});
const pushPipeline = pipelineManager.getPipeline(pipelineManager.actionKeys.PUSH);
pushPipeline.addMiddleware('confirm-force-push', async (next, repository, branchName, options) => {
if (options.force) {
const choice = confirm({
message: 'Are you sure you want to force push?',
detailedMessage: 'This operation could result in losing data on the remote.',
buttons: ['Force Push', 'Cancel Push'],
});
if (choice !== 0) { /* do nothing */ } else { await next(); }
} else {
await next();
}
});
pushPipeline.addMiddleware('set-push-in-progress', async (next, repository, branchName, options) => {
repository.getOperationStates().setPushInProgress(true);
try {
await next();
} finally {
repository.getOperationStates().setPushInProgress(false);
}
});
pushPipeline.addMiddleware('failed-to-push-error', async (next, repository, branchName, options) => {
try {
const result = await next();
return result;
} catch (error) {
if (error instanceof GitError) {
if (/rejected[\s\S]*failed to push/.test(error.stdErr)) {
notificationManager.addError('Push rejected', {
description: 'The tip of your current branch is behind its remote counterpart.' +
' Try pulling before pushing again. Or, to force push, hold `cmd` or `ctrl` while clicking.',
dismissable: true,
});
} else {
notificationManager.addError('Unable to push', {
detail: error.stdErr,
dismissable: true,
});
}
}
throw error;
}
});
const pullPipeline = pipelineManager.getPipeline(pipelineManager.actionKeys.PULL);
pullPipeline.addMiddleware('set-pull-in-progress', async (next, repository, branchName) => {
repository.getOperationStates().setPullInProgress(true);
try {
await next();
} finally {
repository.getOperationStates().setPullInProgress(false);
}
});
pullPipeline.addMiddleware('failed-to-pull-error', async (next, repository, branchName) => {
try {
const result = await next();
return result;
} catch (error) {
if (error instanceof GitError) {
if (/error: Your local changes to the following files would be overwritten by merge/.test(error.stdErr)) {
const lines = error.stdErr.split('\n');
const files = lines.slice(3, lines.length - 3).map(l => `\`${l.trim()}\``).join('\n');
notificationManager.addError('Pull aborted', {
description: DOMPurify.sanitize(
'Local changes to the following would be overwritten by merge:<br/>' + files +
'<br/>Please commit your changes or stash them before you merge.',
),
dismissable: true,
});
} else if (/Automatic merge failed; fix conflicts and then commit the result./.test(error.stdOut)) {
repository.didMergeError();
notificationManager.addWarning('Merge conflicts', {
description: `Your local changes conflicted with changes made on the remote branch. Resolve the conflicts
with the Git panel and commit to continue.`,
dismissable: true,
});
} else {
notificationManager.addError('Unable to pull', {
detail: error.stdErr,
dismissable: true,
});
}
}
throw error;
}
});
const fetchPipeline = pipelineManager.getPipeline(pipelineManager.actionKeys.FETCH);
fetchPipeline.addMiddleware('set-fetch-in-progress', async (next, repository) => {
repository.getOperationStates().setFetchInProgress(true);
try {
await next();
} finally {
repository.getOperationStates().setFetchInProgress(false);
}
});
fetchPipeline.addMiddleware('failed-to-fetch-error', async (next, repository) => {
try {
const result = await next();
return result;
} catch (error) {
if (error instanceof GitError) {
notificationManager.addError('Unable to fetch', {
detail: error.stdErr,
dismissable: true,
});
}
throw error;
}
});
const checkoutPipeline = pipelineManager.getPipeline(pipelineManager.actionKeys.CHECKOUT);
checkoutPipeline.addMiddleware('set-checkout-in-progress', async (next, repository, branchName) => {
repository.getOperationStates().setCheckoutInProgress(true);
try {
await next();
} finally {
repository.getOperationStates().setCheckoutInProgress(false);
}
});
checkoutPipeline.addMiddleware('failed-to-checkout-error', async (next, repository, branchName, options) => {
try {
const result = await next();
return result;
} catch (error) {
if (error instanceof GitError) {
const message = options.createNew ? 'Cannot create branch' : 'Checkout aborted';
let detail = undefined;
let description = undefined;
if (error.stdErr.match(/local changes.*would be overwritten/)) {
const files = error.stdErr.split(/\r?\n/).filter(l => l.startsWith('\t'))
.map(l => `\`${l.trim()}\``).join('<br/>');
description = DOMPurify.sanitize(
'Local changes to the following would be overwritten:<br/>' + files +
'<br/>Please commit your changes or stash them.',
);
} else if (error.stdErr.match(/branch.*already exists/)) {
description = DOMPurify.sanitize(`\`${branchName}\` already exists. Choose another branch name.`);
} else if (error.stdErr.match(/error: you need to resolve your current index first/)) {
description = 'You must first resolve merge conflicts.';
}
if (description === undefined && detail === undefined) {
detail = error.stdErr;
}
notificationManager.addError(message, {description, detail, dismissable: true});
}
throw error;
}
});
const commitPipeline = pipelineManager.getPipeline(pipelineManager.actionKeys.COMMIT);
commitPipeline.addMiddleware('confirm-commit', async (next, repository) => {
function confirmCommit() {
const choice = confirm({
message: 'One or more text editors for the commit message are unsaved.',
detailedMessage: 'Do you want to commit and close all open commit message editors?',
buttons: ['Commit', 'Cancel'],
});
return choice === 0;
}
const commitMessageEditors = getCommitMessageEditors(repository, workspace);
if (commitMessageEditors.length > 0) {
if (!commitMessageEditors.some(e => e.isModified()) || confirmCommit()) {
await next();
commitMessageEditors.forEach(editor => editor.destroy());
}
} else {
await next();
}
});
commitPipeline.addMiddleware('clean-up-disk-commit-msg', async (next, repository) => {
await next();
try {
await deleteFileOrFolder(getCommitMessagePath(repository));
} catch (error) {
// do nothing
}
});
commitPipeline.addMiddleware('set-commit-in-progress', async (next, repository) => {
repository.getOperationStates().setCommitInProgress(true);
try {
await next();
} finally {
repository.getOperationStates().setCommitInProgress(false);
}
});
commitPipeline.addMiddleware('failed-to-commit-error', async (next, repository) => {
try {
const result = await next();
repository.setAmending(false);
repository.setAmendingCommitMessage('');
repository.setRegularCommitMessage('');
destroyFilePatchPaneItems({onlyStaged: true}, workspace);
return result;
} catch (error) {
if (error instanceof GitError) {
notificationManager.addError('Unable to commit', {
detail: error.stdErr,
dismissable: true,
});
}
throw error;
}
});
return pipelineManager;
}