<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          別亂提交代碼了,看下大廠 Git 提交規(guī)范是怎么做的!

          共 3117字,需瀏覽 7分鐘

           ·

          2020-01-07 23:27

          人人貸大前端技術中心
          juejin.im/post/5d0b3f8c6fb9a07ec07fc5d0

          git是現在市面上最流行的版本控制工具,書寫良好的commit message能大大提高代碼維護的效率。但是在日常開發(fā)中由于缺少對于commit message的約束,導致填寫內容隨意、質量參差不齊,可讀性低亦難以維護。在項目中引入commit message規(guī)范已是迫在眉睫。

          用什么規(guī)范?

          現在市面上比較流行的方案是約定式提交規(guī)范Conventional Commits),它受到了Angular提交準則的啟發(fā),并在很大程度上以其為依據。約定式提交規(guī)范是一種基于提交消息的輕量級約定。它提供了一組用于創(chuàng)建清晰的提交歷史的簡單規(guī)則;這使得編寫基于規(guī)范的自動化工具變得更容易。這個約定與SemVer相吻合,在提交信息中描述新特性、bug 修復和破壞性變更。它的 message 格式如下:

          <類型>[可選的作用域]: <描述>

          [可選的正文]

          [可選的腳注]
          Quick Start

          1. 全局安裝commitizen & cz-conventional-changelog

          commitizen是一個撰寫合格commit message的工具,用于代替git commit 指令,而cz-conventional-changelog適配器提供conventional-changelog標準(約定式提交標準)。基于不同需求,也可以使用不同適配器。

          npm install -g commitizen cz-conventional-changelog
          echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

          安裝完畢后,可直接使用git cz來取代git commit

          全局模式下,需要 ~/.czrc 配置文件, 為commitizen指定Adapter

          2. 項目內安裝commitlint & husky

          commitlint負責用于對commit message進行格式校驗,husky負責提供更易用的git hook

          Use npm
          npm i -D husky @commitlint/config-conventional @commitlint/cli

          Use yarn
          yarn add husky @commitlint/config-conventional @commitlint/cli -D

          commitlint只能做格式規(guī)范,無法觸及內容。對于內容質量的把控只能靠我們自己。

          3. 添加相應配置

          創(chuàng)建commitlint.config.js

          # In the same path as package.json

          echo 'module.exports = {extends: ["@commitlint/config-conventional"]};' > ./commitlint.config.js

          引入husky

          # package.json

          ...,
          "husky": {
          "hooks": {
          "commit-msg": "commitlint -e $GIT_PARAMS"
          }
          }

          4. 使用

          執(zhí)行git cz進入interactive模式,根據提示依次填寫

          1.Select the type of change that you're committing 選擇改動類型 ()

          2.What is the scope of this change (e.g. component or file name)? 填寫改動范圍 ()

          3.Write a short, imperative tense description of the change: 寫一個精簡的描述 ()

          4.Provide a longer description of the change: (press enter to skip) 對于改動寫一段長描述 ()

          5.Are there any breaking changes? (y/n) 是破壞性修改嗎?默認n (
          )

          6.Does this change affect any openreve issues? (y/n) 改動修復了哪個問題?默認n (
          )

          生成的commit message格式如下:

          (): 




          填寫完畢后,husky會調用commitlint對message進行格式校驗,默認規(guī)定typesubject為必填項。

          任何git commit指令的option都能用在 git cz指令上, 例如git cz -a

          Commit message規(guī)范在rrd-fe落地使用情況

          針對團隊目前使用的情況,我們討論后擬定了commit message每一部分的填寫規(guī)則。

          1. type

          type為必填項,用于指定commit的類型,約定了featfix兩個主要type,以及docs、style、build、refactor、revert五個特殊type其余type暫不使用。

          # 主要type
          feat: 增加新功能
          fix: 修復bug

          # 特殊type
          docs: 只改動了文檔相關的內容
          style: 不影響代碼含義的改動,例如去掉空格、改變縮進、增刪分號
          build: 構造工具的或者外部依賴的改動,例如webpack,npm
          refactor: 代碼重構時使用
          revert: 執(zhí)行git revert打印的message

          # 暫不使用type
          test: 添加測試或者修改現有測試
          perf: 提高性能的改動
          ci: 與CI(持續(xù)集成服務)有關的改動
          chore: 不修改src或者test的其余修改,例如構建過程或輔助工具的變動

          當一次改動包括主要type特殊type時,統(tǒng)一采用主要type

          2. scope

          scope也為必填項,用于描述改動的范圍,格式為項目名/模塊名,例如:node-pc/common rrd-h5/activity,而we-sdk不需指定模塊名。如果一次commit修改多個模塊,建議拆分成多次commit,以便更好追蹤和維護。

          3. body

          body填寫詳細描述,主要描述改動之前的情況修改動機,對于小的修改不作要求,但是重大需求、更新等必須添加body來作說明。

          4. break changes

          break changes指明是否產生了破壞性修改,涉及break changes的改動必須指明該項,類似版本升級、接口參數減少、接口刪除、遷移等。

          5. affect issues

          affect issues指明是否影響了某個問題。例如我們使用jira時,我們在commit message中可以填寫其影響的JIRA_ID,若要開啟該功能需要先打通jiragitlab參考文檔:docs.gitlab.com/ee/user/pro…

          填寫方式例如:

          re #JIRA_ID
          fix #JIRA_ID
          示例
          • 完整的commit message示例

          babdb7d6502d631634c27354f75b288c.webp

          • 相應的git log

          9f43bc66a4df6615bc7801c591131815.webp

          擴展閱讀

          conventional commits 必讀 介紹約定式提交標準。

          Angular規(guī)范 必讀 介紹Angular標準每個部分該寫什么、該怎么寫。

          @commitlint/config-conventional 必讀 介紹commitlint的校驗規(guī)則config-conventional,以及一些常見passes/fails情況。


          有道無術,術可成;有術無道,止于術

          歡迎大家關注Java之道公眾號


          好文章,我在看??

          瀏覽 39
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产三级黄色 | 亚洲籍视频在线观看 | 亚洲免费视频观看 | 国产一级理论片 | 91麻豆精品成人系列 |