add_commit_range.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/bash
  2. #
  3. # Our travis setup relies heavily on rapidly cancelling
  4. # jobs that are unneeded. For example, if only files
  5. # in aspnet/ were modified, then we don't need to run
  6. # a job that verifies the framework tests in go/
  7. #
  8. # Detecting what files have been changed for a pull
  9. # request is a a bit tricky, as Travis-CI currently
  10. # does not provide a commit range for pull requests
  11. # (see travis-ci/travis-ci#1719). This script provides
  12. # a commit range by adding a branch prbase on the
  13. # first commit in the pull request and a branch
  14. # prhead on the commit that travis is currently
  15. # building.
  16. # This makes
  17. # git diff prbase:prhead
  18. # equivalent to what you'd expect from
  19. # git diff $TRAVIS_COMMIT_RANGE
  20. if [ "${TRAVIS_PULL_REQUEST}" = "false" ]
  21. then
  22. echo "This is not a pull request, nothing to do"
  23. exit 0
  24. fi
  25. # Find the first commit in the pull request
  26. # - download github PR patch file
  27. # - grep commit SHA from first line
  28. # - remove newline
  29. PR_FIRST=$(curl -s https://github.com/${TRAVIS_REPO_SLUG}/pull/${TRAVIS_PULL_REQUEST}.patch | head -1 | grep -o -E '\b[0-9a-f]{40}\b' | tr -d '\n')
  30. # Create new branch for first commit in pull request
  31. git branch -f prbase ${PR_FIRST}
  32. # Create new branch for last commit in pull request
  33. git branch -f prhead ${TRAVIS_COMMIT}
  34. echo "Set prbase branch to commit ${PR_FIRST}"
  35. echo "Set prhead branch to commit ${TRAVIS_COMMIT}"