Skip to content

Commit b0fe0c1

Browse files
committed
Refactor checkpoint creation logic to fall back to file copying instead of throwing an error when Git information is unavailable, and standardize logging across context and checkpoint commands using the Logger utility
1 parent 5562bcb commit b0fe0c1

9 files changed

Lines changed: 174 additions & 89 deletions

File tree

‎packages/vscode/src/commands/apply-context-command.ts‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ export async function resolve_glob_patterns(
6969
}
7070
})
7171
} catch (error) {
72-
console.warn(
73-
`Failed to resolve glob pattern "${normalized_pattern}":`,
74-
error
75-
)
72+
Logger.warn({
73+
function_name: 'resolve_glob_patterns',
74+
message: `Failed to resolve glob pattern "${normalized_pattern}"`,
75+
data: error
76+
})
7677
}
7778
}
7879

@@ -127,9 +128,10 @@ async function apply_saved_context(
127128
if (root) {
128129
resolved_path_part = path.join(root, relative_path)
129130
} else {
130-
console.warn(
131-
`Unknown workspace prefix "${prefix}" in path "${path_part}". Treating as relative to current workspace root.`
132-
)
131+
Logger.warn({
132+
function_name: 'apply_saved_context',
133+
message: `Unknown workspace prefix "${prefix}" in path "${path_part}". Treating as relative to current workspace root.`
134+
})
133135
resolved_path_part = path.join(workspace_root, relative_path)
134136
}
135137
} else {
@@ -305,7 +307,11 @@ async function load_and_merge_file_contexts(): Promise<{
305307
}
306308
}
307309
} catch (error: any) {
308-
console.error(`Error reading contexts file from ${folder.name}:`, error)
310+
Logger.error({
311+
function_name: 'load_and_merge_file_contexts',
312+
message: `Error reading contexts file from ${folder.name}`,
313+
data: error
314+
})
309315
}
310316
}
311317

@@ -665,10 +671,11 @@ export function apply_context_command(
665671
error.message
666672
)
667673
)
668-
console.error(
669-
'Error updating context name in file:',
670-
error
671-
)
674+
Logger.error({
675+
function_name: 'apply_context_command',
676+
message: 'Error updating context name in file',
677+
data: error
678+
})
672679
success = false
673680
break
674681
}
@@ -902,10 +909,11 @@ export function apply_context_command(
902909
selected.label
903910
)
904911
)
905-
console.error(
906-
'Could not find selected context after potential edits:',
907-
selected.label
908-
)
912+
Logger.error({
913+
function_name: 'apply_context_command',
914+
message: 'Could not find selected context after potential edits',
915+
data: selected.label
916+
})
909917
return
910918
}
911919

@@ -928,7 +936,11 @@ export function apply_context_command(
928936
error.message
929937
)
930938
)
931-
console.error('Error selecting saved context:', error)
939+
Logger.error({
940+
function_name: 'apply_context_command',
941+
message: 'Error selecting saved context',
942+
data: error
943+
})
932944
}
933945
}
934946
}

‎packages/vscode/src/commands/checkpoints-command/actions/clear-all-checkpoints.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22
import { CHECKPOINTS_STATE_KEY } from '../../../constants/state-keys'
33
import { get_checkpoint_path } from '../utils'
44
import { get_checkpoints } from './get-checkpoints'
5+
import { Logger } from '@shared/utils/logger'
56

67
export const clear_all_checkpoints = async (
78
context: vscode.ExtensionContext
@@ -15,7 +16,11 @@ export const clear_all_checkpoints = async (
1516
recursive: true
1617
})
1718
} catch (error) {
18-
console.warn(`Could not delete checkpoint file: ${error}`)
19+
Logger.warn({
20+
function_name: 'clear_all_checkpoints',
21+
message: 'Could not delete checkpoint file',
22+
data: error
23+
})
1924
}
2025
}
2126

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
is_git_repository
1818
} from '../utils/git-utils'
1919
import * as path from 'path'
20+
import { Logger } from '@shared/utils/logger'
2021

2122
export const create_checkpoint = async (
2223
workspace_provider: WorkspaceProvider,
@@ -37,7 +38,11 @@ export const create_checkpoint = async (
3738
recursive: true
3839
})
3940
} catch (error) {
40-
console.warn(`Could not delete old temporary checkpoint file: ${error}`)
41+
Logger.warn({
42+
function_name: 'create_checkpoint',
43+
message: 'Could not delete old temporary checkpoint file',
44+
data: error
45+
})
4146
}
4247
await context.workspaceState.update(
4348
TEMPORARY_CHECKPOINT_STATE_KEY,
@@ -88,9 +93,28 @@ export const create_checkpoint = async (
8893
Buffer.from(diff, 'utf8')
8994
)
9095
} else {
91-
throw new Error(
92-
`Failed to get git information for repository in ${folder.name}.`
93-
)
96+
Logger.warn({
97+
function_name: 'create_checkpoint',
98+
message: `Failed to get git information for repository in ${folder.name}. Falling back to file copy.`
99+
})
100+
const dest_folder_path =
101+
workspace_folders.length > 1
102+
? path.join(checkpoint_dir_path, folder.name)
103+
: checkpoint_dir_path
104+
const dest_folder_uri = vscode.Uri.file(dest_folder_path)
105+
if (workspace_folders.length > 1) {
106+
await vscode.workspace.fs.createDirectory(dest_folder_uri)
107+
}
108+
109+
const entries = await vscode.workspace.fs.readDirectory(folder.uri)
110+
for (const [name] of entries) {
111+
await copy_optimised_recursively(
112+
vscode.Uri.joinPath(folder.uri, name),
113+
vscode.Uri.joinPath(dest_folder_uri, name),
114+
folder.uri.fsPath,
115+
workspace_provider
116+
)
117+
}
94118
}
95119
} else {
96120
const dest_folder_path =

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
is_git_repository
99
} from '../utils/git-utils'
1010
import * as path from 'path'
11+
import { Logger } from '@shared/utils/logger'
1112

1213
export const create_temporary_checkpoint = async (
1314
workspace_provider: WorkspaceProvider
@@ -47,9 +48,28 @@ export const create_temporary_checkpoint = async (
4748
Buffer.from(diff, 'utf8')
4849
)
4950
} else {
50-
throw new Error(
51-
`Failed to get git information for repository in ${folder.name}.`
52-
)
51+
Logger.warn({
52+
function_name: 'create_temporary_checkpoint',
53+
message: `Failed to get git information for repository in ${folder.name}. Falling back to file copy.`
54+
})
55+
const dest_folder_path =
56+
workspace_folders.length > 1
57+
? path.join(checkpoint_dir_path, folder.name)
58+
: checkpoint_dir_path
59+
const dest_folder_uri = vscode.Uri.file(dest_folder_path)
60+
if (workspace_folders.length > 1) {
61+
await vscode.workspace.fs.createDirectory(dest_folder_uri)
62+
}
63+
64+
const entries = await vscode.workspace.fs.readDirectory(folder.uri)
65+
for (const [name] of entries) {
66+
await copy_optimised_recursively(
67+
vscode.Uri.joinPath(folder.uri, name),
68+
vscode.Uri.joinPath(dest_folder_uri, name),
69+
folder.uri.fsPath,
70+
workspace_provider
71+
)
72+
}
5373
}
5474
} else {
5575
const dest_folder_path =

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22
import { CHECKPOINTS_STATE_KEY } from '../../../constants/state-keys'
33
import type { Checkpoint } from '../types'
44
import { get_checkpoint_path } from '../utils'
5+
import { Logger } from '@shared/utils/logger'
56

67
export const delete_checkpoint = async (params: {
78
context: vscode.ExtensionContext
@@ -28,6 +29,10 @@ export const delete_checkpoint = async (params: {
2829
recursive: true
2930
})
3031
} catch (error) {
31-
console.warn(`Could not delete checkpoint file: ${error}`)
32+
Logger.warn({
33+
function_name: 'delete_checkpoint',
34+
message: 'Could not delete checkpoint file',
35+
data: error
36+
})
3237
}
3338
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { sync_workspace_from_dir } from './sync-workspace-from-dir'
88
import { get_checkpoint_path, sync_directory } from '../utils'
99
import { apply_git_diff } from '../utils/git-utils'
1010
import * as path from 'path'
11+
import { Logger } from '@shared/utils/logger'
1112

1213
export const restore_checkpoint = async (params: {
1314
checkpoint: Checkpoint
@@ -30,9 +31,11 @@ export const restore_checkpoint = async (params: {
3031
recursive: true
3132
})
3233
} catch (error) {
33-
console.warn(
34-
`Could not delete old temporary checkpoint file: ${error}`
35-
)
34+
Logger.warn({
35+
function_name: 'restore_checkpoint',
36+
message: 'Could not delete old temporary checkpoint file',
37+
data: error
38+
})
3639
}
3740
}
3841

@@ -99,7 +102,11 @@ export const restore_checkpoint = async (params: {
99102
throw new Error(`Failed to apply git diff for ${folder_name}`)
100103
}
101104
} catch (err) {
102-
console.error(`Error restoring git checkpoint: ${err}`)
105+
Logger.error({
106+
function_name: 'restore_checkpoint',
107+
message: 'Error restoring git checkpoint',
108+
data: err
109+
})
103110
throw err
104111
}
105112
} else {

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode'
22
import { execSync } from 'child_process'
33
import * as path from 'path'
4-
4+
import { Logger } from '@shared/utils/logger'
55
export interface GitInfo {
66
branch: string
77
commit_hash: string
@@ -85,14 +85,22 @@ export const get_git_diff = async (
8585
// The diff output is in stdout of the error object
8686
untracked_diff += err.stdout.toString()
8787
} else {
88-
console.warn(`Could not create diff for untracked file: ${file}`, err)
88+
Logger.warn({
89+
function_name: 'get_git_diff',
90+
message: `Could not create diff for untracked file: ${file}`,
91+
data: err
92+
})
8993
}
9094
}
9195
}
9296

9397
return diff + untracked_diff
9498
} catch (error) {
95-
console.error('Error getting git diff:', error)
99+
Logger.error({
100+
function_name: 'get_git_diff',
101+
message: 'Error getting git diff',
102+
data: error
103+
})
96104
return null
97105
}
98106
}
@@ -126,7 +134,11 @@ export const apply_git_diff = async (
126134
try {
127135
execSync(`git checkout ${target_branch}`, { cwd, stdio: 'pipe' })
128136
} catch (err) {
129-
console.warn(`Could not checkout branch ${target_branch}`)
137+
Logger.warn({
138+
function_name: 'apply_git_diff',
139+
message: `Could not checkout branch ${target_branch}`,
140+
data: err
141+
})
130142
}
131143
}
132144

@@ -178,7 +190,11 @@ export const apply_git_diff = async (
178190

179191
return true
180192
} catch (error) {
181-
console.error('Error applying git diff:', error)
193+
Logger.error({
194+
function_name: 'apply_git_diff',
195+
message: 'Error applying git diff',
196+
data: error
197+
})
182198
return false
183199
}
184200
}

0 commit comments

Comments
 (0)