|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eou pipefail |
| 4 | + |
| 5 | +GIT_TOP_DIR=$(git rev-parse --show-toplevel) |
| 6 | + |
| 7 | +TMPFILE=$(mktemp) |
| 8 | +trap "rm -rf ${TMPFILE}" EXIT |
| 9 | + |
| 10 | +# By default just run against the latest commit |
| 11 | +BASE=${BASE:-HEAD~1} |
| 12 | +HEAD=${HEAD:-HEAD} |
| 13 | + |
| 14 | +ancestor=$(git merge-base "${BASE}" "${HEAD}") |
| 15 | +echo "INFO: Checking aginst the following stats" |
| 16 | +( |
| 17 | + set -x |
| 18 | + git diff --stat "$ancestor" "${HEAD}" | sed '$d' > "${TMPFILE}" |
| 19 | +) |
| 20 | + |
| 21 | +while read -r git_attribute; do |
| 22 | + if echo "${git_attribute}" | grep "linguist-generated=true" >/dev/null 2>/dev/null; then |
| 23 | + pattern=$(echo ${git_attribute} | cut -d' ' -f1) |
| 24 | + escaped_pattern=$(printf '%s\n' "$pattern" | sed -e 's/[\/&]/\\&/g') |
| 25 | + # Delete known generated files |
| 26 | + sed -i '/'"${escaped_pattern}"'/d' "${TMPFILE}" |
| 27 | + fi |
| 28 | +done < "${GIT_TOP_DIR}/.gitattributes" |
| 29 | + |
| 30 | +echo "INFO: Showing non-generated files:" |
| 31 | +( |
| 32 | + set -x |
| 33 | + cat "${TMPFILE}" |
| 34 | +) |
| 35 | + |
| 36 | +# Get only files that have changed |
| 37 | +changed_files=$(cut -d' ' -f2 "${TMPFILE}" | xargs) |
| 38 | + |
| 39 | +details=$(git diff --shortstat "$ancestor" "${HEAD}" -- ${changed_files}) |
| 40 | +add=$(echo "$details" | grep -o '[0-9]* insertion' | grep -o '[0-9]*' || true) |
| 41 | +remove=$(echo "$details" | grep -o '[0-9]* deletion' | grep -o '[0-9]*' || true) |
| 42 | +pr_size=0 |
| 43 | +if [ "$add" ]; then |
| 44 | + pr_size=$(("$pr_size" + "$add")) |
| 45 | +fi |
| 46 | +if [ "$remove" ]; then |
| 47 | + pr_size=$(("$pr_size" + "$remove")) |
| 48 | +fi |
| 49 | +echo "INFO: PR SIZE is ${pr_size}" |
| 50 | + |
| 51 | +if ((pr_size > 2000)); then |
| 52 | + echo |
| 53 | + echo 'Your PR is '"$pr_size"' LOC which is more than the 2000 maximum' |
| 54 | + echo 'allowed within PyTorch infra. PLease make sure to split up' |
| 55 | + echo 'your PR into smaller pieces that can be reviewed.' |
| 56 | + echo 'If you think that this rule should not apply to your PR,' |
| 57 | + echo 'please contact @albanD or @seemethere.' |
| 58 | + echo |
| 59 | + exit 1 |
| 60 | +fi |
0 commit comments