|
| 1 | +#!/usr/bin/zsh |
| 2 | +zmodload zsh/zutil |
| 3 | +defaultsrc="../" |
| 4 | +defaultrun="/usr/share/clang/run-clang-tidy.py" |
| 5 | +defaulthash="gr-utils/bindtool/scripts/binding-hash-hotfixer.zsh" |
| 6 | +zparseopts -D -E -F - -runclangtidy:=run h=help -src:=src -hash-updater:=hasher -branch-name:=branch |
| 7 | +parsingsuccess=$? |
| 8 | +if [[ ! -z "${help}" ]]; then |
| 9 | + echo "USAGE:" |
| 10 | + echo "$0 [-h] [--runclangtidy /path/to/run-clang-tidy.py] [PASS-ON]" |
| 11 | + echo "-h: print help" |
| 12 | + echo "--runclangtidy: specifies path to run-clang-tidy.py" |
| 13 | + echo "--src: Path to GNU Radio source root (default: ${defaultsrc})" |
| 14 | + echo "--hash-updater: Path to the hash hotfixer (default: --src/${defaulthash})" |
| 15 | + echo "--branch-name: Name of branch to work on; specify string 'NONE' to disable (default: clang-tidy-{random uuid})" |
| 16 | + echo "PASS-ON: Optional, remainder of options, which is passed on to run-clang-tidy.py (default: ${defaultrun})" |
| 17 | + echo "\nThis tool must be run from a build directory of a CMake run with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON" |
| 18 | + echo "\n\nThis runs clang-tidy on the whole codebase, applying all the checks specified in the .clang-tidy file." |
| 19 | + echo "Afterwards, the changed files are clang-formatted, and all PyBind binding headers are updated" |
| 20 | + exit 0 |
| 21 | +fi |
| 22 | + |
| 23 | +if [[ -z "$run[-1]" ]]; then |
| 24 | + run="${defaultrun}" |
| 25 | +else |
| 26 | + run="${run[-1]}" |
| 27 | +fi |
| 28 | +if [[ -z "$src[-1]" ]]; then |
| 29 | + src="$defaultsrc" |
| 30 | +else |
| 31 | + src="${src[-1]}" |
| 32 | +fi |
| 33 | +if [[ ! -x "$run" ]]; then |
| 34 | + echo "'$run' needs to be executable" |
| 35 | + exit -1 |
| 36 | +fi |
| 37 | +if [[ (! ( -d "${src}" && -x "${src}")) || (! -r "${src}/.clang-tidy") ]]; then |
| 38 | + echo "'${src}' needs to be a searchable directory containing the .clang-tidy file" >&2 |
| 39 | + exit -2 |
| 40 | +fi |
| 41 | + |
| 42 | +branching=1 |
| 43 | +if [[ -z "${branch[-1]}" ]]; then |
| 44 | + branch="clang-tidy-$(uuidgen)" |
| 45 | +elif [[ "${branch}" == "NONE" ]]; then |
| 46 | + branching=0 |
| 47 | +else |
| 48 | + branch="${branch[-1]}" |
| 49 | +fi |
| 50 | + |
| 51 | +gitcmd=(git -C "${src}" --no-pager) |
| 52 | + |
| 53 | +changes=$($gitcmd diff-index --name-only HEAD) |
| 54 | +if [[ ! $? == 0 ]]; then |
| 55 | + echo "git diff-index failed with error code; giving up" >&2 |
| 56 | + exit -3 |
| 57 | +fi |
| 58 | +if [[ ! -z "${changes}" ]]; then |
| 59 | + echo "unclean work tree; commit or stash before proceeding" >&2 |
| 60 | + exit -4 |
| 61 | +fi |
| 62 | + |
| 63 | +if [[ $branching != 0 ]]; then |
| 64 | + $gitcmd checkout -b ${branch} |
| 65 | + if [[ ! $? == 0 ]]; then |
| 66 | + echo "git checkout -b ${branch} failed with error code; giving up" >&2 |
| 67 | + exit -5 |
| 68 | + fi |
| 69 | +fi |
| 70 | + |
| 71 | +if [[ -z "$hasher[-1]" ]]; then |
| 72 | + hasher="${src}/$defaulthash" |
| 73 | +else |
| 74 | + hasher=$hasher[-1] |
| 75 | +fi |
| 76 | + |
| 77 | +#setopt -e |
| 78 | +echo "Running clang-tidy..." |
| 79 | +$run -checks=file '-header-filter=*' -fix "${src}" || echo "WARNING: run-clang-tidy had non-zero return code" >&2 |
| 80 | +echo "clang-tidy done." |
| 81 | + |
| 82 | +echo "Committing... (signoff implied, this operation contributes no code)" |
| 83 | +$gitcmd commit -sam "clang-tidy: run-clang-tidy with default checks on codebase" || echo "WARNING: git commit had non-zero return code" >&2 |
| 84 | +echo "Committing done" |
| 85 | + |
| 86 | +changed_files=$($gitcmd diff-index --name-only -z 'HEAD~1') |
| 87 | +for change in ${changed_files} ; do |
| 88 | + clang-format "${change}" |
| 89 | +done |
| 90 | + |
| 91 | +formatted_files=$($gitcmd diff name-only -z 'HEAD') |
| 92 | + |
| 93 | +if [[ ${#formatted_files} -ne 0 ]]; then |
| 94 | + git commit -sam "Clang-formatted files changed during clang-tidy" || echo "WARNING: git commit clang-formatting had non-zero return code" >&2 |
| 95 | +fi |
0 commit comments