Apply patch to file that's under Git without u

2019-09-03 10:08发布

问题:

I checked out the latest OpenSSL from Git:

git clone git://git.openssl.org/openssl.git

One of the devs sent me a patch for AARCH64 to test:

$ cat RT4237.diff 
diff --git a/crypto/ec/asm/ecp_nistz256-armv8.pl b/crypto/ec/asm/ecp_nistz256-armv8.pl
index 9d1bce1..ce6b69e 100644
--- a/crypto/ec/asm/ecp_nistz256-armv8.pl
+++ b/crypto/ec/asm/ecp_nistz256-armv8.pl
@@ -1289,6 +1289,9 @@ $code.=<<___;
    stp $acc0,$acc1,[$rp_real,#$i]
    stp $acc2,$acc3,[$rp_real,#$i+16]
 ___
+$code.=<<___   if ($i == 0);
+   adr $bp_real,.Lone_mont-64
+___
 }
 $code.=<<___;
    ldp $acc0,$acc1,[$ap_real,#$i]  // in1

The patch does not apply using Git. I'm not sharing the Git failure so folks don't get distracted on solving Git problems. Instead, I simply want to use a tool that actually works.

I've tried using Apply Patch File to a Source Code Tree (patch -p3 < RT4237.diff) but its resulting in errors due to the Git preamble:

$ cd openssl
$ patch -p3 < RT4237.diff
(Stripping trailing CRs from patch; use --binary to disable.)
can't find file to patch at input line 5
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/crypto/ec/asm/ecp_nistz256-armv8.pl b/crypto/ec/asm/ecp_nistz256-armv8.pl
|index 9d1bce1..ce6b69e 100644
|--- a/crypto/ec/asm/ecp_nistz256-armv8.pl
|+++ b/crypto/ec/asm/ecp_nistz256-armv8.pl
--------------------------

How can I apply the patch without using Git?


hikey:openssl$ pwd
/home/jwalton/openssl
hikey:openssl$ find . -name RT4237.diff 
./RT4237.diff
hikey:openssl$ find . -name ecp_nistz256-armv8.pl
./crypto/ec/asm/ecp_nistz256-armv8.pl

回答1:

Based on the pwd and patch file's contents, the patch needed to be applied with the -p1 flag to tell patch to ignore the leading "a/" portion of the path name in order for it to find the file to patch:

$ cd openssl
$ patch -p1 < RT4237.diff 
(Stripping trailing CRs from patch; use --binary to disable.)
patching file crypto/ec/asm/ecp_nistz256-armv8.pl
$


回答2:

As far as I can tell, a git patch is a regular diff with preamble. Therefore, remove the git preamble and try again.



标签: git diff patch