@@ -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,18 +41,20 @@ 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
60
-q Quiet mode. Do not list processed files.
@@ -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,16 @@ 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
111
119
fi
112
120
113
121
if [[ $LIST_KIND == " ALL" && -n $COMMIT_HASH_ARG ]]; then
@@ -209,14 +217,41 @@ function update_file_i18n_hash() {
209
217
# echo "WARNING: the given hash is not on 'main', using this instead: $HASH" >&2
210
218
# fi
211
219
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
220
+ if ! (git branch --contains $HASH | grep -qEe " ^\S?\ s*main$ " ); then
221
+ echo " ERROR: hash isn't on the default branch ( main) , aborting: $HASH - $f " >&2
214
222
exit 1
215
223
fi
216
224
217
225
set_file_i18n_hash " $f " " $HASH " " $msg " $pre_msg $post_msg
218
226
}
219
227
228
+ function set_file_drifted_status() {
229
+ if [[ $FLAG_DRIFTED_STATUS == 0 ]]; then return ; fi
230
+
231
+ local f=" $1 "
232
+ local status=" $2 "
233
+ local pre_msg=" ${3:- \t } "
234
+ local post_msg=" ${4:- $I18N_DLD_KEY key} " # Not used atm
235
+
236
+ if [[ $status == " false" ]]; then
237
+ perl -i -pe " s/(^$I18N_DLD_KEY ):.*$//g" " $f "
238
+ elif grep -q " ^$I18N_DLD_KEY :" " $f " ; then
239
+ perl -i -pe " s/(^$I18N_DLD_KEY ):.*$/\$ 1: $status /" " $f "
240
+ post_msg=" $post_msg UPDATED"
241
+ elif ! grep -q " ^$I18N_DLC_KEY :" " $f " ; then
242
+ echo " ERROR: $I18N_DLC_KEY key is missing. Cannot set $I18N_DLC_KEY in $f " >&2
243
+ exit 1
244
+ else
245
+ # Add drifted status immediately after the i18n hash
246
+ perl -i -0777 -pe " s/($I18N_DLC_KEY :.*?\n)/\$ 1$I18N_DLD_KEY : $status \n/sm" " $f "
247
+ # perl -i -0777 -pe "s/^(---.*?)(\n---\n)/\$1\n$I18N_DLD_KEY: $status\$2/sm" "$f"
248
+ post_msg=" $post_msg ADDED"
249
+ fi
250
+ if [[ -n $FLAG_VERBOSE ]]; then
251
+ echo -e " $pre_msg \t$f $I18N_DLD_KEY key set to $status "
252
+ fi
253
+ }
254
+
220
255
function main() {
221
256
process_CLI_args " $@ "
222
257
@@ -277,12 +312,14 @@ function main() {
277
312
if [[ ! -e " $EN_VERSION " ]]; then
278
313
(( FILE_PROCESSED_COUNT++ ))
279
314
echo -e " File not found\t$EN_VERSION - $f - $DEFAULT_LANG was removed or renamed"
315
+ set_file_drifted_status " $f " " file not found"
280
316
continue
281
317
fi
282
318
283
319
# Check default-language version for changes
284
320
DIFF=$( git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT ...HEAD " $EN_VERSION " 2>&1 )
285
321
DIFF_STATUS=$?
322
+ DRIFTED_STATUS=" false"
286
323
if [ $DIFF_STATUS -gt 1 ]; then
287
324
(( FILE_PROCESSED_COUNT++ ))
288
325
EXIT_STATUS=$DIFF_STATUS
@@ -293,8 +330,9 @@ function main() {
293
330
continue
294
331
elif [[ -n " $DIFF " ]]; then
295
332
(( FILE_PROCESSED_COUNT++ ))
296
- if [[ -n $FLAG_DIFF_DETAILS ]]; then
297
- echo -n " $DIFF "
333
+ DRIFTED_STATUS=" true"
334
+ if [[ $FLAG_DIFF_DETAILS != 0 ]]; then
335
+ echo " $DIFF "
298
336
elif [[ -n $COMMIT_HASH_ARG ]]; then
299
337
update_file_i18n_hash " $f " " $COMMIT_HASH_ARG " " $DIFF "
300
338
elif [[ -z $FLAG_QUIET ]]; then
@@ -312,6 +350,7 @@ function main() {
312
350
(( FILE_PROCESSED_COUNT++ ))
313
351
echo -e " File is in sync\t$f - $LASTCOMMIT "
314
352
fi
353
+ set_file_drifted_status " $f " $DRIFTED_STATUS
315
354
done
316
355
317
356
if [[ -z $FLAG_QUIET ]]; then
0 commit comments