如何向开源社区奉献代码
從事嵌入式工作, 日常就是與u-boot, linux這些開源代碼打交道, 將自己的代碼提交到主線, 將是一件快樂而又能提升逼格的事情. 有些比較新的代碼會使用github進行bug追蹤, pr合并, 這些直接在github提交Pull Request就好了, 比較簡單, 就不在贅述. 本文重點介紹如何使用mail list的方式提交自己的代碼, 以u-boot為例介紹.
一: 新建分支, 實現(xiàn)功能
當需要實現(xiàn)一個feature或者修改一個bug, 我們需要從master上面切換一個分支:
$?git?checkout?-b?allwinner-r40-usb-otg-v2之后在當前分支上對主線代碼進行修改, 不要將所有的修改都提交到一個commit上面, 而是應(yīng)該將修改拆分成幾個獨立的commit, 這樣也便于審核和維護. 在commit之前需要配置git的郵箱等信息:
$?git?config?--global?user.name?name $?git?config?--global?user.email?xxx@xxx.com配置完成之后, 可以使用git config --list檢查下是否已經(jīng)設(shè)置成功.
開源的代碼對commit都有一個約定俗成的三段式寫法, 第一段簡要寫明當前commit修改的主要內(nèi)容. 第二段可以詳細的描述下為什么這么做等等原因. 第三段簽上自己Signed-off-by. 下面是u-boot代碼中的一個例子:
arm:?mvebu:?turris_mox:?add?support?for?board?rescue?modeAdd?necessary?config?options?and?board?code?to?support?board?factory reset?/?rescue?mode?on?Turris?MOX.In?order?to?also?support?invoking?rescue?mode?from?U-Boot?console, without?having?to?press?the?factory?reset?button,?put?the?rescue?command into?`bootcmd_rescue`?default?environment?variable.?When?factory?reset button?is?pressed,?invoke?rescue?mode?via?distroboot?by?setting `boot_targets`?to?`rescue`.Rescue?boot?from?console?can?be?invoked?by?runningrun?bootcmd_rescueSigned-off-by:?Marek?Behún?<marek.behun@nic.cz> Reviewed-by:?Pali?Rohár?<pali@kernel.org> Reviewed-by:?Stefan?Roese?<sr@denx.de>當然, 每次commit都寫一個Signed-off-by很費勁, 因此git有選項可以配置一個commit的模板文件, 在每次commit的時候自動填充一些信息:
???u-boot?git:(allwinner-r40-usb-otg-v2)?git?config?--list?|?grep?commit.template commit.template=/home/qianfan/.git-commit-template.txt ???u-boot?git:(allwinner-r40-usb-otg-v2)?cat?~/.git-commit-template.txtSigned-off-by:?qianfan?Zhao?<qianfanguijin@163.com>???u-boot?git:(allwinner-r40-usb-otg-v2)二: 生成, 檢查patch
新建一個outgoing文件夾, 將當前的修改輸出到patch文件中.
???u-boot?git:(allwinner-r40-usb-otg-v2)?git?format-patch?master?-o?outgoing outgoing/0001-phy-sun4i-usb-Fix-sun8i_r40_cfg.patch outgoing/0002-dts-bpi-m2u-Enable-USB_OTG-by-default.patch outgoing/0003-sunxi-defconfig-bpi-m2u-Enable-usb-gadget-and-ums-by.patchu-boot的源碼中有checkpatch.pl程序, 可以對patch的格式進行檢查, 在提交之前一定要檢查下patch:
???u-boot?git:(allwinner-r40-usb-otg-v2)?./scripts/checkpatch.pl?outgoing/*.patch --------------------------------------------------- outgoing/0001-phy-sun4i-usb-Fix-sun8i_r40_cfg.patch --------------------------------------------------- total:?0?errors,?0?warnings,?0?checks,?8?lines?checkedoutgoing/0001-phy-sun4i-usb-Fix-sun8i_r40_cfg.patch?has?no?obvious?style?problems?and?is?ready?for?submission. --------------------------------------------------------- outgoing/0002-dts-bpi-m2u-Enable-USB_OTG-by-default.patch --------------------------------------------------------- total:?0?errors,?0?warnings,?0?checks,?31?lines?checkedoutgoing/0002-dts-bpi-m2u-Enable-USB_OTG-by-default.patch?has?no?obvious?style?problems?and?is?ready?for?submission. ------------------------------------------------------------------------ outgoing/0003-sunxi-defconfig-bpi-m2u-Enable-usb-gadget-and-ums-by.patch ------------------------------------------------------------------------ total:?0?errors,?0?warnings,?0?checks,?16?lines?checkedoutgoing/0003-sunxi-defconfig-bpi-m2u-Enable-usb-gadget-and-ums-by.patch?has?no?obvious?style?problems?and?is?ready?for?submission.NOTE:?Ignored?message?types:?COMPLEX_MACRO?CONSIDER_KSTRTO?ENOSYS?MINMAX?MULTISTATEMENT_MACRO_USE_DO_WHILE?NETWORKING_BLOCK_COMMENT_STYLE?PREFER_ETHER_ADDR_COPY?USLEEP_RANGE三: 發(fā)送patch
對u-boot而言, 需要將patch發(fā)送到maling list: u-boot@lists.denx.de, 同時需要通知相關(guān)的維護者. 有時候, 我們也不知道誰是這份代碼的維護者, 也不知道應(yīng)該通知誰. 還好, 有工具可以獲取某些patch的維護者:
???u-boot?git:(allwinner-r40-usb-otg-v2)?./scripts/get_maintainer.pl?outgoing/*.patch Chen-Yu?Tsai?<wens@csie.org>?(maintainer:BANANAPI?M2?ULTRA?BOARD) Jagan?Teki?<jagan@amarulasolutions.com>?(maintainer:ARM?SUNXI) Andre?Przywara?<andre.przywara@arm.com>?(maintainer:ARM?SUNXI,commit_signer:4/3=100%) Ivan?Uvarov?<i.uvarov@cognitivepilot.com>?(commit_signer:2/3=67%,authored:2/3=67%,added_lines:688/702=98%,removed_lines:62/62=100%) qianfan?Zhao?<qianfanguijin@163.com>?(commit_signer:1/3=33%,authored:1/3=33%) u-boot@lists.denx.de?(open?list)按照mailing list的要求, 發(fā)送的patch不能有亂七八糟的格式, 就是純文本, 所以不能用添加附件的方式來發(fā)送. 為此, 開源社區(qū)的大佬們做了一個git send-email的程序. 如果未安裝的話, 可以使用apt安裝.
#?apt?install?git-email安裝之后, 需要配置郵箱smtp地址, 賬號, 密碼等. 下面是我使用的網(wǎng)易郵箱的配置. sendemail.smtppass是郵箱密碼的明文.
sendemail.smtpserver=smtp.163.com sendemail.smtpuser=xxx@xxx.com sendemail.smtppass=your_email_passwd sendemail.from=xxx@xxx.com sendemail.smtpencryption=tls配置好賬號信息之后, 就可以使用send-email發(fā)送patch了. 可以將上面得到的維護者的郵箱全部填上:
???u-boot?git:(allwinner-r40-usb-otg-v2)?git?send-email?outgoing/*.patch?-to?u-boot@lists.denx.de?-cc?wens@csie.org?-cc?jagan@amarulasolutions.com?-cc?andre.przywara@arm.com?-cc?marex@denx.de?--smtp-debug待發(fā)送完成之后, 可以在patchwork上面找到這個提交: http://patchwork.ozlabs.org/project/uboot/patch/20210616023326.18135-1-qianfanguijin@163.com/
四: 審核意見
當patch發(fā)送出去之后, 一般而言并不能直接合并入master, 主線代碼維護者會對patch進行審核, 并回復(fù). 回復(fù)的信息會發(fā)送到郵箱中. 后續(xù)可以在使用郵件進行溝通. 郵箱客戶端推薦使用Mozilla Thunderbird.
對審核的建議作出修改, 需要使用git rebase回退到某一個commit修改, 全部修改完成之后, 生成patch, 重復(fù)send-email的工作即可.
感謝完成閱讀,我是喜歡打籃球的寫代碼的籃球球癡,這個是我的公眾號,感謝你關(guān)注并支持。我從大學開始接觸電子和嵌入式軟件知識,至今,已經(jīng)畢業(yè)工作了9年,我喜歡嵌入式,也愿意從事這個行業(yè)。不管是從技術(shù)還是職場經(jīng)驗,都積累了足夠多的經(jīng)驗,目前在一個非常優(yōu)秀的團隊中做開發(fā)工作。
很高興認識每一個對技術(shù)努力,對人用心的朋友。
總結(jié)
以上是生活随笔為你收集整理的如何向开源社区奉献代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kuangbin专题五并查集总结
- 下一篇: Linux 资料大全