Browse Source

Fixes #1866 copy sha & copy message didn't work

main
Eric Amodio 2 years ago
parent
commit
79f0d0b297
4 changed files with 71 additions and 49 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +39
    -29
      src/commands/copyMessageToClipboard.ts
  3. +26
    -20
      src/commands/copyShaToClipboard.ts
  4. +5
    -0
      src/git/models/commit.ts

+ 1
- 0
CHANGELOG.md View File

@ -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

+ 39
- 29
src/commands/copyMessageToClipboard.ts View File

@ -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;
}
}

+ 26
- 20
src/commands/copyShaToClipboard.ts View File

@ -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;
}
}
}

+ 5
- 0
src/git/models/commit.ts View File

@ -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<boolean> {
return this.container.git.hasCommitBeenPushed(this.repoPath, this.ref);

Loading…
Cancel
Save