Changes behavior of copyShaToClipboard

Copies the sha of the most recent commit to the repository if there is no active editor
This commit is contained in:
Eric Amodio
2017-02-17 22:22:21 -05:00
parent d8d221a624
commit 46039dbf24
2 changed files with 23 additions and 13 deletions

View File

@@ -1,27 +1,37 @@
'use strict'; 'use strict';
import { TextEditor, TextEditorEdit, Uri, window } from 'vscode'; import { Iterables } from '../system';
import { EditorCommand } from './commands'; import { TextEditor, Uri, window } from 'vscode';
import { ActiveEditorCommand } from './commands';
import { Commands } from '../constants'; import { Commands } from '../constants';
import GitProvider, { GitUri } from '../gitProvider'; import GitProvider, { GitUri } from '../gitProvider';
import { Logger } from '../logger'; import { Logger } from '../logger';
import { copy } from 'copy-paste'; import { copy } from 'copy-paste';
export default class CopyShaToClipboard extends EditorCommand { export default class CopyShaToClipboardCommand extends ActiveEditorCommand {
constructor(private git: GitProvider) { constructor(private git: GitProvider, public repoPath: string) {
super(Commands.CopyShaToClipboard); super(Commands.CopyShaToClipboard);
} }
async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string): Promise<any> { async execute(editor: TextEditor, uri?: Uri, sha?: string): Promise<any> {
if (!(uri instanceof Uri)) { if (!(uri instanceof Uri)) {
if (!editor.document) return undefined; uri = editor && editor.document && editor.document.uri;
uri = editor.document.uri;
} }
const line = editor.selection.active.line;
const gitUri = GitUri.fromUri(uri, this.git);
try { try {
// If we don't have an editor then get the sha of the last commit to the repository
if (!uri) {
const log = await this.git.getLogForRepo(this.repoPath, undefined, 1);
if (!log) return undefined;
sha = Iterables.first(log.commits.values()).sha;
copy(sha);
return undefined;
}
const line = editor.selection.active.line;
const gitUri = GitUri.fromUri(uri, this.git);
if (!sha) { if (!sha) {
const blameline = line - gitUri.offset; const blameline = line - gitUri.offset;
if (blameline < 0) return undefined; if (blameline < 0) return undefined;
@@ -33,7 +43,7 @@ export default class CopyShaToClipboard extends EditorCommand {
sha = blame.commit.sha; sha = blame.commit.sha;
} }
catch (ex) { catch (ex) {
Logger.error('[GitLens.CopyShaToClipboard]', `getBlameForLine(${blameline})`, ex); Logger.error('[GitLens.CopyShaToClipboardCommand]', `getBlameForLine(${blameline})`, ex);
return window.showErrorMessage(`Unable to copy sha. See output channel for more details`); return window.showErrorMessage(`Unable to copy sha. See output channel for more details`);
} }
} }
@@ -42,7 +52,7 @@ export default class CopyShaToClipboard extends EditorCommand {
return undefined; return undefined;
} }
catch (ex) { catch (ex) {
Logger.error('GitLens.CopyShaToClipboard', ex); Logger.error('GitLens.CopyShaToClipboardCommand', ex);
return window.showErrorMessage(`Unable to copy sha. See output channel for more details`); return window.showErrorMessage(`Unable to copy sha. See output channel for more details`);
} }
} }

View File

@@ -79,7 +79,7 @@ export async function activate(context: ExtensionContext) {
context.subscriptions.push(activeLineController); context.subscriptions.push(activeLineController);
context.subscriptions.push(new CopyMessageToClipboardCommand(git, repoPath)); context.subscriptions.push(new CopyMessageToClipboardCommand(git, repoPath));
context.subscriptions.push(new CopyShaToClipboardCommand(git)); context.subscriptions.push(new CopyShaToClipboardCommand(git, repoPath));
context.subscriptions.push(new DiffWithWorkingCommand(git)); context.subscriptions.push(new DiffWithWorkingCommand(git));
context.subscriptions.push(new DiffLineWithWorkingCommand(git)); context.subscriptions.push(new DiffLineWithWorkingCommand(git));
context.subscriptions.push(new DiffWithPreviousCommand(git)); context.subscriptions.push(new DiffWithPreviousCommand(git));