-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemgit.sh
executable file
·264 lines (227 loc) · 6.73 KB
/
systemgit.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
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
#!/bin/sh
#
# Maintain a list of git repositories on your system and provide a quick way to
# see the status of all of them
#
# For usage, call systemgit -h
#
# Author: Andrew Cox
# Version: 4 Feb 2021
# Reset POSIX variable
OPTIND=1
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'
BOLD=$(tput bold)
NORM=$(tput sgr0)
# ------------------------------------------------------------------------------
# Function Definitions
# ------------------------------------------------------------------------------
isInRepoList() {
local path=$1
# Determine if path is in the REPO_LIST file
while read line
do
if [[ $line == \#* ]]; then
continue
fi
if [ $line = $path ]; then
return 1
fi
done < $REPO_LIST
return 0
}
isGitRepo(){
local path=$1
# Determine if the directory, $path, is a git repository
if [ -d "$path/.git" ]; then
return 1
else
return 0
fi
}
showRepoStatus () {
local path=$1 # path to git repository, no trailing slash
local maxLen=$2 # length of longest path string
# Check that we have a valid repo; return if not
isGitRepo $path
if [ $? = 0 ]; then
return
fi
# Command to get info from a git repo in a different directory
gitAtDir="git --git-dir=$path/.git --work-tree=$path"
# Bring remote references up to date; this can take some computation time!
# If the repo requires a password, the process will hang until the password is input
#update=$($gitAtDir remote update)
# most recent commit
commit=$($gitAtDir log -1 | grep "commit" |\
awk '$2>0 {print substr($2,1,6)}')
# Date of most recent commit
ver=$($gitAtDir log -1 | grep "Date" | awk '{print $3, $4, $5, $6}')
# Current branch
branch=$($gitAtDir status | grep "On branch" | awk '{print $3}')
# Status of current branch w.r.t. origin
status=$($gitAtDir status | grep "Your branch is" |\
awk '{for(i=4;i<=NF;i++) printf $i" "; print ""}')
if [ -z "$status" ]; then
status="N/A"
statusColor="${ORANGE}"
else
if [ "${status#*up to date}" != "$status" ]; then
statusColor="${GREEN}"
else
statusColor="${RED}"
fi
fi
# number of modifications
numMod=$($gitAtDir diff --numstat | wc -l | awk '{print $1}')
if [ -z "$numMod" ] || [ "$numMod" -eq 0 ]; then
numMod="0"
modColor="${GREEN}"
else
modColor="${RED}"
fi
if [ $verbose == 1 ]; then
print "${BLUE}${BOLD}$path:${NORM}${NC}"
print " Currently at $branch/$commit"
print " Updated $ver"
print " ${statusColor}Status: $status${NC}"
print " ${modColor}$numMod modifications${NC}"
print "" # newline after each repo
else
# Use printf to avoid newlines
printf "${BLUE}${BOLD}%-${maxLen}s:${NORM}${NC}" "$path"
printf "%-25s" " $branch/$commit"
printf "%-24s" " | $ver"
printf " | ${statusColor}%-40s${NC}" "$status"
printf " | ${modColor}$numMod diff${NC}"
printf "\n"
fi
}
showHelp(){
print "Usage: systemgit"
print " systemgit [show | add | add-all | remove]\n"
print "Arguments:"
print " show: show the status of all tracked repositories"
print " add: add the current directory as a tracked repository"
print " add-all: add all git repositories within the current directory as"
print " tracked repositories"
print " remove: remove the current directory from being tracked"
print ""
print "Options:"
print " -h Show this help information"
print " -f Specify a path for add/remove/add-all other than the current directory"
print " -v Use verbose output"
}
print() {
printf "$1\n"
}
# ------------------------------------------------------------------------------
# Parse Inputs
# ------------------------------------------------------------------------------
# Default values
path="`pwd`"
verbose=0
REPO_LIST="$HOME/.gitrepos"
# Parse options
while getopts "h?vf:" opt; do
case "$opt" in
h|\?)
showHelp
exit 0
;;
v)
verbose=1
;;
f)
path="$OPTARG"
;;
esac
done
# Shift to after the options
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
if [ "$#" -lt 1 ]; then
# No arguments passed in - do default action
action="show"
else
action="$1"
fi
# Check to make sure REPO_LIST exists
if [ ! -f $REPO_LIST ]; then
touch $REPO_LIST
fi
case $action in
help)
showHelp
;;
show) # Display git-repos
print "Status of all repositories:\n"
# Get length of longest string
maxLen=0
while read line
do
len=${#line}
if [ $len -gt $maxLen ]; then
maxLen=$len
fi
done < $REPO_LIST
# Print the repository status
while read line
do
if [[ $line == \#* ]]; then
# Print lines that begin with "#" character
print "$line"
else
showRepoStatus $line $maxLen
fi
done < $REPO_LIST
print "Done!"
;;
add) # add a repo to the list
isInRepoList $path
if [ $? = 1 ]; then
print "Error: $path is already listed"
exit 1
fi
isGitRepo $path
if [ $? = 0 ]; then
print "Error: $path is not a git repo"
exit 1
fi
# We've passed the checks, add the repo to the list
echo $path >> $REPO_LIST
print "Added $path"
;;
add-all) # add all repos within the current directory to the list
# $0 is the full path to this script, so this essentially just runs
# 'systemgit add' from each git repo found
find "$path" -name .git -type d -execdir "$0" add \;
;;
remove) # remove a repo from the list
isInRepoList $path
if [ $? = 0 ]; then
print "$path is not listed"
exit 0
fi
# The path exists, so delete it from the list
# Read through the lines of REPO_LIST and write any
# that aren't the line to delete to the temp file. Finally, replace
# the list with the temp file and delete the temp file.
tempFile="$REPO_LIST.bak"
touch $tempFile
while read line
do
if [ $line != $path ]; then
echo $line >> $tempFile
fi
done < $REPO_LIST
mv $tempFile $REPO_LIST
;;
*)
print "Error: unrecognized command: $action"
showHelp
;;
esac