Guest User

Untitled

a guest
Jan 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. REAL_GIT=${REAL_GIT-/usr/bin/git}
  4. TICKET_PREFIX=TICKET-PREFIX- # Like JIRA-, REDMINE-, CLEARQUEST- or something.
  5. TICKET_REGEX="${TICKET_PREFIX}[0-9][0-9]*"
  6. main() {
  7. local command=$1
  8. shift
  9. if [[ commit == $command ]] || [[ ticket == $command ]] ; then
  10. get_ticket
  11. fi ;
  12. if [[ ticket != $command ]] ; then
  13. $REAL_GIT $command "$@" || die "ERROR from '$REAL_GIT $command $@': $?." "$?"
  14. fi ;
  15. if [[ commit == $command ]] || [[ ticket == $command ]] ; then
  16. local that_commit=$($REAL_GIT log -1 --pretty=%B)
  17. if ! echo "$that_commit" | egrep -oh "$TICKET_REGEX" ; then
  18. echo "NOTE: Last commit ('$that_commit') lacks any reference to a ticket ($TICKET_PREFIX)." 1>&2
  19. local new_commit_msg=''
  20. [[ -z $that_commit ]] && new_commit_msg="$TICKET" || new_commit_msg="$that_commit $TICKET"
  21. $REAL_GIT commit --amend -m "$new_commit_msg"
  22. else
  23. echo "NOTE: Last commit ('$that_commit') already refers to a ticket ($TICKET_PREFIX)." 1>&2
  24. fi ;
  25. fi ;
  26. }
  27. die() {
  28. echo "${1:-ERROR}" 1>&2
  29. exit "${2-1}"
  30. }
  31. get_ticket() {
  32. local ticket=''
  33. local reporoot=$($REAL_GIT rev-parse --show-toplevel)
  34. local changelog=$(ls "$reporoot/" | grep CHANGELOG \
  35. || ls "$reporoot/" | grep -i CHANGELOG
  36. )
  37. if [[ -z $ticket ]] ; then
  38. if [ ${TICKET+x} ] ; then
  39. # Try $TICKET variable:
  40. ticket=$TICKET
  41. elif [[ $changelog ]] ; then
  42. # Try changelog in reverse chronological order (latest at head instead of tail):
  43. ticket=$(egrep "$TICKET_PREFIX" "$reporoot/$changelog" | egrep -oh "$TICKET_REGEX" | head -1)
  44. fi ;
  45. fi;
  46. if [[ -z $ticket ]] ; then
  47. # Try $DEFAULT_TICKET variable:
  48. if [ ${DEFAULT_TICKET+x} ] ; then
  49. ticket=$DEFAULT_TICKET
  50. fi ;
  51. fi;
  52. if ! echo "$ticket" | egrep "^$TICKET_REGEX" >/dev/null ; then
  53. echo "WARNING: Ticket '$ticket' does not match TICKET_REGEX /^$TICKET_REGEX/." 1>&2
  54. fi ;
  55. if [[ -z $ticket ]] ; then
  56. die "ERROR: Failed to determine current $TICKET_PREFIX ticket (matching /TICKET_REGEX/) via \$TICKET nor $reporoot/$changelog nor \$DEFAULT_TICKET."
  57. return 1
  58. fi ;
  59. TICKET=$ticket
  60. }
  61.  
  62. main "$@"
Add Comment
Please, Sign In to add comment