Skip to content

Commit

Permalink
#341 Added a new extension setting "git-graph.repository.showRemoteBr…
Browse files Browse the repository at this point in the history
…anches", to set the default value of the "Show Remote Branches" repository setting.
  • Loading branch information
mhutchie committed Aug 9, 2020
1 parent 57f52cf commit ef01443
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 11 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,11 @@
"default": true,
"description": "Show commits that are only referenced by tags in Git Graph."
},
"git-graph.repository.showRemoteBranches": {
"type": "boolean",
"default": true,
"description": "Show Remote Branches in Git Graph by default. This can be overridden per repository from the Git Graph View's Control Bar."
},
"git-graph.repository.showTags": {
"type": "boolean",
"default": true,
Expand Down
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ class Config {
return !!this.getRenamedExtensionSetting('repository.showCommitsOnlyReferencedByTags', 'showCommitsOnlyReferencedByTags', true);
}

/**
* Get the value of the `git-graph.repository.showRemoteBranches` Extension Setting.
*/
get showRemoteBranches() {
return !!this.config.get('repository.showRemoteBranches', true);
}

/**
* Get the value of the `git-graph.repository.showTags` Extension Setting.
*/
Expand Down
20 changes: 16 additions & 4 deletions src/extensionState.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as fs from 'fs';
import * as vscode from 'vscode';
import { Avatar, AvatarCache } from './avatarManager';
import { getConfig } from './config';
import { Event } from './event';
import { CodeReview, ErrorInfo, FileViewType, GitGraphViewGlobalState, GitRepoSet, GitRepoState, IncludeCommitsMentionedByReflogs, OnlyFollowFirstParent, RepoCommitOrdering, ShowTags } from './types';
import { CodeReview, ErrorInfo, FileViewType, GitGraphViewGlobalState, GitRepoSet, GitRepoState, IncludeCommitsMentionedByReflogs, OnlyFollowFirstParent, RepoCommitOrdering, ShowRemoteBranches, ShowTags } from './types';
import { GitExecutable, getPathFromStr } from './utils';

const AVATAR_STORAGE_FOLDER = '/avatars';
Expand All @@ -25,6 +26,7 @@ export const DEFAULT_REPO_STATE: GitRepoState = {
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: true,
showRemoteBranchesV2: ShowRemoteBranches.Default,
showTags: ShowTags.Default,
hideRemotes: []
};
Expand Down Expand Up @@ -98,10 +100,20 @@ export class ExtensionState implements vscode.Disposable {
*/
public getRepos() {
const repoSet = this.workspaceState.get<GitRepoSet>(REPO_STATES, {});
Object.keys(repoSet).forEach(repo => {
repoSet[repo] = Object.assign({}, DEFAULT_REPO_STATE, repoSet[repo]);
const outputSet: GitRepoSet = {};
let showRemoteBranchesDefaultValue: boolean | null = null;
Object.keys(repoSet).forEach((repo) => {
outputSet[repo] = Object.assign({}, DEFAULT_REPO_STATE, repoSet[repo]);
if (typeof repoSet[repo].showRemoteBranchesV2 === 'undefined' && typeof repoSet[repo].showRemoteBranches !== 'undefined') {
if (showRemoteBranchesDefaultValue === null) {
showRemoteBranchesDefaultValue = getConfig().showRemoteBranches;
}
if (repoSet[repo].showRemoteBranches !== showRemoteBranchesDefaultValue) {
outputSet[repo].showRemoteBranchesV2 = repoSet[repo].showRemoteBranches ? ShowRemoteBranches.Show : ShowRemoteBranches.Hide;
}
}
});
return repoSet;
return outputSet;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/gitGraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ export class GitGraphView implements vscode.Disposable {
onRepoLoadShowCheckedOutBranch: config.onRepoLoadShowCheckedOutBranch,
referenceLabels: config.referenceLabels,
repoDropdownOrder: config.repoDropdownOrder,
showRemoteBranches: config.showRemoteBranches,
showTags: config.showTags
},
lastActiveRepo: this.extensionState.getLastActiveRepo(),
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export interface GitRepoState {
issueLinkingConfig: IssueLinkingConfig | null;
pullRequestConfig: PullRequestConfig | null;
showRemoteBranches: boolean;
showRemoteBranchesV2: ShowRemoteBranches;
showTags: ShowTags;
hideRemotes: string[];
}
Expand Down Expand Up @@ -228,6 +229,7 @@ export interface GitGraphViewConfig {
readonly onRepoLoadShowCheckedOutBranch: boolean;
readonly referenceLabels: ReferenceLabelsConfig;
readonly repoDropdownOrder: RepoDropdownOrder;
readonly showRemoteBranches: boolean;
readonly showTags: boolean;
}

Expand Down Expand Up @@ -463,6 +465,12 @@ export const enum RepoDropdownOrder {
Name
}

export const enum ShowRemoteBranches {
Default,
Show,
Hide
}

export const enum ShowTags {
Default,
Show,
Expand Down
62 changes: 62 additions & 0 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2898,6 +2898,68 @@ describe('Config', () => {
});
});

describe('showRemoteBranches', () => {
it('Should return TRUE when the configuration value is TRUE', () => {
// Setup
workspaceConfiguration.get.mockReturnValueOnce(true);

// Run
const value = config.showRemoteBranches;

// Assert
expect(workspaceConfiguration.get).toBeCalledWith('repository.showRemoteBranches', true);
expect(value).toBe(true);
});

it('Should return FALSE when the configuration value is FALSE', () => {
// Setup
workspaceConfiguration.get.mockReturnValueOnce(false);

// Run
const value = config.showRemoteBranches;

// Assert
expect(workspaceConfiguration.get).toBeCalledWith('repository.showRemoteBranches', true);
expect(value).toBe(false);
});

it('Should return TRUE when the configuration value is truthy', () => {
// Setup
workspaceConfiguration.get.mockReturnValueOnce(5);

// Run
const value = config.showRemoteBranches;

// Assert
expect(workspaceConfiguration.get).toBeCalledWith('repository.showRemoteBranches', true);
expect(value).toBe(true);
});

it('Should return FALSE when the configuration value is falsy', () => {
// Setup
workspaceConfiguration.get.mockReturnValueOnce(0);

// Run
const value = config.showRemoteBranches;

// Assert
expect(workspaceConfiguration.get).toBeCalledWith('repository.showRemoteBranches', true);
expect(value).toBe(false);
});

it('Should return the default value (TRUE) when the configuration value is not set', () => {
// Setup
workspaceConfiguration.get.mockImplementationOnce((_, defaultValue) => defaultValue);

// Run
const value = config.showRemoteBranches;

// Assert
expect(workspaceConfiguration.get).toBeCalledWith('repository.showRemoteBranches', true);
expect(value).toBe(true);
});
});

describe('showTags', () => {
it('Should return TRUE when the configuration value is TRUE', () => {
// Setup
Expand Down
184 changes: 183 additions & 1 deletion tests/extensionState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ jest.mock('fs');
import * as fs from 'fs';
import { EventEmitter } from '../src/event';
import { ExtensionState } from '../src/extensionState';
import { FileViewType, GitGraphViewGlobalState, IncludeCommitsMentionedByReflogs, OnlyFollowFirstParent, RepoCommitOrdering, ShowTags } from '../src/types';
import { FileViewType, GitGraphViewGlobalState, IncludeCommitsMentionedByReflogs, OnlyFollowFirstParent, RepoCommitOrdering, ShowRemoteBranches, ShowTags } from '../src/types';
import { GitExecutable } from '../src/utils';

let extensionContext = vscode.mocks.extensionContext;
let workspaceConfiguration = vscode.mocks.workspaceConfiguration;
let onDidChangeGitExecutable: EventEmitter<GitExecutable>;

beforeAll(() => {
Expand Down Expand Up @@ -70,6 +71,7 @@ describe('ExtensionState', () => {
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: true,
showRemoteBranchesV2: ShowRemoteBranches.Show,
showTags: ShowTags.Show,
hideRemotes: []
};
Expand Down Expand Up @@ -111,12 +113,192 @@ describe('ExtensionState', () => {
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: true,
showRemoteBranchesV2: ShowRemoteBranches.Default,
showTags: ShowTags.Default,
hideRemotes: []
}
});
});

it('Should migrate showRemoteBranches = TRUE from boolean to enum (repository.showRemoteBranches = TRUE)', () => {
// Setup
extensionContext.workspaceState.get.mockReturnValueOnce({
'/path/to/repo': {
showRemoteBranches: true
}
});
workspaceConfiguration.get.mockReturnValueOnce(true); // repository.showRemoteBranches

// Run
const result = extensionState.getRepos();

// Assert
expect(result).toStrictEqual({
'/path/to/repo': {
columnWidths: null,
cdvDivider: 0.5,
cdvHeight: 250,
commitOrdering: RepoCommitOrdering.Default,
fileViewType: FileViewType.Default,
includeCommitsMentionedByReflogs: IncludeCommitsMentionedByReflogs.Default,
onlyFollowFirstParent: OnlyFollowFirstParent.Default,
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: true,
showRemoteBranchesV2: ShowRemoteBranches.Default,
showTags: ShowTags.Default,
hideRemotes: []
}
});
});

it('Should migrate showRemoteBranches = FALSE from boolean to enum (repository.showRemoteBranches = TRUE)', () => {
// Setup
extensionContext.workspaceState.get.mockReturnValueOnce({
'/path/to/repo': {
showRemoteBranches: false
}
});
workspaceConfiguration.get.mockReturnValueOnce(true); // repository.showRemoteBranches

// Run
const result = extensionState.getRepos();

// Assert
expect(result).toStrictEqual({
'/path/to/repo': {
columnWidths: null,
cdvDivider: 0.5,
cdvHeight: 250,
commitOrdering: RepoCommitOrdering.Default,
fileViewType: FileViewType.Default,
includeCommitsMentionedByReflogs: IncludeCommitsMentionedByReflogs.Default,
onlyFollowFirstParent: OnlyFollowFirstParent.Default,
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: false,
showRemoteBranchesV2: ShowRemoteBranches.Hide,
showTags: ShowTags.Default,
hideRemotes: []
}
});
});

it('Should migrate showRemoteBranches = FALSE from boolean to enum (repository.showRemoteBranches = FALSE)', () => {
// Setup
extensionContext.workspaceState.get.mockReturnValueOnce({
'/path/to/repo': {
showRemoteBranches: false
}
});
workspaceConfiguration.get.mockReturnValueOnce(false); // repository.showRemoteBranches

// Run
const result = extensionState.getRepos();

// Assert
expect(result).toStrictEqual({
'/path/to/repo': {
columnWidths: null,
cdvDivider: 0.5,
cdvHeight: 250,
commitOrdering: RepoCommitOrdering.Default,
fileViewType: FileViewType.Default,
includeCommitsMentionedByReflogs: IncludeCommitsMentionedByReflogs.Default,
onlyFollowFirstParent: OnlyFollowFirstParent.Default,
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: false,
showRemoteBranchesV2: ShowRemoteBranches.Default,
showTags: ShowTags.Default,
hideRemotes: []
}
});
});

it('Should migrate showRemoteBranches = TRUE from boolean to enum (repository.showRemoteBranches = FALSE)', () => {
// Setup
extensionContext.workspaceState.get.mockReturnValueOnce({
'/path/to/repo': {
showRemoteBranches: true
}
});
workspaceConfiguration.get.mockReturnValueOnce(false); // repository.showRemoteBranches

// Run
const result = extensionState.getRepos();

// Assert
expect(result).toStrictEqual({
'/path/to/repo': {
columnWidths: null,
cdvDivider: 0.5,
cdvHeight: 250,
commitOrdering: RepoCommitOrdering.Default,
fileViewType: FileViewType.Default,
includeCommitsMentionedByReflogs: IncludeCommitsMentionedByReflogs.Default,
onlyFollowFirstParent: OnlyFollowFirstParent.Default,
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: true,
showRemoteBranchesV2: ShowRemoteBranches.Show,
showTags: ShowTags.Default,
hideRemotes: []
}
});
});

it('Should migrate multiple showRemoteBranches from boolean to enum (repository.showRemoteBranches = TRUE)', () => {
// Setup
extensionContext.workspaceState.get.mockReturnValueOnce({
'/path/to/repo-1': {
showRemoteBranches: true
},
'/path/to/repo-2': {
showRemoteBranches: false
}
});
workspaceConfiguration.get.mockReturnValueOnce(true); // repository.showRemoteBranches

// Run
const result = extensionState.getRepos();

// Assert
expect(result).toStrictEqual({
'/path/to/repo-1': {
columnWidths: null,
cdvDivider: 0.5,
cdvHeight: 250,
commitOrdering: RepoCommitOrdering.Default,
fileViewType: FileViewType.Default,
includeCommitsMentionedByReflogs: IncludeCommitsMentionedByReflogs.Default,
onlyFollowFirstParent: OnlyFollowFirstParent.Default,
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: true,
showRemoteBranchesV2: ShowRemoteBranches.Default,
showTags: ShowTags.Default,
hideRemotes: []
},
'/path/to/repo-2': {
columnWidths: null,
cdvDivider: 0.5,
cdvHeight: 250,
commitOrdering: RepoCommitOrdering.Default,
fileViewType: FileViewType.Default,
includeCommitsMentionedByReflogs: IncludeCommitsMentionedByReflogs.Default,
onlyFollowFirstParent: OnlyFollowFirstParent.Default,
issueLinkingConfig: null,
pullRequestConfig: null,
showRemoteBranches: false,
showRemoteBranchesV2: ShowRemoteBranches.Hide,
showTags: ShowTags.Default,
hideRemotes: []
}
});
expect(workspaceConfiguration.get).toHaveBeenCalledTimes(1);
});

it('Should return the default value if it is not defined', () => {
// Setup
extensionContext.workspaceState.get.mockImplementationOnce((_, defaultValue) => defaultValue);
Expand Down
Loading

0 comments on commit ef01443

Please sign in to comment.