2
0

git.plugin.zsh 14 KB

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