git.zsh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Check the installed git against a given version number
  2. #
  3. # Prints 1 if installed > input.
  4. # Prints -1 if installed < input.
  5. # Prints 0 if they are the same.
  6. function git_compare_version() {
  7. # Sanitise input. Remove dots, right-pad 0's to 3 digits.
  8. INPUT=${1//./}
  9. INPUT=$(echo $INPUT | sed -e '
  10. :again
  11. s/^.\{1,2\}$/&0/
  12. t again'
  13. )
  14. # Sanitise installed version to 3 digits as well.
  15. # The raw version string takes the form 'git version 1.2.3...'
  16. INSTALLED=$(git --version 2> /dev/null)
  17. INSTALLED=${INSTALLED:12:5}
  18. INSTALLED=${INSTALLED//./}
  19. if [[ $INSTALLED -gt $INPUT ]]; then
  20. echo 1
  21. elif [[ $INSTALLED -lt $INPUT ]]; then
  22. echo -1
  23. else
  24. echo 0
  25. fi
  26. return 0
  27. }
  28. # Prepare compact git log (`gl`) format
  29. #
  30. # Log displays train tracks, decorations, users and dates.
  31. if [[ $(git_compare_version "1.8.3") -ge 0 ]]; then
  32. DECO_COLOUR='%C(auto)'
  33. else
  34. DECO_COLOUR='%Cgreen'
  35. fi
  36. GIT_LOG_PRETTY="'format:%C(yellow)%h %Creset%ad %Cblue%an:$DECO_COLOUR%d %Creset%s'"
  37. GIT_LOG_DEFAULTS="--graph --date=short --pretty=$GIT_LOG_PRETTY"
  38. # Useful oh-my-zsh git plugin aliases:
  39. #
  40. # ga, gc, gco, gb, gba, gm, grhh, grb/grba/grbc, gwip, gunwip
  41. #
  42. # Aliases not in the git plugin:
  43. alias ga.='git add -A .'
  44. alias gap='git add -p'
  45. alias gbav='git branch -av'
  46. alias gbd='git branch -D'
  47. alias gbv='git branch -v'
  48. alias gcob='git checkout -b'
  49. alias gcop='git checkout -p'
  50. alias gds='git diff --staged'
  51. alias gf='git fetch --all --tags && git fetch --all --prune'
  52. alias ggpush='git push -u origin $(current_branch)'
  53. alias gl="git log $GIT_LOG_DEFAULTS"
  54. alias gla='gl --all' # show all refs, not only those reachable from current branch.
  55. alias glb='gl --simplify-by-decoration' # show mostly branches and tags.
  56. alias glh="git --no-pager log --max-count=15 $GIT_LOG_DEFAULTS" # show first few.
  57. alias glp='git log --graph --decorate -p'
  58. alias gls='git log --graph --decorate --stat'
  59. alias gr='git reset'
  60. alias grh='git reset --hard'
  61. alias grs='git reset --soft'
  62. alias gs='git status'
  63. alias gsr='git show --format=raw' # all info about a commmit.
  64. # Searching history.
  65. alias glG='git log --stat -G' # Search DIFFS - changes with given text.
  66. alias glS='git log --stat -S' # Search DIFFS - changes in number of given text.
  67. alias glg='git log --grep' # Search MESSAGES.
  68. # Submodule management.
  69. alias gsm='git submodule'
  70. alias gsmpull='git submodule foreach git pull origin master'
  71. alias gsmup='git submodule sync && git submodule update --init --recursive'
  72. # Fix tracking for origin if not there.
  73. alias track='git branch --set-upstream-to origin/$(current_branch) && git fetch'
  74. # Autosquashing for simple fixups.
  75. alias grbi='git rebase -i --autosquash'
  76. alias gcf='git commit --fixup'
  77. alias gcs='git commit --squash'
  78. # Recover indexed/staged changes that were lost.
  79. #
  80. # By a careless reset, for example. This function locates all dangling/orphaned
  81. # blobs and puts them in text files. These files can then be checked for the
  82. # lost changes. http://blog.ctp.com/2013/11/21/git-recovering-from-mistakes/
  83. function git_retrieve_discarded_index_changes() {
  84. for blob in $(git fsck --lost-found | awk '$2 == "blob" { print $3 }'); do
  85. git cat-file -p $blob > $blob.txt;
  86. done
  87. }