-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateGitSubtree.sh
323 lines (288 loc) · 12.7 KB
/
updateGitSubtree.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
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
#!/bin/sh
# Written by Nicolas Strike, Aug 2018
##############################################################################################################################
# The purpose of this script is to update a git repo's external dependencies
# that are included as a subtree in the repo.
#
# Example use case:
#
# Folder in 3rd party/external repo: (paths are relative to the root of the repo)
# /src/folder/what-I-need
# /random-dir/another-folder/something-else-I-need
# Copy in my repo:
# /src/what-I-need
# /src/something-else-I-need
#
# These paths are just examples, you could map any level of dir's to any other
# set of dirs.
#
# How it works:
# It does this by cloning the external git repo, and locally creating a branch of the
# your repo. Using the subtree command, it then creates local branches
# that contain only the desired folder. Once these new branches
# are made, the script merges the branches with your repo's folders and pushes
# the updates.
# Notes:
# This script is best used when ran automatically and reglarly
# This script was modeled after the process detailed here: https://stackoverflow.com/a/22335955
###############################################################################################################################
srcbranch=master
destbranch=master
forceupdate=0
while [ "$1" != "" ]; do
case $1 in
-m | --my-repo ) shift
myrepo=$1
;;
-t | --their-repo ) shift
theirrepo=$1
;;
--source-branch ) shift
srcbranch=$1
;;
--dest-branch ) shift
destbranch=$1
;;
-f | --map-file ) shift
mapfile=$1
;;
# -f | --force-update ) forceupdate=1
# ;;
-h | --help ) usage
exit
;;
# * ) usage
# exit 1
esac
shift
done
declare -a srcArray
declare -a destArray
echo "myrepo: $myrepo theirrepo: $theirrepo branch:$branch mapfile:$mapfile"
#####################################################################################################
#This sends out an email notification to system admins if any errors were detected during runtime.
# The content of the email can be found in $emailMessage
#####################################################################################################
#sendEmailNotification(){
#
# if [ $errorsDetected != 0 ]
# then
# echo "Errors during run detected. Sending email notification."
# #Set the email
# echo -e "\n\n Thank you,\n\n -camClubbUpdate Nightly Script: " $(readlink --canonicalize --no-newline $BASH_SOURCE) >> $emailMessage
# mail -s " `date +%Y-%m-%d` camClubb autoupdate script `hostname` " $EMAIL < $emailMessage
# exit 1
# else
# echo "No errors detected. Run successful."
# fi
#}
#####################################################################################################
#Sets up common variables (excluding the emailMessage location),
# such as repo URLs and temporary directory locations.
#####################################################################################################
setup(){
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Setting up working environment"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
theirdir=theirDir
mydir=myDir
errorsDetected=0
rm -rf $theirdir
rm -rf $mydir
#echo -e "Hello,\nThis email is automatically generated by a nightly script in charge of updated CAM's Clubb subtrees." >> $emailMessage
echo "Done"
}
#####################################################################################################
# Read the map file provided by the user to dictate which folders on the
# dependancy repo to associate with which folders on their destination repo.
#####################################################################################################
readMapFile(){
counter=0
while IFS=, read src dest;do
srcArray[$counter]=$src
destArray[$counter]s=$dest
counter=$((counter+1))
done < $mapfile
}
#####################################################################################################
#Clones the github repos to a temporary folder. This is necessary for doing things using git.
#####################################################################################################
clone(){
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Cloning git repo"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
git clone $myrepo $mydir
cd $mydir
git fetch
git checkout $destbranch
#testing
git lfs install
git config lfs.cachecredentials 1
#git lfs track *.exe
#git add .gitattributes
#git commit .gitattributes -m "Added gitattributes"
#end testing
myErrorCode=$?
git config --global credential.helper store
git remote add theirrepo $theirrepo
theirErrorCode=$?
git fetch theirrepo
git branch their_branch theirrepo/$srcbranch
# Error handling, run 'exit 1' if clone() failed to complete
if [ $theirErrorCode != 0 ]
then
echo -e "\nI was unable to clone their ('$theirrepo') repo. Please make sure the URL has not changed, there is access to the internet, and that the git servers are operational."
fi
if [ $myErrorCode != 0 ]
then
echo -e "\nI was unable to fetch your repo ('$myrepo'). Please make sure the URL has not changed, there is access to the internet, and that the git servers are operational."
fi
if [ $theirErrorCode != 0 ] || [ $myErrorCode != 0 ] || [ $srcBranch ]
then
echo "Errors detected, exiting"
errorsDetected=1
exit 1
fi
echo "Done"
}
#####################################################################################################
#Creates a number of subtrees (of clubb) that consist of just 1 specific folder each.
#These are to be merged with the CAM to update it's dependancies.
#####################################################################################################
generateSubtrees(){
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Generating temporary subtrees"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
errorCodeSum=0
git checkout -f their_branch
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
for i in "${srcArray[@]}"
do
:
echo "Creating subtree from ${srcArray[@]}"
git subtree split --squash --prefix="${srcArray[@]}" -b branch$i -m "Autoupdated subtree"
# Add resulting error code to the error sum. If there is no error, this adds 0
errorCodeSum=$(($errorCodeSum + $?))
done
if [ $errorCodeSum != 0 ]
then
echo -e "\nThere was an issue generating the subtree's. I reccomend running the script manually to see what went wrong." >> $emailMessage
errorsDetected=$((errorsDetected + errorCodeSum))
exit 1
fi
echo "Done"
}
#####################################################################################################
#If new files have been added to clubb, add these files to CAM
#####################################################################################################
addUntrackedFiles(){
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Adding any untracked files"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
errorCodeSum=0
git checkout -f $destbranch
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
for i in "${destArray[@]}"
do
:
#There is no errorCode check here because if it tries to add a subtree that already exists it will return an error code. We dont' want this.
git subtree add --prefix="${destArray[@]}" branch$i -m "Added "${srcArray[@]}" as subtree in ${destArray[@]}"
done
if [ $errorCodeSum != 0 ]
then
echo -e "\nThere may have been an issue when attempting to add unrevisioned files to CAM"
errorsDetected=$((errorsDetected + errorCodeSum))
fi
echo "Done"
}
#####################################################################################################
#Merge any differences from the subtrees to CAM-CLUBBS copy of clubb files.
#If there are no differences, nothing will be updated (git does this automagically)
#####################################################################################################
merge(){
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Merging Changes"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
errorCodeSum=0
git checkout -f $destbranch
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
for i in "${destArray[@]}"
do
:
git subtree merge --prefix="${destArray[@]}" branch$i -m "Autoupdated ${destArray[@]}"
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
mergeFail=$?
if [ $errorCodeSum != 0 ]
then
echo "While attempting to auto merge, there were conflicts. Attempting to force an update now"
#if [ $mergeFail != 0 ]
#then
# git rm -r "${srcArray[@]}"
# fi
# git commit -a -m "resetting folder"
# addUntrackedFiles
# git commit -a -m "Auto updated, forced CAM update to Larson-Group/CLUBB version. If something is broke, check out the previous working version and compare the differences with this version."
# errorCodeSum=$? # Add resulting error code to the error sum. If there is no error, this adds 0
fi
git commit -a -m "Auto updated, forced update to Larson-Group/CLUBB version"
if [ $errorCodeSum != 0 ]
then
echo -e "\There were some issues when attempting to merge updates. This is unusual, and shouldn't happen."
errorsDetected=$((errorsDetected + errorCodeSum))
fi
done
echo "Done"
}
#####################################################################################################
#Pushes changes made to CAM-CLUBB up to GitHub. If no changes were made, nothing will get pushed.
#####################################################################################################
push(){
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Pushing Changes"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
errorCodeSum=0
git push -u $myrepo $destbranch
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
if [ $errorCodeSum != 0 ]
then
echo -e "\nGit ran into issues when attempting to push. You may run this script manually to help determine the cause."
errorsDetected=$(($errorsDetected + $errorCodeSum))
fi
echo "Done"
}
#####################################################################################################
#Deletes the temporary folders
#####################################################################################################
cleanup(){
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Cleaning up local directory"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
errorCodeSum=0
cd ..
rm -rf $their
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
rm -rf $mydir
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
rm -rf $srcbranch
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
rm -rf $destbranch
errorCodeSum=$(($errorCodeSum + $?)) # Add resulting error code to the error sum. If there is no error, this adds 0
if [ $errorCodeSum != 0 ]
then
echo -e "\nFailed to cleanup directory/remove temporary folders. Please ensure the script has the correct permissions, or delete these folders manually: \n\t$theirdir\n\t$mydir"
errorsDetected=$(($errorsDetected + $errorCodeSum))
fi
echo "Done"
}
#####################################################################################################
#Runs the script.
#####################################################################################################
setup
#clone
generateSubtrees
addUntrackedFiles
merge
push
#cleanup
#sendEmailNotification