Path: blob/main/src/vs/platform/agentHost/node/gitDiffContent.ts
13394 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { decodeHex, encodeHex, VSBuffer } from '../../../base/common/buffer.js';6import { basename } from '../../../base/common/path.js';7import { URI } from '../../../base/common/uri.js';89const GIT_BLOB_SCHEME = 'git-blob';1011/**12* Builds a `git-blob:` URI that references a file blob at a specific git13* commit, scoped to a given session. Resolved by reading the session's14* working directory and shelling out to `git show <sha>:<path>`.15*16* The session URI is preserved so the resolver can find the session's17* working directory; the SHA and repository-relative path identify the18* blob to fetch.19*/20export function buildGitBlobUri(sessionUri: string, sha: string, repoRelativePath: string): string {21return URI.from({22scheme: GIT_BLOB_SCHEME,23authority: encodeHex(VSBuffer.fromString(sessionUri)).toString(),24path: `/${encodeURIComponent(sha)}/${encodeHex(VSBuffer.fromString(repoRelativePath))}/${basename(repoRelativePath)}`,25}).toString();26}2728/** Parsed fields from a `git-blob:` content URI. */29export interface IGitBlobUriFields {30readonly sessionUri: string;31readonly sha: string;32readonly repoRelativePath: string;33}3435/**36* Parses a `git-blob:` URI produced by {@link buildGitBlobUri}.37* Returns `undefined` if the URI is not a valid `git-blob:` URI.38*/39export function parseGitBlobUri(raw: string): IGitBlobUriFields | undefined {40let parsed: URI;41try {42parsed = URI.parse(raw);43} catch {44return undefined;45}46if (parsed.scheme !== GIT_BLOB_SCHEME) {47return undefined;48}49const [, sha, encodedPath] = parsed.path.split('/');50if (!sha || !encodedPath) {51return undefined;52}53try {54return {55sessionUri: decodeHex(parsed.authority).toString(),56sha: decodeURIComponent(sha),57repoRelativePath: decodeHex(encodedPath).toString(),58};59} catch {60return undefined;61}62}636465