From dd3768fa330d945c56662e8b0d74e8622c0a998d Mon Sep 17 00:00:00 2001 From: Geoffrey Date: Thu, 8 Feb 2018 19:10:14 +0100 Subject: [PATCH] Add new command to diff HEAD with branch using command palette --- package.json | 9 +++++++ src/commands.ts | 2 ++ src/commands/common.ts | 1 + src/commands/diffHeadWithBranch.ts | 54 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 src/commands/diffHeadWithBranch.ts diff --git a/package.json b/package.json index 9cc4f5d..ab50226 100644 --- a/package.json +++ b/package.json @@ -1102,6 +1102,11 @@ "category": "GitLens" }, { + "command": "gitlens.diffHeadWithBranch", + "title": "Compare Index (HEAD) with Branch or Tag...", + "category": "GitLens" + }, + { "command": "gitlens.diffWith", "title": "Compare File Revisions", "category": "GitLens" @@ -1653,6 +1658,10 @@ "when": "gitlens:enabled" }, { + "command": "gitlens.diffHeadWithBranch", + "when": "gitlens:enabled" + }, + { "command": "gitlens.diffWith", "when": "false" }, diff --git a/src/commands.ts b/src/commands.ts index fb9fd59..30d8acf 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -9,6 +9,7 @@ export * from './commands/closeUnchangedFiles'; export * from './commands/copyMessageToClipboard'; export * from './commands/copyShaToClipboard'; export * from './commands/diffDirectory'; +export * from './commands/diffHeadWithBranch'; export * from './commands/diffLineWithPrevious'; export * from './commands/diffLineWithWorking'; export * from './commands/diffWith'; @@ -59,6 +60,7 @@ export function configureCommands(): void { Container.context.subscriptions.push(new Commands.CopyMessageToClipboardCommand()); Container.context.subscriptions.push(new Commands.CopyShaToClipboardCommand()); Container.context.subscriptions.push(new Commands.DiffDirectoryCommand()); + Container.context.subscriptions.push(new Commands.DiffHeadWithBranchCommand()); Container.context.subscriptions.push(new Commands.DiffLineWithPreviousCommand()); Container.context.subscriptions.push(new Commands.DiffLineWithWorkingCommand()); Container.context.subscriptions.push(new Commands.DiffWithCommand()); diff --git a/src/commands/common.ts b/src/commands/common.ts index 5a9ade1..0fd7f70 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -12,6 +12,7 @@ export enum Commands { CopyMessageToClipboard = 'gitlens.copyMessageToClipboard', CopyShaToClipboard = 'gitlens.copyShaToClipboard', DiffDirectory = 'gitlens.diffDirectory', + DiffHeadWithBranch = 'gitlens.diffHeadWithBranch', ExternalDiffAll = 'gitlens.externalDiffAll', DiffWith = 'gitlens.diffWith', DiffWithBranch = 'gitlens.diffWithBranch', diff --git a/src/commands/diffHeadWithBranch.ts b/src/commands/diffHeadWithBranch.ts new file mode 100644 index 0000000..79e49a0 --- /dev/null +++ b/src/commands/diffHeadWithBranch.ts @@ -0,0 +1,54 @@ +'use strict'; +import { CancellationTokenSource, TextEditor, Uri, window } from 'vscode'; +import { ActiveEditorCommand, Commands, getCommandUri } from './common'; +import { GlyphChars } from '../constants'; +import { Container } from '../container'; +import { Logger } from '../logger'; +import { Messages } from '../messages'; +import { BranchesAndTagsQuickPick, CommandQuickPickItem } from '../quickPicks'; + +export class DiffHeadWithBranchCommand extends ActiveEditorCommand { + + constructor() { + super([Commands.DiffHeadWithBranch]); + } + + async execute(editor?: TextEditor, uri?: Uri): Promise { + uri = getCommandUri(uri, editor); + + let progressCancellation: CancellationTokenSource | undefined; + + try { + const repoPath = await Container.git.getRepoPath(uri); + if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to open directory compare`); + + const placeHolder = `Compare Index (HEAD) to ${GlyphChars.Ellipsis}`; + + progressCancellation = BranchesAndTagsQuickPick.showProgress(placeHolder); + + const [branches, tags] = await Promise.all([ + Container.git.getBranches(repoPath), + Container.git.getTags(repoPath) + ]); + + if (progressCancellation.token.isCancellationRequested) return undefined; + + const pick = await BranchesAndTagsQuickPick.show(branches, tags, placeHolder, { progressCancellation: progressCancellation }); + if (pick === undefined) return undefined; + + if (pick instanceof CommandQuickPickItem) return pick.execute(); + + const compareWith = pick.name; + if (compareWith === undefined) return undefined; + + Container.resultsExplorer.showComparisonInResults(repoPath, compareWith, 'HEAD'); + + return undefined; + } catch (ex) { + Logger.error(ex, 'DiffHeadWithBranchCommand'); + return window.showErrorMessage(`Unable to open directory compare. See output channel for more details`); + } finally { + progressCancellation && progressCancellation.dispose(); + } + } +} \ No newline at end of file