-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpatch-proc.sh
executable file
·231 lines (179 loc) · 4.97 KB
/
patch-proc.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
#!/usr/bin/env bash
###############################################################################
NORM=0
BOLD=1
UNLN=4
RED=31
GREEN=32
YELLOW=33
BLUE=34
MAG=35
CYAN=36
GREY=37
DARK=90
CL_NORM="\e[${NORM}m"
CL_BOLD="\e[${BOLD}m"
CL_UNLN="\e[${UNLN}m"
CL_RED="\e[${RED}m"
CL_GREEN="\e[${GREEN}m"
CL_YELLOW="\e[${YELLOW}m"
CL_BLUE="\e[${BLUE}m"
CL_MAG="\e[${MAG}m"
CL_CYAN="\e[${CYAN}m"
CL_GREY="\e[${GREY}m"
CL_DARK="\e[${DARK}m"
###############################################################################
main() {
if [[ $# -ne 4 ]] ; then
usage
exit 0
fi
check "$@"
local choice
show "Copy unchanged files? (y/N):" $BOLD
read -p "> " choice
if [[ "$choice" == "Y" || "$choice" == "y" ]] ; then
copy "$@"
fi
}
check() {
checkArgs "$@"
local patch_file data_dir old_ver new_ver old_ver_dir new_ver_dir
local sources old_ver_hash new_ver_hash diff_size
patch_file="$1"
data_dir="$2"
old_ver="$3"
new_ver="$4"
old_ver_dir="${data_dir}/nginx-${old_ver}-orig"
new_ver_dir="${data_dir}/nginx-${new_ver}-orig"
if [[ ! -e "$old_ver_dir" ]] ; then
show "Directory $old_ver_dir doesn't exist" $RED
exit 1
fi
if [[ ! -e "$new_ver_dir" ]] ; then
show "Directory $new_ver_dir doesn't exist" $RED
exit 1
fi
sources=$(grep '+++' "$patch_file" | tr "\t" " " | cut -f2 -d" " | cut -f2-99 -d "/")
show ""
for source_file in $sources ; do
old_ver_hash=$(getHash "$old_ver_dir/$source_file")
new_ver_hash=$(getHash "$new_ver_dir/$source_file")
if [[ "$old_ver_hash" == "$new_ver_hash" ]] ; then
show " ${CL_GREEN}✔ ${CL_NORM}$source_file"
else
diff_size=$(getDiffSize "$old_ver_dir/$source_file" "$new_ver_dir/$source_file")
show " ${CL_RED}✖ ${CL_NORM}${CL_BOLD}$source_file ${CL_DARK}(± $diff_size lines)${CL_NORM}"
showDiff "$old_ver_dir/$source_file" "$new_ver_dir/$source_file"
fi
done
show ""
}
copy() {
checkArgs "$@"
local patch_file data_dir old_ver new_ver
local old_ver_dir new_ver_dir old_ver_pt_dir new_ver_pt_dir
local sources old_ver_hash new_ver_hash
patch_file="$1"
data_dir="$2"
old_ver="$3"
new_ver="$4"
old_ver_dir="${data_dir}/nginx-${old_ver}-orig"
new_ver_dir="${data_dir}/nginx-${new_ver}-orig"
old_ver_pt_dir="${data_dir}/nginx-${old_ver}"
new_ver_pt_dir="${data_dir}/nginx-${new_ver}"
sources=$(grep '+++' "$patch_file" | tr "\t" " " | cut -f2 -d" " | cut -f2-99 -d "/")
show ""
for source_file in $sources ; do
old_ver_hash=$(getHash "$old_ver_dir/$source_file")
new_ver_hash=$(getHash "$new_ver_dir/$source_file")
if [[ "$old_ver_hash" == "$new_ver_hash" ]] ; then
show " $data_dir/${CL_BOLD}{$old_ver → $new_ver}${CL_NORM}/$source_file"
cp "$old_ver_pt_dir/$source_file" "$new_ver_pt_dir/$source_file"
fi
done
show ""
}
getDiffSize() {
local file1="$1"
local file2="$2"
local diff_size=$(diff -U 0 "$file1" "$file2" | wc -l)
diff_size=$(( diff_size - 3 ))
echo "$diff_size"
}
showDiff() {
local file1="$1"
local file2="$2"
show "$CL_GREY"
diff -U 0 "$file1" "$file2" | sed -n 3,9999p | sed 's/^/ /g' | sed 's/@@ //g' | sed 's/ @@//g'
show "$CL_NORM"
}
checkArgs() {
local patch_file data_dir old_ver new_ver
local old_ver_dir new_ver_dir old_ver_pt_dir new_ver_pt_dir
patch_file="$1"
data_dir="$2"
old_ver="$3"
new_ver="$4"
if ! head -5 "$patch_file" | grep -q -o '+++ nginx-1' ; then
error "File $patch_file is not a patch file"
exit 1
fi
if [[ $(echo "$old_ver" | tr -d '[:digit:].') != "" ]] ; then
error "$old_ver is not a valid Nginx version"
exit 1
fi
if [[ $(echo "$new_ver" | tr -d '[:digit:].') != "" ]] ; then
error "$new_ver is not a valid Nginx version"
exit 1
fi
old_ver_dir="${data_dir}/nginx-${old_ver}-orig"
new_ver_dir="${data_dir}/nginx-${new_ver}-orig"
old_ver_pt_dir="${data_dir}/nginx-${old_ver}"
new_ver_pt_dir="${data_dir}/nginx-${new_ver}"
if [[ ! -e "$data_dir" ]] ; then
error "Directory $data_dir doesn't exist"
exit 1
fi
if [[ ! -e "$old_ver_dir" ]] ; then
error "Directory $old_ver_dir doesn't exist"
exit 1
fi
if [[ ! -e "$new_ver_dir" ]] ; then
error "Directory $new_ver_dir doesn't exist"
exit 1
fi
if [[ ! -e "$old_ver_pt_dir" ]] ; then
error "Directory $old_ver_pt_dir doesn't exist"
exit 1
fi
if [[ ! -e "$new_ver_pt_dir" ]] ; then
error "Directory $new_ver_pt_dir doesn't exist"
exit 1
fi
}
getHash() {
sha256sum "$1" | cut -f1 -d" "
}
show() {
if [[ -n "$2" ]] ; then
echo -e "\e[${2}m${1}\e[0m"
else
echo -e "$*"
fi
}
error() {
show "$*" $RED 1>&2
}
usage() {
show ""
show "${CL_BOLD}Usage:${CL_NORM} ./patch-proc.sh patch data-dir prev-ver new-ver"
show ""
show "Examples" $BOLD
show ""
show " ./patch-proc.sh SOURCES/webkaos.patch /some/dir 1.17.5 1.17.6"
show " Check diff and copy unchanged files" $DARK
show ""
}
###############################################################################
main "$@"