Skip to content

Commit 6ae9d0e

Browse files
committed
Fixes issues with findWorkingFileName
1 parent 7a6b09c commit 6ae9d0e

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

src/commands/diffWithWorking.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
6969
}
7070
}
7171

72-
const workingFileName = await Container.git.findWorkingFileName(gitUri.repoPath, gitUri.fsPath);
72+
const [workingFileName] = await Container.git.findWorkingFileName(gitUri.fsPath, gitUri.repoPath);
7373
if (workingFileName === undefined) return undefined;
7474

7575
const diffArgs: DiffWithCommandArgs = {

src/commands/showQuickCommitFileDetails.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class ShowQuickCommitFileDetailsCommand extends ActiveEditorCachedCommand
103103

104104
// Attempt to the most recent commit -- so that we can find the real working filename if there was a rename
105105
args.commit.workingFileName = workingFileName;
106-
args.commit.workingFileName = await Container.git.findWorkingFileName(args.commit);
106+
[args.commit.workingFileName] = await Container.git.findWorkingFileName(args.commit);
107107

108108
const shortSha = GitService.shortenSha(args.sha!);
109109

src/git/git.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ export class Git {
407407
return gitCommand({ cwd: root }, ...params);
408408
}
409409

410+
static async log_recent(repoPath: string, fileName: string) {
411+
try {
412+
const data = await gitCommandCore({ cwd: repoPath }, 'log', '--full-history', '-M', '-n1', '--format=%H', '--', fileName);
413+
return data.trim();
414+
}
415+
catch {
416+
return undefined;
417+
}
418+
}
419+
410420
static async log_resolve(repoPath: string, fileName: string, ref: string) {
411421
try {
412422
const data = await gitCommandCore({ cwd: repoPath }, 'log', '--full-history', '-M', '-n1', '--format=%H', ref, '--', fileName);

src/gitService.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,15 @@ export class GitService extends Disposable {
334334
: await this.findNextFileNameCore(repoPath, fileName, ref);
335335
}
336336

337-
private async findNextFileNameCore(repoPath: string, fileName: string, sha?: string): Promise<string | undefined> {
338-
if (sha === undefined) {
337+
private async findNextFileNameCore(repoPath: string, fileName: string, ref?: string): Promise<string | undefined> {
338+
if (ref === undefined) {
339339
// Get the most recent commit for this file name
340-
const c = await this.getRecentLogCommitForFile(repoPath, fileName);
341-
if (c === undefined) return undefined;
342-
343-
sha = c.sha;
340+
ref = await this.getRecentShaForFile(repoPath, fileName);
341+
if (ref === undefined) return undefined;
344342
}
345343

346344
// Get the full commit (so we can see if there are any matching renames in the file statuses)
347-
const log = await this.getLog(repoPath, { maxCount: 1, ref: sha });
345+
const log = await this.getLog(repoPath, { maxCount: 1, ref: ref });
348346
if (log === undefined) return undefined;
349347

350348
const c = Iterables.first(log.commits.values());
@@ -354,28 +352,31 @@ export class GitService extends Disposable {
354352
return status.fileName;
355353
}
356354

357-
async findWorkingFileName(commit: GitCommit): Promise<string | undefined>;
358-
async findWorkingFileName(repoPath: string | undefined, fileName: string): Promise<string | undefined>;
359-
async findWorkingFileName(commitOrRepoPath: GitCommit | string | undefined, fileName?: string): Promise<string | undefined> {
360-
let repoPath: string | undefined;
361-
if (commitOrRepoPath === undefined || typeof commitOrRepoPath === 'string') {
362-
repoPath = commitOrRepoPath;
363-
if (fileName === undefined) throw new Error('Invalid fileName');
355+
async findWorkingFileName(commit: GitCommit): Promise<[string | undefined, string | undefined]>;
356+
async findWorkingFileName(fileName: string, repoPath?: string, ref?: string): Promise<[string | undefined, string | undefined]>;
357+
async findWorkingFileName(commitOrFileName: GitCommit | string, repoPath?: string, ref?: string): Promise<[string | undefined, string | undefined]> {
358+
let fileName;
359+
if (typeof commitOrFileName === 'string') {
360+
fileName = commitOrFileName;
361+
if (repoPath === undefined) {
362+
repoPath = await this.getRepoPath(fileName, { ref: ref });
363+
[fileName, repoPath] = Git.splitPath(fileName, repoPath);
364+
}
364365

365-
[fileName] = Git.splitPath(fileName, repoPath);
366366
}
367367
else {
368-
const c = commitOrRepoPath;
368+
const c = commitOrFileName;
369369
repoPath = c.repoPath;
370-
if (c.workingFileName && await this.fileExists(repoPath, c.workingFileName)) return c.workingFileName;
370+
if (c.workingFileName && await this.fileExists(repoPath, c.workingFileName)) return [c.workingFileName, repoPath];
371371
fileName = c.fileName;
372372
}
373373

374+
// Keep walking up to the most recent commit for a given filename, until it exists on disk
374375
while (true) {
375-
if (await this.fileExists(repoPath!, fileName)) return fileName;
376+
if (await this.fileExists(repoPath, fileName)) return [fileName, repoPath];
376377

377-
fileName = await this.findNextFileNameCore(repoPath!, fileName);
378-
if (fileName === undefined) return undefined;
378+
fileName = await this.findNextFileNameCore(repoPath, fileName);
379+
if (fileName === undefined) return [undefined, undefined];
379380
}
380381
}
381382

@@ -823,6 +824,10 @@ export class GitService extends Disposable {
823824
return this.getLogCommitForFile(repoPath, fileName, undefined);
824825
}
825826

827+
async getRecentShaForFile(repoPath: string, fileName: string) {
828+
return await Git.log_recent(repoPath, fileName);
829+
}
830+
826831
async getLogCommit(repoPath: string, ref: string): Promise<GitLogCommit | undefined> {
827832
Logger.log(`getLogCommit('${repoPath}', '${ref}'`);
828833

src/quickPicks/fileHistory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class FileHistoryQuickPick {
4444
items.splice(0, 0, options.showAllCommand);
4545
}
4646
else if (!options.pickerOnly) {
47-
const workingFileName = await Container.git.findWorkingFileName(log.repoPath, path.relative(log.repoPath, uri.fsPath));
47+
const [workingFileName] = await Container.git.findWorkingFileName(path.relative(log.repoPath, uri.fsPath), log.repoPath);
4848
if (workingFileName) {
4949
index++;
5050
items.splice(0, 0, new CommandQuickPickItem({

0 commit comments

Comments
 (0)