-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitdir
437 lines (394 loc) · 15.2 KB
/
gitdir
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env bash
#
# Created By: stephansarver
# Created Date: 20180531-092037
#
# set -o errexit
# set -o errtrace
set -o nounset
# TODO: Make gitdir aware of the calling directory so the installation or location of the script does not matter!
clear
#Initialize the command switches
cmd_switch_check_uncommitted="-c"
cmd_switch_delete_config="-d"
cmd_switch_git_fetch="-f"
cmd_switch_show_repo_list="-l"
cmd_switch_show_repo_branch="-b"
cmd_switch_git_pull="-p"
cmd_switch_update_repo_list="-u"
cmd_switch_set_config_for_current_repos="-m"
# TODO: Figure out how to do this
# cmd_switch_display_review_requests="-r"
github_user=""
github_org_name=""
github_team_name=""
github_api_base="https://api.github.com"
github_repo_base="https://github.com/"
github_api_per_page="100"
file_name_for_config=".gitdir_conf"
file_name_for_repos=".gitdir_repos"
file_name_for_skip_repos=".gitdir_skip_repos"
file_name_for_git_token=".gitdir_pat"
#repos=()
function usage(){
echo "USAGE:"
echo " "
echo " Name: gitdir"
echo " "
echo " Description: "
echo " This script is used to handle some basic batch git functions for a organizations git repositories."
echo " The script will store in hidden files the github organization name and github user name so it does"
echo " not have to be re-entered each time the script is run. This script will also store in a hidden file "
echo " a list of repos that were looked up for the organization that the user has access to."
echo " See additional notes below."
echo " "
echo " "
echo " Command Switches:"
echo " -c Used to check for repos with uncommitted files."
echo " -d Used to delete locally stored configuration and start new."
echo " -f Used to fetch and update the local git indexes for the repositories."
echo " -l Used to show the list of cloned repositories."
echo " -b Used to show the checkout branch of each cloned repository."
echo " -p Used to pull the latested currently checked out versions of the repositories."
echo " -u Used for updating the locally cached repository list."
echo " -m Match or set the gitdir config for the currently cloned repos(do not pull all from org)."
# TODO: Figure out how to do this
# echo " -r Used for listing review requests."
echo " "
echo " "
echo " Config files:"
echo " [$file_name_for_config] - This file contains the organization, username and team name which"
echo " is parsed when interacting with the github api. This file is a '|' seperated list of "
echo " github organization name|github username|github team name"
echo " github organization and team can be blank."
echo " [$file_name_for_repos] - This file contains repos that gitdir manages"
echo " [$file_name_for_skip_repos] - This file contains directories(repos) that gitdir will ignore"
echo " [$file_name_for_git_token] - This file contains your personal access token. This file is not"
echo " required if you want to be prompted to enter it each time gitdir makes an api call."
echo " "
}
function throw_error(){
local_param_property=$1
local_param_message=$2
echo "********************"
echo "Error: "
echo " "
echo " $local_param_property $local_param_message "
echo " "
echo "********************"
exit 1
}
function populate_repo_list {
local page_count=1
local repo_count=0
if [ -f "$file_name_for_repos" ]; then
rm "$file_name_for_repos"
fi
touch "$file_name_for_repos"
while [ "$(expr $repo_count % $github_api_per_page)" == "0" ]
do
git_user_token=
if [ -f "$file_name_for_git_token" ]; then
git_user_token=":$(cat $file_name_for_git_token)"
fi
git_user_string="$github_user$git_user_token"
if [ "" == "$github_team_name" ]; then
if [ "" == "$github_org_name" ]; then
output=$(curl -L -u "$github_user" "$github_api_base/user/repos?page=$page_count&per_page=$github_api_per_page")
else
output=$(curl -L -u "$github_user" "$github_api_base/orgs/$github_org_name/repos?page=$page_count&per_page=$github_api_per_page")
fi
else
echo "Selecting repos by team using: $github_org_name and $github_team_name"
output=$(curl -L -u "$git_user_string" "$github_api_base/users/$github_user/orgs")
github_org_id=$(echo "$output" | jq -r --arg ORG_NAME "$github_org_name" '.[] | select(.login==$ORG_NAME) | .id')
output=$(curl -L -u "$git_user_string" "$github_api_base/orgs/$github_org_name/teams?page=$page_count&per_page=$github_api_per_page")
github_team_id=$(echo "$output" | jq -r --arg TEAM_NAME "$github_team_name" '.[] | select(.name==$TEAM_NAME) | .id')
github_team_repositories_url=$(echo "$output" | jq -r --arg TEAM_NAME "$github_team_name" '.[] | select(.name==$TEAM_NAME) | .repositories_url')
output=$(curl -u "$git_user_string" "$github_team_repositories_url?page=$page_count&per_page=$github_api_per_page")
fi
echo "$output" | jq -r ".[].name" >>"$file_name_for_repos"
repo_count=$(wc -l < "$file_name_for_repos")
page_count=$((page_count+1))
done
}
# Iterate local directorry and only add to config directories that have a .git directory!
function populate_cloned_repo_list {
rm "$file_name_for_repos"
touch "$file_name_for_repos"
for dir in $(ls)
do
if [ -d "$dir" ]; then
for nesteddir in $(ls -a $dir)
do
if [ ".git" == "$nesteddir" ]; then
echo "Setting - $dir"
echo "$dir" >>"$file_name_for_repos"
fi
done
fi
done
}
function delete_git_config {
echo "You are about to delete your saved github organization name and github user name you will have to re-enter this the next time you run this script are you sure you want to proceed?"
read -r delete_config
if [[ "y" == "$delete_config" || "Y" == "$delete_config" || "yes" == "$delete_config" || "YES" == "$delete_config" || "Yes" == "$delete_config" ]]; then
if [ -f "$file_name_for_config" ]; then
rm "$file_name_for_config"
else
echo "The config file does not exist and could not be deleted: $file_name_for_config"
fi
else
throw_error "Aborted deleteing gitdir config." "$delete_config"
fi
}
function load_git_config {
if [ ! -f "$file_name_for_config" ]; then
echo "Creating git config file"
create_git_config
fi
if [ -f "$file_name_for_config" ]; then
echo "loading from git config file"
gitdir_config=$(cat $file_name_for_config)
echo "$gitdir_config"
#declare -a gitdir_configs=(${gitdir_config//|/ })
OLDIFS=$IFS
IFS='|' read -r -a gitdir_configs <<< "$gitdir_config"
IFS=$OLDIFS
github_org_name="${gitdir_configs[0]}"
github_user="${gitdir_configs[1]}"
if [ "${#gitdir_configs[@]}" -eq "3" ]; then
github_team_name="${gitdir_configs[2]}"
fi
fi
if [[ "" == "$github_user" ]]; then
echo "Storing to git config file"
create_git_config
fi
}
function create_git_config {
echo " "
echo "Please enter the github organization name and hit enter (This can be empty):"
read -r github_org_name
echo " "
echo "Please enter your github user name and hit enter:"
read -r github_user
echo " "
echo "Please enter your github team name to restrict gitdir to only your teams repos hit enter (This can be empty):"
read -r github_team_name
echo "You have entered the following values:"
echo " github organization (This can be empty): $github_org_name"
echo " github user: $github_user"
if [ "" == "$github_team_name" ]; then
echo " No github team entered (This can be empty)!"
else
echo " github team: $github_team_name"
fi
echo " "
echo "Are these correct?"
read -r correct_entries
if [[ "y" == "$correct_entries" || "Y" == "$correct_entries" || "yes" == "$correct_entries" || "YES" == "$correct_entries" || "Yes" == "$correct_entries" ]]; then
touch "$file_name_for_config"
echo "$github_org_name|$github_user|$github_team_name">"$file_name_for_config"
else
throw_error "Aborting saving github organization and username."
fi
}
function pull_repos {
fetch_repos "y"
}
function fetch_repos {
count=0
pull_current_branch="$1"
if [ -f "$file_name_for_repos" ]; then
while read -r repo; do
skip_repo_processing='false'
message="Repo: $repo"
repo_cloned="false"
for directory in *
do
if [[ -d $directory ]]; then
if [[ "$directory" == "$repo" ]]; then
if [ -f "$file_name_for_skip_repos" ]; then
while read -r skip_repo; do
if [ "$repo" == "$skip_repo" ]; then
skip_repo_processing='true'
fi
done <"$file_name_for_skip_repos"
fi
count=$((count+1))
if [ "true" == "$skip_repo_processing" ]; then
echo "***** - $message - $count Skipping processing!"
else
message="$message - $count - Found - fetching from git"
if [[ "y" == "$pull_current_branch" ]]; then
message="$message - pulling latest into current branch."
fi
pushd . > /dev/null 2>&1
cd "$repo" > /dev/null 2>&1
echo "$message"
git fetch --all
if [[ "y" == "$pull_current_branch" ]]; then
git pull
fi
repo_cloned="true"
popd > /dev/null 2>&1
fi
repo_cloned="true"
popd > /dev/null 2>&1
fi
fi
done
if [[ "false" == "$skip_repo_processing" ]]; then
if [[ "false" == "$repo_cloned" ]] ; then
echo "$message not found, cloning! - $count"
git_user_token=
if [ -f "$file_name_for_git_token" ]; then
git_user_token=":$(cat $file_name_for_git_token)"
git_user_string="$github_user$git_user_token"
git_clone_string="https://$git_user_string@${github_repo_base#https://}$github_org_name/$repo.git"
git clone "$git_clone_string"
else
git clone "$github_repo_base$github_org_name/$repo.git"
fi
fi
fi
echo " "
done <"$file_name_for_repos"
else
echo "No repos file list found to fetch! Use gitdir with the -u flag to update local cache file."
fi
}
function check_for_uncommitted {
no_local_changes_found="true"
while read -r repo; do
for directory in *
do
if [[ -d $directory ]]; then
if [[ "$directory" == "$repo" ]]; then
pushd . > /dev/null 2>&1
cd "$repo" > /dev/null 2>&1
status="$(git status)"
if [[ "$status" == *"Changes not staged for commit"* || "$status" == *"Untracked files"* ]]; then
echo " "
echo " "
echo "--------------------------------------"
echo "$repo : Local changes found!"
echo "--------------------------------------"
echo "$status"
no_local_changes_found="false"
fi
popd > /dev/null 2>&1
fi
fi
done
done <"$file_name_for_repos"
if [ "true" == "$no_local_changes_found" ]; then
echo " "
echo "No changes found!"
echo " "
fi
}
function show_repo_list {
cat "$file_name_for_repos"
}
function show_repos_branch {
while read -r repo; do
for directory in *
do
if [[ -d $directory ]]; then
if [[ "$directory" == "$repo" ]]; then
pushd . > /dev/null 2>&1
cd "$repo" > /dev/null 2>&1
# echo "=============="
branch_name=$(git branch | grep '*')
leadspace=' '
padspace=' '
printf '%s%15.25s%s%s\n' "$leadspace" "$repo$padspace" "$leadspace" "${branch_name#*[*]}"
popd > /dev/null 2>&1
fi
fi
done
done <"$file_name_for_repos"
}
# TODO: Figure out how to do this
# function list_review_requests {
# echo "---- List open review requests ----"
# echo " "
# # echo "Implement me!!!"
#
# # URL="https://github.com/pulls/review-requested"
# # URL="https://github.com/pulls?q=is%3Aopen+is%3Apr+review-requested%3Ascsarver+archived%3Afalse+user%3Aopploans"
# # curl -L --user "$github_user" "$URL"
#
# #https://developer.github.com/v3/search/
# #https://github.com/pulls/review-requested
#
# # curl --user "$github_user" "$github_api_base/orgs/$github_org_name/repos?per_page=100" | jq -r ".[].name" >"$file_name_for_repos"
# # curl -v -H "Authorization: token TOKEN" https://api.github.com/search/issues?q=is:open+is:pr+review-requested:"$github_user"
# # curl -v --user "$github_user" "$github_api_base/orgs/$github_org_name/search/issues?q=is:open+is:pr+review-requested:$github_user"
#
# #is:open is:pr review-requested:scsarver archived:false
# # curl -L -v --user "$github_user" "https://github.com/pulls/review-requested"
#
#
# # SEARCH_PATH="/search/pulls"
# # SEARCH_STRING="q=is:open+is:pr+review-requested:$github_user+archived:false+org:$github_org_name"
# # echo " "
# # echo "Requesting: ${github_api_base}${SEARCH_PATH}?${SEARCH_STRING}"
#
# # curl -v --user "$github_user" -H "application/vnd.github.symmetra-preview+json" "$github_api_base/orgs/$github_org_name/search/pulls?q=is:open+is:pr+review-requested:$github_user"
# # curl --user "$github_user" -H "application/vnd.github.symmetra-preview+json" "$github_api_base/search/pulls?q=is:open+is:pr+review-requested:$github_user"
# # curl --user "$github_user" "${github_api_base}/search/pulls?q=is:open+is:pr+review-requested:$github_user"
#
# # curl --user "$github_user" "${github_api_base}${SEARCH_PATH}?${SEARCH_STRING}"
# # | jq -r '.items[] | .url'
# }
#Start processing by checking for command switches if there are none print usage and exit.
if [[ "" == "$*" ]]; then
usage
exit 1
fi
for arg in "$@"
do
if [[ "$arg" == "$cmd_switch_check_uncommitted" ]]; then
echo "---- Checking repos for uncommitted files ----"
check_for_uncommitted
elif [[ "$arg" == "$cmd_switch_delete_config" ]]; then
echo "---- Deleting gitdir config! ---- "
delete_git_config
elif [[ "$arg" == "$cmd_switch_git_fetch" ]]; then
echo "---- Executing gitdir fetch! ---- "
load_git_config
fetch_repos "n"
elif [[ "$arg" == "$cmd_switch_show_repo_list" ]]; then
echo "---- Show gitdir repo list! ---- "
show_repo_list
elif [[ "$arg" == "$cmd_switch_show_repo_branch" ]]; then
echo "---- Show gitdir repos branch! ---- "
show_repos_branch
elif [[ "$arg" == "$cmd_switch_git_pull" ]]; then
echo "---- Executing gitdir pull! ---- "
load_git_config
pull_repos
elif [[ "$arg" == "$cmd_switch_set_config_for_current_repos" ]]; then
echo "---- Executing gitdir set the gitdir config for the currently cloned repos(do not pull all from org) ---- "
load_git_config
populate_cloned_repo_list
elif [[ "$arg" == "$cmd_switch_update_repo_list" ]]; then
echo "---- Executing gitdir update repo list! ---- "
load_git_config
populate_repo_list
# elif [[ "$arg" == "$cmd_switch_display_review_requests" ]]; then
# echo "---- Executing gitdir list review requests! ---- "
# load_git_config
# list_review_requests
else
usage
throw_error "Unexpected parameter found:" "$arg"
fi
done
echo " "
echo "_______________________________"
echo "Completed: $(date +%Y.%m.%d-%H:%M:%S)"
echo " "