@@ -465,6 +465,26 @@ def p4_system(cmd, *k, **kw):
465465 raise subprocess .CalledProcessError (retcode , real_cmd )
466466
467467
468+ def diffTreeApply (id , applyArgs , ignore_error = False ):
469+ """Pipe `git diff-tree --full-index -p <id>` into `git apply <applyArgs>`
470+ without a shell, so id can never be interpreted as shell syntax. Returns
471+ the exit status of git apply, raising CalledProcessError on a non-zero
472+ status unless ignore_error is set."""
473+ diffArgv = ["git" , "diff-tree" , "--full-index" , "-p" , id ]
474+ applyArgv = ["git" , "apply" ] + applyArgs
475+ if verbose :
476+ print ("TryPatch: %s | %s" % (" " .join (diffArgv ), " " .join (applyArgv )))
477+ diffProc = subprocess .Popen (diffArgv , stdout = subprocess .PIPE )
478+ applyProc = subprocess .Popen (applyArgv , stdin = diffProc .stdout )
479+ diffProc .stdout .close ()
480+ applyProc .wait ()
481+ diffProc .wait ()
482+ retcode = applyProc .returncode
483+ if retcode and not ignore_error :
484+ raise subprocess .CalledProcessError (retcode , applyArgv )
485+ return retcode
486+
487+
468488def die_bad_access (s ):
469489 die ("failure accessing depot: {0}" .format (s .rstrip ()))
470490
@@ -2234,16 +2254,11 @@ def applyCommit(self, id):
22342254 else :
22352255 die ("unknown modifier %s for %s" % (modifier , path ))
22362256
2237- diffcmd = "git diff-tree --full-index -p \" %s\" " % (id )
2238- patchcmd = diffcmd + " | git apply "
2239- tryPatchCmd = patchcmd + "--check -"
2240- applyPatchCmd = patchcmd + "--check --apply -"
2257+ tryPatchArgs = ["--check" , "-" ]
2258+ applyPatchArgs = ["--check" , "--apply" , "-" ]
22412259 patch_succeeded = True
22422260
2243- if verbose :
2244- print ("TryPatch: %s" % tryPatchCmd )
2245-
2246- if os .system (tryPatchCmd ) != 0 :
2261+ if diffTreeApply (id , tryPatchArgs , ignore_error = True ) != 0 :
22472262 fixed_rcs_keywords = False
22482263 patch_succeeded = False
22492264 print ("Unfortunately applying the change failed!" )
@@ -2279,7 +2294,7 @@ def applyCommit(self, id):
22792294
22802295 if fixed_rcs_keywords :
22812296 print ("Retrying the patch with RCS keywords cleaned up" )
2282- if os . system ( tryPatchCmd ) == 0 :
2297+ if diffTreeApply ( id , tryPatchArgs , ignore_error = True ) == 0 :
22832298 patch_succeeded = True
22842299 print ("Patch succeesed this time with RCS keywords cleaned" )
22852300
@@ -2291,7 +2306,7 @@ def applyCommit(self, id):
22912306 #
22922307 # Apply the patch for real, and do add/delete/+x handling.
22932308 #
2294- system ( applyPatchCmd , shell = True )
2309+ diffTreeApply ( id , applyPatchArgs )
22952310
22962311 for f in filesToChangeType :
22972312 p4_edit (f , "-t" , "auto" )
0 commit comments