Skip to content

Commit 7d60c1c

Browse files
authored
Prevent current branch being duplicated when an experiment is running (#4084)
1 parent 6dda738 commit 7d60c1c

File tree

8 files changed

+97
-83
lines changed

8 files changed

+97
-83
lines changed

extension/src/experiments/data/index.ts

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,15 @@ export class ExperimentsData extends BaseData<ExperimentsOutput> {
3333

3434
void this.watchExpGitRefs()
3535
void this.managedUpdate()
36-
void this.updateAvailableBranchesToSelect()
3736
}
3837

3938
public managedUpdate() {
4039
return this.processManager.run('update')
4140
}
4241

4342
public async update(): Promise<void> {
44-
const allBranches = await this.internalCommands.executeCommand<string[]>(
45-
AvailableCommands.GIT_GET_BRANCHES,
46-
this.dvcRoot
47-
)
48-
49-
void this.updateAvailableBranchesToSelect(allBranches)
50-
51-
const branches = await this.getBranchesToShow(allBranches)
43+
await this.updateBranches()
44+
const branches = this.experiments.getBranchesToShow()
5245
let gitLog = ''
5346
const rowOrder: { branch: string; sha: string }[] = []
5447
const availableNbCommits: { [branch: string]: number } = {}
@@ -115,34 +108,19 @@ export class ExperimentsData extends BaseData<ExperimentsOutput> {
115108
return gitLog
116109
}
117110

118-
private async getBranchesToShow(allBranches: string[]) {
119-
const currentBranch = await this.internalCommands.executeCommand<string>(
120-
AvailableCommands.GIT_GET_CURRENT_BRANCH,
121-
this.dvcRoot
122-
)
123-
124-
this.experiments.pruneBranchesToShow(allBranches)
125-
126-
const currentBranches = [
127-
currentBranch,
128-
...this.experiments
129-
.getBranchesToShow()
130-
.filter(branch => branch !== currentBranch)
131-
]
132-
133-
this.experiments.setBranchesToShow(currentBranches)
134-
135-
return currentBranches
136-
}
137-
138-
private async updateAvailableBranchesToSelect(branches?: string[]) {
139-
const allBranches =
140-
branches ||
141-
(await this.internalCommands.executeCommand<string[]>(
111+
private async updateBranches() {
112+
const [currentBranch, allBranches] = await Promise.all([
113+
this.internalCommands.executeCommand<string>(
114+
AvailableCommands.GIT_GET_CURRENT_BRANCH,
115+
this.dvcRoot
116+
),
117+
this.internalCommands.executeCommand<string[]>(
142118
AvailableCommands.GIT_GET_BRANCHES,
143119
this.dvcRoot
144-
))
145-
this.experiments.setAvailableBranchesToShow(allBranches)
120+
)
121+
])
122+
123+
this.experiments.setBranches(currentBranch, allBranches)
146124
}
147125

148126
private async watchExpGitRefs(): Promise<void> {

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,21 +388,30 @@ describe('ExperimentsModel', () => {
388388
)
389389
})
390390

391-
it('should remove outdated branches to show when calling pruneBranchesToShow', () => {
391+
it('should always return the current branch first', () => {
392392
const model = new ExperimentsModel('', buildMockMemento())
393393

394-
model.setBranchesToShow(['A', 'old', 'B', 'C', 'older'])
395-
model.pruneBranchesToShow(['A', 'B', 'C', 'four', 'five', 'six'])
394+
model.setSelectedBranches(['A', 'old', 'B', 'C', 'older'])
395+
model.setBranches('six', ['A', 'B', 'C', 'four', 'five', 'six'])
396396

397-
expect(model.getBranchesToShow()).toStrictEqual(['A', 'B', 'C'])
397+
expect(model.getBranchesToShow()).toStrictEqual(['six', 'A', 'B', 'C'])
398398
})
399399

400-
it('should persist the branches to show when calling pruneBranchesToShow', () => {
400+
it('should remove outdated selected branches when calling setBranches', () => {
401+
const model = new ExperimentsModel('', buildMockMemento())
402+
403+
model.setSelectedBranches(['A', 'old', 'B', 'C', 'older'])
404+
model.setBranches('main', ['A', 'B', 'C', 'four', 'five', 'six'])
405+
406+
expect(model.getBranchesToShow()).toStrictEqual(['main', 'A', 'B', 'C'])
407+
})
408+
409+
it('should persist the selected branches when calling setBranches', () => {
401410
const memento = buildMockMemento()
402411
const model = new ExperimentsModel('', memento)
403412

404-
model.setBranchesToShow(['A', 'old', 'B', 'C', 'older'])
405-
model.pruneBranchesToShow(['A', 'B', 'C', 'four', 'five', 'six'])
413+
model.setSelectedBranches(['A', 'old', 'B', 'C', 'older'])
414+
model.setBranches('main', ['A', 'B', 'C', 'four', 'five', 'six'])
406415

407416
expect(memento.get(PersistenceKey.EXPERIMENTS_BRANCHES)).toStrictEqual([
408417
'A',
@@ -411,6 +420,35 @@ describe('ExperimentsModel', () => {
411420
])
412421
})
413422

423+
it('should persist the selected branches when calling setSelectedBranches', () => {
424+
const memento = buildMockMemento()
425+
const model = new ExperimentsModel('', memento)
426+
427+
model.setSelectedBranches(['A', 'old', 'B', 'C', 'older'])
428+
429+
expect(memento.get(PersistenceKey.EXPERIMENTS_BRANCHES)).toStrictEqual([
430+
'A',
431+
'old',
432+
'B',
433+
'C',
434+
'older'
435+
])
436+
})
437+
438+
it('should remove the current branch from the selected branches and only persist the selected branches', () => {
439+
const memento = buildMockMemento()
440+
const model = new ExperimentsModel('', memento)
441+
442+
model.setSelectedBranches(['A', 'old', 'B', 'C', 'older'])
443+
model.setBranches('A', ['A', 'B', 'C', 'four', 'five', 'six'])
444+
445+
expect(model.getBranchesToShow()).toStrictEqual(['A', 'B', 'C'])
446+
expect(memento.get(PersistenceKey.EXPERIMENTS_BRANCHES)).toStrictEqual([
447+
'B',
448+
'C'
449+
])
450+
})
451+
414452
const getSelectedRevisions = (
415453
model: ExperimentsModel
416454
): { id: string; displayColor: string }[] =>

extension/src/experiments/model/index.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ export class ExperimentsModel extends ModelWithPersistence {
6262
private coloredStatus: ColoredStatus
6363
private starredExperiments: StarredExperiments
6464
private numberOfCommitsToShow: Record<string, number>
65-
private branchesToShow: string[] = []
65+
private currentBranch: string | undefined
66+
private selectedBranches: string[] = []
6667
private availableBranchesToShow: string[] = []
6768
private hasMoreCommits: { [branch: string]: boolean } = {}
6869
private isShowingMoreCommits: { [branch: string]: boolean } = {}
@@ -102,7 +103,7 @@ export class ExperimentsModel extends ModelWithPersistence {
102103
this.numberOfCommitsToShow = {}
103104
}
104105

105-
this.branchesToShow = this.revive<string[]>(
106+
this.selectedBranches = this.revive<string[]>(
106107
PersistenceKey.EXPERIMENTS_BRANCHES,
107108
[]
108109
)
@@ -463,24 +464,24 @@ export class ExperimentsModel extends ModelWithPersistence {
463464
return this.numberOfCommitsToShow
464465
}
465466

466-
public setBranchesToShow(branches: string[]) {
467-
this.branchesToShow = branches
467+
public setBranches(currentBranch: string, allBranches: string[]) {
468+
this.availableBranchesToShow = allBranches
469+
this.currentBranch = currentBranch
470+
this.selectedBranches = this.selectedBranches.filter(
471+
branch => allBranches.includes(branch) && branch !== this.currentBranch
472+
)
468473
this.persistBranchesToShow()
469474
}
470475

471-
public pruneBranchesToShow(branches: string[]) {
472-
this.branchesToShow = this.branchesToShow.filter(branch =>
473-
branches.includes(branch)
476+
public setSelectedBranches(branches: string[]) {
477+
this.selectedBranches = branches.filter(
478+
branch => branch !== this.currentBranch
474479
)
475480
this.persistBranchesToShow()
476481
}
477482

478483
public getBranchesToShow() {
479-
return this.branchesToShow
480-
}
481-
482-
public setAvailableBranchesToShow(branches: string[]) {
483-
this.availableBranchesToShow = branches
484+
return [this.currentBranch as string, ...this.selectedBranches]
484485
}
485486

486487
public getAvailableBranchesToShow() {
@@ -588,7 +589,7 @@ export class ExperimentsModel extends ModelWithPersistence {
588589
private persistBranchesToShow() {
589590
return this.persist(
590591
PersistenceKey.EXPERIMENTS_BRANCHES,
591-
this.branchesToShow
592+
this.selectedBranches
592593
)
593594
}
594595

extension/src/experiments/webview/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export class WebviewMessages {
225225
undefined,
226226
undefined
227227
)
228-
this.experiments.setBranchesToShow(selectedBranches)
228+
this.experiments.setSelectedBranches(selectedBranches)
229229
await this.update()
230230
}
231231

extension/src/test/suite/experiments/data/index.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ suite('Experiments Data Test Suite', () => {
105105
getNbOfCommitsToShow: () => ({
106106
main: DEFAULT_NUM_OF_COMMITS_TO_SHOW
107107
}),
108-
pruneBranchesToShow: stub(),
109-
setAvailableBranchesToShow: stub(),
110-
setBranchesToShow: stub()
108+
setBranches: stub()
111109
} as unknown as ExperimentsModel
112110
)
113111
)
@@ -168,9 +166,7 @@ suite('Experiments Data Test Suite', () => {
168166
getNbOfCommitsToShow: () => ({
169167
main: DEFAULT_NUM_OF_COMMITS_TO_SHOW
170168
}),
171-
pruneBranchesToShow: stub(),
172-
setAvailableBranchesToShow: stub(),
173-
setBranchesToShow: stub()
169+
setBranches: stub()
174170
} as unknown as ExperimentsModel
175171
)
176172
)
@@ -202,23 +198,21 @@ suite('Experiments Data Test Suite', () => {
202198
'secret-branch',
203199
'old-branch'
204200
]
205-
const { data, mockPruneBranchesToShow, mockGetBranchesToShow } =
201+
const { data, mockSetBranches, mockGetBranchesToShow } =
206202
buildExperimentsData(disposable)
207203
mockGetBranchesToShow.returns(branchesToShow)
208204

209205
await data.update()
210206

211-
expect(mockPruneBranchesToShow).to.be.calledOnce
207+
expect(mockSetBranches).to.be.calledOnce
212208
})
213209

214-
it('should add the current branch to the exp show output', async () => {
210+
it('should get the required commits from the git log output', async () => {
215211
stub(ExperimentsData.prototype, 'managedUpdate').resolves()
216-
const { data, mockExpShow, mockGetBranchesToShow } = buildExperimentsData(
217-
disposable,
218-
'branch-283498'
219-
)
212+
const { data, mockExpShow, mockGetBranchesToShow } =
213+
buildExperimentsData(disposable)
220214

221-
mockGetBranchesToShow.returns([])
215+
mockGetBranchesToShow.returns(['main'])
222216

223217
await data.update()
224218

extension/src/test/suite/experiments/index.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,10 +1451,13 @@ suite('Experiments Test Suite', () => {
14511451
mockUpdateExperimentsData,
14521452
mockSelectBranches
14531453
} = setupExperimentsAndMockCommands()
1454-
const mockSetBranchesToShow = stub(experimentsModel, 'setBranchesToShow')
1454+
const mockSetSelectedBranches = stub(
1455+
experimentsModel,
1456+
'setSelectedBranches'
1457+
)
14551458

14561459
const waitForBranchesToBeSelected = new Promise(resolve =>
1457-
mockSetBranchesToShow.callsFake(() => resolve(undefined))
1460+
mockSetSelectedBranches.callsFake(() => resolve(undefined))
14581461
)
14591462

14601463
const webview = await experiments.showWebview()
@@ -1469,7 +1472,7 @@ suite('Experiments Test Suite', () => {
14691472

14701473
await waitForBranchesToBeSelected
14711474

1472-
expect(mockSetBranchesToShow).to.be.calledOnceWith(['main', 'other'])
1475+
expect(mockSetSelectedBranches).to.be.calledOnceWith(['main', 'other'])
14731476

14741477
expect(mockUpdateExperimentsData).to.be.calledOnce
14751478
}).timeout(WEBVIEW_TEST_TIMEOUT)
@@ -1506,7 +1509,7 @@ suite('Experiments Test Suite', () => {
15061509
mockUpdateExperimentsData,
15071510
mockSelectBranches
15081511
} = setupExperimentsAndMockCommands()
1509-
const mockSetBranchesToShow = stub(experimentsModel, 'setBranchesToShow')
1512+
const mockSetBranches = stub(experimentsModel, 'setBranches')
15101513
mockSelectBranches.resolves(undefined)
15111514

15121515
const webview = await experiments.showWebview()
@@ -1519,7 +1522,10 @@ suite('Experiments Test Suite', () => {
15191522

15201523
expect(mockSelectBranches).to.be.calledOnce
15211524

1522-
expect(mockSetBranchesToShow).not.to.be.calledOnceWith(['main', 'other'])
1525+
expect(mockSetBranches).not.to.be.calledOnceWith('main', [
1526+
'main',
1527+
'other'
1528+
])
15231529

15241530
expect(mockUpdateExperimentsData).not.to.be.calledOnce
15251531
}).timeout(WEBVIEW_TEST_TIMEOUT)

extension/src/test/suite/experiments/util.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,12 @@ export const buildExperimentsData = (
199199
stub(gitReader, 'getNumCommits').resolves(404)
200200

201201
const mockGetBranchesToShow = stub().returns(['main'])
202-
const mockPruneBranchesToShow = stub()
202+
const mockSetBranches = stub()
203203
const data = disposer.track(
204204
new ExperimentsData(dvcDemoPath, internalCommands, {
205205
getBranchesToShow: mockGetBranchesToShow,
206206
getNbOfCommitsToShow: () => DEFAULT_NUM_OF_COMMITS_TO_SHOW,
207-
pruneBranchesToShow: mockPruneBranchesToShow,
208-
setAvailableBranchesToShow: stub(),
209-
setBranchesToShow: stub()
207+
setBranches: mockSetBranches
210208
} as unknown as ExperimentsModel)
211209
)
212210

@@ -215,7 +213,7 @@ export const buildExperimentsData = (
215213
mockCreateFileSystemWatcher,
216214
mockExpShow,
217215
mockGetBranchesToShow,
218-
mockPruneBranchesToShow
216+
mockSetBranches
219217
}
220218
}
221219

extension/src/test/suite/util.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,10 @@ export const buildMockExperimentsData = (update = stub()) =>
183183
({
184184
dispose: stub(),
185185
dvcYamlChanged: stub(),
186-
getCurrentBranchesToShow: () => ['current'],
186+
getBranchesToShow: () => ['main'],
187187
onDidChangeDvcYaml: stub(),
188188
onDidUpdate: stub(),
189-
pruneBranchesToShow: stub(),
190-
setCurrentBranchesToShow: stub(),
189+
setBranches: stub(),
191190
update
192191
} as unknown as ExperimentsData)
193192

0 commit comments

Comments
 (0)