Skip to content

Commit

Permalink
Adds branch/tag option to open/diff revision cmds
Browse files Browse the repository at this point in the history
Changes default sort of branch/tag pickers to be by date desc
  • Loading branch information
eamodio committed Sep 30, 2020
1 parent 4c7366a commit 89784cd
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 59 deletions.
37 changes: 19 additions & 18 deletions src/commands/diffWithRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GitRevision } from '../git/git';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { CommitPicker, DirectiveQuickPickItem } from '../quickpicks';
import { CommandQuickPickItem, CommitPicker } from '../quickpicks';
import { Strings } from '../system';

export interface DiffWithRevisionCommandArgs {
Expand Down Expand Up @@ -55,24 +55,25 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand {
{
picked: gitUri.sha,
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, quickpick) => {
const [item] = quickpick.activeItems;
if (item != null && !DirectiveQuickPickItem.is(item)) {
void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
repoPath: gitUri.repoPath,
lhs: {
sha: item.item.ref,
uri: gitUri,
},
rhs: {
sha: '',
uri: gitUri,
},
line: args!.line,
showOptions: args!.showOptions,
}));
}
onDidPressKey: async (key, item) => {
void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
repoPath: gitUri.repoPath,
lhs: {
sha: item.item.ref,
uri: gitUri,
},
rhs: {
sha: '',
uri: gitUri,
},
line: args!.line,
showOptions: args!.showOptions,
}));
},
showOtherReferences: CommandQuickPickItem.fromCommand(
'Choose a branch or tag...',
Commands.DiffWithRevisionFrom,
),
},
);
if (pick == null) return;
Expand Down
14 changes: 9 additions & 5 deletions src/commands/openBranchOnRemote.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict';
import { TextEditor, Uri, window } from 'vscode';
import { RemoteResourceType } from '../git/git';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
import { CommandQuickPickItem, ReferencePicker, ReferencesQuickPickIncludes } from '../quickpicks';
import {
ActiveEditorCommand,
command,
Expand All @@ -14,7 +10,12 @@ import {
getRepoPathOrActiveOrPrompt,
isCommandViewContextWithBranch,
} from './common';
import { BranchSorting } from '../configuration';
import { RemoteResourceType } from '../git/git';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
import { OpenOnRemoteCommandArgs } from './openOnRemote';
import { CommandQuickPickItem, ReferencePicker, ReferencesQuickPickIncludes } from '../quickpicks';

export interface OpenBranchOnRemoteCommandArgs {
branch?: string;
Expand Down Expand Up @@ -67,8 +68,11 @@ export class OpenBranchOnRemoteCommand extends ActiveEditorCommand {
{
autoPick: true,
// checkmarks: false,
filterBranches: b => b.tracking != null,
filter: { branches: b => b.tracking != null },
include: ReferencesQuickPickIncludes.Branches,
sort: {
branches: { current: true, orderBy: BranchSorting.DateDesc },
},
},
);
if (pick == null || pick instanceof CommandQuickPickItem) return;
Expand Down
23 changes: 12 additions & 11 deletions src/commands/openFileAtRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { GitUri } from '../git/gitUri';
import { GitActions } from './gitCommands';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { CommitPicker, DirectiveQuickPickItem } from '../quickpicks';
import { CommandQuickPickItem, CommitPicker } from '../quickpicks';
import { Strings } from '../system';

export interface OpenFileAtRevisionCommandArgs {
Expand Down Expand Up @@ -83,17 +83,18 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand {
{
picked: gitUri.sha,
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, quickpick) => {
const [item] = quickpick.activeItems;
if (item != null && !DirectiveQuickPickItem.is(item)) {
void (await GitActions.Commit.openFileAtRevision(item.item.uri.fsPath, item.item, {
annotationType: args!.annotationType,
line: args!.line,
preserveFocus: true,
preview: false,
}));
}
onDidPressKey: async (key, item) => {
void (await GitActions.Commit.openFileAtRevision(item.item.uri.fsPath, item.item, {
annotationType: args!.annotationType,
line: args!.line,
preserveFocus: true,
preview: false,
}));
},
showOtherReferences: CommandQuickPickItem.fromCommand(
'Choose a branch or tag...',
Commands.OpenFileAtRevisionFrom,
),
},
);
if (pick == null) return;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/openFileAtRevisionFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class OpenFileAtRevisionFromCommand extends ActiveEditorCommand {
}

if (args.reference == null) {
const title = `Open File at Revision${Strings.pad(GlyphChars.Dot, 2, 2)}`;
const title = `Open File at Branch or Tag${Strings.pad(GlyphChars.Dot, 2, 2)}`;
const pick = await ReferencePicker.show(
gitUri.repoPath,
`${title}${gitUri.getFormattedFilename({ truncateTo: quickPickTitleMaxChars - title.length })}`,
Expand Down
6 changes: 5 additions & 1 deletion src/commands/openFileOnRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isCommandViewContextWithCommit,
} from './common';
import { UriComparer } from '../comparers';
import { BranchSorting } from '../configuration';
import { GlyphChars } from '../constants';
import { Container } from '../container';
import { GitRevision, RemoteResourceType } from '../git/git';
Expand Down Expand Up @@ -124,8 +125,11 @@ export class OpenFileOnRemoteCommand extends ActiveEditorCommand {
{
autoPick: true,
// checkmarks: false,
filterBranches: b => b.tracking != null,
filter: { branches: b => b.tracking != null },
include: ReferencesQuickPickIncludes.Branches,
sort: {
branches: { current: true, orderBy: BranchSorting.DateDesc },
},
},
);
if (pick == null) return;
Expand Down
4 changes: 2 additions & 2 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1176,14 +1176,14 @@ export class GitService implements Disposable {
} = {},
) {
const [branches, tags] = await Promise.all<GitBranch[] | undefined, GitTag[] | undefined>([
include === 'all' || include === 'branches'
include == null || include === 'all' || include === 'branches'
? this.getBranches(repoPath, {
...options,
filter: filter?.branches,
sort: typeof sort === 'boolean' ? undefined : sort?.branches,
})
: undefined,
include === 'all' || include === 'tags'
include == null || include === 'all' || include === 'tags'
? this.getTags(repoPath, {
...options,
filter: filter?.tags,
Expand Down
40 changes: 30 additions & 10 deletions src/quickpicks/commitPicker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
'use strict';
import { Disposable, QuickPick, window } from 'vscode';
import { Disposable, window } from 'vscode';
import { configuration } from '../configuration';
import { Container } from '../container';
import { GitLog, GitLogCommit } from '../git/git';
import { KeyboardScope, Keys } from '../keyboard';
import { CommitQuickPickItem, Directive, DirectiveQuickPickItem, getQuickPickIgnoreFocusOut } from '../quickpicks';
import {
CommandQuickPickItem,
CommitQuickPickItem,
Directive,
DirectiveQuickPickItem,
getQuickPickIgnoreFocusOut,
} from '../quickpicks';
import { Iterables, Promises } from '../system';

export namespace CommitPicker {
Expand All @@ -15,13 +21,11 @@ export namespace CommitPicker {
options?: {
picked?: string;
keys?: Keys[];
onDidPressKey?(
key: Keys,
quickpick: QuickPick<CommitQuickPickItem | DirectiveQuickPickItem>,
): void | Promise<void>;
onDidPressKey?(key: Keys, item: CommitQuickPickItem): void | Promise<void>;
showOtherReferences?: CommandQuickPickItem;
},
): Promise<GitLogCommit | undefined> {
const quickpick = window.createQuickPick<CommitQuickPickItem | DirectiveQuickPickItem>();
const quickpick = window.createQuickPick<CommandQuickPickItem | CommitQuickPickItem | DirectiveQuickPickItem>();
quickpick.ignoreFocusOut = getQuickPickIgnoreFocusOut();

quickpick.title = title;
Expand All @@ -44,13 +48,14 @@ export namespace CommitPicker {
quickpick.items = getItems(log);

if (options?.picked) {
quickpick.activeItems = quickpick.items.filter(i => i.picked);
quickpick.activeItems = quickpick.items.filter(i => (CommandQuickPickItem.is(i) ? false : i.picked));
}

function getItems(log: GitLog | undefined) {
return log == null
? [DirectiveQuickPickItem.create(Directive.Cancel)]
: [
...(options?.showOtherReferences != null ? [options?.showOtherReferences] : []),
...Iterables.map(log.commits.values(), commit =>
CommitQuickPickItem.create(commit, options?.picked === commit.ref, {
compact: true,
Expand Down Expand Up @@ -102,7 +107,14 @@ export namespace CommitPicker {
{
onDidPressKey: key => {
if (quickpick.activeItems.length !== 0) {
void options.onDidPressKey!(key, quickpick);
const [item] = quickpick.activeItems;
if (
item != null &&
!DirectiveQuickPickItem.is(item) &&
!CommandQuickPickItem.is(item)
) {
void options.onDidPressKey!(key, item);
}
}
},
},
Expand All @@ -114,7 +126,9 @@ export namespace CommitPicker {
}

try {
const pick = await new Promise<CommitQuickPickItem | DirectiveQuickPickItem | undefined>(resolve => {
const pick = await new Promise<
CommandQuickPickItem | CommitQuickPickItem | DirectiveQuickPickItem | undefined
>(resolve => {
disposables.push(
quickpick.onDidHide(() => resolve()),
quickpick.onDidAccept(() => {
Expand Down Expand Up @@ -154,6 +168,12 @@ export namespace CommitPicker {
});
if (pick == null || DirectiveQuickPickItem.is(pick)) return undefined;

if (pick instanceof CommandQuickPickItem) {
void (await pick.execute());

return undefined;
}

return pick.item;
} finally {
quickpick.dispose();
Expand Down
12 changes: 8 additions & 4 deletions src/quickpicks/quickPicksItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,20 @@ export namespace DirectiveQuickPickItem {
}

export class CommandQuickPickItem<Arguments extends any[] = any[]> implements QuickPickItem {
static fromCommand<T>(label: string, command: Commands, args: T): CommandQuickPickItem;
static fromCommand<T>(item: QuickPickItem, command: Commands, args: T): CommandQuickPickItem;
static fromCommand<T>(labelOrItem: string | QuickPickItem, command: Commands, args: T): CommandQuickPickItem {
static fromCommand<T>(label: string, command: Commands, args?: T): CommandQuickPickItem;
static fromCommand<T>(item: QuickPickItem, command: Commands, args?: T): CommandQuickPickItem;
static fromCommand<T>(labelOrItem: string | QuickPickItem, command: Commands, args?: T): CommandQuickPickItem {
return new CommandQuickPickItem(
typeof labelOrItem === 'string' ? { label: labelOrItem } : labelOrItem,
command,
[args],
args == null ? [] : [args],
);
}

static is(item: QuickPickItem): item is CommandQuickPickItem {
return item instanceof CommandQuickPickItem;
}

label!: string;
description?: string;
detail?: string | undefined;
Expand Down
13 changes: 9 additions & 4 deletions src/quickpicks/referencePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GitBranch, GitReference, GitTag } from '../git/git';
import { KeyboardScope, Keys } from '../keyboard';
import { BranchQuickPickItem, getQuickPickIgnoreFocusOut, RefQuickPickItem, TagQuickPickItem } from '../quickpicks';
import { getBranchesAndOrTags, getValidateGitReferenceFn } from '../commands/quickCommand';
import { BranchSorting, TagSorting } from '../configuration';

export type ReferencesQuickPickItem = BranchQuickPickItem | TagQuickPickItem | RefQuickPickItem;

Expand All @@ -22,11 +23,11 @@ export interface ReferencesQuickPickOptions {
allowEnteringRefs?: boolean;
autoPick?: boolean;
picked?: string;
filterBranches?(branch: GitBranch): boolean;
filterTags?(tag: GitTag): boolean;
filter?: { branches?(b: GitBranch): boolean; tags?(t: GitTag): boolean };
include?: ReferencesQuickPickIncludes;
keys?: Keys[];
onDidPressKey?(key: Keys, quickpick: QuickPick<ReferencesQuickPickItem>): void | Promise<void>;
sort?: boolean | { branches?: { current?: boolean; orderBy?: BranchSorting }; tags?: { orderBy?: TagSorting } };
}

export namespace ReferencePicker {
Expand Down Expand Up @@ -135,7 +136,7 @@ export namespace ReferencePicker {

async function getItems(
repoPath: string,
{ picked, filterBranches, filterTags, include }: ReferencesQuickPickOptions,
{ picked, filter, include, sort }: ReferencesQuickPickOptions,
): Promise<ReferencesQuickPickItem[]> {
include = include ?? ReferencesQuickPickIncludes.BranchesAndTags;

Expand All @@ -149,8 +150,12 @@ export namespace ReferencePicker {
? ['tags']
: [],
{
filter: { branches: filterBranches, tags: filterTags },
filter: filter,
picked: picked,
sort: sort ?? {
branches: { current: false, orderBy: BranchSorting.DateDesc },
tags: { orderBy: TagSorting.DateDesc },
},
},
);

Expand Down
2 changes: 1 addition & 1 deletion src/terminal/linkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class GitTerminalLinkProvider implements Disposable, TerminalLinkProvider

const links: GitTerminalLink[] = [];

const branchesAndTags = await Container.git.getBranchesAndOrTags(repoPath, { include: 'all' });
const branchesAndTags = await Container.git.getBranchesAndOrTags(repoPath);

// Don't use the shared regex instance directly, because we can be called reentrantly (because of the awaits below)
const regex = new RegExp(refRegex, 'gi');
Expand Down
12 changes: 10 additions & 2 deletions src/views/nodes/compareBranchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from 'vscode';
import { CommitsView } from '../commitsView';
import { BranchComparison, BranchComparisons, GlyphChars, WorkspaceState } from '../../constants';
import { ViewShowBranchComparison } from '../../config';
import { BranchSorting, TagSorting, ViewShowBranchComparison } from '../../configuration';
import { Container } from '../../container';
import { GitBranch, GitRevision } from '../../git/git';
import { GitUri } from '../../git/gitUri';
Expand Down Expand Up @@ -189,7 +189,15 @@ export class CompareBranchNode extends ViewNode<CommitsView | RepositoriesView>
this.branch.repoPath,
`Compare ${this.branch.name}${this.compareWithWorkingTree ? ' (working)' : ''} with`,
'Choose a reference to compare with',
{ allowEnteringRefs: true, picked: this.branch.ref /*checkmarks: true*/ },
{
allowEnteringRefs: true,
picked: this.branch.ref,
// checkmarks: true,
sort: {
branches: { current: true, orderBy: BranchSorting.DateDesc },
tags: { orderBy: TagSorting.DateDesc },
},
},
);
if (pick === undefined || pick instanceof CommandQuickPickItem) return;

Expand Down
9 changes: 9 additions & 0 deletions src/views/nodes/compareNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { getRepoPathOrPrompt } from '../../commands';
import { BranchSorting, TagSorting } from '../../configuration';
import { CommandContext, NamedRef, setCommandContext } from '../../constants';
import { GitRevision } from '../../git/git';
import { ReferencePicker, ReferencesQuickPickIncludes } from '../../quickpicks';
Expand Down Expand Up @@ -150,6 +151,10 @@ export class CompareNode extends ViewNode<CompareView> {
ReferencesQuickPickIncludes.BranchesAndTags |
ReferencesQuickPickIncludes.HEAD |
ReferencesQuickPickIncludes.WorkingTree,
sort: {
branches: { current: true, orderBy: BranchSorting.DateDesc },
tags: { orderBy: TagSorting.DateDesc },
},
},
);
if (pick === undefined) {
Expand Down Expand Up @@ -190,6 +195,10 @@ export class CompareNode extends ViewNode<CompareView> {
ReferencesQuickPickIncludes.BranchesAndTags |
ReferencesQuickPickIncludes.HEAD |
ReferencesQuickPickIncludes.WorkingTree,
sort: {
branches: { current: true, orderBy: BranchSorting.DateDesc },
tags: { orderBy: TagSorting.DateDesc },
},
});
if (pick == null) {
await this.view.show();
Expand Down

0 comments on commit 89784cd

Please sign in to comment.