Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/git/src/uri.ts
3314 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Uri } from 'vscode';
7
import { Change, Status } from './api/git';
8
9
export interface GitUriParams {
10
path: string;
11
ref: string;
12
submoduleOf?: string;
13
}
14
15
export function isGitUri(uri: Uri): boolean {
16
return /^git$/.test(uri.scheme);
17
}
18
19
export function fromGitUri(uri: Uri): GitUriParams {
20
return JSON.parse(uri.query);
21
}
22
23
export interface GitUriOptions {
24
scheme?: string;
25
replaceFileExtension?: boolean;
26
submoduleOf?: string;
27
}
28
29
// As a mitigation for extensions like ESLint showing warnings and errors
30
// for git URIs, let's change the file extension of these uris to .git,
31
// when `replaceFileExtension` is true.
32
export function toGitUri(uri: Uri, ref: string, options: GitUriOptions = {}): Uri {
33
const params: GitUriParams = {
34
path: uri.fsPath,
35
ref
36
};
37
38
if (options.submoduleOf) {
39
params.submoduleOf = options.submoduleOf;
40
}
41
42
let path = uri.path;
43
44
if (options.replaceFileExtension) {
45
path = `${path}.git`;
46
} else if (options.submoduleOf) {
47
path = `${path}.diff`;
48
}
49
50
return uri.with({ scheme: options.scheme ?? 'git', path, query: JSON.stringify(params) });
51
}
52
53
/**
54
* Assuming `uri` is being merged it creates uris for `base`, `ours`, and `theirs`
55
*/
56
export function toMergeUris(uri: Uri): { base: Uri; ours: Uri; theirs: Uri } {
57
return {
58
base: toGitUri(uri, ':1'),
59
ours: toGitUri(uri, ':2'),
60
theirs: toGitUri(uri, ':3'),
61
};
62
}
63
64
export function toMultiFileDiffEditorUris(change: Change, originalRef: string, modifiedRef: string): { originalUri: Uri | undefined; modifiedUri: Uri | undefined } {
65
switch (change.status) {
66
case Status.INDEX_ADDED:
67
return {
68
originalUri: undefined,
69
modifiedUri: toGitUri(change.uri, modifiedRef)
70
};
71
case Status.DELETED:
72
return {
73
originalUri: toGitUri(change.uri, originalRef),
74
modifiedUri: undefined
75
};
76
case Status.INDEX_RENAMED:
77
return {
78
originalUri: toGitUri(change.originalUri, originalRef),
79
modifiedUri: toGitUri(change.uri, modifiedRef)
80
};
81
default:
82
return {
83
originalUri: toGitUri(change.uri, originalRef),
84
modifiedUri: toGitUri(change.uri, modifiedRef)
85
};
86
}
87
}
88
89