git-resquash.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/sh
  2. #
  3. # Provides a convenient alias for "git rebase -i --autosquash --keep-root"
  4. # on gits that have it, and a replacement on gits that don't.
  5. set -e
  6. PARENT="$1"
  7. if test "$PARENT" = ""; then
  8. echo "You must specify the parent branch."
  9. exit 1
  10. fi
  11. # Can we use git rebase --keep-base? Detect the git version to find out.
  12. GITVER=$(git version)
  13. if test "$(echo "$GITVER"|cut -d ' ' -f 1-2)" = "git version"; then
  14. # --keep-base was added in git 2.24. Detect if we have that version.
  15. GITVER=$(echo "$GITVER" | cut -d ' ' -f 3)
  16. major=$(echo "$GITVER" | cut -d . -f 1)
  17. minor=$(echo "$GITVER" | cut -d . -f 2)
  18. if test "$major" -lt 2; then
  19. USE_KEEP_BASE=0
  20. elif test "$major" -eq 2 && test "$minor" -lt 24; then
  21. USE_KEEP_BASE=0
  22. else
  23. USE_KEEP_BASE=1
  24. fi
  25. else
  26. # This isn't a git that reports its version in a way recognize; assume that
  27. # --keep-base will work
  28. USE_KEEP_BASE=1
  29. fi
  30. if test "$USE_KEEP_BASE" = "1" ; then
  31. exec git rebase -i --autosquash --keep-base "${PARENT}"
  32. else
  33. REV=$(git log --reverse --format='%H' "${PARENT}..HEAD" | head -1)
  34. if test "${REV}" = ""; then
  35. echo "No changes here since ${PARENT}"
  36. exit 1
  37. fi
  38. exec git rebase -i --autosquash "${REV}^"
  39. fi