• 使用gitlint及template_git对git commit的提交进行提示及强制规范


    Git Commit强制规范(commitlint+husky)
    gitlint替代commitlint的用法


    初次下载及未配置环境者需要在首次commit前有如下动作以保证commit格式符合规范:


    1、将.template_git文件(UTF-8格式)放到git全局路径下

    2、执行git config --global commit.template .template_git

    3、在CMD中输入pip install gitlint //注意放到环境变量下面

    4、将.gitlint放到.git\hooks文件夹下

    5、执行gitlint install-hook

    6、执行git flow init

    7、修改git默认编辑器为VSCode 执行 git config --global core.editor “code --wait”

    8、修改gitlint中的cli.py文件(否则不识别中文):

    if msg_filename:
            LOG.debug("Using --msg-filename.")
            LOG.debug(msg_filename)
    
            #UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 
    
            #缺省是gbk编码,无法提交中文,需要修改为utf-8就可以了。
            msg_filename = open(msg_filename.name,'r',encoding='UTF-8')
            return from_commit_msg(str(msg_filename.read()))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    效果如下:


    git commit时检测到数据不合规范,会显示如下提示:
    gitlint: checking commit message…
    1: T8 Title is too short (3<5): “123”
    3: B6 Body message is missing

    gitlint: Your commit message contains violations.
    Continue with commit anyways (this keeps the current commit message)? [y(es)/n(no)/e(dit)]
    可以强制commit和退出commit或者重新编辑


    .template_git内容:


    <type>(<scope>) : <subject>
    
    <body>
    
    <footer>
    
    #说明
    #
    #<type> 必需
    #  feat     - 添加新特性
    #  fix      - 修复bug
    #  docs     - 仅修改了文档
    #  style    - 仅修改了空格、格式缩进、逗号等等,不改变代码逻辑
    #  refactor - 代码重构,没有添加新功能或者修复bug
    #  perf     - 提高性能
    #  test     - 增加测试用例
    #  chore    - 改变构建过程、或者增加依赖库、工具等
    #  revert   - 回滚到上一个版本
    #
    #<scope> 可选
    # 用于说明commit影响的范围,比如数据层、控制层、视图层等
    #
    #subject 必需
    # commit的简短描述,不超过50个字符
    # 以动词开头,使用第一人称现在时,比如change,而不是changed或changes
    # 第一个字母小写
    # 结尾不加句号(.)
    #
    #<body> 可选
    # 使用第一人称现在时,比如使用change而不是changed或changes
    # 应该说明代码变动的动机,以及与以前行为的对比
    #
    #<footer> 可选 只用于两种情况
    # 1 不兼容变动
    #    如果当前代码与上一个版本不兼容,则Footer部分以BREAKING CHANGE开头,后面是对变动的描述、以及变动理由和迁移方法
    #    例子:
    #        BREAKING CHANGE: isolate scope bindings definition has changed.
    #
    #            To migrate the code follow the example below:
    #
    #            Before:
    #
    #            scope: {
    #              myAttr: 'attribute',
    #            }
    #
    #            After:
    #
    #            scope: {
    #              myAttr: '@',
    #            }
    #
    #            The removed `inject` wasn't generaly useful for directives so there should be no code using it.
    #
    # 2 关闭Issue
    #    如果当前commit针对某个issue,那么可以在Footer部分关闭这个issue或多个issue
    #    例子:Closes #123, #245, #992
    #
    #还有一种特殊情况,如果当前commit用于撤销以前的commit,则必须以revert:开头,后面跟着被撤销Commit的Header
    #    例子: 
    #        revert: feat(pencil): add 'graphiteWidth' option
    #
    #        This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
    #Body部分的格式是固定的,必须写成This reverts commit &lt;hash>.,其中的hash是被撤销commit的SHA标识符
    #如果当前commit与被撤销的commit,在同一个发布(release)里面,那么它们都不会出现在Change log里面。
    #如果两者在不同的发布,那么当前commit,会出现在Change log的Reverts小标题下面
    #
    #
    #
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    .gitlint文件:


    [general]
    # You HAVE to add the rule here to enable it, only configuring (such as below)
    # does NOT enable it.
    contrib=contrib-title-conventional-commits,CT1
    ignore=B6
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    【AI实战】CUDA 编程入门及开源项目代码分享
    接口性能优化的11个小技巧(荣耀典藏版)
    腾讯云轻量数据库是什么?轻量数据库测评详细介绍
    tiny模式基本原理整合
    Linux介绍及基本操作
    Redis 学习
    重拾html5
    java微博 10 request and response
    mongo实际业务场景实战
    干货分享:小技巧大用处之Bean管理类工厂多种实现方式
  • 原文地址:https://blog.csdn.net/Fei_Yang_YF/article/details/126352656