Skip to content

Commit 4739b79

Browse files
authored
Improve reset the workspace SCM command (#1900)
* add has git changes to repository model * rename command to discard workspace changes
1 parent cfbfaa9 commit 4739b79

File tree

12 files changed

+89
-34
lines changed

12 files changed

+89
-34
lines changed

extension/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@
166166
"category": "DVC",
167167
"icon": "$(trash)"
168168
},
169+
{
170+
"title": "%command.discardWorkspaceChanges%",
171+
"command": "dvc.discardWorkspaceChanges",
172+
"category": "DVC",
173+
"icon": "$(discard)"
174+
},
169175
{
170176
"title": "%command.experimentGarbageCollect%",
171177
"command": "dvc.experimentGarbageCollect",
@@ -305,12 +311,6 @@
305311
"command": "dvc.renameTarget",
306312
"category": "DVC"
307313
},
308-
{
309-
"title": "%command.resetWorkspace%",
310-
"command": "dvc.resetWorkspace",
311-
"category": "DVC",
312-
"icon": "$(discard)"
313-
},
314314
{
315315
"title": "%command.runExperiment%",
316316
"command": "dvc.runExperiment",
@@ -682,7 +682,7 @@
682682
"when": "false"
683683
},
684684
{
685-
"command": "dvc.resetWorkspace",
685+
"command": "dvc.discardWorkspaceChanges",
686686
"when": "dvc.commands.available && dvc.project.available"
687687
},
688688
{
@@ -792,7 +792,7 @@
792792
],
793793
"scm/title": [
794794
{
795-
"command": "dvc.resetWorkspace",
795+
"command": "dvc.discardWorkspaceChanges",
796796
"group": "navigation@1",
797797
"when": "scmProvider == dvc && dvc.commands.available"
798798
},

extension/package.nls.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"command.copyFilePath": "Copy Path",
1616
"command.copyRelativeFilePath": "Copy Relative Path",
1717
"command.deleteTarget": "Delete",
18+
"command.discardWorkspaceChanges": "Discard Workspace Changes",
1819
"command.experimentGarbageCollect": "Garbage Collect Experiments",
1920
"command.findInFolder": "Find in Folder...",
2021
"command.getStarted": "Get Started",
@@ -39,7 +40,6 @@
3940
"command.removeQueuedExperiment": "Remove Queued Experiment",
4041
"command.removeTarget": "Remove",
4142
"command.renameTarget": "Rename",
42-
"command.resetWorkspace": "Reset the Workspace",
4343
"command.runExperiment": "Run Experiment",
4444
"command.resumeCheckpointExperiment": "Resume Experiment",
4545
"command.startExperimentsQueue": "Start the Experiments Queue",

extension/src/commands/external.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export enum RegisteredCommands {
6868
DELETE_TARGET = 'dvc.deleteTarget',
6969
MOVE_TARGETS = 'dvc.moveTargets',
7070

71-
RESET_WORKSPACE = 'dvc.resetWorkspace',
71+
DISCARD_WORKSPACE_CHANGES = 'dvc.discardWorkspaceChanges',
7272

7373
TRACKED_EXPLORER_OPEN_FILE = 'dvc.views.trackedExplorerTree.openFile',
7474
TRACKED_EXPLORER_COMPARE_SELECTED = 'dvc.compareSelected',

extension/src/git.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ export const getAllUntracked = async (
5151
return new Set([...files, ...dirs])
5252
}
5353

54+
export const getHasChanges = async (
55+
repositoryRoot: string
56+
): Promise<boolean> => {
57+
const output = await executeProcess({
58+
args: ['status', '-z', '-uall'],
59+
cwd: repositoryRoot,
60+
executable: 'git'
61+
})
62+
return !!output
63+
}
64+
5465
export const gitReset = (cwd: string, ...args: string[]): Promise<string> =>
5566
executeProcess({
5667
args: ['reset', ...args],

extension/src/repository/commands/register.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const registerRootCommands = (
7373
)
7474

7575
internalCommands.registerExternalCommand<Root>(
76-
RegisteredCommands.RESET_WORKSPACE,
76+
RegisteredCommands.DISCARD_WORKSPACE_CHANGES,
7777
getResetRootCommand(repositories, internalCommands)
7878
)
7979
}

extension/src/repository/data/index.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { Event, EventEmitter, RelativePattern } from 'vscode'
33
import { AvailableCommands, InternalCommands } from '../../commands/internal'
44
import { DiffOutput, ListOutput, StatusOutput } from '../../cli/reader'
55
import { isAnyDvcYaml } from '../../fileSystem'
6-
import { DOT_GIT, getAllUntracked, getGitRepositoryRoot } from '../../git'
6+
import {
7+
DOT_GIT,
8+
getAllUntracked,
9+
getGitRepositoryRoot,
10+
getHasChanges
11+
} from '../../git'
712
import { ProcessManager } from '../../processManager'
813
import {
914
createFileSystemWatcher,
@@ -20,6 +25,7 @@ export type Data = {
2025
diffFromHead: DiffOutput
2126
diffFromCache: StatusOutput
2227
untracked: Set<string>
28+
hasGitChanges: boolean
2329
tracked?: ListOutput[]
2430
}
2531

@@ -97,25 +103,31 @@ export class RepositoryData extends DeferredDisposable {
97103
this.dvcRoot
98104
)
99105

100-
const [diffFromHead, diffFromCache, untracked] =
106+
const [diffFromHead, diffFromCache, untracked, hasGitChanges] =
101107
await this.getPartialUpdateData()
102108

103109
return this.notifyChanged({
104110
diffFromCache,
105111
diffFromHead,
112+
hasGitChanges,
106113
tracked,
107114
untracked
108115
})
109116
}
110117

111118
private async partialUpdate() {
112-
const [diffFromHead, diffFromCache, untracked] =
119+
const [diffFromHead, diffFromCache, untracked, hasGitChanges] =
113120
await this.getPartialUpdateData()
114-
return this.notifyChanged({ diffFromCache, diffFromHead, untracked })
121+
return this.notifyChanged({
122+
diffFromCache,
123+
diffFromHead,
124+
hasGitChanges,
125+
untracked
126+
})
115127
}
116128

117129
private async getPartialUpdateData(): Promise<
118-
[DiffOutput, StatusOutput, Set<string>]
130+
[DiffOutput, StatusOutput, Set<string>, boolean]
119131
> {
120132
const diffFromCache =
121133
await this.internalCommands.executeCommand<StatusOutput>(
@@ -128,9 +140,12 @@ export class RepositoryData extends DeferredDisposable {
128140
this.dvcRoot
129141
)
130142

131-
const untracked = await getAllUntracked(this.dvcRoot)
143+
const [untracked, hasGitChanges] = await Promise.all([
144+
getAllUntracked(this.dvcRoot),
145+
getHasChanges(this.dvcRoot)
146+
])
132147

133-
return [diffFromHead, diffFromCache, untracked]
148+
return [diffFromHead, diffFromCache, untracked, hasGitChanges]
134149
}
135150

136151
private notifyChanged(data: Data) {

extension/src/repository/model/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe('RepositoryState', () => {
7676
model.setState({
7777
diffFromCache: status,
7878
diffFromHead: diff,
79+
hasGitChanges: true,
7980
tracked: list,
8081
untracked: new Set<string>()
8182
})
@@ -84,6 +85,7 @@ describe('RepositoryState', () => {
8485
added: emptySet,
8586
deleted: new Set([join(dvcDemoPath, deleted)]),
8687
gitModified: new Set([join(dvcDemoPath, output)]),
88+
hasGitChanges: true,
8789
hasRemote: new Set(list.map(entry => join(dvcDemoPath, entry.path))),
8890
modified: new Set([
8991
join(dvcDemoPath, rawDataDir),
@@ -122,6 +124,7 @@ describe('RepositoryState', () => {
122124
model.setState({
123125
diffFromCache: status,
124126
diffFromHead: diff,
127+
hasGitChanges: true,
125128
tracked: list,
126129
untracked: new Set<string>()
127130
})
@@ -133,6 +136,7 @@ describe('RepositoryState', () => {
133136
join(dvcDemoPath, rawDataDir),
134137
join(dvcDemoPath, data)
135138
]),
139+
hasGitChanges: true,
136140
hasRemote: new Set([join(dvcDemoPath, data)]),
137141
modified: emptySet,
138142
notInCache: emptySet,
@@ -165,6 +169,7 @@ describe('RepositoryState', () => {
165169
model.setState({
166170
diffFromCache: status,
167171
diffFromHead: diff,
172+
hasGitChanges: true,
168173
tracked: list,
169174
untracked: new Set<string>()
170175
})
@@ -173,6 +178,7 @@ describe('RepositoryState', () => {
173178
added: emptySet,
174179
deleted: emptySet,
175180
gitModified: emptySet,
181+
hasGitChanges: true,
176182
hasRemote: new Set([join(dvcDemoPath, data)]),
177183
modified: new Set([join(dvcDemoPath, rawDataDir)]),
178184
notInCache: emptySet,
@@ -202,13 +208,15 @@ describe('RepositoryState', () => {
202208
model.setState({
203209
diffFromCache: status,
204210
diffFromHead: diff,
211+
hasGitChanges: false,
205212
untracked: new Set<string>()
206213
})
207214

208215
expect(model.getState()).toStrictEqual({
209216
added: emptySet,
210217
deleted: emptySet,
211218
gitModified: emptySet,
219+
hasGitChanges: false,
212220
hasRemote: emptySet,
213221
modified: emptySet,
214222
notInCache: emptySet,
@@ -351,6 +359,7 @@ describe('RepositoryState', () => {
351359
model.setState({
352360
diffFromCache: status,
353361
diffFromHead: diff,
362+
hasGitChanges: true,
354363
tracked: list,
355364
untracked: new Set<string>()
356365
})
@@ -368,6 +377,7 @@ describe('RepositoryState', () => {
368377
added: emptySet,
369378
deleted: emptySet,
370379
gitModified: emptySet,
380+
hasGitChanges: true,
371381
hasRemote: new Set([
372382
...list.map(({ path }) => resolve(dvcDemoPath, path)),
373383
resolve(dvcDemoPath, 'data', 'MNIST', 'raw')
@@ -541,6 +551,7 @@ describe('RepositoryState', () => {
541551
model.setState({
542552
diffFromCache: status,
543553
diffFromHead: diff,
554+
hasGitChanges: true,
544555
tracked: list,
545556
untracked: new Set<string>()
546557
})
@@ -558,6 +569,7 @@ describe('RepositoryState', () => {
558569
added: emptySet,
559570
deleted: emptySet,
560571
gitModified: emptySet,
572+
hasGitChanges: true,
561573
hasRemote: new Set([
562574
resolve(dvcDemoPath, 'misclassified.jpg'),
563575
resolve(dvcDemoPath, 'model.pt'),

extension/src/repository/model/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ import {
2424
} from '../../cli/reader'
2525
import { Disposable } from '../../class/dispose'
2626
import { sameContents } from '../../util/array'
27-
28-
type OutputData = {
29-
diffFromCache: StatusOutput
30-
diffFromHead: DiffOutput
31-
tracked?: ListOutput[]
32-
untracked: Set<string>
33-
}
27+
import { Data } from '../data'
3428

3529
type ModifiedAndNotInCache = {
3630
[Status.MODIFIED]: Set<string>
@@ -47,6 +41,7 @@ export class RepositoryModel
4741
added: new Set<string>(),
4842
deleted: new Set<string>(),
4943
gitModified: new Set<string>(),
44+
hasGitChanges: false,
5045
modified: new Set<string>(),
5146
notInCache: new Set<string>(),
5247
renamed: new Set<string>(),
@@ -84,15 +79,17 @@ export class RepositoryModel
8479
public setState({
8580
diffFromCache,
8681
diffFromHead,
82+
hasGitChanges,
8783
tracked,
8884
untracked
89-
}: OutputData) {
85+
}: Data) {
9086
if (tracked) {
9187
this.updateTracked(tracked)
9288
}
9389
this.updateStatus(diffFromHead, diffFromCache)
9490

9591
this.state.untracked = untracked
92+
this.state.hasGitChanges = hasGitChanges
9693
}
9794

9895
public hasChanges(): boolean {
@@ -101,7 +98,8 @@ export class RepositoryModel
10198
this.state.deleted.size > 0 ||
10299
this.state.gitModified.size > 0 ||
103100
this.state.modified.size > 0 ||
104-
this.state.renamed.size > 0
101+
this.state.renamed.size > 0 ||
102+
this.state.hasGitChanges
105103
)
106104
}
107105

extension/src/telemetry/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export interface IEventNamePropertyMapping {
150150
[EventName.COMMIT_TARGET]: undefined
151151
[EventName.COMMIT]: undefined
152152
[EventName.DELETE_TARGET]: undefined
153+
[EventName.DISCARD_WORKSPACE_CHANGES]: undefined
153154
[EventName.INIT]: undefined
154155
[EventName.MOVE_TARGETS]: undefined
155156
[EventName.PULL_TARGET]: undefined
@@ -158,7 +159,6 @@ export interface IEventNamePropertyMapping {
158159
[EventName.PUSH]: undefined
159160
[EventName.REMOVE_TARGET]: undefined
160161
[EventName.RENAME_TARGET]: undefined
161-
[EventName.RESET_WORKSPACE]: undefined
162162

163163
[EventName.GIT_STAGE_ALL]: undefined
164164
[EventName.GIT_UNSTAGE_ALL]: undefined

0 commit comments

Comments
 (0)