2
2
#
3
3
# Inspired by https://github.com/kubernetes/website/blob/main/scripts/lsync.sh
4
4
5
+ DEFAULT_LANG=" en"
6
+ DEFAULT_TARGET=" content"
5
7
EXTRA_DIFF_ARGS=" --numstat"
8
+ FLAG_UPDATE=" "
9
+ FLAG_VERBOSE=" "
10
+ I18N_DLC_KEY=" default_lang_commit"
6
11
TARGET_PATHS=" "
7
12
8
13
function _usage() {
9
14
cat << EOS
10
- Usage: $( basename " $0 " ) [options] TARGET_PATH ...
15
+ Usage: $( basename " $0 " ) [options] [ TARGET_PATH ...]
11
16
12
17
For each localized page target, this script reports whether the English
13
18
language version of that page has changed since the localized file was
14
19
first written or last edited.
15
20
16
21
TARGET_PATH can be a single markdown file of a localized page, such as
17
22
'content/ja/_index.md', or a directory of localized pages, such as 'content/ja'.
23
+ The default TARGET_PATH is '$DEFAULT_TARGET '.
18
24
19
25
-h Output this usage info.
20
26
-d Output diff details.
21
- -v Verbose mode.
27
+ -u Update, or add, target commit hashes to match the last commit they were updated from.
28
+ -v Enables verbose command progress and status output.
22
29
EOS
23
30
}
24
31
@@ -29,16 +36,19 @@ function usage() {
29
36
}
30
37
31
38
function process_CLI_args() {
32
- while getopts " :hdv " opt; do
39
+ while getopts " :hduv " opt; do
33
40
case $opt in
34
41
h)
35
42
usage
36
43
;;
37
44
d)
38
45
EXTRA_DIFF_ARGS=" "
39
46
;;
47
+ u)
48
+ FLAG_UPDATE=1
49
+ ;;
40
50
v)
41
- VERBOSE =1
51
+ FLAG_VERBOSE =1
42
52
;;
43
53
\? )
44
54
echo " ERROR: unrecognized flag: -$OPTARG "
@@ -48,48 +58,79 @@ function process_CLI_args() {
48
58
done
49
59
50
60
shift $(( OPTIND- 1 ))
51
- if [ " $# " -lt 1 ]; then
52
- echo " ERROR: target path argument is missing" >&2
53
- usage 1
54
- fi
55
-
56
61
TARGET_PATHS=" $@ "
57
62
63
+ if [[ -z " $TARGET_PATHS " ]]; then
64
+ TARGET_PATHS=" $DEFAULT_TARGET "
65
+ if [[ -n $FLAG_VERBOSE ]]; then echo " INFO: using default target path: $TARGET_PATHS " ; fi
66
+ fi
67
+
58
68
if [[ -f " TARGET_PATHS" && ! -e " $TARGET_PATHS " ]] ; then
59
69
echo " Path not found: '$TARGET_PATHS '" >&2
60
70
exit 2
61
71
fi
62
72
}
63
73
74
+ function update_i18n_hash() {
75
+ # Usage: update_i18n_hash <file> <commit>
76
+ #
77
+ # Adds to or updates the file's front matter's field
78
+ # $I18N_DLC_KEY with value <commit>.
79
+
80
+ local LASTCOMMIT=" $1 "
81
+ local f=" $2 "
82
+
83
+ if grep -q " ^$I18N_DLC_KEY :" " $f " ; then
84
+ perl -i -pe " s/(^$I18N_DLC_KEY ):.*/\$ 1: $LASTCOMMIT /" " $f "
85
+ else
86
+ perl -i -0777 -pe " s/^(---.*?)(\n---\n)/\$ 1\n$I18N_DLC_KEY : $LASTCOMMIT \$ 2/sm" " $f "
87
+ fi
88
+ if [[ -n $FLAG_VERBOSE ]]; then
89
+ echo -e " i18n commit ID\t$f $LASTCOMMIT - updated"
90
+ fi
91
+ }
92
+
64
93
function main() {
65
94
process_CLI_args " $@ "
66
95
67
96
if [ -f " $TARGET_PATHS " ] ; then
68
97
TARGETS=" $TARGET_PATHS "
69
98
else
70
- TARGETS=$( find $TARGET_PATHS -name " *.md" )
99
+ TARGETS=$( find $TARGET_PATHS -name " *.md" -not -path " */ $DEFAULT_LANG /* " )
71
100
if [[ -z " $TARGETS " ]]; then
72
101
echo " ERROR: target directory contains no markdown files: '$TARGET_PATHS '" >&2
73
102
exit 1
74
103
fi
75
- # if [[ -n $VERBOSE ]]; then echo -e "All targets: $TARGETS"; fi
104
+ # if [[ -n $FLAG_VERBOSE ]]; then echo -e "All targets: $TARGETS"; fi
76
105
fi
77
106
78
107
SYNCED=1
79
108
for f in $TARGETS ; do
80
- # if [[ -n $VERBOSE ]]; then echo -e "Checking\t$f"; fi
109
+ # if [[ -n $FLAG_VERBOSE ]]; then echo -e "Checking\t$f"; fi
81
110
EN_VERSION=$( echo " $f " | sed " s/content\/.\{2,5\}\//content\/en\//g" )
111
+
112
+ # Try to get commit ref from file front matter
113
+ LASTCOMMIT=$( perl -ne " print \"\$ 1\" if /^$I18N_DLC_KEY :\\ s*(.*)/" " $f " )
114
+ if [[ -z $LASTCOMMIT ]]; then
115
+ # Get commit hash from git commit info
116
+ LASTCOMMIT=$( git log -n 1 --pretty=format:%h -- " $f " )
117
+ fi
118
+
119
+ if [[ -n $FLAG_UPDATE ]]; then
120
+ update_i18n_hash " $LASTCOMMIT " " $f "
121
+ fi
122
+
82
123
if [[ ! -e " $EN_VERSION " ]]; then
83
- echo " Base file renamed or removed: $EN_VERSION "
124
+ echo -e " File not found\t $EN_VERSION - $f - $DEFAULT_LANG was removed or renamed "
84
125
SYNCED=0
85
126
continue
86
127
fi
87
128
88
- LASTCOMMIT =$( git log -n 1 --pretty=format:%h -- " $f " )
89
- git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT ...HEAD " $EN_VERSION "
90
- if [ $? -ne 0 ] ; then
129
+ DIFF =$( git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT ...HEAD " $EN_VERSION " )
130
+ if [[ -n " $DIFF " ]] ; then # [[ $? -ne 0 ]]
131
+ echo " $DIFF - $f "
91
132
SYNCED=0
92
- elif [[ -n $VERBOSE ]]; then
133
+ elif [[ -n $FLAG_VERBOSE ]]; then
93
134
echo -e " File is in sync\t$f "
94
135
fi
95
136
done
0 commit comments