Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/node/gitDiffContent.ts
13394 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 { decodeHex, encodeHex, VSBuffer } from '../../../base/common/buffer.js';
7
import { basename } from '../../../base/common/path.js';
8
import { URI } from '../../../base/common/uri.js';
9
10
const GIT_BLOB_SCHEME = 'git-blob';
11
12
/**
13
* Builds a `git-blob:` URI that references a file blob at a specific git
14
* commit, scoped to a given session. Resolved by reading the session's
15
* working directory and shelling out to `git show <sha>:<path>`.
16
*
17
* The session URI is preserved so the resolver can find the session's
18
* working directory; the SHA and repository-relative path identify the
19
* blob to fetch.
20
*/
21
export function buildGitBlobUri(sessionUri: string, sha: string, repoRelativePath: string): string {
22
return URI.from({
23
scheme: GIT_BLOB_SCHEME,
24
authority: encodeHex(VSBuffer.fromString(sessionUri)).toString(),
25
path: `/${encodeURIComponent(sha)}/${encodeHex(VSBuffer.fromString(repoRelativePath))}/${basename(repoRelativePath)}`,
26
}).toString();
27
}
28
29
/** Parsed fields from a `git-blob:` content URI. */
30
export interface IGitBlobUriFields {
31
readonly sessionUri: string;
32
readonly sha: string;
33
readonly repoRelativePath: string;
34
}
35
36
/**
37
* Parses a `git-blob:` URI produced by {@link buildGitBlobUri}.
38
* Returns `undefined` if the URI is not a valid `git-blob:` URI.
39
*/
40
export function parseGitBlobUri(raw: string): IGitBlobUriFields | undefined {
41
let parsed: URI;
42
try {
43
parsed = URI.parse(raw);
44
} catch {
45
return undefined;
46
}
47
if (parsed.scheme !== GIT_BLOB_SCHEME) {
48
return undefined;
49
}
50
const [, sha, encodedPath] = parsed.path.split('/');
51
if (!sha || !encodedPath) {
52
return undefined;
53
}
54
try {
55
return {
56
sessionUri: decodeHex(parsed.authority).toString(),
57
sha: decodeURIComponent(sha),
58
repoRelativePath: decodeHex(encodedPath).toString(),
59
};
60
} catch {
61
return undefined;
62
}
63
}
64
65