git.plugin.zsh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. # Git version checking
  2. autoload -Uz is-at-least
  3. git_version="${${(As: :)$(git version 2>/dev/null)}[3]}"
  4. #
  5. # Functions
  6. #
  7. # The name of the current branch
  8. # Back-compatibility wrapper for when this function was defined here in
  9. # the plugin, before being pulled in to core lib/git.zsh as git_current_branch()
  10. # to fix the core -> git plugin dependency.
  11. function current_branch() {
  12. git_current_branch
  13. }
  14. # Pretty log messages
  15. function _git_log_prettily(){
  16. if ! [ -z $1 ]; then
  17. git log --pretty=$1
  18. fi
  19. }
  20. compdef _git _git_log_prettily=git-log
  21. # Warn if the current branch is a WIP
  22. function work_in_progress() {
  23. if $(git log -n 1 2>/dev/null | grep -q -c "\-\-wip\-\-"); then
  24. echo "WIP!!"
  25. fi
  26. }
  27. # Check if main exists and use instead of master
  28. function git_main_branch() {
  29. command git rev-parse --git-dir &>/dev/null || return
  30. local ref
  31. for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk}; do
  32. if command git show-ref -q --verify $ref; then
  33. echo ${ref:t}
  34. return
  35. fi
  36. done
  37. echo master
  38. }
  39. # Check for develop and similarly named branches
  40. function git_develop_branch() {
  41. command git rev-parse --git-dir &>/dev/null || return
  42. local branch
  43. for branch in dev devel development; do
  44. if command git show-ref -q --verify refs/heads/$branch; then
  45. echo $branch
  46. return
  47. fi
  48. done
  49. echo develop
  50. }
  51. #
  52. # Aliases
  53. # (sorted alphabetically)
  54. #
  55. alias g='git'
  56. alias ga='git add'
  57. alias gaa='git add --all'
  58. alias gapa='git add --patch'
  59. alias gau='git add --update'
  60. alias gav='git add --verbose'
  61. alias gap='git apply'
  62. alias gapt='git apply --3way'
  63. alias gb='git branch'
  64. alias gba='git branch -a'
  65. alias gbd='git branch -d'
  66. alias gbda='git branch --no-color --merged | command grep -vE "^([+*]|\s*($(git_main_branch)|$(git_develop_branch))\s*$)" | command xargs git branch -d 2>/dev/null'
  67. alias gbD='git branch -D'
  68. alias gbl='git blame -b -w'
  69. alias gbnm='git branch --no-merged'
  70. alias gbr='git branch --remote'
  71. alias gbs='git bisect'
  72. alias gbsb='git bisect bad'
  73. alias gbsg='git bisect good'
  74. alias gbsr='git bisect reset'
  75. alias gbss='git bisect start'
  76. alias gc='git commit -v'
  77. alias gc!='git commit -v --amend'
  78. alias gcn!='git commit -v --no-edit --amend'
  79. alias gca='git commit -v -a'
  80. alias gca!='git commit -v -a --amend'
  81. alias gcan!='git commit -v -a --no-edit --amend'
  82. alias gcans!='git commit -v -a -s --no-edit --amend'
  83. alias gcam='git commit -a -m'
  84. alias gcsm='git commit -s -m'
  85. alias gcas='git commit -a -s'
  86. alias gcasm='git commit -a -s -m'
  87. alias gcb='git checkout -b'
  88. alias gcf='git config --list'
  89. function gccd() {
  90. command git clone --recurse-submodules "$@"
  91. [[ -d "$_" ]] && cd "$_" || cd "${${_:t}%.git}"
  92. }
  93. compdef _git gccd=git-clone
  94. alias gcl='git clone --recurse-submodules'
  95. alias gclean='git clean -id'
  96. alias gpristine='git reset --hard && git clean -dffx'
  97. alias gcm='git checkout $(git_main_branch)'
  98. alias gcd='git checkout $(git_develop_branch)'
  99. alias gcmsg='git commit -m'
  100. alias gco='git checkout'
  101. alias gcor='git checkout --recurse-submodules'
  102. alias gcount='git shortlog -sn'
  103. alias gcp='git cherry-pick'
  104. alias gcpa='git cherry-pick --abort'
  105. alias gcpc='git cherry-pick --continue'
  106. alias gcs='git commit -S'
  107. alias gcss='git commit -S -s'
  108. alias gcssm='git commit -S -s -m'
  109. alias gd='git diff'
  110. alias gdca='git diff --cached'
  111. alias gdcw='git diff --cached --word-diff'
  112. alias gdct='git describe --tags $(git rev-list --tags --max-count=1)'
  113. alias gds='git diff --staged'
  114. alias gdt='git diff-tree --no-commit-id --name-only -r'
  115. alias gdup='git diff @{upstream}'
  116. alias gdw='git diff --word-diff'
  117. function gdnolock() {
  118. git diff "$@" ":(exclude)package-lock.json" ":(exclude)*.lock"
  119. }
  120. compdef _git gdnolock=git-diff
  121. function gdv() { git diff -w "$@" | view - }
  122. compdef _git gdv=git-diff
  123. alias gf='git fetch'
  124. # --jobs=<n> was added in git 2.8
  125. is-at-least 2.8 "$git_version" \
  126. && alias gfa='git fetch --all --prune --jobs=10' \
  127. || alias gfa='git fetch --all --prune'
  128. alias gfo='git fetch origin'
  129. alias gfg='git ls-files | grep'
  130. alias gg='git gui citool'
  131. alias gga='git gui citool --amend'
  132. function ggf() {
  133. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  134. git push --force origin "${b:=$1}"
  135. }
  136. compdef _git ggf=git-checkout
  137. function ggfl() {
  138. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  139. git push --force-with-lease origin "${b:=$1}"
  140. }
  141. compdef _git ggfl=git-checkout
  142. function ggl() {
  143. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  144. git pull origin "${*}"
  145. else
  146. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  147. git pull origin "${b:=$1}"
  148. fi
  149. }
  150. compdef _git ggl=git-checkout
  151. function ggp() {
  152. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  153. git push origin "${*}"
  154. else
  155. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  156. git push origin "${b:=$1}"
  157. fi
  158. }
  159. compdef _git ggp=git-checkout
  160. function ggpnp() {
  161. if [[ "$#" == 0 ]]; then
  162. ggl && ggp
  163. else
  164. ggl "${*}" && ggp "${*}"
  165. fi
  166. }
  167. compdef _git ggpnp=git-checkout
  168. function ggu() {
  169. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  170. git pull --rebase origin "${b:=$1}"
  171. }
  172. compdef _git ggu=git-checkout
  173. alias ggpur='ggu'
  174. alias ggpull='git pull origin "$(git_current_branch)"'
  175. alias ggpush='git push origin "$(git_current_branch)"'
  176. alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
  177. alias gpsup='git push --set-upstream origin $(git_current_branch)'
  178. alias ghh='git help'
  179. alias gignore='git update-index --assume-unchanged'
  180. alias gignored='git ls-files -v | grep "^[[:lower:]]"'
  181. alias git-svn-dcommit-push='git svn dcommit && git push github $(git_main_branch):svntrunk'
  182. alias gk='\gitk --all --branches &!'
  183. alias gke='\gitk --all $(git log -g --pretty=%h) &!'
  184. alias gl='git pull'
  185. alias glg='git log --stat'
  186. alias glgp='git log --stat -p'
  187. alias glgg='git log --graph'
  188. alias glgga='git log --graph --decorate --all'
  189. alias glgm='git log --graph --max-count=10'
  190. alias glo='git log --oneline --decorate'
  191. alias glol="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset'"
  192. alias glols="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset' --stat"
  193. alias glod="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"
  194. alias glods="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset' --date=short"
  195. alias glola="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset' --all"
  196. alias glog='git log --oneline --decorate --graph'
  197. alias gloga='git log --oneline --decorate --graph --all'
  198. alias glp="_git_log_prettily"
  199. alias gm='git merge'
  200. alias gmom='git merge origin/$(git_main_branch)'
  201. alias gmtl='git mergetool --no-prompt'
  202. alias gmtlvim='git mergetool --no-prompt --tool=vimdiff'
  203. alias gmum='git merge upstream/$(git_main_branch)'
  204. alias gma='git merge --abort'
  205. alias gp='git push'
  206. alias gpd='git push --dry-run'
  207. alias gpf='git push --force-with-lease'
  208. alias gpf!='git push --force'
  209. alias gpoat='git push origin --all && git push origin --tags'
  210. alias gpr='git pull --rebase'
  211. alias gpu='git push upstream'
  212. alias gpv='git push -v'
  213. alias gr='git remote'
  214. alias gra='git remote add'
  215. alias grb='git rebase'
  216. alias grba='git rebase --abort'
  217. alias grbc='git rebase --continue'
  218. alias grbd='git rebase $(git_develop_branch)'
  219. alias grbi='git rebase -i'
  220. alias grbm='git rebase $(git_main_branch)'
  221. alias grbo='git rebase --onto'
  222. alias grbs='git rebase --skip'
  223. alias grev='git revert'
  224. alias grh='git reset'
  225. alias grhh='git reset --hard'
  226. alias groh='git reset origin/$(git_current_branch) --hard'
  227. alias grm='git rm'
  228. alias grmc='git rm --cached'
  229. alias grmv='git remote rename'
  230. alias grrm='git remote remove'
  231. alias grs='git restore'
  232. alias grset='git remote set-url'
  233. alias grss='git restore --source'
  234. alias grst='git restore --staged'
  235. alias grt='cd "$(git rev-parse --show-toplevel || echo .)"'
  236. alias gru='git reset --'
  237. alias grup='git remote update'
  238. alias grv='git remote -v'
  239. alias gsb='git status -sb'
  240. alias gsd='git svn dcommit'
  241. alias gsh='git show'
  242. alias gsi='git submodule init'
  243. alias gsps='git show --pretty=short --show-signature'
  244. alias gsr='git svn rebase'
  245. alias gss='git status -s'
  246. alias gst='git status'
  247. # use the default stash push on git 2.13 and newer
  248. is-at-least 2.13 "$git_version" \
  249. && alias gsta='git stash push' \
  250. || alias gsta='git stash save'
  251. alias gstaa='git stash apply'
  252. alias gstc='git stash clear'
  253. alias gstd='git stash drop'
  254. alias gstl='git stash list'
  255. alias gstp='git stash pop'
  256. alias gsts='git stash show --text'
  257. alias gstu='gsta --include-untracked'
  258. alias gstall='git stash --all'
  259. alias gsu='git submodule update'
  260. alias gsw='git switch'
  261. alias gswc='git switch -c'
  262. alias gswm='git switch $(git_main_branch)'
  263. alias gswd='git switch $(git_develop_branch)'
  264. alias gts='git tag -s'
  265. alias gtv='git tag | sort -V'
  266. alias gtl='gtl(){ git tag --sort=-v:refname -n -l "${1}*" }; noglob gtl'
  267. alias gunignore='git update-index --no-assume-unchanged'
  268. alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'
  269. alias gup='git pull --rebase'
  270. alias gupv='git pull --rebase -v'
  271. alias gupa='git pull --rebase --autostash'
  272. alias gupav='git pull --rebase --autostash -v'
  273. alias glum='git pull upstream $(git_main_branch)'
  274. alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
  275. alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]"'
  276. alias gam='git am'
  277. alias gamc='git am --continue'
  278. alias gams='git am --skip'
  279. alias gama='git am --abort'
  280. alias gamscp='git am --show-current-patch'
  281. function grename() {
  282. if [[ -z "$1" || -z "$2" ]]; then
  283. echo "Usage: $0 old_branch new_branch"
  284. return 1
  285. fi
  286. # Rename branch locally
  287. git branch -m "$1" "$2"
  288. # Rename branch in origin remote
  289. if git push origin :"$1"; then
  290. git push --set-upstream origin "$2"
  291. fi
  292. }
  293. unset git_version