Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extensions/git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"findFiles2",
"quickDiffProvider",
"quickPickSortByLabel",
"resolvers",
"scmActionButton",
"scmArtifactProvider",
"scmHistoryProvider",
Comment thread
sarathfrancis90 marked this conversation as resolved.
Expand Down
37 changes: 29 additions & 8 deletions extensions/git/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as os from 'os';
import * as path from 'path';
import { Command, commands, Disposable, MessageOptions, Position, QuickPickItem, Range, SourceControlResourceState, TextDocumentShowOptions, TextEditor, Uri, ViewColumn, window, workspace, WorkspaceEdit, WorkspaceFolder, TimelineItem, env, Selection, TextDocumentContentProvider, InputBoxValidationSeverity, TabInputText, TabInputTextMerge, QuickPickItemKind, TextDocument, LogOutputChannel, l10n, Memento, UIKind, QuickInputButton, ThemeIcon, SourceControlHistoryItem, SourceControl, InputBoxValidationMessage, Tab, TabInputNotebook, QuickInputButtonLocation, languages, SourceControlArtifact, ProgressLocation } from 'vscode';
import { Command, commands, Disposable, MessageItem, MessageOptions, Position, QuickPickItem, Range, SourceControlResourceState, TextDocumentShowOptions, TextEditor, Uri, ViewColumn, window, workspace, WorkspaceEdit, WorkspaceFolder, TimelineItem, env, Selection, TextDocumentContentProvider, InputBoxValidationSeverity, TabInputText, TabInputTextMerge, QuickPickItemKind, TextDocument, LogOutputChannel, l10n, Memento, UIKind, QuickInputButton, ThemeIcon, SourceControlHistoryItem, SourceControl, InputBoxValidationMessage, Tab, TabInputNotebook, QuickInputButtonLocation, languages, SourceControlArtifact, ProgressLocation } from 'vscode';
import TelemetryReporter from '@vscode/extension-telemetry';
import type { CommitOptions, RemoteSourcePublisher, Remote, Branch, Ref } from './api/git';
import { ForcePushMode, GitErrorCodes, RefType, Status } from './api/git.constants';
Expand Down Expand Up @@ -360,6 +360,30 @@ class StashItem implements QuickPickItem {
constructor(readonly stash: Stash) { }
}

type DirtyWorkTreeCheckoutAction = 'stash' | 'migrate' | 'force';

interface DirtyWorkTreeCheckoutActionItem extends MessageItem {
readonly action: DirtyWorkTreeCheckoutAction;
}

type ShowDirtyWorkTreeCheckoutWarning = (message: string, options: MessageOptions, ...items: DirtyWorkTreeCheckoutActionItem[]) => Thenable<DirtyWorkTreeCheckoutActionItem | undefined>;

export async function pickDirtyWorkTreeCheckoutAction(showWarningMessage: ShowDirtyWorkTreeCheckoutWarning = window.showWarningMessage): Promise<DirtyWorkTreeCheckoutAction | undefined> {
const actions: DirtyWorkTreeCheckoutActionItem[] = [
{ action: 'stash', title: l10n.t('Stash & Checkout') },
{ action: 'migrate', title: l10n.t('Migrate Changes') },
{ action: 'force', title: l10n.t('Force Checkout') }
];

const choice = await showWarningMessage(
l10n.t('Your local changes would be overwritten by checkout.'),
{ modal: true, useCustom: true },
...actions
);

return choice?.action;
}

interface ScmCommandOptions {
repository?: boolean;
repositoryFilter?: ('repository' | 'submodule' | 'worktree')[];
Expand Down Expand Up @@ -2958,19 +2982,16 @@ export class CommandCenter {
return false;
}

const stash = l10n.t('Stash & Checkout');
const migrate = l10n.t('Migrate Changes');
const force = l10n.t('Force Checkout');
const choice = await window.showWarningMessage(l10n.t('Your local changes would be overwritten by checkout.'), { modal: true }, stash, migrate, force);
const choice = await pickDirtyWorkTreeCheckoutAction();

if (choice === force) {
if (choice === 'force') {
await this.cleanAll(repository);
await item.run(repository, opts);
} else if (choice === stash || choice === migrate) {
} else if (choice === 'stash' || choice === 'migrate') {
if (await this._stash(repository, true)) {
await item.run(repository, opts);

if (choice === migrate) {
if (choice === 'migrate') {
await this.stashPopLatest(repository);
}
}
Expand Down
61 changes: 61 additions & 0 deletions extensions/git/src/test/commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import 'mocha';
import assert from 'assert';
import { l10n, MessageItem, MessageOptions } from 'vscode';
import { pickDirtyWorkTreeCheckoutAction } from '../commands';

suite('Git commands', () => {

test('dirty work tree checkout prompt uses custom modal dialog', async () => {
let actualOptions: MessageOptions | undefined;

await pickDirtyWorkTreeCheckoutAction(async (_message, options, ...items) => {
actualOptions = options;

return items[0];
});

assert.strictEqual(actualOptions?.modal, true);
assert.strictEqual(actualOptions?.useCustom, true);
});

test('dirty work tree checkout prompt maps all choices to actions', async () => {
const expectedMessage = l10n.t('Your local changes would be overwritten by checkout.');
const expectedTitles = [
l10n.t('Stash & Checkout'),
l10n.t('Migrate Changes'),
l10n.t('Force Checkout')
];
const cases: [number, 'stash' | 'migrate' | 'force'][] = [
[0, 'stash'],
[1, 'migrate'],
[2, 'force']
];

for (const [choiceIndex, expectedAction] of cases) {
let actualMessage: string | undefined;
let actualItems: MessageItem[] | undefined;

const action = await pickDirtyWorkTreeCheckoutAction(async (message, _options, ...items) => {
actualMessage = message;
actualItems = items;

return items[choiceIndex];
});

assert.strictEqual(actualMessage, expectedMessage);
assert.deepStrictEqual(actualItems?.map(item => item.title), expectedTitles);
assert.strictEqual(action, expectedAction);
}
});
Comment thread
sarathfrancis90 marked this conversation as resolved.

test('dirty work tree checkout prompt returns undefined when dismissed', async () => {
const action = await pickDirtyWorkTreeCheckoutAction(async () => undefined);

assert.strictEqual(action, undefined);
});
Comment thread
sarathfrancis90 marked this conversation as resolved.
});
1 change: 1 addition & 0 deletions extensions/git/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"../../src/vscode-dts/vscode.proposed.envIsConnectionMetered.d.ts",
"../../src/vscode-dts/vscode.proposed.quickDiffProvider.d.ts",
"../../src/vscode-dts/vscode.proposed.quickPickSortByLabel.d.ts",
"../../src/vscode-dts/vscode.proposed.resolvers.d.ts",
"../../src/vscode-dts/vscode.proposed.scmActionButton.d.ts",
"../../src/vscode-dts/vscode.proposed.scmArtifactProvider.d.ts",
"../../src/vscode-dts/vscode.proposed.scmHistoryProvider.d.ts",
Expand Down