<commit_before>'use strict';
import * as paths from 'path';
import { Uri } from 'vscode';
import { UriComparer } from '../comparers';
import { DocumentSchemes } from '../constants';
import { Container } from '../container';
import { GitCommit, GitFile, GitRevision } from '../git/git';
import { Logger } from '../logger';
import { debug, memoize, Strings } from '../system';

const emptyStr = '';
const slash = '/';

export interface GitCommitish {
	fileName?: string;
	repoPath: string;
	sha?: string;
	versionedPath?: string;
}

// Taken from https://github.com/Microsoft/vscode/blob/master/src/vs/base/common/uri.ts#L331-L337
interface UriComponents {
	scheme: string;
	authority: string;
	path: string;
	query: string;
	fragment: string;
}

interface UriEx {
	new (): Uri;
	new (scheme: string, authority: string, path: string, query: string, fragment: string): Uri;
	// Use this ctor, because vscode doesn't validate it
	new (components: UriComponents): Uri;
}

export class GitUri extends ((Uri as any) as UriEx) {
	static is(uri: any): uri is GitUri {
		return uri instanceof GitUri;
	}

	readonly repoPath?: string;
	readonly sha?: string;
	readonly versionedPath?: string;

	constructor(uri?: Uri);
	constructor(uri: Uri, commit: GitCommitish);
	constructor(uri: Uri, repoPath: string | undefined);
	constructor(uri?: Uri, commitOrRepoPath?: GitCommitish | string) {
		if (uri == null) {
			super('unknown', emptyStr, emptyStr, emptyStr, emptyStr);

			return;
		}

		if (uri.scheme === DocumentSchemes.GitLens) {
			const data = JSON.parse(uri.query) as UriRevisionData;

			// When Uri's come from the FileSystemProvider, the uri.query only contains the root repo info (not the actual file path), so fix that here
			const index = uri.path.indexOf(data.path);
			if (index + data.path.length < uri.path.length) {
				data.path = index === 0 ? uri.path : uri.path.substr(index);
			}

			super({
				scheme: uri.scheme,
				authority: uri.authority,
				path: data.path,
				query: JSON.stringify(data),
				fragment: uri.fragment,
			});

			this.repoPath = data.repoPath;
			if (GitRevision.isUncommittedStaged(data.ref) || !GitRevision.isUncommitted(data.ref)) {
				this.sha = data.ref;
			}<commit_msg>Honors uri.path unless it starts with //<commit_after>'use strict';
import * as paths from 'path';
import { Uri } from 'vscode';
import { UriComparer } from '../comparers';
import { DocumentSchemes } from '../constants';
import { Container } from '../container';
import { GitCommit, GitFile, GitRevision } from '../git/git';
import { Logger } from '../logger';
import { debug, memoize, Strings } from '../system';

const emptyStr = '';
const slash = '/';

export interface GitCommitish {
	fileName?: string;
	repoPath: string;
	sha?: string;
	versionedPath?: string;
}

// Taken from https://github.com/Microsoft/vscode/blob/master/src/vs/base/common/uri.ts#L331-L337
interface UriComponents {
	scheme: string;
	authority: string;
	path: string;
	query: string;
	fragment: string;
}

interface UriEx {
	new (): Uri;
	new (scheme: string, authority: string, path: string, query: string, fragment: string): Uri;
	// Use this ctor, because vscode doesn't validate it
	new (components: UriComponents): Uri;
}

export class GitUri extends ((Uri as any) as UriEx) {
	static is(uri: any): uri is GitUri {
		return uri instanceof GitUri;
	}

	readonly repoPath?: string;
	readonly sha?: string;
	readonly versionedPath?: string;

	constructor(uri?: Uri);
	constructor(uri: Uri, commit: GitCommitish);
	constructor(uri: Uri, repoPath: string | undefined);
	constructor(uri?: Uri, commitOrRepoPath?: GitCommitish | string) {
		if (uri == null) {
			super('unknown', emptyStr, emptyStr, emptyStr, emptyStr);

			return;
		}

		if (uri.scheme === DocumentSchemes.GitLens) {
			const data = JSON.parse(uri.query) as UriRevisionData;

			// Fixes issues with uri.query:
			// When Uri's come from the FileSystemProvider, the uri.query only contains the root repo info (not the actual file path)
			// When Uri's come from breadcrumbs (via the FileSystemProvider), the uri.query contains the wrong file path
			if (data.path !== uri.path) {
				if (data.path.startsWith('//') && !uri.path.startsWith('//')) {
					data.path = `/${uri.path}`;
				} else {
					data.path = uri.path;
				}
			}

			super({
				scheme: uri.scheme,
				authority: uri.authority,
				path: data.path,
				query: JSON.stringify(data),
				fragment: uri.fragment,
			});

			this.repoPath = data.repoPath;
			if (GitRevision.isUncommittedStaged(data.ref) || !GitRevision.isUncommitted(data.ref)) {
				this.sha = data.ref;
			}
{'fraction_non_alphanumeric': 0.14090316977854972, 'fraction_numerical': 0.002822405557967868, 'mean_word_length': 3.901063829787234, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '185678', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1460, 'n_words': 442}