git.plugin.zsh 14 KB

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