-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreposync.sh
130 lines (110 loc) · 3.4 KB
/
reposync.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
#!/usr/bin/env bash
#########################################################################
## Title: reposync.sh
## Description: Syncs multiple git repositories in ~/tools/ based on the
## ~/.repolist file. Put each repo URL in there.
## Author: Andrew Lamarra
## Created: 01/23/2019
## Dependencies: bash (v4.0+), git
#########################################################################
# Setup
base=~/tools
list=.repolist
to_sync=~/$list
cd ~
urls=()
# Get the list of Git repos in list.txt
readarray -t urls < $to_sync
# Get the list of directories in ~/tools/
dirs=( $(find $base -maxdepth 1 -mindepth 1 -type d) )
untracked=("${dirs[@]}")
function dir_not_found {
repo_url="$1"
echo "The following repository was found in your list but not in the $base/ directory:"
echo -e "\t$repo_url"
# Ask the user about cloning the repo (and repeat if input is invalid)
ans=''
while [[ $ans != 'y' ]] && [[ $ans != 'n' ]]; do
read -rp "Would you like to clone it? (Y/n) " ans
if [[ $ans == '' ]]; then ans="y"; fi
ans=$(echo "$ans" | tr '[:upper:]' '[:lower:]')
done
# Clone the repo if the user answers 'yes'
if [[ $ans == 'y' ]]; then
cd "$base"
git clone "$repo_url"
return
fi
# Ask the user if they'd like to remove that repo from the list
ans=''
while [[ $ans != 'y' ]] && [[ $ans != 'n' ]]; do
read -rp "Would you like to remove this repo from your $list file? (Y/n) " ans
if [[ $ans == '' ]]; then ans="y"; fi
ans=$(echo "$ans" | tr '[:upper:]' '[:lower:]')
done
# If 'yes' then delete the line from the list of repos
if [[ $ans == 'y' ]]; then
user=$(echo "$repo_url" | awk -F/ '{print $(NF-1)}' | cut -d: -f2)
repo=$(echo "$repo_url" | awk -F/ '{print $NF}')
sed -i "/$user\/$repo/d" $to_sync
fi
}
function repo_not_found {
repo_url="$1"
local_dir="$2"
echo "The following repository was found in $local_dir/ but not your list:"
echo -e "\t$repo_url"
# Ask the user about adding it to the list
ans=''
while [[ $ans != 'y' ]] && [[ $ans != 'n' ]]; do
read -rp "Would you like to add it to your $list file? (Y/n) " ans
if [[ $ans == '' ]]; then ans="y"; fi
ans=$(echo "$ans" | tr '[:upper:]' '[:lower:]')
done
if [[ $ans == 'y' ]]; then
echo "$repo_url" >> "$to_sync"
return
fi
# Ask the user if they'd like to remove that repo from the list
ans=''
while [[ $ans != 'y' ]] && [[ $ans != 'n' ]]; do
read -rp "Would you like to remove the repository ($local_dir)? (Y/n) " ans
if [[ $ans == '' ]]; then ans="y"; fi
ans=$(echo "$ans" | tr '[:upper:]' '[:lower:]')
done
# If 'yes' then delete the directory
if [[ $ans == 'y' ]]; then
rm -rf "$local_dir"
fi
}
# Iterate over the repos found in the list
for url in "${urls[@]}"; do
counter=0
for dir in "${dirs[@]}"; do
# Save the current number of elements in the dirs array
num_dirs="${#dirs[@]}"
cd "$dir"
if [[ $url == $(git config --get remote.origin.url) ]]; then
echo "Updating $(echo "$url" | awk -F/ '{print $NF}' | cut -d. -f1)..."
git pull
echo
# Remove the found directory
unset "untracked[$counter]"
break
fi
counter=$((counter + 1))
done
if [[ $counter -ge $num_dirs ]]; then
dir_not_found "$url"
echo
fi
done
# Iterate through leftover dirs to see if any are repos not in the list
for dir in "${untracked[@]}"; do
cd "$dir"
url=$(git config --get remote.origin.url)
if [[ $? == 0 ]]; then
repo_not_found "$url" "$dir"
echo
fi
done