Skip to content

Commit

Permalink
Adds compare references command to the palette
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Nov 16, 2020
1 parent 281249c commit f29bb1a
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 119 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

### Added

- Adds a _Compare References..._ command (`gitlens.compareWith`) to compare two selected references
- Adds ability to enter reference ranges (e.g. `main...release/1.0`) to the _Git Command Palette_'s _history_ command

### Fixed
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,9 @@ Additionally, these integrations provide commands to copy the url of or open, fi

- Adds a _Switch to Another Branch_ (`gitlens.views.switchToAnotherBranch`) command — to quickly switch the current branch

- Adds a _Compare HEAD with..._ command (`gitlens.diffHeadWith`) to compare the index (HEAD) with the selected reference
- Adds a _Compare Working Tree with..._ command (`gitlens.diffWorkingWith`) to compare the working tree with the selected reference
- Adds a _Compare References..._ command (`gitlens.compareWith`) to compare two selected references
- Adds a _Compare HEAD with..._ command (`gitlens.compareHeadWith`) to compare the index (HEAD) with the selected reference
- Adds a _Compare Working Tree with..._ command (`gitlens.compareWorkingWith`) to compare the working tree with the selected reference

- Adds an _Open Changes (difftool)_ command (`gitlens.externalDiff`) to open the changes of a file or set of files with the configured git difftool
- Adds an _Open All Changes (difftool)_ command (`gitlens.externalDiffAll`) to open all working changes with the configured git difftool
Expand Down
45 changes: 29 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@
"onCommand:gitlens.showWelcomeView",
"onCommand:gitlens.closeUpdatesView",
"onCommand:gitlens.closeWelcomeView",
"onCommand:gitlens.compareWith",
"onCommand:gitlens.compareHeadWith",
"onCommand:gitlens.compareWorkingWith",
"onCommand:gitlens.diffDirectory",
"onCommand:gitlens.diffDirectoryWithHead",
"onCommand:gitlens.diffHeadWith",
"onCommand:gitlens.diffWorkingWith",
"onCommand:gitlens.diffWithNext",
"onCommand:gitlens.diffWithNextInDiffLeft",
"onCommand:gitlens.diffWithNextInDiffRight",
Expand Down Expand Up @@ -2622,23 +2623,31 @@
"icon": "$(close)"
},
{
"command": "gitlens.diffDirectory",
"title": "Open Directory Compare (difftool) with...",
"category": "GitLens"
"command": "gitlens.compareWith",
"title": "Compare References...",
"category": "GitLens",
"icon": "$(compare-changes)"
},
{
"command": "gitlens.diffDirectoryWithHead",
"title": "Open Directory Compare (difftool)",
"category": "GitLens"
"command": "gitlens.compareHeadWith",
"title": "Compare HEAD with...",
"category": "GitLens",
"icon": "$(compare-changes)"
},
{
"command": "gitlens.diffHeadWith",
"title": "Compare HEAD with...",
"command": "gitlens.compareWorkingWith",
"title": "Compare Working Tree with...",
"category": "GitLens",
"icon": "$(compare-changes)"
},
{
"command": "gitlens.diffDirectory",
"title": "Open Directory Compare (difftool) with...",
"category": "GitLens"
},
{
"command": "gitlens.diffWorkingWith",
"title": "Compare Working Tree with...",
"command": "gitlens.diffDirectoryWithHead",
"title": "Open Directory Compare (difftool)",
"category": "GitLens"
},
{
Expand Down Expand Up @@ -4446,19 +4455,23 @@
"when": "false"
},
{
"command": "gitlens.diffDirectory",
"command": "gitlens.compareWith",
"when": "gitlens:enabled"
},
{
"command": "gitlens.diffDirectoryWithHead",
"command": "gitlens.compareHeadWith",
"when": "gitlens:enabled"
},
{
"command": "gitlens.diffHeadWith",
"command": "gitlens.compareWorkingWith",
"when": "gitlens:enabled"
},
{
"command": "gitlens.diffWorkingWith",
"command": "gitlens.diffDirectory",
"when": "gitlens:enabled"
},
{
"command": "gitlens.diffDirectoryWithHead",
"when": "gitlens:enabled"
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export * from './commands/browseRepoAtRevision';
export * from './commands/closeUnchangedFiles';
export * from './commands/closeView';
export * from './commands/common';
export * from './commands/compareWith';
export * from './commands/copyMessageToClipboard';
export * from './commands/copyShaToClipboard';
export * from './commands/diffBranchWith';
export * from './commands/openDirectoryCompare';
export * from './commands/diffLineWithPrevious';
export * from './commands/diffLineWithWorking';
Expand Down
7 changes: 5 additions & 2 deletions src/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export enum Commands {
CloseUnchangedFiles = 'gitlens.closeUnchangedFiles',
CloseUpdatesView = 'gitlens.closeUpdatesView',
CloseWelcomeView = 'gitlens.closeWelcomeView',
CompareWith = 'gitlens.compareWith',
CompareHeadWith = 'gitlens.compareHeadWith',
CompareWorkingWith = 'gitlens.compareWorkingWith',
ComputingFileAnnotations = 'gitlens.computingFileAnnotations',
ConnectRemoteProvider = 'gitlens.connectRemoteProvider',
CopyMessageToClipboard = 'gitlens.copyMessageToClipboard',
Expand All @@ -45,8 +48,6 @@ export enum Commands {
CopyShaToClipboard = 'gitlens.copyShaToClipboard',
DiffDirectory = 'gitlens.diffDirectory',
DiffDirectoryWithHead = 'gitlens.diffDirectoryWithHead',
DiffHeadWith = 'gitlens.diffHeadWith',
DiffWorkingWith = 'gitlens.diffWorkingWith',
DiffWith = 'gitlens.diffWith',
DiffWithNext = 'gitlens.diffWithNext',
DiffWithNextInDiffLeft = 'gitlens.diffWithNextInDiffLeft',
Expand Down Expand Up @@ -151,6 +152,8 @@ export enum Commands {
ViewsOpenDirectoryDiff = 'gitlens.views.openDirectoryDiff',
ViewsOpenDirectoryDiffWithWorking = 'gitlens.views.openDirectoryDiffWithWorking',

Deprecated_DiffHeadWith = 'gitlens.diffHeadWith',
Deprecated_DiffWorkingWith = 'gitlens.diffWorkingWith',
Deprecated_OpenBranchesInRemote = 'gitlens.openBranchesInRemote',
Deprecated_OpenBranchInRemote = 'gitlens.openBranchInRemote',
Deprecated_OpenCommitInRemote = 'gitlens.openCommitInRemote',
Expand Down
88 changes: 88 additions & 0 deletions src/commands/compareWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict';
import { TextEditor, Uri } from 'vscode';
import {
ActiveEditorCommand,
command,
CommandContext,
Commands,
getCommandUri,
getRepoPathOrActiveOrPrompt,
} from './common';
import { Container } from '../container';
import { Logger } from '../logger';
import { Messages } from '../messages';

export interface CompareWithCommandArgs {
ref1?: string;
ref2?: string;
}

@command()
export class CompareWithCommand extends ActiveEditorCommand {
constructor() {
super([
Commands.CompareWith,
Commands.CompareHeadWith,
Commands.CompareWorkingWith,
Commands.Deprecated_DiffHeadWith,
Commands.Deprecated_DiffWorkingWith,
]);
}

protected preExecute(context: CommandContext, args?: CompareWithCommandArgs) {
switch (context.command) {
case Commands.CompareWith:
args = { ...args };
break;

case Commands.CompareHeadWith:
case Commands.Deprecated_DiffHeadWith:
args = { ...args };
args.ref1 = 'HEAD';
break;

case Commands.CompareWorkingWith:
case Commands.Deprecated_DiffWorkingWith:
args = { ...args };
args.ref1 = '';
break;
}

return this.execute(context.editor, context.uri, args);
}

async execute(editor?: TextEditor, uri?: Uri, args?: CompareWithCommandArgs) {
uri = getCommandUri(uri, editor);
args = { ...args };

try {
let title;
switch (args.ref1) {
case null:
title = 'Compare';
break;
case '':
title = 'Compare Working Tree with';
break;
case 'HEAD':
title = 'Compare HEAD with';
break;
default:
title = `Compare ${args.ref1} with`;
break;
}

const repoPath = await getRepoPathOrActiveOrPrompt(uri, editor, title);
if (!repoPath) return;

if (args.ref1 != null && args.ref2 != null) {
void (await Container.searchAndCompareView.compare(repoPath, args.ref1, args.ref2));
} else {
Container.searchAndCompareView.selectForCompare(repoPath, args.ref1, { prompt: true });
}
} catch (ex) {
Logger.error(ex, 'CompareWithCommmand');
void Messages.showGenericErrorMessage('Unable to open comparison');
}
}
}
92 changes: 0 additions & 92 deletions src/commands/diffBranchWith.ts

This file was deleted.

12 changes: 6 additions & 6 deletions src/views/searchAndCompareView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ export class SearchAndCompareViewNode extends ViewNode<SearchAndCompareView> {
void (await this.view.compare(repoPath, selectedRef.ref, ref));
}

async selectForCompare(repoPath?: string, ref?: string | NamedRef) {
async selectForCompare(repoPath?: string, ref?: string | NamedRef, options?: { prompt?: boolean }) {
if (repoPath == null) {
repoPath = await getRepoPathOrPrompt('Compare');
}
if (repoPath == null) return;

this.removeComparePicker(true);

let autoCompare = false;
let prompt = options?.prompt ?? false;
if (ref == null) {
const pick = await ReferencePicker.show(repoPath, 'Compare', 'Choose a reference to compare', {
allowEnteringRefs: true,
Expand All @@ -210,7 +210,7 @@ export class SearchAndCompareViewNode extends ViewNode<SearchAndCompareView> {

ref = pick.ref;

autoCompare = true;
prompt = true;
}

this.comparePicker = new ComparePickerNode(this.view, this, {
Expand All @@ -225,7 +225,7 @@ export class SearchAndCompareViewNode extends ViewNode<SearchAndCompareView> {

await this.view.reveal(this.comparePicker, { focus: false, select: true });

if (autoCompare) {
if (prompt) {
await this.compareWithSelected();
}
}
Expand Down Expand Up @@ -359,8 +359,8 @@ export class SearchAndCompareView extends ViewBase<SearchAndCompareViewNode, Sea
void this.ensureRoot().compareWithSelected(repoPath, ref);
}

selectForCompare(repoPath?: string, ref?: string | NamedRef) {
void this.ensureRoot().selectForCompare(repoPath, ref);
selectForCompare(repoPath?: string, ref?: string | NamedRef, options?: { prompt?: boolean }) {
void this.ensureRoot().selectForCompare(repoPath, ref, options);
}

async search(
Expand Down

0 comments on commit f29bb1a

Please sign in to comment.