views:

139

answers:

2

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.

+1  A: 

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.

alanc
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.
Shcheklein
A: 

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.

Gilles
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.
Shcheklein
@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?
Gilles
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.
Shcheklein