-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-ahead
executable file
·103 lines (96 loc) · 2.94 KB
/
git-ahead
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env sh
_parse_arguments() {
local arg
while (( $# > 0 ))
do
arg="$1"
case "$arg" in
--submodules)
_fetch_submodules="--recurse-submodules"
;;
--force)
_force_fetch="yes"
;;
-*)
_git_options="${_git_options:-} $arg"
;;
--*)
_git_options="${_git_options:-} $arg"
;;
*)
[[ -z ${_target_branch} ]] && _target_branch="$arg"
;;
esac
shift
done
return 0
}
_get_branch_details() {
_current_branch=$(git rev-parse --abbrev-ref HEAD)
_tracking_branch=$(git config branch.$_current_branch.merge)
_tracking_remote=$(git config branch.$_current_branch.remote)
_tracking_branch_name=${_tracking_branch#"refs/heads/"}
_remote_url=$(git config remote.$_tracking_remote.url)
}
_fetch_origin_if_needed() {
_last_fetch=$(git config gitincoming.lastFetch || echo 0)
_cache_duration=$(git config gitincoming.cacheDuration || echo 3600)
_current_time=$(date +%s)
_next_fetch=$((_last_fetch + _cache_duration))
[[ ${_current_time} -ge ${_next_fetch} ]] && _should_fetch_remote="yes"
if [[ -n ${_should_fetch_remote} || -n ${_force_fetch} ]]; then
git fetch ${_fetch_submodules:-"--no-recurse-submodules"} ${_tracking_remote} && git config gitincoming.lastFetch $(date +%s)
fi
}
_show_remote_log() {
local binaryName=$(basename $0)
local actionName=${binaryName#git-}
case "$actionName" in
behind)
git log ${_git_options:-} ..${_tracking_remote}/${_tracking_branch_name}
;;
ahead)
git log ${_git_options:-} ${_tracking_remote}/${_tracking_branch_name}..
;;
esac
}
_show_local_log() {
local binaryName=$(basename $0)
local actionName=${binaryName#git-}
case "$actionName" in
behind)
git log ${_git_options:-} ..${_tracking_branch_name}
;;
ahead)
git log ${_git_options:-} ${_tracking_branch_name}..
;;
esac
}
_show_target_log() {
local binaryName=$(basename $0)
local actionName=${binaryName#git-}
case "$actionName" in
behind)
git log ${_git_options:-} ..${1}
;;
ahead)
git log ${_git_options:-} ${1}..
;;
esac
}
main() {
_parse_arguments $@
_get_branch_details
# if we have a target branch, compare with that and exit
[[ -n ${_target_branch:-} ]] && \
_show_target_log "$_target_branch" && \
exit $?
# check if we need to compare against a local branch and exit
[[ -z ${_tracking_remote} || ${_tracking_remote} == "." ]] && \
_show_local_log && \
exit $?
# otherwise, fetch info and compare against the tracked remote branch
_fetch_origin_if_needed
_show_remote_log
}
main $@