-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch
293 lines (252 loc) · 7.49 KB
/
patch
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
#!/usr/bin/env bash
currentdir=$(pwd)
repolink=""
branchName=""
repofolder=""
patchAuthor="Silverwolfg11 <[email protected]>"
mergetool=""
# Text file that stores the latest commit of the upstream project
upstreamFile="$currentdir/current-upstream.txt"
function cloneProj {
echo "Cloning git repo into specified directory..."
if [ -z "$branchName" ]; then
git clone $repolink .
else
git clone $repolink --branch $branchName --single-branch .
fi
echo "Git project cloned succesfully!"
}
function reAuthor {
if [ ! -z "$patchAuthor" ]; then
for file in "$currentdir"/Patches/*
do
sed -i '2d' "$file"
sed -i "2i From: $patchAuthor" "$file"
done
echo "Patches have been re-authored!"
fi
}
# Checks whether the repo folder exists, and exits if it does not
function validateRepoFolder {
if [ ! -d "$repofolder" ]; then
echo "Repo folder doesn't exist!"
exit 1
fi
}
function resetToUpstream {
# Check if upstream file exists
if [ ! -f "$upstreamFile" ]; then
echo "Could not find last upstream commit!"
exit 1
fi
# Read latest commit to latest commit variable
read -r recentCommit < "$upstreamFile"
cd "$currentdir/$repofolder"
# Reset to latest commit
echo "Resetting head to $recentCommit"
git reset --hard $recentCommit
}
function getPatchFromNumb {
patchNumber=$1
patchNumber=$((patchNumber + 0))
filesLength=$(ls Patches/ -1 | wc -l)
if [ $filesLength == '0' ]; then
echo "No patches to test!"
exit 1
fi
if [ $filesLength -lt $patchNumber ]; then
echo "Patch number too large!"
exit 1
fi
patchNumber=$2
patchLength=${#patchNumber}
while [ $patchLength -lt 4 ];
do
patchNumber="0${patchNumber}"
patchLength=${#patchNumber}
done
for file in "$currentdir"/Patches/*
do
basename=$(basename "$file")
tempFile=${basename:0:4}
if [ $tempFile == $patchNumber ]; then
echo "Applying patch..."
cd "$currentdir"
cd $repofolder
return $file
fi
done
}
if [ $# -gt 0 ]; then
case $1 in
"download" | "d" )
# Create repo folder if it doesn't exists
if [ ! -d $repofolder ]; then
mkdir $repofolder
fi
# Clone project into repo folder
cd $repofolder
if [ ! -d .git ]; then
cloneProj
# Check if the upstream file exists
if [ ! -f "$upstreamFile" ]; then
# If it doesn't store latest commit to upstream file
git rev-parse HEAD > "$upstreamFile";
else
# If it does, then reset to recorded branch
echo "Resetting git repo to last stored commit..."
resetToUpstream
fi
fi
;;
"rebuild" | "rb" )
validateRepoFolder
cd $repofolder;
echo "Determining # patches that are not applied upstream..."
# Git Cherry allows us to view the patches that haven't been applied to upstream
git cherry > "$currentdir/commits.txt"
# Num will serve as our patch counter to increment patch numbers
num=1
# Read the commits.txt file
input="$currentdir/commits.txt"
echo "Rebuilding patches..."
while IFS= read -r line
do
patchhash="${line:2}"
echo "git format-patch -l $patchhash"
git format-patch -1 $patchhash -o "$currentdir/Patches" --start-number $num
num=$((num+1))
done < "$input"
rm -f "$currentdir/commits.txt"
echo "Patches rebuilt succesfully!"
cd -
reAuthor
;;
"apply" | "a")
validateRepoFolder
cd $repofolder;
resetToUpstream
echo "Applying patches to project..."
git am --ignore-whitespace "$currentdir"/Patches/*.patch
echo "Patches applied succesfully!"
exit 1
;;
"test" | "t")
if [ $# -lt 2 ]; then
echo "Please attach a patch number!"
exit 1
fi
file='getPatchFromNumb $2'
git am --ignore-whitespace "$file"
;;
"unapply" | "ua")
if [ $# -lt 2 ]; then
echo "Please attach a patch number!"
exit 1
fi
file='getPatchFromNumb $2'
git apply -R "$file"
echo "Reverted patch!"
;;
"pull")
validateRepoFolder
resetToUpstream
cd "$currentdir/$repofolder"
git pull
echo "Storing latest commit in $upstreamFile ..."
# Get the latest upstream commit and store it in a file
git rev-parse HEAD > "$upstreamFile"
echo "Patches have been un-applied. Make sure to re-apply them!"
;;
"reset" )
validateRepoFolder
resetToUpstream
;;
"reauthor" | "ra" )
reAuthor
exit 1
;;
"edit" | "e" )
if [ $# -gt 1 ] && [ $2 == 'continue' ]; then
if (! test -d "$repofolder/.git/rebase-merge" && ! test -d "$repofolder/.git/rebase-apply") ; then
echo "No Rebase in progress!"
exit 1
fi
cd $repofolder
git add .
git commit --amend
git rebase --continue
cd -
exit 1
fi
if (test -d "$repofolder/.git/rebase-merge" || test -d "$repofolder/.git/rebase-apply") ; then
echo "You are already editing a patch!"
exit 1
fi
filesLength=$(ls Patches/ -1 | wc -l)
if [ $filesLength == '0' ]; then
echo "No patches to edit!"
exit 1
fi
cd $repofolder
git rebase -i HEAD~$filesLength
;;
"generate" | "gen")
if [ $# -lt 2 ]; then
echo "Please enter a valid patch message!"
exit 1
fi
patchmessage=''
for ((n=2;n<=$#;n++)); do
patchmessage="$patchmessage ${!n}"
done
length=${#patchmessage}
patchmessage=${patchmessage:1:length}
cd $repofolder
git add .
git commit -m "$patchmessage"
exit 1
;;
"build" | "b")
validateRepoFolder
cd $repofolder
mvn clean package
;;
"resolve" | "res")
validateRepoFolder
cd $repofolder
git am -3
if [ -z "$mergetool" ]; then
git mergetool -t $mergetool
else
git merge
fi
git am --continue
echo "Resolved conflict. Make sure to rebuild patches!"
;;
"update" | "u")
if [ -f "getpatch" ]; then
echo "Removing existing patch bootstrap!"
rm "getpatch"
fi
echo "Downloading latest patch bootstrap!"
curl "https://raw.githubusercontent.com/silverwolfg11/PatchStrap/master/getpatch" >> getpatch
echo "Running patchstrap!"
./getpatch -rl=$repolink -rf=$repofolder -bn=$branchName -mt=$mergetool -pa=${patchAuthor//[[:blank:]]/}
exit 1
;;
"deletebootstrap" )
if [ -f "getpatch" ]; then
echo "Removing patch bootstrap!"
rm "getpatch"
fi
exit 1
;;
*)
echo "Invalid argument! Please enter one of the following arguments: download, pull, apply, rebuild, reset, reauthor, edit, generate"
exit 1
;;
esac
else
echo "Please attach one of the following arguments: download, pull, apply, rebuild, reset, reauthor, edit, generate"
fi