forked from fboender/multi-git-status
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mgitstatus
executable file
·386 lines (349 loc) · 14.1 KB
/
mgitstatus
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/bin/bash
# MIT license
VERSION="2.0"
DEBUG=0
usage () {
cat << EOF >&2
Usage: $0 [--version] [-w] [-e] [-f] [--no-X] [-d/--depth=2] [DIR [DIR]...]
mgitstatus shows uncommited, untracked and unpushed changes in multiple Git
repositories. By default, mgitstatus scans two directories deep. This can be
changed with the -d (--depth) option. If DEPTH is 0, the scan is infinitely
deep.
--version Show version
-w Warn about dirs that are not Git repositories
-e Exclude repos that are 'ok'
-f Do a 'git fetch' on each repo (slow for many repos)
-c Force color output (preserve colors when using pipes)
-d, --depth=2 Scan this many directories deep
--info Adds info output
You can limit output with the following options:
--no-push
--no-pull
--no-upstream
--no-unnecessary-upstream Don't print if that branch has no unique commits to send
--no-uncommitted
--no-untracked
--no-stashes
--no-stalled
--only-submodules
EOF
}
# Parse command line arguments
# ---------------------------
# Initialize parameters
WARN_NOT_REPO=0
EXCLUDE_OK=0
DO_FETCH=0
FORCE_COLOR=0
NO_PUSH=0
NO_PULL=0
NO_UPSTREAM=0
NO_UNCOMMITTED=0
NO_UNTRACKED=0
NO_STASHES=0
ONLY_SUBMODULES=0
DEPTH=2
INFO_OUTPUT=0
NO_STALLED=
NO_UNNECESSARY_UPSTREAM=0
# ---------------------------
args_backup=("$@")
args=()
_count=1
while :; do
key="${1:-}"
case $key in
-h|-\?|--help)
usage # Display a usage synopsis.
exit
;;
# --------------------------------------------------------
--version) shift
echo "v$VERSION"
exit 0
;;
-w) shift
WARN_NOT_REPO=1
;;
-e) shift
EXCLUDE_OK=1
;;
-f) shift
DO_FETCH=1
;;
-c) shift
FORCE_COLOR=1
;;
--no-push) shift
NO_PUSH=1
;;
--no-pull) shift
NO_PULL=1
;;
--no-upstream) shift
NO_UPSTREAM=1
;;
--no-unnecessary-upstream) shift
NO_UNNECESSARY_UPSTREAM=1
;;
--no-uncommitted) shift
NO_UNCOMMITTED=1
;;
--no-untracked) shift
NO_UNTRACKED=1
;;
--no-stashes) shift
NO_STASHES=1
;;
--no-stalled) shift
NO_STALLED=1
;;
--only-submodules) shift
ONLY_SUBMODULES=1
;;
--info) shift
INFO_OUTPUT=1
;;
-d|-d=*|--depth|--depth=*)
if [[ "$1" == *"="* ]]; then
DEPTH="${1#*=}" # remove the prefix
else
shift
DEPTH="$1"
fi
shift
echo "$DEPTH" | grep -E "^[0-9]+$" > /dev/null 2>&1
IS_NUM="$?"
if [ "$IS_NUM" -ne 0 ]; then
echo "Invalid value for 'depth' (must be a number): ${DEPTH:-NULL}" >&2
exit 1
fi
;;
# --------------------------------------------------------
-*) # Handle unrecognized options
echo
echo "Unknown option: $1"
echo
exit 1
;;
*) # Generate the new positional arguments: $arg1, $arg2, ... and ${args[@]}
if [[ ! -z ${1:-} ]]; then
declare arg$((_count++))="$1"
args+=("$1")
shift
fi
esac
[[ -z ${1:-} ]] && break
done; set -- "${args_backup[@]}"
# Use $arg1 in place of $1, $arg2 in place of $2 and so on,
# "$@" is in the original state,
# use ${args[@]} for new positional arguments
if [ -t 1 ] || [ "$FORCE_COLOR" -eq 1 ]; then
# Our output is not being redirected, so we can use colors.
C_RED="\033[1;31m"
C_GREEN="\033[1;32m"
C_YELLOW="\033[1;33m"
C_BLUE="\033[1;34m"
C_PURPLE="\033[1;35m"
C_CYAN="\033[1;36m"
C_RESET="$(tput sgr0)"
fi
C_OK="$C_GREEN"
C_LOCKED="$C_RED"
C_NEEDS_PUSH="$C_YELLOW"
C_NEEDS_PULL="$C_BLUE"
C_NEEDS_COMMIT="$C_RED"
C_NEEDS_UPSTREAM="$C_PURPLE"
C_UNTRACKED="$C_CYAN"
C_STASHES="$C_YELLOW"
C_STALLED="$C_YELLOW"
C_NO_COMMITS_YET="$C_YELLOW"
# Find all .git dirs, up to DEPTH levels deep. If DEPTH is 0, the scan in
# infinitely deep
FIND_OPTS=""
if [ "$DEPTH" -ne 0 ]; then
FIND_OPTS="$FIND_OPTS -maxdepth $DEPTH"
fi
containsElement () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
# Go through positional arguments (DIRs) or '.' if no argumnets are given
for DIR in "${args[@]:-"."}"; do
# We *want* to expand parameters, so disable shellcheck for this error:
# shellcheck disable=SC2086
readarray -d '' PROJ_DIRS < <(find -L "$DIR" $FIND_OPTS -depth -name ".git" -printf "%h\0" )
[ $INFO_OUTPUT -eq 1 ] && \
1>&2 echo "INFO: Examining ${#PROJ_DIRS[@]} git folders recursively..."
PROJ_NEED_ATTENTION=0
i=0
while :; do
#printf ' -- PROJ_DIRS: %s\n' "${PROJ_DIRS[@]}"
(("$i" >= "${#PROJ_DIRS[@]}")) && break
PROJ_DIR="${PROJ_DIRS[$i]}"
i=$((i+1))
GIT_DIR="$PROJ_DIR/.git"
IS_SUBMODULE=
if [[ -f "$GIT_DIR" ]]; then
# This is a submodule
IS_SUBMODULE="yes"
BARE_DIR="$(cat "$PROJ_DIR/.git" | awk '{print $2}')"
GIT_DIR="$(realpath "$PROJ_DIR/$BARE_DIR")"
fi
GIT_CONF="$GIT_DIR/config"
# If this dir is not a repo, and WARN_NOT_REPO is 1, tell the user.
if [ ! -d "$GIT_DIR" ]; then
if [ "$WARN_NOT_REPO" -eq 1 ] && [ "$PROJ_DIR" != "." ]; then
printf "${PROJ_DIR}: not a git repo\n"
fi
continue
fi
# Check git config for this project to see if we should ignore this repo.
IGNORE=$(git config -f "$GIT_CONF" --bool mgitstatus.ignore)
if [ "$IGNORE" = "true" ]; then
continue
fi
[ $DEBUG -eq 1 ] && echo "${PROJ_DIR}"
# Check if repo is locked
if [ -f "$GIT_DIR/index.lock" ]; then
printf "${PROJ_DIR}: ${C_LOCKED}Locked. Skipping.${C_RESET}\n"
continue
fi
# Do a 'git fetch' if requested
if [ "$DO_FETCH" -eq 1 ]; then
git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" fetch -q >/dev/null
fi
# Refresh the index, or we might get wrong results.
git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" update-index -q --refresh >/dev/null 2>&1
# Now we know we are dealing with a git repo. Find out its submodules:
SUBMODULES=($(cd "$PROJ_DIR"; git config --file=.gitmodules --get-regexp ^^submodule.*\.path$ | cut -d " " -f 2))
if [[ ! -z $SUBMODULES ]]; then
#printf ' | Submodule: %s\n' "${SUBMODULES[@]}"
j=0
for submodule in "${SUBMODULES[@]}"; do
SUBMODULE_PATH="${PROJ_DIR%/}/${submodule}"
if ! containsElement "$SUBMODULE_PATH" "${PROJ_DIRS[@]}"; then
#echo "...appending submodule path: $SUBMODULE_PATH"
#PROJ_DIRS+=( "$SUBMODULE_PATH" ) # Below is the same as this, but fixes the order
PROJ_DIRS=( "${PROJ_DIRS[@]:0:$(($i+$j))}" "$SUBMODULE_PATH" "${PROJ_DIRS[@]:$(($i+$j))}" )
j=$(($j + 1))
fi
done
fi
[ "$IS_SUBMODULE" != "yes" ] && [ $ONLY_SUBMODULES -eq 1 ] && continue
BNAME=$(cd "$PROJ_DIR"; git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [[ $? -gt 0 ]]; then
printf "${PFX}${PROJ_DIR}: ${C_NO_COMMITS_YET}Repo has no commits yet${C_RESET}\n"
PROJ_NEED_ATTENTION=$(($PROJ_NEED_ATTENTION + 1))
continue
fi
# Find all remote branches that have been checked out and figure out if
# they need a push or pull. We do this with various tests and put the name
# of the branches in NEEDS_XXXX, seperated by newlines. After we're done,
# we remove duplicates from NEEDS_XXX.
NEEDS_PUSH_BRANCHES=""
NEEDS_PULL_BRANCHES=""
NEEDS_UPSTREAM_BRANCHES=""
for REF_HEAD in $(cd "$GIT_DIR/refs/heads" && find . -type 'f' | sed "s/^\.\///"); do
# Check if this branch is tracking an upstream (local/remote branch)
UPSTREAM=$(git --git-dir "$GIT_DIR" rev-parse --abbrev-ref --symbolic-full-name "$REF_HEAD@{u}" 2>/dev/null)
EXIT_CODE="$?"
if [ "$EXIT_CODE" -eq 0 ]; then
# Branch is tracking a remote branch. Find out how much behind /
# ahead it is of that remote branch.
CNT_AHEAD_BEHIND=$(git --git-dir "$GIT_DIR" rev-list --left-right --count "$REF_HEAD...$UPSTREAM")
CNT_AHEAD=$(echo "$CNT_AHEAD_BEHIND" | awk '{ print $1 }')
CNT_BEHIND=$(echo "$CNT_AHEAD_BEHIND" | awk '{ print $2 }')
[ $DEBUG -eq 1 ] && echo "CNT_AHEAD_BEHIND: $CNT_AHEAD_BEHIND"
[ $DEBUG -eq 1 ] && echo "CNT_AHEAD: $CNT_AHEAD"
[ $DEBUG -eq 1 ] && echo "CNT_BEHIND: $CNT_BEHIND"
if [ "$CNT_AHEAD" -gt 0 ]; then
NEEDS_PUSH_BRANCHES="${NEEDS_PUSH_BRANCHES}\n$REF_HEAD"
fi
if [ "$CNT_BEHIND" -gt 0 ]; then
NEEDS_PULL_BRANCHES="${NEEDS_PULL_BRANCHES}\n$REF_HEAD"
fi
# Check if this branch is a branch off another branch. and if it needs
# to be updated.
REV_LOCAL=$(git --git-dir "$GIT_DIR" rev-parse --verify "$REF_HEAD" 2>/dev/null)
REV_REMOTE=$(git --git-dir "$GIT_DIR" rev-parse --verify "$UPSTREAM" 2>/dev/null)
REV_BASE=$(git --git-dir "$GIT_DIR" merge-base "$REF_HEAD" "$UPSTREAM" 2>/dev/null)
[ $DEBUG -eq 1 ] && echo "REV_LOCAL: $REV_LOCAL"
[ $DEBUG -eq 1 ] && echo "REV_REMOTE: $REV_REMOTE"
[ $DEBUG -eq 1 ] && echo "REV_BASE: $REV_BASE"
if [ "$REV_LOCAL" = "$REV_REMOTE" ]; then
: # NOOP
else
if [ "$REV_LOCAL" = "$REV_BASE" ]; then
NEEDS_PULL_BRANCHES="${NEEDS_PULL_BRANCHES}\n$REF_HEAD"
fi
if [ "$REV_REMOTE" = "$REV_BASE" ]; then
NEEDS_PUSH_BRANCHES="${NEEDS_PUSH_BRANCHES}\n$REF_HEAD"
fi
fi
else
# Branch does not have an upstream (local/remote branch).
[[ $NO_UNNECESSARY_UPSTREAM -eq 0 || "$BNAME" == "$REF_HEAD" ]] && \
NEEDS_UPSTREAM_BRANCHES="${NEEDS_UPSTREAM_BRANCHES}\n$REF_HEAD"
fi
done
# Remove duplicates from NEEDS_XXXX and make comma-seperated
NEEDS_PUSH_BRANCHES=$(printf "$NEEDS_PUSH_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/")
NEEDS_PULL_BRANCHES=$(printf "$NEEDS_PULL_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/")
NEEDS_UPSTREAM_BRANCHES=$(printf "$NEEDS_UPSTREAM_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/")
# Find out if there are unstaged, uncommitted or untracked changes
UNSTAGED=$(git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" diff-index --quiet HEAD -- 2>/dev/null; echo $?)
UNCOMMITTED=$(git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" diff-files --quiet --ignore-submodules --; echo $?)
UNTRACKED=$(git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" ls-files --exclude-standard --others)
STASHES=$(cd "$PROJ_DIR"; git --work-tree=. stash list | wc -l)
STALLED=$(git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" branch --no-merged | xargs | sed -e 's/ /,/g')
# Build up the status string
IS_OK=0 # 0 = Repo needs something, 1 = Repo needs nothing ('ok')
STATUS_NEEDS=""
if [ -n "$NEEDS_PUSH_BRANCHES" ] && [ "$NO_PUSH" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_PUSH}Needs push ($NEEDS_PUSH_BRANCHES)${C_RESET} "
fi
if [ -n "$NEEDS_PULL_BRANCHES" ] && [ "$NO_PULL" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_PULL}Needs pull ($NEEDS_PULL_BRANCHES)${C_RESET} "
fi
if [ -n "$NEEDS_UPSTREAM_BRANCHES" ] && [ "$NO_UPSTREAM" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_UPSTREAM}Needs upstream ($NEEDS_UPSTREAM_BRANCHES)${C_RESET} "
fi
if [ "$UNSTAGED" -ne 0 ] || [ "$UNCOMMITTED" -ne 0 ] && [ "$NO_UNCOMMITTED" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_COMMIT}Uncommitted changes${C_RESET} "
fi
if [ "$UNTRACKED" != "" ] && [ "$NO_UNTRACKED" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_UNTRACKED}Untracked files${C_RESET} "
fi
if [ "$STASHES" -ne 0 ] && [ "$NO_STASHES" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_STASHES}$STASHES stashes${C_RESET} "
fi
STALLED_COUNT=$(echo $STALLED | tr ',' ' ' | wc -w)
if [ "$STALLED_COUNT" -ne 0 ] && [[ "$BNAME" != "HEAD" ]] && [ -z "$NO_STALLED" ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_STALLED}${STALLED_COUNT} stalled ($STALLED)${C_RESET} "
## dump the commit logs which only the other branches have
#for b in `git branch | grep -v "*"`; do
# git log --cherry-pick --oneline --no-merges --left-only ${b}...${BNAME}
#done
fi
if [ "$STATUS_NEEDS" = "" ]; then
IS_OK=1
STATUS_NEEDS="${STATUS_NEEDS}${C_OK}ok${C_RESET} "
else
PROJ_NEED_ATTENTION=$(($PROJ_NEED_ATTENTION + 1))
fi
# Print the output, unless repo is 'ok' and -e was specified
PFX=
BRANCH_INFO=
if [ "$IS_OK" -ne 1 ] || [ "$EXCLUDE_OK" -ne 1 ]; then
if [ $INFO_OUTPUT -eq 1 ]; then
PFX="STATUS: "
BRANCH_INFO=" ${C_CYAN}@$BNAME${C_RESET}:"
fi
printf "${PFX}${PROJ_DIR}:${BRANCH_INFO} $STATUS_NEEDS\n"
fi
done
[ $INFO_OUTPUT -eq 1 ] && echo "INFO: Projects require attention: ${PROJ_NEED_ATTENTION}"
done