|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script is used in Travis to check that PRs do not add obvious |
| 4 | +# flake8 violations. It relies on two things: |
| 5 | +# - find common ancestor between branch and |
| 6 | +# scikit-learn/scikit-learn remote |
| 7 | +# - run flake8 --diff on the diff between the branch and the common |
| 8 | +# ancestor |
| 9 | +# |
| 10 | +# Additional features: |
| 11 | +# - the line numbers in Travis match the local branch on the PR |
| 12 | +# author machine. |
| 13 | +# - ./build_tools/travis/flake8_diff.sh can be run locally for quick |
| 14 | +# turn-around |
| 15 | + |
| 16 | +set -e |
| 17 | +# pipefail is necessary to propagate exit codes |
| 18 | +set -o pipefail |
| 19 | + |
| 20 | +PROJECT=scikit-learn-contrib/imbalanced-learn |
| 21 | +PROJECT_URL=https://github.com/$PROJECT.git |
| 22 | + |
| 23 | +# Find the remote with the project name (upstream in most cases) |
| 24 | +REMOTE=$(git remote -v | grep $PROJECT | cut -f1 | head -1 || echo '') |
| 25 | + |
| 26 | +# Add a temporary remote if needed. For example this is necessary when |
| 27 | +# Travis is configured to run in a fork. In this case 'origin' is the |
| 28 | +# fork and not the reference repo we want to diff against. |
| 29 | +if [[ -z "$REMOTE" ]]; then |
| 30 | + TMP_REMOTE=tmp_reference_upstream |
| 31 | + REMOTE=$TMP_REMOTE |
| 32 | + git remote add $REMOTE $PROJECT_URL |
| 33 | +fi |
| 34 | + |
| 35 | +echo "Remotes:" |
| 36 | +echo '--------------------------------------------------------------------------------' |
| 37 | +git remote --verbose |
| 38 | + |
| 39 | +# Travis does the git clone with a limited depth (50 at the time of |
| 40 | +# writing). This may not be enough to find the common ancestor with |
| 41 | +# $REMOTE/master so we unshallow the git checkout |
| 42 | +if [[ -a .git/shallow ]]; then |
| 43 | + echo -e '\nTrying to unshallow the repo:' |
| 44 | + echo '--------------------------------------------------------------------------------' |
| 45 | + git fetch --unshallow |
| 46 | +fi |
| 47 | + |
| 48 | +if [[ "$TRAVIS" == "true" ]]; then |
| 49 | + if [[ "$TRAVIS_PULL_REQUEST" == "false" ]] |
| 50 | + then |
| 51 | + # In main repo, using TRAVIS_COMMIT_RANGE to test the commits |
| 52 | + # that were pushed into a branch |
| 53 | + if [[ "$PROJECT" == "$TRAVIS_REPO_SLUG" ]]; then |
| 54 | + if [[ -z "$TRAVIS_COMMIT_RANGE" ]]; then |
| 55 | + echo "New branch, no commit range from Travis so passing this test by convention" |
| 56 | + exit 0 |
| 57 | + fi |
| 58 | + COMMIT_RANGE=$TRAVIS_COMMIT_RANGE |
| 59 | + fi |
| 60 | + else |
| 61 | + # We want to fetch the code as it is in the PR branch and not |
| 62 | + # the result of the merge into master. This way line numbers |
| 63 | + # reported by Travis will match with the local code. |
| 64 | + LOCAL_BRANCH_REF=travis_pr_$TRAVIS_PULL_REQUEST |
| 65 | + # In Travis the PR target is always origin |
| 66 | + git fetch origin pull/$TRAVIS_PULL_REQUEST/head:refs/$LOCAL_BRANCH_REF |
| 67 | + fi |
| 68 | +fi |
| 69 | + |
| 70 | +# If not using the commit range from Travis we need to find the common |
| 71 | +# ancestor between $LOCAL_BRANCH_REF and $REMOTE/master |
| 72 | +if [[ -z "$COMMIT_RANGE" ]]; then |
| 73 | + if [[ -z "$LOCAL_BRANCH_REF" ]]; then |
| 74 | + LOCAL_BRANCH_REF=$(git rev-parse --abbrev-ref HEAD) |
| 75 | + fi |
| 76 | + echo -e "\nLast 2 commits in $LOCAL_BRANCH_REF:" |
| 77 | + echo '--------------------------------------------------------------------------------' |
| 78 | + git log -2 $LOCAL_BRANCH_REF |
| 79 | + |
| 80 | + REMOTE_MASTER_REF="$REMOTE/master" |
| 81 | + # Make sure that $REMOTE_MASTER_REF is a valid reference |
| 82 | + echo -e "\nFetching $REMOTE_MASTER_REF" |
| 83 | + echo '--------------------------------------------------------------------------------' |
| 84 | + git fetch $REMOTE master:refs/remotes/$REMOTE_MASTER_REF |
| 85 | + LOCAL_BRANCH_SHORT_HASH=$(git rev-parse --short $LOCAL_BRANCH_REF) |
| 86 | + REMOTE_MASTER_SHORT_HASH=$(git rev-parse --short $REMOTE_MASTER_REF) |
| 87 | + |
| 88 | + COMMIT=$(git merge-base $LOCAL_BRANCH_REF $REMOTE_MASTER_REF) || \ |
| 89 | + echo "No common ancestor found for $(git show $LOCAL_BRANCH_REF -q) and $(git show $REMOTE_MASTER_REF -q)" |
| 90 | + |
| 91 | + if [ -z "$COMMIT" ]; then |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + COMMIT_SHORT_HASH=$(git rev-parse --short $COMMIT) |
| 96 | + |
| 97 | + echo -e "\nCommon ancestor between $LOCAL_BRANCH_REF ($LOCAL_BRANCH_SHORT_HASH)"\ |
| 98 | + "and $REMOTE_MASTER_REF ($REMOTE_MASTER_SHORT_HASH) is $COMMIT_SHORT_HASH:" |
| 99 | + echo '--------------------------------------------------------------------------------' |
| 100 | + git show --no-patch $COMMIT_SHORT_HASH |
| 101 | + |
| 102 | + COMMIT_RANGE="$COMMIT_SHORT_HASH..$LOCAL_BRANCH_SHORT_HASH" |
| 103 | + |
| 104 | + if [[ -n "$TMP_REMOTE" ]]; then |
| 105 | + git remote remove $TMP_REMOTE |
| 106 | + fi |
| 107 | + |
| 108 | +else |
| 109 | + echo "Got the commit range from Travis: $COMMIT_RANGE" |
| 110 | +fi |
| 111 | + |
| 112 | +echo -e '\nRunning flake8 on the diff in the range' "$COMMIT_RANGE" \ |
| 113 | + "($(git rev-list $COMMIT_RANGE | wc -l) commit(s)):" |
| 114 | +echo '--------------------------------------------------------------------------------' |
| 115 | + |
| 116 | +check_files() { |
| 117 | + files="$1" |
| 118 | + options="$2" |
| 119 | + # Conservative approach: diff without context (--unified=0) so that code |
| 120 | + # that was not changed does not create failures |
| 121 | + git diff --unified=0 $COMMIT_RANGE -- $files | flake8 --diff --show-source $options |
| 122 | +} |
| 123 | + |
| 124 | +echo -e "No problem detected by flake8\n" |
0 commit comments