-
Notifications
You must be signed in to change notification settings - Fork 114
/
diff_version.sh
executable file
·291 lines (259 loc) · 12.7 KB
/
diff_version.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
#!/bin/bash
#------------------------------------------------------------------------------------------#
# This is how it works: you give two directories with EDBRAMS. It will go through #
# all files and check whether the files exists on both locations. #
# 1. If they do and are exactly the same, nothing happens; #
# 2. If they do and they are different, the editor will open three files, two of them are #
# the source code at the locations, and a third with the result of "diff -ib". Look #
# at the file, when you are done, close all the three files so it continues. #
# 3. If the file exists in only one location, a message will appear on screen. #
#------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------#
# Settings. #
#------------------------------------------------------------------------------------------#
#------ System. ---------------------------------------------------------------------------#
myos=$(uname -s)
#------ How detailed should the check be (partial - c, f90, and F90; total - everything. --#
comptype="partial"
#------ Sub-directories to be compared. ---------------------------------------------------#
subdirs="ED BRAMS Ramspost R-utils"
#------ Editor to use (I've only tested with nedit, use others at your own risk). ---------#
editor="nedit"
#------ Two paths with EDBRAMS (full path). -----------------------------------------------#
ours="${HOME}/Downloads/MLN-EDBRAMS"
theirs="${HOME}/Downloads/MLO-EDBRAMS"
ournew=false
#------ Working path when implementing a partial merge (otherwise, leave it blank). -------#
work="${HOME}/Models/EDBRAMS"
#------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------#
# Extensions. Decide how much to check. #
#------------------------------------------------------------------------------------------#
case "${comptype}" in
partial) exts=".c .F90 .f90" ;;
*) exts=".c .F90 .f90 .r .txt" ;;
esac
#------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------#
# Remove any comparison file that may exist. #
#------------------------------------------------------------------------------------------#
/bin/rm -f notthesame.* \~notthesame.*
#------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------#
# Add settings for partial merge (if sought). #
#------------------------------------------------------------------------------------------#
if [[ "${work}" == "" ]]
then
#---- Disable work version (preferred method, much less messy). ------------------------#
load_work=false
work=${theirs}
#---------------------------------------------------------------------------------------#
else
#---- Enable work version (more prone to complications, be careful). -------------------#
load_work=true
#---------------------------------------------------------------------------------------#
fi
#------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------#
# Loop over the sub-directories. #
#------------------------------------------------------------------------------------------#
for subdir in ${subdirs}
do
#---------------------------------------------------------------------------------------#
# Loop over the extensions. #
#---------------------------------------------------------------------------------------#
for ext in ${exts}
do
if [[ ${subdir} == "R-utils" ]] && [[ ${ext} == ".txt" ]]
then
srcours="${ours}/${subdir}/samap"
srctheirs="${theirs}/${subdir}/samap"
elif [[ ${subdir} == "R-utils" ]]
then
srcours="${ours}/${subdir}"
srctheirs="${theirs}/${subdir}"
else
srcours="${ours}/${subdir}/src"
srctheirs="${theirs}/${subdir}/src"
fi
#-------------------------------------------------------------------------------------#
#-------------------------------------------------------------------------------------#
# Look for files that were changed or were removed in the remote test. #
#-------------------------------------------------------------------------------------#
lookuptable=$(find ${srcours} -name "*${ext}")
for fileours in ${lookuptable}
do
file=$(basename ${fileours})
theirpath=$(dirname ${fileours} | sed s@${ours}@${theirs}@g)
workpath=$(dirname ${fileours} | sed s@${ours}@${work}@g)
case "${myos}" in
Darwin|Cygwin)
#---- Case-insensitive file system. ---------------------------------------------#
filetheirs="${theirpath}/${file}"
filework="${workpath}/${file}"
theirs_alt=""
;;
#--------------------------------------------------------------------------------#
*)
#----- Probably Linux or Unix, assume case-sensitive file system. ---------------#
case "${ext}" in
".f90")
alt_file=$(echo ${file} | sed s@"\\.f90"@".F90"@g)
if [[ -s "${theirpath}/${alt_file}" ]]
then
filetheirs="${theirpath}/${alt_file}"
theirs_alt=".F90"
else
filetheirs="${theirpath}/${file}"
theirs_alt=""
fi
;;
".F90")
alt_file=$(echo ${file} | sed s@"\\.F90"@".f90"@g)
if [[ -s "${theirpath}/${alt_file}" ]]
then
filetheirs="${theirpath}/${alt_file}"
theirs_alt=".f90"
else
filetheirs="${theirpath}/${file}"
theirs_alt=""
fi
;;
*)
filetheirs="${theirpath}/${file}"
theirs_alt=""
;;
esac
#--------------------------------------------------------------------------------#
#----- Probably Linux or Unix, assume case-sensitive file system. ---------------#
case "${ext}" in
".f90")
alt_file=$(echo ${file} | sed s@"\\.f90"@".F90"@g)
if [[ -s "${workpath}/${alt_file}" ]]
then
filework="${workpath}/${alt_file}"
work_alt=".F90"
else
filework="${workpath}/${file}"
work_alt=""
fi
;;
".F90")
alt_file=$(echo ${file} | sed s@"\\.F90"@".f90"@g)
if [[ -s "${workpath}/${alt_file}" ]]
then
filework="${workpath}/${alt_file}"
work_alt=".f90"
else
filework="${workpath}/${file}"
work_alt=""
fi
;;
*)
filework="${workpath}/${file}"
work_alt=""
;;
esac
#--------------------------------------------------------------------------------#
;;
esac
#-----------------------------------------------------------------------------------#
# If we are doing partial merge, we must check the existence three-way, #
# otherwise we compare just the "ours" and "theirs". #
#-----------------------------------------------------------------------------------#
if [[ -s ${filetheirs} ]]
then
#---- Check for extension change. -----------------------------------------------#
woroot=$(echo ${fileours} | sed s@"${srcours}/"@""@g)
case "${theirs_alt}" in
.f90|.F90)
if ${ournew}
then
ext_mess="Old extension was ${theirs_alt}."
else
ext_mess="New extension is ${theirs_alt}."
fi
;;
*)
ext_mess=""
;;
esac
#--------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------#
# Check for changes (ignoring spaces and commented lines). #
#--------------------------------------------------------------------------------#
ldif=$(diff -ibB <(grep -vE "^\s*!" ${fileours}) <(grep -vE "^\s*!" ${filetheirs}) | wc -l)
if [[ ${ldif} -gt 0 ]]
then
change=true
if [[ "${ext_mess}" == "" ]]
then
echo "File ${woroot}. Code has changed."
else
echo "File ${woroot}. Code has changed. ${ext_mess}"
fi
elif [[ "${ext_mess}" == "" ]]
then
change=false
else
change=false
echo "File ${woroot}. ${ext_mess}"
fi
#--------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------#
# If files have changes, report differences. #
#--------------------------------------------------------------------------------#
if ${change}
then
#----- Write differences to a temporary file. --------------------------------#
if ${ournew}
then
diff -uibB <(grep -vE "^\s*!" ${filetheirs}) \
<(grep -vE "^\s*!" ${fileours}) > notthesame${ext}
else
diff -uibB <(grep -vE "^\s*!" ${fileours}) \
<(grep -vE "^\s*!" ${filetheirs}) > notthesame${ext}
fi
#-----------------------------------------------------------------------------#
#----- Open editor with all files needed. ------------------------------------#
if ${load_work} && [[ -s ${filework} ]]
then
${editor} ${filework} ${filetheirs} notthesame${ext} \
1> /dev/null 2> /dev/null
/bin/rm -f notthesame${ext}
elif ${load_work}
then
${editor} ${filetheirs} notthesame${ext} 1> /dev/null 2> /dev/null
echo " ---> Working file is missing!"
else
${editor} ${fileours} ${filetheirs} notthesame${ext} \
1> /dev/null 2> /dev/null
fi
#-----------------------------------------------------------------------------#
fi
else
woroot=$(echo ${fileours} | sed s@"${srcours}/"@""@g)
echo "${woroot} is exclusive to ${ours} version."
fi #if [[ -s ${filetheirs} ]]
done #for fileours in ${lookuptable}
#-------------------------------------------------------------------------------------#
#-------------------------------------------------------------------------------------#
# Look for files that were changed or were removed in the remote test. #
#-------------------------------------------------------------------------------------#
lookuptable=$(find ${srctheirs} -name "*${ext}")
for filetheirs in ${lookuptable}
do
file=$(basename ${filetheirs})
ourpath=$(dirname ${filetheirs} | sed s@${theirs}@${ours}@g)
fileours="${ourpath}/${file}"
if [[ ! -s ${fileours} ]]
then
woroot=$(echo ${filetheirs} | sed s@"${srctheirs}/"@""@g)
echo "${woroot} is exclusive to ${theirs} version."
fi #if [[ -s ${filetheirs} ]]
done #for fileours in ${lookuptable}
#-------------------------------------------------------------------------------------#
done #for ext in ${exts}
#---------------------------------------------------------------------------------------#
done #for subdir in ${subdirs}
#------------------------------------------------------------------------------------------#