git.plugin.zsh 13 KB

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