@@ -84,6 +84,98 @@ ipcMain.handle('git:worktree-list', async (_, projectPath: string) => {
8484 } ) ;
8585} ) ;
8686
87+ // Git diff and status operations
88+ ipcMain . handle ( 'git:status' , async ( _ , worktreePath : string ) => {
89+ return new Promise ( ( resolve , reject ) => {
90+ const child = spawn ( 'git' , [ 'status' , '--porcelain=v1' ] , {
91+ cwd : worktreePath
92+ } ) ;
93+
94+ let stdout = '' ;
95+ let stderr = '' ;
96+
97+ child . stdout . on ( 'data' , ( data ) => {
98+ stdout += data . toString ( ) ;
99+ } ) ;
100+
101+ child . stderr . on ( 'data' , ( data ) => {
102+ stderr += data . toString ( ) ;
103+ } ) ;
104+
105+ child . on ( 'close' , ( code ) => {
106+ if ( code === 0 ) {
107+ resolve ( parseGitStatus ( stdout ) ) ;
108+ } else {
109+ reject ( new Error ( stderr || 'Failed to get git status' ) ) ;
110+ }
111+ } ) ;
112+ } ) ;
113+ } ) ;
114+
115+ ipcMain . handle ( 'git:diff' , async ( _ , worktreePath : string , filePath ?: string ) => {
116+ return new Promise ( ( resolve , reject ) => {
117+ const args = [ 'diff' ] ;
118+ if ( filePath ) {
119+ args . push ( filePath ) ;
120+ }
121+
122+ const child = spawn ( 'git' , args , {
123+ cwd : worktreePath
124+ } ) ;
125+
126+ let stdout = '' ;
127+ let stderr = '' ;
128+
129+ child . stdout . on ( 'data' , ( data ) => {
130+ stdout += data . toString ( ) ;
131+ } ) ;
132+
133+ child . stderr . on ( 'data' , ( data ) => {
134+ stderr += data . toString ( ) ;
135+ } ) ;
136+
137+ child . on ( 'close' , ( code ) => {
138+ if ( code === 0 ) {
139+ resolve ( stdout ) ;
140+ } else {
141+ reject ( new Error ( stderr || 'Failed to get git diff' ) ) ;
142+ }
143+ } ) ;
144+ } ) ;
145+ } ) ;
146+
147+ ipcMain . handle ( 'git:diff-staged' , async ( _ , worktreePath : string , filePath ?: string ) => {
148+ return new Promise ( ( resolve , reject ) => {
149+ const args = [ 'diff' , '--staged' ] ;
150+ if ( filePath ) {
151+ args . push ( filePath ) ;
152+ }
153+
154+ const child = spawn ( 'git' , args , {
155+ cwd : worktreePath
156+ } ) ;
157+
158+ let stdout = '' ;
159+ let stderr = '' ;
160+
161+ child . stdout . on ( 'data' , ( data ) => {
162+ stdout += data . toString ( ) ;
163+ } ) ;
164+
165+ child . stderr . on ( 'data' , ( data ) => {
166+ stderr += data . toString ( ) ;
167+ } ) ;
168+
169+ child . on ( 'close' , ( code ) => {
170+ if ( code === 0 ) {
171+ resolve ( stdout ) ;
172+ } else {
173+ reject ( new Error ( stderr || 'Failed to get staged git diff' ) ) ;
174+ }
175+ } ) ;
176+ } ) ;
177+ } ) ;
178+
87179ipcMain . handle ( 'git:worktree-add' , async ( _ , projectPath : string , branchName : string ) => {
88180 const worktreePath = path . join ( projectPath , '..' , `${ path . basename ( projectPath ) } -${ branchName } ` ) ;
89181
@@ -150,4 +242,18 @@ function parseWorktrees(output: string): Array<{ path: string; branch: string; h
150242 }
151243
152244 return worktrees ;
245+ }
246+
247+ function parseGitStatus ( output : string ) : Array < { path : string ; status : string ; staged : boolean ; modified : boolean } > {
248+ const lines = output . trim ( ) . split ( '\n' ) . filter ( line => line . length > 0 ) ;
249+ return lines . map ( line => {
250+ const status = line . substring ( 0 , 2 ) ;
251+ const path = line . substring ( 3 ) ;
252+ return {
253+ path,
254+ status,
255+ staged : status [ 0 ] !== ' ' && status [ 0 ] !== '?' ,
256+ modified : status [ 1 ] !== ' ' && status [ 1 ] !== '?'
257+ } ;
258+ } ) ;
153259}
0 commit comments