vitiral
vitiral

Reputation: 9238

unidiff: encode file move?

Is there a way to encode moving a file path that patch respects?

echo '# Story' > story.txt

patch -Nfu << EOF  
--- story.txt
+++ kitty.txt
@@ -1 +1 @@
-# Story
+# Kitty
EOF

echo "STORY"; cat story.txt
echo "KITTY"; cat kitty.txt

Results in the following (story.txt wasn't moved to kitty.txt as expected)

STORY
# Kitty
KITTY
cat: kitty.txt: No such file or directory

Notes:

Upvotes: -1

Views: 54

Answers (1)

jhnc
jhnc

Reputation: 16819

It's not portable but with GNU patch you might get away with:

echo '# Story' >story.txt

patch -Nfu << EOF  
--- kitty.txt
+++ kitty.txt
@@ -0,0 +1 @@
+# Kitty
--- story.txt
+++ story.txt 1970-01-01 00:00:00.000000000 +0000
@@ -1 +0,0 @@
-# Story
EOF

or

echo '# Story' >story.txt

patch -Nfu << EOF  
--- /dev/null
+++ kitty.txt
@@ -0,0 +1 @@
+# Kitty
--- story.txt
+++ /dev/null
@@ -1 +0,0 @@
-# Story
EOF

From the documentation:

Sometimes when comparing two directories, a file may exist in one directory but not the other. If you give diff the --new-file (-N) option, or if you supply an old or new file that is named /dev/null or is empty and is dated the Epoch (1970-01-01 00:00:00 UTC), diff outputs a patch that adds or deletes the contents of this file. When given such a patch, patch normally creates a new file or removes the old file. However, when conforming to POSIX (see patch and the POSIX Standard), patch does not remove the old file, but leaves it empty. The --remove-empty-files (-E) option causes patch to remove output files that are empty after applying a patch, even if the patch does not appear to be one that removed the file.

Upvotes: 0

Related Questions