|
1 | | -import { dirname, join, resolve } from 'path' |
| 1 | +import { dirname, join, relative, resolve } from 'path' |
2 | 2 | import { Uri } from 'vscode' |
3 | 3 | import { Resource } from '../commands' |
4 | 4 | import { addToMapSet } from '../../util/map' |
5 | | -import { PathOutput } from '../../cli/reader' |
6 | | -import { isSameOrChild } from '../../fileSystem' |
| 5 | +import { ExperimentsOutput, PathOutput } from '../../cli/reader' |
| 6 | +import { isSameOrChild, relativeWithUri } from '../../fileSystem' |
7 | 7 | import { getDirectChild, getPath, getPathArray } from '../../fileSystem/util' |
8 | 8 |
|
9 | 9 | export type PathItem = Resource & { |
10 | 10 | isDirectory: boolean |
11 | 11 | isTracked: boolean |
12 | 12 | } |
13 | 13 |
|
14 | | -const transform = ( |
| 14 | +const transformToAbsTree = ( |
15 | 15 | dvcRoot: string, |
16 | 16 | acc: Map<string, Set<string>>, |
17 | | - isTracked: Set<string> |
| 17 | + trackedRelPaths: Set<string> |
18 | 18 | ): Map<string, PathItem[]> => { |
19 | | - const treeMap = new Map<string, PathItem[]>() |
| 19 | + const absTree = new Map<string, PathItem[]>() |
20 | 20 |
|
21 | | - for (const [path, paths] of acc.entries()) { |
22 | | - const items = [...paths].map(path => ({ |
| 21 | + for (const [path, childPaths] of acc.entries()) { |
| 22 | + const items = [...childPaths].map(childPath => ({ |
23 | 23 | dvcRoot, |
24 | | - isDirectory: !!acc.get(path), |
25 | | - isTracked: isTracked.has(path), |
26 | | - resourceUri: Uri.file(join(dvcRoot, path)) |
| 24 | + isDirectory: !!acc.get(childPath), |
| 25 | + isTracked: trackedRelPaths.has(childPath), |
| 26 | + resourceUri: Uri.file(join(dvcRoot, childPath)) |
27 | 27 | })) |
28 | 28 | const absPath = Uri.file(join(dvcRoot, path)).fsPath |
29 | | - treeMap.set(absPath, items) |
| 29 | + absTree.set(absPath, items) |
30 | 30 | } |
31 | 31 |
|
32 | | - return treeMap |
| 32 | + return absTree |
33 | 33 | } |
34 | 34 |
|
35 | 35 | export const collectTree = ( |
36 | 36 | dvcRoot: string, |
37 | | - paths: string[] |
| 37 | + absLeafs: Set<string>, |
| 38 | + trackedRelPaths = new Set<string>() |
38 | 39 | ): Map<string, PathItem[]> => { |
39 | | - const acc = new Map<string, Set<string>>() |
40 | | - const isTracked = new Set<string>() |
| 40 | + const relTree = new Map<string, Set<string>>() |
41 | 41 |
|
42 | | - for (const path of paths) { |
43 | | - const pathArray = getPathArray(path) |
| 42 | + for (const absLeaf of absLeafs) { |
| 43 | + const relPath = relative(dvcRoot, absLeaf) |
| 44 | + const relPathArray = getPathArray(relPath) |
44 | 45 |
|
45 | | - isTracked.add(path) |
46 | | - const dir = dirname(path) |
47 | | - if (dir !== '.') { |
48 | | - isTracked.add(dir) |
49 | | - } |
| 46 | + trackedRelPaths.add(relPath) |
50 | 47 |
|
51 | | - for (let idx = 0; idx < pathArray.length; idx++) { |
52 | | - const path = getPath(pathArray, idx) |
53 | | - addToMapSet(acc, path, getDirectChild(pathArray, idx)) |
| 48 | + for (let idx = 0; idx < relPathArray.length; idx++) { |
| 49 | + const path = getPath(relPathArray, idx) |
| 50 | + addToMapSet(relTree, path, getDirectChild(relPathArray, idx)) |
54 | 51 | } |
55 | 52 | } |
56 | 53 |
|
57 | | - return transform(dvcRoot, acc, isTracked) |
| 54 | + return transformToAbsTree(dvcRoot, relTree, trackedRelPaths) |
58 | 55 | } |
59 | 56 |
|
60 | 57 | const collectMissingParents = (acc: string[], absPath: string) => { |
@@ -94,28 +91,62 @@ export const collectModifiedAgainstHead = ( |
94 | 91 | return acc |
95 | 92 | } |
96 | 93 |
|
97 | | -const collectPath = (acc: Set<string>, dvcRoot: string, path: string) => { |
98 | | - const pathArray = getPathArray(path) |
| 94 | +const collectAbsPath = ( |
| 95 | + acc: Set<string>, |
| 96 | + absLeafs: Set<string>, |
| 97 | + dvcRoot: string, |
| 98 | + absPath: string |
| 99 | +) => { |
| 100 | + const relPathArray = getPathArray(relative(dvcRoot, absPath)) |
99 | 101 |
|
100 | | - for (let reverseIdx = pathArray.length; reverseIdx > 0; reverseIdx--) { |
101 | | - const path = join(dvcRoot, getPath(pathArray, reverseIdx)) |
102 | | - if (acc.has(path)) { |
| 102 | + for (let reverseIdx = relPathArray.length; reverseIdx > 0; reverseIdx--) { |
| 103 | + const absPath = join(dvcRoot, getPath(relPathArray, reverseIdx)) |
| 104 | + if (acc.has(absPath) || absLeafs.has(absPath)) { |
103 | 105 | continue |
104 | 106 | } |
105 | 107 |
|
106 | | - acc.add(path) |
| 108 | + acc.add(absPath) |
107 | 109 | } |
108 | 110 | } |
109 | 111 |
|
110 | | -export const collectTracked = ( |
| 112 | +export const collectTrackedNonLeafs = ( |
111 | 113 | dvcRoot: string, |
112 | | - paths: string[] = [] |
| 114 | + absLeafs = new Set<string>() |
113 | 115 | ): Set<string> => { |
114 | 116 | const acc = new Set<string>() |
115 | 117 |
|
116 | | - for (const path of paths) { |
117 | | - collectPath(acc, dvcRoot, path) |
| 118 | + for (const absPath of absLeafs) { |
| 119 | + collectAbsPath(acc, absLeafs, dvcRoot, absPath) |
| 120 | + } |
| 121 | + |
| 122 | + return acc |
| 123 | +} |
| 124 | + |
| 125 | +export const collectTrackedOuts = (data: ExperimentsOutput): Set<string> => { |
| 126 | + const acc = new Set<string>() |
| 127 | + for (const [relPath, { use_cache }] of Object.entries( |
| 128 | + data.workspace.baseline.data?.outs || {} |
| 129 | + )) { |
| 130 | + if (use_cache) { |
| 131 | + acc.add(relPath) |
| 132 | + } |
118 | 133 | } |
| 134 | + return acc |
| 135 | +} |
119 | 136 |
|
| 137 | +export const collectTrackedPaths = async ( |
| 138 | + { dvcRoot, resourceUri, isTracked }: PathItem, |
| 139 | + getChildren: (path: string) => Promise<PathItem[]> |
| 140 | +): Promise<string[]> => { |
| 141 | + const acc = [] |
| 142 | + |
| 143 | + if (isTracked) { |
| 144 | + acc.push(relativeWithUri(dvcRoot, resourceUri)) |
| 145 | + return acc |
| 146 | + } |
| 147 | + const children = await getChildren(resourceUri.fsPath) |
| 148 | + for (const child of children) { |
| 149 | + acc.push(...(await collectTrackedPaths(child, getChildren))) |
| 150 | + } |
120 | 151 | return acc |
121 | 152 | } |
0 commit comments