Patches and Rejects in Software Configuration Management
In this conclusion to a four-part series on cross-platform software development, you'll learn how to handle patches and rejects. This article is excerpted from chapter three of the book Cross-Platform Development in C++, written by Syd Logan (Addison-Wesley; ISBN: 032124642X).
The patch program has a number of options. (You can refer to the patch man page for more details.) However, in practice, the only option that matters much is the -p argument, which is used to align the absolute paths used in the patch ﬁle with the local directory structure containing the sources that the patch is being applied to. When you run cvs diff to create a patch ﬁle, it is best to do it from within the source tree, at the highest level in the directory hierarchy necessary to include all the ﬁles that have changes. The resulting patch ﬁle will, for each ﬁle that has changes, identify the ﬁle with a relative path, and patch uses this relative path to ﬁgure out what ﬁle in the target directory to apply changes to. For example:
The ﬁrst line in the preceding patch (the line preﬁxed with Index:) speciﬁes the pathname of the ﬁle to be patched. Assuming that the patch is contained in a ﬁle named patch.txt, then, if the preceding patch ﬁle were copied to the same relative location in the target tree, then issuing the following command is sufﬁcient for patch to locate the ﬁles that are speciﬁed in the patch ﬁle:
$ patch -p0 < patch.txt
The -p argument will remove the smallest preﬁx containing the speciﬁed number of leading slashes from each ﬁlename in the patch ﬁle, using the result to locate the ﬁle in the local source tree. Because the patch ﬁle was copied to the same relative location of the target tree that was used to generate the patch ﬁle in the source tree, we must use -p0 because we do not want patch to remove any portion of the path names when searching for ﬁles. If -p1 were used (with the same patch ﬁle, located in the same place in the target tree), the pathname layout/layout.cpp would be reduced to layout.cpp, and as a result, patch would not be able to locate the ﬁle, because the ﬁle would not be located in the current directory. Copying the patch ﬁle down into the layout directory would ﬁx this, but this could only be done if, and only if, the patch ﬁle affected only sources that were located in the layout directory, because the -p0 is applied by patch to all sources represented in the patch ﬁle.