Skip to content

Commit f29bb1a

Browse files
committed
Adds compare references command to the palette
1 parent 281249c commit f29bb1a

File tree

8 files changed

+133
-119
lines changed

8 files changed

+133
-119
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
88

99
### Added
1010

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

1314
### Fixed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,9 @@ Additionally, these integrations provide commands to copy the url of or open, fi
563563

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

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

569570
- Adds an _Open Changes (difftool)_ command (`gitlens.externalDiff`) to open the changes of a file or set of files with the configured git difftool
570571
- Adds an _Open All Changes (difftool)_ command (`gitlens.externalDiffAll`) to open all working changes with the configured git difftool

package.json

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@
8686
"onCommand:gitlens.showWelcomeView",
8787
"onCommand:gitlens.closeUpdatesView",
8888
"onCommand:gitlens.closeWelcomeView",
89+
"onCommand:gitlens.compareWith",
90+
"onCommand:gitlens.compareHeadWith",
91+
"onCommand:gitlens.compareWorkingWith",
8992
"onCommand:gitlens.diffDirectory",
9093
"onCommand:gitlens.diffDirectoryWithHead",
91-
"onCommand:gitlens.diffHeadWith",
92-
"onCommand:gitlens.diffWorkingWith",
9394
"onCommand:gitlens.diffWithNext",
9495
"onCommand:gitlens.diffWithNextInDiffLeft",
9596
"onCommand:gitlens.diffWithNextInDiffRight",
@@ -2622,23 +2623,31 @@
26222623
"icon": "$(close)"
26232624
},
26242625
{
2625-
"command": "gitlens.diffDirectory",
2626-
"title": "Open Directory Compare (difftool) with...",
2627-
"category": "GitLens"
2626+
"command": "gitlens.compareWith",
2627+
"title": "Compare References...",
2628+
"category": "GitLens",
2629+
"icon": "$(compare-changes)"
26282630
},
26292631
{
2630-
"command": "gitlens.diffDirectoryWithHead",
2631-
"title": "Open Directory Compare (difftool)",
2632-
"category": "GitLens"
2632+
"command": "gitlens.compareHeadWith",
2633+
"title": "Compare HEAD with...",
2634+
"category": "GitLens",
2635+
"icon": "$(compare-changes)"
26332636
},
26342637
{
2635-
"command": "gitlens.diffHeadWith",
2636-
"title": "Compare HEAD with...",
2638+
"command": "gitlens.compareWorkingWith",
2639+
"title": "Compare Working Tree with...",
2640+
"category": "GitLens",
2641+
"icon": "$(compare-changes)"
2642+
},
2643+
{
2644+
"command": "gitlens.diffDirectory",
2645+
"title": "Open Directory Compare (difftool) with...",
26372646
"category": "GitLens"
26382647
},
26392648
{
2640-
"command": "gitlens.diffWorkingWith",
2641-
"title": "Compare Working Tree with...",
2649+
"command": "gitlens.diffDirectoryWithHead",
2650+
"title": "Open Directory Compare (difftool)",
26422651
"category": "GitLens"
26432652
},
26442653
{
@@ -4446,19 +4455,23 @@
44464455
"when": "false"
44474456
},
44484457
{
4449-
"command": "gitlens.diffDirectory",
4458+
"command": "gitlens.compareWith",
44504459
"when": "gitlens:enabled"
44514460
},
44524461
{
4453-
"command": "gitlens.diffDirectoryWithHead",
4462+
"command": "gitlens.compareHeadWith",
44544463
"when": "gitlens:enabled"
44554464
},
44564465
{
4457-
"command": "gitlens.diffHeadWith",
4466+
"command": "gitlens.compareWorkingWith",
44584467
"when": "gitlens:enabled"
44594468
},
44604469
{
4461-
"command": "gitlens.diffWorkingWith",
4470+
"command": "gitlens.diffDirectory",
4471+
"when": "gitlens:enabled"
4472+
},
4473+
{
4474+
"command": "gitlens.diffDirectoryWithHead",
44624475
"when": "gitlens:enabled"
44634476
},
44644477
{

src/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export * from './commands/browseRepoAtRevision';
55
export * from './commands/closeUnchangedFiles';
66
export * from './commands/closeView';
77
export * from './commands/common';
8+
export * from './commands/compareWith';
89
export * from './commands/copyMessageToClipboard';
910
export * from './commands/copyShaToClipboard';
10-
export * from './commands/diffBranchWith';
1111
export * from './commands/openDirectoryCompare';
1212
export * from './commands/diffLineWithPrevious';
1313
export * from './commands/diffLineWithWorking';

src/commands/common.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export enum Commands {
3232
CloseUnchangedFiles = 'gitlens.closeUnchangedFiles',
3333
CloseUpdatesView = 'gitlens.closeUpdatesView',
3434
CloseWelcomeView = 'gitlens.closeWelcomeView',
35+
CompareWith = 'gitlens.compareWith',
36+
CompareHeadWith = 'gitlens.compareHeadWith',
37+
CompareWorkingWith = 'gitlens.compareWorkingWith',
3538
ComputingFileAnnotations = 'gitlens.computingFileAnnotations',
3639
ConnectRemoteProvider = 'gitlens.connectRemoteProvider',
3740
CopyMessageToClipboard = 'gitlens.copyMessageToClipboard',
@@ -45,8 +48,6 @@ export enum Commands {
4548
CopyShaToClipboard = 'gitlens.copyShaToClipboard',
4649
DiffDirectory = 'gitlens.diffDirectory',
4750
DiffDirectoryWithHead = 'gitlens.diffDirectoryWithHead',
48-
DiffHeadWith = 'gitlens.diffHeadWith',
49-
DiffWorkingWith = 'gitlens.diffWorkingWith',
5051
DiffWith = 'gitlens.diffWith',
5152
DiffWithNext = 'gitlens.diffWithNext',
5253
DiffWithNextInDiffLeft = 'gitlens.diffWithNextInDiffLeft',
@@ -151,6 +152,8 @@ export enum Commands {
151152
ViewsOpenDirectoryDiff = 'gitlens.views.openDirectoryDiff',
152153
ViewsOpenDirectoryDiffWithWorking = 'gitlens.views.openDirectoryDiffWithWorking',
153154

155+
Deprecated_DiffHeadWith = 'gitlens.diffHeadWith',
156+
Deprecated_DiffWorkingWith = 'gitlens.diffWorkingWith',
154157
Deprecated_OpenBranchesInRemote = 'gitlens.openBranchesInRemote',
155158
Deprecated_OpenBranchInRemote = 'gitlens.openBranchInRemote',
156159
Deprecated_OpenCommitInRemote = 'gitlens.openCommitInRemote',

src/commands/compareWith.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
import { TextEditor, Uri } from 'vscode';
3+
import {
4+
ActiveEditorCommand,
5+
command,
6+
CommandContext,
7+
Commands,
8+
getCommandUri,
9+
getRepoPathOrActiveOrPrompt,
10+
} from './common';
11+
import { Container } from '../container';
12+
import { Logger } from '../logger';
13+
import { Messages } from '../messages';
14+
15+
export interface CompareWithCommandArgs {
16+
ref1?: string;
17+
ref2?: string;
18+
}
19+
20+
@command()
21+
export class CompareWithCommand extends ActiveEditorCommand {
22+
constructor() {
23+
super([
24+
Commands.CompareWith,
25+
Commands.CompareHeadWith,
26+
Commands.CompareWorkingWith,
27+
Commands.Deprecated_DiffHeadWith,
28+
Commands.Deprecated_DiffWorkingWith,
29+
]);
30+
}
31+
32+
protected preExecute(context: CommandContext, args?: CompareWithCommandArgs) {
33+
switch (context.command) {
34+
case Commands.CompareWith:
35+
args = { ...args };
36+
break;
37+
38+
case Commands.CompareHeadWith:
39+
case Commands.Deprecated_DiffHeadWith:
40+
args = { ...args };
41+
args.ref1 = 'HEAD';
42+
break;
43+
44+
case Commands.CompareWorkingWith:
45+
case Commands.Deprecated_DiffWorkingWith:
46+
args = { ...args };
47+
args.ref1 = '';
48+
break;
49+
}
50+
51+
return this.execute(context.editor, context.uri, args);
52+
}
53+
54+
async execute(editor?: TextEditor, uri?: Uri, args?: CompareWithCommandArgs) {
55+
uri = getCommandUri(uri, editor);
56+
args = { ...args };
57+
58+
try {
59+
let title;
60+
switch (args.ref1) {
61+
case null:
62+
title = 'Compare';
63+
break;
64+
case '':
65+
title = 'Compare Working Tree with';
66+
break;
67+
case 'HEAD':
68+
title = 'Compare HEAD with';
69+
break;
70+
default:
71+
title = `Compare ${args.ref1} with`;
72+
break;
73+
}
74+
75+
const repoPath = await getRepoPathOrActiveOrPrompt(uri, editor, title);
76+
if (!repoPath) return;
77+
78+
if (args.ref1 != null && args.ref2 != null) {
79+
void (await Container.searchAndCompareView.compare(repoPath, args.ref1, args.ref2));
80+
} else {
81+
Container.searchAndCompareView.selectForCompare(repoPath, args.ref1, { prompt: true });
82+
}
83+
} catch (ex) {
84+
Logger.error(ex, 'CompareWithCommmand');
85+
void Messages.showGenericErrorMessage('Unable to open comparison');
86+
}
87+
}
88+
}

src/commands/diffBranchWith.ts

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/views/searchAndCompareView.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ export class SearchAndCompareViewNode extends ViewNode<SearchAndCompareView> {
180180
void (await this.view.compare(repoPath, selectedRef.ref, ref));
181181
}
182182

183-
async selectForCompare(repoPath?: string, ref?: string | NamedRef) {
183+
async selectForCompare(repoPath?: string, ref?: string | NamedRef, options?: { prompt?: boolean }) {
184184
if (repoPath == null) {
185185
repoPath = await getRepoPathOrPrompt('Compare');
186186
}
187187
if (repoPath == null) return;
188188

189189
this.removeComparePicker(true);
190190

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

211211
ref = pick.ref;
212212

213-
autoCompare = true;
213+
prompt = true;
214214
}
215215

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

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

228-
if (autoCompare) {
228+
if (prompt) {
229229
await this.compareWithSelected();
230230
}
231231
}
@@ -359,8 +359,8 @@ export class SearchAndCompareView extends ViewBase<SearchAndCompareViewNode, Sea
359359
void this.ensureRoot().compareWithSelected(repoPath, ref);
360360
}
361361

362-
selectForCompare(repoPath?: string, ref?: string | NamedRef) {
363-
void this.ensureRoot().selectForCompare(repoPath, ref);
362+
selectForCompare(repoPath?: string, ref?: string | NamedRef, options?: { prompt?: boolean }) {
363+
void this.ensureRoot().selectForCompare(repoPath, ref, options);
364364
}
365365

366366
async search(

0 commit comments

Comments
 (0)