blob: d2262d45264be16187b1cba200b59732b355ee43 (
plain) (
blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
dot_git=""
cwd=""
remote=""
in_current_dir() {
local wd="$(pwd)"
if [[ "$wd" == $cwd ]]; then
cwd="$wd"
return 0
else
cwd="$wd"
return 1
fi
}
debug_print() {
local debug=$1
local message=$2
if [[ $debug == "debug" ]]; then
echo $message
fi
}
dot_git() {
if in_current_dir && [[ -n "$dot_git" ]]; then
# cache dot_git to save calls to rev-parse
echo $dot_git
elif [ -d .git ]; then
dot_git=".git"
echo $dot_git
else
dot_git="$(git rev-parse --git-dir 2>/dev/null)"
echo $dot_git
fi
}
is_repo() {
if [[ -n "$(dot_git)" ]]; then
return 0
else
return 1
fi
}
git_root() {
if [ -d .git ]; then
echo "$(pwd)"
else
echo "$(git rev-parse --show-toplevel 2>/dev/null)"
fi
}
record_timestamp() {
if is_repo; then
touch "$(dot_git)/lastupdatetime"
fi
}
timestamp() {
if is_repo; then
echo "$(stat -f%m "$(dot_git)/lastupdatetime")"
fi
}
time_now() {
echo "$(date +%s)"
}
time_to_update() {
if is_repo; then
local timesincelastupdate="$(($(time_now) - $(timestamp)))"
local fiveminutes="$((5 * 60))"
if (( "$timesincelastupdate" > "$5minutes" )); then
# time to update return 0 (which is true)
return 0
else
# not time to update return 1 (which is false)
return 1
fi
else
return 1
fi
}
fetch_async() {
local debug="$1"
if time_to_update; then
debug_print $debug "Starting fetch"
fetch $debug &
else
debug_print $debug "Didn't fetch"
fi
}
fetch() {
local debug="$1"
git fetch
debug_print $debug "Finished fetch"
}
branch_name() {
if is_repo; then
local localBranch="$(git symbolic-ref --short HEAD)"
echo $localBranch
fi
}
is_tracking_remote() {
if [[ -n "$(remote_branch_name)" ]]; then
return 0
else
return 1
fi
}
remote_branch_name() {
if is_repo; then
local remoteBranch="$(git for-each-ref --format='%(upstream:short)' | grep "$(branch_name)")"
if [[ -n $remoteBranch ]]; then
echo $remoteBranch
return 0
else
return 1
fi
fi
}
commits_behind_of_remote() {
if is_tracking_remote; then
set --
set -- $(git rev-list --left-right --count $(remote_branch_name)...HEAD)
behind=$1
ahead=$2
set --
echo $behind
else
echo "0"
fi
}
commits_ahead_of_remote() {
if is_tracking_remote; then
set --
set -- $(git rev-list --left-right --count $(remote_branch_name)...HEAD)
behind=$1
ahead=$2
set --
echo $ahead
else
echo "0"
fi
}
remote_behind_of_master() {
if is_tracking_remote; then
set --
set -- $(git rev-list --left-right --count origin/master...$(remote_branch_name))
behind=$1
ahead=$2
set --
echo $behind
else
echo "0"
fi
}
remote_ahead_of_master() {
if is_tracking_remote; then
set --
set -- $(git rev-list --left-right --count origin/master...$(remote_branch_name))
behind=$1
ahead=$2
set --
echo $ahead
else
echo "0"
fi
}
|