Skip to content

applyPatch can't handle clearing the file #116

@chaaz

Description

@chaaz

If a patch is supposed to completely erase a file, applyPatch will instead keep the first line.

$ cat testfile 
This
is
a
file,
yo
$ cat > testfile          # empty the file out
$ cat testfile
$ git diff > diff.patch   # create unified patch w/ git
$ cat diff.patch 
diff --git a/testfile b/testfile
index c9d61c6..e69de29 100644
--- a/testfile
+++ b/testfile
@@ -1,5 +0,0 @@
-This
-is
-a
-file,
-yo
$ git checkout -- .       # restore testfile
$ cat testfile 
This
is
a
file,
yo
$ patch -p1 < diff.patch  # test with 'patch'
patching file testfile
$ cat testfile            # yep, worked
$ git checkout -- .       # restore testfile again
$ cat patch.js
var fs = require('fs');
var diff = require('diff');

console.log(
  diff.applyPatch(
    fs.readFileSync('testfile', 'utf8'),
    fs.readFileSync('diff.patch', 'utf8')
  )
);

$ node patch.js           # this should be empty
This

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions