Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce steps needed to reset workspace #860

Merged
merged 8 commits into from
Sep 30, 2021
Merged
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
25 changes: 20 additions & 5 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@
"command": "dvc.renameTarget",
"category": "DVC"
},
{
"title": "%command.resetWorkspace%",
"command": "dvc.resetWorkspace",
"category": "DVC",
"icon": "$(discard)"
},
{
"title": "%command.runExperiment%",
"command": "dvc.runExperiment",
Expand Down Expand Up @@ -395,6 +401,10 @@
"command": "dvc.renameTarget",
"when": "false"
},
{
"command": "dvc.resetWorkspace",
"when": "false"
},
{
"command": "dvc.runExperiment",
"when": "dvc.commands.available == true"
Expand Down Expand Up @@ -442,23 +452,28 @@
],
"scm/title": [
{
"command": "dvc.checkout",
"group": "navigation",
"command": "dvc.resetWorkspace",
"group": "navigation@1",
"when": "scmProvider == dvc && dvc.commands.available == true"
},
{
"command": "dvc.commit",
"group": "navigation",
"group": "navigation@2",
"when": "scmProvider == dvc && dvc.commands.available == true"
},
{
"command": "dvc.pull",
"group": "navigation",
"group": "navigation@3",
"when": "scmProvider == dvc && dvc.commands.available == true"
},
{
"command": "dvc.push",
"group": "navigation",
"group": "navigation@4",
"when": "scmProvider == dvc && dvc.commands.available == true"
},
{
"command": "dvc.checkout",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] This now goes into a new context menu which we can build on.

"group": "DVC",
"when": "scmProvider == dvc && dvc.commands.available == true"
}
],
Expand Down
1 change: 1 addition & 0 deletions extension/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"command.removeExperimentsTableSorts": "Remove Sort(s) From Experiments Table",
"command.removeTarget": "Remove",
"command.renameTarget": "Rename",
"command.resetWorkspace": "Reset the Workspace",
"command.runExperiment": "Run Experiment",
"command.runQueuedExperiments": "Run All Queued Experiments",
"command.runResetExperiment": "Run and Reset Experiment",
Expand Down
2 changes: 2 additions & 0 deletions extension/src/commands/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export enum RegisteredCommands {
DELETE_TARGET = 'dvc.deleteTarget',
MOVE_TARGETS = 'dvc.moveTargets',

RESET_WORKSPACE = 'dvc.resetWorkspace',

TRACKED_EXPLORER_OPEN_FILE = 'dvc.views.trackedExplorerTree.openFile',
TRACKED_EXPLORER_COMPARE_SELECTED = 'dvc.compareSelected',
TRACKED_EXPLORER_COPY_FILE_PATH = 'dvc.copyFilePath',
Expand Down
15 changes: 15 additions & 0 deletions extension/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,18 @@ export const getAllUntracked = async (
])
return new Set([...files, ...dirs])
}

export const gitResetWorkspace = async (
repositoryRoot: string
): Promise<void> => {
await executeProcess({
args: ['reset', '--hard', 'HEAD'],
cwd: repositoryRoot,
executable: 'git'
})
await executeProcess({
args: ['clean', '-f', '-d', '-q'],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] We need this to remove untracked from the workspace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this mirrors what the git extension does

cwd: repositoryRoot,
executable: 'git'
})
}
34 changes: 33 additions & 1 deletion extension/src/repository/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { relative } from 'path'
import { Uri } from 'vscode'
import { tryThenMaybeForce } from '../../cli/actions'
import { CommandId, InternalCommands } from '../../commands/internal'
import { Flag } from '../../cli/args'
import {
AvailableCommands,
CommandId,
InternalCommands
} from '../../commands/internal'
import { gitResetWorkspace } from '../../git'
import { getWarningResponse } from '../../vscode/modal'

export type Resource = {
dvcRoot: string
Expand Down Expand Up @@ -40,3 +47,28 @@ export const getRootCommand =

return tryThenMaybeForce(internalCommands, commandId, cwd)
}

export const getResetRootCommand =
(internalCommands: InternalCommands): RootCommand =>
async ({ rootUri }) => {
const cwd = rootUri.fsPath

const response = await getWarningResponse(
'Are you sure you want to discard ALL workspace changes?\n' +
'This is IRREVERSIBLE!\n' +
'Your current working set will be FOREVER LOST if you proceed.',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] This pretty much matches what the git extension throws up. You can see a comparison in the first demo.

'Discard Changes'
)

if (response !== 'Discard Changes') {
return
}

await gitResetWorkspace(cwd)

return internalCommands.executeCommand(
AvailableCommands.CHECKOUT,
cwd,
Flag.FORCE
)
}
11 changes: 10 additions & 1 deletion extension/src/repository/commands/register.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { commands } from 'vscode'
import {
getResetRootCommand,
getResourceCommand,
getRootCommand,
getSimpleResourceCommand,
Resource,
Root
} from '.'
import { tryThenMaybeForce } from '../../cli/actions'
import { RegisteredCliCommands } from '../../commands/external'
import {
RegisteredCliCommands,
RegisteredCommands
} from '../../commands/external'
import { AvailableCommands, InternalCommands } from '../../commands/internal'

const registerResourceCommands = (internalCommands: InternalCommands): void => {
Expand Down Expand Up @@ -52,6 +56,11 @@ const registerRootCommands = (internalCommands: InternalCommands) => {
RegisteredCliCommands.PUSH,
getRootCommand(internalCommands, AvailableCommands.PUSH)
)

internalCommands.registerExternalCommand<Root>(
RegisteredCommands.RESET_WORKSPACE,
getResetRootCommand(internalCommands)
)
}

export const registerRepositoryCommands = (
Expand Down
1 change: 1 addition & 0 deletions extension/src/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface IEventNamePropertyMapping {
[EventName.PUSH]: undefined
[EventName.REMOVE_TARGET]: undefined
[EventName.RENAME_TARGET]: undefined
[EventName.RESET_WORKSPACE]: undefined

[EventName.TRACKED_EXPLORER_COMPARE_SELECTED]: undefined
[EventName.TRACKED_EXPLORER_COPY_FILE_PATH]: undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { window, commands, Uri, MessageItem } from 'vscode'
import { Disposable } from '../../../extension'
import { CliExecutor } from '../../../cli/executor'
import { dvcDemoPath } from '../util'
import { RegisteredCliCommands } from '../../../commands/external'
import {
RegisteredCliCommands,
RegisteredCommands
} from '../../../commands/external'
import * as ProcessExecution from '../../../processExecution'

suite('Source Control Management Test Suite', () => {
const disposable = Disposable.fn()
Expand Down Expand Up @@ -179,5 +183,51 @@ suite('Source Control Management Test Suite', () => {
expect(mockPush).to.be.calledOnce
expect(mockShowErrorMessage).to.be.calledOnce
})

it('should not reset the workspace if the user does not confirm', async () => {
const mockCheckout = stub(CliExecutor.prototype, 'checkout').resolves('')
const mockGitReset = stub(ProcessExecution, 'executeProcess').resolves('')

const mockShowWarningMessage = stub(
window,
'showWarningMessage'
).resolves('' as unknown as MessageItem)

await commands.executeCommand(RegisteredCommands.RESET_WORKSPACE, {
rootUri
})

expect(mockShowWarningMessage).to.be.calledOnce
expect(mockCheckout).not.to.be.called
expect(mockGitReset).not.to.be.called
})

it('should reset the workspace if the user confirms they want to', async () => {
const mockCheckout = stub(CliExecutor.prototype, 'checkout').resolves('')
const mockGitReset = stub(ProcessExecution, 'executeProcess').resolves('')

const mockShowWarningMessage = stub(
window,
'showWarningMessage'
).resolves('Discard Changes' as unknown as MessageItem)

await commands.executeCommand(RegisteredCommands.RESET_WORKSPACE, {
rootUri
})

expect(mockShowWarningMessage).to.be.calledOnce
expect(mockCheckout).to.be.calledOnce
expect(mockGitReset).to.be.calledTwice
expect(mockGitReset).to.be.calledWith({
args: ['reset', '--hard', 'HEAD'],
cwd: dvcDemoPath,
executable: 'git'
})
expect(mockGitReset).to.be.calledWith({
args: ['clean', '-f', '-d', '-q'],
cwd: dvcDemoPath,
executable: 'git'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] Definitely do not want to be calling these actions in tests

})
})
})
})