check-pr 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. #
  3. # Check an OpenCPN pull request for whitespace errors.
  4. #
  5. function usage() {
  6. cat << EOT
  7. Usage:
  8. check-pr [-l | -h] <<PR url> | <Repo url> <branch>>
  9. Parameters:
  10. PR url:
  11. As copied from the Github UI, something like
  12. https://github.com/Github6am/OpenCPN/tree/opti_amerz
  13. Repo url:
  14. As of the Github UI, the Code button; something like
  15. https://github.com/Github6am/OpenCPN
  16. Options:
  17. -l Print a list of offending files rather than the complete diff.
  18. -h Print this help message and exit
  19. EOT
  20. }
  21. # Handle options and parameters
  22. if [ "$1" = "-h" ]; then usage; exit 0; fi
  23. if [ "$1" = "-l" ]; then list_opt='true'; shift; fi
  24. if [ $# -eq 1 ]; then
  25. url=${1%%OpenCPN/*}/OpenCPN
  26. branch=${1##*/tree/}
  27. elif [ $# -eq 2 ]; then
  28. url=$1
  29. branch=$2
  30. else
  31. usage >&1
  32. exit 1
  33. fi
  34. # Set up the pr-tmp remote and the empty tree we compare against
  35. if git config remote.pr-tmp.url >/dev/null; then
  36. git remote remove pr-tmp
  37. fi
  38. git remote add pr-tmp $url
  39. git fetch pr-tmp $branch
  40. empty_tree=$(git hash-object -t tree /dev/null)
  41. # Do the check
  42. if [ -n "$list_opt" ]; then
  43. exec > >(sed -e 's/:.*//' -e '/^+/d' | uniq)
  44. fi
  45. for f in $(git diff --name-only --diff-filter=ACMR HEAD pr-tmp/$branch); do
  46. git diff-tree --check $empty_tree pr-tmp/$branch $f
  47. done
  48. # Cleanup (stdout possibly redirected).
  49. git remote remove pr-tmp