patch 命令用于打补丁,补丁文件是使用diff产生的
patch 命令语法
patch [ -b [ -B Prefix ] ] [ -f ] [ -l ] [ -N ] [ -R ] [ -sSEO靠我 ] [ -v ] [ -c | -e | -n ] [ -d Directory ] [ -D Define ] [ -F Number ] [ -i PatchFile ] [ -oSEO靠我 OutFile ] [ -p Number ] [ -r RejectFile ] [ -x Number ] [ File ]
patch 命令失败或拒绝接受补丁时,会产生一个和原文件同名,以SEO靠我".rej"为后缀的差异文件。
当知道 -b 时,会产生一个和原文件同名,以".orig"为后缀的备份文件。常使用的 patch 参数:
-p 指定目录级别(从路径全称中除去几层目录)
如,如果补丁文件包含SEO靠我路径名称 /curds/whey/src/blurfl/blurfl.c,那么:
-p 0 使用完整路径名
-p 1 除去前导斜杠,留下 curds/whey/src/blurfl/blurfl.c。
-pSEO靠我 4 除去前导斜杠和前三个目录,留下 blurfl/blurfl.c。-d Directory 打补丁前,更改当前目录到指定目录
-i PatchFile 从指定文件,而不是从标准输入中读取补丁信息
-RSEO靠我 逆向补丁,这个选项在防止打错补丁很有用处
补丁的产生一般用
diff -Nrua a b > c.patch
如:
#diff -Nrua linux-2.6.14/Makefile linux-2SEO靠我.6.26/Makefile >c.patch
#cat c.patch--- linux-2.6.14/Makefile 2008-07-30 16:54:20.000000000 +0800
+++ SEO靠我linux-2.6.26/Makefile 2008-07-14 05:51:29.000000000 +0800
@@ -1,8 +1,8 @@
VERSION = 2
PATCHLEVEL = 6
-SUSEO靠我BLEVEL = 14
+SUBLEVEL = 26
EXTRAVERSION =
-NAME=Affluent Albatross
+NAME = Rotary Wombat
# *DOCUMENTATIONSEO靠我*
# To see a list of typical targets execute "make help"--- 的文件表示将被打补丁的文件 如:linux-2.6.14/Makefile
+++ SEO靠我的文件表示补丁来源文件 如:linux-2.6.26/Makefile应用 patch
#ls
c.patch linux-2.6.14 linux-2.6.26
#cd linux-2.6.14
#SEO靠我patch -p1 <../c.patch如果有多个补丁要打,则应该注意打补丁的顺序!
1命令应用
1.1 用SEO靠我途1.2 语法1.3 描述2文件名
3应用程序
4准备补丁
4.1 原则4.2 标志4.3 示例4.4 文件/usr/bin/patch 包含 patch 命令。
AIX Version 4.3 Commands Reference, Volume 4
Applies changes to files.
The patchcommand reads a source files instructions on how to change a filSEO靠我e, then applies the changes. The source file contains difference listings (or diff listings) produceSEO靠我d by thediffcommand. The diff listings are the result of comparing two files and building instructioSEO靠我ns about how to reconcile the differences. By default, thepatchcommand uses the source file read froSEO靠我m standard input, but this can be overridden using the-i flag and the PatchFile variable.
Diff listingSEO靠我s come in three formats: normal, context, or in the style of the ed editor. Thepatchcommand determinSEO靠我es the diff listing format, unless overruled by the-c, -e, or -n flags.
By default, the patched version SEO靠我of a file replaces the original version. When the-bflag is specified, the original of each patched fSEO靠我ile is saved in a file of the same name with the suffix.origappended to it. You may also specify wheSEO靠我re you want the output to go with the-o flag.
The -pflag makes it possible to customize a patch file toSEO靠我 local user directory structures without manually editing the patch file. This is done by specifyingSEO靠我 how many components to remove from the full path name. For example, if the patch file contained theSEO靠我 path name /curds/whey/src/blurfl/blurfl.c, then:
-p 0 causes the entire path name to be used.-p 1remoSEO靠我ves the leading slash, leaving curds/whey/src/blurfl/blurfl.c.-p 4removes leading slashes and the fiSEO靠我rst three directories, leaving blurfl/blurfl.c.Not specifying the -p flag causes the patchcommand to usSEO靠我e the base name. In the above example, that would be blurfl.c.
Patch File FormatThe patch file must cSEO靠我ontain one or more lines of header information followed by one or more patches. Each patch must contSEO靠我ain one or more lines of file name identification in the format produced by thediff -ccommand, and onSEO靠我e or more sets of diff command output, customarily called hunks.
The patchcommand skips any leading textSEO靠我 in a patch file, applies the actual diff listing, and skips any trailing text. Thus, you could use SEO靠我as a patch file an article or message that includes a diff listing, and thepatchcommand would still SEO靠我work. In such a case, if the entire diff listing is indented by a consistent amount, thepatchcommandSEO靠我 will also adjust for that spacing.
To change a line range within the original file, each hunk withinSEO靠我 a patch must be a separate diff listing. The line numbers for successive hunks within a patch must SEO靠我occur in ascending order.
File Name DeterminationIf no File parameter is specified, the patchcommand pSEO靠我erforms the following steps to obtain the name of the file to edit:
In the header of a context diff lSEO靠我isting, the file name is determined from lines beginning with *** (three asterisks) or --- (three daSEO靠我shes). A line beginning with *** indicates the name of the file from which the patches were taken, wSEO靠我hile a line beginning with --- indicates the name of the file to which the patches should be appliedSEO靠我. The shortest name of an existing file is selected.If there is an Index: line in the leading text, SEO靠我thepatchcommand tries to use the file name from that line.A context diff header takes precedence oveSEO靠我r an Index: line.If no file name can be determined from the leading text, thepatchcommand prompts yoSEO靠我u for the name of the file to patch.If the original file cannot be found, but a suitable SCCS or RCSSEO靠我 file is available, thepatchcommand attempts to get or check out the file.If the leading text contaiSEO靠我ns a Prereq: line, thepatchcommand takes the first word from the prerequisites line (normally a versSEO靠我ion number) and checks the input file to see if that word can be found. If not, thepatchcommand promSEO靠我pts you for confirmation before proceeding. Patch ApplicationIf the patch file contains moreSEO靠我 than one patch, thepatchcommand tries to apply each diff listing as if it came from a separate patcSEO靠我h file. In this case, the name of the file to patch is determined for each diff listing, and the heaSEO靠我der text before each diff listing is examined for information such as file name and revision level.
ISEO靠我f you specify the-c, -e, or -n flag, the patchcommand interprets information within each hunk as a conteSEO靠我xt difference, an ed editor difference, or a normal difference, respectively. Otherwise, thepatchcomSEO靠我mand determines the type of difference based on the format of the information within the hunk.
The patSEO靠我chcommand searches for the place to apply each hunk by taking the first line number of the hunk and SEO靠我adding or subtracting any line offset caused by applying the previous hunk. If an exact match is notSEO靠我 possible at this line location, thepatchcommand scans both forward and backward for a set of lines SEO靠我matching the hunks content exactly.
If no such place is found, and if the patchcommand is applying a cSEO靠我ontext diff listing, thepatchcommand can search for a less exact match. A fuzz factor specifies how SEO靠我many lines can be inexactly matched. If the fuzz factor is set to 1 or more, thepatchcommand performSEO靠我s a second scan, this time ignoring the first and last line of context. If no match results, and theSEO靠我 maximum fuzz factor is set to 2 or more, thepatchcommand performs a third scan, this time ignoring SEO靠我the first two lines and the last two lines of the context. (The default maximum fuzz factor is 2.) ISEO靠我f no match is found, thepatchcommand places the hunk in a reject file. The reject file is created wiSEO靠我th the same name as the output file and the suffix.rej. This naming convention can be overridden by SEO靠我using the -r flag.
The rejected hunk is written in context diff listing form, regardless of the format SEO靠我of the patch file. If the input was a normal or ed editor style difference, the reject file may contSEO靠我ain differences with zero lines of context format. The line numbers on the hunks in the reject file SEO靠我may be different from the line numbers in the patch file. This is because the reject file line numbeSEO靠我rs reflect the approximate locations for the failed hunks in the new file rather than the old one.
AsSEO靠我 each hunk is completed, thepatchcommand tells you whether the hunk succeeded or failed. You are alsSEO靠我o informed of the new line number assumed for each hunk. If this is different from the line number sSEO靠我pecified in the diff listing, you are notified of the offset. Thepatchcommand also tells you if a fuSEO靠我zz factor was used to make the match.
Note: A single large offset may be an indication that a hunk wasSEO靠我 installed in the wrong place. Use of a fuzz factor may also indicate bad placement. PreparSEO靠我ing Patches for Other UsersProgrammers preparing patches that will be shipped to other users should SEO靠我consider the following additional guidelines:
If you try to apply the same patch twice, the patchcommaSEO靠我nd assumes the second application should be a reverse patch and prompts you for confirmation of thisSEO靠我 reversal. Therefore, avoid sending out reversed patches, since this makes users wonder whether theySEO靠我 already applied the patch.It is recommended that you keep apatchlevel.hfile that is updated with thSEO靠我e latest patch level. The patch level can then be used as the first diff listing in the patch file ySEO靠我ou send out. If your patch includes a Prereq: line, users cannot apply patches out of order without SEO靠我receiving a warning.Make sure you specify the file names correctly, either in a context diff listingSEO靠我 header or with an Index: line. If you are patching something in a subdirectory, be sure to tell theSEO靠我 patch user to specify a-pflag as needed.You can create a file by sending out a diff listing that coSEO靠我mpares a null file to the file you want to create. However, this only works if the file you want to SEO靠我create does not already exist in the target directory.While you may be able to put many diff listingSEO靠我s into one file, it is advisable to group related patches into separate files.Thepatchcommand cannotSEO靠我 tell if the line numbers are incorrect in an ed script, and can only detect bad line numbers in a nSEO靠我ormal diff listing when it finds a change or a delete command. A context diff listing using a fuzz fSEO靠我actor of 3 may have the same line-number problem. Until a suitable interactive interface is added, uSEO靠我se a context diff listing in such cases to check the changes for accuracy. Compilation without errorSEO靠我s usually means that the patch worked, but it is not an infallible indicator.The results of thepatchSEO靠我command are guaranteed only when the patch is applied to exactly the same version of the file from wSEO靠我hich the patch was generated.If the code has been duplicated, for example: #ifdef ... NEWCODSEO靠我E #else ... OLDCODE # endif thepatchcommand is incapable of patching both veSEO靠我rsions. If thepatchcommand succeeds, it may have patched the wrong version and return a successful eSEO靠我xit status.The Define variable is uSEO靠我sed as the differentiating symbol. This flag only works when the normal or context form of diff listSEO靠我ing is used as a patch file.
-eInterprets the patch file as an ed editor script. This flag cannot be SEO靠我used with the-c or -n flag.-fSuppresses queries to the user. To suppress commentary, use the -s flag.-FNuSEO靠我mberSets the maximum fuzz factor. This flag applies to context diff listings only and causes thepatcSEO靠我h command to ignore the specified number of lines when determining where to install a hunk. If the -FfSEO靠我lag is not specified, the default fuzz factor is 2. The factor may not be set to more than the numbeSEO靠我r of lines of content in the context diff listing (ordinarily 3).Note: A larger fuzz factor increasesSEO靠我 the odds of a faulty patch.-iPatchFileReads the patch information from the specified file, rather tSEO靠我han from standard input.-l(lowercase L) Causes any sequence of blank characters in the diff listing SEO靠我script to match any sequence of blank characters in the input file. Other characters are matched exaSEO靠我ctly.-nInterprets the script as a normal diff listing. This flag cannot be used with the -c or -e flag.-SEO靠我NIgnores patches where the differences have already been applied to the file. By default, already-apSEO靠我plied patches are rejected.-oOutFileCopies the files to be patched, applies the changes, then writesSEO靠我 the modified version to the specified output file. Multiple patches for a single file are applied tSEO靠我o the intermediate versions of the file created by any previous patches. Therefore, multiple patchesSEO靠我 result in multiple, concatenated versions of the output file.-pNumberSets the path name strip countSEO靠我, which controls how path names found in the patch file are treated. This flag is useful if you keepSEO靠我 your files in a directory different from the specified path. The strip count specifies how many slaSEO靠我shes are stripped from the front of the path name. Any intervening directory names are also strippedSEO靠我. For example, assume a patch file specified /u/leon/src/blurf1/blurf1.c:-p 0leaves the entire path SEO靠我name unmodified.-p 1 removes the leading slash, leaving u/leon/src/blurf1/blurf1.c.-p 4removes four sSEO靠我lashes and three directories, leaving blurf1/blurf1.c.If the -pflag is not specified, only the base nSEO靠我ame (the final path name component) is used. This flag works only when the File parameter is not speSEO靠我cified.
-rRejectFileOverrides the default reject file name. The default reject file name is formed bySEO靠我 appending the suffix.rej to the original file name.-RReverses the sense of the patch script. For exaSEO靠我mple, if the diff listing was created from new version to old version, using the-R flag causes the patSEO靠我chcommand to reverse each portion of the script before applying it. Rejected differences are saved iSEO靠我n swapped format. The-Rflag cannot be used with ed scripts, because there is too little information SEO靠我to reconstruct the reverse operation. If the-R flag is not specified, the patchcommand attempts to appSEO靠我ly each portion in its reversed sense as well as in its normal sense, until a portion of the patch fSEO靠我ile is successfully applied. If the attempt is successful, the user is prompted to determine if the-SEO靠我R flag should be set. Note: This method cannot detect a reversed patch if used with a normal diff listiSEO靠我ng where the first command is an append (that is, would have been a delete). Appends always succeed SEO靠我because a null context matches anywhere. Fortunately, most patches add or change lines rather than dSEO靠我elete lines. Therefore most reversed normal diff listings begin with a delete, causing a failure andSEO靠我 triggering heuristics.-sPatches silently unless an error occurs.-vPrints the revision header and paSEO靠我tch level. If the-v flag is used with other flags, the other flags are ignored.-xNumberSets internal SEO靠我debugging flags. This flag is only forpatch command developers.The following exit values aSEO靠我re returned:
0Successful completion.1An error occurred.The diff command, ed command.
网站备案号:浙ICP备17034767号-2