From 2ef6c37c89c257fc0ad84caed5823e070f1ab50b Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 15 Mar 2017 12:00:33 -0400 Subject: [PATCH] Fixes issue showing repo history w/ no active editor --- src/commands/showQuickFileHistory.ts | 2 +- src/commands/showQuickRepoHistory.ts | 9 ++++---- src/git/gitUri.ts | 32 ++++++++++++++++++++++------ src/gitProvider.ts | 7 +++--- src/quickPicks/fileHistory.ts | 4 ++-- src/quickPicks/quickPicks.ts | 18 +++++++++++++--- src/quickPicks/repoHistory.ts | 12 +++++------ 7 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/commands/showQuickFileHistory.ts b/src/commands/showQuickFileHistory.ts index 4f2b8d8..f1f24a0 100644 --- a/src/commands/showQuickFileHistory.ts +++ b/src/commands/showQuickFileHistory.ts @@ -25,7 +25,7 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCommand { maxCount = this.git.config.advanced.maxQuickHistory; } - const progressCancellation = FileHistoryQuickPick.showProgress(maxCount); + const progressCancellation = FileHistoryQuickPick.showProgress(gitUri); try { if (!log) { log = await this.git.getLogForFile(gitUri.fsPath, gitUri.sha, gitUri.repoPath, undefined, maxCount); diff --git a/src/commands/showQuickRepoHistory.ts b/src/commands/showQuickRepoHistory.ts index e04a412..ba7ce5c 100644 --- a/src/commands/showQuickRepoHistory.ts +++ b/src/commands/showQuickRepoHistory.ts @@ -16,22 +16,21 @@ export class ShowQuickRepoHistoryCommand extends ActiveEditorCommand { uri = editor && editor.document && editor.document.uri; } - const gitUri = await GitUri.fromUri(uri, this.git); - Logger.log(`ShowQuickRepoHistoryCommand.execute`, gitUri.shortSha); + const gitUri = uri && await GitUri.fromUri(uri, this.git); if (maxCount == null) { maxCount = this.git.config.advanced.maxQuickHistory; } - const progressCancellation = RepoHistoryQuickPick.showProgress(maxCount); + const progressCancellation = RepoHistoryQuickPick.showProgress(); try { if (!log) { - const repoPath = gitUri.repoPath || await this.git.getRepoPathFromUri(uri, this.repoPath); + const repoPath = (gitUri && gitUri.repoPath) || await this.git.getRepoPathFromUri(uri, this.repoPath); if (!repoPath) return window.showWarningMessage(`Unable to show repository history`); if (progressCancellation.token.isCancellationRequested) return undefined; - log = await this.git.getLogForRepo(repoPath, gitUri.sha, maxCount); + log = await this.git.getLogForRepo(repoPath, (gitUri && gitUri.sha), maxCount); if (!log) return window.showWarningMessage(`Unable to show repository history`); } diff --git a/src/git/gitUri.ts b/src/git/gitUri.ts index b928802..bb07d8b 100644 --- a/src/git/gitUri.ts +++ b/src/git/gitUri.ts @@ -11,7 +11,9 @@ export class GitUri extends Uri { repoPath?: string | undefined; sha?: string | undefined; - constructor(uri?: Uri, commit?: IGitCommitInfo) { + constructor(uri?: Uri, commit?: IGitCommitInfo); + constructor(uri?: Uri, repoPath?: string); + constructor(uri?: Uri, commitOrRepoPath?: IGitCommitInfo | string) { super(); if (!uri) return; @@ -33,12 +35,18 @@ export class GitUri extends Uri { this.repoPath = data.repoPath; } } - else if (commit) { - base._fsPath = path.resolve(commit.repoPath, commit.originalFileName || commit.fileName); + else if (commitOrRepoPath) { + if (typeof commitOrRepoPath === 'string') { + this.repoPath = commitOrRepoPath; + } + else { + const commit = commitOrRepoPath; + base._fsPath = path.resolve(commit.repoPath, commit.originalFileName || commit.fileName); - if (!Git.isUncommitted(commit.sha)) { - this.sha = commit.sha; - this.repoPath = commit.repoPath; + if (!Git.isUncommitted(commit.sha)) { + this.sha = commit.sha; + this.repoPath = commit.repoPath; + } } } } @@ -51,6 +59,16 @@ export class GitUri extends Uri { return Uri.file(this.sha ? this.path : this.fsPath); } + getFormattedPath(separator: string = ' \u00a0\u2022\u00a0 '): string { + let directory = path.dirname(this.fsPath); + if (this.repoPath) { + directory = path.relative(this.repoPath, directory); + } + return (!directory || directory === '.') + ? path.basename(this.fsPath) + : `${path.basename(this.fsPath)}${separator}${directory}`; + } + static async fromUri(uri: Uri, git: GitProvider) { if (uri instanceof GitUri) return uri; @@ -64,7 +82,7 @@ export class GitUri extends Uri { if (commit) return new GitUri(uri, commit); } - return new GitUri(uri); + return new GitUri(uri, git && git.repoPath); } } diff --git a/src/gitProvider.ts b/src/gitProvider.ts index 5daeaa0..cde0e63 100644 --- a/src/gitProvider.ts +++ b/src/gitProvider.ts @@ -58,9 +58,10 @@ export class GitProvider extends Disposable { return this._onDidBlameFailEmitter.event; } + public repoPath: string; + private _gitCache: Map | undefined; private _cacheDisposable: Disposable | undefined; - private _repoPath: string; private _uriCache: Map | undefined; config: IConfig; @@ -76,7 +77,7 @@ export class GitProvider extends Disposable { constructor(private context: ExtensionContext) { super(() => this.dispose()); - this._repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string; + this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string; this._onConfigurationChanged(); @@ -185,7 +186,7 @@ export class GitProvider extends Disposable { return; } - const gitignorePath = path.join(this._repoPath, '.gitignore'); + const gitignorePath = path.join(this.repoPath, '.gitignore'); fs.exists(gitignorePath, e => { if (e) { fs.readFile(gitignorePath, 'utf8', (err, data) => { diff --git a/src/quickPicks/fileHistory.ts b/src/quickPicks/fileHistory.ts index d238c78..dfc770d 100644 --- a/src/quickPicks/fileHistory.ts +++ b/src/quickPicks/fileHistory.ts @@ -9,8 +9,8 @@ import * as path from 'path'; export class FileHistoryQuickPick { - static showProgress(maxCount?: number) { - return showQuickPickProgress(`Loading file history \u2014 ${maxCount ? ` limited to ${maxCount} commits` : ` this may take a while`}\u2026`, + static showProgress(uri: GitUri) { + return showQuickPickProgress(`${uri.getFormattedPath()}${uri.sha ? ` \u00a0\u2022\u00a0 ${uri.sha.substring(0, 8)}` : ''}`, { left: KeyNoopCommand, ',': KeyNoopCommand, diff --git a/src/quickPicks/quickPicks.ts b/src/quickPicks/quickPicks.ts index 797da9f..b0041a1 100644 --- a/src/quickPicks/quickPicks.ts +++ b/src/quickPicks/quickPicks.ts @@ -1,5 +1,5 @@ 'use strict'; -import { CancellationTokenSource, commands, QuickPickItem, QuickPickOptions, TextEditor, Uri, window, workspace } from 'vscode'; +import { CancellationTokenSource, commands, Disposable, QuickPickItem, QuickPickOptions, TextEditor, Uri, window, workspace } from 'vscode'; import { Commands, Keyboard, KeyboardScope, KeyMapping, openEditor } from '../commands'; import { IAdvancedConfig } from '../configuration'; // import { Logger } from '../logger'; @@ -8,9 +8,21 @@ export function getQuickPickIgnoreFocusOut() { return !workspace.getConfiguration('gitlens').get('advanced').quickPick.closeOnFocusOut; } -export function showQuickPickProgress(message: string, mapping?: KeyMapping): CancellationTokenSource { +export function showQuickPickProgress(message: string, mapping?: KeyMapping, delay: boolean = false): CancellationTokenSource { const cancellation = new CancellationTokenSource(); - _showQuickPickProgress(message, cancellation, mapping); + + if (delay) { + let disposable: Disposable; + const timer = setTimeout(() => { + disposable && disposable.dispose(); + _showQuickPickProgress(message, cancellation, mapping); + }, 250); + disposable = cancellation.token.onCancellationRequested(() => clearTimeout(timer)); + } + else { + _showQuickPickProgress(message, cancellation, mapping); + } + return cancellation; } diff --git a/src/quickPicks/repoHistory.ts b/src/quickPicks/repoHistory.ts index 7c90ccf..d4a936d 100644 --- a/src/quickPicks/repoHistory.ts +++ b/src/quickPicks/repoHistory.ts @@ -8,8 +8,8 @@ import { CommandQuickPickItem, getQuickPickIgnoreFocusOut, showQuickPickProgress export class RepoHistoryQuickPick { - static showProgress(maxCount?: number) { - return showQuickPickProgress(`Loading repository history \u2014 ${maxCount ? ` limited to ${maxCount} commits` : ` this may take a while`}\u2026`, + static showProgress() { + return showQuickPickProgress('Repository history \u2014 search by commit message, filename, or sha', { left: KeyNoopCommand, ',': KeyNoopCommand, @@ -22,11 +22,11 @@ export class RepoHistoryQuickPick { let previousPageCommand: CommandQuickPickItem; - if (log.truncated || uri.sha) { + if (log.truncated || (uri && uri.sha)) { items.splice(0, 0, new CommandQuickPickItem({ label: `$(sync) Show All Commits`, description: `\u00a0 \u2014 \u00a0\u00a0 this may take a while` - }, Commands.ShowQuickRepoHistory, [Uri.file(uri.fsPath), 0, goBackCommand])); + }, Commands.ShowQuickRepoHistory, [uri && Uri.file(uri.fsPath), 0, goBackCommand])); if (nextPageCommand) { items.splice(0, 0, nextPageCommand); @@ -43,7 +43,7 @@ export class RepoHistoryQuickPick { previousPageCommand = new CommandQuickPickItem({ label: `$(arrow-left) Show Previous Commits`, description: `\u00a0 \u2014 \u00a0\u00a0 shows ${log.maxCount} older commits` - }, Commands.ShowQuickRepoHistory, [new GitUri(uri, last), log.maxCount, goBackCommand, undefined, npc]); + }, Commands.ShowQuickRepoHistory, [new GitUri(uri ? uri : last.uri, last), log.maxCount, goBackCommand, undefined, npc]); items.splice(0, 0, previousPageCommand); } @@ -66,7 +66,7 @@ export class RepoHistoryQuickPick { const pick = await window.showQuickPick(items, { matchOnDescription: true, matchOnDetail: true, - placeHolder: 'Search by commit message, filename, or sha', + placeHolder: 'Repository history \u2014 search by commit message, filename, or sha', ignoreFocusOut: getQuickPickIgnoreFocusOut() // onDidSelectItem: (item: QuickPickItem) => { // scope.setKeyCommand('right', item);