patch diff stuff

翻译 2012年03月23日 18:12:47

The original article is here


Applying A Patch

Applying a 'patch' (output from diff -u) is done with the 'patch' tool inside the Bash Shell (i.e.Cygwin). It is best to put the patch files in the same directory as the source code and will make it easier for you when you have to apply them.

  cd to/source/root
  patch < patchfile

patch knows that the patchfile is a set of changes on one or more files, and will do those to your local files. If your files have changed too much for the patch to work, it will save the sections of the patch that aren't possible to apply in a file called "filename.rej" (filename being the name of the file for which the failing section was intended for). Then you must take care of them manually.

If there is path information in the patchfile that you want to cut off from the left, tell patch how many directory levels to cut off to find the names in your file system:

  patch -p0 < patchfile
  patch -p1 < patchfile
  patch -p2 < patchfile

... each example line removes one extra level of dir info from the left.

You can use the --dry-run option to patch to make sure that the patch applies clean. It doesn't actually apply the patch, only prints what would happen if you run it.



Removing A Patch

You can remove a patch again from the sources by doing the reverse action of a specific patch. You do this with the -R (or --reverse) options, such as:

  patch -p1 -R < patchfile




These tools will assume and operate on "unix-style" newlines. That means all files that you're diffing and patching must have LF newlines only, and not the Windows/DOS standard CRLF newlines.

Do you have a newline problem? You might want to try Dos2unix or unix2dos.

  dos2unix -d filename

To change Dos CRLF to Unix LF newline style.

  dos2unix -u filename

To change Unix LF to Dos CRLF newline style.

both commands will change and save the specific file. You can put an asterisk (*) instead of filename to go through a library, or

  find -exec dos2unix -U {} \;

To run recursively on every subdirectory and file.



Problems with Windows Patch

For some users, the Windows patch program above malfunctions. If patch doesn't output anything, set up a build environment as described on this page.



Creating A Patch

We generate diffs (often called patches) using 'diff' in a manner similar to this:

  diff -u oldfile newfile > patch

People who have checked out code with SVN can do diffs using svn like this:

  svn diff file > patch

'diff' can also be used on a whole directory etc to generate one file with changes done to multiple:

  diff -u -r olddir newdir > patch

The -u option means the output is using the 'unified diff' format. Older diff programs don't have that, and then -c (for 'context diff') is OK.

The -r option recursively runs diff command on every subdirectory and file.

Include New Files

Newly created files can be included with the help of 'svn add'.

First add the files like this:

  svn add file

after adding the files they can be included in the patch:

  svn diff [files] > patch


Linux的diff和git diff生成patch/打patch

一、Linux的diff生成patch和打patch 1、单个文件生成patch  #  diff -up xxx/a.txt yyy/b.txt  > mypatch_1  参数解释:   -u:显...
  • u010164190
  • u010164190
  • 2017年04月27日 19:25
  • 2754


1,制作补丁 diff的使用 diff可以完成比较功能,生成补丁文件 格式::diff [option] oldfile newfile 常用的option选项有: -r 对目录进行递归处理 -u...
  • zqixiao_09
  • zqixiao_09
  • 2016年07月05日 21:31
  • 3853

diff 和patch

对于开源源码修改过程中的必经阶段:对源码打补丁,总是不够精通,搜索了补丁的原理的详细过程,如下: 在移植或版本升级过程中,手动比对(用比对工具)转换是很费力的事情,特别是发生变化...
  • gjsisi
  • gjsisi
  • 2013年11月11日 13:18
  • 1573

diff && patch 命令制作补丁

diff 命令diff可以比较两个文件,并可同时记录下二者的区别。制作补丁时的一般用法和常见选项为: diff 【选项】 源文件(夹) 目的文件(夹) -r:递归,设置后diff会将两个不同...
  • ZCF1002797280
  • ZCF1002797280
  • 2015年10月28日 14:36
  • 2873


在做开发的过程中难免需要给内核及下载的一些源码打补丁,所以我们先学习下Linux下使用如如何使用diff制作补丁以及如何使用patch打补丁。 通过diff命令比较出新旧版本之间的差异,并以pa...
  • tao_627
  • tao_627
  • 2016年10月26日 22:37
  • 2578

【Git】diff 与 patch

diff用法 1 {% extends "japanese/index.html" %} 2 {% block article %} 3 4 5 煩い(うるさい) 6 同じことが何度も繰り返され...
  • FeeLang
  • FeeLang
  • 2014年04月22日 14:31
  • 2982

Diff/Patch 工具的使用

补丁Patch是天才程序员、Perl的发明者Larry Wall发明的,它应高效地交流程序源代码之需求而生,随着以Linux为代表的开发源代码运行的蓬勃发展,patch这个概念已经成为开放源代码发起者...
  • zhanglianpin
  • zhanglianpin
  • 2014年06月09日 09:57
  • 4594

diff 和 patch 的入门(及 Windows 下的用法)

自从我开始使用 Git 以及接触 Linux 之后,我发现我越来越离不开 Linux 上那些方便好用的工具。比如 diff &patch。     如果修改了开源代码,为了方便分享(如提交漏洞)或自...
  • vigour1000
  • vigour1000
  • 2014年04月05日 12:22
  • 3152

svn diff 和patch的使用

生成diff文件: svn diff > xxx.diff  生成 patch: 同事 A 运行如下命令生成 patch: svn diff > aaa.patch   应用 pa...
  • zhangzhizhen1988
  • zhangzhizhen1988
  • 2012年12月25日 14:22
  • 4370


  • exbob
  • exbob
  • 2011年07月15日 20:14
  • 2013
您举报文章:patch diff stuff