git.plugin.zsh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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,stable,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\]" | cut -c 3- | awk '"'"'{print $1}'"'"' | xargs git branch -d'
  131. alias gbgD='LANG=C git branch --no-color -vv | grep ": gone\]" | cut -c 3- | 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. alias gclf='git clone --recursive --shallow-submodules --filter=blob:none --also-filter-submodules'
  149. function gccd() {
  150. setopt localoptions extendedglob
  151. # get repo URI from args based on valid formats: https://git-scm.com/docs/git-clone#URLS
  152. local repo="${${@[(r)(ssh://*|git://*|ftp(s)#://*|http(s)#://*|*@*)(.git/#)#]}:-$_}"
  153. # clone repository and exit if it fails
  154. command git clone --recurse-submodules "$@" || return
  155. # if last arg passed was a directory, that's where the repo was cloned
  156. # otherwise parse the repo URI and use the last part as the directory
  157. [[ -d "$_" ]] && cd "$_" || cd "${${repo:t}%.git/#}"
  158. }
  159. compdef _git gccd=git-clone
  160. alias gcam='git commit --all --message'
  161. alias gcas='git commit --all --signoff'
  162. alias gcasm='git commit --all --signoff --message'
  163. alias gcs='git commit --gpg-sign'
  164. alias gcss='git commit --gpg-sign --signoff'
  165. alias gcssm='git commit --gpg-sign --signoff --message'
  166. alias gcmsg='git commit --message'
  167. alias gcsm='git commit --signoff --message'
  168. alias gc='git commit --verbose'
  169. alias gca='git commit --verbose --all'
  170. alias gca!='git commit --verbose --all --amend'
  171. alias gcan!='git commit --verbose --all --no-edit --amend'
  172. alias gcans!='git commit --verbose --all --signoff --no-edit --amend'
  173. alias gcann!='git commit --verbose --all --date=now --no-edit --amend'
  174. alias gc!='git commit --verbose --amend'
  175. alias gcn='git commit --verbose --no-edit'
  176. alias gcn!='git commit --verbose --no-edit --amend'
  177. alias gcf='git config --list'
  178. alias gcfu='git commit --fixup'
  179. alias gdct='git describe --tags $(git rev-list --tags --max-count=1)'
  180. alias gd='git diff'
  181. alias gdca='git diff --cached'
  182. alias gdcw='git diff --cached --word-diff'
  183. alias gds='git diff --staged'
  184. alias gdw='git diff --word-diff'
  185. function gdv() { git diff -w "$@" | view - }
  186. compdef _git gdv=git-diff
  187. alias gdup='git diff @{upstream}'
  188. function gdnolock() {
  189. git diff "$@" ":(exclude)package-lock.json" ":(exclude)*.lock"
  190. }
  191. compdef _git gdnolock=git-diff
  192. alias gdt='git diff-tree --no-commit-id --name-only -r'
  193. alias gf='git fetch'
  194. # --jobs=<n> was added in git 2.8
  195. is-at-least 2.8 "$git_version" \
  196. && alias gfa='git fetch --all --tags --prune --jobs=10' \
  197. || alias gfa='git fetch --all --tags --prune'
  198. alias gfo='git fetch origin'
  199. alias gg='git gui citool'
  200. alias gga='git gui citool --amend'
  201. alias ghh='git help'
  202. alias glgg='git log --graph'
  203. alias glgga='git log --graph --decorate --all'
  204. alias glgm='git log --graph --max-count=10'
  205. alias glods='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset" --date=short'
  206. alias glod='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset"'
  207. alias glola='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset" --all'
  208. alias glols='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset" --stat'
  209. alias glol='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset"'
  210. alias glo='git log --oneline --decorate'
  211. alias glog='git log --oneline --decorate --graph'
  212. alias gloga='git log --oneline --decorate --graph --all'
  213. # Pretty log messages
  214. function _git_log_prettily(){
  215. if ! [ -z $1 ]; then
  216. git log --pretty=$1
  217. fi
  218. }
  219. compdef _git _git_log_prettily=git-log
  220. alias glp='_git_log_prettily'
  221. alias glg='git log --stat'
  222. alias glgp='git log --stat --patch'
  223. alias gignored='git ls-files -v | grep "^[[:lower:]]"'
  224. alias gfg='git ls-files | grep'
  225. alias gm='git merge'
  226. alias gma='git merge --abort'
  227. alias gmc='git merge --continue'
  228. alias gms="git merge --squash"
  229. alias gmff="git merge --ff-only"
  230. alias gmom='git merge origin/$(git_main_branch)'
  231. alias gmum='git merge upstream/$(git_main_branch)'
  232. alias gmtl='git mergetool --no-prompt'
  233. alias gmtlvim='git mergetool --no-prompt --tool=vimdiff'
  234. alias gl='git pull'
  235. alias gpr='git pull --rebase'
  236. alias gprv='git pull --rebase -v'
  237. alias gpra='git pull --rebase --autostash'
  238. alias gprav='git pull --rebase --autostash -v'
  239. function ggu() {
  240. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  241. git pull --rebase origin "${b:=$1}"
  242. }
  243. compdef _git ggu=git-checkout
  244. alias gprom='git pull --rebase origin $(git_main_branch)'
  245. alias gpromi='git pull --rebase=interactive origin $(git_main_branch)'
  246. alias gprum='git pull --rebase upstream $(git_main_branch)'
  247. alias gprumi='git pull --rebase=interactive upstream $(git_main_branch)'
  248. alias ggpull='git pull origin "$(git_current_branch)"'
  249. function ggl() {
  250. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  251. git pull origin "${*}"
  252. else
  253. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  254. git pull origin "${b:=$1}"
  255. fi
  256. }
  257. compdef _git ggl=git-checkout
  258. alias gluc='git pull upstream $(git_current_branch)'
  259. alias glum='git pull upstream $(git_main_branch)'
  260. alias gp='git push'
  261. alias gpd='git push --dry-run'
  262. function ggf() {
  263. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  264. git push --force origin "${b:=$1}"
  265. }
  266. compdef _git ggf=git-checkout
  267. alias gpf!='git push --force'
  268. is-at-least 2.30 "$git_version" \
  269. && alias gpf='git push --force-with-lease --force-if-includes' \
  270. || alias gpf='git push --force-with-lease'
  271. function ggfl() {
  272. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  273. git push --force-with-lease origin "${b:=$1}"
  274. }
  275. compdef _git ggfl=git-checkout
  276. alias gpsup='git push --set-upstream origin $(git_current_branch)'
  277. is-at-least 2.30 "$git_version" \
  278. && alias gpsupf='git push --set-upstream origin $(git_current_branch) --force-with-lease --force-if-includes' \
  279. || alias gpsupf='git push --set-upstream origin $(git_current_branch) --force-with-lease'
  280. alias gpv='git push --verbose'
  281. alias gpoat='git push origin --all && git push origin --tags'
  282. alias gpod='git push origin --delete'
  283. alias ggpush='git push origin "$(git_current_branch)"'
  284. function ggp() {
  285. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  286. git push origin "${*}"
  287. else
  288. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  289. git push origin "${b:=$1}"
  290. fi
  291. }
  292. compdef _git ggp=git-checkout
  293. alias gpu='git push upstream'
  294. alias grb='git rebase'
  295. alias grba='git rebase --abort'
  296. alias grbc='git rebase --continue'
  297. alias grbi='git rebase --interactive'
  298. alias grbo='git rebase --onto'
  299. alias grbs='git rebase --skip'
  300. alias grbd='git rebase $(git_develop_branch)'
  301. alias grbm='git rebase $(git_main_branch)'
  302. alias grbom='git rebase origin/$(git_main_branch)'
  303. alias grbum='git rebase upstream/$(git_main_branch)'
  304. alias grf='git reflog'
  305. alias gr='git remote'
  306. alias grv='git remote --verbose'
  307. alias gra='git remote add'
  308. alias grrm='git remote remove'
  309. alias grmv='git remote rename'
  310. alias grset='git remote set-url'
  311. alias grup='git remote update'
  312. alias grh='git reset'
  313. alias gru='git reset --'
  314. alias grhh='git reset --hard'
  315. alias grhk='git reset --keep'
  316. alias grhs='git reset --soft'
  317. alias gpristine='git reset --hard && git clean --force -dfx'
  318. alias gwipe='git reset --hard && git clean --force -df'
  319. alias groh='git reset origin/$(git_current_branch) --hard'
  320. alias grs='git restore'
  321. alias grss='git restore --source'
  322. alias grst='git restore --staged'
  323. alias gunwip='git rev-list --max-count=1 --format="%s" HEAD | grep -q "\--wip--" && git reset HEAD~1'
  324. alias grev='git revert'
  325. alias greva='git revert --abort'
  326. alias grevc='git revert --continue'
  327. alias grm='git rm'
  328. alias grmc='git rm --cached'
  329. alias gcount='git shortlog --summary --numbered'
  330. alias gsh='git show'
  331. alias gsps='git show --pretty=short --show-signature'
  332. alias gstall='git stash --all'
  333. alias gstaa='git stash apply'
  334. alias gstc='git stash clear'
  335. alias gstd='git stash drop'
  336. alias gstl='git stash list'
  337. alias gstp='git stash pop'
  338. # use the default stash push on git 2.13 and newer
  339. is-at-least 2.13 "$git_version" \
  340. && alias gsta='git stash push' \
  341. || alias gsta='git stash save'
  342. alias gsts='git stash show --patch'
  343. alias gst='git status'
  344. alias gss='git status --short'
  345. alias gsb='git status --short --branch'
  346. alias gsi='git submodule init'
  347. alias gsu='git submodule update'
  348. alias gsd='git svn dcommit'
  349. alias git-svn-dcommit-push='git svn dcommit && git push github $(git_main_branch):svntrunk'
  350. alias gsr='git svn rebase'
  351. alias gsw='git switch'
  352. alias gswc='git switch --create'
  353. alias gswd='git switch $(git_develop_branch)'
  354. alias gswm='git switch $(git_main_branch)'
  355. alias gta='git tag --annotate'
  356. alias gts='git tag --sign'
  357. alias gtv='git tag | sort -V'
  358. alias gignore='git update-index --assume-unchanged'
  359. alias gunignore='git update-index --no-assume-unchanged'
  360. alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
  361. alias gwt='git worktree'
  362. alias gwta='git worktree add'
  363. alias gwtls='git worktree list'
  364. alias gwtmv='git worktree move'
  365. alias gwtrm='git worktree remove'
  366. alias gstu='gsta --include-untracked'
  367. alias gtl='gtl(){ git tag --sort=-v:refname -n --list "${1}*" }; noglob gtl'
  368. alias gk='\gitk --all --branches &!'
  369. alias gke='\gitk --all $(git log --walk-reflogs --pretty=%h) &!'
  370. unset git_version
  371. # Logic for adding warnings on deprecated aliases
  372. local old_alias new_alias
  373. for old_alias new_alias (
  374. # TODO(2023-10-19): remove deprecated `git pull --rebase` aliases
  375. gup gpr
  376. gupv gprv
  377. gupa gpra
  378. gupav gprav
  379. gupom gprom
  380. gupomi gpromi
  381. ); do
  382. aliases[$old_alias]="
  383. 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\"
  384. $new_alias"
  385. done
  386. unset old_alias new_alias