/
merge.ts
218 lines (187 loc) · 6.11 KB
/
merge.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
'use strict';
import { Container } from '../../container';
import { GitReference, GitRevision, Repository } from '../../git/gitService';
import { GlyphChars } from '../../constants';
import {
getBranchesAndOrTags,
getValidateGitReferenceFn,
QuickCommandBase,
QuickPickStep,
StepAsyncGenerator,
StepSelection,
StepState
} from '../quickCommand';
import {
Directive,
DirectiveQuickPickItem,
FlagsQuickPickItem,
ReferencesQuickPickItem,
RepositoryQuickPickItem
} from '../../quickpicks';
import { Strings } from '../../system';
import { Logger } from '../../logger';
type Flags = '--ff-only' | '--no-ff' | '--squash';
interface State {
repo: Repository;
reference: GitReference;
flags: Flags[];
}
export interface MergeGitCommandArgs {
readonly command: 'merge';
state?: Partial<State>;
}
export class MergeGitCommand extends QuickCommandBase<State> {
constructor(args?: MergeGitCommandArgs) {
super('merge', 'merge', 'Merge', {
description: 'integrates changes from a specified branch into the current branch'
});
if (args == null || args.state === undefined) return;
let counter = 0;
if (args.state.repo !== undefined) {
counter++;
}
if (args.state.reference !== undefined) {
counter++;
}
this._initialState = {
counter: counter,
confirm: true,
...args.state
};
}
get canSkipConfirm(): boolean {
return false;
}
execute(state: State) {
return state.repo.merge(...state.flags, state.reference.ref);
}
protected async *steps(): StepAsyncGenerator {
const state: StepState<State> = this._initialState === undefined ? { counter: 0 } : this._initialState;
let repos;
if (state.flags == null) {
state.flags = [];
}
while (true) {
try {
if (repos === undefined) {
repos = [...(await Container.git.getOrderedRepositories())];
}
if (state.repo === undefined || state.counter < 1) {
if (repos.length === 1) {
state.counter++;
state.repo = repos[0];
} else {
const active = state.repo ? state.repo : await Container.git.getActiveRepository();
const step = this.createPickStep<RepositoryQuickPickItem>({
title: this.title,
placeholder: 'Choose a repository',
items: await Promise.all(
repos.map(r =>
RepositoryQuickPickItem.create(r, r.id === (active && active.id), {
branch: true,
fetched: true,
status: true
})
)
)
});
const selection: StepSelection<typeof step> = yield step;
if (!this.canPickStepMoveNext(step, state, selection)) {
break;
}
state.repo = selection[0].item;
}
}
const destination = await state.repo.getBranch();
if (destination === undefined) break;
if (state.reference === undefined || state.counter < 2) {
const destId = destination.id;
const step = this.createPickStep<ReferencesQuickPickItem>({
title: `${this.title} into ${destination.name}${Strings.pad(GlyphChars.Dot, 2, 2)}${
state.repo.formattedName
}`,
placeholder: `Choose a branch or tag to merge into ${destination.name}${GlyphChars.Space.repeat(
3
)}(select or enter a reference)`,
matchOnDescription: true,
matchOnDetail: true,
items: await getBranchesAndOrTags(state.repo, ['branches', 'tags'], {
filterBranches: b => b.id !== destId,
picked: state.reference && state.reference.ref
}),
onValidateValue: getValidateGitReferenceFn(state.repo)
});
const selection: StepSelection<typeof step> = yield step;
if (!this.canPickStepMoveNext(step, state, selection)) {
if (repos.length === 1) {
break;
}
continue;
}
state.reference = selection[0].item;
}
const count =
(await Container.git.getCommitCount(state.repo.path, [
GitRevision.createRange(destination.name, state.reference.name)
])) || 0;
if (count === 0) {
const step = this.createConfirmStep(
`Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${state.repo.formattedName}`,
[],
DirectiveQuickPickItem.create(Directive.Cancel, true, {
label: `Cancel ${this.title}`,
detail: `${destination.name} is up to date with ${state.reference.name}`
})
);
yield step;
break;
}
const step: QuickPickStep<FlagsQuickPickItem<Flags>> = this.createConfirmStep(
`Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${state.repo.formattedName}`,
[
FlagsQuickPickItem.create<Flags>(state.flags, [], {
label: this.title,
description: `${state.reference.name} into ${destination.name}`,
detail: `Will merge ${Strings.pluralize('commit', count)} from ${
state.reference.name
} into ${destination.name}`
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--ff-only'], {
label: `Fast-forward ${this.title}`,
description: `--ff-only ${state.reference.name} into ${destination.name}`,
detail: `Will fast-forward merge ${Strings.pluralize('commit', count)} from ${
state.reference.name
} into ${destination.name}`
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--no-ff'], {
label: `No Fast-forward ${this.title}`,
description: `--no-ff ${state.reference.name} into ${destination.name}`,
detail: `Will create a merge commit when merging ${Strings.pluralize(
'commit',
count
)} from ${state.reference.name} into ${destination.name}`
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--squash'], {
label: `Squash ${this.title}`,
description: `--squash ${state.reference.name} into ${destination.name}`,
detail: `Will squash ${Strings.pluralize('commit', count)} from ${
state.reference.name
} into one when merging into ${destination.name}`
})
]
);
const selection: StepSelection<typeof step> = yield step;
if (!this.canPickStepMoveNext(step, state, selection)) {
continue;
}
state.flags = selection[0].item;
this.execute(state as State);
break;
} catch (ex) {
Logger.error(ex, this.title);
throw ex;
}
}
return undefined;
}
}