Skip to content

Commit 82b4adc

Browse files
committed
Add shell script formatter
Leverage shfmt as the standard formatter for shell scripts in this repo. - add `.editorconfig` with formatting rules and apply shfmt in .ci/check-format.sh. The formatting rules are as follows: * Use spaces for indentation * Indent with 4 spaces * Use Unix-style line endings (LF) * Remove trailing whitespace at the end of lines * Ensure the file ends with a newline * Place the opening brace of a function on the next line * Indent case statements within switch blocks * Add spaces around redirection operators (e.g., >, >>) * Place binary operators (e.g., &&, |) on the next line when breaking lines - update CONTRIBUTING.md with usage instructions (all contributors should use it to ensure consistent shell script style). The early exit behavior (set -e) in .ci/check-format.sh has been removed to allow collecting all formatting mismatches in a single run. The script’s exit code is now the sum of the line-level mismatch count from clang-format-18 and the file-level mismatch count from shfmt, which should be 0 if all files are properly formatted.
1 parent e32a84a commit 82b4adc

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

.ci/check-format.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
#!/usr/bin/env bash
22

3-
set -e -u -o pipefail
3+
# The -e is not set because we want to get all the mismatch format at once
44

5-
SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(c|cxx|cpp|h|hpp)\$")
5+
set -u -o pipefail
66

77
set -x
88

9-
for file in ${SOURCES};
10-
do
11-
clang-format-18 ${file} > expected-format
9+
REPO_ROOT="$(git rev-parse --show-toplevel)"
10+
11+
C_SOURCES=$(find "${REPO_ROOT}" | egrep "\.(c|cxx|cpp|h|hpp)$")
12+
for file in ${C_SOURCES}; do
13+
clang-format-18 ${file} >expected-format
1214
diff -u -p --label="${file}" --label="expected coding style" ${file} expected-format
1315
done
14-
exit $(clang-format-18 --output-replacements-xml ${SOURCES} | egrep -c "</replacement>")
16+
C_MISMATCH_LINE_CNT=$(clang-format-18 --output-replacements-xml ${C_SOURCES} | egrep -c "</replacement>")
17+
18+
SH_SOURCES=$(find "${REPO_ROOT}" | egrep "\.sh$")
19+
for file in ${SH_SOURCES}; do
20+
shfmt -d "${file}"
21+
done
22+
SH_MISMATCH_FILE_CNT=$(shfmt -l ${SH_SOURCES})
23+
24+
exit $((C_MISMATCH_LINE_CNT + SH_MISMATCH_FILE_CNT))

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Top-level EditorConfig file
2+
root = true
3+
4+
# Shell script-specific settings
5+
[*.sh]
6+
indent_style = space
7+
indent_size = 4
8+
end_of_line = lf
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
function_next_line = true
12+
switch_case_indent = true
13+
space_redirects = true
14+
binary_next_line = true

CONTRIBUTING.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ However, participation requires adherence to fundamental ground rules:
4444
This variant should be considered the standard for all documentation efforts.
4545
For instance, opt for "initialize" over "initialise" and "color" rather than "colour".
4646

47-
Software requirement: [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18 or later.
47+
Software requirement:
48+
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18 or later.
49+
* [shfmt](https://github.com/mvdan/sh).
4850

49-
This repository consistently contains an up-to-date `.clang-format` file with rules that match the explained ones.
50-
For maintaining a uniform coding style, execute the command `clang-format -i *.{c,h}`.
51+
This repository consistently contains an up-to-date `.clang-format` file with rules that match the explained ones and uses shell script formatting supported by `shfmt`.
52+
For maintaining a uniform coding style, execute the command `clang-format -i *.{c,h}` and `shfmt -w $(find . -type f -name "*.sh")`.
5153

5254
## Coding Style for Modern C
5355

@@ -877,22 +879,22 @@ Author: Jim Huang <jserv@ccns.ncku.edu.tw>
877879
Date: Mon Feb 24 13:08:32 2025 +0800
878880
879881
Introduce CPU architecture filtering in scheduler
880-
882+
881883
In environments with mixed CPU architectures, it is crucial to ensure
882884
that an instance runs only on a host with a compatible CPU
883885
type—preventing, for example, a RISC-V instance from being scheduled on
884886
an Arm host.
885-
887+
886888
This new scheduler filter enforces that requirement by comparing an
887889
instance's architecture against the host's allowed architectures. For
888890
the libvirt driver, the host's guest capabilities are queried, and the
889891
permitted architectures are recorded in the permitted_instances_types
890892
list within the host's cpu_info dictionary.
891-
893+
892894
The filter systematically excludes hosts that do not support the
893895
instance's CPU architecture. Additionally, RISC-V has been added to the
894896
set of acceptable architectures for scheduling.
895-
897+
896898
Note that the CPU architecture filter is disabled by default.
897899
```
898900

0 commit comments

Comments
 (0)