4
4
5
5
script_dir=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " > /dev/null 2>&1 && pwd) "
6
6
source_dir=" $( dirname " $script_dir " ) "
7
+ since_rev=" "
7
8
8
9
check_only=0
9
10
no_version_check=0
@@ -30,6 +31,7 @@ Options:
30
31
--md Format only markdown files
31
32
--sh Format only shell script files
32
33
--cmake Format only CMake files
34
+ --since <rev> Only format files since Git revision <rev>
33
35
EOF
34
36
}
35
37
@@ -65,6 +67,10 @@ while [[ "$#" -gt 0 ]]; do
65
67
source_dir=" $2 "
66
68
shift
67
69
;;
70
+ --since)
71
+ since_rev=" $2 "
72
+ shift
73
+ ;;
68
74
* )
69
75
echo " unrecognized argument: $1 "
70
76
show_help
@@ -108,23 +114,31 @@ require_bin() {
108
114
}
109
115
110
116
require_bin " git" " 1.8"
111
- require_bin " gersemi" " 0.17"
112
- require_bin " xargs" " 3"
117
+ (( run_all || run_cmake )) && require_bin " gersemi" " 0.17"
118
+ (( run_all || run_cpp )) && require_bin " xargs" " 3"
113
119
require_bin " diff" " 2"
114
- require_bin " clang-format" " 17" " 18"
115
- require_bin " prettier" " 3"
116
- require_bin " shfmt" " 3"
120
+ (( run_all || run_cpp )) && require_bin " clang-format" " 17" " 18"
121
+ (( run_all || run_yaml || run_markdown )) && require_bin " prettier" " 3"
122
+ (( run_all || run_sh )) && require_bin " shfmt" " 3"
117
123
118
124
if [ " $missing_bin " ]; then
119
125
exit 1
120
126
fi
121
127
122
128
exit_code=0
123
129
130
+ function list_files() {
131
+ if [ " $since_rev " ]; then
132
+ git diff --name-only " $since_rev " HEAD $@
133
+ else
134
+ git ls-files $@
135
+ fi
136
+ }
137
+
124
138
cmake_formatting () {
125
139
echo " Formatting CMake files..." >&2
126
140
127
- readarray -t files < <( git ls-files ' *.cmake' ' CMakeLists.txt' ' **/CMakeLists.txt' )
141
+ readarray -t files < <( list_files ' *.cmake' ' CMakeLists.txt' ' **/CMakeLists.txt' )
128
142
129
143
common_args=(
130
144
# turn on warning when this is fixed https://github.com/BlankSpruce/gersemi/issues/39
@@ -150,7 +164,7 @@ track_progress() {
150
164
local total=$1
151
165
local current=0
152
166
153
- while IFS= read -r _; do
167
+ (( total )) && while IFS= read -r _; do
154
168
(( current++ )) || :
155
169
percent=$(( current * 100 / total))
156
170
printf ' \rProgress: [%-50s] %d%%' " $( printf ' #%.0s' $( seq 1 $(( percent / 2 )) ) ) " " $percent " >&2
@@ -161,7 +175,7 @@ track_progress() {
161
175
cpp_formatting () {
162
176
echo " Formatting cpp files..." >&2
163
177
164
- readarray -t files < <( git ls-files ' *.cpp' ' *.hpp' ' *.c' ' *.h' ' :!external/**' )
178
+ readarray -t files < <( list_files ' *.cpp' ' *.hpp' ' *.c' ' *.h' ' :!external/**' )
165
179
166
180
# The progress reporting is a bit sneaky, we use `--verbose` with xargs which
167
181
# prints a line to stderr for each command, and we simply count these...
@@ -211,23 +225,23 @@ prettier_formatting() {
211
225
yaml_json_formatting () {
212
226
echo " Formatting yaml and json files..." >&2
213
227
214
- readarray -t files < <( git ls-files " *.yaml" " *.yml" " *.json" ' :!external/**' )
228
+ readarray -t files < <( list_files " *.yaml" " *.yml" " *.json" ' :!external/**' )
215
229
216
230
prettier_formatting
217
231
}
218
232
219
233
markdown_formatting () {
220
234
echo " Formatting markdown files..." >&2
221
235
222
- readarray -t files < <( git ls-files " *.md" ' :!external/**' )
236
+ readarray -t files < <( list_files " *.md" ' :!external/**' )
223
237
224
238
prettier_formatting
225
239
}
226
240
227
241
sh_formatting () {
228
242
echo " Formatting sh files..." >&2
229
243
230
- readarray -t files < <( git ls-files " *.sh" )
244
+ readarray -t files < <( list_files " *.sh" )
231
245
232
246
common_args=(
233
247
# default 8 is way too wide
0 commit comments