From 2a9b274920d57f9f84760672d7280cfd43e72dce Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sun, 9 Apr 2017 10:58:28 -0400 Subject: [PATCH] Changes search prefixes Fixes issue with author searching --- src/commands/showCommitSearch.ts | 50 ++++++++++++++++++++-------------------- src/gitService.ts | 20 +++++++++++----- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/commands/showCommitSearch.ts b/src/commands/showCommitSearch.ts index 73d63f5..ed116c6 100644 --- a/src/commands/showCommitSearch.ts +++ b/src/commands/showCommitSearch.ts @@ -1,17 +1,24 @@ 'use strict'; import { commands, InputBoxOptions, TextEditor, Uri, window } from 'vscode'; import { ActiveEditorCachedCommand, Commands } from './common'; -import { Git, GitService, GitUri } from '../gitService'; +import { Git, GitRepoSearchBy, GitService, GitUri } from '../gitService'; import { Logger } from '../logger'; import { CommandQuickPickItem, CommitsQuickPick } from '../quickPicks'; +const searchByRegex = /^([@:#])/; +const searchByMap = new Map([ + ['@', GitRepoSearchBy.Author], + [':', GitRepoSearchBy.Files], + ['#', GitRepoSearchBy.Sha] +]); + export class ShowCommitSearchCommand extends ActiveEditorCachedCommand { constructor(private git: GitService) { super(Commands.ShowCommitSearch); } - async execute(editor: TextEditor, uri?: Uri, search?: string, searchBy?: undefined | 'author' | 'files' | 'message' | 'sha', goBackCommand?: CommandQuickPickItem) { + async execute(editor: TextEditor, uri?: Uri, search?: string, searchBy?: GitRepoSearchBy, goBackCommand?: CommandQuickPickItem) { if (!(uri instanceof Uri)) { if (!editor || !editor.document) return undefined; uri = editor.document.uri; @@ -23,27 +30,20 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand { search = await window.showInputBox({ value: search, prompt: `Please enter a search string`, - placeHolder: `search by message, author (use a:), files (use f:), or sha (use s:)` + placeHolder: `search by message, author (use @), files (use :), or commit id (use #)` } as InputBoxOptions); if (search === undefined) return goBackCommand && goBackCommand.execute(); - if (Git.isSha(search)) { - searchBy = 'sha'; - } - else if (search.startsWith('a:')) { - searchBy = 'author'; - search = search.substring((search[2] === ' ') ? 3 : 2); - } - else if (search.startsWith('f:')) { - searchBy = 'files'; - search = search.substring((search[2] === ' ') ? 3 : 2); + const match = searchByRegex.exec(search); + if (match && match[1]) { + searchBy = searchByMap.get(match[1]); + search = search.substring((search[1] === ' ') ? 2 : 1); } - else if (search.startsWith('s:')) { - searchBy = 'sha'; - search = search.substring((search[2] === ' ') ? 3 : 2); + else if (Git.isSha(search)) { + searchBy = GitRepoSearchBy.Sha; } else { - searchBy = 'message'; + searchBy = GitRepoSearchBy.Message; } } @@ -53,21 +53,21 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand { let originalSearch: string; let placeHolder: string; switch (searchBy) { - case 'author': - originalSearch = `a:${search}`; + case GitRepoSearchBy.Author: + originalSearch = `@${search}`; placeHolder = `commits with author matching '${search}'`; break; - case 'files': - originalSearch = `f:${search}`; + case GitRepoSearchBy.Files: + originalSearch = `:${search}`; placeHolder = `commits with files matching '${search}'`; break; - case 'message': + case GitRepoSearchBy.Message: originalSearch = search; placeHolder = `commits with message matching '${search}'`; break; - case 'sha': - originalSearch = `s:${search}`; - placeHolder = `commits with sha matching '${search}'`; + case GitRepoSearchBy.Sha: + originalSearch = `#${search}`; + placeHolder = `commits with id matching '${search}'`; break; } diff --git a/src/gitService.ts b/src/gitService.ts index 9ed80d6..663f387 100644 --- a/src/gitService.ts +++ b/src/gitService.ts @@ -48,6 +48,14 @@ enum RemoveCacheReason { DocumentSaved } +export type GitRepoSearchBy = 'author' | 'files' | 'message' | 'sha'; +export const GitRepoSearchBy = { + Author: 'author' as GitRepoSearchBy, + Files: 'files' as GitRepoSearchBy, + Message: 'message' as GitRepoSearchBy, + Sha: 'sha' as GitRepoSearchBy +}; + export class GitService extends Disposable { private _onDidChangeGitCacheEmitter = new EventEmitter(); @@ -562,7 +570,7 @@ export class GitService extends Disposable { } } - async getLogForRepoSearch(repoPath: string, search: string, searchBy: 'author' | 'files' | 'message' | 'sha', maxCount?: number): Promise { + async getLogForRepoSearch(repoPath: string, search: string, searchBy: GitRepoSearchBy, maxCount?: number): Promise { Logger.log(`getLogForRepoSearch('${repoPath}', ${search}, ${searchBy}, ${maxCount})`); if (maxCount == null) { @@ -571,16 +579,16 @@ export class GitService extends Disposable { let searchArgs: string[]; switch (searchBy) { - case 'author': - searchArgs = [`'--author='${search}`]; + case GitRepoSearchBy.Author: + searchArgs = [`--author=${search}`]; break; - case 'files': + case GitRepoSearchBy.Files: searchArgs = [`--`, `${search}`]; break; - case 'message': + case GitRepoSearchBy.Message: searchArgs = [`--grep=${search}`]; break; - case 'sha': + case GitRepoSearchBy.Sha: searchArgs = [search]; maxCount = 1; break;