diff --git a/CHANGELOG.md b/CHANGELOG.md index c8d19d2..2b24d0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ### Fixed +- Fixes [#1866](https://github.com/gitkraken/vscode-gitlens/issues/1866) - Copy SHA and Copy Message don't work from the views (commits, branches, etc) - Fixes [#1865](https://github.com/gitkraken/vscode-gitlens/issues/1865) - Value shortOffset out of range for Intl.DateTimeFormat options property timeZoneName - Fixes [#1742](https://github.com/gitkraken/vscode-gitlens/issues/1742) - New file lines keep jumping down - Fixes [#1846](https://github.com/gitkraken/vscode-gitlens/issues/1846) - Restoring (checkout) a deleted file from a commit doesn't work diff --git a/src/commands/copyMessageToClipboard.ts b/src/commands/copyMessageToClipboard.ts index 9a607f0..b58ac6a 100644 --- a/src/commands/copyMessageToClipboard.ts +++ b/src/commands/copyMessageToClipboard.ts @@ -27,11 +27,19 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand { super(Commands.CopyMessageToClipboard); } - protected override preExecute(context: CommandContext, args?: CopyMessageToClipboardCommandArgs) { + protected override async preExecute(context: CommandContext, args?: CopyMessageToClipboardCommandArgs) { if (isCommandContextViewNodeHasCommit(context)) { args = { ...args }; args.sha = context.node.commit.sha; - return this.execute(context.editor, context.node.commit.file?.uri, args); + if (context.node.commit.message != null) { + await context.node.commit.ensureFullDetails(); + } + args.message = context.node.commit.message; + return this.execute( + context.editor, + context.node.commit.file?.uri ?? context.node.commit.getRepository()?.uri, + args, + ); } else if (isCommandContextViewNodeHasBranch(context)) { args = { ...args }; args.sha = context.node.branch.sha; @@ -50,43 +58,45 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand { args = { ...args }; try { - let repoPath; + if (!args.message) { + let repoPath; - // If we don't have an editor then get the message of the last commit to the branch - if (uri == null) { - repoPath = this.container.git.getBestRepository(editor)?.path; - if (!repoPath) return; + // If we don't have an editor then get the message of the last commit to the branch + if (uri == null) { + repoPath = this.container.git.getBestRepository(editor)?.path; + if (!repoPath) return; - const log = await this.container.git.getLog(repoPath, { limit: 1 }); - if (log == null) return; + const log = await this.container.git.getLog(repoPath, { limit: 1 }); + if (log == null) return; - const commit = first(log.commits.values()); - if (commit?.message == null) return; + const commit = first(log.commits.values()); + if (commit?.message == null) return; - args.message = commit.message; - } else if (args.message == null) { - const gitUri = await GitUri.fromUri(uri); - repoPath = gitUri.repoPath; + args.message = commit.message; + } else if (args.message == null) { + const gitUri = await GitUri.fromUri(uri); + repoPath = gitUri.repoPath; - if (args.sha == null) { - const blameline = editor?.selection.active.line ?? 0; - if (blameline < 0) return; + if (args.sha == null) { + const blameline = editor?.selection.active.line ?? 0; + if (blameline < 0) return; - try { - const blame = await this.container.git.getBlameForLine(gitUri, blameline, editor?.document); - if (blame == null || blame.commit.isUncommitted) return; + try { + const blame = await this.container.git.getBlameForLine(gitUri, blameline, editor?.document); + if (blame == null || blame.commit.isUncommitted) return; - void (await GitActions.Commit.copyMessageToClipboard(blame.commit)); - return; - } catch (ex) { - Logger.error(ex, 'CopyMessageToClipboardCommand', `getBlameForLine(${blameline})`); - void Messages.showGenericErrorMessage('Unable to copy message'); + void (await GitActions.Commit.copyMessageToClipboard(blame.commit)); + return; + } catch (ex) { + Logger.error(ex, 'CopyMessageToClipboardCommand', `getBlameForLine(${blameline})`); + void Messages.showGenericErrorMessage('Unable to copy message'); + return; + } + } else { + void (await GitActions.Commit.copyMessageToClipboard({ ref: args.sha, repoPath: repoPath! })); return; } - } else { - void (await GitActions.Commit.copyMessageToClipboard({ ref: args.sha, repoPath: repoPath! })); - return; } } diff --git a/src/commands/copyShaToClipboard.ts b/src/commands/copyShaToClipboard.ts index 1d558e7..d3e641a 100644 --- a/src/commands/copyShaToClipboard.ts +++ b/src/commands/copyShaToClipboard.ts @@ -31,7 +31,11 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand { args.sha = this.container.config.advanced.abbreviateShaOnCopy ? context.node.commit.shortSha : context.node.commit.sha; - return this.execute(context.editor, context.node.commit.file?.uri, args); + return this.execute( + context.editor, + context.node.commit.file?.uri ?? context.node.commit.getRepository()?.uri, + args, + ); } else if (isCommandContextViewNodeHasBranch(context)) { args = { ...args }; args.sha = context.node.branch.sha; @@ -50,30 +54,32 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand { args = { ...args }; try { - // If we don't have an editor then get the sha of the last commit to the branch - if (uri == null) { - const repoPath = this.container.git.getBestRepository(editor)?.path; - if (!repoPath) return; + if (!args.sha) { + // If we don't have an editor then get the sha of the last commit to the branch + if (uri == null) { + const repoPath = this.container.git.getBestRepository(editor)?.path; + if (!repoPath) return; - const log = await this.container.git.getLog(repoPath, { limit: 1 }); - if (log == null) return; + const log = await this.container.git.getLog(repoPath, { limit: 1 }); + if (log == null) return; - args.sha = first(log.commits.values()).sha; - } else if (args.sha == null) { - const blameline = editor?.selection.active.line ?? 0; - if (blameline < 0) return; + args.sha = first(log.commits.values()).sha; + } else if (args.sha == null) { + const blameline = editor?.selection.active.line ?? 0; + if (blameline < 0) return; - try { - const gitUri = await GitUri.fromUri(uri); - const blame = await this.container.git.getBlameForLine(gitUri, blameline, editor?.document); - if (blame == null) return; + try { + const gitUri = await GitUri.fromUri(uri); + const blame = await this.container.git.getBlameForLine(gitUri, blameline, editor?.document); + if (blame == null) return; - args.sha = blame.commit.sha; - } catch (ex) { - Logger.error(ex, 'CopyShaToClipboardCommand', `getBlameForLine(${blameline})`); - void Messages.showGenericErrorMessage('Unable to copy commit SHA'); + args.sha = blame.commit.sha; + } catch (ex) { + Logger.error(ex, 'CopyShaToClipboardCommand', `getBlameForLine(${blameline})`); + void Messages.showGenericErrorMessage('Unable to copy commit SHA'); - return; + return; + } } } diff --git a/src/git/models/commit.ts b/src/git/models/commit.ts index 94f39bb..b53239c 100644 --- a/src/git/models/commit.ts +++ b/src/git/models/commit.ts @@ -13,6 +13,7 @@ import { GitUri } from '../gitUri'; import { GitFile, GitFileChange, GitFileWorkingTreeStatus } from './file'; import { PullRequest } from './pullRequest'; import { GitReference, GitRevision, GitRevisionReference, GitStashReference } from './reference'; +import { Repository } from './repository'; const stashNumberRegex = /stash@{(\d+)}/; @@ -473,6 +474,10 @@ export class GitCommit implements GitRevisionReference { return this._previousShaPromise; } + getRepository(): Repository | undefined { + return this.container.git.getRepository(this.repoPath); + } + @gate() async isPushed(): Promise { return this.container.git.hasCommitBeenPushed(this.repoPath, this.ref);