如何在 Windows 上应用差异补丁?

发布于 2024-07-13 09:50:42 字数 609 浏览 7 评论 0原文

有很多程序可以创建差异补丁,但我在尝试应用一个程序时遇到了很大的困难。 我正在尝试分发补丁,但用户向我询问了如何应用该补丁。 于是我尝试自己弄清楚,结果发现我毫无头绪,而且我能找到的大多数工具都是命令行的。 (我可以处理命令行,但是如果没有一个漂亮、友好的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(22

阳光①夏 2024-07-20 09:50:43

应用补丁

使用 TortoiseMerge:

  1. 查找并打开现有的 SVN 存储库目录
  2. 如果尚不存在,则创建一个名为“merges”的新目录
  3. 复制要应用 .patch 文件的文件
  4. 在继续下一步之前添加并提交到 svn 存储库
  5. 右键单击​​合并并选择应用补丁...
  6. 双击列表中的文件
  7. 显示带有 diff 的修补文件在右侧窗格中
  8. 单击该窗格并点击保存,或者使用文件->另存为...导出。

如果您从 TortoiseMerge 打开,则可以使用替代屏幕。 在下面的屏幕中,目录指的是上面步骤 2 中提到的“merges”目录:
Screeny

WinMerge GUI 的屏幕截图:
Screeny

Apply Patch

With TortoiseMerge:

  1. Find and open an existing SVN repo directory
  2. Create a new directory named "merges", if it does not exist already
  3. Copy the file onto which you want to apply the .patch file
  4. ADD and COMMIT to the svn repository before you continue to the next step
  5. Right click on merges and choose Apply patch...
  6. Double click the file from list
  7. The patched file with diff is displayed on the right pane
  8. Click on that pane and hit Save or export with File->Save As...

Alternative screeny if you Open from TortoiseMerge. In the screeny below, directory refers to the "merges" directory mentioned at step 2 above:
Screeny

Screenshot of WinMerge GUI:
Screeny

我为此制作了纯Python工具。 它具有可预测的跨平台行为。 尽管它不创建新文件(在撰写本文时)并且缺少 GUI,但它可以用作创建图形工具的库。

更新:如果安装了Python,使用起来应该会更方便。

pip install patch
python -m patch

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.

pip install patch
python -m patch
澉约 2024-07-20 09:50:43

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.

美胚控场 2024-07-20 09:50:43

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.

痞味浪人 2024-07-20 09:50:43

我知道您说过您更喜欢 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.

丢了幸福的猪 2024-07-20 09:50:43

在 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.

穿透光 2024-07-20 09:50:43

您可以使用补丁实用程序的 Win32 本机端口。

它附带了更多可供选择的其他实用程序,与 Cygwin 和类似工具相比,它不需要任何 DLL 或类似工具。 只需选择您选择的小型可执行文件并将其存储在您想要的任何地方。

简单用法:

patch.exe -i <patchfile>

获取更多帮助:

patch.exe --help

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:

patch.exe -i <patchfile>

Get more help:

patch.exe --help
梦幻的味道 2024-07-20 09:50:43

我已经在使用 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.

空城旧梦 2024-07-20 09:50:43

我使用 http://www.msys2.org/ 中的 MSYS2

它提供了许多实用程序,例如 patchwhichgittree 等等。

安装 MSYS2 后,只需运行包管理器来安装补丁:

pacman -S patch

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:

pacman -S patch
爱她像谁 2024-07-20 09:50:43

编辑:看看到目前为止的回复,似乎只有当文件已经版本化时,Tortoise 才会正确执行。 这里的情况并非如此。 我需要能够对不是来自 SVN 存储库的文件应用补丁。 我刚刚尝试使用 Tortoise,因为我碰巧知道 SVN 使用差异并且必须知道如何创建它们并应用它们。

您可以安装Cygwin,然后使用命令行patch工具应用补丁。 另请参阅此 Unix 手册页,它适用于补丁

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.

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.

策马西风 2024-07-20 09:50:43

看来 TortoiseSVN (TortoiseMerge) 需要行 Index: foobar.py在 diff/patch 文件中。 这就是我需要做的事情,使非 TortoiseSVN 补丁文件与 TortoiseSVN 的右键单击应用补丁命令一起使用。

之前:

--- foobar.py.org   Sat May 08 16:00:56 2010
+++ foobar.py   Sat May 08 15:47:48 2010

之后:

Index: foobar.py
===================================================================
--- foobar.py
+++ foobar.py   (working copy)

或者,如果您知道贡献者所使用的具体修订版:

Index: foobar.py
===================================================================
--- foobar.py   (revision 1157)
+++ foobar.py   (working copy)

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:

--- 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)
真心难拥有 2024-07-20 09:50:43

使用 git Diff 或 linux patch 使用 git diff 在 Windows 上应用补丁

在 Linux、MacOS 或其他任何地方使用 GNU patch 命令或 git diff 创建的补丁都可以使用 git apply 在 Windows 上应用。

创建补丁 例如,从 2 个目录(其中 1 个或多个文件已更改)创建

补丁 0001-path-file.patch

  • “-N”将缺失的文件视为空文件,仅考虑修改的内容
  • “-a”将所有文件视为文本,不是强制的,如果处理二进制补丁则需要删除。
  • “-r”递归目录遍历
  • “-u”默认在所有差异周围添加 3 行上下文

,或者使用 git diff

git diff original_dir modded_dir > 0001-path-file.patch

然后复制windows环境下的.patch文件以及包含原始未更改文件的original_dir目录。

使用 git diff 申请:

1 复制父目录下的补丁文件

2 cd 到原目录

3 使用 git apply 应用补丁

copy 0001-path-file.patch ..\original_dir\
cd original_dir
git apply < 0001-path-file.patch

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

  • "-N" treat absent files as empty, necessary to only consider modified content
  • "-a" treat all files as text, not compulsory, needs removed if dealing - with binary patches.
  • "-r" recursive for directories traversal
  • "-u" adds 3 lines of context by default around all diffs

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

copy 0001-path-file.patch ..\original_dir\
cd original_dir
git apply < 0001-path-file.patch
江南烟雨〆相思醉 2024-07-20 09:50:43

补丁告诉它要应用到哪个文件。 标题应该类似于(在记事本或您最喜欢的文本编辑器中查看):

--- Folder/old_file
+++ Folder/new_file

对于 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):

--- Folder/old_file
+++ Folder/new_file

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.

凯凯我们等你回来 2024-07-20 09:50:43

Eclipse 应该能够做到这一点,进入 TeamSynchronize 透视图,然后进入 Project->Apply patch

Eclipse should be able to do it, go to TeamSynchronize perspective and then into Project->Apply patch

将军与妓 2024-07-20 09:50:43

对于 Java 项目,我使用 NetBeans 来应用补丁文件。 如果您要修补的 Java 代码还不是 NetBeans 项目,请为其创建一个项目。 要创建新项目:

  • 选择菜单文件 -> 新建项目
  • 在出现的对话框中,将其设为Java 应用程序项目。 在对话框中为其命名,然后单击完成
  • 右键单击项目名称,然后从上下文菜单中选择“属性”。
  • 在出现的对话框中,选择“源”,然后添加“源文件夹”。 浏览到您的 Java 源代码。

现在您已经有了一个项目,请应用补丁:

  • 突出显示您的项目以将其选中
  • 从主菜单中,选择菜单工具 -> 应用差异补丁
  • 在出现的对话框中,浏览到您的补丁文件,选择它,然后按“补丁”按钮。

就是这样。 您的补丁应该被应用,并且您应该看到一个显示更改的差异窗口。

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:

  • Select menu File -> New Project
  • In the resulting dialog, make it a Java Application project. Give it a name in the dialog, and click Finish.
  • Right-click the name of your project, and select Properties from the context menu
  • In the resulting dialog, select Sources, and add a Source Folder. Browse to your Java source.

Now that you have a project, apply the patch:

  • Highlight your project to select it
  • From the main menu, select menu Tools -> Apply Diff Patch
  • In the resulting dialog, browse to your patch file, select it, and press the Patch button.

That's it. Your patch should be applied, and you should see a diff window showing the changes.

朱染 2024-07-20 09:50:43

如果您使用 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 issue hg rollback after the merge.

最终幸福 2024-07-20 09:50:43

如果您在 Windows 上安装了 git,并且想要为 git 存储库应用补丁,您只需从 Windows Power Shell 执行以下操作

git apply ..\0001-your-patch.patch

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:

git apply ..\0001-your-patch.patch
小傻瓜 2024-07-20 09:50:43

当使用 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.

眼泪都笑了 2024-07-20 09:50:43

如果您收到“不是工作副本”错误消息,请尝试从 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.

梦幻的心爱 2024-07-20 09:50:43

适用于 Windows 的 BusyBox 端口同时具有 diff 和 patch 命令,但它们只支持统一格式。

A BusyBox port for Windows has both a diff and patch command, but they only support unified format.

因为看清所以看轻 2024-07-20 09:50:43

只需使用:

patch -p0 < path-file.patch

记住仅从创建补丁的文件夹位置执行此命令。

Just use:

patch -p0 < path-file.patch

remember execute this command only from the folder location where you created the patch.

乖乖公主 2024-07-20 09:50:43

为了响应之前有关补丁和 Python 3 的查询,我收到了错误
TypeError:只能将 str (不是“字节”)连接到 str

我的解决方案是在 _normalize_filenames 函数中更改源:

-        debug("    target = " + p.target)
-        debug("    source = " + p.source)
+        debug("    target = ", p.target)
+        debug("    source = ", p.source)

然后补丁对我来说效果很好(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:

-        debug("    target = " + p.target)
-        debug("    source = " + p.source)
+        debug("    target = ", p.target)
+        debug("    source = ", p.source)

then patch worked fine for me (Python 3, Windows 10)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文