Skip to content

Instantly share code, notes, and snippets.

@gpeal
Created January 17, 2021 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpeal/5fce5b443c155fbbcc2fa0d5b9300133 to your computer and use it in GitHub Desktop.
Save gpeal/5fce5b443c155fbbcc2fa0d5b9300133 to your computer and use it in GitHub Desktop.
#!/bin/bash -e
# Run Detekt on pre-push on changed files
cd Android
while read -r local_ref local_sha remote_ref remote_sha; do
if [[ "$local_sha" =~ ^0+$ ]]; then
# Handle delete
:
else
if [[ "$remote_sha" =~ ^0+$ ]]; then
# New branch: find first parent that exists on remote to compute diff
remote_sha=$(
git rev-list "$local_sha" | while read -r ref; do
remote_branches=$( git branch -r --contains "$ref" )
if [ -n "$remote_branches" ]; then
printf "%s" "$ref"
break
fi
done
)
fi
range="$remote_sha..$local_sha"
# List Added (A), Copied (C), Modified (M), Renamed (R), have their type changed (T) files
IFS= read -rd '' changed_files < <( git diff --name-only --relative --diff-filter=ACMRT "$range" "**/src/main/**/*.kt" ) || true
echo "Commit range: $range; number of changed kt files: $( printf "%s" "$changed_files" | wc -l | xargs )"
if [ -n "$changed_files" ]
then
# `paste -sd "," -` replaces newlines with commas
./detekt -i "$( printf "%s" "$changed_files" | paste -sd "," - )"
fi
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment