Skip to content

Commit 440b7f7

Browse files
committed
Implement progress reporting for checkpoint restoration and directory synchronization operations
1 parent 6f6f5fb commit 440b7f7

4 files changed

Lines changed: 112 additions & 12 deletions

File tree

‎packages/vscode/src/commands/checkpoints-command/actions/create-checkpoint.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ export const create_checkpoint = async (
3030
description?: string
3131
): Promise<Checkpoint | undefined> => {
3232
try {
33-
const operation_in_progress = context.workspaceState.get<boolean>(
33+
const operation_in_progress = context.workspaceState.get<number>(
3434
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY
3535
)
36-
if (operation_in_progress) {
36+
if (
37+
operation_in_progress &&
38+
Date.now() - operation_in_progress < 60 * 1000
39+
) {
3740
vscode.window.showWarningMessage(
3841
dictionary.warning_message.CHECKPOINT_OPERATION_IN_PROGRESS
3942
)
@@ -76,7 +79,7 @@ export const create_checkpoint = async (
7679
const create_checkpoint_task = async () => {
7780
await context.workspaceState.update(
7881
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
79-
true
82+
Date.now()
8083
)
8184
await vscode.workspace.saveAll()
8285
const checkpoint_dir_path = get_checkpoint_path(timestamp)
@@ -177,7 +180,7 @@ export const create_checkpoint = async (
177180
await panel_provider.send_checkpoints()
178181
await context.workspaceState.update(
179182
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
180-
false
183+
undefined
181184
)
182185
new_checkpoint = checkpoint_object
183186
}
@@ -201,7 +204,7 @@ export const create_checkpoint = async (
201204
)
202205
await context.workspaceState.update(
203206
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
204-
false
207+
undefined
205208
)
206209
return undefined
207210
}

‎packages/vscode/src/commands/checkpoints-command/actions/restore-checkpoint.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export const restore_checkpoint = async (params: {
126126
: 'Restoring checkpoint...',
127127
cancellable: false
128128
},
129-
async () => {
129+
async (progress) => {
130130
const checkpoint_dir_path = get_checkpoint_path(
131131
params.checkpoint.timestamp
132132
)
@@ -235,7 +235,8 @@ export const restore_checkpoint = async (params: {
235235
source_dir: temp_git_dir_uri,
236236
dest_dir: folder.uri,
237237
root_path: folder.uri.fsPath,
238-
workspace_provider: params.workspace_provider
238+
workspace_provider: params.workspace_provider,
239+
progress
239240
})
240241
} catch (err) {
241242
Logger.error({
@@ -265,15 +266,17 @@ export const restore_checkpoint = async (params: {
265266
source_dir: source_folder_uri,
266267
dest_dir: folder.uri,
267268
root_path: folder.uri.fsPath,
268-
workspace_provider: params.workspace_provider
269+
workspace_provider: params.workspace_provider,
270+
progress
269271
})
270272
} catch {}
271273
}
272274
}
273275
} else {
274276
await sync_workspace_from_dir({
275277
source_dir_uri: checkpoint_dir_uri,
276-
workspace_provider: params.workspace_provider
278+
workspace_provider: params.workspace_provider,
279+
progress
277280
})
278281
}
279282
}

‎packages/vscode/src/commands/checkpoints-command/actions/sync-workspace-from-dir.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { sync_directory } from '../utils'
55
export const sync_workspace_from_dir = async (params: {
66
source_dir_uri: vscode.Uri
77
workspace_provider: WorkspaceProvider
8+
progress?: vscode.Progress<{ message?: string; increment?: number }>
89
}) => {
910
const workspace_folders = vscode.workspace.workspaceFolders!
1011

@@ -24,7 +25,8 @@ export const sync_workspace_from_dir = async (params: {
2425
source_dir: source_folder_uri,
2526
dest_dir: folder.uri,
2627
root_path: folder.uri.fsPath,
27-
workspace_provider: params.workspace_provider
28+
workspace_provider: params.workspace_provider,
29+
progress: params.progress
2830
})
2931
} else {
3032
}
@@ -34,7 +36,8 @@ export const sync_workspace_from_dir = async (params: {
3436
source_dir: params.source_dir_uri,
3537
dest_dir: workspace_folders[0].uri,
3638
root_path: workspace_folders[0].uri.fsPath,
37-
workspace_provider: params.workspace_provider
39+
workspace_provider: params.workspace_provider,
40+
progress: params.progress
3841
})
3942
}
4043
}

‎packages/vscode/src/commands/checkpoints-command/utils/sync-directory.ts‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,77 @@ import * as vscode from 'vscode'
22
import * as path from 'path'
33
import { WorkspaceProvider } from '@/context/providers/workspace-provider'
44

5+
async function count_sync_operations(params: {
6+
source_dir: vscode.Uri
7+
dest_dir: vscode.Uri
8+
root_path: string
9+
workspace_provider: WorkspaceProvider
10+
}): Promise<number> {
11+
let count = 0
12+
13+
try {
14+
await vscode.workspace.fs.stat(params.dest_dir)
15+
} catch (e) {
16+
return 1 // It will be one copy operation
17+
}
18+
19+
const dest_entries = await vscode.workspace.fs.readDirectory(params.dest_dir)
20+
const source_entries = await vscode.workspace.fs.readDirectory(
21+
params.source_dir
22+
)
23+
const source_map = new Map(source_entries)
24+
25+
// Count deletions
26+
for (const [name] of dest_entries) {
27+
if (!source_map.has(name)) {
28+
const dest_uri = vscode.Uri.joinPath(params.dest_dir, name)
29+
const relative_path = path.relative(params.root_path, dest_uri.fsPath)
30+
31+
if (
32+
params.workspace_provider.is_excluded(relative_path) ||
33+
params.workspace_provider.is_ignored_by_patterns(dest_uri.fsPath)
34+
) {
35+
continue
36+
}
37+
count++
38+
}
39+
}
40+
41+
// Count copies
42+
for (const [name, type] of source_entries) {
43+
const source_uri = vscode.Uri.joinPath(params.source_dir, name)
44+
const dest_uri = vscode.Uri.joinPath(params.dest_dir, name)
45+
46+
let dest_stat: vscode.FileStat | undefined
47+
try {
48+
dest_stat = await vscode.workspace.fs.stat(dest_uri)
49+
} catch {}
50+
51+
if (type === vscode.FileType.Directory) {
52+
if (dest_stat?.type === vscode.FileType.Directory) {
53+
count += await count_sync_operations({
54+
...params,
55+
source_dir: source_uri,
56+
dest_dir: dest_uri
57+
})
58+
} else {
59+
count++ // delete whatever is at dest, then copy dir
60+
}
61+
} else if (type === vscode.FileType.File) {
62+
count++ // copy file
63+
}
64+
}
65+
66+
return count
67+
}
68+
569
export const sync_directory = async (params: {
670
source_dir: vscode.Uri
771
dest_dir: vscode.Uri
872
root_path: string
973
workspace_provider: WorkspaceProvider
74+
progress?: vscode.Progress<{ message?: string; increment?: number }>
75+
_increment?: number
1076
}) => {
1177
let dest_entries: [string, vscode.FileType][]
1278
try {
@@ -15,6 +81,9 @@ export const sync_directory = async (params: {
1581
await vscode.workspace.fs.copy(params.source_dir, params.dest_dir, {
1682
overwrite: true
1783
})
84+
if (params.progress && params._increment) {
85+
params.progress.report({ increment: params._increment })
86+
}
1887
return
1988
}
2089

@@ -23,6 +92,15 @@ export const sync_directory = async (params: {
2392
)
2493
const source_map = new Map(source_entries)
2594

95+
if (params.progress && params._increment === undefined) {
96+
const total_ops = await count_sync_operations(params)
97+
if (total_ops > 0) {
98+
params = { ...params, _increment: 100 / total_ops }
99+
} else {
100+
params = { ...params, _increment: 0 }
101+
}
102+
}
103+
26104
for (const [name] of dest_entries) {
27105
if (!source_map.has(name)) {
28106
const dest_uri = vscode.Uri.joinPath(params.dest_dir, name)
@@ -36,6 +114,9 @@ export const sync_directory = async (params: {
36114
}
37115

38116
await vscode.workspace.fs.delete(dest_uri, { recursive: true })
117+
if (params.progress && params._increment) {
118+
params.progress.report({ increment: params._increment })
119+
}
39120
}
40121
}
41122

@@ -55,7 +136,8 @@ export const sync_directory = async (params: {
55136
await sync_directory({
56137
...params,
57138
source_dir: source_uri,
58-
dest_dir: dest_uri
139+
dest_dir: dest_uri,
140+
_increment: params._increment
59141
})
60142
} else {
61143
if (dest_stat) {
@@ -66,13 +148,19 @@ export const sync_directory = async (params: {
66148
await vscode.workspace.fs.copy(source_uri, dest_uri, {
67149
overwrite: true
68150
})
151+
if (params.progress && params._increment) {
152+
params.progress.report({ increment: params._increment })
153+
}
69154
}
70155
} else if (type == vscode.FileType.File) {
71156
if (dest_stat?.type == vscode.FileType.File) {
72157
if (source_stat.mtime != dest_stat.mtime) {
73158
await vscode.workspace.fs.copy(source_uri, dest_uri, {
74159
overwrite: true
75160
})
161+
if (params.progress && params._increment) {
162+
params.progress.report({ increment: params._increment })
163+
}
76164
}
77165
} else {
78166
if (dest_stat) {
@@ -83,6 +171,9 @@ export const sync_directory = async (params: {
83171
await vscode.workspace.fs.copy(source_uri, dest_uri, {
84172
overwrite: true
85173
})
174+
if (params.progress && params._increment) {
175+
params.progress.report({ increment: params._increment })
176+
}
86177
}
87178
}
88179
}

0 commit comments

Comments
 (0)