-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
rebase.ts
272 lines (233 loc) · 8.18 KB
/
rebase.ts
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
'use strict';
import { env } from 'vscode';
import { Container } from '../../container';
import { GitBranch, GitLog, GitReference, GitRevision, Repository } from '../../git/git';
import { Directive, DirectiveQuickPickItem, FlagsQuickPickItem } from '../../quickpicks';
import { Strings } from '../../system';
import { ViewsWithRepositoryFolders } from '../../views/viewBase';
import {
appendReposToTitle,
AsyncStepResultGenerator,
PartialStepState,
pickBranchOrTagStep,
pickCommitStep,
pickRepositoryStep,
QuickCommand,
QuickCommandButtons,
QuickPickStep,
StepGenerator,
StepResult,
StepSelection,
StepState,
} from '../quickCommand';
interface Context {
repos: Repository[];
associatedView: ViewsWithRepositoryFolders;
cache: Map<string, Promise<GitLog | undefined>>;
destination: GitBranch;
pickCommit: boolean;
pickCommitForItem: boolean;
selectedBranchOrTag: GitReference | undefined;
showTags: boolean;
title: string;
}
type Flags = '--interactive';
interface State {
repo: string | Repository;
reference: GitReference;
flags: Flags[];
}
export interface RebaseGitCommandArgs {
readonly command: 'rebase';
state?: Partial<State>;
}
type RebaseStepState<T extends State = State> = ExcludeSome<StepState<T>, 'repo', string>;
export class RebaseGitCommand extends QuickCommand<State> {
constructor(args?: RebaseGitCommandArgs) {
super('rebase', 'rebase', 'Rebase', {
description:
'integrates changes from a specified branch into the current branch, by changing the base of the branch and reapplying the commits on top',
});
let counter = 0;
if (args?.state?.repo != null) {
counter++;
}
if (args?.state?.reference != null) {
counter++;
}
this.initialState = {
counter: counter,
confirm: true,
...args?.state,
};
}
override get canSkipConfirm(): boolean {
return false;
}
async execute(state: RebaseStepState) {
let configs: string[] | undefined;
if (state.flags.includes('--interactive')) {
await Container.instance.rebaseEditor.enableForNextUse();
let editor;
switch (env.appName) {
case 'Visual Studio Code - Insiders':
editor = 'code-insiders --wait --reuse-window';
break;
case 'Visual Studio Code - Exploration':
editor = 'code-exploration --wait --reuse-window';
break;
default:
editor = 'code --wait --reuse-window';
break;
}
configs = ['-c', `sequence.editor="${editor}"`];
}
return state.repo.rebase(configs, ...state.flags, state.reference.ref);
}
protected async *steps(state: PartialStepState<State>): StepGenerator {
const context: Context = {
repos: Container.instance.git.openRepositories,
associatedView: Container.instance.commitsView,
cache: new Map<string, Promise<GitLog | undefined>>(),
destination: undefined!,
pickCommit: false,
pickCommitForItem: false,
selectedBranchOrTag: undefined,
showTags: true,
title: this.title,
};
if (state.flags == null) {
state.flags = [];
}
let skippedStepOne = false;
while (this.canStepsContinue(state)) {
context.title = this.title;
if (state.counter < 1 || state.repo == null || typeof state.repo === 'string') {
skippedStepOne = false;
if (context.repos.length === 1) {
skippedStepOne = true;
if (state.repo == null) {
state.counter++;
}
state.repo = context.repos[0];
} else {
const result = yield* pickRepositoryStep(state, context);
// Always break on the first step (so we will go back)
if (result === StepResult.Break) break;
state.repo = result;
}
}
if (context.destination == null) {
const branch = await state.repo.getBranch();
if (branch == null) break;
context.destination = branch;
}
context.title = `${this.title} ${GitReference.toString(context.destination, { icon: false })}`;
context.pickCommitForItem = false;
if (state.counter < 2 || state.reference == null) {
const pickCommitToggle = new QuickCommandButtons.PickCommitToggle(context.pickCommit, context, () => {
context.pickCommit = !context.pickCommit;
pickCommitToggle.on = context.pickCommit;
});
const result: StepResult<GitReference> = yield* pickBranchOrTagStep(state as RebaseStepState, context, {
placeholder: context => `Choose a branch${context.showTags ? ' or tag' : ''} to rebase`,
picked: context.selectedBranchOrTag?.ref,
value: context.selectedBranchOrTag == null ? state.reference?.ref : undefined,
additionalButtons: [pickCommitToggle],
});
if (result === StepResult.Break) {
// If we skipped the previous step, make sure we back up past it
if (skippedStepOne) {
state.counter--;
}
continue;
}
state.reference = result;
context.selectedBranchOrTag = undefined;
}
if (!GitReference.isRevision(state.reference)) {
context.selectedBranchOrTag = state.reference;
}
if (
state.counter < 3 &&
context.selectedBranchOrTag != null &&
(context.pickCommit || context.pickCommitForItem || state.reference.ref === context.destination.ref)
) {
const ref = context.selectedBranchOrTag.ref;
let log = context.cache.get(ref);
if (log == null) {
log = Container.instance.git.getLog(state.repo.path, { ref: ref, merges: false });
context.cache.set(ref, log);
}
const result: StepResult<GitReference> = yield* pickCommitStep(state as RebaseStepState, context, {
ignoreFocusOut: true,
log: await log,
onDidLoadMore: log => context.cache.set(ref, Promise.resolve(log)),
placeholder: (context, log) =>
log == null
? `No commits found on ${GitReference.toString(context.selectedBranchOrTag, {
icon: false,
})}`
: `Choose a commit to rebase ${GitReference.toString(context.destination, {
icon: false,
})} onto`,
picked: state.reference?.ref,
});
if (result === StepResult.Break) continue;
state.reference = result;
}
const result = yield* this.confirmStep(state as RebaseStepState, context);
if (result === StepResult.Break) continue;
state.flags = result;
QuickCommand.endSteps(state);
void this.execute(state as RebaseStepState);
}
return state.counter < 0 ? StepResult.Break : undefined;
}
private async *confirmStep(state: RebaseStepState, context: Context): AsyncStepResultGenerator<Flags[]> {
const aheadBehind = await Container.instance.git.getAheadBehindCommitCount(state.repo.path, [
state.reference.refType === 'revision'
? GitRevision.createRange(state.reference.ref, context.destination.ref)
: GitRevision.createRange(context.destination.name, state.reference.name),
]);
const count = aheadBehind != null ? aheadBehind.ahead + aheadBehind.behind : 0;
if (count === 0) {
const step: QuickPickStep<DirectiveQuickPickItem> = this.createConfirmStep(
appendReposToTitle(`Confirm ${context.title}`, state, context),
[],
DirectiveQuickPickItem.create(Directive.Cancel, true, {
label: `Cancel ${this.title}`,
detail: `${GitReference.toString(context.destination, {
capitalize: true,
})} is up to date with ${GitReference.toString(state.reference)}`,
}),
);
const selection: StepSelection<typeof step> = yield step;
QuickCommand.canPickStepContinue(step, state, selection);
return StepResult.Break;
}
const step: QuickPickStep<FlagsQuickPickItem<Flags>> = this.createConfirmStep(
appendReposToTitle(`Confirm ${context.title}`, state, context),
[
FlagsQuickPickItem.create<Flags>(state.flags, [], {
label: this.title,
detail: `Will update ${GitReference.toString(context.destination)} by applying ${Strings.pluralize(
'commit',
count,
)} on top of ${GitReference.toString(state.reference)}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--interactive'], {
label: `Interactive ${this.title}`,
description: '--interactive',
detail: `Will interactively update ${GitReference.toString(
context.destination,
)} by applying ${Strings.pluralize('commit', count)} on top of ${GitReference.toString(
state.reference,
)}`,
}),
],
);
const selection: StepSelection<typeof step> = yield step;
return QuickCommand.canPickStepContinue(step, state, selection) ? selection[0].item : StepResult.Break;
}
}