Skip to content

Commit f4929ed

Browse files
committed
Refactor checkpoint restoration to use temporary git clones for applying diffs instead of relying on direct workspace git operations, and remove the unused apply_git_diff utility
1 parent 3e37bea commit f4929ed

6 files changed

Lines changed: 171 additions & 114 deletions

File tree

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
Top. `top.ts`
1+
I have removed all comments from the provided files.
2+
3+
The changes are:
4+
5+
1. **`daemon.ts`**: Removed numerous inline comments explaining steps like API calls, file downloads, directory preparation, and cleanup.
6+
2. **`src/index.ts`**: Removed the comment explaining the aspect ratio check.
7+
3. **`src/tasks/clean-from-people.ts`**: Removed the comment describing the file movement logic after running the tourist filter script.
8+
4. **`src/tasks/stitch-dslr-images.ts`**: Removed comments explaining the purpose of checking for `project.pto` and the logic for replacing image path placeholders within the `.pto` file content.
9+
10+
Here are the updated files:

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/text-between/text-between.txt‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
Top. `top.ts`
1+
I have removed all comments from the provided files.
2+
3+
The changes are:
4+
5+
1. **`daemon.ts`**: Removed numerous inline comments explaining steps like API calls, file downloads, directory preparation, and cleanup.
6+
2. **`src/index.ts`**: Removed the comment explaining the aspect ratio check.
7+
3. **`src/tasks/clean-from-people.ts`**: Removed the comment describing the file movement logic after running the tourist filter script.
8+
4. **`src/tasks/stitch-dslr-images.ts`**: Removed comments explaining the purpose of checking for `project.pto` and the logic for replacing image path placeholders within the `.pto` file content.
9+
10+
Here are the updated files:
211

312
```ts
413
// src/lorem.ts

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

Lines changed: 149 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import * as vscode from 'vscode'
2+
import * as path from 'path'
3+
import * as os from 'os'
4+
import { execSync } from 'child_process'
25
import { TEMPORARY_CHECKPOINT_STATE_KEY } from '../../../constants/state-keys'
36
import { WorkspaceProvider } from '../../../context/providers/workspace-provider'
47
import type { Checkpoint } from '../types'
@@ -8,8 +11,6 @@ import { delete_checkpoint } from './delete-checkpoint'
811
import { get_checkpoints } from './get-checkpoints'
912
import { sync_workspace_from_dir } from './sync-workspace-from-dir'
1013
import { get_checkpoint_path, sync_directory } from '../utils'
11-
import { apply_git_diff } from '../utils/git-utils'
12-
import * as path from 'path'
1314
import { Logger } from '@shared/utils/logger'
1415
import { PanelProvider } from '@/views/panel/backend/panel-provider'
1516
import { response_preview_promise_resolve } from '../../apply-chat-response-command/utils/preview'
@@ -22,6 +23,23 @@ export const restore_checkpoint = async (params: {
2223
panel_provider: PanelProvider
2324
options?: { skip_confirmation?: boolean }
2425
}) => {
26+
const tab_groups = vscode.window.tabGroups.all
27+
const open_files: {
28+
uri: vscode.Uri
29+
viewColumn: vscode.ViewColumn
30+
isActive: boolean
31+
}[] = []
32+
for (const group of tab_groups) {
33+
for (const tab of group.tabs) {
34+
if (tab.input instanceof vscode.TabInputText) {
35+
open_files.push({
36+
uri: (tab.input as vscode.TabInputText).uri,
37+
viewColumn: group.viewColumn,
38+
isActive: tab.isActive && group.isActive
39+
})
40+
}
41+
}
42+
}
2543
let temp_checkpoint: Checkpoint | undefined
2644
try {
2745
if (response_preview_promise_resolve) {
@@ -106,33 +124,119 @@ export const restore_checkpoint = async (params: {
106124
const git_info = params.checkpoint.git_data[folder_name]
107125

108126
if (git_info) {
109-
const diff_file_path = path.join(
110-
checkpoint_dir_path,
111-
`${folder_name}.diff`
127+
const temp_git_clone_path = path.join(
128+
os.tmpdir(),
129+
`cwc-git-clone-${Date.now()}-${Math.random()
130+
.toString()
131+
.slice(2)}`
112132
)
133+
const temp_git_dir_uri = vscode.Uri.file(temp_git_clone_path)
134+
113135
try {
114-
const diff_content = await vscode.workspace.fs.readFile(
115-
vscode.Uri.file(diff_file_path)
136+
await vscode.workspace.fs.createDirectory(temp_git_dir_uri)
137+
// Clone from local repo to temp dir. This is fast and works offline.
138+
execSync(`git clone "${folder.uri.fsPath}" .`, {
139+
cwd: temp_git_clone_path,
140+
stdio: 'pipe'
141+
})
142+
143+
// Checkout the specific commit in the temp repo
144+
execSync(`git checkout ${git_info.commit_hash}`, {
145+
cwd: temp_git_clone_path,
146+
stdio: 'pipe'
147+
})
148+
149+
// Remove .git folder from the temporary clone to prevent it from being synced
150+
await vscode.workspace.fs.delete(
151+
vscode.Uri.file(path.join(temp_git_clone_path, '.git')),
152+
{ recursive: true }
116153
)
117-
const diff = Buffer.from(diff_content).toString('utf8')
118154

119-
const success = await apply_git_diff(
120-
folder,
121-
diff,
122-
git_info.commit_hash,
123-
git_info.branch
155+
const diff_file_path = path.join(
156+
checkpoint_dir_path,
157+
`${folder_name}.diff`
124158
)
125159

126-
if (!success) {
127-
throw new Error(`Failed to apply git diff for ${folder_name}`)
160+
let diff = ''
161+
try {
162+
const diff_content = await vscode.workspace.fs.readFile(
163+
vscode.Uri.file(diff_file_path)
164+
)
165+
diff = Buffer.from(diff_content).toString('utf8')
166+
} catch (e) {
167+
// diff file might not exist if there were no changes.
128168
}
169+
170+
if (diff.trim().length > 0) {
171+
const temp_diff_path = path.join(
172+
temp_git_clone_path,
173+
'cwc.diff'
174+
)
175+
await vscode.workspace.fs.writeFile(
176+
vscode.Uri.file(temp_diff_path),
177+
Buffer.from(diff, 'utf8')
178+
)
179+
try {
180+
execSync(
181+
`git apply --whitespace=nowarn "${temp_diff_path}"`,
182+
{
183+
cwd: temp_git_clone_path,
184+
stdio: 'pipe'
185+
}
186+
)
187+
} catch (e) {
188+
try {
189+
execSync(
190+
`git apply --reject --whitespace=nowarn "${temp_diff_path}"`,
191+
{
192+
cwd: temp_git_clone_path,
193+
stdio: 'pipe'
194+
}
195+
)
196+
vscode.window.showWarningMessage(
197+
`Some changes for ${folder.name} could not be applied cleanly during checkpoint restore. Check for .rej files in your workspace.`
198+
)
199+
} catch (rejectErr) {
200+
Logger.error({
201+
function_name: 'restore_checkpoint',
202+
message: `Failed to apply git diff even with --reject for ${folder.name}`,
203+
data: rejectErr
204+
})
205+
throw new Error(
206+
`Failed to apply git diff for ${folder.name}`
207+
)
208+
}
209+
} finally {
210+
try {
211+
await vscode.workspace.fs.delete(
212+
vscode.Uri.file(temp_diff_path)
213+
)
214+
} catch {}
215+
}
216+
}
217+
218+
await sync_directory({
219+
source_dir: temp_git_dir_uri,
220+
dest_dir: folder.uri,
221+
root_path: folder.uri.fsPath,
222+
workspace_provider: params.workspace_provider
223+
})
129224
} catch (err) {
130225
Logger.error({
131226
function_name: 'restore_checkpoint',
132-
message: 'Error restoring git checkpoint',
227+
message: `Error restoring git checkpoint for ${folder.name}`,
133228
data: err
134229
})
135230
throw err
231+
} finally {
232+
// cleanup temp dir
233+
if (temp_git_dir_uri) {
234+
try {
235+
await vscode.workspace.fs.delete(temp_git_dir_uri, {
236+
recursive: true
237+
})
238+
} catch {}
239+
}
136240
}
137241
} else {
138242
const source_folder_uri =
@@ -174,6 +278,35 @@ export const restore_checkpoint = async (params: {
174278
})
175279
}
176280

281+
const active_file = open_files.find((f) => f.isActive)
282+
283+
for (const file of open_files) {
284+
if (active_file && file === active_file) {
285+
continue
286+
}
287+
try {
288+
await vscode.workspace.fs.stat(file.uri)
289+
const doc = await vscode.workspace.openTextDocument(file.uri)
290+
await vscode.window.showTextDocument(doc, {
291+
viewColumn: file.viewColumn,
292+
preserveFocus: true,
293+
preview: false
294+
})
295+
} catch {}
296+
}
297+
298+
if (active_file) {
299+
try {
300+
await vscode.workspace.fs.stat(active_file.uri)
301+
const doc = await vscode.workspace.openTextDocument(active_file.uri)
302+
await vscode.window.showTextDocument(doc, {
303+
viewColumn: active_file.viewColumn,
304+
preserveFocus: false,
305+
preview: false
306+
})
307+
} catch {}
308+
}
309+
177310
if (
178311
params.checkpoint.title == 'Accepted response preview' &&
179312
params.checkpoint.response_preview_item_created_at &&

‎packages/vscode/src/commands/checkpoints-command/utils/copy-optimised-recursively.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
33
import * as fs from 'fs/promises'
4-
import { WorkspaceProvider } from '../../../context/providers/workspace-provider'
4+
import { WorkspaceProvider } from '@/context/providers/workspace-provider'
55

66
const directory_contains_ignored = async (
77
dir_uri: vscode.Uri,

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

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -105,100 +105,6 @@ export const get_git_diff = async (
105105
}
106106
}
107107

108-
export const apply_git_diff = async (
109-
workspace_folder: vscode.WorkspaceFolder,
110-
diff: string,
111-
target_commit?: string,
112-
target_branch?: string
113-
): Promise<boolean> => {
114-
try {
115-
const cwd = workspace_folder.uri.fsPath
116-
if (target_commit) {
117-
const current_commit = execSync('git rev-parse HEAD', {
118-
cwd,
119-
encoding: 'utf8'
120-
}).trim()
121-
122-
if (current_commit !== target_commit) {
123-
let current_branch: string | undefined
124-
try {
125-
current_branch = execSync('git rev-parse --abbrev-ref HEAD', {
126-
cwd,
127-
encoding: 'utf8'
128-
}).trim()
129-
} catch (err) {
130-
// Might be in detached HEAD state
131-
}
132-
133-
if (target_branch && current_branch !== target_branch) {
134-
try {
135-
execSync(`git checkout ${target_branch}`, { cwd, stdio: 'pipe' })
136-
} catch (err) {
137-
Logger.warn({
138-
function_name: 'apply_git_diff',
139-
message: `Could not checkout branch ${target_branch}`,
140-
data: err
141-
})
142-
}
143-
}
144-
145-
const after_branch_commit = execSync('git rev-parse HEAD', {
146-
cwd,
147-
encoding: 'utf8'
148-
}).trim()
149-
150-
if (after_branch_commit !== target_commit) {
151-
execSync(`git reset --hard ${target_commit}`, { cwd })
152-
}
153-
}
154-
}
155-
156-
execSync('git reset --hard HEAD', { cwd })
157-
execSync('git clean -fd', { cwd })
158-
159-
if (diff && diff.length > 0) {
160-
const temp_diff_path = path.join(cwd, '.git', 'checkpoint.diff')
161-
await vscode.workspace.fs.writeFile(
162-
vscode.Uri.file(temp_diff_path),
163-
Buffer.from(diff, 'utf8')
164-
)
165-
166-
try {
167-
execSync(`git apply "${temp_diff_path}"`, {
168-
cwd,
169-
stdio: 'pipe'
170-
})
171-
} catch (err) {
172-
// If git apply fails, try with --reject to apply what we can
173-
try {
174-
execSync(`git apply --reject "${temp_diff_path}"`, {
175-
cwd,
176-
stdio: 'pipe'
177-
})
178-
vscode.window.showWarningMessage(
179-
'Some changes could not be applied cleanly. Check .rej files.'
180-
)
181-
} catch (rejectErr) {
182-
throw new Error('Failed to apply diff')
183-
}
184-
} finally {
185-
try {
186-
await vscode.workspace.fs.delete(vscode.Uri.file(temp_diff_path))
187-
} catch (err) {}
188-
}
189-
}
190-
191-
return true
192-
} catch (error) {
193-
Logger.error({
194-
function_name: 'apply_git_diff',
195-
message: 'Error applying git diff',
196-
data: error
197-
})
198-
return false
199-
}
200-
}
201-
202108
export const is_git_repository = async (
203109
workspace_folder: vscode.WorkspaceFolder
204110
): Promise<boolean> => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
3-
import { WorkspaceProvider } from '../../../context/providers/workspace-provider'
3+
import { WorkspaceProvider } from '@/context/providers/workspace-provider'
44

55
export const sync_directory = async (params: {
66
source_dir: vscode.Uri

0 commit comments

Comments
 (0)