-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-informed.sh
executable file
·74 lines (67 loc) · 1.87 KB
/
git-informed.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
67
68
69
70
71
72
73
74
#!/bin/bash
# set -e
# 'Darwin'|'Linux'
platform=`uname`
show_notification() {
[[ -n $NO_NOTIFICATION ]] && return
if [[ $platform == 'Darwin' ]]; then
eval "osascript -e 'display notification \"$2\" with title \"$1\"'"
elif [[ $platform == 'Linux' ]]; then
eval "notify-send \"$1\" \"$2\""
fi
}
show_alert_modal() {
[[ -z $SHOW_ALERT ]] && return
if [[ $platform == 'Darwin' ]]; then
eval "osascript -e 'display alert \"New commit on origin/$1\" message \"$2\"'"
fi
}
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-v|--verbose)
VERBOSE=true
shift # past argument
;;
-a|--alert)
SHOW_ALERT=true
shift # past argument
;;
-N|--no-notification)
NO_NOTIFICATION=true
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
# 1st positional arg is branch
prev_remote_ahead=0
while :
do
git fetch origin > /dev/null
repo=`basename $(git rev-parse --show-toplevel)`
[ "$1" ] \
&& branch="$1" \
|| branch=`git branch --show-current`
compare=`git rev-list --left-right --count origin/${branch}...${branch}`
compare_array=($compare)
remote_ahead=${compare_array[0]}
local_ahead=${compare_array[1]}
message="${repo} (${branch}) $remote_ahead commits behind origin"
[[ -n $VERBOSE ]] && echo $message
if [[ $remote_ahead -gt $prev_remote_ahead ]]; then
log_lines=$(( $remote_ahead > 1 ? 2 : 1 ))
log=`git log --oneline --pretty=format:"• %<(50,trunc)%s" -n $log_lines origin/${branch}`
show_notification "${message}" "${log}"
last_log=`git log --oneline --pretty=format:"• %s" -n 1 origin/${branch}`
show_alert_modal "${branch}" "${last_log}"
fi
prev_remote_ahead=${remote_ahead}
sleep 5
done