|
| 1 | +#!/bin/bash |
| 2 | +# Check the changed code against the clang-format code style for the repository. |
| 3 | +# Uses a copy of https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/git-clang-format |
| 4 | +# located in the scripts directory to check the formatting of the diff against main. |
| 5 | +# The script will exit with a 1 if a diff is detected and a 0 if it isn't. |
| 6 | +# If a diff is detected, you should run the script to fix the issues locally and commit again. |
| 7 | + |
| 8 | +# Disable exit on error so we can handle return codes manually |
| 9 | +set +e |
| 10 | + |
| 11 | +# === Argument Parsing === |
| 12 | + |
| 13 | +FIX=0 |
| 14 | +BASE_REF="main" |
| 15 | +OPEN_SIM_BASE="." |
| 16 | +TOOLS_DIR="./scripts/tools" |
| 17 | +LOG_DIR="./logs" |
| 18 | +POSITIONAL=() |
| 19 | + |
| 20 | +# Parse optional --fix flag |
| 21 | +while [[ $# -gt 0 ]]; do |
| 22 | + case "$1" in |
| 23 | + -f|--fix) |
| 24 | + FIX=1 |
| 25 | + shift |
| 26 | + ;; |
| 27 | + -b|--branch) |
| 28 | + BASE_REF="$2" |
| 29 | + shift 2 |
| 30 | + ;; |
| 31 | + -h|--help) |
| 32 | + echo "Usage: $0 [OPTIONS] [<opensim-base-dir>] [<tools-dir>] |
| 33 | +
|
| 34 | +Options: |
| 35 | + -f, --fix Automatically apply formatting fixes if issues are found. |
| 36 | + -b, --branch <name> Set the git base branch to diff against. Default: main |
| 37 | + -h, --help Show this help message. |
| 38 | +
|
| 39 | +Positional Arguments: |
| 40 | + <opensim-base-dir> Path to the OpenSim base directory. Default: current directory |
| 41 | + <tools-dir> Path to the tools directory containing git-clang-format.py. Default: ./scripts/tools |
| 42 | +
|
| 43 | +Examples: |
| 44 | + $0 |
| 45 | + $0 -b develop |
| 46 | + $0 --fix $OPEN_SIM_BASE $TOOLS_DIR |
| 47 | +" |
| 48 | + exit 0 |
| 49 | + ;; |
| 50 | + -*) |
| 51 | + echo "Unknown option: $1" |
| 52 | + exit 1 |
| 53 | + ;; |
| 54 | + *) |
| 55 | + POSITIONAL+=("$1") |
| 56 | + shift |
| 57 | + ;; |
| 58 | + esac |
| 59 | +done |
| 60 | + |
| 61 | +# Restore positional arguments |
| 62 | +set -- "${POSITIONAL[@]}" |
| 63 | + |
| 64 | +# Assign positional arguments with defaults |
| 65 | +if [ ${#POSITIONAL[@]} -gt 0 ]; then |
| 66 | + OPEN_SIM_BASE="${POSITIONAL[0]}" |
| 67 | +fi |
| 68 | + |
| 69 | +if [ ${#POSITIONAL[@]} -gt 1 ]; then |
| 70 | + TOOLS_DIR="${POSITIONAL[1]}" |
| 71 | +fi |
| 72 | + |
| 73 | +OPEN_SIM_BASE=$(realpath "$OPEN_SIM_BASE") |
| 74 | +TOOLS_DIR=$(realpath "$TOOLS_DIR") |
| 75 | + |
| 76 | +# Independent log directory (relative to current working directory) |
| 77 | +LOG_FILE="$LOG_DIR/clang-format.log" |
| 78 | +GIT_CLANG_FORMAT="$TOOLS_DIR/git-clang-format.py" |
| 79 | + |
| 80 | +# Ensure git-clang-format.py exists |
| 81 | +if [ ! -f "$GIT_CLANG_FORMAT" ]; then |
| 82 | + echo "Error: git-clang-format.py not found at $GIT_CLANG_FORMAT" |
| 83 | + exit 1 |
| 84 | +fi |
| 85 | + |
| 86 | +# Create log directory if it doesn't exist |
| 87 | +mkdir -p "$LOG_DIR" |
| 88 | + |
| 89 | +# Clear or create the log file |
| 90 | +: > "$LOG_FILE" |
| 91 | + |
| 92 | +# Save current directory |
| 93 | +ORIG_DIR=$(pwd) |
| 94 | + |
| 95 | +# Change to OpenSim base directory |
| 96 | +cd "$OPEN_SIM_BASE" || { |
| 97 | + echo "Error: Failed to cd into $OPEN_SIM_BASE" |
| 98 | + exit 1 |
| 99 | +} |
| 100 | + |
| 101 | +# Run git-clang-format with logging (from OpenSim base dir) |
| 102 | +"$GIT_CLANG_FORMAT" "$BASE_REF" --diff | tee -a "$ORIG_DIR/$LOG_FILE" |
| 103 | +rc1=${PIPESTATUS[0]} |
| 104 | + |
| 105 | +"$GIT_CLANG_FORMAT" "$BASE_REF" --diffstat | tee -a "$ORIG_DIR/$LOG_FILE" |
| 106 | +rc2=${PIPESTATUS[0]} |
| 107 | + |
| 108 | +# Combine exit codes |
| 109 | +FORMAT_ERROR=$(( rc1 || rc2 )) |
| 110 | + |
| 111 | +# If --fix is specified and formatting issues exist, apply the fix |
| 112 | +if [ "$FORMAT_ERROR" -ne 0 ] && [ "$FIX" -eq 1 ]; then |
| 113 | + echo | tee -a "$ORIG_DIR/$LOG_FILE" |
| 114 | + echo "Applying automatic formatting fixes..." | tee -a "$ORIG_DIR/$LOG_FILE" |
| 115 | + "$GIT_CLANG_FORMAT" "$BASE_REF" | tee -a "$ORIG_DIR/$LOG_FILE" |
| 116 | + echo "Fixes applied. Please review and re-commit your changes." | tee -a "$ORIG_DIR/$LOG_FILE" |
| 117 | +fi |
| 118 | + |
| 119 | + |
| 120 | +# Return to original directory |
| 121 | +cd "$ORIG_DIR" |
| 122 | + |
| 123 | +# Final message |
| 124 | +if [ "$FORMAT_ERROR" -ne 0 ]; then |
| 125 | + if [ "$FIX" -eq 1 ]; then |
| 126 | + echo "Fix mode enabled. Formatting was corrected." | tee -a "$LOG_FILE" |
| 127 | + FORMAT_ERROR=0 |
| 128 | + else |
| 129 | + echo | tee -a "$LOG_FILE" |
| 130 | + echo "Formatting issues found! Run the following command to fix them:" | tee -a "$LOG_FILE" |
| 131 | + echo | tee -a "$LOG_FILE" |
| 132 | + echo " $0 --fix --branch \"$BASE_REF\" \"$OPEN_SIM_BASE\" \"$TOOLS_DIR\"" | tee -a "$LOG_FILE" |
| 133 | + echo | tee -a "$LOG_FILE" |
| 134 | + fi |
| 135 | +else |
| 136 | + echo "No formatting issues found." | tee -a "$LOG_FILE" |
| 137 | +fi |
| 138 | + |
| 139 | +exit $FORMAT_ERROR |
0 commit comments