The Ten Minute Guide to diff and patch
2014-07-24 11:43
393 查看
Situation one: you are trying to compile a package from source and you discover that somebody has already done the work for you of modifying it slightly to compile on your system. They have made their work available as a "patch", but you're not sure how to make use of it. The answer is that you apply the patch to the original source code with a command line tool called, appropriately, patch.
Situation two: you have downloaded the source code to an open source package and after an hour or so of minor edits, you manage to make it compile on your system. You would like to make your work available to other programmers, or to the authors of the package, without redistributing the entire modified package. Now you are in a situation where you need to create a patch of your own, and the tool you need is diff.This is a quick guide to diff and patch which will help you in these situations by describing the tools as they are most commonly used. It tells you enough to get started right away. Later, you can learn the ins and outs of diff and patch at your leisure, using the man pages.
Applying patches with patch
To apply a patch to a single file, change to the directory where the file is located and call patch:patch < foo.patch
These instructions assume the patch is distributed in unified format, which identifies the file the patch should be applied to. If not, you can specify the file on the command line:
patch foo.txt < bar.patch
Applying patches to entire directories (perhaps the more common case) is similar, but you have to be careful about setting a "p level". What this means is that, within patch files, the files to be patched are identified by path names which may be different now that the files are located on your computer rather than on the computer where the patch was created. The p level instructs patch to ignore parts of the path name so that it can identify the files correctly. Most often a p level of one will work, so you use:
patch -p1 < baz.patch
You should change to the top level source directory before running this command. If a patch level of one does not correctly identify any files to patch, inspect the patch file for file names. If you see a name like
/users/stephen/package/src/net/http.c
and you are working in a directory that contains net/http.c, use
patch -p5 < baz.patch
In general, count up one for each path separator (slash character) that you remove from the beginning of the path, until what's left is a path that exists in your working directory. The count you reach is the p level.
To remove a patch, use the -R flag, ie
patch -p5 -R < baz.patch
Creating patches with diff
Using diff is simple whether you are working with single files or entire source directories. To create a patch for a single file, use the form:diff -u original.c new.c > original.patch
To create a patch for an entire source tree, make a copy of the tree:
cp -R original new
Make any changes required in the directory new/. Then create a patch with the following command:
diff -rupN original/ new/ > original.patch
That's all you need to get started with diff and patch. For more information use:
man diff man patch
Translations
This article has been translated and republished in the following languages:Spanish: Guia de 10 minutos de diff y patch
Portuguese: O Guia de Dez Minutos de diff e patch
French: diff et patch en dix minutes
Polish: 10 minut z diff i patch
German diff und patch in zehn Minuten
Comment on this article and its topic
Google Friend Connect is going or gone so it has been turned off! Commenting will return if I find something good to replace it.Copyright by Stephen Jungels. Written permission is required to repost or reprint this article
( from: http://jungels.net/articles/diff-patch-ten-minutes.html )
相关文章推荐
- The Ten Minute Guide to diff and patch
- The Ten Minute Guide to diff and patch
- A step-by-step guide to the use of the Intel OpenCV library and the Microsoft DirectShow technology
- Advanced Project Management: A Complete Guide to the Key Processes, Models and Technique
- The Essential Guide to CSS and HTML Web Design
- The IT Professional's Business and Communications Guide: A Real-World Approach to CompTIA A+ Soft Sk
- The Focal easy guide to Maya 5: for new users and professionals
- The 10-Minute Guide to RTOS (uc/os II)
- The Book of Wireless: A Painless Guide to Wi-Fi and Broadband Wireless [ILLUSTRATED]
- Pluggable Authentication Modules: The Definitive Guide to PAM for Linux SysAdmins and C Developers
- .NET Internationalization: The Developer's Guide to Building Global Windows and Web Applications
- Guide to Television and Video Technology, Fourth Edition: The Guide for the Digital Age - from HDTV,
- Database Administration: The Complete Guide to Practices and Procedures
- The Definitive Guide to Terracotta: Cluster the JVM for Spring, Hibernate and POJO Scalability
- The New Six Sigma: A Leader's Guide to Achieving Rapid Business Improvement and Sustainable Results
- IBM Rational(R) ClearCase(R), Ant, and CruiseControl: The Java(TM) Developer's Guide to Accelerating
- The Guru's guide to SQL server stored procedures, XML, and HTML
- use noscript html tag when user disable the javascript in browser, guide user how to enable the js in different browser and retu
- Implementing NAP and NAC Security Technologies: The Complete Guide to Network Access Control
- A Designer's Guide to Adobe InDesign and XML: Harness the Power of XML to Automate your Print and We