Using the 'patch' and 'diff' utilities
Windows Research Kernel @ HPIIn some upcoming postings, we will describe simple demo modifications of the Windows Research Kernel. The examples will be downloadable from this site as kernel patches and small demo applications.
The required tools can be downloaded from the Win32 GNU tools sourceforge page. You need the DiffUtils and the Patch tool. Download and install the tools on your workstation.
The diff
tool can be used to write the differences
between two source trees (normally a modified and an unmodified
version) into a single (text) file. With patch
a
created patch file can be applied to an unmodified source tree. In
this way, kernel modifications can be distributed without having to
provide the whole source code and without a common repository.
Creating a patch
It is assumed that you have a directory %temp% with two subdirectories %temp%\original and %temp%\modified. Each of this subdirectories should only contain the source code without object files or other intermediately created files. In a cmd-shell opened in the %temp% directory a patch can be created by typing
diff -Naur original modified --strip-trailing-cr >
test.patch
.
The -Naur
option tells the diff command that (1)
missing files in the either directory are considered as empty files,
(2) all files are considered as text files, (3) three lines of
"context" information are put into the patch file, and (3) the
source code directories are traversed and compared recursively.
Afterwards, the file test.patch
contains the patch
information which can be distributed by EMail or put on a website
for download.
A simple example: the following boxes show the original text file and the modified version. Both files are saved in a directory structure as described above.
abcdefg
hijklmn
opqrstu
vwxyz
1234567
abcdefg !!!
hijklmn
opqrstu
1234567
The diff
utility creates the following patch
file:
diff -Naur original/test.txt modified/test.txt
- original/test.txt 2007-05-22 14:50:51.841199200 +0200
+++ modified/test.txt 2007-05-22 14:56:16.670944400 +0200
@@ -1,5 +1,4 @@
-abcdefg
+abcdefg !!!
hijklmn
opqrstu
-vwxyz
-1234567
\ Kein Zeilenumbruch am Dateiende.
+1234567
The '+' and '-' signs at the beginning of the lines in the patch file indicate whether a line was added/modified or removed.
Applying a patch
It is assumed that you have an unmodified base source code tree of the original source code version saved in %original%.
If you get a patch file created as described above, you can copy it to the %original% directory and (if the patch file is named %name%.patch) type
patch -p1 < %name%.patch
.
This will apply the patch to the unmodified version and transform it into the patched version.
The p
option tells the patch
tool to
strip a specific number of leading components from file names used
in the patch file. An example: the patch file shown above contains
the directory names ("original" and "modified") used by the patch
creator. If you have a directory "foo" which contains the orginal
source code, the patch
utility needs information about
how to resolve the file names. Therefore, the first component (the
patch creation directory) can be stripped from the filenames by
using -p1
.
Summary
With diff
and patch
patches which contain
only the differences between two source code trees can be created
and re-applied to unmodified versions. In this way modified source
code versions can be easily distributed or made available for
download.