Can't apply unified diff patch on Solaris
Asked Answered
S

3

6

For example, if I have two files:

file1:

This is file 1

and file2:

This is file 2

and create patch with the following command:

diff -u file1 file2 > files.patch

result is:

--- file1       Fri Aug 13 17:53:28 2010
+++ file2       Fri Aug 13 17:53:38 2010
@@ -1,1 +1,1 @@
-This is file 1
+This is file 2

Then if I try to apply this patch on Solaris with patch command:

patch -u -i files.patch

it hangs on:

 Looks like a unified context diff.
File to patch:

1. Is there a way to use Solaris native patch command with unified diffs?

2. Which diff format is considered most portable if it's not possible to apply unified format?

Update: I've found answer on the first part of my question. Seems that patch on Solaris hangs if the second file (file2 in this case) exists in the same folder as the first one (file1). For example, the following quite common diff:

--- a/src/file.src       Sat Aug 14 23:07:29 2010
+++ b/src/file.src       Sat Aug 14 23:07:37 2010
@@ -1,2 +1,1 @@
-1
-
+2

will not work with quite common patch command:

patch -p1 -u -d a < file.patch

while the following diff (note second file is renamed):

--- a/src/file.src       Sat Aug 14 23:07:29 2010
+++ b/src/file_new.src       Sat Aug 14 23:07:37 2010
@@ -1,2 +1,1 @@
-1
-
+2

will work perfectly.

For the second part of my question see accepted answer below.

Stockyard answered 13/8, 2010 at 14:4 Comment(0)
L
1

Single Unix v2 and v3 both support context diffs but not unified diffs, so for better portability you should use context diffs (-c option to diff and patch).

On older Solaris releases (pre-10, I think), you need to make sure that /usr/xpg4/bin is before /usr/bin in your $PATH, otherwise you may get compatibility versions of some utilities instead of standard ones.

Lavone answered 13/8, 2010 at 18:54 Comment(3)
Thank you, Gilles for explanations. I'll consider using context diffs. As far as I understand, you can't write in context diff patch information about source file to change. Am I right? It's not very convenient ... Anyway, I still can't understand why I failed to run patch -u with diff produced by diff -u.Stockyard
@Shcheklein: What do you mean by “patch information about source file to change”? Context diffs do include file names. POSIX requires diff -cr to be supported, so this has to work. By the way, does patch -p0 make a difference?Rem
Yes, context diffs include enough information. However, the problem with Solaris's patch was the different (see update of the question). -p0 doesn't make any difference. Seems that patch on Solaris doesn't like when both names in hunk are same or both files exist.Stockyard
J
6

On Solaris /usr/bin/patch is an old version required to comply with some ancient standards. A modern version of GNU patch is provided as /usr/bin/gpatch on Solaris 8 and later.

Johnnyjumpup answered 13/8, 2010 at 15:36 Comment(1)
I know. It's possible that gpatch is not installed by default and it's not very good when compilation of your product failed because there is no gpatch.Stockyard
C
2
diff -cr old.new new.txt > patch.txt

gpatch -p0 < patch.txt

Works perfectly for me (using gpatch)

Clubby answered 2/3, 2012 at 18:12 Comment(0)
L
1

Single Unix v2 and v3 both support context diffs but not unified diffs, so for better portability you should use context diffs (-c option to diff and patch).

On older Solaris releases (pre-10, I think), you need to make sure that /usr/xpg4/bin is before /usr/bin in your $PATH, otherwise you may get compatibility versions of some utilities instead of standard ones.

Lavone answered 13/8, 2010 at 18:54 Comment(3)
Thank you, Gilles for explanations. I'll consider using context diffs. As far as I understand, you can't write in context diff patch information about source file to change. Am I right? It's not very convenient ... Anyway, I still can't understand why I failed to run patch -u with diff produced by diff -u.Stockyard
@Shcheklein: What do you mean by “patch information about source file to change”? Context diffs do include file names. POSIX requires diff -cr to be supported, so this has to work. By the way, does patch -p0 make a difference?Rem
Yes, context diffs include enough information. However, the problem with Solaris's patch was the different (see update of the question). -p0 doesn't make any difference. Seems that patch on Solaris doesn't like when both names in hunk are same or both files exist.Stockyard

© 2022 - 2024 — McMap. All rights reserved.