0

Currently, I am editing files on vim by setting patchmode=.orig and then, having two files:

  1. file.txt.orig <- backup without changes
  2. file.txt <- update file

However, I would like to generate a diff without having to rename the .orig file, so patch understands it is just file.txt which has been updated.

Ideally I would really enjoy a workflow I could run

diff -u file.txt which would automatically compare file.txt.orig and file.txt and output a .diff containing

--- file.txt    2021-05-03 17:47:12.685416342 +0200
+++ file.txt    2021-05-03 17:47:30.538750593 +0200
@@ -1 +1,2 @@
 foo bar
+bar baz

instead of diff -u file.txt.orig file.txt

--- file.txt.orig   2021-05-03 17:47:12.685416342 +0200
+++ file.txt    2021-05-03 17:47:30.538750593 +0200
@@ -1 +1,2 @@
 foo bar
+bar baz

The above does require me to manually edit the .diff file every time. If using .orig files automatically, would be possible at all, I would love to integrate vim patchmode with diff and have a valid patch without extra effort.

Perhaps there's an even better approach to this? Any suggestions would be appreciated. Both diff or git diff would work for my case.

2
  • 1
    If you explicitly specify the file you want to patch (like patch file.txt <file.diff), patch doesn't care about what file name is written inside file.diff.
    – raj
    May 3, 2021 at 19:24
  • Thank you, raj. That was very insightful as I had no idea that the file name was irrelevant for both patch and git apply. It makes more sense to me now.
    – zanona
    May 3, 2021 at 19:56

1 Answer 1

1

The workflow works for me. Perhaps you're testing by trying to apply the patch the file you've previously updated?

Preparation

echo this is original >file.txt.orig
echo this is new >file.txt
diff -u file.txt.orig file.txt >file.diff

cat file.diff
--- file.txt.orig       2021-05-03 19:17:13.038918015 +0100
+++ file.txt    2021-05-03 19:17:21.548900027 +0100
@@ -1 +1 @@
-this is original
+this is new

Create copy of original file to patch

mkdir x
cp -p file.txt.orig x/file.txt
cd x

cat file.txt
this is original

patch <../file.diff
patching file file.txt

cat file.txt
this is new
2
  • 1
    Thanks a lot for the example, roaima. You were right I about me patching a previously updated patch causing an unexpected behaviour. After checking your example I now realized that the name of the original file doesn't really matter as raj pointed out on his comment.
    – zanona
    May 3, 2021 at 19:54
  • 1
    Notice in my example that I specifically do not mention the file to be patched - I leave that for patch to infer from the patch file May 3, 2021 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .