forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-tab-controller.js
More file actions
305 lines (268 loc) · 9.27 KB
/
Copy pathgit-tab-controller.js
File metadata and controls
305 lines (268 loc) · 9.27 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/** @jsx etch.dom */
/* eslint react/no-unknown-property: "off" */
import path from 'path';
import etch from 'etch';
import GitTabView from '../views/git-tab-view';
import ModelObserver from '../models/model-observer';
import {autobind} from 'core-decorators';
import yubikiri from 'yubikiri';
export default class GitTabController {
constructor(props) {
this.props = props;
this.stagingOperationInProgress = false;
this.repositoryObserver = new ModelObserver({
fetchData: this.fetchRepositoryData,
didUpdate: () => {
this.refreshResolutionProgress(false, false);
return etch.update(this);
},
});
this.repositoryObserver.setActiveModel(props.repository);
etch.initialize(this);
}
render() {
const modelData = this.repositoryObserver.getActiveModelData() || {fetchInProgress: true};
const hasUndoHistory = this.props.repository ? this.hasUndoHistory() : false;
return (
<GitTabView
ref="gitPanel"
{...modelData}
repository={this.props.repository}
resolutionProgress={this.props.resolutionProgress}
workspace={this.props.workspace}
commandRegistry={this.props.commandRegistry}
notificationManager={this.props.notificationManager}
initializeRepo={this.props.initializeRepo}
didSelectFilePath={this.props.didSelectFilePath}
didDiveIntoFilePath={this.props.didDiveIntoFilePath}
didSelectMergeConflictFile={this.props.didSelectMergeConflictFile}
didDiveIntoMergeConflictPath={this.props.didDiveIntoMergeConflictPath}
focusFilePatchView={this.props.focusFilePatchView}
stageFilePatch={this.stageFilePatch}
unstageFilePatch={this.unstageFilePatch}
openFiles={this.props.openFiles}
discardWorkDirChangesForPaths={this.props.discardWorkDirChangesForPaths}
attemptFileStageOperation={this.attemptFileStageOperation}
prepareToCommit={this.prepareToCommit}
commit={this.commit}
setAmending={this.setAmending}
isAmending={this.props.isAmending}
hasUndoHistory={hasUndoHistory}
undoLastDiscard={this.props.undoLastDiscard}
abortMerge={this.abortMerge}
resolveAsOurs={this.resolveAsOurs}
resolveAsTheirs={this.resolveAsTheirs}
push={this.push}
pull={this.pull}
fetch={this.fetch}
checkout={this.checkout}
/>
);
}
async update(props) {
const oldProps = this.props;
this.props = {...this.props, ...props};
if (this.props.repository !== oldProps.repository) {
await this.repositoryObserver.setActiveModel(props.repository);
} else if (this.props.isAmending !== oldProps.isAmending) {
await this.repositoryObserver.refreshModelData(this.getActiveRepository());
}
return etch.update(this);
}
destroy() {
this.repositoryObserver.destroy();
}
getLastModelDataRefreshPromise() {
return this.repositoryObserver.getLastModelDataRefreshPromise();
}
getActiveRepository() {
return this.repositoryObserver.getActiveModel();
}
refreshModelData() {
return this.repositoryObserver.refreshModelData();
}
@autobind
fetchRepositoryData(repository) {
return yubikiri({
lastCommit: repository.getLastCommit(),
isMerging: repository.isMerging(),
isRebasing: repository.isRebasing(),
currentBranch: repository.getCurrentBranch(),
unstagedChanges: repository.getUnstagedChanges(),
stagedChanges: this.fetchStagedChanges(repository),
mergeConflicts: repository.getMergeConflicts(),
workingDirectoryPath: repository.getWorkingDirectoryPath(),
mergeMessage: async query => {
const isMerging = await query.isMerging;
return isMerging ? repository.getMergeMessage() : null;
},
});
}
fetchStagedChanges(repository) {
if (this.props.isAmending) {
return repository.getStagedChangesSinceParentCommit();
} else {
return repository.getStagedChanges();
}
}
/*
* Begin (but don't await) an async conflict-counting task for each merge conflict path that has no conflict
* marker count yet. Omit any path that's already open in a TextEditor or that has already been counted.
*
* includeOpen - update marker counts for files that are currently open in TextEditors
* includeCounts - update marker counts for files that have been counted before
*/
refreshResolutionProgress(includeOpen, includeCounted) {
const data = this.repositoryObserver.getActiveModelData();
if (!data) {
return;
}
const openPaths = new Set(
this.props.workspace.getTextEditors().map(editor => editor.getPath()),
);
for (let i = 0; i < data.mergeConflicts.length; i++) {
const conflictPath = path.join(data.workingDirectoryPath, data.mergeConflicts[i].filePath);
if (!includeOpen && openPaths.has(conflictPath)) {
continue;
}
if (!includeCounted && this.props.resolutionProgress.getRemaining(conflictPath) !== undefined) {
continue;
}
this.props.refreshResolutionProgress(conflictPath);
}
}
unstageFilePatch(filePatch) {
return this.getActiveRepository().applyPatchToIndex(filePatch.getUnstagePatch());
}
@autobind
attemptFileStageOperation(filePaths, stageStatus) {
if (this.stagingOperationInProgress) {
return {
stageOperationPromise: Promise.resolve(),
selectionUpdatePromise: Promise.resolve(),
};
}
this.stagingOperationInProgress = true;
const fileListUpdatePromise = this.refs.gitPanel.refs.stagingView.getNextListUpdatePromise();
let stageOperationPromise;
if (stageStatus === 'staged') {
stageOperationPromise = this.unstageFiles(filePaths);
} else {
stageOperationPromise = this.stageFiles(filePaths);
}
const selectionUpdatePromise = fileListUpdatePromise.then(() => {
this.stagingOperationInProgress = false;
});
return {stageOperationPromise, selectionUpdatePromise};
}
async stageFiles(filePaths) {
const pathsToIgnore = [];
const repository = this.getActiveRepository();
for (const filePath of filePaths) {
if (await repository.pathHasMergeMarkers(filePath)) { // eslint-disable-line babel/no-await-in-loop
const choice = atom.confirm({
message: 'File contains merge markers: ',
detailedMessage: `Do you still want to stage this file?\n${filePath}`,
buttons: ['Stage', 'Cancel'],
});
if (choice !== 0) { pathsToIgnore.push(filePath); }
}
}
const pathsToStage = filePaths.filter(filePath => !pathsToIgnore.includes(filePath));
return repository.stageFiles(pathsToStage);
}
@autobind
unstageFiles(filePaths) {
const repository = this.getActiveRepository();
if (this.props.isAmending) {
return repository.stageFilesFromParentCommit(filePaths);
} else {
return repository.unstageFiles(filePaths);
}
}
@autobind
async prepareToCommit() {
return !await this.props.ensureGitPanel();
}
@autobind
async commit(message) {
try {
await this.getActiveRepository().commit(message, {amend: this.props.isAmending});
this.setAmending(false);
} catch (e) {
if (e.code === 'ECONFLICT') {
this.props.notificationManager.addError(
'Cannot commit without resolving all the merge conflicts first.',
{dismissable: true},
);
} else {
console.error(e); // eslint-disable-line no-console
}
}
}
@autobind
setAmending(isAmending) {
this.props.didChangeAmending(isAmending);
}
@autobind
async abortMerge() {
const choice = atom.confirm({
message: 'Abort merge',
detailedMessage: 'Are you sure?',
buttons: ['Abort', 'Cancel'],
});
if (choice !== 0) { return null; }
try {
await this.getActiveRepository().abortMerge();
} catch (e) {
if (e.code === 'EDIRTYSTAGED') {
this.props.notificationManager.addError(
`Cannot abort because ${e.path} is both dirty and staged.`,
{dismissable: true},
);
}
}
return etch.update(this);
}
@autobind
async resolveAsOurs(paths) {
const data = this.repositoryObserver.getActiveModelData();
if (!data) {
return;
}
const side = data.isRebasing ? 'theirs' : 'ours';
await this.getActiveRepository().checkoutSide(side, paths);
this.refreshResolutionProgress(false, true);
}
@autobind
async resolveAsTheirs(paths) {
const data = this.repositoryObserver.getActiveModelData();
if (!data) {
return;
}
const side = data.isRebasing ? 'ours' : 'theirs';
await this.getActiveRepository().checkoutSide(side, paths);
this.refreshResolutionProgress(false, true);
}
@autobind
checkout(branchName, options) {
return this.getActiveRepository().checkout(branchName, options);
}
focus() {
this.refs.gitPanel.focus();
}
focusAndSelectStagingItem(filePath, stagingStatus) {
return this.refs.gitPanel.focusAndSelectStagingItem(filePath, stagingStatus);
}
isFocused() {
return this.refs.gitPanel.isFocused();
}
@autobind
quietlySelectItem(filePath, stagingStatus) {
return this.refs.gitPanel.quitelySelectItem(filePath, stagingStatus);
}
@autobind
hasUndoHistory() {
return this.props.repository.hasDiscardHistory();
}
}