如何在 Windows 上应用差异补丁?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/517257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I apply a diff patch on Windows?
提问by Mason Wheeler
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.)
有很多程序可以创建差异补丁,但我有很多时间尝试应用一个。我正在尝试分发补丁,但我从用户那里得到了一个关于如何应用它的问题。于是我自己摸索了一番,却发现自己毫无头绪,能找到的大部分工具都是命令行的。(我可以处理命令行,但是如果没有一个漂亮、友好的 GUI,很多人会迷失方向。所以这些对这个目的没有好处。)
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.
我尝试使用 TortoiseSVN。我有我想应用的补丁。我右键单击补丁,在 TortoiseSVN 子菜单下有一个选项,上面写着“应用补丁”。它所做的只是拉起一个空窗口。
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?
所以我尝试点击 Open。它有两个选项:合并和应用统一差异。(幸运的是,补丁是统一的差异格式。)但是简单的应用选项不起作用:它要求补丁和文件夹。不知何故,它忘记要求应用补丁的文件!所以 TortoiseSVN 只是简单的不起作用。是否有基于 Windows GUI 的实用程序可以获取补丁和文件并正确应用它?
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.
编辑:查看到目前为止的回复,似乎 Tortoise 只有在它是一个已经版本化的文件时才会正确执行。这不是这里的情况。我需要能够将补丁应用于不是来自 SVN 存储库的文件。我只是尝试使用 Tortoise,因为我碰巧知道 SVN 使用差异,并且必须知道如何创建和应用差异。
回答by Sheriff Md
Apply Patch
应用补丁
With TortoiseMerge:
使用 TortoiseMerge:
- Find and open an existing SVN repo directory
- Create a new directory named "merges", if it does not exist already
- Copy the file onto which you want to apply the .patch file
- ADD and COMMIT to the svn repository before you continue to the next step
- Right click on merges and choose Apply patch...
- Double click the file from list
- The patched file with diff is displayed on the right pane
- Click on that pane and hit Saveor export with File->Save As...
- 查找并打开现有的 SVN 存储库目录
- 创建一个名为“merges”的新目录,如果它不存在的话
- 复制要应用 .patch 文件的文件
- 在继续下一步之前,将 ADD 和 COMMIT 添加到 svn 存储库
- 右键单击合并并选择应用补丁...
- 双击列表中的文件
- 带有差异的修补文件显示在右侧窗格中
- 单击该窗格并点击保存或导出文件->另存为...
Alternative screeny if you Open from TortoiseMerge. In the screeny below, directory refers to the "merges" directory mentioned at step 2 above:
如果您从 TortoiseMerge 打开,则替代筛选。在下面的屏幕中,目录是指上面第 2 步中提到的“合并”目录:
Screenshot of WinMerge GUI:
WinMerge GUI 的屏幕截图:
回答by anatoly techtonik
I made pure Python tooljust 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.
我为此制作了纯 Python 工具。它具有可预测的跨平台行为。尽管它不创建新文件(在撰写本文时)并且缺少 GUI,但它可以用作创建图形工具的库。
UPDATE: It should be more convenient to use it if you have Python installed.
更新:如果您安装了 Python,使用它应该会更方便。
pip install patch
python -m patch
回答by Paul Shannon
TortoiseMergeis a separate utility that comes bundled with TortoiseSVN.
TortoiseMerge是一个单独的实用程序,它与 TortoiseSVN 捆绑在一起。
It can also be can be downloaded separately in the TortoiseDiff.ziparchive. This will allow you to apply unified diffs to non-versioned files.
也可以在TortoiseDiff.zip存档中单独下载。这将允许您将统一差异应用于非版本化文件。
回答by Sardaukar
I know you said you would prefer a GUI, but the commandline tools will do the work nicely. See GnuWinfor a port of unix tools to Windows. You'd need the patch command, obviously ;-)
我知道您说过您更喜欢 GUI,但命令行工具可以很好地完成这项工作。请参阅GnuWin以获取 unix 工具到 Windows 的端口。显然,您需要 patch 命令;-)
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.
但是,您可能会遇到线路终止问题。GnuWin 端口将假定补丁文件具有 DOS 样式的行终止 (CR/LF)。尝试在相当智能的编辑器中打开补丁文件,它会为您进行转换。
回答by Dan
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:
在 TortoiseSVN 中,补丁应用确实有效。您需要将补丁应用到与从. 记住这一点总是很重要的。所以这里是你如何在 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.
祝你好运。
回答by logisch
You can use thisWin32 native port of the patch utility.
您可以使用补丁实用程序的这个Win32 本机端口。
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.
它带有更多其他实用程序的选择,与 Cygwin 和类似工具相比,它不需要任何 DLL 或类似工具。只需选择您选择的小型可执行文件并将其存储在您想要的任何位置。
Simple usage:
简单用法:
patch.exe -i <patchfile>
Get more help:
获得更多帮助:
patch.exe --help
回答by Evgeniy Generalov
The patch.exe utility from the Git installation works on Windows 10.
Git 安装中的 patch.exe 实用程序适用于 Windows 10。
Install Git for Windowsthen use the "C:\Program Files\Git\usr\bin\patch.exe"
command to apply a patch.
安装适用于 Windows 的 Git,然后使用该"C:\Program Files\Git\usr\bin\patch.exe"
命令应用补丁。
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.
如果Hunk #1 FAILED at 1 (different line endings).
在应用补丁期间输出中出现任何错误消息,请尝试将-l
(这是 的快捷方式--ignore-whitespace
)或--binary
开关添加到命令行。
回答by isapir
I use MSYS2 from http://www.msys2.org/
我使用来自http://www.msys2.org/ 的MSYS2
It provides many utilities like patch
, which
, git
, tree
, and many more.
它提供了许多实用程序,例如patch
、which
、git
、tree
等等。
After installing MSYS2 simply run the package manager to install patch
:
安装 MSYS2 后,只需运行包管理器即可安装patch
:
pacman -S patch
回答by Brian Clapper
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.
编辑:查看到目前为止的回复,似乎 Tortoise 只有在它是一个已经版本化的文件时才会正确执行。这不是这里的情况。我需要能够将补丁应用于不是来自 SVN 存储库的文件。我只是尝试使用 Tortoise,因为我碰巧知道 SVN 使用差异,并且必须知道如何创建和应用差异。
You can install Cygwin, then use the command-line patchtool to apply the patch. See also this Unix man page, which applies to patch.
回答by matt wilkie
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 Patchcommand.
TortoiseSVN (TortoiseMerge) 似乎需要Index: foobar.py
diff/patch 文件中的这一行。这是我需要做的,使非 TortoiseSVN 补丁文件与 TortoiseSVN 的右键单击应用补丁命令一起工作。
Before:
前:
--- foobar.py.org Sat May 08 16:00:56 2010
+++ foobar.py Sat May 08 15:47:48 2010
After:
后:
Index: foobar.py
===================================================================
--- foobar.py
+++ foobar.py (working copy)
Or if you know the specific revision your contributor was working from:
或者,如果您知道您的贡献者正在使用的特定修订版:
Index: foobar.py
===================================================================
--- foobar.py (revision 1157)
+++ foobar.py (working copy)