]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
More robust script
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 26 Nov 2019 14:39:05 +0000 (15:39 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 26 Nov 2019 14:39:05 +0000 (15:39 +0100)
build-scripts/format-code

index 6a4d6d10f54489f26b222598c1dfeacbc0e0f5f8..e3398bd2ea2e97bcf33e3420ab04eb3c91fc25bd 100755 (executable)
@@ -2,28 +2,39 @@
 
 #
 # Reformat code, but do not touch if no changes.
-# Report if a files was changed.
 #
 
-if [ "$0" != "./build-scripts/format-code" ]; then
+if [ "$0" != "./build-scripts/format-code" -a "$0" != "build-scripts/format-code" ]; then
     echo "Please run me from the root checkout dir"
     exit 1
 fi
 
+if [ $# = 0 ]; then
+    echo usage: $0 file...
+    echo
+    echo format C++ files, does not touch non-regular files
+    exit 0
+fi
 if [ ! -e .clang-format ]; then
     echo "No .clang-format file found in .";
     exit 1
 fi
 
 for file in "${@}"; do
-    if ! clang-format -style=file "$file" > "$file.xxx"; then
-        rm "$file.xxx"
+    if [ -h "$file" -o ! -f "$file" ]; then
+        echo "$file: skipped, not a regular file or unreadable"
+        continue
+    fi
+    tmp=$(mktemp $file.XXXXXXXX)
+    if ! clang-format -style=file "$file" > "$tmp"; then
+        rm "$tmp"
     else
-        if ! cmp -s "$file" "$file.xxx"; then
-            echo "$file reformatted"
-            mv "$file.xxx" "$file"
+        if ! cmp -s "$file" "$tmp"; then
+            echo "$file: reformatted"
+            mv "$tmp" "$file"
         else
-            rm "$file.xxx"
+            echo "$file: already formatted to perfection"
+            rm "$tmp"
         fi
     fi
 done