Skip to content

Commit

Permalink
Adds open/copy commit on remote to Timeline view
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Oct 24, 2020
1 parent 7f06da0 commit cbed5de
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5814,6 +5814,14 @@
"group": "9_gitlens@1"
}
],
"timeline/item/context": [
{
"command": "gitlens.openCommitInRemote",
"when": "gitlens:enabled && gitlens:hasRemotes && timelineItem =~ /git:file:commit\\b/",
"group": "inline@99",
"alt": "gitlens.copyRemoteCommitUrl"
}
],
"view/title": [
{
"command": "gitlens.closeUpdatesView",
Expand Down
22 changes: 22 additions & 0 deletions src/@types/timeline.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AccessibilityInformation, Command, ThemeIcon, Uri } from 'vscode';

declare module 'vscode' {
export interface TimelineItem {
readonly timestamp: number;
readonly label: string;

readonly id?: string;
readonly iconPath?: Uri | { light: Uri; dark: Uri } | ThemeIcon;
readonly description?: string;
readonly detail?: string;
readonly command?: Command;
readonly contextValue?: string;
readonly accessibilityInformation?: AccessibilityInformation;
}

export interface GitTimelineItem extends TimelineItem {
readonly ref: string;
readonly previousRef: string;
readonly message: string;
}
}
36 changes: 35 additions & 1 deletion src/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {
commands,
Disposable,
ExtensionContext,
SourceControl,
GitTimelineItem,
SourceControlResourceGroup,
SourceControlResourceState,
TextDocumentShowOptions,
TextEditor,
TextEditorEdit,
TimelineItem,
Uri,
ViewColumn,
window,
Expand Down Expand Up @@ -215,6 +216,12 @@ export interface CommandBaseContext {
uri?: Uri;
}

export interface CommandGitTimelineItemContext extends CommandBaseContext {
readonly type: 'timeline-item:git';
readonly item: GitTimelineItem;
readonly uri: Uri;
}

export interface CommandScmGroupsContext extends CommandBaseContext {
readonly type: 'scm-groups';
readonly scmResourceGroups: SourceControlResourceGroup[];
Expand Down Expand Up @@ -247,6 +254,10 @@ export interface CommandViewNodeContext extends CommandBaseContext {
readonly node: ViewNode;
}

export function isCommandContextGitTimelineItem(context: CommandContext): context is CommandGitTimelineItemContext {
return context.type === 'timeline-item:git';
}

export function isCommandContextViewNodeHasBranch(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { branch: GitBranch } } {
Expand Down Expand Up @@ -344,6 +355,7 @@ export function isCommandContextViewNodeHasTag(
}

export type CommandContext =
| CommandGitTimelineItemContext
| CommandScmGroupsContext
| CommandScmStatesContext
| CommandUnknownContext
Expand All @@ -369,6 +381,23 @@ function isScmResourceState(resource: any): resource is SourceControlResourceSta
return (resource as SourceControlResourceState).resourceUri != null;
}

function isTimelineItem(item: any): item is TimelineItem {
if (item == null) return false;

return (item as TimelineItem).timestamp != null && (item as TimelineItem).label != null;
}

function isGitTimelineItem(item: any): item is GitTimelineItem {
if (item == null) return false;

return (
isTimelineItem(item) &&
(item as GitTimelineItem).ref != null &&
(item as GitTimelineItem).previousRef != null &&
(item as GitTimelineItem).message != null
);
}

export abstract class Command implements Disposable {
static getMarkdownCommandArgsCore<T>(command: Commands, args: T): string {
return `command:${command}?${encodeURIComponent(JSON.stringify(args))}`;
Expand Down Expand Up @@ -489,6 +518,11 @@ export abstract class Command implements Disposable {
return [{ command: command, type: 'scm-groups', scmResourceGroups: groups }, args.slice(count)];
}

if (isGitTimelineItem(firstArg)) {
const [item, uri, ...rest] = args as [GitTimelineItem, Uri, any];
return [{ command: command, type: 'timeline-item:git', item: item, uri: uri }, rest];
}

return [{ command: command, type: 'unknown', editor: editor, uri: editor?.document.uri }, args];
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/commands/openCommitOnRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Commands,
executeCommand,
getCommandUri,
isCommandContextGitTimelineItem,
isCommandContextViewNodeHasCommit,
} from './common';
import { Container } from '../container';
Expand Down Expand Up @@ -44,6 +45,11 @@ export class OpenCommitOnRemoteCommand extends ActiveEditorCommand {
uri = context.node.commit.isFile ? context.node.commit.uri : context.node.uri;
}

if (isCommandContextGitTimelineItem(context)) {
args = { sha: context.item.ref };
uri = context.uri;
}

if (context.command === Commands.CopyRemoteCommitUrl) {
args = { ...args, clipboard: true };
}
Expand Down

0 comments on commit cbed5de

Please sign in to comment.