From 825e3d937a5693c8c96b920c63bc1034cea87237 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 8 Jul 2019 22:53:15 -0400 Subject: [PATCH] Adds working tree compare to branch compare node --- README.md | 2 +- package.json | 63 ++++++++++++++++++++++++++++++++--- src/config.ts | 7 +++- src/extension.ts | 10 ++++-- src/views/nodes/compareBranchNode.ts | 39 +++++++++++++++++----- src/views/nodes/repositoryNode.ts | 2 +- src/views/repositoriesView.ts | 19 +++++++++++ src/webviews/apps/settings/index.html | 17 ++++++++-- 8 files changed, 138 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 082e781..53e1b15 100644 --- a/README.md +++ b/README.md @@ -780,7 +780,7 @@ See also [View Settings](#view-settings- 'Jump to the View settings') | `gitlens.views.repositories.files.threshold` | Specifies when to switch between displaying files as a `tree` or `list` based on the number of files in a nesting level in the _Repositories_ view. Only applies when `gitlens.views.repositories.files.layout` is set to `auto` | | `gitlens.views.repositories.includeWorkingTree` | Specifies whether to include working tree file status for each repository in the _Repositories_ view | | `gitlens.views.repositories.location` | Specifies where to show the _Repositories_ view

`gitlens` - adds to the GitLens side bar
`explorer` - adds to the Explorer side bar
`scm` - adds to the Source Control side bar | -| `gitlens.views.repositories.showBranchComparison` | Specifies whether to show a comparison of the current branch to a user-selected reference in the _Repositories_ view | +| `gitlens.views.repositories.showBranchComparison` | Specifies whether to show a comparison of a user-selected reference (branch, tag. etc) to the current branch or the working tree in the _Repositories_ view | | `gitlens.views.repositories.showTrackingBranch` | Specifies whether to show the tracking branch when displaying local branches in the _Repositories_ view | ### File History View Settings [#](#file-history-view-settings- 'File History View Settings') diff --git a/package.json b/package.json index 4aa29e0..6c0f4e9 100644 --- a/package.json +++ b/package.json @@ -1520,9 +1520,26 @@ "scope": "window" }, "gitlens.views.repositories.showBranchComparison": { - "type": "boolean", - "default": true, - "markdownDescription": "Specifies whether to show a comparison of the current branch to a user-selected reference in the _Repositories_ view", + "anyOf": [ + { + "enum": [ + false + ] + }, + { + "type": "string", + "default": "working", + "enum": [ + "branch", + "working" + ], + "enumDescriptions": [ + "Compares the current branch to the user-selected reference", + "Compares the working tree to the user-selected reference" + ] + } + ], + "markdownDescription": "Specifies whether to show a comparison of a user-selected reference (branch, tag. etc) to the current branch or the working tree in the _Repositories_ view", "scope": "window" }, "gitlens.views.repositories.showTrackingBranch": { @@ -2692,6 +2709,24 @@ } }, { + "command": "gitlens.views.repositories.setBranchComparisonToWorking", + "title": "Switch to Working Tree Comparison", + "category": "GitLens", + "icon": { + "dark": "images/dark/icon-compare-ref-working.svg", + "light": "images/light/icon-compare-ref-working.svg" + } + }, + { + "command": "gitlens.views.repositories.setBranchComparisonToBranch", + "title": "Switch to Branch Comparison", + "category": "GitLens", + "icon": { + "dark": "images/dark/icon-compare-refs.svg", + "light": "images/light/icon-compare-refs.svg" + } + }, + { "command": "gitlens.views.repositories.setFilesLayoutToAuto", "title": "Automatic Layout", "category": "GitLens" @@ -4712,6 +4747,26 @@ "group": "inline@99" }, { + "command": "gitlens.views.repositories.setBranchComparisonToWorking", + "when": "config.gitlens.views.repositories.showBranchComparison == branch && viewItem =~ /gitlens:compare:branch\\b/", + "group": "inline@1" + }, + { + "command": "gitlens.views.repositories.setBranchComparisonToBranch", + "when": "config.gitlens.views.repositories.showBranchComparison == working && viewItem =~ /gitlens:compare:branch\\b/", + "group": "inline@1" + }, + { + "command": "gitlens.views.repositories.setBranchComparisonToWorking", + "when": "config.gitlens.views.repositories.showBranchComparison == branch && viewItem =~ /gitlens:compare:branch\\b/", + "group": "1_gitlens@1" + }, + { + "command": "gitlens.views.repositories.setBranchComparisonToBranch", + "when": "config.gitlens.views.repositories.showBranchComparison == working && viewItem =~ /gitlens:compare:branch\\b/", + "group": "1_gitlens@1" + }, + { "command": "gitlens.views.compare.pinComparison", "when": "viewItem =~ /gitlens:compare:results\\b(?!.*?\\+pinned\\b.*?)/", "group": "inline@1" @@ -4728,7 +4783,7 @@ }, { "command": "gitlens.views.refreshNode", - "when": "viewItem =~ /gitlens:compare:results\\b/", + "when": "viewItem =~ /gitlens:compare:(branch|results)\\b/", "group": "inline@3" }, { diff --git a/src/config.ts b/src/config.ts index 0b81c5e..b45c403 100644 --- a/src/config.ts +++ b/src/config.ts @@ -193,6 +193,11 @@ export enum ViewLocation { SourceControl = 'scm' } +export enum ViewShowBranchComparison { + Branch = 'branch', + Working = 'working' +} + export interface AdvancedConfig { abbreviatedShaLength: number; blame: { @@ -364,7 +369,7 @@ export interface RepositoriesViewConfig { files: ViewsFilesConfig; includeWorkingTree: boolean; location: ViewLocation; - showBranchComparison: boolean; + showBranchComparison: false | ViewShowBranchComparison; showTrackingBranch: boolean; } diff --git a/src/extension.ts b/src/extension.ts index d5896ff..3a7ea4a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,7 +1,7 @@ 'use strict'; import { commands, ExtensionContext, extensions, window, workspace } from 'vscode'; import { Commands, registerCommands } from './commands'; -import { ModeConfig } from './config'; +import { ModeConfig, ViewShowBranchComparison } from './config'; import { Config, configuration, Configuration } from './configuration'; import { CommandContext, extensionQualifiedId, GlobalState, GlyphChars, setCommandContext } from './constants'; import { Container } from './container'; @@ -105,7 +105,13 @@ async function migrateSettings(context: ExtensionContext, previousVersion: strin const previous = Versions.fromString(previousVersion); try { - if (Versions.compare(previous, Versions.from(9, 6, 3)) !== 1) { + if (Versions.compare(previous, Versions.from(9, 8, 2)) !== 1) { + const name = configuration.name('views')('repositories')('showBranchComparison').value; + await configuration.migrate(name, name, { + migrationFn: (v: boolean) => (v === false ? false : ViewShowBranchComparison.Working) + }); + } + else if (Versions.compare(previous, Versions.from(9, 6, 3)) !== 1) { const formatMigrationFn = (v: string) => { if (v == null || v.length === 0) return v; diff --git a/src/views/nodes/compareBranchNode.ts b/src/views/nodes/compareBranchNode.ts index d6cf9da..19bf4c5 100644 --- a/src/views/nodes/compareBranchNode.ts +++ b/src/views/nodes/compareBranchNode.ts @@ -9,6 +9,7 @@ import { CommitsQueryResults, ResultsCommitsNode } from './resultsCommitsNode'; import { Container } from '../../container'; import { Strings } from '../../system'; import { ResultsFilesNode } from './resultsFilesNode'; +import { ViewShowBranchComparison } from '../../config'; export class CompareBranchNode extends ViewNode { private _children: ViewNode[] | undefined; @@ -42,7 +43,13 @@ export class CompareBranchNode extends ViewNode { querying: true } ), - new ResultsFilesNode(this.view, this, this.uri.repoPath!, this._compareWith, this.branch.ref) + new ResultsFilesNode( + this.view, + this, + this.uri.repoPath!, + this._compareWith, + this.compareWithWorkingTree ? '' : this.branch.ref + ) ]; } return this._children; @@ -53,11 +60,13 @@ export class CompareBranchNode extends ViewNode { let label; let description; if (this._compareWith === undefined) { - label = `Compare ${this.branch.name} with `; + label = `Compare ${this.branch.name}${ + this.compareWithWorkingTree ? ' (working)' : '' + } with `; state = TreeItemCollapsibleState.None; } else { - label = `${this.branch.name}`; + label = `${this.branch.name}${this.compareWithWorkingTree ? ' (working)' : ''}`; description = `${GlyphChars.ArrowLeftRightLong}${GlyphChars.Space} ${GitService.shortenSha( this._compareWith, { @@ -69,25 +78,37 @@ export class CompareBranchNode extends ViewNode { const item = new TreeItem(label, state); item.command = { - title: `Compare ${this.branch.name} with${GlyphChars.Ellipsis}`, + title: `Compare ${this.branch.name}${this.compareWithWorkingTree ? ' (working)' : ''} with${ + GlyphChars.Ellipsis + }`, command: 'gitlens.views.executeNodeCallback', arguments: [() => this.compareWith()] }; item.contextValue = ResourceType.CompareBranch; item.description = description; item.iconPath = { - dark: Container.context.asAbsolutePath('images/dark/icon-compare-refs.svg'), - light: Container.context.asAbsolutePath('images/light/icon-compare-refs.svg') + dark: Container.context.asAbsolutePath( + `images/dark/icon-compare-${this.compareWithWorkingTree ? 'ref-working' : 'refs'}.svg` + ), + light: Container.context.asAbsolutePath( + `images/light/icon-compare-${this.compareWithWorkingTree ? 'ref-working' : 'refs'}.svg` + ) }; item.id = this.id; - item.tooltip = `Click to compare ${this.branch.name} with${GlyphChars.Ellipsis}`; + item.tooltip = `Click to compare ${this.branch.name}${this.compareWithWorkingTree ? ' (working)' : ''} with${ + GlyphChars.Ellipsis + }`; return item; } + get compareWithWorkingTree() { + return this.view.config.showBranchComparison === ViewShowBranchComparison.Working; + } + async compareWith() { const pick = await new ReferencesQuickPick(this.branch.repoPath).show( - `Compare ${this.branch.name} with${GlyphChars.Ellipsis}`, + `Compare ${this.branch.name}${this.compareWithWorkingTree ? ' (working)' : ''} with${GlyphChars.Ellipsis}`, { allowEnteringRefs: true, checked: this.branch.ref, checkmarks: true } ); if (pick === undefined || pick instanceof CommandQuickPickItem) return; @@ -104,7 +125,7 @@ export class CompareBranchNode extends ViewNode { const log = await Container.git.getLog(this.uri.repoPath!, { maxCount: maxCount, - ref: `${this._compareWith || 'HEAD'}${notation}${this.branch.ref}` + ref: `${this._compareWith || 'HEAD'}${notation}${this.compareWithWorkingTree ? '' : this.branch.ref}` }); const count = log !== undefined ? log.count : 0; diff --git a/src/views/nodes/repositoryNode.ts b/src/views/nodes/repositoryNode.ts index 606de22..acbf779 100644 --- a/src/views/nodes/repositoryNode.ts +++ b/src/views/nodes/repositoryNode.ts @@ -75,7 +75,7 @@ export class RepositoryNode extends SubscribeableViewNode { children.push(new StatusFilesNode(this.view, this, status, range)); } - if (this.view.config.showBranchComparison) { + if (this.view.config.showBranchComparison !== false) { children.push(new CompareBranchNode(this.uri, this.view, this, branch)); } diff --git a/src/views/repositoriesView.ts b/src/views/repositoriesView.ts index b71591d..51c4ccb 100644 --- a/src/views/repositoriesView.ts +++ b/src/views/repositoriesView.ts @@ -5,6 +5,7 @@ import { CommandContext, setCommandContext, WorkspaceState } from '../constants' import { Container } from '../container'; import { RepositoriesNode } from './nodes'; import { ViewBase } from './viewBase'; +import { ViewShowBranchComparison } from '../config'; export class RepositoriesView extends ViewBase { constructor() { @@ -54,6 +55,17 @@ export class RepositoriesView extends ViewBase { () => this.setAutoRefresh(Container.config.views.repositories.autoRefresh, false), this ); + + commands.registerCommand( + this.getQualifiedCommand('setBranchComparisonToWorking'), + () => this.setBranchComparison(ViewShowBranchComparison.Working), + this + ); + commands.registerCommand( + this.getQualifiedCommand('setBranchComparisonToBranch'), + () => this.setBranchComparison(ViewShowBranchComparison.Branch), + this + ); } protected onConfigurationChanged(e: ConfigurationChangeEvent) { @@ -110,6 +122,13 @@ export class RepositoriesView extends ViewBase { this._onDidChangeAutoRefresh.fire(); } + private setBranchComparison(comparison: ViewShowBranchComparison) { + return configuration.updateEffective( + configuration.name('views')('repositories')('showBranchComparison').value, + comparison + ); + } + private setFilesLayout(layout: ViewFilesLayout) { return configuration.updateEffective( configuration.name('views')('repositories')('files')('layout').value, diff --git a/src/webviews/apps/settings/index.html b/src/webviews/apps/settings/index.html index d566844..7a74dd9 100644 --- a/src/webviews/apps/settings/index.html +++ b/src/webviews/apps/settings/index.html @@ -2441,7 +2441,7 @@