Adds clipboard default into commit search

This commit is contained in:
Eric Amodio
2017-05-14 01:48:53 -04:00
parent 361789f859
commit 33ef9687f2

View File

@@ -1,122 +1,128 @@
'use strict'; 'use strict';
import { commands, InputBoxOptions, TextEditor, Uri, window } from 'vscode'; import { commands, InputBoxOptions, TextEditor, Uri, window } from 'vscode';
import { ActiveEditorCachedCommand, Commands, getCommandUri } from './common'; import { ActiveEditorCachedCommand, Commands, getCommandUri } from './common';
import { GitRepoSearchBy, GitService, GitUri } from '../gitService'; import { GitRepoSearchBy, GitService, GitUri } from '../gitService';
import { Logger } from '../logger'; import { Logger } from '../logger';
import { CommandQuickPickItem, CommitsQuickPick } from '../quickPicks'; import { CommandQuickPickItem, CommitsQuickPick } from '../quickPicks';
import { ShowQuickCommitDetailsCommandArgs } from './showQuickCommitDetails'; import { ShowQuickCommitDetailsCommandArgs } from './showQuickCommitDetails';
import { paste } from 'copy-paste';
const searchByRegex = /^([@:#])/;
const searchByMap = new Map<string, GitRepoSearchBy>([ const searchByRegex = /^([@:#])/;
['@', GitRepoSearchBy.Author], const searchByMap = new Map<string, GitRepoSearchBy>([
[':', GitRepoSearchBy.Files], ['@', GitRepoSearchBy.Author],
['#', GitRepoSearchBy.Sha] [':', GitRepoSearchBy.Files],
]); ['#', GitRepoSearchBy.Sha]
]);
export interface ShowCommitSearchCommandArgs {
search?: string; export interface ShowCommitSearchCommandArgs {
searchBy?: GitRepoSearchBy; search?: string;
searchBy?: GitRepoSearchBy;
goBackCommand?: CommandQuickPickItem;
} goBackCommand?: CommandQuickPickItem;
}
export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {
export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {
constructor(private git: GitService) {
super(Commands.ShowCommitSearch); constructor(private git: GitService) {
} super(Commands.ShowCommitSearch);
}
async execute(editor: TextEditor, uri?: Uri, args: ShowCommitSearchCommandArgs = {}) {
uri = getCommandUri(uri, editor); async execute(editor: TextEditor, uri?: Uri, args: ShowCommitSearchCommandArgs = {}) {
if (uri === undefined) return undefined; uri = getCommandUri(uri, editor);
if (uri === undefined) return undefined;
const gitUri = await GitUri.fromUri(uri, this.git);
if (gitUri.repoPath === undefined) return undefined; const gitUri = await GitUri.fromUri(uri, this.git);
if (gitUri.repoPath === undefined) return undefined;
if (!args.search || args.searchBy == null) { if (!args.search || args.searchBy == null) {
args.search = await window.showInputBox({ if (!args.search) {
value: args.search, args.search = await new Promise<string>((resolve, reject) => {
prompt: `Please enter a search string`, paste((err: Error, content: string) => resolve(err ? '' : content));
placeHolder: `search by message, author (use @<name>), files (use :<pattern>), or commit id (use #<sha>)` });
} as InputBoxOptions); }
if (args.search === undefined) return args.goBackCommand === undefined ? undefined : args.goBackCommand.execute(); args.search = await window.showInputBox({
value: args.search,
const match = searchByRegex.exec(args.search); prompt: `Please enter a search string`,
if (match && match[1]) { placeHolder: `search by message, author (use @<name>), files (use :<pattern>), or commit id (use #<sha>)`
args.searchBy = searchByMap.get(match[1]); } as InputBoxOptions);
args.search = args.search.substring((args.search[1] === ' ') ? 2 : 1); if (args.search === undefined) return args.goBackCommand === undefined ? undefined : args.goBackCommand.execute();
}
else if (GitService.isSha(args.search)) { const match = searchByRegex.exec(args.search);
args.searchBy = GitRepoSearchBy.Sha; if (match && match[1]) {
} args.searchBy = searchByMap.get(match[1]);
else { args.search = args.search.substring((args.search[1] === ' ') ? 2 : 1);
args.searchBy = GitRepoSearchBy.Message; }
} else if (GitService.isSha(args.search)) {
} args.searchBy = GitRepoSearchBy.Sha;
}
try { else {
if (args.searchBy === undefined) { args.searchBy = GitRepoSearchBy.Message;
args.searchBy = GitRepoSearchBy.Message; }
} }
const log = await this.git.getLogForRepoSearch(gitUri.repoPath, args.search, args.searchBy); try {
if (log === undefined) return undefined; if (args.searchBy === undefined) {
args.searchBy = GitRepoSearchBy.Message;
let originalSearch: string | undefined = undefined; }
let placeHolder: string | undefined = undefined;
switch (args.searchBy) { const log = await this.git.getLogForRepoSearch(gitUri.repoPath, args.search, args.searchBy);
case GitRepoSearchBy.Author: if (log === undefined) return undefined;
originalSearch = `@${args.search}`;
placeHolder = `commits with author matching '${args.search}'`; let originalSearch: string | undefined = undefined;
break; let placeHolder: string | undefined = undefined;
case GitRepoSearchBy.Files: switch (args.searchBy) {
originalSearch = `:${args.search}`; case GitRepoSearchBy.Author:
placeHolder = `commits with files matching '${args.search}'`; originalSearch = `@${args.search}`;
break; placeHolder = `commits with author matching '${args.search}'`;
case GitRepoSearchBy.Message: break;
originalSearch = args.search; case GitRepoSearchBy.Files:
placeHolder = `commits with message matching '${args.search}'`; originalSearch = `:${args.search}`;
break; placeHolder = `commits with files matching '${args.search}'`;
case GitRepoSearchBy.Sha: break;
originalSearch = `#${args.search}`; case GitRepoSearchBy.Message:
placeHolder = `commits with id matching '${args.search}'`; originalSearch = args.search;
break; placeHolder = `commits with message matching '${args.search}'`;
} break;
case GitRepoSearchBy.Sha:
// Create a command to get back to here originalSearch = `#${args.search}`;
const currentCommand = new CommandQuickPickItem({ placeHolder = `commits with id matching '${args.search}'`;
label: `go back \u21A9`, break;
description: `\u00a0 \u2014 \u00a0\u00a0 to commit search` }
}, Commands.ShowCommitSearch, [
gitUri, // Create a command to get back to here
{ const currentCommand = new CommandQuickPickItem({
search: originalSearch, label: `go back \u21A9`,
goBackCommand: args.goBackCommand description: `\u00a0 \u2014 \u00a0\u00a0 to commit search`
} as ShowCommitSearchCommandArgs }, Commands.ShowCommitSearch, [
]); gitUri,
{
const pick = await CommitsQuickPick.show(this.git, log, placeHolder!, currentCommand); search: originalSearch,
if (pick === undefined) return undefined; goBackCommand: args.goBackCommand
} as ShowCommitSearchCommandArgs
if (pick instanceof CommandQuickPickItem) return pick.execute(); ]);
return commands.executeCommand(Commands.ShowQuickCommitDetails, const pick = await CommitsQuickPick.show(this.git, log, placeHolder!, currentCommand);
new GitUri(pick.commit.uri, pick.commit), if (pick === undefined) return undefined;
{
sha: pick.commit.sha, if (pick instanceof CommandQuickPickItem) return pick.execute();
commit: pick.commit,
goBackCommand: new CommandQuickPickItem({ return commands.executeCommand(Commands.ShowQuickCommitDetails,
label: `go back \u21A9`, new GitUri(pick.commit.uri, pick.commit),
description: `\u00a0 \u2014 \u00a0\u00a0 to search for ${placeHolder}` {
}, Commands.ShowCommitSearch, [ sha: pick.commit.sha,
gitUri, commit: pick.commit,
args goBackCommand: new CommandQuickPickItem({
]) label: `go back \u21A9`,
} as ShowQuickCommitDetailsCommandArgs); description: `\u00a0 \u2014 \u00a0\u00a0 to search for ${placeHolder}`
} }, Commands.ShowCommitSearch, [
catch (ex) { gitUri,
Logger.error(ex, 'ShowCommitSearchCommand'); args
return window.showErrorMessage(`Unable to find commits. See output channel for more details`); ])
} } as ShowQuickCommitDetailsCommandArgs);
} }
catch (ex) {
Logger.error(ex, 'ShowCommitSearchCommand');
return window.showErrorMessage(`Unable to find commits. See output channel for more details`);
}
}
} }