11import * as vscode from 'vscode'
2- import { execSync } from 'child_process'
2+ import { exec } from 'child_process'
33import { 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+
420export 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