44

Ok, I've tried all answers i could find on stackoverflow, but apparently none seem to be able to solve my problem. I want to apply a patch made by SVN to a git repository. Apparently the easiest way to do it is by using 'git apply', but that does not seem to work.

$ git apply --ignore-space-change --ignore-whitespace < xxx_parser.patch


<stdin>:10: trailing whitespace.
        FORCE_LA_CHECK = false; stdin:23: trailing whitespace.

<stdin>:79: trailing whitespace
. 
. 
. 
. 
error: pmd/grammar/JspParser.jjt: No such file or directory 
error: patch failed: pmd/pom.xml:251 
error: pmd/pom.xml: patch does not apply

This is the content of xxx_parser.patch:

 $ head xxx_parser.patch Index: etc/grammar/JspParser.jjt
 --- etc/grammar/JspParser.jjt   (revision 7704)
 +++ etc/grammar/JspParser.jjt   (working copy)

now why does it complain that it cannot find file pmd/grammar/JspParser.jjt?

The path in the patch is pointing to proper directory.

victor
  • 1,626
  • 1
  • 14
  • 23
  • You know that a patch file can have changes for more than one file right? Just looking at the head of the file as you have here will only show you the first file in the patch. – asm Jul 17 '12 at 11:41

2 Answers2

54

I've had a few issues applying SVN generated patches with git. I'd recommend applying any subversion patches directly with patch command, and use git to verify that said patch was successfully applied.

$ patch -p0 < xxx_parser.patch
$ git diff
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Unfortunately i get the following error when i try the above command Assertion failed: (s && size), function savebuf, file /SourceCache/gpatch/gpatch-2/patch/util.c, line 424. Abort trap: 6 – Anthony McCormick Nov 22 '12 at 10:11
  • I believe `assert (s && size)` was bug related to "_\ No newline at end of file_" in older versions of [patch](http://savannah.gnu.org/projects/patch/). Luckily, it has been resolved. Ensure that your system is up to date with [patch-2.7.1](http://ftp.gnu.org/gnu/patch/). – emcconville Nov 23 '12 at 14:21
  • 2
    In case of line ending issues (too many lines modified) try using binary option: patch --binary --no-backup-if-mismatch -p0 < xxx_parser.patch – too Sep 07 '15 at 08:51
10

@emcconville answer works if you have patch as an executable command in the command line.

For others:

  1. Go to the svn repo

    svn diff --git >> gitFormat.patch

  2. From your (Copy this file to the) git repo

    git apply gitFormat.patch

Vineeth Chitteti
  • 1,454
  • 2
  • 14
  • 30
  • 1
    This worked for me, though I had to hand-edit the .patch file to fix the paths to the files - in my case the SVN repository root was two directories up from the project that the patch related to. – David Gardiner Aug 10 '18 at 22:52
  • 1
    for adjusting path `-p Remove leading slashes from traditional diff paths` might be useful – Greg Domjan Feb 13 '19 at 07:57