git: hook 修改提交信息

git获取数字顺序版本号

因为git的版本使用的是hash值,不能很直观的看出那个版本,所以想找到一种方法,获取顺序的版本号,在网上找到了方法,可以获取顺序版本号

摘自:[使用bash从SVN和Git中获取顺序版本号](http://zengrong.net/post/1798.htm)   
 基准版本号默认是1,可以通过传递一个参数修改  
get_version()  
{
    local base=${1:-1}
    echo $((`git rev-list --all|wc -l` + $base))
}
get_version 7000

hook修改提交信息,自动添加顺序版本号

hook解释

  1. 自定义 Git - Git 钩子
  2. Git钩子:自定义你的工作流

实现我们的需求

git提交时都需要写提交信息,eg:本次修改了某个函数...
我想通过hook修改为: [ 顺序版本号 ]: 本次修改了某个函数...
直接拷贝一份.git/hook目录下的 commit-msg.sample --> commit-msg
然后添加一下代码:

get_version()
{
    local base=${1:-1}
    return $((`git rev-list --all|wc -l` + $base))
}
get_version  
version=$? 
echo "[ $version ]"':  '$(cat "$1") > "$1"

注解1:

echo "[ $version ]"': '$(cat "$1") > "$1" 这种写法其实参考了http://stackoverflow.com/questions/5894946/how-to-add-gits-branch-name-to-the-commit-message
代码如下:

Here is my commit-msg script as an example:

#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
#
NAME=$(git branch | grep '*' | sed 's/* //') 
DESCRIPTION=$(git config branch."$NAME".description)

echo "$NAME"': '$(cat "$1") > "$1"
if [ -n "$DESCRIPTION" ] 
then
   echo "" >> "$1"
   echo $DESCRIPTION >> "$1"
fi 
Creates following commit message:

[branch_name]: [original_message]

[branch_description]
I'm using issue number as branch_name, issue description is placed to the branch_description using git branch --edit-description [branch_name] command.

More about branch descriptions you can find on this Q&A.
注解2:
get_version  
version=$? 

这是shell获取函数返回值的方式,参考:Linux Shell函数返回值

原文地址:https://www.cnblogs.com/ZhYQ-Note/p/6009067.html