Git笔记【5】git应用--利用git标签生成版本号

  1.  内容提要:通过bash脚本生成项目的版本信息。
  2. 说明:
    1. 脚本执行后,输出如下信息:
    2. git branch --contains # 用来获取当前的分支名。
    3. git describe --tags # 用来获取标签名、基于当前标签的提交次数和与当前标签挂钩的commit号。
#!/bin/bash

set -o nounset                              # Treat unset variables as an error
export LC_ALL=en_US.UTF-8

git_dir="../../../" # the path of .git
commit_id="HEAD"

ver_date=`date "+%Y-%m-%d %H-%M-%S"`
cli_full_info=`git describe --tags 2>/dev/null`

cd ${git_dir}

# get branch name
get_git_branch () {
	local branch
	branch=`git branch --contain ${commit_id} | grep "*" | head -n1 | cut -d' ' -f 2-`
	echo ${branch}
}

# get commit id
get_git_commit () {
	local build_cli
	build_cli=`echo ${cli_full_info} | cut -d'-' -f 3 | sed -e 's|^g||' 2>/dev/null`
	echo "${build_cli}"
}

# get commit numbers
get_git_commits () {
	local build_cli
	build_cli=`echo ${cli_full_info} | cut -d'-' -f 2 2>/dev/null`
	echo ${build_cli}
}

echo "ATCA-BYPASS $(get_git_commits) ($(get_git_branch)-$(get_git_commit) $ver_date)" 

 

你可能感兴趣的:(git)