在第三方代码中维护自定义补丁

6
我正在开发一个使用第三方JavaScript库(TinyMCE)的Web应用程序。
我的应用程序有一些特定需求,需要我在几个地方打补丁。这些补丁很简单(少于十几行),但因为它们是针对我们的使用情况而非错误,所以很具体。
我希望能够在发布新版库时进行更新,但这会覆盖我们Git存储库中的更改。
我需要一种方法来确保在更新第三方代码时始终先应用我们的补丁。由于更改非常小,手动应用它们也不是问题。
当我更新第三方代码时,如何确保我们的补丁应用到我们的存储库中?
2个回答

6

创建一个用于跟踪第三方代码的代码库,并将您的补丁放在单独的分支中。当您需要最新版本时,请获取更改并使用变基您的分支。

例如:

$ git clone --origin github https://github.com/tinymce/tinymce.git
$ cd tinymce/
$ git remote add origin git@myrepo.example.org:tinymce

然后,制作您的补丁并将其推送到您的存储库中:
$ git commit -m "my patches to tinymce"
$ git push --set-upstream origin master

此时你的代码库看起来是这样的:
(0) --- (1) --- ... (n) --- (X)
                             |
                           master

X代表你的补丁。

现在设置一个分支从github远程获取新版本:

$ git branch tinymce_import github/master
$ git checkout tinymce_import
$ git pull --ff-only

那么您的代码库就会变成这样(git branch 足够智能,可以使用 github 远程库中的最后一个版本作为源):

                           master
                             |
                     +----- (X)
                     |
(0) --- (1) --- ... (n) --- (n+1) --- ... (n+m)
                                            |
                                      tinymce_import

最后,在 tinymce_import 分支上 rebase 你的 master 分支:
$ git checkout master
$ git rebase tinymce_import

                                                  master
                                                    |
                                            +----- (X)
                                            |
(0) --- (1) --- ... (n) --- (n+1) --- ... (n+m)
                                            |
                                      tinymce_import

1
如果您将TinyMCE存储在Git仓库中,那么您可以使用Git的post-commit-hook在获取新版本的TinyMCE后执行补丁操作(然后提交这些补丁)。
工作流程大致如下:
[get new version of TinyMCE]
["git commit -a" to update all tracked files]
[post-commit-hook patches TinyMCE]
["git commit -a" to pick up all of the changes from the patches]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接