git-log-pretty-since.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bash
  2. # Prints a nicely-formatted commit history.
  3. # - Commits are grouped below their merge-commit.
  4. # - Issue numbers are moved next to the commit-id.
  5. #
  6. # Parameters:
  7. # $1 "since" commit
  8. # $2 "inverse match" regex pattern
  9. set -e
  10. set -u
  11. set -o pipefail
  12. __SINCE=$1
  13. __INVMATCH=$2
  14. is_merge_commit() {
  15. git rev-parse $1 >/dev/null 2>&1 \
  16. || { echo "ERROR: invalid commit: $1"; exit 1; }
  17. git log $1^2 >/dev/null 2>&1 && return 0 || return 1
  18. }
  19. # Removes parens from issue/ticket/PR numbers.
  20. #
  21. # Example:
  22. # in: 3340e08becbf foo (#9423)
  23. # out: 3340e08becbf foo #9423
  24. _deparen() {
  25. sed 's/(\(\#[0-9]\{3,\}\))/\1/g'
  26. }
  27. # Cleans up issue/ticket/PR numbers in the commit descriptions.
  28. #
  29. # Example:
  30. # in: 3340e08becbf foo (#9423)
  31. # out: 3340e08becbf #9423 foo
  32. _format_ticketnums() {
  33. nvim -Es +'g/\v(#[0-9]{3,})/norm! ngEldE0ep' +'%p' | _deparen
  34. }
  35. for commit in $(git log --format='%H' --first-parent "$__SINCE"..HEAD); do
  36. if is_merge_commit ${commit} ; then
  37. if [ -z "$__INVMATCH" ] || ! git log --oneline ${commit}^1..${commit}^2 \
  38. | >/dev/null 2>&1 grep -E "$__INVMATCH" ; then
  39. git log -1 --oneline ${commit}
  40. git log --format=' %h %s' ${commit}^1..${commit}^2
  41. fi
  42. else
  43. git log -1 --oneline ${commit}
  44. fi
  45. done | _format_ticketnums