Repository navigation
Expand file tree
/
Copy pathgithub1sfs.ts
More file actions
568 lines (529 loc) · 15.6 KB
/
Copy pathgithub1sfs.ts
File metadata and controls
568 lines (529 loc) · 15.6 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/**
* @file VSCode GitHub1sFs Provider
* @author netcon
*/
import {
window,
workspace,
Disposable,
FileSystemProvider,
FileSearchProvider,
FileSearchQuery,
FileSearchOptions,
CancellationToken,
ProviderResult,
FileSystemError,
Event,
EventEmitter,
FileChangeEvent,
FileStat,
FileType,
Uri,
FileDecoration,
ThemeColor,
FileDecorationProvider,
} from 'vscode';
import Fuse from 'fuse.js';
import {
noop,
trimStart,
parseGitmodules,
parseSubmoduleUrl,
reuseable,
getCurrentAuthority,
} from './util';
import {
readGitHubDirectory,
readGitHubFile,
getGithubAllFiles,
isGraphQLEnabled,
} from './api';
import { apolloClient } from './client';
import { githubObjectQuery } from './github-api-gql';
import { toUint8Array as decodeBase64 } from 'js-base64';
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
export class File implements FileStat {
type: FileType;
ctime: number;
mtime: number;
size: number;
name: string;
sha: string;
data?: Uint8Array;
constructor(public uri: Uri, name: string, options?: any) {
this.type = FileType.File;
this.ctime = Date.now();
this.mtime = Date.now();
this.name = name;
this.sha = options && 'sha' in options ? options.sha : '';
this.size = options && 'size' in options ? options.size : 0;
this.data = options && 'data' in options ? options.data : null;
}
}
export class Directory implements FileStat {
type: FileType;
ctime: number;
mtime: number;
size: number;
sha: string;
name: string;
entries: Map<string, File | Directory> | null;
isSubmodule: boolean;
constructor(public uri: Uri, name: string, options?: any) {
this.type = FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
this.entries = null;
this.sha = options && 'sha' in options ? options.sha : '';
this.size = options && 'size' in options ? options.size : 0;
this.isSubmodule =
options && 'isSubmodule' in options ? options.isSubmodule : false;
}
getNameTypePairs(): [string, FileType][] {
return Array.from(
this.entries?.entries() || []
).map(([name, item]: [string, Entry]) => [
name,
item instanceof Directory ? FileType.Directory : FileType.File,
]);
}
}
export const createEntry = (
type: 'tree' | 'blob' | 'commit',
uri: Uri,
name: string,
options?: any
) => {
switch (type) {
case 'tree':
return new Directory(uri, name, options);
case 'commit':
return new Directory(uri, name, { ...options, isSubmodule: true });
default:
return new File(uri, name, options);
}
};
interface GithubRESTEntry {
path: string;
type: 'tree' | 'blob' | 'commit';
sha: string;
size?: number;
}
interface GithubGraphQLEntry {
name: string;
oid: string;
path: string;
type: 'tree' | 'blob' | 'commit';
object?: any;
}
export type Entry = File | Directory;
const insertGitHubRESTEntryToDirectory = (
githubEntry: GithubRESTEntry,
baseDirectory: Directory
) => {
const pathParts = githubEntry.path.split('/').filter(Boolean);
const fileName = pathParts.pop();
let current = baseDirectory;
pathParts.forEach((part) => {
if (
!(current.entries || (current.entries = new Map<string, Entry>())).has(
part
)
) {
current.entries.set(part, createEntry('tree', current.uri, current.name));
}
current = current.entries.get(part) as Directory;
});
if (
!(current.entries || (current.entries = new Map<string, Entry>())).has(
fileName
)
) {
const entryUri = Uri.joinPath(current.uri, current.name);
current.entries.set(
fileName,
createEntry(githubEntry.type, entryUri, fileName)
);
}
const entry: Entry = current.entries.get(fileName);
entry.sha = githubEntry.sha;
entry.type === FileType.File && (entry.size = githubEntry.size!);
};
/**
* This funtion must be used for only GraphQL output
*
* @param entries the entries of a GitObject
* @param parentDirectory the parent Directory
*/
const insertGitHubGraphQLEntriesToDirectory = (
entries: GithubGraphQLEntry[],
parentDirectory: Directory
) => {
if (!entries) {
return null;
}
const map =
parentDirectory.entries ||
(parentDirectory.entries = new Map<string, Entry>());
entries.forEach((item) => {
const entryUri = Uri.joinPath(parentDirectory.uri, parentDirectory.name);
const entry =
map.get(item.name) || createEntry(item.type, entryUri, item.name);
if (item.type === 'tree') {
insertGitHubGraphQLEntriesToDirectory(
item?.object?.entries,
entry as Directory
);
} else if (item.type === 'blob') {
(entry as File).size = item.object?.byteSize;
// Set data to `null` if the blob is binary so that it will trigger the RESTful endpoint fallback.
(entry as File).data = item.object?.isBinary
? null
: textEncoder.encode(item?.object?.text);
}
entry.sha = item.oid;
map.set(item.name, entry);
});
return map;
};
export class GitHub1sFS
implements
FileSystemProvider,
FileSearchProvider,
FileDecorationProvider,
Disposable {
static scheme = 'github1s';
private readonly disposable: Disposable;
private _emitter = new EventEmitter<FileChangeEvent[]>();
private root: Map<string, Directory | File> = new Map();
private fuseMap: Map<string, Fuse<GithubRESTEntry>> = new Map();
private static submoduleDecorationData: FileDecoration = {
tooltip: 'Submodule',
badge: 'S',
color: new ThemeColor('gitDecoration.submoduleResourceForeground'),
};
onDidChangeFile: Event<FileChangeEvent[]> = this._emitter.event;
constructor() {
this.disposable = Disposable.from(
workspace.registerFileSystemProvider(GitHub1sFS.scheme, this, {
isCaseSensitive: true,
isReadonly: true,
}),
workspace.registerFileSearchProvider(GitHub1sFS.scheme, this),
window.registerFileDecorationProvider(this)
);
}
onDidChangeFileDecorations?: Event<Uri | Uri[]>;
dispose() {
this.disposable?.dispose();
}
// --- lookup
// ensure the authority field in `the uri of returned entry` is exists
private async _lookup(uri: Uri, silent: false): Promise<Entry>;
private async _lookup(uri: Uri, silent: boolean): Promise<Entry | undefined>;
private async _lookup(uri: Uri, silent: boolean): Promise<Entry | undefined> {
let parts = uri.path.split('/').filter(Boolean);
// if the authority of uri is empty, we should use `current authority`
const authority = uri.authority || (await getCurrentAuthority());
if (!this.root.has(authority)) {
this.root.set(
authority,
createEntry('tree', uri.with({ authority, path: '/' }), '')
);
}
let entry = this.root.get(authority);
for (const part of parts) {
let child: Entry | undefined;
if (entry instanceof Directory) {
if (entry.entries === null) {
await this.readDirectory(Uri.joinPath(entry.uri, entry.name));
}
child = entry.entries.get(part);
}
if (!child) {
if (!silent) {
throw FileSystemError.FileNotFound(uri);
} else {
return undefined;
}
}
entry = child;
}
return entry;
}
private async _lookupAsDirectory(
uri: Uri,
silent: boolean
): Promise<Directory> {
const entry = await this._lookup(uri, silent);
if (entry instanceof Directory) {
return entry;
}
if (!silent) {
throw FileSystemError.FileNotADirectory(uri);
}
}
private async _lookupAsFile(uri: Uri, silent: boolean): Promise<File> {
const entry = await this._lookup(uri, silent);
if (entry instanceof File) {
return entry;
}
if (!silent) {
throw FileSystemError.FileIsADirectory(uri);
}
}
watch(
uri: Uri,
options: { recursive: boolean; excludes: string[] }
): Disposable {
return new Disposable(noop);
}
stat(uri: Uri): FileStat | Thenable<FileStat> {
return this._lookup(uri, false);
}
// update the uri of a git submodule as directory, which the type of corresponding githubEntry should be `commit`.
// the `directory.uri.authority` and the `directory.uri.path` must belong to the `parent repository` before called.
// and the `directory.name` is the corresponding `directory name` in `parent repository` before called.
// once the function is called successful, the `directory.uri.authority` field, the `directory.uri.path`,
// and the `directory.uri.name` field would be changed to the `submodule repository's`.
//
// so this function could be called only once for a submodule directory, for example:
// - the directory argument before called may looks like:
// {
// uri: {
// scheme: 'github1s',
// authority: 'conwnet+github1s+master', // this is the authority of `parent repository`
// path: '/some/submodule/path' // the corresponding path in `parent repository`
// },
// name: 'vscode', // the name is the `directory name` of `parent repository` before called
// entries: null, // the entries should be null to indicated we haven't call this for `parent`
// isSubmodule: true, // this Directory must be a submodule
// ...otherFields
// }
// - and the directory argument after called may looks like:
// {
// uri: {
// scheme: 'github1s',
// authority: 'microsoft+vscode+master', // this is the authority of `submodule repository`
// path: '/' // the `path` filed should be '/' to indicated to the root directory of `submodule repository`
// },
// name: '', // the name is the '' to indicated it is a root directory of `submodule repository`
// entries: Map<string, Entry> {...}, // the entries contains the files of `submodule repository`
// isSubmodule: true, // this Directory must be a submodule
// ...otherFields
// }
_updateSubmoduleDirectory = reuseable(
async (directory: Directory): Promise<[string, FileType][]> => {
// if the directory is not submodule, or it has be called already
if (!directory.isSubmodule || directory.entries) {
return directory.getNameTypePairs() || [];
}
const parentRepositoryRoot = await this._lookupAsDirectory(
directory.uri.with({ path: '/' }),
false
);
if (
!parentRepositoryRoot.entries ||
!parentRepositoryRoot.entries.has('.gitmodules')
) {
throw FileSystemError.FileNotFound('.gitmodules can not be found');
}
const submodulesFileContent = textDecoder.decode(
await this.readFile(
Uri.joinPath(parentRepositoryRoot.uri, '.gitmodules'),
false
)
);
// the path should declared in .gitmodules file
const submodulePath = trimStart(
Uri.joinPath(directory.uri, directory.name).path,
'/'
);
const gitmoduleData = parseGitmodules(submodulesFileContent).find(
(item) => item.path === submodulePath
);
if (!gitmoduleData) {
throw FileSystemError.FileNotFound(
`can't found corresponding declare in .gitmodules`
);
}
const [submoduleOwner, submoduleRepo] = parseSubmoduleUrl(
gitmoduleData.url
);
const submoduleAuthority = `${submoduleOwner}+${submoduleRepo}+${
directory.sha || 'HEAD'
}`;
directory.name = ''; // update the name field to '' to indicated it is an root directory
// update the uri field to indicated it is belong the `submodule repository`
directory.uri = directory.uri.with({
authority: submoduleAuthority,
path: '/',
});
// insert the directory in to this.root map because it indicated another repository
this.root.set(submoduleAuthority, directory);
}
);
readDirectory = reuseable(
(uri: Uri): [string, FileType][] | Thenable<[string, FileType][]> => {
return this._lookupAsDirectory(uri, false).then(async (parent) => {
if (parent.entries !== null) {
return parent.getNameTypePairs();
}
if (parent.isSubmodule) {
await this._updateSubmoduleDirectory(parent);
}
const [owner, repo, ref] = parent.uri.authority.split('+');
if (isGraphQLEnabled()) {
return apolloClient
.query({
query: githubObjectQuery,
variables: {
owner,
repo,
expression: `${ref}:${Uri.joinPath(
parent.uri,
parent.name
).path.slice(1)}`,
},
})
.then((response) => {
const entries = response.data?.repository?.object?.entries;
if (!entries) {
throw FileSystemError.FileNotADirectory(uri);
}
insertGitHubGraphQLEntriesToDirectory(entries, parent);
return parent.getNameTypePairs();
});
}
return readGitHubDirectory(
owner,
repo,
ref,
Uri.joinPath(parent.uri, parent.name).path
).then((data) => {
// create new Entry to `parent.entries` only if `parent.entries.get(item.path)` is nil
(data.tree || []).forEach((item: GithubRESTEntry) =>
insertGitHubRESTEntryToDirectory(item, parent)
);
return parent.getNameTypePairs();
});
});
},
(uri: Uri) => uri.toString()
);
readFile = reuseable(
(uri: Uri): Uint8Array | Thenable<Uint8Array> => {
return this._lookupAsFile(uri, false).then(async (file) => {
if (file.data !== null) {
return file.data;
}
/**
* Below code will only be triggered in two cases:
* 1. The GraphQL query is disabled
* 2. The GraphQL query is enabled, but the blob/file is binary
*/
const [owner, repo] = file.uri.authority.split('+');
return readGitHubFile(owner, repo, file.sha).then((blob) => {
file.data = decodeBase64(blob.content);
return file.data;
});
});
},
(uri: Uri) => uri.toString()
);
createDirectory(uri: Uri): void | Thenable<void> {
return Promise.resolve();
}
writeFile(
uri: Uri,
content: Uint8Array,
options: { create: boolean; overwrite: boolean }
): void | Thenable<void> {
return Promise.resolve();
}
delete(uri: Uri, options: { recursive: boolean }): void | Thenable<void> {
return Promise.resolve();
}
rename(
oldUri: Uri,
newUri: Uri,
options: { overwrite: boolean }
): void | Thenable<void> {
return Promise.resolve();
}
copy?(
source: Uri,
destination: Uri,
options: { overwrite: boolean }
): void | Thenable<void> {
return Promise.resolve();
}
/**
* getFuse for fuzzy file search,
* it maybe take longer time, so we just run it in backend
* if this is failed, the fuzzy search maybe not work fine
*/
getFuse = reuseable(
async (authority): Promise<Fuse<GithubRESTEntry>> => {
if (this.fuseMap.has(authority)) {
return this.fuseMap.get(authority);
}
const [owner, repo, ref] = authority.split('+');
return getGithubAllFiles(owner, repo, ref).then(async (treeData) => {
if (!treeData.truncated) {
// the number of items in the tree array maybe exceeded maximum limit
// only update the rootDirectory if `treeData.truncated` is false
const rootDirectory = await this._lookupAsDirectory(
Uri.parse('').with({
scheme: GitHub1sFS.scheme,
authority,
path: '/',
}),
false
);
(treeData.tree || []).forEach((githubEntry: GithubRESTEntry) => {
insertGitHubRESTEntryToDirectory(githubEntry, rootDirectory);
});
}
const fuse = new Fuse(
((treeData.tree || []) as GithubRESTEntry[]).filter(
(item) => item.type === 'blob'
),
{ keys: ['path'] }
);
this.fuseMap.set(authority, fuse);
return fuse;
});
}
);
provideFileSearchResults(
query: FileSearchQuery,
_options: FileSearchOptions,
_token: CancellationToken
): ProviderResult<Uri[]> {
return getCurrentAuthority().then(async (authority) => {
const fuse = await this.getFuse(authority);
return fuse.search(query.pattern).map((result) => {
return Uri.parse('').with({
authority,
scheme: GitHub1sFS.scheme,
path: `/${result.item.path}`,
});
});
});
}
provideFileDecoration(
uri: Uri,
_token: CancellationToken
): ProviderResult<FileDecoration> {
return this._lookup(uri, false).then((entry) => {
if (entry instanceof Directory && entry.isSubmodule === true) {
return GitHub1sFS.submoduleDecorationData;
}
return null;
});
}
}