zoukankan      html  css  js  c++  java
  • husky 7.0.4 git hooks 前端 commit 钩子 git转svn [已解决]

    husky 7 的安装,注意下版本

    第一步 安装
    cnpm install husky@7.0.4 --save-dev
    第二步 在package.json script加入
    "prepare": "husky install",
    第三步 执行下
    npm run prepare
    第四步 添加hooks
    yarn husky add .husky/pre-commit "npm run test"
    第五步 进入.husky/pre-commit 写shell脚本

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    git log -2 > a.txt
    

    就一句,把log生成个文件


    需求:
    git转svn的时候,git信息会丢失,就想每次提交的时候把git的提交记录,弄个文本,放进去,然后打包的时候copy进dist
    问题:
    每次提交的最新的信息没有
    获取不到commit的文本信息
    生成的文件,git下又会发现新的变动,导致一直有没提交的文件

    最后解决方案:
    放弃husky,加个bat,执行下 git log -10 > a.txt,自动构建打包的时候把a.txt放到dist

    package.json 里面 scripts 加个
    "gitLog": "git log -10 > gitlog.txt",
    直接能执行,但是也是有 最后生成文件后,会有文件change问题。


    刚又有个新想法,就是在 pre-commit 里面 git add 这个 gitlog.txt 然后自动commit会不会就ok了呢

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    echo "当前git提交时间 $(date "+%Y-%m-%d %H:%M:%S") $1" > gitlog.txt
    git log -10 >> gitlog.txt
    git add gitlog.txt
    
    

    解决提交的问题了,现在还有 得不到commit-msg 文字的问题

    prepare-commit-msg

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    . "$(dirname "$0")/common.sh"
    
    echo "2 当前git提交时间 $(date "+%Y-%m-%d %H:%M:%S") " >> gitlog.txt
    cat "$1" >> gitlog.txt
    git add gitlog.txt
    

    原来$1是文件地址 要用cat读取
    msg =$(cat $1)
    新的问题是,能读到msg的时候,已经commit完了。郁闷。。

    最新总结
    pre-commit

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    # <!doctype html><html lang="en"><head><meta charset="utf-8"/></head><body></body></html>
    msg="<meta charset="utf-8"/><pre>
    "
    msg="$msg husky Recording function: 
    "
    msg="$msg 1. 记录最后一次commit时间
    "
    msg="$msg 2. 记录最后5次commit记录
    "
    msg="$msg 
    "
    msg="$msg 当前最后一次git提交时间 $(date "+%Y-%m-%d %H:%M:%S")
    "
    msg="$msg 由于最后一次commit没有记录功能,下面List为第2-6次的git commit提交信息:
    "
    echo -e "$msg" > public/gitlog.html
    git log -5 >> public/gitlog.html
    echo "</pre>" >> public/gitlog.html
    git add public/gitlog.html
    

    下面这个废弃了,因为生成文件可以直接放入public目录,这样build自动就copy了。
    打包的时候,再copy到dist目录

    const fs = require('fs-extra');
    fs.copyFileSync('gitlog.txt', 'build/gitlog.html');
    

    最后找的解决方案了!!

    pre-commit

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    touch .commit
    

    post-commit

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    # 因为post-commit 是肯定走的hooks 但是 pre-commit是可以取消的,
    # 这里就用在 pre-commit 生成一个文件,然后这里再删除,这样就不会进入死循环了。
    if [ -e .commit ]; then
    
    rm -rf .commit
    
    msg="<meta charset="utf-8"/><pre>
    "
    msg="$msg husky Recording function: 
    "
    msg="$msg 1. 记录最后一次commit时间
    "
    msg="$msg 2. 记录最后5次commit记录
    "
    msg="$msg 
    "
    msg="$msg 当前最后一次git提交时间 $(date "+%Y-%m-%d %H:%M:%S")
    "
    msg="$msg 最后5次的git commit提交信息:
    "
    echo -e "$msg" > public/gitlog.html
    git log -5 >> public/gitlog.html
    echo "</pre>" >> public/gitlog.html 
    git add public/gitlog.html 
    git commit --amend -C HEAD --no-verify
    
    fi
    
    
    

    20211110 更新
    问题:多人维护的时候,这个gitlog文件 每次都冲突,改成自己的git用户名
    在gitlog目录建立个 index.html 再做几个链接就完了
    小瑕疵:$(git config user.name) 想存个变量 再用,但是没有研究出来

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    # 因为post-commit 是肯定走的hooks 但是 pre-commit是可以取消的,
    # 这里就用在 pre-commit 生成一个文件,然后这里再删除,这样就不会进入死循环了。
    if [ -e .commit ]; then
    
    rm -rf .commit
    
    msg="<meta charset="utf-8"/><pre>
    "
    msg="$msg husky Recording function: 
    "
    msg="$msg 1. 记录最后一次commit时间
    "
    msg="$msg 2. 记录最后5次commit记录
    "
    msg="$msg 
    "
    msg="$msg 当前最后一次git提交时间 $(date "+%Y-%m-%d %H:%M:%S")
    "
    msg="$msg 最后5次的git commit提交信息:
    "
    echo -e "$msg" > public/gitlog/$(git config user.name).html
    git log -5 >> public/gitlog/$(git config user.name).html
    echo "</pre>" >> public/gitlog/$(git config user.name).html
    git add public/gitlog/$(git config user.name).html
    git commit --amend -C HEAD --no-verify
    
    fi
    
    
    

    资料
    https://www.npmjs.com/package/husky

    ---------------------------------------------
    生活的意义并不是与他人争高下,而在于享受努力实现目标的过程,结果是对自己行动的嘉奖。
    ↑面的话,越看越不痛快,应该这么说:

    生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
  • 相关阅读:
    Qt中将QTableView中的数据导出为Excel文件
    QTableView另类打印解决方案(复用render函数去解决print问题)
    [网络]_[批量下载网站文件]
    Codecs是以plugin的形式被调用的(显示中文的codec plugin文件是qcncodecs4.dll),可静态载入和动态载入
    Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)
    MinGW 编译zlib、libpng、libjpeg、libcurl等(全都是Qt项目)
    Move WriteBuffer ReadBuffer String
    Delphi调试DLL 不能调试 不能进入调试 注意!!!
    ssh三大框架,三层架构 整合测试!完整分页代码,JdbcTemplate等测试,存储过程调用,留着以后复习吧
    Delphi 的动态数组
  • 原文地址:https://www.cnblogs.com/pengchenggang/p/15439160.html
Copyright © 2011-2022 走看看