Browse Source

Fixes issues with findWorkingFileName

main
Eric Amodio 7 years ago
parent
commit
6ae9d0e808
5 changed files with 38 additions and 23 deletions
  1. +1
    -1
      src/commands/diffWithWorking.ts
  2. +1
    -1
      src/commands/showQuickCommitFileDetails.ts
  3. +10
    -0
      src/git/git.ts
  4. +25
    -20
      src/gitService.ts
  5. +1
    -1
      src/quickPicks/fileHistory.ts

+ 1
- 1
src/commands/diffWithWorking.ts View File

@ -69,7 +69,7 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
}
}
const workingFileName = await Container.git.findWorkingFileName(gitUri.repoPath, gitUri.fsPath);
const [workingFileName] = await Container.git.findWorkingFileName(gitUri.fsPath, gitUri.repoPath);
if (workingFileName === undefined) return undefined;
const diffArgs: DiffWithCommandArgs = {

+ 1
- 1
src/commands/showQuickCommitFileDetails.ts View File

@ -103,7 +103,7 @@ export class ShowQuickCommitFileDetailsCommand extends ActiveEditorCachedCommand
// Attempt to the most recent commit -- so that we can find the real working filename if there was a rename
args.commit.workingFileName = workingFileName;
args.commit.workingFileName = await Container.git.findWorkingFileName(args.commit);
[args.commit.workingFileName] = await Container.git.findWorkingFileName(args.commit);
const shortSha = GitService.shortenSha(args.sha!);

+ 10
- 0
src/git/git.ts View File

@ -407,6 +407,16 @@ export class Git {
return gitCommand({ cwd: root }, ...params);
}
static async log_recent(repoPath: string, fileName: string) {
try {
const data = await gitCommandCore({ cwd: repoPath }, 'log', '--full-history', '-M', '-n1', '--format=%H', '--', fileName);
return data.trim();
}
catch {
return undefined;
}
}
static async log_resolve(repoPath: string, fileName: string, ref: string) {
try {
const data = await gitCommandCore({ cwd: repoPath }, 'log', '--full-history', '-M', '-n1', '--format=%H', ref, '--', fileName);

+ 25
- 20
src/gitService.ts View File

@ -334,17 +334,15 @@ export class GitService extends Disposable {
: await this.findNextFileNameCore(repoPath, fileName, ref);
}
private async findNextFileNameCore(repoPath: string, fileName: string, sha?: string): Promise<string | undefined> {
if (sha === undefined) {
private async findNextFileNameCore(repoPath: string, fileName: string, ref?: string): Promise<string | undefined> {
if (ref === undefined) {
// Get the most recent commit for this file name
const c = await this.getRecentLogCommitForFile(repoPath, fileName);
if (c === undefined) return undefined;
sha = c.sha;
ref = await this.getRecentShaForFile(repoPath, fileName);
if (ref === undefined) return undefined;
}
// Get the full commit (so we can see if there are any matching renames in the file statuses)
const log = await this.getLog(repoPath, { maxCount: 1, ref: sha });
const log = await this.getLog(repoPath, { maxCount: 1, ref: ref });
if (log === undefined) return undefined;
const c = Iterables.first(log.commits.values());
@ -354,28 +352,31 @@ export class GitService extends Disposable {
return status.fileName;
}
async findWorkingFileName(commit: GitCommit): Promise<string | undefined>;
async findWorkingFileName(repoPath: string | undefined, fileName: string): Promise<string | undefined>;
async findWorkingFileName(commitOrRepoPath: GitCommit | string | undefined, fileName?: string): Promise<string | undefined> {
let repoPath: string | undefined;
if (commitOrRepoPath === undefined || typeof commitOrRepoPath === 'string') {
repoPath = commitOrRepoPath;
if (fileName === undefined) throw new Error('Invalid fileName');
async findWorkingFileName(commit: GitCommit): Promise<[string | undefined, string | undefined]>;
async findWorkingFileName(fileName: string, repoPath?: string, ref?: string): Promise<[string | undefined, string | undefined]>;
async findWorkingFileName(commitOrFileName: GitCommit | string, repoPath?: string, ref?: string): Promise<[string | undefined, string | undefined]> {
let fileName;
if (typeof commitOrFileName === 'string') {
fileName = commitOrFileName;
if (repoPath === undefined) {
repoPath = await this.getRepoPath(fileName, { ref: ref });
[fileName, repoPath] = Git.splitPath(fileName, repoPath);
}
[fileName] = Git.splitPath(fileName, repoPath);
}
else {
const c = commitOrRepoPath;
const c = commitOrFileName;
repoPath = c.repoPath;
if (c.workingFileName && await this.fileExists(repoPath, c.workingFileName)) return c.workingFileName;
if (c.workingFileName && await this.fileExists(repoPath, c.workingFileName)) return [c.workingFileName, repoPath];
fileName = c.fileName;
}
// Keep walking up to the most recent commit for a given filename, until it exists on disk
while (true) {
if (await this.fileExists(repoPath!, fileName)) return fileName;
if (await this.fileExists(repoPath, fileName)) return [fileName, repoPath];
fileName = await this.findNextFileNameCore(repoPath!, fileName);
if (fileName === undefined) return undefined;
fileName = await this.findNextFileNameCore(repoPath, fileName);
if (fileName === undefined) return [undefined, undefined];
}
}
@ -823,6 +824,10 @@ export class GitService extends Disposable {
return this.getLogCommitForFile(repoPath, fileName, undefined);
}
async getRecentShaForFile(repoPath: string, fileName: string) {
return await Git.log_recent(repoPath, fileName);
}
async getLogCommit(repoPath: string, ref: string): Promise<GitLogCommit | undefined> {
Logger.log(`getLogCommit('${repoPath}', '${ref}'`);

+ 1
- 1
src/quickPicks/fileHistory.ts View File

@ -44,7 +44,7 @@ export class FileHistoryQuickPick {
items.splice(0, 0, options.showAllCommand);
}
else if (!options.pickerOnly) {
const workingFileName = await Container.git.findWorkingFileName(log.repoPath, path.relative(log.repoPath, uri.fsPath));
const [workingFileName] = await Container.git.findWorkingFileName( path.relative(log.repoPath, uri.fsPath), log.repoPath);
if (workingFileName) {
index++;
items.splice(0, 0, new CommandQuickPickItem({

Loading…
Cancel
Save