forked from bleis-tift/Git-Hooks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.sh
executable file
·60 lines (52 loc) · 1.03 KB
/
common.sh
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
#! /bin/sh
getGitBranchName()
{
branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
"$(git describe --contains --all HEAD)"
echo ${branch##refs/heads/}
}
isOnMasterBranch()
{
if [ "$(getGitBranchName)" = "master" ]; then
return 0
fi
return 1
}
appendMsgTo1stLine()
{
mv $1 $1.$$
if [ -s "$1.$$" ]; then
if head -1 "$1.$$" | grep "$2" > /dev/null; then
cp "$1.$$" "$1"
else
sed '1s/$/ '"$2"'/' "$1.$$" > $1
fi
else
echo "$2" > "$1"
fi
rm -f $1.$$
}
extractTicketId()
{
echo "$(getGitBranchName)" \
| awk 'BEGIN{ FS="[/]"}
$1 == "id" { printf "refs %s", $2 }
$2 == "id" { printf "refs %s", $3 }'
}
hasTicketId()
{
first="$(git cat-file -p $1 \
| sed '1,/^$/d' | head -1 \
| sed '/.*refs [0-9][0-9]*.*/!d')"
if [ -n "${first}" ]; then
echo "true"
else
echo "false"
fi
}
extractParents()
{
parents="$(git cat-file -p $1 \
| grep '^parent [0-9a-f]\{40\}$')"
echo "${parents##parent }"
}