共計 4040 個字符,預(yù)計需要花費 11 分鐘才能閱讀完成。
如何進(jìn)行 cherry-pick 的分析,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
盡管 Kubernetes 擁有眾多分支,但社區(qū)主要圍繞在 master 分支進(jìn)行開發(fā)。即便其他分支存在 bug,通常也是先在 master 分支進(jìn)行修復(fù),然后再 cherry-pick 到其他分支。
軟件開發(fā)不可避免的會出現(xiàn) bug,所以經(jīng)常需要從 master 同步一些補丁到其他仍在維護(hù)的分支。而同步的手段可以有多種:
手動提交 PR 到其他分支;
自動提交 PR 到其他分支;
一般來說,自動提交的方式更普遍,它能把某個 master 已合入的 PR 自動 cherry-pick 到你指定的分支上,而手動提交只有在極少數(shù)情況下才會使用。
自動 cherry-pick
使用 Kubernetes 倉庫中 hack/cherry_pick_pull.sh 腳本可以把一個合入到 master 分支的 PR 同步到其他分支。它可以幫你自動完成提交 PR 的所有過程。
前提條件
使用 hack/cherry_pick_pull.sh 自動同步需要滿足一定的前提條件:
你必須已經(jīng)簽署了 CLA 聲明,這也是每個貢獻(xiàn)者必須要簽署的內(nèi)容;
針對 master 的 PR 已經(jīng)被合入;
你的 Github 帳號中已經(jīng) fork 了 Kubernetes 倉庫,并且你本地的 shell 中已經(jīng) clone 了此 fork 倉庫;
你本的倉庫中必須添加的遠(yuǎn)端倉庫名為 upstream(通過命令 git remote add upstream https://github.com/kubernetes/kubernetes.git);
在你環(huán)境變量中添加了 export GITHUB_USER= GitHub ID;
已安裝了 hub 命令行工具;
如果你已經(jīng)參與過代碼貢獻(xiàn),那么這些要求一般都會自動滿足,除了最后兩個。
hub 命令行工具為 GitHub 官方提供的工具,用于從命令行操作 GitHub。而環(huán)境變量中需要添加 GITHUB_USER 正是因為 hub 命令行工具會使用。如果你還沒有安裝,可以使用命令 go get github.com/github/hub 安裝它。
如何自動 cherry-pick
比如,需要將 master 上的 PR #85444 同步到分支 release-1.17,在前面條件都具備的情況下,只需要使用如下命令:
hack/cherry_pick_pull.sh upstream/release-1.17 85444
注意:此腳本會自動創(chuàng)建 PR,不要輕易嘗試,除非你真的需要這么做。
腳本執(zhí)行過程中會有大量信息輸出,可以了解到具體執(zhí)行過程:
[root@ecs-d8b6 kubernetes]# hack/cherry_pick_pull.sh upstream/release-1.17 85444
+++ Updating remotes...
Fetching upstream
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Total 48 (delta 34), reused 34 (delta 34), pack-reused 14
Unpacking objects: 100% (48/48), done.
From https://github.com/kubernetes/kubernetes
d87c921a516..1f913b45820 master - upstream/master
c0f31a4ef63..5c651b7bd5f release-1.16 - upstream/release-1.16
486425533b6..27babd49b95 release-1.17 - upstream/release-1.17
* [new tag] v1.17.0-rc.1 - v1.17.0-rc.1
Fetching origin
+++ Creating local branch automated-cherry-pick-of-#85444-upstream-release-1.17-1574748271
Branch automated-cherry-pick-of-#85444-upstream-release-1.17-1574748271 set up to track remote branch release-1.17 from upstream .
Switched to a new branch automated-cherry-pick-of-#85444-upstream-release-1.17-1574748271
+++ Downloading patch to /tmp/85444.patch (in case you need to do this again)
+++ About to attempt cherry pick of PR. To reattempt:
$ git am -3 /tmp/85444.patch
Applying: Provided a mechanism to re-register hidden metrics.
+++ I m about to do the following to push to GitHub (and I m assuming origin is your personal fork):
git push origin automated-cherry-pick-of-#85444-upstream-release-1.17-1574748271:automated-cherry-pick-of-#85444-upstream-release-1.17
+++ Proceed (anything but y aborts the cherry-pick)? [y/n] y
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 4 threads
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 2.19 KiB | 1.09 MiB/s, done.
Total 10 (delta 8), reused 0 (delta 0)
remote: Resolving deltas: 100% (8/8), completed with 8 local objects.
remote:
remote: Create a pull request for automated-cherry-pick-of-#85444-upstream-release-1.17 on GitHub by visiting:
remote: https://github.com/RainbowMango/kubernetes/pull/new/automated-cherry-pick-of-%2385444-upstream-release-1.17
remote:
To https://github.com/RainbowMango/kubernetes.git
* [new branch] automated-cherry-pick-of-#85444-upstream-release-1.17-1574748271 - automated-cherry-pick-of-#85444-upstream-release-1.17
+++ Creating a pull request on GitHub at RainbowMango:automated-cherry-pick-of-#85444-upstream-release-1.17
https://github.com/kubernetes/kubernetes/pull/85627
+++ Returning you to the master branch and cleaning up.
實際上該腳本實際上模擬了手工操作:
創(chuàng)建一個本地分支,比如 automated-cherry-pick-of-#85444-upstream-release-1.17;
通過 hub 工具獲取 PR 的 commit 信息(可能有多個 commit);
將 upstream/master 中相關(guān)的 commit cherry-pick 到本地分支中;
將本地分支推送到遠(yuǎn)端(origin);
使用 GitHub 的接口提交 PR;
手動 cherry-pick
手動 cherry-pick 一般不常發(fā)生,因為大多數(shù)情況下自動 cherry-pick 都可以勝任。只有在沖突比較大時,需要手工處理沖突時才會手動 cherry-pick。
手動 chery-pick,需要處理沖突,處理完成后提交 PR 的過程與向 master 提交完全一致,這里不再贅述。
提交到 release 分支的 PR 審核過程與提交到 master 分支的略有不同,提交到 release 分支的 PR 除了需要相關(guān)領(lǐng)域的 approver 批準(zhǔn)以外,還需要 release manager 批準(zhǔn)才可以合入。
提交到 release 分支的 PR,自動會被添加上 do-not-merge/cherry-pick-not-approved 標(biāo)簽,只有 release manager 批準(zhǔn)后才會去除該標(biāo)簽,然后 CI 才會合入該 PR。
看完上述內(nèi)容,你們掌握如何進(jìn)行 cherry-pick 的分析的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道,感謝各位的閱讀!