git_info_for_prompt 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. local g="$(git rev-parse --git-dir 2>/dev/null)"
  2. if [ -n "$g" ]; then
  3. local r
  4. local b
  5. if [ -d "$g/../.dotest" ]
  6. then
  7. if test -f "$g/../.dotest/rebasing"
  8. then
  9. r="|REBASE"
  10. elif test -f "$g/../.dotest/applying"
  11. then
  12. r="|AM"
  13. else
  14. r="|AM/REBASE"
  15. fi
  16. b="$(git symbolic-ref HEAD 2>/dev/null)"
  17. elif [ -f "$g/.dotest-merge/interactive" ]
  18. then
  19. r="|REBASE-i"
  20. b="$(cat "$g/.dotest-merge/head-name")"
  21. elif [ -d "$g/.dotest-merge" ]
  22. then
  23. r="|REBASE-m"
  24. b="$(cat "$g/.dotest-merge/head-name")"
  25. elif [ -f "$g/MERGE_HEAD" ]
  26. then
  27. r="|MERGING"
  28. b="$(git symbolic-ref HEAD 2>/dev/null)"
  29. else
  30. if [ -f "$g/BISECT_LOG" ]
  31. then
  32. r="|BISECTING"
  33. fi
  34. if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
  35. then
  36. if ! b="tag: $(git describe --exact-match HEAD 2>/dev/null)"
  37. then
  38. b="$(cut -c1-7 "$g/HEAD")..."
  39. fi
  40. fi
  41. fi
  42. if [ -n "$1" ]; then
  43. printf "$1" "${b##refs/heads/}$r"
  44. else
  45. printf "[%s]" "${b##refs/heads/}$r"
  46. fi
  47. fi