2022-03-31 19:27:58 +02:00
|
|
|
# vim:ft=zsh ts=2 sw=2 sts=2
|
|
|
|
#
|
|
|
|
# Miscellaneous git helpers
|
|
|
|
|
|
|
|
|
|
|
|
# Create a gitignore for the given argument (e.g. python, cpp, etc)
|
|
|
|
function gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Completion helper, from requires __completion_wrapper from `misc.zsh`
|
|
|
|
__git_completion_wrapper() {
|
|
|
|
# Wrapper for `__git_*` completion functions
|
|
|
|
# Can be used to force loading of specialized git completions
|
|
|
|
# When defining custom commands. See below for usage examples.
|
|
|
|
__completion_wrapper $1 _git
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# remove local branches which have been merged into master/main
|
|
|
|
function git-prune-branches(){
|
|
|
|
local main=$(git_main_branch)
|
|
|
|
git branch --merged $main | grep -v '^[ * ]*'$main'$' | xargs git branch -d
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# sort branches by latest commit
|
|
|
|
function git-sort-branch-by-usage(){
|
|
|
|
git for-each-ref --sort=committerdate refs/heads/ --format='%(refname:short)'
|
|
|
|
}
|
|
|
|
|
|
|
|
# switch to a recent branch
|
|
|
|
function git-switch-recent-branch(){
|
|
|
|
git switch $1
|
|
|
|
}
|
|
|
|
# compdef git-sort-branch-by-usage git-switch-recent-branch
|
|
|
|
compdef "__git_completion_wrapper __git_recent_branches" git-switch-recent-branch
|
|
|
|
|
|
|
|
# Runs git log with fzf with preview
|
|
|
|
function fgitlog() {
|
2022-06-21 23:06:21 +02:00
|
|
|
git showtool $(git log --oneline $@ | fzf --multi --preview 'git -p show --color=always {+1}' --preview-window=right,60% | awk '{print $1}')
|
2022-03-31 19:27:58 +02:00
|
|
|
}
|
|
|
|
compdef _git fgitlog=git-log
|
|
|
|
|
2022-06-21 23:06:21 +02:00
|
|
|
function git-commit-show() {
|
2022-03-31 19:27:58 +02:00
|
|
|
git log --graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr" $@ |
|
|
|
|
fzf --ansi --no-sort --reverse --tiebreak=index --preview \
|
|
|
|
'f() {
|
|
|
|
set -- $(echo -- "$@" | grep -o "[a-f0-9]\{7\}")
|
|
|
|
[ $# -eq 0 ] || git show --color=always $1
|
|
|
|
}
|
|
|
|
f {}' \
|
|
|
|
--bind "j:down,k:up,alt-j:preview-down,alt-k:preview-up,ctrl-f:preview-page-down,ctrl-b:preview-page-up,q:abort,ctrl-m:execute:
|
|
|
|
(grep -o '[a-f0-9]\{7\}' | head -1 |
|
|
|
|
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
|
|
|
|
{} FZF-EOF" \
|
|
|
|
--preview-window=right:60%
|
|
|
|
}
|
|
|
|
compdef "__git_completion_wrapper __git_recent_commits" git-commit-show
|
|
|
|
|
2022-06-21 23:06:21 +02:00
|
|
|
function git-fixup() {
|
2022-03-31 19:27:58 +02:00
|
|
|
git commit --fixup=$1
|
|
|
|
git -c sequence.editor=true rebase --interactive --autosquash $1^
|
|
|
|
}
|
|
|
|
compdef "__git_completion_wrapper __git_recent_commits" git-fixup
|
|
|
|
|
2022-06-21 23:06:21 +02:00
|
|
|
function git-diff-branch() {
|
2022-03-31 19:27:58 +02:00
|
|
|
git diff $@
|
|
|
|
}
|
|
|
|
compdef "__git_completion_wrapper __git_branch_names" git-diff-branch
|
2022-06-21 23:06:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
function git-show-changes(){
|
|
|
|
git log --reverse HEAD^..
|
|
|
|
}
|
2022-08-18 14:56:34 +02:00
|
|
|
|
2022-08-18 16:37:24 +02:00
|
|
|
# oh-my-zsh has some aliases which we might want to override
|
|
|
|
# with functions
|
|
|
|
function _disable_alias() {
|
|
|
|
[[ -n $(alias "$1") ]] && unalias "$1"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_disable_alias gco
|
2022-08-18 14:56:34 +02:00
|
|
|
function gco() {
|
|
|
|
if [[ "$#" -ge 1 ]]; then
|
|
|
|
g checkout $@
|
|
|
|
elif [[ "$#" -eq 0 ]]; then
|
|
|
|
g checkout $(gb | fzf)
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:37:24 +02:00
|
|
|
|
|
|
|
_disable_alias gcor
|
2022-08-18 14:56:34 +02:00
|
|
|
function gcor() {
|
|
|
|
if [[ "$#" -ge 1 ]]; then
|
|
|
|
g checkout $@
|
|
|
|
elif [[ "$#" -eq 0 ]]; then
|
|
|
|
g checkout --track $(gbr | fzf)
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:37:24 +02:00
|
|
|
_disable_alias gbd
|
2022-08-18 14:56:34 +02:00
|
|
|
function gbd() {
|
|
|
|
if [[ "$#" -ge 1 ]]; then
|
|
|
|
g branch -d $@
|
|
|
|
elif [[ "$#" -eq 0 ]]; then
|
|
|
|
g branch -d $(gb | fzf -m)
|
|
|
|
fi
|
|
|
|
}
|
2022-12-30 16:01:16 +01:00
|
|
|
compdef "__git_completion_wrapper __git_recent_branches" gbd
|
2022-08-18 14:56:34 +02:00
|
|
|
|
2022-08-18 16:37:24 +02:00
|
|
|
_disable_alias gbD
|
2022-08-18 14:56:34 +02:00
|
|
|
function gbD() {
|
|
|
|
if [[ "$#" -ge 1 ]]; then
|
|
|
|
g branch -D $@
|
|
|
|
elif [[ "$#" -eq 0 ]]; then
|
|
|
|
g branch -D $(gb | fzf -m)
|
|
|
|
fi
|
|
|
|
}
|
2022-12-30 16:01:16 +01:00
|
|
|
compdef "__git_completion_wrapper __git_recent_branches" gbD
|