@@ -3,14 +3,11 @@ import * as path from 'path'
33import * as fs from 'fs'
44import { exec } from 'child_process'
55import { Logger } from '@shared/utils/logger'
6- import { promisify } from 'util'
76import { OriginalFileState } from '../types/original-file-state'
87import { create_safe_path } from '@/utils/path-sanitizer'
98import { apply_diff } from '../utils/edit-formats/diffs'
109import { remove_directory_if_empty } from '../utils/file-operations'
1110
12- const execAsync = promisify ( exec )
13-
1411const INDENTATION_BASED_EXTENSIONS = new Set ( [ '.py' , '.yaml' , '.yml' ] )
1512
1613const is_indentation_based_file = ( file_path : string ) : boolean => {
@@ -398,17 +395,34 @@ const handle_deleted_file_patch = async (
398395 }
399396}
400397
398+ const run_git_apply = (
399+ patch : string ,
400+ cwd : string ,
401+ flags : string
402+ ) : Promise < void > => {
403+ return new Promise ( ( resolve , reject ) => {
404+ const child = exec ( `git apply ${ flags } -` , { cwd } , ( error ) => {
405+ if ( error ) {
406+ reject ( error )
407+ } else {
408+ resolve ( )
409+ }
410+ } )
411+ child . stdin ?. write ( patch )
412+ child . stdin ?. end ( )
413+ } )
414+ }
415+
401416const process_diff = async ( params : {
402417 file_path : string
403- diff_patch_path : string
418+ diff_patch_content : string
404419 use_strict_whitespace ?: boolean
405420} ) : Promise < string > => {
406421 const file_content = fs . readFileSync ( params . file_path , 'utf8' )
407- const diff_patch_content = fs . readFileSync ( params . diff_patch_path , 'utf8' )
408422
409423 const result = apply_diff ( {
410424 original_code : file_content ,
411- diff_patch : diff_patch_content ,
425+ diff_patch : params . diff_patch_content ,
412426 use_strict_whitespace : params . use_strict_whitespace
413427 } )
414428
@@ -447,7 +461,6 @@ export const apply_git_patch = async (
447461 }
448462
449463 let closed_files : vscode . Uri [ ] = [ ]
450- const temp_file = path . join ( workspace_path , '.tmp_patch' )
451464 let original_states : OriginalFileState [ ] | undefined
452465
453466 try {
@@ -462,16 +475,6 @@ export const apply_git_patch = async (
462475 workspace_path
463476 )
464477
465- // Write patch file and ensure it's synced to disk before git apply reads it
466- const patch_buffer = Buffer . from ( patch_content )
467- const fd = await fs . promises . open ( temp_file , 'w' )
468- try {
469- await fd . write ( patch_buffer )
470- await fd . sync ( ) // Ensure data is flushed to disk
471- } finally {
472- await fd . close ( )
473- }
474-
475478 let last_error : any = null
476479 let success = false
477480 let diff_application_method : 'recount' | 'search_and_replace' | undefined =
@@ -497,9 +500,7 @@ export const apply_git_patch = async (
497500 . filter ( Boolean )
498501 . join ( ' ' )
499502
500- await execAsync ( `git apply ${ flags } "${ temp_file } "` , {
501- cwd : workspace_path
502- } )
503+ await run_git_apply ( patch_content , workspace_path , flags )
503504 success = true
504505 diff_application_method = 'recount'
505506 Logger . info ( {
@@ -527,7 +528,7 @@ export const apply_git_patch = async (
527528
528529 applied_content = await process_diff ( {
529530 file_path : file_path_safe ,
530- diff_patch_path : temp_file ,
531+ diff_patch_content : patch_content ,
531532 use_strict_whitespace
532533 } )
533534 success = true
@@ -544,7 +545,6 @@ export const apply_git_patch = async (
544545 if ( success ) {
545546 await process_modified_files ( file_paths , workspace_path )
546547 await reopen_closed_files ( closed_files )
547- await vscode . workspace . fs . delete ( vscode . Uri . file ( temp_file ) )
548548
549549 if ( original_states ) {
550550 for ( const state of original_states ) {
@@ -592,18 +592,6 @@ export const apply_git_patch = async (
592592 // This outer catch handles setup errors and final application failures
593593 await reopen_closed_files ( closed_files )
594594
595- try {
596- if ( fs . existsSync ( temp_file ) ) {
597- await vscode . workspace . fs . delete ( vscode . Uri . file ( temp_file ) )
598- }
599- } catch ( deleteError ) {
600- Logger . warn ( {
601- function_name : 'apply_git_patch' ,
602- message : 'Failed to delete temp patch file in error handler.' ,
603- data : deleteError
604- } )
605- }
606-
607595 const has_rejects = error ?. message ?. includes ( '.rej' )
608596 if ( has_rejects ) {
609597 const file_paths = extract_file_paths_from_patch ( patch_content )
0 commit comments