如何在 Windows 上应用差异补丁?
有很多程序可以创建差异补丁,但我在尝试应用一个程序时遇到了很大的困难。 我正在尝试分发补丁,但用户向我询问了如何应用该补丁。 于是我尝试自己弄清楚,结果发现我毫无头绪,而且我能找到的大多数工具都是命令行的。 (我可以处理命令行,但是如果没有一个漂亮、友好的 GUI,很多人会迷失方向。所以这些对于这个目的没有好处。)
我尝试使用 TortoiseSVN。 我有我想要应用的补丁。 我右键单击该补丁,TortoiseSVN 子菜单下有一个选项,显示“应用补丁”。 它所做的只是打开一个空窗口。
所以我尝试点击“打开”。 它有两个选项:合并和应用统一差异。 (幸运的是,补丁采用统一的差异格式。)但是应用选项只是简单地不起作用:它要求补丁和文件夹。 不知何故,它忘记了请求要应用补丁的文件!所以TortoiseSVN 根本不起作用。 是否有基于 Windows GUI 的实用程序可以获取补丁和文件并正确应用它?
编辑:看看到目前为止的回复,似乎只有当文件已经版本化时,Tortoise 才会正确执行。 这里的情况并非如此。 我需要能够对不是来自 SVN 存储库的文件应用补丁。 我刚刚尝试使用 Tortoise,因为我碰巧知道 SVN 使用差异并且必须知道如何创建它们并应用它们。
There are plenty of programs out there that can create a diff patch, but I'm having a heck of a time trying to apply one. I'm trying to distribute a patch, and I got a question from a user about how to apply it. So I tried to figure it out on my own and found out that I have no clue, and most of the tools I can find are command-line. (I can handle a command line, but a lot of people would be lost without a nice, friendly GUI. So those are no good for this purpose.)
I tried using TortoiseSVN. I have the patch I'd like to apply. I right-click on the patch, and there's an option under the TortoiseSVN submenu that says "Apply patch." All it does is pull up an empty window.
So I tried hitting Open. It has two options: merge and apply unified diff. (The patch is in unified diff format, luckily.) But the apply option just plain doesn't work: It asks for the patch and a folder. Somehow it forgot to ask for the file to apply the patch to! So TortoiseSVN just plain doesn't work. Is there a Windows GUI-based utility that will take a patch and a file and apply it properly?
EDIT: Looking at the replies so far, it seems that Tortoise will only do it right if it's a file that's already versioned. That's not the case here. I need to be able to apply a patch to a file that did not come out of an SVN repository. I just tried using Tortoise, because I happen to know that SVN uses diffs and has to know how to both create them and apply them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
应用补丁
使用 TortoiseMerge:
如果您从 TortoiseMerge 打开,则可以使用替代屏幕。 在下面的屏幕中,目录指的是上面步骤 2 中提到的“merges”目录:
WinMerge GUI 的屏幕截图:
Apply Patch
With TortoiseMerge:
Alternative screeny if you Open from TortoiseMerge. In the screeny below, directory refers to the "merges" directory mentioned at step 2 above:
Screenshot of WinMerge GUI:
我为此制作了纯Python工具。 它具有可预测的跨平台行为。 尽管它不创建新文件(在撰写本文时)并且缺少 GUI,但它可以用作创建图形工具的库。
更新:如果安装了Python,使用起来应该会更方便。
I made pure Python tool just for that. It has predictable cross-platform behavior. Although it doesn't create new files (at the time of writing this) and lacks a GUI, it can be used as a library to create graphic tool.
UPDATE: It should be more convenient to use it if you have Python installed.
Git 安装中的 patch.exe 实用程序适用于 Windows 10。
安装 Git for Windows,然后使用
“C:\Program Files\Git\usr\bin\patch.exe”
命令来应用补丁。如果在应用补丁期间输出中出现任何错误消息,例如
Hunk #1 FAILED at 1 (不同的行结尾).
,请尝试添加-l
(这是--ignore-whitespace
的快捷方式)或--binary
切换到命令行。The patch.exe utility from the Git installation works on Windows 10.
Install Git for Windows then use the
"C:\Program Files\Git\usr\bin\patch.exe"
command to apply a patch.If any error message like a
Hunk #1 FAILED at 1 (different line endings).
had been got on the output during applying a patch, try to add the-l
(that is a shortcut for the--ignore-whitespace
) or the--binary
switches to the command line.TortoiseMerge 是一个与 TortoiseSVN 捆绑在一起的独立实用程序。
也可以在 TortoiseDiff.zip 存档中单独下载。 这将允许您将统一差异应用于非版本化文件。
TortoiseMerge is a separate utility that comes bundled with TortoiseSVN.
It can also be can be downloaded separately in the TortoiseDiff.zip archive. This will allow you to apply unified diffs to non-versioned files.
我知道您说过您更喜欢 GUI,但命令行工具可以很好地完成这项工作。 请参阅 GnuWin 了解 Unix 工具到 Windows 的移植。 显然,您需要 patch 命令;-)
不过,您可能会遇到线路终止问题。 GnuWin 端口将假定补丁文件具有 DOS 风格的行终止符 (CR/LF)。 尝试在相当智能的编辑器中打开补丁文件,它会为您转换它。
I know you said you would prefer a GUI, but the commandline tools will do the work nicely. See GnuWin for a port of unix tools to Windows. You'd need the patch command, obviously ;-)
You might run into a problem with the line termination, though. The GnuWin port will assume that the patchfile has DOS style line termination (CR/LF). Try to open the patchfile in a reasonably smart editor and it will convert it for you.
在 TortoiseSVN 中,补丁应用确实有效。 您需要将补丁应用到创建补丁的同一目录。。 牢记这一点始终很重要。 下面是在 TortoiseSVN 中执行此操作的方法:
右键单击要应用补丁的文件夹。 它将显示一个对话框,询问补丁文件的位置。 选择文件,这将打开一个小文件列表窗口,其中列出了已更改的文件,单击每个项目将打开一个差异窗口,显示补丁将对该文件执行的操作。
祝你好运。
In TortoiseSVN, patch applying does work. You need to apply the patch to the same directory as it was created from. It is always important to keep this in mind. So here's how you do it in TortoiseSVN:
Right click on the folder you want to apply the patch to. It will present a dialog asking for the location of the patch file. Select the file and this should open up a little file list window that lists the changed files, and clicking each item should open a diff window that shows what the patch is about to do to that file.
Good luck.
您可以使用补丁实用程序的此 Win32 本机端口。
它附带了更多可供选择的其他实用程序,与 Cygwin 和类似工具相比,它不需要任何 DLL 或类似工具。 只需选择您选择的小型可执行文件并将其存储在您想要的任何地方。
简单用法:
获取更多帮助:
You can use this Win32 native port of the patch utility.
It comes along with a bigger selection of other utilities and in contrast to Cygwin and similar it does not need any DLLs or similar. Just pick your tiny executable of choice and store it wherever you want.
Simple usage:
Get more help:
我已经在使用 BeyondCompare(商业)进行差异和合并,并且该工具还具有功能< /a> 创建、查看和应用补丁。
I am already using BeyondCompare (commercial) for diffs and merges, and this tool also has the capability to create, view and apply patches.
我使用 http://www.msys2.org/ 中的 MSYS2
它提供了许多实用程序,例如
patch
、which
、git
、tree
等等。安装 MSYS2 后,只需运行包管理器来安装补丁:
I use MSYS2 from http://www.msys2.org/
It provides many utilities like
patch
,which
,git
,tree
, and many more.After installing MSYS2 simply run the package manager to install
patch
:您可以安装Cygwin,然后使用命令行patch工具应用补丁。 另请参阅此 Unix 手册页,它适用于补丁 。
You can install Cygwin, then use the command-line patch tool to apply the patch. See also this Unix man page, which applies to patch.
看来 TortoiseSVN (TortoiseMerge) 需要行
Index: foobar.py
在 diff/patch 文件中。 这就是我需要做的事情,使非 TortoiseSVN 补丁文件与 TortoiseSVN 的右键单击应用补丁命令一起使用。
之前:
之后:
或者,如果您知道贡献者所使用的具体修订版:
It appears that TortoiseSVN (TortoiseMerge) requires the line
Index: foobar.py
in the diff/patch file. This is what I needed to do to make a non-TortoiseSVN patch file work with TortoiseSVN's right-click Apply Patch command.Before:
After:
Or if you know the specific revision your contributor was working from:
使用 git Diff 或 linux patch 使用 git diff 在 Windows 上应用补丁
在 Linux、MacOS 或其他任何地方使用 GNU patch 命令或 git diff 创建的补丁都可以使用 git apply 在 Windows 上应用。
:
创建补丁 例如,从 2 个目录(其中 1 个或多个文件已更改)创建
补丁 0001-path-file.patch
,或者使用 git diff
git diff original_dir modded_dir > 0001-path-file.patch
然后复制windows环境下的.patch文件以及包含原始未更改文件的original_dir目录。
使用 git diff 申请:
1 复制父目录下的补丁文件
2 cd 到原目录
3 使用 git apply 应用补丁
Using git Diff or linux patch to apply a patch on windows using git diff
Patches created anywhere on linux, MacOS or else, using the GNU patch command or git diff can be all applied on windows using git apply.
Create the patch
For instance to create the patch from 2 directories in which 1 or more files have been changed:
diff -Naru original_dir modified_dir > 0001-path-file.patch
OR using git diff
git diff original_dir modified_dir > 0001-path-file.patch
Then copy the .patch file on the windows environment as well as the original_dir directory that contains the original unchanged files.
Apply with git diff:
1 Copy the patch file in the parent directory
2 cd into the original directory
3 Apply the patch using git apply
补丁告诉它要应用到哪个文件。 标题应该类似于(在记事本或您最喜欢的文本编辑器中查看):
对于 Subversion 补丁,您也会有修订号(因为文件名相同)。
GNU 补丁会让你覆盖这些名称,但我不不知道有什么 GUI 工具可以做同样的事情。 我会检查各种差异程序 - 不过,WinMerge 似乎不支持应用补丁。
The patch tells it what file to apply to. The header should be something like (view it in Notepad or your fav text editor):
In the case of a Subversion patch, you'd have revision numbers as well (since the file names are the same).
GNU patch will let you override those names, but I don't know of any GUI tools to do the same. I'd check with the various diff programs - though, it does not appear WinMerge supports applying patches.
Eclipse 应该能够做到这一点,进入 TeamSynchronize 透视图,然后进入 Project->Apply patch
Eclipse should be able to do it, go to TeamSynchronize perspective and then into Project->Apply patch
对于 Java 项目,我使用 NetBeans 来应用补丁文件。 如果您要修补的 Java 代码还不是 NetBeans 项目,请为其创建一个项目。 要创建新项目:
现在您已经有了一个项目,请应用补丁:
就是这样。 您的补丁应该被应用,并且您应该看到一个显示更改的差异窗口。
For Java projects, I have used NetBeans to apply patch files. If the Java code you are patching is not already a NetBeans project, create a project for it. To create a new project:
Now that you have a project, apply the patch:
That's it. Your patch should be applied, and you should see a diff window showing the changes.
如果您使用 Mercurial,这是通过“导入”完成的。 因此,在命令行中,使用
hg import
命令,或者(您可能会发现--no-commit
选项很有用),或者“Repository”=> Hg 工作台中的“导入...”。请注意,这些将默认提交更改; 如果使用命令行,您可以使用
hg import --no-commit
选项来避免这种情况,或者如果您使用 Hg Workbench,您可能会发现发出hg rollback
很有用合并后。If you are using Mercurial, this is done via "import". So at the command line, the
hg import
command, or (you may find the--no-commit
option useful), or "Repository" => "Import..." in Hg Workbench.Note that these will commit the changes by default; you can avoid this using
hg import --no-commit
option if using the command-line, or if you used Hg Workbench, you might find it useful to issuehg rollback
after the merge.如果您在 Windows 上安装了 git,并且想要为 git 存储库应用补丁,您只需从 Windows Power Shell 执行以下操作:
If you have git install on Windows and you want to apply a patch for a git repository, you can simply do from a Windows Power Shell:
当使用 TortoiseSVN 应用补丁时,我通常将路径保存在签出存储库的根目录中。 然后,您应该能够右键单击补丁,转到 TortoiseSVN 菜单,然后单击 ApplyPatch。 ApplyPatch 应该自动确定补丁是在目录层次结构中的哪个级别创建的。
然而,我过去在应用包含新文件或涉及文件重命名的补丁时遇到过问题。 无论 Tortoise 使用什么算法,似乎都不能很好地处理这些场景。 Unicode 也会给你带来类似的问题。
When applying patches using TortoiseSVN, I typically save the path in the root of the checked out repository. You should then be able to right click on the patch, go to the TortoiseSVN menu, and click ApplyPatch. ApplyPatch should automatically figure out which level in the directory hierarchy the patch was created.
I have, however, had issues in the past with applying patches that contain new files, or which involve renames to files. Whatever algorithm Tortoise uses for this doesn't seem to handle those scenarios very well. Unicode can give you similar issues.
如果您收到“不是工作副本”错误消息,请尝试从 TortoiseMerge 对话框中选择一个目录,该目录是 SVN 的工作目录。
If you are getting "Not a working copy" error message then try selecting a directory from TortoiseMerge dialog box which is a working directory of SVN.
适用于 Windows 的 BusyBox 端口同时具有 diff 和 patch 命令,但它们只支持统一格式。
A BusyBox port for Windows has both a diff and patch command, but they only support unified format.
只需使用:
记住仅从创建补丁的文件夹位置执行此命令。
Just use:
remember execute this command only from the folder location where you created the patch.
为了响应之前有关补丁和 Python 3 的查询,我收到了错误
TypeError:只能将 str (不是“字节”)连接到 str
我的解决方案是在 _normalize_filenames 函数中更改源:
然后补丁对我来说效果很好(Python 3,Windows 10)
in response to the query earlier about patch and Python 3, I got the error
TypeError: can only concatenate str (not "bytes") to str
The solution for me was to change the source, in the _normalize_filenames function:
then patch worked fine for me (Python 3, Windows 10)