validate-commit-msg.sh 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/bash
  2. #
  3. # Validate commit message matches our expected format
  4. #
  5. # Arguments:
  6. # <commit> Commit revision to validate
  7. #
  8. # Example:
  9. # ./validate-commit-msg.sh abcdef0
  10. # Exit on first error
  11. set -e
  12. # If we didn't receive a commit, then error out
  13. # DEV: If want the HEAD commit, then use `git rev-parse HEAD`
  14. COMMIT_REV="$1"
  15. if [ -z "$COMMIT_REV" ]; then
  16. echo "Expected a commit revision to validate but received nothing" 1>&2
  17. exit 1
  18. fi
  19. # Resolve our file for output
  20. # DEV: We use `.git` to avoid cluttering the working directory
  21. GIT_DIR="$(git rev-parse --git-dir)"
  22. TARGET_FILENAME=VALIDATE_COMMIT_MSG
  23. TARGET_FILE="$GIT_DIR/$TARGET_FILENAME"
  24. echo "Retrieving relevant commit message for $COMMIT_REV"
  25. # Output our log message to a file
  26. # DEV: We use `--no-merges` to skip merge commits introduced by GitHub
  27. git log --no-merges --format=format:"%s" -n 1 "$COMMIT_REV" > "$TARGET_FILE"
  28. echo "Validating contents of $TARGET_FILE"
  29. cat "$TARGET_FILE"
  30. # Validate our message
  31. ./node_modules/.bin/validate-commit-msg "$TARGET_FILENAME"