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