Skip to content

Commit 3038001

Browse files
Enhance restyle-diff.sh with commit and push options (project-chip#71805)
* Enhance restyle-diff.sh to add -c and -u flags * Address bot review comments regarding variable expansion and initialization * Restyled by shellharden * Add help flag and usage text to restyle-diff.sh * Restyled by shfmt
1 parent 3f9cd16 commit 3038001

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

scripts/helpers/restyle-diff.sh

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,25 @@
2121
# you've written is kosher to CI
2222
#
2323
# Usage:
24-
# restyle-diff.sh [-d] [-p] [ref]
24+
# restyle-diff.sh [-d] [-p] [-c] [-u] [-h] [ref]
2525
#
2626
# if unspecified, ref defaults to upstream/master (or master)
2727
# -d enables debug logging for Restyle CLI
2828
# -p pull restyler Docker images before running (default: skip pull, reuse cached images)
29+
# -c explicitly pass --commit to restyle CLI to generate commits per tool
30+
# -u automatically push resulting commits using 'git push'
31+
# -h show this help message
32+
33+
show_help() {
34+
echo "Usage: restyle-diff.sh [-d] [-p] [-c] [-u] [-h] [ref]"
35+
echo
36+
echo " [ref] Base reference for restyle (defaults to upstream/master or master)"
37+
echo " -d Enable debug logging for Restyle CLI"
38+
echo " -p Pull restyler Docker images before running"
39+
echo " -c Explicitly pass --commit to restyle CLI to generate commits per tool"
40+
echo " -u Automatically push resulting commits using 'git push'"
41+
echo " -h Show this help message"
42+
}
2943
#
3044

3145
here=${0%/*}
@@ -45,7 +59,7 @@ restyle-paths() {
4559
else
4660
echo "[restyle-diff.sh] Please wait, Restyling files (using cached images; pass -p to pull updates)"
4761
fi
48-
restyle --config-file=.restyled.yaml "$@"
62+
restyle --config-file=.restyled.yaml "${COMMIT_FLAG:+"$COMMIT_FLAG"}" "$@"
4963

5064
# warn if restyle left any files owned by root (which means older restyle-CLI is being used)
5165
root_owned=$(find "$@" -maxdepth 0 -user 0 2>/dev/null || true)
@@ -125,6 +139,8 @@ export -f restyle-paths
125139
# config bumps a tag. When that happens, Restyle CLI's `docker run --pull never` fails with a "No such image" error;
126140
# if you see that error, re-run this script with -p to fetch the new image.
127141
export PULL=False
142+
export PUSH=False
143+
export COMMIT_FLAG=""
128144

129145
while [[ $# -gt 0 ]]; do
130146
case "$1" in
@@ -136,6 +152,18 @@ while [[ $# -gt 0 ]]; do
136152
export PULL=True
137153
shift
138154
;;
155+
-c)
156+
export COMMIT_FLAG="--commit"
157+
shift
158+
;;
159+
-u)
160+
export PUSH=True
161+
shift
162+
;;
163+
-h | --help)
164+
show_help
165+
exit 0
166+
;;
139167
*)
140168
ref="$1"
141169
shift
@@ -153,3 +181,8 @@ paths=$(git diff --ignore-submodules --name-only --merge-base "$ref")
153181
ensure_restyle_installed
154182

155183
echo "$paths" | xargs -n "$MAX_ARGS" "$BASH" -c 'restyle-paths "$@"' -
184+
185+
if [[ "$PUSH" == "True" ]]; then
186+
echo "[restyle-diff.sh] Pushing changes..."
187+
git push
188+
fi

0 commit comments

Comments
 (0)