Skip to content

Commit 123d115

Browse files
committed
add --quiet option to code sctyle checker and add pre-commit hook with code style
1 parent 7fdb6d6 commit 123d115

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

scripts/code_style.sh

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,38 @@ REPOSITORY_PATH="$(dirname "$SCRIPT_DIR")"
44

55
set -e
66

7-
cpplint ${REPOSITORY_PATH}/Src/modules/*/*pp \
8-
${REPOSITORY_PATH}/Src/peripheral/*/*pp Src/platform/*/*pp
7+
QUIET=false
8+
if [[ "$1" == "--quiet" ]]; then
9+
QUIET=true
10+
fi
11+
12+
SOURCE_PATTERNS=(
13+
"${REPOSITORY_PATH}/Src/modules/*/*.cpp"
14+
"${REPOSITORY_PATH}/Src/peripheral/*/*.cpp"
15+
"${REPOSITORY_PATH}/Src/platform/*/*.cpp"
16+
)
17+
18+
# Expand wildcards to get the actual list of files
19+
# because cpplint does not understand shell wildcards (*/*pp) directly
20+
SOURCE_FILES=()
21+
for pattern in "${SOURCE_PATTERNS[@]}"; do
22+
for file in $pattern; do
23+
if [[ -f "$file" ]]; then
24+
SOURCE_FILES+=("$file")
25+
fi
26+
done
27+
done
28+
29+
if [[ ${#SOURCE_FILES[@]} -eq 0 ]]; then
30+
echo "No files found to lint."
31+
exit 0
32+
fi
33+
34+
if $QUIET; then
35+
cpplint "${SOURCE_FILES[@]}" > /dev/null || {
36+
echo "Code style check failed!"
37+
exit 1
38+
}
39+
else
40+
cpplint "${SOURCE_FILES[@]}"
41+
fi

scripts/pre_commit.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
3+
4+
set -e
5+
6+
${SCRIPT_DIR}/code_style.sh --quiet

scripts/prebuild_check.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def is_dir_nonempty(directory) -> bool:
3131
def is_first_build() -> bool:
3232
return not is_dir_nonempty(BUILD_DIR)
3333

34+
def is_precommit_hook_configured(repo_path: str = REPO_DIR) -> bool:
35+
pre_commit_hook_path = os.path.join(repo_path, '.git', 'hooks', 'pre-commit')
36+
return os.path.isfile(pre_commit_hook_path) and os.access(pre_commit_hook_path, os.X_OK)
37+
3438
def get_all_branch_tags() -> str:
3539
cmd = ["git", "tag", "--merged"]
3640
tags = subprocess.check_output(cmd, stderr=subprocess.DEVNULL, text=True).strip()
@@ -118,6 +122,44 @@ def check_submodules(self) -> None:
118122
except KeyboardInterrupt:
119123
sys.exit(1)
120124

125+
def check_pre_commit_hook(self) -> None:
126+
"""
127+
Check if the pre-commit hook is configured. If not, prompt the user to create it.
128+
"""
129+
130+
if is_precommit_hook_configured():
131+
logger.info("The pre-commit hook is already configured.")
132+
return
133+
134+
logger.info("The pre-commit hook is not configured.")
135+
if not self.prompt:
136+
return
137+
138+
hook_path = os.path.join('.git', 'hooks', 'pre-commit')
139+
hook_script = (
140+
"#!/bin/bash\n"
141+
"CRNT_DIR=$( cd -- \"$( dirname -- \"${BASH_SOURCE[0]}\" )\" &> /dev/null && pwd )\n"
142+
"GIT_DIR=\"$(dirname \"$CRNT_DIR\")\"\n"
143+
"REPO_DIR=\"$(dirname \"$GIT_DIR\")\"\n"
144+
"${REPO_DIR}/scripts/pre_commit.sh\n"
145+
)
146+
try:
147+
user_input = input("Do you want to create a pre-commit hook? [y/N]:\n").strip().lower()
148+
if user_input != 'y':
149+
logger.info("Skipped pre-commit hook setup.")
150+
return
151+
152+
# Write the hook script
153+
with open(hook_path, 'w', encoding="utf-8") as hook_file:
154+
hook_file.write(hook_script)
155+
156+
# Make the hook executable
157+
os.chmod(hook_path, 0o755)
158+
logger.info("Pre-commit hook created successfully at '%s'.", hook_path)
159+
160+
except KeyboardInterrupt:
161+
sys.exit(1)
162+
121163
if __name__ == "__main__":
122164
logging.basicConfig(level=logging.ERROR)
123165
logger.setLevel(logging.DEBUG)
@@ -131,3 +173,4 @@ def check_submodules(self) -> None:
131173
checker.check_branch_tags()
132174
checker.check_python_requirements()
133175
checker.check_submodules()
176+
checker.check_pre_commit_hook()

0 commit comments

Comments
 (0)