Consolidates certain getLogForFile usage patterns into getLogCommit

This commit is contained in:
Eric Amodio
2017-03-24 16:09:52 -04:00
parent ee40dc6325
commit c10a79a7ee
8 changed files with 40 additions and 35 deletions

View File

@@ -298,8 +298,7 @@ export class BlameActiveLineController extends Disposable {
// Get the full commit message -- since blame only returns the summary // Get the full commit message -- since blame only returns the summary
let logCommit: GitCommit; let logCommit: GitCommit;
if (!commit.isUncommitted) { if (!commit.isUncommitted) {
const log = await this.git.getLogForFile(this._uri.repoPath, this._uri.fsPath, commit.sha, undefined, 1); logCommit = await this.git.getLogCommit(this._uri.repoPath, this._uri.fsPath, commit.sha);
logCommit = log && log.commits.get(commit.sha);
} }
// I have no idea why I need this protection -- but it happens // I have no idea why I need this protection -- but it happens

View File

@@ -56,10 +56,7 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
} }
// Get the full commit message -- since blame only returns the summary // Get the full commit message -- since blame only returns the summary
const log = await this.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, sha, undefined, 1); const commit = await this.git.getLogCommit(gitUri.repoPath, gitUri.fsPath, sha);
if (!log) return undefined;
const commit = log.commits.get(sha);
if (!commit) return undefined; if (!commit) return undefined;
message = commit.message; message = commit.message;

View File

@@ -1,5 +1,5 @@
'use strict'; 'use strict';
import { Iterables } from '../system'; // import { Iterables } from '../system';
import { commands, TextEditor, Uri, window } from 'vscode'; import { commands, TextEditor, Uri, window } from 'vscode';
import { ActiveEditorCommand, Commands } from './commands'; import { ActiveEditorCommand, Commands } from './commands';
import { BuiltInCommands } from '../constants'; import { BuiltInCommands } from '../constants';
@@ -27,13 +27,11 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
const gitUri = await GitUri.fromUri(uri, this.git); const gitUri = await GitUri.fromUri(uri, this.git);
try { try {
const log = await this.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, gitUri.sha, undefined, gitUri.sha ? undefined : 1); commit = await this.git.getLogCommit(gitUri.repoPath, gitUri.fsPath, gitUri.sha, { firstIfMissing: true });
if (!log) return window.showWarningMessage(`Unable to open diff. File is probably not under source control`); if (!commit) return window.showWarningMessage(`Unable to open diff. File is probably not under source control`);
commit = (gitUri.sha && log.commits.get(gitUri.sha)) || Iterables.first(log.commits.values());
} }
catch (ex) { catch (ex) {
Logger.error('[GitLens.DiffWithWorkingCommand]', `getLogForFile(${gitUri.repoPath}, ${gitUri.fsPath})`, ex); Logger.error('[GitLens.DiffWithWorkingCommand]', `getLogCommit(${gitUri.repoPath}, ${gitUri.fsPath}, ${gitUri.sha})`, ex);
return window.showErrorMessage(`Unable to open diff. See output channel for more details`); return window.showErrorMessage(`Unable to open diff. See output channel for more details`);
} }
} }

View File

@@ -54,10 +54,8 @@ export class ShowQuickCommitFileDetailsCommand extends ActiveEditorCachedCommand
} }
if (!fileLog) { if (!fileLog) {
const log = await this.git.getLogForFile(commit ? commit.repoPath : gitUri.repoPath, commit ? commit.uri.fsPath : gitUri.fsPath, sha, undefined, 2); commit = await this.git.getLogCommit(commit ? commit.repoPath : gitUri.repoPath, commit ? commit.uri.fsPath : gitUri.fsPath, sha, { previous: true });
if (!log) return window.showWarningMessage(`Unable to show commit file details`); if (!commit) return window.showWarningMessage(`Unable to show commit file details`);
commit = log.commits.get(sha);
} }
} }

View File

@@ -1,5 +1,4 @@
'use strict'; 'use strict';
import { Iterables } from '../system';
import { Uri } from 'vscode'; import { Uri } from 'vscode';
import { DocumentSchemes } from '../constants'; import { DocumentSchemes } from '../constants';
import { Git, GitService } from '../gitService'; import { Git, GitService } from '../gitService';
@@ -83,8 +82,7 @@ export class GitUri extends Uri {
// If this is a git uri, assume it is showing the most recent commit // If this is a git uri, assume it is showing the most recent commit
if (uri.scheme === 'git' && uri.query === '~') { if (uri.scheme === 'git' && uri.query === '~') {
const log = await git.getLogForFile(undefined, uri.fsPath, undefined, undefined, 1); const commit = await git.getLogCommit(undefined, uri.fsPath);
const commit = log && Iterables.first(log.commits.values());
if (commit) return new GitUri(uri, commit); if (commit) return new GitUri(uri, commit);
} }

View File

@@ -1,5 +1,5 @@
'use strict'; 'use strict';
import { Iterables } from './system'; // import { Iterables } from './system';
import { CancellationToken, CodeLens, CodeLensProvider, DocumentSelector, ExtensionContext, Range, TextDocument, Uri } from 'vscode'; import { CancellationToken, CodeLens, CodeLensProvider, DocumentSelector, ExtensionContext, Range, TextDocument, Uri } from 'vscode';
import { Commands } from './commands'; import { Commands } from './commands';
import { DocumentSchemes } from './constants'; import { DocumentSchemes } from './constants';
@@ -31,10 +31,7 @@ export class GitRevisionCodeLensProvider implements CodeLensProvider {
const lenses: CodeLens[] = []; const lenses: CodeLens[] = [];
const log = await this.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, gitUri.sha, undefined, 2); const commit = await this.git.getLogCommit(gitUri.repoPath, gitUri.fsPath, gitUri.sha, { firstIfMissing: true, previous: true });
if (!log) return lenses;
const commit = (gitUri.sha && log.commits.get(gitUri.sha)) || Iterables.first(log.commits.values());
if (!commit) return lenses; if (!commit) return lenses;
lenses.push(new GitDiffWithWorkingCodeLens(this.git, commit.uri.fsPath, commit, new Range(0, 0, 0, 1))); lenses.push(new GitDiffWithWorkingCodeLens(this.git, commit.uri.fsPath, commit, new Range(0, 0, 0, 1)));

View File

@@ -4,7 +4,7 @@ import { Disposable, Event, EventEmitter, ExtensionContext, FileSystemWatcher, l
import { CommandContext, setCommandContext } from './commands'; import { CommandContext, setCommandContext } from './commands';
import { CodeLensVisibility, IConfig } from './configuration'; import { CodeLensVisibility, IConfig } from './configuration';
import { DocumentSchemes, WorkspaceState } from './constants'; import { DocumentSchemes, WorkspaceState } from './constants';
import { Git, GitBlameParser, GitBranch, GitCommit, GitLogParser, GitRemote, GitStatusFile, GitStatusParser, IGitAuthor, IGitBlame, IGitBlameLine, IGitBlameLines, IGitLog, IGitStatus } from './git/git'; import { Git, GitBlameParser, GitBranch, GitCommit, GitLogCommit, GitLogParser, GitRemote, GitStatusFile, GitStatusParser, IGitAuthor, IGitBlame, IGitBlameLine, IGitBlameLines, IGitLog, IGitStatus } from './git/git';
import { IGitUriData, GitUri } from './git/gitUri'; import { IGitUriData, GitUri } from './git/gitUri';
import { GitCodeLensProvider } from './gitCodeLensProvider'; import { GitCodeLensProvider } from './gitCodeLensProvider';
import { Logger } from './logger'; import { Logger } from './logger';
@@ -243,13 +243,11 @@ export class GitService extends Disposable {
if (await this._fileExists(repoPath, fileName)) return fileName; if (await this._fileExists(repoPath, fileName)) return fileName;
// Get the most recent commit for this file name // Get the most recent commit for this file name
let log = await this.getLogForFile(repoPath, fileName, undefined, undefined, 1); let c = await this.getLogCommit(repoPath, fileName);
if (!log) return undefined; if (!c) return undefined;
let c = Iterables.first(log.commits.values());
// Get the full commit (so we can see if there are any matching renames in the file statuses) // Get the full commit (so we can see if there are any matching renames in the file statuses)
log = await this.getLogForRepo(repoPath, c.sha, 1); let log = await this.getLogForRepo(repoPath, c.sha, 1);
if (!log) return undefined; if (!log) return undefined;
c = Iterables.first(log.commits.values()); c = Iterables.first(log.commits.values());
@@ -500,6 +498,28 @@ export class GitService extends Disposable {
} }
} }
async getLogCommit(repoPath: string, fileName: string, options?: { firstIfMissing?: boolean, previous?: boolean }): Promise<GitLogCommit | undefined>;
async getLogCommit(repoPath: string, fileName: string, sha: string, options?: { firstIfMissing?: boolean, previous?: boolean }): Promise<GitLogCommit | undefined>;
async getLogCommit(repoPath: string, fileName: string, shaOrOptions?: string | { firstIfMissing?: boolean, previous?: boolean }, options?: { firstIfMissing?: boolean, previous?: boolean }): Promise<GitLogCommit | undefined> {
let sha: string;
if (typeof shaOrOptions === 'string') {
sha = shaOrOptions;
}
else if (!options) {
options = shaOrOptions;
}
options = options || {};
const log = await this.getLogForFile(repoPath, fileName, sha, undefined, options.previous ? 2 : 1);
if (!log) return undefined;
const commit = sha && log.commits.get(sha);
if (!commit && !options.firstIfMissing) return undefined;
return commit || Iterables.first(log.commits.values());
}
getLogForFile(repoPath: string, fileName: string, sha?: string, range?: Range, maxCount?: number, reverse: boolean = false): Promise<IGitLog | undefined> { getLogForFile(repoPath: string, fileName: string, sha?: string, range?: Range, maxCount?: number, reverse: boolean = false): Promise<IGitLog | undefined> {
Logger.log(`getLogForFile('${repoPath}', '${fileName}', ${sha}, ${range && `[${range.start.line}, ${range.end.line}]`}, ${maxCount}, ${reverse})`); Logger.log(`getLogForFile('${repoPath}', '${fileName}', ${sha}, ${range && `[${range.start.line}, ${range.end.line}]`}, ${maxCount}, ${reverse})`);

View File

@@ -39,10 +39,8 @@ export class CommitFileDetailsQuickPick {
const isUncommitted = commit.isUncommitted; const isUncommitted = commit.isUncommitted;
if (isUncommitted) { if (isUncommitted) {
// Since we can't trust the previous sha on an uncommitted commit, find the last commit for this file // Since we can't trust the previous sha on an uncommitted commit, find the last commit for this file
const log = await git.getLogForFile(undefined, commit.uri.fsPath, undefined, undefined, 2); commit = await git.getLogCommit(undefined, commit.uri.fsPath, { previous: true });
if (!log) return undefined; if (!commit) return undefined;
commit = Iterables.first(log.commits.values());
} }
items.push(new CommandQuickPickItem({ items.push(new CommandQuickPickItem({
@@ -141,7 +139,7 @@ export class CommitFileDetailsQuickPick {
c = undefined; c = undefined;
// Try to find the next commit // Try to find the next commit
const nextLog = await git.getLogForFile(commit.repoPath, uri.fsPath, commit.sha, undefined, 1, true); const nextLog = await git.getLogForFile(commit.repoPath, uri.fsPath, commit.sha, undefined, 1, true, true);
const next = nextLog && Iterables.first(nextLog.commits.values()); const next = nextLog && Iterables.first(nextLog.commits.values());
if (next && next.sha !== commit.sha) { if (next && next.sha !== commit.sha) {
c = commit; c = commit;