Skip to content

Commit

Permalink
add --quiet option to code sctyle checker and add pre-commit hook wit…
Browse files Browse the repository at this point in the history
…h code style
  • Loading branch information
PonomarevDA committed Jan 20, 2025
1 parent 7fdb6d6 commit 123d115
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
37 changes: 35 additions & 2 deletions scripts/code_style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
6 changes: 6 additions & 0 deletions scripts/pre_commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

set -e

${SCRIPT_DIR}/code_style.sh --quiet
43 changes: 43 additions & 0 deletions scripts/prebuild_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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()

0 comments on commit 123d115

Please sign in to comment.