diff --git a/src/commands/git/cherry-pick.ts b/src/commands/git/cherry-pick.ts index 5f846d0..974d3d3 100644 --- a/src/commands/git/cherry-pick.ts +++ b/src/commands/git/cherry-pick.ts @@ -8,6 +8,7 @@ import { getBranchesAndOrTags, getValidateGitReferenceFn, QuickCommandBase, + QuickPickStep, StepAsyncGenerator, StepSelection, StepState @@ -16,17 +17,19 @@ import { CommitQuickPickItem, Directive, DirectiveQuickPickItem, - GitFlagsQuickPickItem, + FlagsQuickPickItem, ReferencesQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; import { runGitCommandInTerminal } from '../../terminal'; import { Logger } from '../../logger'; +type Flags = '--edit'; + interface State { repo: Repository; references?: GitReference[]; - flags: string[]; + flags: Flags[]; } export interface CherryPickGitCommandArgs { @@ -80,6 +83,10 @@ export class CherryPickGitCommand extends QuickCommandBase { let repos; let selectedBranchOrTag: GitReference | undefined; + if (state.flags == null) { + state.flags = []; + } + while (true) { try { if (repos === undefined) { @@ -198,10 +205,10 @@ export class CherryPickGitCommand extends QuickCommandBase { state.references = selection.map(i => i.item); } - const step = this.createConfirmStep( + const step: QuickPickStep> = this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${state.repo.formattedName}`, [ - { + FlagsQuickPickItem.create(state.flags, [], { label: this.title, description: `${ state.references!.length === 1 @@ -212,10 +219,9 @@ export class CherryPickGitCommand extends QuickCommandBase { state.references!.length === 1 ? `commit ${state.references![0].name}` : `${state.references!.length} commits` - } onto ${destination.name}`, - item: [] - }, - { + } onto ${destination.name}` + }), + FlagsQuickPickItem.create(state.flags, ['--edit'], { label: `${this.title} & Edit`, description: `--edit ${ state.references!.length === 1 @@ -226,9 +232,8 @@ export class CherryPickGitCommand extends QuickCommandBase { state.references!.length === 1 ? `commit ${state.references![0].name}` : `${state.references!.length} commits` - } onto ${destination.name}`, - item: ['--edit'] - } + } onto ${destination.name}` + }) ] ); const selection: StepSelection = yield step; diff --git a/src/commands/git/fetch.ts b/src/commands/git/fetch.ts index 9882d26..1b99db8 100644 --- a/src/commands/git/fetch.ts +++ b/src/commands/git/fetch.ts @@ -1,15 +1,17 @@ 'use strict'; import { Container } from '../../container'; import { Repository } from '../../git/gitService'; -import { QuickCommandBase, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; -import { GitFlagsQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; +import { QuickCommandBase, QuickPickStep, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; +import { FlagsQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; import { Dates, Strings } from '../../system'; import { GlyphChars } from '../../constants'; import { Logger } from '../../logger'; +type Flags = '--all' | '--prune'; + interface State { repos: Repository[]; - flags: string[]; + flags: Flags[]; } export interface FetchGitCommandArgs { @@ -48,6 +50,10 @@ export class FetchGitCommand extends QuickCommandBase { const state: StepState = this._initialState === undefined ? { counter: 0 } : this._initialState; let repos; + if (state.flags == null) { + state.flags = []; + } + while (true) { try { if (repos === undefined) { @@ -102,53 +108,49 @@ export class FetchGitCommand extends QuickCommandBase { } } - const step = this.createConfirmStep( + const step: QuickPickStep> = this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${ state.repos.length === 1 ? `${state.repos[0].formattedName}${fetchedOn}` : `${state.repos.length} repositories` }`, [ - { + FlagsQuickPickItem.create(state.flags, [], { label: this.title, description: '', detail: `Will fetch ${ state.repos.length === 1 ? state.repos[0].formattedName : `${state.repos.length} repositories` - }`, - item: [] - }, - { + }` + }), + FlagsQuickPickItem.create(state.flags, ['--prune'], { label: `${this.title} & Prune`, description: '--prune', detail: `Will fetch and prune ${ state.repos.length === 1 ? state.repos[0].formattedName : `${state.repos.length} repositories` - }`, - item: ['--prune'] - }, - { + }` + }), + FlagsQuickPickItem.create(state.flags, ['--all'], { label: `${this.title} All`, description: '--all', detail: `Will fetch all remotes of ${ state.repos.length === 1 ? state.repos[0].formattedName : `${state.repos.length} repositories` - }`, - item: ['--all'] - }, - { + }` + }), + FlagsQuickPickItem.create(state.flags, ['--all', '--prune'], { label: `${this.title} All & Prune`, description: '--all --prune', detail: `Will fetch and prune all remotes of ${ state.repos.length === 1 ? state.repos[0].formattedName : `${state.repos.length} repositories` - }`, - item: ['--all', '--prune'] - } + }` + }) ] ); const selection: StepSelection = yield step; @@ -162,8 +164,6 @@ export class FetchGitCommand extends QuickCommandBase { } state.flags = selection[0].item; - } else { - state.flags = state.flags || []; } this.execute(state as State); diff --git a/src/commands/git/merge.ts b/src/commands/git/merge.ts index 56d8956..ffbf2b8 100644 --- a/src/commands/git/merge.ts +++ b/src/commands/git/merge.ts @@ -6,6 +6,7 @@ import { getBranchesAndOrTags, getValidateGitReferenceFn, QuickCommandBase, + QuickPickStep, StepAsyncGenerator, StepSelection, StepState @@ -13,7 +14,7 @@ import { import { Directive, DirectiveQuickPickItem, - GitFlagsQuickPickItem, + FlagsQuickPickItem, ReferencesQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; @@ -21,10 +22,12 @@ import { Strings } from '../../system'; import { runGitCommandInTerminal } from '../../terminal'; import { Logger } from '../../logger'; +type Flags = '--ff-only' | '--no-ff' | '--squash'; + interface State { repo: Repository; reference: GitReference; - flags: string[]; + flags: Flags[]; } export interface MergeGitCommandArgs { @@ -68,6 +71,10 @@ export class MergeGitCommand extends QuickCommandBase { const state: StepState = this._initialState === undefined ? { counter: 0 } : this._initialState; let repos; + if (state.flags == null) { + state.flags = []; + } + while (true) { try { if (repos === undefined) { @@ -156,42 +163,38 @@ export class MergeGitCommand extends QuickCommandBase { break; } - const step = this.createConfirmStep( + const step: QuickPickStep> = this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${state.repo.formattedName}`, [ - { + FlagsQuickPickItem.create(state.flags, [], { label: this.title, description: `${state.reference.name} into ${destination.name}`, detail: `Will merge ${Strings.pluralize('commit', count)} from ${ state.reference.name - } into ${destination.name}`, - item: [] - }, - { + } into ${destination.name}` + }), + FlagsQuickPickItem.create(state.flags, ['--ff-only'], { label: `Fast-forward ${this.title}`, description: `--ff-only ${state.reference.name} into ${destination.name}`, detail: `Will fast-forward merge ${Strings.pluralize('commit', count)} from ${ state.reference.name - } into ${destination.name}`, - item: ['--ff-only'] - }, - { + } into ${destination.name}` + }), + FlagsQuickPickItem.create(state.flags, ['--no-ff'], { label: `No Fast-forward ${this.title}`, description: `--no-ff ${state.reference.name} into ${destination.name}`, detail: `Will create a merge commit when merging ${Strings.pluralize( 'commit', count - )} from ${state.reference.name} into ${destination.name}`, - item: ['--no-ff'] - }, - { + )} from ${state.reference.name} into ${destination.name}` + }), + FlagsQuickPickItem.create(state.flags, ['--squash'], { label: `Squash ${this.title}`, description: `--squash ${state.reference.name} into ${destination.name}`, detail: `Will squash ${Strings.pluralize('commit', count)} from ${ state.reference.name - } into one when merging into ${destination.name}`, - item: ['--squash'] - } + } into one when merging into ${destination.name}` + }) ] ); const selection: StepSelection = yield step; diff --git a/src/commands/git/pull.ts b/src/commands/git/pull.ts index 6e36cde..d292ae1 100644 --- a/src/commands/git/pull.ts +++ b/src/commands/git/pull.ts @@ -2,15 +2,17 @@ import { QuickInputButton } from 'vscode'; import { Container } from '../../container'; import { Repository } from '../../git/gitService'; -import { QuickCommandBase, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; -import { GitFlagsQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; +import { QuickCommandBase, QuickPickStep, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; +import { FlagsQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; import { Dates, Strings } from '../../system'; import { GlyphChars } from '../../constants'; import { Logger } from '../../logger'; +type Flags = '--rebase'; + interface State { repos: Repository[]; - flags: string[]; + flags: Flags[]; } export interface PullGitCommandArgs { @@ -58,6 +60,10 @@ export class PullGitCommand extends QuickCommandBase { const state: StepState = this._initialState === undefined ? { counter: 0 } : this._initialState; let repos; + if (state.flags == null) { + state.flags = []; + } + while (true) { try { if (repos === undefined) { @@ -102,25 +108,23 @@ export class PullGitCommand extends QuickCommandBase { } if (this.confirm(state.confirm)) { - let step; + let step: QuickPickStep>; if (state.repos.length > 1) { - step = this.createConfirmStep( + step = this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${ state.repos.length } repositories`, [ - { + FlagsQuickPickItem.create(state.flags, [], { label: this.title, description: '', - detail: `Will pull ${state.repos.length} repositories`, - item: [] - }, - { + detail: `Will pull ${state.repos.length} repositories` + }), + FlagsQuickPickItem.create(state.flags, ['--rebase'], { label: `${this.title} with Rebase`, description: '--rebase', - detail: `Will pull with rebase ${state.repos.length} repositories`, - item: ['--rebase'] - } + detail: `Will pull with rebase ${state.repos.length} repositories` + }) ] ); } else { @@ -138,8 +142,6 @@ export class PullGitCommand extends QuickCommandBase { } state.flags = selection[0].item; - } else { - state.flags = state.flags || []; } this.execute(state as State); @@ -170,35 +172,30 @@ export class PullGitCommand extends QuickCommandBase { ).fromNow()}`; } - const step = this.createConfirmStep( + const step = this.createConfirmStep>( `${title}${fetchedOn}`, [ - { + FlagsQuickPickItem.create(state.flags!, [], { label: this.title, description: '', - detail: `Will pull ${detail}`, - item: [] - }, - { + detail: `Will pull ${detail}` + }), + FlagsQuickPickItem.create(state.flags!, ['--rebase'], { label: `${this.title} with Rebase`, description: '--rebase', - detail: `Will pull ${detail} with rebase`, - item: ['--rebase'] - } + detail: `Will pull ${detail} with rebase` + }) ], undefined, { additionalButtons: [this.Buttons.Fetch], onDidClickButton: async (quickpick, button) => { if (button !== this.Buttons.Fetch) return; - quickpick.title = `${title}${Strings.pad(GlyphChars.Dot, 2, 2)}Fetching${GlyphChars.Ellipsis}`; quickpick.busy = true; quickpick.enabled = false; - try { await repo.fetch({ progress: true }); - const step = await this.getSingleRepoConfirmStep(state); quickpick.title = step.title; quickpick.items = step.items as any; diff --git a/src/commands/git/push.ts b/src/commands/git/push.ts index 0ad47f3..4c5d811 100644 --- a/src/commands/git/push.ts +++ b/src/commands/git/push.ts @@ -1,15 +1,17 @@ 'use strict'; import { Container } from '../../container'; import { Repository } from '../../git/gitService'; -import { QuickCommandBase, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; -import { Directive, DirectiveQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; +import { QuickCommandBase, QuickPickStep, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; +import { Directive, DirectiveQuickPickItem, FlagsQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; import { Strings } from '../../system'; import { GlyphChars } from '../../constants'; import { Logger } from '../../logger'; +type Flags = '--force'; + interface State { repos: Repository[]; - flags: string[]; + flags: Flags[]; } export interface PushGitCommandArgs { @@ -47,6 +49,10 @@ export class PushGitCommand extends QuickCommandBase { const state: StepState = this._initialState === undefined ? { counter: 0 } : this._initialState; let repos; + if (state.flags == null) { + state.flags = []; + } + while (true) { try { if (repos === undefined) { @@ -91,25 +97,23 @@ export class PushGitCommand extends QuickCommandBase { } if (this.confirm(state.confirm)) { - let step; + let step: QuickPickStep>; if (state.repos.length > 1) { step = this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${ state.repos.length } repositories`, [ - { + FlagsQuickPickItem.create(state.flags, [], { label: this.title, description: '', - detail: `Will push ${state.repos.length} repositories`, - item: [] - }, - { + detail: `Will push ${state.repos.length} repositories` + }), + FlagsQuickPickItem.create(state.flags, ['--force'], { label: `Force ${this.title}`, description: '--force', - detail: `Will force push ${state.repos.length} repositories`, - item: ['--force'] - } + detail: `Will force push ${state.repos.length} repositories` + }) ] ); } else { @@ -127,8 +131,6 @@ export class PushGitCommand extends QuickCommandBase { } state.flags = selection[0].item; - } else { - state.flags = state.flags || []; } this.execute(state as State); @@ -166,18 +168,16 @@ export class PushGitCommand extends QuickCommandBase { return this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${repo.formattedName}`, [ - { + FlagsQuickPickItem.create(state.flags!, [], { label: this.title, description: '', - detail: `Will push ${detail}`, - item: [] - }, - { + detail: `Will push ${detail}` + }), + FlagsQuickPickItem.create(state.flags!, ['--force'], { label: `Force ${this.title}`, description: '--force', - detail: `Will force push ${detail}`, - item: ['--force'] - } + detail: `Will force push ${detail}` + }) ] ); } diff --git a/src/commands/git/rebase.ts b/src/commands/git/rebase.ts index 9d87eb5..980303a 100644 --- a/src/commands/git/rebase.ts +++ b/src/commands/git/rebase.ts @@ -7,6 +7,7 @@ import { getBranchesAndOrTags, getValidateGitReferenceFn, QuickCommandBase, + QuickPickStep, StepAsyncGenerator, StepSelection, StepState @@ -15,7 +16,7 @@ import { CommitQuickPickItem, Directive, DirectiveQuickPickItem, - GitFlagsQuickPickItem, + FlagsQuickPickItem, ReferencesQuickPickItem, RefQuickPickItem, RepositoryQuickPickItem @@ -24,10 +25,12 @@ import { Iterables, Mutable, Strings } from '../../system'; import { runGitCommandInTerminal } from '../../terminal'; import { Logger } from '../../logger'; +type Flags = '--interactive'; + interface State { repo: Repository; reference: GitReference; - flags: string[]; + flags: Flags[]; } export interface RebaseGitCommandArgs { @@ -92,6 +95,10 @@ export class RebaseGitCommand extends QuickCommandBase { let selectedBranchOrTag: GitReference | undefined; let pickCommit = false; + if (state.flags == null) { + state.flags = []; + } + while (true) { try { if (repos === undefined) { @@ -238,27 +245,25 @@ export class RebaseGitCommand extends QuickCommandBase { break; } - const step = this.createConfirmStep( + const step: QuickPickStep> = this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${state.repo.formattedName}`, [ - { + FlagsQuickPickItem.create(state.flags, [], { label: this.title, description: `${destination.name} with ${state.reference.name}`, detail: `Will update ${destination.name} by applying ${Strings.pluralize( 'commit', count - )} on top of ${state.reference.name}`, - item: [] - }, - { + )} on top of ${state.reference.name}` + }), + FlagsQuickPickItem.create(state.flags, ['--interactive'], { label: `Interactive ${this.title}`, description: `--interactive ${destination.name} with ${state.reference.name}`, detail: `Will interactively update ${destination.name} by applying ${Strings.pluralize( 'commit', count - )} on top of ${state.reference.name}`, - item: ['--interactive'] - } + )} on top of ${state.reference.name}` + }) ] ); const selection: StepSelection = yield step; diff --git a/src/commands/git/reset.ts b/src/commands/git/reset.ts index 6c37b52..95120be 100644 --- a/src/commands/git/reset.ts +++ b/src/commands/git/reset.ts @@ -4,21 +4,23 @@ import { Container } from '../../container'; import { GitReference, Repository } from '../../git/gitService'; import { GlyphChars } from '../../constants'; import { Iterables, Strings } from '../../system'; -import { QuickCommandBase, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; +import { QuickCommandBase, QuickPickStep, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; import { CommitQuickPickItem, Directive, DirectiveQuickPickItem, - GitFlagsQuickPickItem, + FlagsQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; import { runGitCommandInTerminal } from '../../terminal'; import { Logger } from '../../logger'; +type Flags = '--hard'; + interface State { repo: Repository; reference?: GitReference; - flags: string[]; + flags: Flags[]; } export interface ResetGitCommandArgs { @@ -60,6 +62,10 @@ export class ResetGitCommand extends QuickCommandBase { const state: StepState = this._initialState === undefined ? { counter: 0 } : this._initialState; let repos; + if (state.flags == null) { + state.flags = []; + } + while (true) { try { if (repos === undefined) { @@ -141,21 +147,19 @@ export class ResetGitCommand extends QuickCommandBase { state.reference = selection[0].item; } - const step = this.createConfirmStep( + const step: QuickPickStep> = this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${state.repo.formattedName}`, [ - { + FlagsQuickPickItem.create(state.flags, [], { label: `Soft ${this.title}`, description: `--soft ${destination.name} to ${state.reference.name}`, - detail: `Will soft reset (leaves changes in the working tree) ${destination.name} to ${state.reference.name}`, - item: ['--soft'] - }, - { + detail: `Will soft reset (leaves changes in the working tree) ${destination.name} to ${state.reference.name}` + }), + FlagsQuickPickItem.create(state.flags, ['--hard'], { label: `Hard ${this.title}`, description: `--hard ${destination.name} to ${state.reference.name}`, - detail: `Will hard reset (discards all changes) ${destination.name} to ${state.reference.name}`, - item: ['--hard'] - } + detail: `Will hard reset (discards all changes) ${destination.name} to ${state.reference.name}` + }) ] ); const selection: StepSelection = yield step; diff --git a/src/commands/git/revert.ts b/src/commands/git/revert.ts index 5ccaf22..cd84df5 100644 --- a/src/commands/git/revert.ts +++ b/src/commands/git/revert.ts @@ -5,20 +5,13 @@ import { GitReference, Repository } from '../../git/gitService'; import { GlyphChars } from '../../constants'; import { Iterables, Strings } from '../../system'; import { QuickCommandBase, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; -import { - CommitQuickPickItem, - Directive, - DirectiveQuickPickItem, - GitFlagsQuickPickItem, - RepositoryQuickPickItem -} from '../../quickpicks'; +import { CommitQuickPickItem, Directive, DirectiveQuickPickItem, RepositoryQuickPickItem } from '../../quickpicks'; import { runGitCommandInTerminal } from '../../terminal'; import { Logger } from '../../logger'; interface State { repo: Repository; references?: GitReference[]; - flags: string[]; } export interface RevertGitCommandArgs { @@ -57,7 +50,7 @@ export class RevertGitCommand extends QuickCommandBase { execute(state: State) { runGitCommandInTerminal( 'revert', - [...state.flags, ...state.references!.map(c => c.ref).reverse()].join(' '), + [...state.references!.map(c => c.ref).reverse()].join(' '), state.repo.path, true ); @@ -151,7 +144,7 @@ export class RevertGitCommand extends QuickCommandBase { state.references = selection.map(i => i.item); } - const step = this.createConfirmStep( + const step = this.createConfirmStep( `Confirm ${this.title}${Strings.pad(GlyphChars.Dot, 2, 2)}${state.repo.formattedName}`, [ { @@ -165,8 +158,7 @@ export class RevertGitCommand extends QuickCommandBase { state.references.length === 1 ? `commit ${state.references[0].name}` : `${state.references.length} commits` - } on ${destination.name}`, - item: [] + } on ${destination.name}` } ] ); @@ -176,8 +168,6 @@ export class RevertGitCommand extends QuickCommandBase { continue; } - state.flags = selection[0].item; - this.execute(state as State); break; } catch (ex) { diff --git a/src/commands/git/stash.ts b/src/commands/git/stash.ts index 39521a9..c42c29f 100644 --- a/src/commands/git/stash.ts +++ b/src/commands/git/stash.ts @@ -3,14 +3,21 @@ import { QuickInputButton, QuickInputButtons, QuickPickItem, Uri, window } from 'vscode'; import { Container } from '../../container'; import { GitStashCommit, GitUri, Repository, SearchPattern } from '../../git/gitService'; -import { BreakQuickCommand, QuickCommandBase, StepAsyncGenerator, StepSelection, StepState } from '../quickCommand'; +import { + BreakQuickCommand, + QuickCommandBase, + QuickPickStep, + StepAsyncGenerator, + StepSelection, + StepState +} from '../quickCommand'; import { CommandQuickPickItem, CommitQuickPick, CommitQuickPickItem, Directive, DirectiveQuickPickItem, - GitFlagsQuickPickItem, + FlagsQuickPickItem, QuickPickItemOfT, RepositoryQuickPickItem } from '../../quickpicks'; @@ -23,14 +30,12 @@ interface ApplyState { subcommand: 'apply'; repo: Repository; stash: { stashName: string; message: string; ref: string; repoPath: string }; - flags: string[]; } interface DropState { subcommand: 'drop'; repo: Repository; stash: { stashName: string; message: string; ref: string; repoPath: string }; - flags: string[]; } interface ListState { @@ -42,15 +47,16 @@ interface PopState { subcommand: 'pop'; repo: Repository; stash: { stashName: string; message: string; ref: string; repoPath: string }; - flags: string[]; } +type PushFlags = '--include-untracked' | '--keep-index'; + interface PushState { subcommand: 'push'; repo: Repository; message?: string; uris?: Uri[]; - flags: string[]; + flags: PushFlags[]; } type State = ApplyState | DropState | ListState | PopState | PushState; @@ -373,7 +379,7 @@ export class StashGitCommand extends QuickCommandBase { ? `${state.stash.message.substring(0, 80)}${GlyphChars.Ellipsis}` : state.stash.message; - const step = this.createConfirmStep( + const step = this.createConfirmStep( `Confirm ${this.title} ${getSubtitle(state.subcommand)}${Strings.pad(GlyphChars.Dot, 2, 2)}${ state.repo.formattedName }`, @@ -389,8 +395,7 @@ export class StashGitCommand extends QuickCommandBase { : `Will apply the changes from ${state.stash!.stashName} to the working tree of ${ state.repo.formattedName }`, - command: state.subcommand!, - item: [] + command: state.subcommand! }, // Alternate confirmation (if pop then apply, and vice versa) { @@ -404,8 +409,7 @@ export class StashGitCommand extends QuickCommandBase { : `Will delete ${ state.stash!.stashName } and apply the changes to the working tree of ${state.repo.formattedName}`, - command: state.subcommand === 'pop' ? 'apply' : 'pop', - item: [] + command: state.subcommand === 'pop' ? 'apply' : 'pop' } ], undefined, @@ -429,9 +433,6 @@ export class StashGitCommand extends QuickCommandBase { } state.subcommand = selection[0].command; - state.flags = selection[0].item; - } else { - state.flags = state.flags || []; } void Container.git.stashApply(state.repo.path, state.stash!.stashName, state.subcommand === 'pop'); @@ -504,7 +505,7 @@ export class StashGitCommand extends QuickCommandBase { ? `${state.stash.message.substring(0, 80)}${GlyphChars.Ellipsis}` : state.stash.message; - const step = this.createConfirmStep( + const step = this.createConfirmStep( `Confirm ${this.title} ${getSubtitle(state.subcommand)}${Strings.pad(GlyphChars.Dot, 2, 2)}${ state.repo.formattedName }`, @@ -648,6 +649,10 @@ export class StashGitCommand extends QuickCommandBase { // eslint-disable-next-line @typescript-eslint/require-await private async *push(state: StashStepState): StepAsyncGenerator { + if (state.flags == null) { + state.flags = []; + } + while (true) { if (state.message === undefined || state.counter < 3) { const step = this.createInputStep({ @@ -668,52 +673,47 @@ export class StashGitCommand extends QuickCommandBase { } if (this.confirm(state.confirm)) { - const step = this.createConfirmStep( + const step: QuickPickStep> = this.createConfirmStep( `Confirm ${this.title} ${getSubtitle(state.subcommand)}${Strings.pad(GlyphChars.Dot, 2, 2)}${ state.repo.formattedName }`, state.uris === undefined || state.uris.length === 0 ? [ - { + FlagsQuickPickItem.create(state.flags, [], { label: `${this.title} ${getSubtitle(state.subcommand)}`, description: state.message, - detail: 'Will stash uncommitted changes', - item: [] - }, - { + detail: 'Will stash uncommitted changes' + }), + FlagsQuickPickItem.create(state.flags, ['--include-untracked'], { label: `${this.title} ${getSubtitle(state.subcommand)} & Include Untracked`, description: `--include-untracked ${state.message}`, - detail: 'Will stash uncommitted changes, including untracked files', - item: ['--include-untracked'] - }, - { + detail: 'Will stash uncommitted changes, including untracked files' + }), + FlagsQuickPickItem.create(state.flags, ['--keep-index'], { label: `${this.title} ${getSubtitle(state.subcommand)} & Keep Staged`, description: `--keep-index ${state.message}`, - detail: 'Will stash uncommitted changes, but will keep staged files intact', - item: ['--keep-index'] - } + detail: 'Will stash uncommitted changes, but will keep staged files intact' + }) ] : [ - { + FlagsQuickPickItem.create(state.flags, [], { label: `${this.title} ${getSubtitle(state.subcommand)}`, description: state.message, detail: `Will stash changes in ${ state.uris.length === 1 ? GitUri.getFormattedPath(state.uris[0], { relativeTo: state.repo.path }) : `${state.uris.length} files` - }`, - item: [] - }, - { + }` + }), + FlagsQuickPickItem.create(state.flags, ['--keep-index'], { label: `${this.title} ${getSubtitle(state.subcommand)} & Keep Staged`, description: `--keep-index ${state.message}`, detail: `Will stash changes in ${ state.uris.length === 1 ? GitUri.getFormattedPath(state.uris[0], { relativeTo: state.repo.path }) : `${state.uris.length} files` - }, but will keep staged files intact`, - item: ['--keep-index'] - } + }, but will keep staged files intact` + }) ], undefined, { placeholder: `Confirm ${this.title} ${getSubtitle(state.subcommand)}` } @@ -725,8 +725,6 @@ export class StashGitCommand extends QuickCommandBase { } state.flags = selection[0].item; - } else { - state.flags = state.flags || []; } void Container.git.stashSave(state.repo.path, state.message, state.uris, { diff --git a/src/commands/quickCommand.ts b/src/commands/quickCommand.ts index a8120c5..d1717b2 100644 --- a/src/commands/quickCommand.ts +++ b/src/commands/quickCommand.ts @@ -169,7 +169,7 @@ export abstract class QuickCommandBase implements QuickPickItem { placeholder: `Confirm ${this.title}`, title: title, items: [...confirmations, cancel || DirectiveQuickPickItem.create(Directive.Cancel)], - selectedItems: [confirmations[0]], + selectedItems: [confirmations.find(c => c.picked) || confirmations[0]], ...options }); } diff --git a/src/quickpicks/gitQuickPicks.ts b/src/quickpicks/gitQuickPicks.ts index 600a4cb..fd623a4 100644 --- a/src/quickpicks/gitQuickPicks.ts +++ b/src/quickpicks/gitQuickPicks.ts @@ -18,7 +18,19 @@ export interface QuickPickItemOfT extends QuickPickItem { readonly item: T; } -export interface GitFlagsQuickPickItem extends QuickPickItemOfT {} +export interface FlagsQuickPickItem extends QuickPickItemOfT {} +export namespace FlagsQuickPickItem { + export function create(flags: T[], item: T[], options: QuickPickItem) { + return { ...options, item: item, picked: hasFlags(flags, item) }; + } +} + +function hasFlags(flags: T[], has?: T | T[]): boolean { + if (has === undefined) return flags.length === 0; + if (!Array.isArray(has)) return flags.includes(has); + + return has.length === 0 ? flags.length === 0 : has.every(f => flags.includes(f)); +} export enum Directive { Back = 'Back',