Skip to content

Commit 3e2107f

Browse files
committed
Replace synchronous Git commands with asynchronous ones
1 parent baec098 commit 3e2107f

2 files changed

Lines changed: 57 additions & 31 deletions

File tree

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ export const create_checkpoint = async (
9090
const folder_git_statuses = await Promise.all(
9191
workspace_folders.map((folder) => is_git_repository(folder))
9292
)
93-
const all_folders_use_git = folder_git_statuses.every((status) => status)
94-
9593
let new_checkpoint: Checkpoint | undefined
9694

9795
const create_checkpoint_task = async () => {
@@ -232,17 +230,27 @@ export const create_checkpoint = async (
232230
new_checkpoint = checkpoint_object
233231
}
234232

235-
if (all_folders_use_git) {
236-
await create_checkpoint_task()
237-
} else {
238-
await vscode.window.withProgress(
239-
{
240-
location: vscode.ProgressLocation.Notification,
233+
let timer: ReturnType<typeof setTimeout> | undefined
234+
let did_show_modal = false
235+
236+
try {
237+
timer = setTimeout(() => {
238+
did_show_modal = true
239+
panel_provider.send_message({
240+
command: 'SHOW_PROGRESS',
241241
title: 'Creating checkpoint...',
242242
cancellable: false
243-
},
244-
create_checkpoint_task
245-
)
243+
})
244+
}, 500)
245+
246+
await create_checkpoint_task()
247+
} finally {
248+
if (timer) clearTimeout(timer)
249+
if (did_show_modal) {
250+
panel_provider.send_message({
251+
command: 'HIDE_PROGRESS'
252+
})
253+
}
246254
}
247255
return new_checkpoint
248256
} catch (err: any) {

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import * as vscode from 'vscode'
2-
import { execSync } from 'child_process'
2+
import { exec } from 'child_process'
33
import { Logger } from '@shared/utils/logger'
4+
5+
const execAsync = (command: string, options: any): Promise<string> => {
6+
return new Promise((resolve, reject) => {
7+
exec(command, options, (error, stdout, stderr) => {
8+
if (error) {
9+
;(error as any).stdout = stdout
10+
;(error as any).stderr = stderr
11+
;(error as any).status = error.code
12+
reject(error)
13+
} else {
14+
resolve(stdout.toString())
15+
}
16+
})
17+
})
18+
}
19+
420
export interface GitInfo {
521
branch: string
622
commit_hash: string
@@ -11,15 +27,19 @@ export const get_git_info = async (
1127
workspace_folder: vscode.WorkspaceFolder
1228
): Promise<GitInfo | null> => {
1329
try {
14-
const branch = execSync('git rev-parse --abbrev-ref HEAD', {
15-
cwd: workspace_folder.uri.fsPath,
16-
encoding: 'utf8'
17-
}).trim()
30+
const branch = (
31+
await execAsync('git rev-parse --abbrev-ref HEAD', {
32+
cwd: workspace_folder.uri.fsPath,
33+
encoding: 'utf8'
34+
})
35+
).trim()
1836

19-
const commit_hash = execSync('git rev-parse HEAD', {
20-
cwd: workspace_folder.uri.fsPath,
21-
encoding: 'utf8'
22-
}).trim()
37+
const commit_hash = (
38+
await execAsync('git rev-parse HEAD', {
39+
cwd: workspace_folder.uri.fsPath,
40+
encoding: 'utf8'
41+
})
42+
).trim()
2343

2444
return {
2545
branch,
@@ -38,8 +58,8 @@ export const get_git_diff = async (
3858
let diff = ''
3959
try {
4060
// Add --binary to handle binary files correctly.
41-
// execSync will throw if there are differences, so we catch it.
42-
diff = execSync('git diff --binary HEAD', {
61+
// execAsync will reject if there are differences (exit code 1), so we catch it.
62+
diff = await execAsync('git diff --binary HEAD', {
4363
cwd: workspace_folder.uri.fsPath,
4464
encoding: 'utf8',
4565
maxBuffer: 50 * 1024 * 1024 // 50MB max
@@ -54,12 +74,11 @@ export const get_git_diff = async (
5474
}
5575
}
5676

57-
const untracked_files = execSync(
58-
'git ls-files --others --exclude-standard',
59-
{
77+
const untracked_files = (
78+
await execAsync('git ls-files --others --exclude-standard', {
6079
cwd: workspace_folder.uri.fsPath,
6180
encoding: 'utf8'
62-
}
81+
})
6382
)
6483
.trim()
6584
.split('\n')
@@ -69,8 +88,8 @@ export const get_git_diff = async (
6988
for (const file of untracked_files) {
7089
try {
7190
// Use git diff to create a patch for the new file, which handles binary files correctly.
72-
// execSync will throw if there are differences, but the diff will be in stdout.
73-
const file_diff = execSync(
91+
// execAsync will throw if there are differences, but the diff will be in stdout.
92+
const file_diff = await execAsync(
7493
`git diff --no-index --binary /dev/null "${file}"`,
7594
{
7695
cwd: workspace_folder.uri.fsPath,
@@ -108,9 +127,8 @@ export const is_git_repository = async (
108127
workspace_folder: vscode.WorkspaceFolder
109128
): Promise<boolean> => {
110129
try {
111-
execSync('git rev-parse --git-dir', {
112-
cwd: workspace_folder.uri.fsPath,
113-
stdio: 'pipe'
130+
await execAsync('git rev-parse --git-dir', {
131+
cwd: workspace_folder.uri.fsPath
114132
})
115133
return true
116134
} catch (error) {

0 commit comments

Comments
 (0)