-
Notifications
You must be signed in to change notification settings - Fork 6
/
getrev.sh
executable file
·66 lines (56 loc) · 1.72 KB
/
getrev.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
60
61
62
63
64
65
66
zeroone() {
case "$1" in
0) echo "";;
1) echo "$1 $2";;
*) echo "$1 $3";;
esac
}
concat() {
if [[ "$2" != "" ]]; then
echo "$1, $2"
else
echo "$1"
fi
}
timediff() {
local t="$1"
local d="$(zeroone "$((t/60/60/24))" day days)"
local h="$(zeroone "$((t/60/60%24))" hour hours)"
local m="$(zeroone "$((t/60%60))" minute minutes)"
local s="$(zeroone "$((t%60))" second seconds)"
if [[ "$d" != "" ]]; then concat "$d" "$h"
elif [[ "$h" != "" ]]; then concat "$h" "$m"
elif [[ "$m" != "" ]]; then concat "$m" "$s"
elif [[ "$s" != "" ]]; then echo "$s"
else echo "???"
fi
}
rev="$1"
branch="$3"
github="$4"
cd "$2"
git fetch origin > /dev/null 2> /dev/null
ancestor=false
descendant=false
if git merge-base --is-ancestor origin/"$branch" "$rev" >/dev/null 2>/dev/null; then
descendant=true
fi
if git merge-base --is-ancestor $rev origin/"$branch" >/dev/null 2>/dev/null; then
ancestor=true
fi
if $ancestor && $descendant; then
echo "Current server revision is equal to $branch; no update required"
elif $ancestor; then
commit_count="$(git log --oneline $rev..origin/"$branch" | wc -l)"
if [[ $commit_count == 1 ]]; then commit_count="1 commit"
else commit_count="$commit_count commits"
fi
dev_time=$(git log origin/"$branch" -n 1 --format=%ct) # Unix timestamps
rev_time=$(git log $rev -n 1 --format=%ct)
time_difference=$(timediff $(($dev_time - $rev_time)))
echo "Current server revision is $time_difference ($commit_count) behind $branch; update required - https://$github/compare/$rev...$branch"
elif $descendant; then
echo "Current server revision is descendant of $branch; is the server ahead of GitHub?"
else
echo "Current server revision has unknown status. Possibly $branch and the server have diverged."
fi