@@ -8,12 +8,15 @@ DEFAULT_LANG="en"
8
8
DEFAULT_TARGET=" $DEFAULT_CONTENT "
9
9
EXIT_STATUS=0
10
10
EXTRA_DIFF_ARGS=" --numstat"
11
- FLAG_DIFF_DETAILS=" "
11
+ FLAG_CHANGE_OR_ADD=0
12
+ FLAG_DIFF_DETAILS=0
13
+ FLAG_DRIFTED_STATUS=0
12
14
FLAG_FAIL_ON_LIST_OR_MISSING=0
13
15
FLAG_INFO=" "
14
16
FLAG_QUIET=" "
15
17
FLAG_VERBOSE=" "
16
18
I18N_DLC_KEY=" default_lang_commit"
19
+ I18N_DLD_KEY=" drifted_from_default" # true, false, file not found
17
20
LIST_KIND=" DRIFTED" # or "ALL" or "NEW"
18
21
TARGET_PATHS=" "
19
22
@@ -38,22 +41,24 @@ Options:
38
41
39
42
-a List/process all localization page files accessible through target paths.
40
43
41
- -c HASH Update or add the '$I18N_DLC_KEY ' key value to HASH for all selected
44
+ -c HASH Change or add the '$I18N_DLC_KEY ' key value to HASH for all selected
42
45
localization pages. Use 'HEAD' as a shorthand for the hash of 'main'
43
- at HEAD (read locally).
46
+ at HEAD (read locally). Also sets '$I18N_DLD_KEY ' to true for files
47
+ that have drifted.
44
48
45
49
TIP: first fetch and pull the upstream 'main' if you want to use the
46
50
remote HEAD hash.
47
51
48
52
-d Output diff details.
53
+ -D Update or add the '$I18N_DLD_KEY ' key to all target localization pages.
49
54
-h Help! Output this usage info.
50
55
51
56
-i Print commit hashes of the local 'main' branch that might be useful to
52
- se as an argument to -c.
57
+ use as an argument to -c.
53
58
54
59
-n List/process only new localization pages, those without a '$I18N_DLC_KEY ' key.
55
- -q Quiet mode. Do not list processed files.
56
- -v Enables verbose command progress and status output .
60
+ -q Quiet mode. Do not list processed files. Prints summary unless -x is set.
61
+ -v Verbose mode. List all processed files and their status .
57
62
-x Return non-zero exit code if files were listed or hashes are missing.
58
63
EOS
59
64
}
@@ -65,15 +70,18 @@ function usage() {
65
70
}
66
71
67
72
function process_CLI_args() {
68
- while getopts " :ac:dhinqvx " opt; do
73
+ while getopts " :ac:dDhinqvx " opt; do
69
74
case $opt in
70
75
a)
71
76
LIST_KIND=" ALL" ;;
72
77
c)
78
+ FLAG_CHANGE_OR_ADD=1;
73
79
COMMIT_HASH_ARG=" $OPTARG " ;;
74
80
d)
75
81
FLAG_DIFF_DETAILS=1
76
82
EXTRA_DIFF_ARGS=" " ;;
83
+ D)
84
+ FLAG_DRIFTED_STATUS=1;;
77
85
h)
78
86
usage;;
79
87
i)
@@ -98,16 +106,21 @@ function process_CLI_args() {
98
106
if [[ -n $COMMIT_HASH_ARG ]]; then
99
107
COMMIT_HASH_ARG=$( echo $COMMIT_HASH_ARG | tr ' [:upper:]' ' [:lower:]' )
100
108
validate_hash $COMMIT_HASH_ARG
109
+ fi
101
110
102
- if [[ -n $FLAG_DIFF_DETAILS ]]; then
103
- echo -e " ERROR: you can't use -c and -d at the same time, specify one or the other.\n"
104
- usage 1
105
- fi
111
+ if (( FLAG_CHANGE_OR_ADD + FLAG_DIFF_DETAILS + FLAG_DRIFTED_STATUS > 1 )) ; then
112
+ echo -e " ERROR: you can't use -c, -d, and -D at the same time; choose one. For help use -h.\n"
113
+ exit 1
106
114
fi
107
115
108
- if [[ -n $FLAG_QUIET && -n $FLAG_DIFF_DETAILS ]]; then
109
- echo -e " ERROR: you can't use -d and -q at the same time, specify one or the other.\n"
110
- usage 1
116
+ if [[ -n $FLAG_QUIET && $FLAG_DIFF_DETAILS != 0 ]]; then
117
+ echo -e " ERROR: use -d or -q not both. For help use -h.\n"
118
+ exit 1
119
+ fi
120
+
121
+ if [[ -n $FLAG_QUIET && ($LIST_KIND == " ALL" || -n $FLAG_VERBOSE ) ]]; then
122
+ echo -e " ERROR: -q flag ignored when -a or -v is used. For help use -h.\n"
123
+ exit 1
111
124
fi
112
125
113
126
if [[ $LIST_KIND == " ALL" && -n $COMMIT_HASH_ARG ]]; then
@@ -209,14 +222,41 @@ function update_file_i18n_hash() {
209
222
# echo "WARNING: the given hash is not on 'main', using this instead: $HASH" >&2
210
223
# fi
211
224
212
- if ! (git branch --contains $HASH | grep -q " ^\s*main\b " ); then
213
- echo " ERROR: hash is empty or isn't on ' main' , aborting: $HASH - $f " >&2
225
+ if ! (git branch --contains $HASH | grep -qEe " ^\S?\ s*main$ " ); then
226
+ echo " ERROR: hash isn't on the default branch ( main) , aborting: $HASH - $f " >&2
214
227
exit 1
215
228
fi
216
229
217
230
set_file_i18n_hash " $f " " $HASH " " $msg " $pre_msg $post_msg
218
231
}
219
232
233
+ function set_file_drifted_status() {
234
+ if [[ $FLAG_DRIFTED_STATUS == 0 ]]; then return ; fi
235
+
236
+ local f=" $1 "
237
+ local status=" $2 "
238
+ local pre_msg=" ${3:- \t } "
239
+ local post_msg=" ${4:- $I18N_DLD_KEY key} " # Not used atm
240
+
241
+ if [[ $status == " false" ]]; then
242
+ perl -i -pe " s/(^$I18N_DLD_KEY ):.*$//g" " $f "
243
+ elif grep -q " ^$I18N_DLD_KEY :" " $f " ; then
244
+ perl -i -pe " s/(^$I18N_DLD_KEY ):.*$/\$ 1: $status /" " $f "
245
+ post_msg=" $post_msg UPDATED"
246
+ elif ! grep -q " ^$I18N_DLC_KEY :" " $f " ; then
247
+ echo " ERROR: $I18N_DLC_KEY key is missing. Cannot set $I18N_DLC_KEY in $f " >&2
248
+ exit 1
249
+ else
250
+ # Add drifted status immediately after the i18n hash
251
+ perl -i -0777 -pe " s/($I18N_DLC_KEY :.*?\n)/\$ 1$I18N_DLD_KEY : $status \n/sm" " $f "
252
+ # perl -i -0777 -pe "s/^(---.*?)(\n---\n)/\$1\n$I18N_DLD_KEY: $status\$2/sm" "$f"
253
+ post_msg=" $post_msg ADDED"
254
+ fi
255
+ if [[ -n $FLAG_VERBOSE ]]; then
256
+ echo -e " $pre_msg \t$f $I18N_DLD_KEY key set to $status "
257
+ fi
258
+ }
259
+
220
260
function main() {
221
261
process_CLI_args " $@ "
222
262
@@ -276,29 +316,33 @@ function main() {
276
316
EN_VERSION=$( echo " $f " | sed " s/$DEFAULT_CONTENT \/.\{2,5\}\//$DEFAULT_CONTENT \/$DEFAULT_LANG \//g" )
277
317
if [[ ! -e " $EN_VERSION " ]]; then
278
318
(( FILE_PROCESSED_COUNT++ ))
279
- echo -e " File not found\t$EN_VERSION - $f - $DEFAULT_LANG was removed or renamed"
319
+ if [[ -z $FLAG_QUIET ]]; then
320
+ echo -e " File not found:\t$EN_VERSION - $f - $DEFAULT_LANG was removed or renamed"
321
+ fi
322
+ set_file_drifted_status " $f " " file not found"
280
323
continue
281
324
fi
282
325
283
326
# Check default-language version for changes
284
327
DIFF=$( git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT ...HEAD " $EN_VERSION " 2>&1 )
285
328
DIFF_STATUS=$?
329
+ DRIFTED_STATUS=" false"
286
330
if [ $DIFF_STATUS -gt 1 ]; then
287
331
(( FILE_PROCESSED_COUNT++ ))
288
332
EXIT_STATUS=$DIFF_STATUS
289
- # if [[ -z $FLAG_QUIET ]]; then
290
- echo -e " HASH\tERROR\t$f : git diff error ($DIFF_STATUS ) or invalid hash $LASTCOMMIT . For details, use -v."
291
- # fi
333
+ echo -e " HASH\tERROR\t$f : git diff error ($DIFF_STATUS ) or invalid hash $LASTCOMMIT . For details, use -v."
292
334
if [[ -n $FLAG_VERBOSE ]]; then echo " $DIFF " ; fi
293
335
continue
294
336
elif [[ -n " $DIFF " ]]; then
295
337
(( FILE_PROCESSED_COUNT++ ))
296
- if [[ -n $FLAG_DIFF_DETAILS ]]; then
297
- echo -n " $DIFF "
338
+ DRIFTED_STATUS=" true"
339
+ if [[ $FLAG_DIFF_DETAILS != 0 ]]; then
340
+ echo " $DIFF "
298
341
elif [[ -n $COMMIT_HASH_ARG ]]; then
299
342
update_file_i18n_hash " $f " " $COMMIT_HASH_ARG " " $DIFF "
300
343
elif [[ -z $FLAG_QUIET ]]; then
301
- echo " $DIFF - $f "
344
+ echo -n " > Drifted file: $f "
345
+ if [[ -n $FLAG_VERBOSE ]]; then echo " ; diff summary: $DIFF " ; else echo ; fi
302
346
fi
303
347
elif [[ -z $LASTCOMMIT ]]; then
304
348
(( FILE_PROCESSED_COUNT++ ))
@@ -312,9 +356,10 @@ function main() {
312
356
(( FILE_PROCESSED_COUNT++ ))
313
357
echo -e " File is in sync\t$f - $LASTCOMMIT "
314
358
fi
359
+ set_file_drifted_status " $f " $DRIFTED_STATUS
315
360
done
316
361
317
- if [[ -z $FLAG_QUIET ]]; then
362
+ if [[ -z $FLAG_QUIET || $FLAG_FAIL_ON_LIST_OR_MISSING == 0 ]]; then
318
363
echo " $LIST_KIND files: $FILE_PROCESSED_COUNT out of $FILE_COUNT "
319
364
fi
320
365
0 commit comments