Skip to content

Instantly share code, notes, and snippets.

@cvvergara
Last active July 1, 2025 20:33
Show Gist options
  • Select an option

  • Save cvvergara/dd38ca93e2484e553b82e0f81e8a6657 to your computer and use it in GitHub Desktop.

Select an option

Save cvvergara/dd38ca93e2484e553b82e0f81e8a6657 to your computer and use it in GitHub Desktop.
clang tidy vs commit
#!/usr/bin/env bash
# Copyright (c) 2024, pgRouting developers
# All rights reserved.
# This source code is licensed under the GPL-version2 license
# Runs clang-tidy only on changed files of pgRouting project
# How to use:
# Copy to the root of your repository
# install clang-tidy and to the cmake command add the flag:
# -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
# - Adjust the code to the situation you are handling
# - Do not add to .gitignore
# - Do not add to the pgRouting repository
# Example use:
# bash tidy-vs-commit.sh upstream/develop
# bash tidy-vs-commit.sh f8350e2b1b
readonly BASE=$1
readonly BUILD_DIR=build
MODIFIED_FILES=$(git diff-tree --no-commit-id --diff-filter=d --name-only -r "$BASE" HEAD src | grep '\.c')
MODIFIED_HEADERS=$(git diff-tree --no-commit-id --diff-filter=d --name-only -r "$BASE" HEAD include | grep '\.h')
POSTGRES_SERVER=$(grep -o -m1 '\-isystem .*' "${BUILD_DIR}/compile_commands.json" | head -1 | awk '{print $2}')
echo "POSTGRES_SERVER ${POSTGRES_SERVER}"
# Useful when cleaning code
# From the list of clang-tidy checks, only check this one:
CHECKS="-checks=cppcoreguidelines-no-malloc"
if [ -z "${MODIFIED_FILES}" ] && [ -z "${MODIFIED_HEADERS}" ]; then
echo "No paths modified"
exit 0
fi
# C/C++ use the compile command
if [ ${#MODIFIED_FILES[@]} != 0 ] ; then
for f in ${MODIFIED_FILES}
do
if [ "${f##*.}" == 'conf' ]; then continue; fi
echo "${f}"
# when fixing code for a special check uncomment
#clang-tidy -p "${BUILD_DIR}" "${CHECKS}" -header-filter="^$(pwd).*" "${f}"
# Use pgRouting's clang-tidy configuration file
clang-tidy -p "${BUILD_DIR}" --config-file=./.clang-tidy "${f}"
done
fi
# .h/.hpp do not have a compile command
# workaround: add the includes
if [ ${#MODIFIED_HEADERS[@]} != 0 ] ; then
for f in ${MODIFIED_HEADERS}
do
echo "${f}"
clang-tidy --config-file=./.clang-tidy "${f}"
-- -I./include -isystem "${POSTGRES_SERVER}"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment