{"thread":{"id":"16180","subject":"let git-diff allow patch to delete empty files?","startedAt":"2008-11-05T10:40:01Z","lastAt":"2008-11-06T03:33:17Z","messageCount":4,"participants":["Sam Liddicott","Nanako Shiraishi"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"94969","messageId":"49118FEE.30408@liddicott.com","threadId":"16180","inReplyTo":null,"subject":"let git-diff allow patch to delete empty files?","fromName":"Sam Liddicott","fromEmail":"sam@liddicott.com","sentAt":null,"receivedAt":"2008-11-05T10:40:01Z","isPatch":false,"sender":{"key":"sam@liddicott.com","avatar":null},"body":"In some cases \"patch\" cannot apply diff's generated using git-diff, I've\nhad a \"git diff\" output look like this when an empty file was removed as\nthe only change:\n\ndiff --git a/source3/include/dcerpc.h b/source3/include/dcerpc.h\ndeleted file mode 100644\nindex e69de29..0000000\n\nwhich patch rejected, saying:\npatch: **** Only garbage was found in the patch input.\n\nThere is a ghastly diff notation recognized by patch, using the magic\ndate 1970-01-01 01:00:00.000000000 to signify deletion which git-diff\nproperly uses for non-empty files.\n\nEmpty files are a different matter because there is no unified diff that\ncan represent deletion of an empty file because patch doesn't like\nunified diff's with no context.\n\nHowever this equivalent pair works by making the file non-empty and then\ndeleting it.\n\ndiff -Nru 1/here 2/here\n--- 1/here    2008-11-05 09:43:55.000000000 +0000\n+++ 2/here    2008-11-05 09:43:58.000000000 +0000\n@@ -0,0 +1 @@\n+\ndiff -Nru 1/here 2/here\n--- 1/here    2008-11-05 09:37:23.000000000 +0000\n+++ 2/here    1970-01-01 01:00:00.000000000 +0100\n@@ -1 +0,0 @@\n-\n\n\nIn considering this filthy hack we should recognize that currently :\n\n* patch will choke on certain \"git diff\" output. If patch's exit code is\nchecked as part of a loop of patches, then the patch procedure will\nfail. This happened to me\n* if delete-empty-file is one action in a larger patch file it will do\nnothing - which can be worse if the \"existence\" of a file affects the\nbuild system.\n\nThis filthy hack fixes both problems.\n\nSam\n"},{"id":"94973","messageId":"4911B297.2070800@liddicott.com","threadId":"16180","inReplyTo":"49118FEE.30408@liddicott.com","subject":"Re: let git-diff allow patch to delete empty files?","fromName":"Sam Liddicott","fromEmail":"sam@liddicott.com","sentAt":null,"receivedAt":"2008-11-05T13:07:57Z","isPatch":false,"sender":{"key":"sam@liddicott.com","avatar":null},"body":"* Sam Liddicott wrote, On 05/11/08 12:22:\n> In some cases \"patch\" cannot apply diff's generated using git-diff, I've\n> had a \"git diff\" output look like this when an empty file was removed as\n> the only change:\n>\n> ..\n> However this equivalent pair works by making the file non-empty and then\n> deleting it.\n>\n> diff -Nru 1/here 2/here\n> --- 1/here    2008-11-05 09:43:55.000000000 +0000\n> +++ 2/here    2008-11-05 09:43:58.000000000 +0000\n> @@ -0,0 +1 @@\n> +\n> diff -Nru 1/here 2/here\n> --- 1/here    2008-11-05 09:37:23.000000000 +0000\n> +++ 2/here    1970-01-01 01:00:00.000000000 +0100\n> @@ -1 +0,0 @@\n> -\n>   \nThe same problem occurs with new empty files.\n\nAttached is an awk filter which will expand out these git notes into the\n2-part unified diff's.\nIt would be nicer if git did it natively.\nSam\n\n\n#! /usr/bin/awk -f\n\nfunction do_empty() {\n  if (diff!=\"\" && deleted!=\"\" && indx!=\"\") {\n    printf(\"Make patch give the file 1 line\\n\");\n    printf(\"--- %s\\n\",diff);\n    printf(\"+++ %s\\n\",diff);\n    printf(\"@@ -0,0 +1 @@\\n+\\n\");\n    printf(\"Make patch delete the 1 line file\\n\");\n    printf(\"--- %s\\t\\n\",diff);\n    printf(\"+++ /dev/null\\t1970-01-01 01:00:00.000000000\\n\");\n    printf(\"@@ -1 +0,0 @@\\n-\\n\");\n  }\n  if (diff!=\"\" && created!=\"\" && indx!=\"\") {\n    printf(\"Make patch create the file with 1 line\\n\");\n    printf(\"--- %s\\n\",diff);\n    printf(\"+++ %s\\n\",diff);\n    printf(\"@@ -0,0 +1 @@\\n+\\n\");\n    printf(\"Make patch create delete the line but keep the file\\n\");\n    printf(\"--- %s\\n\",diff);\n    printf(\"+++ %s\\n\",diff);\n    printf(\"@@ -1 +0,0 @@\\n-\\n\");\n  }\n  no_empty();\n}\n\nfunction no_empty() {\n  diff=\"\";\n  deleted=\"\";\n  created=\"\";\n  indx=\"\";\n}\n\n{\n  if (in_hunk > 0) in_hunk--;\n}\n\n/^diff --git / {\n  do_empty();\n  if (! in_hunk) diff=$3;\n}\n\n/^deleted file mode / {\n  if (! in_hunk && diff!=\"\") deleted=$0;\n}\n\n/^new file mode / {\n  if (! in_hunk && diff!=\"\") created=$0;\n}\n\n/^index / {\n  if (! in_hunk && diff!=\"\") indx=$0;\n}\n\n/^--- / {\n  no_empty();\n}\n\n/^@@ / {\n  # read the hunk size\n  p=index($2,\",\");\n  if (p==0) c1=1;\n  else c1=strtonum(substr($2, p+1));\n\n  p=index($3,\",\");\n  if (p==0) in_hunk=1;\n  else in_hunk=strtonum(substr($2, p+1));\n\n  if (c1 > in_hunk) in_hunk=c1;\n}\n\n{\n  print;\n}\n\nEND {\n  do_empty();\n}\n"},{"id":"94998","messageId":"20081106060940.6117@nanako3.lavabit.com","threadId":"16180","inReplyTo":"49118FEE.30408@liddicott.com","subject":"Re: let git-diff allow patch to delete empty files?","fromName":"Nanako Shiraishi","fromEmail":"nanako3@lavabit.com","sentAt":"2008-11-05T21:09:40Z","receivedAt":"2008-11-05T21:09:40Z","isPatch":false,"sender":{"key":"nanako3@lavabit.com","avatar":"https://gravatar.com/avatar/3777b9e201c5883a62b1a6fdf7c53f2d712d1d80989146063ea861e33aad72a8?d=mp&s=160"},"body":"Quoting \"Sam Liddicott\" <sam@liddicott.com>:\n\n> In some cases \"patch\" cannot apply diff's generated using git-diff, I've\n> had a \"git diff\" output look like this when an empty file was removed as\n> the only change:\n\nEven 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\".\n\n-- \nNanako Shiraishi\nhttp://ivory.ap.teacup.com/nanako3/\n"},{"id":"95031","messageId":"4912AB20.4070608@liddicott.com","threadId":"16180","inReplyTo":"20081106060940.6117@nanako3.lavabit.com","subject":"Re: let git-diff allow patch to delete empty files?","fromName":"Sam Liddicott","fromEmail":"sam@liddicott.com","sentAt":null,"receivedAt":"2008-11-06T03:33:17Z","isPatch":false,"sender":{"key":"sam@liddicott.com","avatar":null},"body":"* Nanako Shiraishi wrote, On 05/11/08 21:09:\n> Quoting \"Sam Liddicott\" <sam@liddicott.com>:\n>\n>   \n>> In some cases \"patch\" cannot apply diff's generated using git-diff, I've\n>> had a \"git diff\" output look like this when an empty file was removed as\n>> the only change:\n>>     \n>\n> 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\".\n>\n>   \nGood tip, thanks.\n\nIn this case it is rpmbuild that is doing the patching, and making git a\nbuild-requires would not be popular.\n\nSam\n"}]}