forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockRepository.ts
More file actions
374 lines (317 loc) · 11 KB
/
Copy pathmockRepository.ts
File metadata and controls
374 lines (317 loc) · 11 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { EventEmitter, Uri } from 'vscode';
import { RefType } from '../../api/api1';
import type {
Repository,
RepositoryState,
RepositoryUIState,
Commit,
Change,
Branch,
CommitOptions,
InputBox,
Ref,
BranchQuery,
FetchOptions,
RefQuery,
Worktree,
} from '../../api/api';
type Mutable<T> = {
-readonly [P in keyof T]: T[P];
};
export class MockRepository implements Repository {
add(paths: string[]): Promise<void> {
return Promise.reject(new Error(`Unexpected add(${paths.join(', ')})`));
}
commit(message: string, opts?: CommitOptions): Promise<void> {
return Promise.reject(new Error(`Unexpected commit(${message}, ${opts})`));
}
renameRemote(name: string, newName: string): Promise<void> {
return Promise.reject(new Error(`Unexpected renameRemote (${name}, ${newName})`));
}
getGlobalConfig(key: string): Promise<string> {
return Promise.reject(new Error(`Unexpected getGlobalConfig(${key})`));
}
detectObjectType(object: string): Promise<{ mimetype: string; encoding?: string | undefined }> {
return Promise.reject(new Error(`Unexpected detectObjectType(${object})`));
}
buffer(ref: string, path: string): Promise<Buffer> {
return Promise.reject(new Error(`Unexpected buffer(${ref}, ${path})`));
}
clean(paths: string[]): Promise<void> {
return Promise.reject(new Error(`Unexpected clean(${paths})`));
}
diffWithHEAD(path?: any): any {
return Promise.reject(new Error(`Unexpected diffWithHEAD(${path})`));
}
diffIndexWithHEAD(path?: any): any {
return Promise.reject(new Error(`Unexpected diffIndexWithHEAD(${path})`));
}
diffIndexWith(ref: any, path?: any): any {
return Promise.reject(new Error(`Unexpected diffIndexWith(${ref}, ${path})`));
}
getMergeBase(ref1: string, ref2: string): Promise<string> {
return Promise.reject(new Error(`Unexpected getMergeBase(${ref1}, ${ref2})`));
}
async getRefs(_query: RefQuery, _cancellationToken?: any): Promise<Ref[]> {
// ignore the query
return this._state.refs;
}
log(options?: any): Promise<Commit[]> {
return Promise.reject(new Error(`Unexpected log(${options})`));
}
private _onDidChangeState = new EventEmitter<void>();
private _state: Mutable<RepositoryState & { refs: Ref[] }> = {
HEAD: {
type: RefType.Head
},
refs: [],
remotes: [],
submodules: [],
worktrees: undefined,
rebaseCommit: undefined,
mergeChanges: [],
indexChanges: [],
workingTreeChanges: [],
onDidChange: this._onDidChangeState.event,
};
private _config: { key: string; value: string }[] = [];
private _branches: Branch[] = [];
preserveConfigOnNextBranchDelete = false;
private _expectedFetches: { remoteName?: string; ref?: string; depth?: number }[] = [];
private _expectedPulls: { unshallow?: boolean }[] = [];
private _expectedPushes: { remoteName?: string; branchName?: string; setUpstream?: boolean }[] = [];
inputBox: InputBox = { value: '' };
rootUri = Uri.file('/root');
state: RepositoryState = this._state;
ui: RepositoryUIState = {
selected: true,
onDidChange: () => ({ dispose() { } }),
};
async getConfigs(): Promise<{ key: string; value: string }[]> {
return [...this._config];
}
async getConfig(key: string): Promise<string> {
for (let i = this._config.length - 1; i >= 0; i--) {
if (this._config[i].key === key) {
return this._config[i].value;
}
}
return '';
}
async setConfig(key: string, value: string): Promise<string> {
const oldValue = await this.getConfig(key);
this._config.push({ key, value });
return oldValue;
}
async unsetConfig(key: string): Promise<string> {
const matchingIndexes = this._config
.map((config, index) => config.key === key ? index : -1)
.filter(index => index !== -1);
if (matchingIndexes.length !== 1) {
return '';
}
const [{ value: oldValue }] = this._config.splice(matchingIndexes[0], 1);
return oldValue;
}
getObjectDetails(treeish: string, treePath: string): Promise<{ mode: string; object: string; size: number }> {
return Promise.reject(new Error(`Unexpected getObjectDetails(${treeish}, ${treePath})`));
}
show(ref: string, treePath: string): Promise<string> {
return Promise.reject(new Error(`Unexpected show(${ref}, ${treePath})`));
}
getCommit(ref: string): Promise<Commit> {
return Promise.reject(new Error(`Unexpected getCommit(${ref})`));
}
apply(patch: string, reverse?: boolean | undefined): Promise<void> {
return Promise.reject(new Error(`Unexpected apply(..., ${reverse})`));
}
diff(cached?: boolean | undefined): Promise<string> {
return Promise.reject(new Error(`Unexpected diff(${cached})`));
}
diffWith(ref: string): Promise<Change[]>;
diffWith(ref: string, treePath: string): Promise<string>;
diffWith(ref: string, treePath?: string) {
return Promise.reject(new Error(`Unexpected diffWith(${ref}, ${treePath})`));
}
diffBlobs(object1: string, object2: string): Promise<string> {
return Promise.reject(new Error(`Unexpected diffBlobs(${object1}, ${object2})`));
}
diffBetween(ref1: string, ref2: string): Promise<Change[]>;
diffBetween(ref1: string, ref2: string, treePath: string): Promise<string>;
diffBetween(ref1: string, ref2: string, treePath?: string) {
return Promise.reject(new Error(`Unexpected diffBlobs(${ref1}, ${ref2}, ${treePath})`));
}
hashObject(data: string): Promise<string> {
return Promise.reject(new Error('Unexpected hashObject(...)'));
}
private _hasBranch(ref: string) {
return this._branches.some(b => b.name === ref);
}
async createBranch(name: string, checkout: boolean, ref?: string | undefined): Promise<void> {
if (this._hasBranch(name)) {
throw new Error(`A branch named ${name} already exists`);
}
const branch = {
type: RefType.Head,
name,
commit: ref,
};
if (checkout) {
this._state.HEAD = branch;
}
this._state.refs.push(branch);
this._branches.push(branch);
}
async deleteBranch(name: string, force?: boolean | undefined): Promise<void> {
const index = this._branches.findIndex(b => b.name === name);
if (index === -1) {
const error: Error & { stderr?: string } = new Error(`Attempt to delete nonexistent branch ${name}`);
error.stderr = `error: branch '${name}' not found.`;
throw error;
}
this._branches.splice(index, 1);
if (this.preserveConfigOnNextBranchDelete) {
this.preserveConfigOnNextBranchDelete = false;
} else {
const prefix = `branch.${name}.`;
this._config = this._config.filter(config => !config.key.startsWith(prefix));
}
}
async getBranch(name: string): Promise<Branch> {
const branch = this._branches.find(b => b.name === name);
if (!branch) {
throw new Error(`getBranch called with unrecognized name "${name}"`);
}
return branch;
}
async getBranches(_query: BranchQuery): Promise<Ref[]> {
return [];
}
async getBranchBase(name: string): Promise<Branch | undefined> {
throw new Error(`Unexpected getBranchBase(${name})`);
}
async setBranchUpstream(name: string, upstream: string): Promise<void> {
const index = this._branches.findIndex(b => b.name === name);
if (index === -1) {
throw new Error(`setBranchUpstream called with unrecognized branch name ${name})`);
}
const match = /^refs\/remotes\/([^\/]+)\/(.+)$/.exec(upstream);
if (!match) {
throw new Error(
`upstream ${upstream} provided to setBranchUpstream did match pattern refs/remotes/<name>/<remote-branch>`,
);
}
const [, remoteName, remoteRef] = match;
const existing = this._branches[index];
const replacement = {
...existing,
upstream: {
remote: remoteName,
name: remoteRef,
},
};
this._branches.splice(index, 1, replacement);
if (this._state.HEAD === existing) {
this._state.HEAD = replacement;
}
}
status(): Promise<void> {
return Promise.reject(new Error('Unexpected status()'));
}
async checkout(treeish: string): Promise<void> {
const branch = this._branches.find(b => b.name === treeish);
// Also: tags
if (!branch) {
throw new Error(`checked called with unrecognized ref ${treeish}`);
}
this._state.HEAD = branch;
}
async addRemote(name: string, url: string): Promise<void> {
if (this._state.remotes.some(r => r.name === name)) {
throw new Error(`A remote named ${name} already exists.`);
}
this._state.remotes.push({
name,
fetchUrl: url,
pushUrl: url,
isReadOnly: false,
});
}
async removeRemote(name: string): Promise<void> {
const index = this._state.remotes.findIndex(r => r.name === name);
if (index === -1) {
throw new Error(`No remote named ${name} exists.`);
}
this._state.remotes.splice(index, 1);
}
async fetch(arg0?: string | undefined | FetchOptions, ref?: string | undefined, depth?: number | undefined): Promise<void> {
let remoteName: string | undefined;
if (typeof arg0 === 'object') {
remoteName = arg0.remote;
ref = arg0.ref;
depth = arg0.depth;
} else {
remoteName = arg0;
}
const index = this._expectedFetches.findIndex(
f => f.remoteName === remoteName && f.ref === ref && f.depth === depth,
);
if (index === -1) {
throw new Error(`Unexpected fetch(${remoteName}, ${ref}, ${depth})`);
}
if (ref && !this._hasBranch(ref)) {
const match = /^(?:\+?[^:]+\:)?(.*)$/.exec(ref);
if (match) {
const [, localRef] = match;
await this.createBranch(localRef, false);
}
}
this._expectedFetches.splice(index, 1);
}
async pull(unshallow?: boolean | undefined): Promise<void> {
const index = this._expectedPulls.findIndex(f => f.unshallow === unshallow);
if (index === -1) {
throw new Error(`Unexpected pull(${unshallow})`);
}
this._expectedPulls.splice(index, 1);
}
async push(
remoteName?: string | undefined,
branchName?: string | undefined,
setUpstream?: boolean | undefined,
): Promise<void> {
const index = this._expectedPushes.findIndex(
f => f.remoteName === remoteName && f.branchName === branchName && f.setUpstream === setUpstream,
);
if (index === -1) {
throw new Error(`Unexpected push(${remoteName}, ${branchName}, ${setUpstream})`);
}
this._expectedPushes.splice(index, 1);
}
blame(treePath: string): Promise<string> {
return Promise.reject(new Error(`Unexpected blame(${treePath})`));
}
expectFetch(remoteName?: string, ref?: string, depth?: number) {
this._expectedFetches.push({ remoteName, ref, depth });
}
expectPull(unshallow?: boolean) {
this._expectedPulls.push({ unshallow });
}
expectPush(remoteName?: string, branchName?: string, setUpstream?: boolean) {
this._expectedPushes.push({ remoteName, branchName, setUpstream });
}
merge(ref: string): Promise<void> {
return Promise.reject(new Error(`Unexpected merge(${ref})`));
}
mergeAbort(): Promise<void> {
return Promise.reject(new Error(`Unexpected mergeAbort`));
}
setWorktrees(worktrees: Worktree[]) {
this._state.worktrees = worktrees;
this._onDidChangeState.fire();
}
}