-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
fetch.ts
189 lines (161 loc) · 5.41 KB
/
fetch.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
import { GlyphChars } from '../../constants';
import { Container } from '../../container';
import { GitBranchReference, GitReference, Repository } from '../../git/models';
import { FlagsQuickPickItem } from '../../quickpicks';
import { isStringArray } from '../../system/array';
import { fromNow } from '../../system/date';
import { pad } from '../../system/string';
import { ViewsWithRepositoryFolders } from '../../views/viewBase';
import {
appendReposToTitle,
AsyncStepResultGenerator,
PartialStepState,
pickRepositoriesStep,
QuickCommand,
QuickPickStep,
StepGenerator,
StepResult,
StepSelection,
StepState,
} from '../quickCommand';
interface Context {
repos: Repository[];
associatedView: ViewsWithRepositoryFolders;
title: string;
}
type Flags = '--all' | '--prune';
interface State {
repos: string | string[] | Repository | Repository[];
reference?: GitBranchReference;
flags: Flags[];
}
export interface FetchGitCommandArgs {
readonly command: 'fetch';
confirm?: boolean;
state?: Partial<State>;
}
type FetchStepState<T extends State = State> = ExcludeSome<StepState<T>, 'repos', string | string[] | Repository>;
export class FetchGitCommand extends QuickCommand<State> {
constructor(container: Container, args?: FetchGitCommandArgs) {
super(container, 'fetch', 'fetch', 'Fetch', { description: 'fetches changes from one or more remotes' });
let counter = 0;
if (args?.state?.repos != null && (!Array.isArray(args.state.repos) || args.state.repos.length !== 0)) {
counter++;
}
this.initialState = {
counter: counter,
confirm: args?.confirm,
...args?.state,
};
}
execute(state: FetchStepState) {
if (GitReference.isBranch(state.reference)) {
return state.repos[0].fetch({ branch: state.reference });
}
return this.container.git.fetchAll(state.repos, {
all: state.flags.includes('--all'),
prune: state.flags.includes('--prune'),
});
}
protected async *steps(state: PartialStepState<State>): StepGenerator {
const context: Context = {
repos: this.container.git.openRepositories,
associatedView: this.container.commitsView,
title: this.title,
};
if (state.flags == null) {
state.flags = [];
}
if (state.repos != null && !Array.isArray(state.repos)) {
state.repos = [state.repos as string];
}
let skippedStepOne = false;
while (this.canStepsContinue(state)) {
context.title = this.title;
if (state.counter < 1 || state.repos == null || state.repos.length === 0 || isStringArray(state.repos)) {
skippedStepOne = false;
if (context.repos.length === 1) {
skippedStepOne = true;
state.counter++;
state.repos = [context.repos[0]];
} else {
const result = yield* pickRepositoriesStep(
state as ExcludeSome<typeof state, 'repos', string | Repository>,
context,
{ skipIfPossible: state.counter >= 1 },
);
// Always break on the first step (so we will go back)
if (result === StepResult.Break) break;
state.repos = result;
}
}
if (this.confirm(state.confirm)) {
const result = yield* this.confirmStep(state as FetchStepState, context);
if (result === StepResult.Break) {
// If we skipped the previous step, make sure we back up past it
if (skippedStepOne) {
state.counter--;
}
continue;
}
state.flags = result;
}
QuickCommand.endSteps(state);
void this.execute(state as FetchStepState);
}
return state.counter < 0 ? StepResult.Break : undefined;
}
private async *confirmStep(state: FetchStepState, context: Context): AsyncStepResultGenerator<Flags[]> {
let lastFetchedOn = '';
if (state.repos.length === 1) {
const lastFetched = await state.repos[0].getLastFetched();
if (lastFetched !== 0) {
lastFetchedOn = `${pad(GlyphChars.Dot, 2, 2)}Last fetched ${fromNow(new Date(lastFetched))}`;
}
}
let step: QuickPickStep<FlagsQuickPickItem<Flags>>;
if (state.repos.length === 1 && GitReference.isBranch(state.reference)) {
step = this.createConfirmStep(
appendReposToTitle(`Confirm ${context.title}`, state, context, lastFetchedOn),
[
FlagsQuickPickItem.create<Flags>(state.flags, [], {
label: this.title,
detail: `Will fetch ${GitReference.toString(state.reference)}`,
}),
],
);
} else {
const reposToFetch =
state.repos.length === 1
? `$(repo) ${state.repos[0].formattedName}`
: `${state.repos.length} repositories`;
step = QuickCommand.createConfirmStep(
appendReposToTitle(`Confirm ${this.title}`, state, context, lastFetchedOn),
[
FlagsQuickPickItem.create<Flags>(state.flags, [], {
label: this.title,
detail: `Will fetch ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--prune'], {
label: `${this.title} & Prune`,
description: '--prune',
detail: `Will fetch and prune ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--all'], {
label: `${this.title} All`,
description: '--all',
detail: `Will fetch all remotes of ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--all', '--prune'], {
label: `${this.title} All & Prune`,
description: '--all --prune',
detail: `Will fetch and prune all remotes of ${reposToFetch}`,
}),
],
context,
);
}
const selection: StepSelection<typeof step> = yield step;
return QuickCommand.canPickStepContinue(step, state, selection) ? selection[0].item : StepResult.Break;
}
}