gpt4 book ai didi

python - 如何将统一的差异文件应用到原始文件?

转载 作者:太空宇宙 更新时间:2023-11-03 19:16:47 25 4
gpt4 key购买 nike

我需要一些函数“apply_patch”:

result_string = apply_patch('origin_file.txt', 'origin_file.txt.patch')

或一些命令(适用于 Windows 和 Linux):

if linux:
result_string = os.system('linux_patch --apply origin_file.txt origin_file.txt.patch')
elif windows:
result_string = os.system('windows_patch --apply origin_file.txt origin_file.txt.patch')

有用信息:我使用 Mercurial 作为版本控制系统

最佳答案

检查实用程序 patch 。它会做你所期望的事情。假设你在项目的根目录下,你可以尝试:

$ patch --dry-run < file.diff

--dry-run 会告诉您补丁是否适用,而无需真正应用它。最终,您将必须使用选项 -p n 让 patch 知道从路径中删除目录(查看 diff 文件的 header )。

如果补丁已经应用,该实用程序会询问您是否要撤消补丁。

使用 Mercurial,您还可以尝试“hg import”。如果您的程序需要它,那么您可以从 Mercurial 导入相应的模块。

关于python - 如何将统一的差异文件应用到原始文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11092265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com