From 98c7aeac6f48edbce26a4918002643c1216a1274 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 1 Sep 2020 01:58:01 -0400 Subject: [PATCH] Adds author toggle to commits view --- package.json | 30 ++++++++++++++++++++++++++++++ src/constants.ts | 1 + src/views/commitsView.ts | 36 +++++++++++++++++++++++++++++++++++- src/views/nodes/branchNode.ts | 3 +++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 459e61b..3a24c19 100644 --- a/package.json +++ b/package.json @@ -3411,6 +3411,18 @@ "icon": "$(list-flat)" }, { + "command": "gitlens.views.commits.setMyCommitsOnlyOn", + "title": "Toggle Authors (Everyone)", + "category": "GitLens", + "icon": "$(organization)" + }, + { + "command": "gitlens.views.commits.setMyCommitsOnlyOff", + "title": "Toggle Authors (You)", + "category": "GitLens", + "icon": "$(person)" + }, + { "command": "gitlens.views.commits.setShowAvatarsOn", "title": "Show Avatars", "category": "GitLens" @@ -4630,6 +4642,14 @@ "when": "false" }, { + "command": "gitlens.views.commits.setMyCommitsOnlyOn", + "when": "false" + }, + { + "command": "gitlens.views.commits.setMyCommitsOnlyOff", + "when": "false" + }, + { "command": "gitlens.views.commits.setShowAvatarsOn", "when": "false" }, @@ -5670,6 +5690,16 @@ "group": "navigation@13" }, { + "command": "gitlens.views.commits.setMyCommitsOnlyOn", + "when": "view =~ /^gitlens\\.views\\.commits/ && !gitlens:views:commits:myCommitsOnly", + "group": "navigation@14" + }, + { + "command": "gitlens.views.commits.setMyCommitsOnlyOff", + "when": "view =~ /^gitlens\\.views\\.commits/ && gitlens:views:commits:myCommitsOnly", + "group": "navigation@14" + }, + { "command": "gitlens.views.commits.refresh", "when": "view =~ /^gitlens\\.views\\.commits/", "group": "navigation@99" diff --git a/src/constants.ts b/src/constants.ts index c373e89..cce601b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -40,6 +40,7 @@ export enum CommandContext { Readonly = 'gitlens:readonly', ViewsCanCompare = 'gitlens:views:canCompare', ViewsCanCompareFile = 'gitlens:views:canCompare:file', + ViewsCommitsMyCommitsOnly = 'gitlens:views:commits:myCommitsOnly', ViewsCompareKeepResults = 'gitlens:views:compare:keepResults', ViewsFileHistoryCursorFollowing = 'gitlens:views:fileHistory:cursorFollowing', ViewsFileHistoryEditorFollowing = 'gitlens:views:fileHistory:editorFollowing', diff --git a/src/views/commitsView.ts b/src/views/commitsView.ts index ab25e38..729896a 100644 --- a/src/views/commitsView.ts +++ b/src/views/commitsView.ts @@ -9,7 +9,7 @@ import { window, } from 'vscode'; import { CommitsViewConfig, configuration, ViewFilesLayout } from '../configuration'; -import { GlyphChars } from '../constants'; +import { CommandContext, GlyphChars, setCommandContext } from '../constants'; import { Container } from '../container'; import { GitLogCommit, @@ -55,11 +55,20 @@ export class CommitsRepositoryNode extends SubscribeableViewNode { const branch = await this.repo.getBranch(); if (branch == null) return [new MessageNode(this.view, this, 'No commits could be found.')]; + let authors; + if (this.view.state.myCommitsOnly) { + const user = await Container.git.getCurrentUser(this.repo.path); + if (user != null) { + authors = [`^${user.name} <${user.email}>$`]; + } + } + this.children = [ new BranchNode(this.uri, this.view, this, branch, true, { expanded: true, showCurrent: false, showTracking: true, + authors: authors, }), ]; @@ -205,6 +214,10 @@ export class CommitsViewNode extends ViewNode { } } +interface CommitsViewState { + myCommitsOnly?: boolean; +} + export class CommitsView extends ViewBase { protected readonly configKey = 'commits'; @@ -212,6 +225,11 @@ export class CommitsView extends ViewBase { super('gitlens.views.commits', 'Commits'); } + private readonly _state: CommitsViewState = {}; + get state(): CommitsViewState { + return this._state; + } + getRoot() { return new CommitsViewNode(this); } @@ -240,6 +258,16 @@ export class CommitsView extends ViewBase { () => this.setFilesLayout(ViewFilesLayout.Tree), this, ); + commands.registerCommand( + this.getQualifiedCommand('setMyCommitsOnlyOn'), + () => this.setMyCommitsOnly(true), + this, + ); + commands.registerCommand( + this.getQualifiedCommand('setMyCommitsOnlyOff'), + () => this.setMyCommitsOnly(false), + this, + ); commands.registerCommand(this.getQualifiedCommand('setShowAvatarsOn'), () => this.setShowAvatars(true), this); commands.registerCommand(this.getQualifiedCommand('setShowAvatarsOff'), () => this.setShowAvatars(false), this); } @@ -338,4 +366,10 @@ export class CommitsView extends ViewBase { private setShowAvatars(enabled: boolean) { return configuration.updateEffective('views', this.configKey, 'avatars', enabled); } + + private setMyCommitsOnly(enabled: boolean) { + void setCommandContext(CommandContext.ViewsCommitsMyCommitsOnly, enabled); + this.state.myCommitsOnly = enabled; + void this.refresh(true); + } } diff --git a/src/views/nodes/branchNode.ts b/src/views/nodes/branchNode.ts index 2857282..c14c4c6 100644 --- a/src/views/nodes/branchNode.ts +++ b/src/views/nodes/branchNode.ts @@ -30,6 +30,7 @@ export class BranchNode expanded: boolean; showCurrent: boolean; showTracking: boolean; + authors?: string[]; }; protected splatted = true; @@ -45,6 +46,7 @@ export class BranchNode expanded?: boolean; showCurrent?: boolean; showTracking?: boolean; + authors?: string[]; }, ) { super(uri, view, parent); @@ -271,6 +273,7 @@ export class BranchNode this._log = await Container.git.getLog(this.uri.repoPath!, { limit: this.limit ?? this.view.config.defaultItemLimit, ref: this.ref.ref, + authors: this.options?.authors, }); }