diff --git a/scripts/code_style.sh b/scripts/code_style.sh index 1f03e74..db8bd74 100755 --- a/scripts/code_style.sh +++ b/scripts/code_style.sh @@ -4,5 +4,38 @@ REPOSITORY_PATH="$(dirname "$SCRIPT_DIR")" set -e -cpplint ${REPOSITORY_PATH}/Src/modules/*/*pp \ - ${REPOSITORY_PATH}/Src/peripheral/*/*pp Src/platform/*/*pp \ No newline at end of file +QUIET=false +if [[ "$1" == "--quiet" ]]; then + QUIET=true +fi + +SOURCE_PATTERNS=( + "${REPOSITORY_PATH}/Src/modules/*/*.cpp" + "${REPOSITORY_PATH}/Src/peripheral/*/*.cpp" + "${REPOSITORY_PATH}/Src/platform/*/*.cpp" +) + +# Expand wildcards to get the actual list of files +# because cpplint does not understand shell wildcards (*/*pp) directly +SOURCE_FILES=() +for pattern in "${SOURCE_PATTERNS[@]}"; do + for file in $pattern; do + if [[ -f "$file" ]]; then + SOURCE_FILES+=("$file") + fi + done +done + +if [[ ${#SOURCE_FILES[@]} -eq 0 ]]; then + echo "No files found to lint." + exit 0 +fi + +if $QUIET; then + cpplint "${SOURCE_FILES[@]}" > /dev/null || { + echo "Code style check failed!" + exit 1 + } +else + cpplint "${SOURCE_FILES[@]}" +fi diff --git a/scripts/pre_commit.sh b/scripts/pre_commit.sh new file mode 100755 index 0000000..ad72790 --- /dev/null +++ b/scripts/pre_commit.sh @@ -0,0 +1,6 @@ +#!/bin/bash +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +set -e + +${SCRIPT_DIR}/code_style.sh --quiet diff --git a/scripts/prebuild_check.py b/scripts/prebuild_check.py index 2618648..715e09a 100755 --- a/scripts/prebuild_check.py +++ b/scripts/prebuild_check.py @@ -31,6 +31,10 @@ def is_dir_nonempty(directory) -> bool: def is_first_build() -> bool: return not is_dir_nonempty(BUILD_DIR) +def is_precommit_hook_configured(repo_path: str = REPO_DIR) -> bool: + pre_commit_hook_path = os.path.join(repo_path, '.git', 'hooks', 'pre-commit') + return os.path.isfile(pre_commit_hook_path) and os.access(pre_commit_hook_path, os.X_OK) + def get_all_branch_tags() -> str: cmd = ["git", "tag", "--merged"] tags = subprocess.check_output(cmd, stderr=subprocess.DEVNULL, text=True).strip() @@ -118,6 +122,44 @@ def check_submodules(self) -> None: except KeyboardInterrupt: sys.exit(1) + def check_pre_commit_hook(self) -> None: + """ + Check if the pre-commit hook is configured. If not, prompt the user to create it. + """ + + if is_precommit_hook_configured(): + logger.info("The pre-commit hook is already configured.") + return + + logger.info("The pre-commit hook is not configured.") + if not self.prompt: + return + + hook_path = os.path.join('.git', 'hooks', 'pre-commit') + hook_script = ( + "#!/bin/bash\n" + "CRNT_DIR=$( cd -- \"$( dirname -- \"${BASH_SOURCE[0]}\" )\" &> /dev/null && pwd )\n" + "GIT_DIR=\"$(dirname \"$CRNT_DIR\")\"\n" + "REPO_DIR=\"$(dirname \"$GIT_DIR\")\"\n" + "${REPO_DIR}/scripts/pre_commit.sh\n" + ) + try: + user_input = input("Do you want to create a pre-commit hook? [y/N]:\n").strip().lower() + if user_input != 'y': + logger.info("Skipped pre-commit hook setup.") + return + + # Write the hook script + with open(hook_path, 'w', encoding="utf-8") as hook_file: + hook_file.write(hook_script) + + # Make the hook executable + os.chmod(hook_path, 0o755) + logger.info("Pre-commit hook created successfully at '%s'.", hook_path) + + except KeyboardInterrupt: + sys.exit(1) + if __name__ == "__main__": logging.basicConfig(level=logging.ERROR) logger.setLevel(logging.DEBUG) @@ -131,3 +173,4 @@ def check_submodules(self) -> None: checker.check_branch_tags() checker.check_python_requirements() checker.check_submodules() + checker.check_pre_commit_hook()