Last active
March 21, 2023 21:36
-
-
Save cnizzardini/e2ebce772b08a6472caaa5b7a9c0a52f to your computer and use it in GitHub Desktop.
Generate a list of changed files in a branch to run against static analysis (see comments below)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### Run static analysis on changed files only | |
# | |
# Determine the base branch for comparison | |
# | |
BASE_BRANCH="develop" | |
if [[ $BRANCH = *"hotfix/"* ]];then | |
BASE_BRANCH="master" | |
fi | |
echo "Base Branch: $BASE_BRANCH" | |
# | |
# Get the current branch | |
# | |
BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
echo "Branch: $BRANCH" | |
# | |
# Get the first and last commit hashes | |
# | |
FIRST_COMMIT=($(git log $BASE_BRANCH..$BRANCH --oneline | tail -1)) | |
FIRST_COMMIT=${FIRST_COMMIT[0]} | |
LAST_COMMIT=($(git log -n 1 $BRANCH --oneline | tail -1)) | |
LAST_COMMIT=${LAST_COMMIT[0]} | |
if [[ $FIRST_COMMIT = "" ]];then | |
echo "No commits to compare yet, exiting analysis early." | |
exit 0 | |
fi | |
echo "First Commit: $FIRST_COMMIT" | |
echo "Last Commit: $LAST_COMMIT" | |
# | |
# Get list of commit hashes which are not merges, then get the files in that commit and add them to an array uniquely | |
# only if the file exists | |
# | |
declare -A FILES | |
PWD=$(pwd) | |
for hash in $(git rev-list --ancestry-path $FIRST_COMMIT..$LAST_COMMIT) | |
do | |
if [[ $(git log -1 $hash) != *"Merge: "* ]];then | |
for file in $(git diff-tree --no-commit-id --name-only $hash -r) | |
do | |
if [[ -f "$PWD/$file" ]]; then | |
FILES[$file]=$file | |
fi | |
done | |
fi | |
done | |
# | |
# Concat to a single string | |
# | |
PHPCS_LIST="" | |
PHPMD_LIST="" | |
for file in "${FILES[@]}" | |
do | |
PHPCS_LIST+="$file " | |
PHPMD_LIST+="$file," | |
done | |
# | |
# Run static analysis | |
# | |
vendor/bin/phpcs -p --colors $PHPCS_LIST | |
vendor/bin/phpmd ${PHPMD_LIST::-1} ansi phpmd.xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will get a list of files that were changed in the branch while ignoring merges and any files that don't exist and then pass those files into phpcs or phpmd.
For base branch comparison:
This is not perfect in that it can include files that were once part of the branch but have since been reverted to their base branch state. Probably several other issues I haven't found yet, but it is a crutch for running analysis on large code bases that you hope to clean up slowly overtime.