Skip to content

Commit 64c8bd4

Browse files
committed
Fixes versioned files not working
1 parent b268b2d commit 64c8bd4

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

src/commands/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ export async function openEditor(uri: Uri, options: TextDocumentShowOptions & {
295295
const { rethrow, ...opts } = options;
296296
try {
297297
if (uri instanceof GitUri) {
298-
uri = uri.fileUri(false);
298+
uri = uri.fileUri({ noSha: true });
299299
}
300300

301301
const document = await workspace.openTextDocument(uri);

src/git/gitUri.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface IGitCommitInfo {
1010
fileName?: string;
1111
repoPath: string;
1212
sha?: string;
13+
versionedPath?: string;
1314
}
1415

1516
// Taken from https://github.com/Microsoft/vscode/blob/master/src/vs/base/common/uri.ts#L331-L337
@@ -30,8 +31,10 @@ interface UriEx {
3031

3132
export class GitUri extends ((Uri as any) as UriEx) {
3233

33-
repoPath?: string | undefined;
34-
sha?: string | undefined;
34+
repoPath?: string;
35+
sha?: string;
36+
37+
versionedPath?: string;
3538

3639
constructor(uri?: Uri)
3740
constructor(uri: Uri, commit: IGitCommitInfo);
@@ -75,6 +78,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
7578
super({ scheme: uri.scheme, authority: authority, path: fsPath, query: uri.query, fragment: uri.fragment });
7679

7780
this.repoPath = commitOrRepoPath.repoPath;
81+
this.versionedPath = commitOrRepoPath.versionedPath;
7882
if (GitService.isStagedUncommitted(commitOrRepoPath.sha) || !GitService.isUncommitted(commitOrRepoPath.sha)) {
7983
this.sha = commitOrRepoPath.sha;
8084
}
@@ -84,8 +88,10 @@ export class GitUri extends ((Uri as any) as UriEx) {
8488
return this.sha && GitService.shortenSha(this.sha);
8589
}
8690

87-
fileUri(useSha: boolean = true) {
88-
return Uri.file(useSha && this.sha ? this.path : this.fsPath);
91+
fileUri(options: { noSha?: boolean, useVersionedPath?: boolean } = {}) {
92+
if (options.useVersionedPath && this.versionedPath !== undefined) return Uri.file(this.versionedPath);
93+
94+
return Uri.file(!options.noSha && this.sha ? this.path : this.fsPath);
8995
}
9096

9197
getFormattedPath(separator: string = Strings.pad(GlyphChars.Dot, 2, 2), relativeTo?: string): string {
@@ -190,8 +196,8 @@ export class GitUri extends ((Uri as any) as UriEx) {
190196
} as IGitCommitInfo);
191197
}
192198

193-
const gitUri = await Container.git.getGitUri(uri);
194-
if (gitUri !== undefined) return gitUri;
199+
const versionedUri = await Container.git.getVersionedUri(uri);
200+
if (versionedUri !== undefined) return versionedUri;
195201

196202
return new GitUri(uri, await Container.git.getRepoPath(uri));
197203
}
@@ -240,6 +246,13 @@ export class GitUri extends ((Uri as any) as UriEx) {
240246
return Strings.normalizePath(relativePath);
241247
}
242248

249+
static toKey(fileName: string): string;
250+
static toKey(uri: Uri): string;
251+
static toKey(fileNameOrUri: string | Uri): string;
252+
static toKey(fileNameOrUri: string | Uri): string {
253+
return Strings.normalizePath(typeof fileNameOrUri === 'string' ? fileNameOrUri : fileNameOrUri.fsPath).toLowerCase();
254+
}
255+
243256
static toRevisionUri(uri: GitUri): Uri;
244257
static toRevisionUri(sha: string, fileName: string, repoPath: string): Uri;
245258
static toRevisionUri(sha: string, status: IGitStatusFile, repoPath: string): Uri;

src/gitService.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ConfigurationChangeEvent, Disposable, Event, EventEmitter, Range, TextE
44
import { configuration, IRemotesConfig } from './configuration';
55
import { CommandContext, DocumentSchemes, setCommandContext } from './constants';
66
import { Container } from './container';
7-
import { CachedBlame, CachedDiff, CachedLog, DocumentTracker, GitDocumentState, TrackedDocument } from './trackers/documentTracker';
7+
import { CachedBlame, CachedDiff, CachedLog, GitDocumentState, TrackedDocument } from './trackers/documentTracker';
88
import { RemoteProviderFactory, RemoteProviderMap } from './git/remotes/factory';
99
import { CommitFormatting, Git, GitAuthor, GitBlame, GitBlameCommit, GitBlameLine, GitBlameLines, GitBlameParser, GitBranch, GitBranchParser, GitCommit, GitCommitType, GitDiff, GitDiffChunkLine, GitDiffParser, GitDiffShortStat, GitLog, GitLogCommit, GitLogParser, GitRemote, GitRemoteParser, GitStash, GitStashParser, GitStatus, GitStatusFile, GitStatusParser, GitTag, GitTagParser, IGit, Repository } from './git/git';
1010
import { GitUri, IGitCommitInfo } from './git/gitUri';
@@ -45,12 +45,14 @@ export class GitService extends Disposable {
4545
private _repositoriesLoadingPromise: Promise<void> | undefined;
4646
private _suspended: boolean = false;
4747
private readonly _trackedCache: Map<string, boolean | Promise<boolean>>;
48+
private _versionedUriCache: Map<string, GitUri>;
4849

4950
constructor() {
5051
super(() => this.dispose());
5152

5253
this._repositoryTree = TernarySearchTree.forPaths();
5354
this._trackedCache = new Map();
55+
this._versionedUriCache = new Map();
5456

5557
this._disposable = Disposable.from(
5658
window.onDidChangeWindowState(this.onWindowStateChanged, this),
@@ -65,6 +67,7 @@ export class GitService extends Disposable {
6567
dispose() {
6668
this._repositoryTree.forEach(r => r.dispose());
6769
this._trackedCache.clear();
70+
this._versionedUriCache.clear();
6871

6972
this._disposable && this._disposable.dispose();
7073
}
@@ -684,11 +687,6 @@ export class GitService extends Disposable {
684687
return await Git.config_get(key, repoPath);
685688
}
686689

687-
async getGitUri(uri: Uri) {
688-
const doc = await Container.tracker.get(uri);
689-
return doc !== undefined ? doc.uri : undefined;
690-
}
691-
692690
async getDiffForFile(uri: GitUri, sha1?: string, sha2?: string): Promise<GitDiff | undefined> {
693691
if (sha1 !== undefined && sha2 === undefined && uri.sha !== undefined) {
694692
sha2 = uri.sha;
@@ -1201,6 +1199,10 @@ export class GitService extends Disposable {
12011199
}
12021200

12031201
const file = await Git.getVersionedFile(repoPath, fileName, sha);
1202+
if (file === undefined) return undefined;
1203+
1204+
this._versionedUriCache.set(GitUri.toKey(file), new GitUri(Uri.file(fileName), { sha: sha, repoPath: repoPath!, versionedPath: file }));
1205+
12041206
return file;
12051207
}
12061208

@@ -1210,6 +1212,10 @@ export class GitService extends Disposable {
12101212
return Git.show(repoPath, fileName, sha, { encoding: GitService.getEncoding(repoPath, fileName) });
12111213
}
12121214

1215+
getVersionedUri(uri: Uri) {
1216+
return this._versionedUriCache.get(GitUri.toKey(uri));
1217+
}
1218+
12131219
isTrackable(scheme: string): boolean;
12141220
isTrackable(uri: Uri): boolean;
12151221
isTrackable(schemeOruri: string | Uri): boolean {
@@ -1233,15 +1239,15 @@ export class GitService extends Disposable {
12331239
let fileName: string;
12341240
if (typeof fileNameOrUri === 'string') {
12351241
[fileName, repoPath] = Git.splitPath(fileNameOrUri, repoPath);
1236-
cacheKey = DocumentTracker.toStateKey(fileNameOrUri);
1242+
cacheKey = GitUri.toKey(fileNameOrUri);
12371243
}
12381244
else {
12391245
if (!this.isTrackable(fileNameOrUri)) return false;
12401246

12411247
fileName = fileNameOrUri.fsPath;
12421248
repoPath = fileNameOrUri.repoPath;
12431249
sha = fileNameOrUri.sha;
1244-
cacheKey = DocumentTracker.toStateKey(fileName);
1250+
cacheKey = GitUri.toKey(fileName);
12451251
}
12461252

12471253
if (sha !== undefined) {

src/trackers/documentTracker.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
2-
import { Functions, IDeferrable, Strings } from './../system';
2+
import { Functions, IDeferrable } from './../system';
33
import { ConfigurationChangeEvent, Disposable, Event, EventEmitter, TextDocument, TextDocumentChangeEvent, TextEditor, Uri, window, workspace } from 'vscode';
44
import { configuration } from './../configuration';
55
import { CommandContext, DocumentSchemes, isActiveDocument, isTextEditor, setCommandContext } from './../constants';
6+
import { GitUri } from '../gitService';
67
import { DocumentBlameStateChangeEvent, TrackedDocument } from './trackedDocument';
78

89
export { CachedBlame, CachedDiff, CachedLog, GitDocumentState } from './gitDocumentState';
@@ -47,6 +48,7 @@ export class DocumentTracker<T> extends Disposable {
4748
this._disposable = Disposable.from(
4849
configuration.onDidChange(this.onConfigurationChanged, this),
4950
window.onDidChangeActiveTextEditor(Functions.debounce(this.onActiveTextEditorChanged, 0), this),
51+
// window.onDidChangeVisibleTextEditors(Functions.debounce(this.onVisibleEditorsChanged, 5000), this),
5052
workspace.onDidChangeTextDocument(Functions.debounce(this.onTextDocumentChanged, 50), this),
5153
workspace.onDidCloseTextDocument(this.onTextDocumentClosed, this),
5254
workspace.onDidSaveTextDocument(this.onTextDocumentSaved, this)
@@ -159,6 +161,15 @@ export class DocumentTracker<T> extends Disposable {
159161
}
160162
}
161163

164+
// private onVisibleEditorsChanged(editors: TextEditor[]) {
165+
// if (this._documentMap.size === 0) return;
166+
167+
// // If we have no visible editors, or no "real" visible editors reset our cache
168+
// if (editors.length === 0 || editors.every(e => !isTextEditor(e))) {
169+
// this.clear();
170+
// }
171+
// }
172+
162173
async add(fileName: string): Promise<TrackedDocument<T>>;
163174
async add(document: TextDocument): Promise<TrackedDocument<T>>;
164175
async add(uri: Uri): Promise<TrackedDocument<T>>;
@@ -193,7 +204,7 @@ export class DocumentTracker<T> extends Disposable {
193204
has(uri: Uri): boolean;
194205
has(key: string | TextDocument | Uri): boolean {
195206
if (typeof key === 'string' || key instanceof Uri) {
196-
key = DocumentTracker.toStateKey(key);
207+
key = GitUri.toKey(key);
197208
}
198209
return this._documentMap.has(key);
199210
}
@@ -202,6 +213,9 @@ export class DocumentTracker<T> extends Disposable {
202213
if (typeof documentOrId === 'string') {
203214
documentOrId = await workspace.openTextDocument(documentOrId);
204215
}
216+
else if (documentOrId instanceof GitUri) {
217+
documentOrId = await workspace.openTextDocument(documentOrId.fileUri({ useVersionedPath: true }));
218+
}
205219
else if (documentOrId instanceof Uri) {
206220
documentOrId = await workspace.openTextDocument(documentOrId);
207221
}
@@ -213,8 +227,11 @@ export class DocumentTracker<T> extends Disposable {
213227
}
214228

215229
private async _get(documentOrId: string | TextDocument | Uri) {
216-
if (typeof documentOrId === 'string' || documentOrId instanceof Uri) {
217-
documentOrId = DocumentTracker.toStateKey(documentOrId);
230+
if (documentOrId instanceof GitUri) {
231+
documentOrId = GitUri.toKey(documentOrId.fileUri({ useVersionedPath: true }));
232+
}
233+
else if (typeof documentOrId === 'string' || documentOrId instanceof Uri) {
234+
documentOrId = GitUri.toKey(documentOrId);
218235
}
219236

220237
const doc = this._documentMap.get(documentOrId);
@@ -225,7 +242,7 @@ export class DocumentTracker<T> extends Disposable {
225242
}
226243

227244
private addCore(document: TextDocument): TrackedDocument<T> {
228-
const key = DocumentTracker.toStateKey(document.uri);
245+
const key = GitUri.toKey(document.uri);
229246

230247
// Always start out false, so we will fire the event if needed
231248
const doc = new TrackedDocument<T>(document, key, false, {
@@ -281,11 +298,4 @@ export class DocumentTracker<T> extends Disposable {
281298

282299
this._dirtyStateChangedDebounced(e);
283300
}
284-
285-
static toStateKey(fileName: string): string;
286-
static toStateKey(uri: Uri): string;
287-
static toStateKey(fileNameOrUri: string | Uri): string;
288-
static toStateKey(fileNameOrUri: string | Uri): string {
289-
return Strings.normalizePath(typeof fileNameOrUri === 'string' ? fileNameOrUri : fileNameOrUri.fsPath).toLowerCase();
290-
}
291301
}

0 commit comments

Comments
 (0)