Skip to contentSkip to conversation

let git-diff allow patch to delete empty files?

PATCH Sam Liddicott 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

In some cases "patch" cannot apply diff's generated using git-diff, I've had a "git diff" output look like this when an empty file was removed as the only change:

source3/include/dcerpc.h deleted +0 -0
deleted file mode 100644
index e69de29..0000000

which patch rejected, saying:
patch: **** Only garbage was found in the patch input.

There is a ghastly diff notation recognized by patch, using the magic date 1970-01-01 01:00:00.000000000 to signify deletion which git-diff properly uses for non-empty files.

Empty files are a different matter because there is no unified diff that can represent deletion of an empty file because patch doesn't like unified diff's with no context.

However this equivalent pair works by making the file non-empty and then deleting it.

here 2008-11-05 09:43:58.000000000 +0000 renamed from here 2008-11-05 09:43:55.000000000 +0000 +1 -0
diff -Nru 1/here 2/here
@@ -0,0 +1 @@
+
here 1970-01-01 01:00:00.000000000 +0100 renamed from here 2008-11-05 09:37:23.000000000 +0000 +0 -1
diff -Nru 1/here 2/here
@@ -1 +0,0 @@
-

In considering this filthy hack we should recognize that currently :

* patch will choke on certain "git diff" output. If patch's exit code is checked as part of a loop of patches, then the patch procedure will fail. This happened to me
* if delete-empty-file is one action in a larger patch file it will do nothing - which can be worse if the "existence" of a file affects the build system.

This filthy hack fixes both problems.

Sam

#1 lore raw

Quoting "Sam Liddicott" <[email protected]>:

Quoted 3 lines

In some cases "patch" cannot apply diff's generated using git-diff, I've had a "git diff" output look like this when an empty file was removed as the only change:

Even if you do not use git to manage your changes, you can use "git apply" from outside of a git repository as a replacement for "patch".

Signature · 2 lines
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/
#2 lore raw

* Nanako Shiraishi wrote, On 05/11/08 21:09:

Quoted 11 lines

Quoting "Sam Liddicott" <[email protected]>:

Quoted 4 lines

In some cases "patch" cannot apply diff's generated using git-diff, I've had a "git diff" output look like this when an empty file was removed as the only change:

Even if you do not use git to manage your changes, you can use "git apply" from outside of a git repository as a replacement for "patch".

Good tip, thanks.

In this case it is rpmbuild that is doing the patching, and making git a build-requires would not be popular.

Sam

#3 lore raw
In reply to Sam Liddicott

* Sam Liddicott wrote, On 05/11/08 12:22:

Quoted 18 lines

In some cases "patch" cannot apply diff's generated using git-diff, I've had a "git diff" output look like this when an empty file was removed as the only change:

..
However this equivalent pair works by making the file non-empty and then deleting it.

diff -Nru 1/here 2/here

here 2008-11-05 09:43:58.000000000 +0000 renamed from here 2008-11-05 09:43:55.000000000 +0000 +1 -0
@@ -0,0 +1 @@
+

diff -Nru 1/here 2/here

here 1970-01-01 01:00:00.000000000 +0100 line 1

Quoted 1 line
here 1970-01-01 01:00:00.000000000 +0100 renamed from here 2008-11-05 09:37:23.000000000 +0000 +0 -1
@@ -1 +0,0 @@
-

The same problem occurs with new empty files.

Attached is an awk filter which will expand out these git notes into the 2-part unified diff's.
It would be nicer if git did it natively.
Sam

#! /usr/bin/awk -f

function do_empty() {

  if (diff!="" && deleted!="" && indx!="") {
    printf("Make patch give the file 1 line\n");
    printf("--- %s\n",diff);
    printf("+++ %s\n",diff);
    printf("@@ -0,0 +1 @@\n+\n");
    printf("Make patch delete the 1 line file\n");
    printf("--- %s\t\n",diff);
    printf("+++ /dev/null\t1970-01-01 01:00:00.000000000\n");
    printf("@@ -1 +0,0 @@\n-\n");
  }
  if (diff!="" && created!="" && indx!="") {
    printf("Make patch create the file with 1 line\n");
    printf("--- %s\n",diff);
    printf("+++ %s\n",diff);
    printf("@@ -0,0 +1 @@\n+\n");
    printf("Make patch create delete the line but keep the file\n");
    printf("--- %s\n",diff);
    printf("+++ %s\n",diff);
    printf("@@ -1 +0,0 @@\n-\n");

}
no_empty();
}

function no_empty() {
diff="";
deleted="";
created="";
indx="";
}

{
if (in_hunk > 0) in_hunk--;
}

/^diff --git / {
do_empty();
if (! in_hunk) diff=$3;
}

/^deleted file mode / {
if (! in_hunk && diff!="") deleted=$0;
}

/^new file mode / {
if (! in_hunk && diff!="") created=$0;
}

/^index / {
if (! in_hunk && diff!="") indx=$0;
}

/^--- / {
no_empty();
}

/^@@ / {

  # read the hunk size
  p=index($2,",");
  if (p==0) c1=1;
  else c1=strtonum(substr($2, p+1));
  p=index($3,",");
  if (p==0) in_hunk=1;
  else in_hunk=strtonum(substr($2, p+1));

if (c1 > in_hunk) in_hunk=c1;
}

{
print;
}

END {
do_empty();
}