shell获取git相关信息

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
#!/bin/bash
# 返回$1指定的git项目的当前分支(branch)或标签名(tag)
# $1 git项目源码位置,为空获则默认为当前文件夹
function current_branch() {
local folder="$(pwd)"
[ -n "$1" ] && folder="$1"
git -C "$folder" rev-parse --abbrev-ref HEAD | grep -v HEAD
# git -C "$folder" describe --tags --abbrev=8 --dirty --always
# git -C "$folder" rev-parse --abbrev-ref HEAD | grep -v HEAD || \
# git -C "$folder" describe --exact-match HEAD || \
# git -C "$folder" rev-parse HEAD
}

function current_tag() {
local folder="$(pwd)"
[ -n "$1" ] && folder="$1"
git -C "$folder" describe --tags --abbrev=8 --dirty --always
}

workdir=$(cd $(dirname $0); pwd)
branch="$(current_branch $workdir/../)"
tag="$(current_tag $workdir/../)"

version="$branch-$tag"
echo $version

shell下载文件

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
#!/bin/bash
url=$1
dir=$2

echo $1 $2
download()
{
cd $dir &> /dev/null
if [ $? - ne 0 ]
then
read -p "$dir No such file or directory,create now?(y/n)" answer
if [ "$answer" == "y" ]; then
mkdir -p $dir
cd $dir
wget $url &> /dev/null
if [ $? - ne 0 ]; then
return "52"
fi
else
return "51"
fi
else
wget $url &> /dev/null
if [ $? - ne 0 ]; then
return "52"
fi
fi
}

download $url $dir
ret=$?
echo $ret
# if [ $ret == 0 ]; then
# echo "Download successfully."
# else
# echo "Download failed."
# fi
# # echo $?
1