Skip to content

Commit 8636f80

Browse files
authored
Only pull selected files if they include the invoked path (#1963)
1 parent eee25bc commit 8636f80

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

extension/src/fileSystem/tree.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,7 @@ export class TrackedExplorerTree
258258

259259
private tryThenForce(commandId: CommandId) {
260260
return async (pathItem: PathItem) => {
261-
const selected = collectSelected([
262-
...this.getSelectedPathItems(),
263-
pathItem
264-
])
261+
const selected = collectSelected(pathItem, this.getSelectedPathItems())
265262

266263
for (const [dvcRoot, pathItems] of Object.entries(selected)) {
267264
const tracked = []

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

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,42 +214,62 @@ describe('collectSelected', () => {
214214
resourceUri: makeUri('logs', 'loss.tsv')
215215
}
216216

217-
it('should return an empty object if no path items are provided', () => {
218-
expect(collectSelected([])).toStrictEqual({})
217+
it('should return the original item if no other items are selected', () => {
218+
const selected = collectSelected(logsPathItem, [])
219+
220+
expect(selected).toStrictEqual({
221+
[dvcDemoPath]: [logsPathItem]
222+
})
219223
})
220224

221-
it('should return the original item if only one is provided', () => {
222-
const selected = collectSelected([logsPathItem])
225+
it('should return only the invoked item if it is not included in the selected paths', () => {
226+
const selected = collectSelected(lossPathItem, [accPathItem])
223227

224228
expect(selected).toStrictEqual({
225-
[dvcDemoPath]: [logsPathItem]
229+
[dvcDemoPath]: [lossPathItem]
226230
})
227231
})
228232

229233
it('should return a root given it is select', () => {
230-
const selected = collectSelected([dvcDemoPath, logsPathItem, accPathItem])
234+
const selected = collectSelected(logsPathItem, [
235+
dvcDemoPath,
236+
logsPathItem,
237+
accPathItem
238+
])
231239

232240
expect(selected).toStrictEqual({
233241
[dvcDemoPath]: [dvcDemoPath]
234242
})
235243
})
236244

237245
it('should return siblings if a parent is not provided', () => {
238-
const selected = collectSelected([accPathItem, lossPathItem])
246+
const selected = collectSelected(lossPathItem, [accPathItem, lossPathItem])
239247

240248
expect(selected).toStrictEqual({
241249
[dvcDemoPath]: [accPathItem, lossPathItem]
242250
})
243251
})
244252

245253
it('should exclude all children from the final list', () => {
246-
const selected = collectSelected([lossPathItem, accPathItem, logsPathItem])
254+
const selected = collectSelected(logsPathItem, [
255+
lossPathItem,
256+
accPathItem,
257+
logsPathItem
258+
])
247259

248260
expect(selected).toStrictEqual({
249261
[dvcDemoPath]: [logsPathItem]
250262
})
251263
})
252264

265+
it('should not exclude children from the final list if the invoked item is not in the selected list', () => {
266+
const selected = collectSelected(accPathItem, [lossPathItem, logsPathItem])
267+
268+
expect(selected).toStrictEqual({
269+
[dvcDemoPath]: [accPathItem]
270+
})
271+
})
272+
253273
it('should return multiple entries when multiple roots are provided', () => {
254274
const mockOtherRepoItem = {
255275
dvcRoot: __dirname,
@@ -258,7 +278,7 @@ describe('collectSelected', () => {
258278
resourceUri: Uri.file(join(__dirname, 'mock', 'path'))
259279
}
260280

261-
const selected = collectSelected([
281+
const selected = collectSelected(logsPathItem, [
262282
mockOtherRepoItem,
263283
{
264284
dvcRoot: dvcDemoPath,
@@ -274,4 +294,27 @@ describe('collectSelected', () => {
274294
[dvcDemoPath]: [logsPathItem]
275295
})
276296
})
297+
298+
it('should only return the invoked item if multiple roots are provided but the invoked item is not selected', () => {
299+
const mockOtherRepoItem = {
300+
dvcRoot: __dirname,
301+
isDirectory: true,
302+
isTracked: true,
303+
resourceUri: Uri.file(join(__dirname, 'mock', 'path'))
304+
}
305+
306+
const selected = collectSelected(logsPathItem, [
307+
mockOtherRepoItem,
308+
{
309+
dvcRoot: dvcDemoPath,
310+
isDirectory: false,
311+
isTracked: true,
312+
resourceUri: makeUri('logs', 'acc.tsv')
313+
}
314+
])
315+
316+
expect(selected).toStrictEqual({
317+
[dvcDemoPath]: [logsPathItem]
318+
})
319+
})
277320
})

extension/src/repository/model/collect.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,26 @@ const collectRootOrPathItem = (
227227
}
228228

229229
export const collectSelected = (
230-
pathItems: (string | PathItem)[]
230+
invokedPathItem: PathItem,
231+
selectedPathItems: (string | PathItem)[]
231232
): SelectedPathAccumulator => {
232-
const selectedPaths = collectSelectedPaths(pathItems)
233+
const invokedPath = invokedPathItem.resourceUri.fsPath
234+
235+
if (
236+
!selectedPathItems.some(pathItem => {
237+
if (typeof pathItem === 'string') {
238+
return pathItem === invokedPath
239+
}
240+
return pathItem.resourceUri.fsPath === invokedPath
241+
})
242+
) {
243+
return { [invokedPathItem.dvcRoot]: [invokedPathItem] }
244+
}
245+
246+
const selectedPaths = collectSelectedPaths(selectedPathItems)
233247
const acc: SelectedPathAccumulator = {}
234248
const addedPaths = new Set<string>()
235-
for (const pathItem of pathItems) {
249+
for (const pathItem of selectedPathItems) {
236250
collectRootOrPathItem(acc, addedPaths, selectedPaths, pathItem)
237251
}
238252
return acc

0 commit comments

Comments
 (0)