mirror of
https://git.decapod.one/brethil/dotfiles
synced 2024-11-23 12:01:30 +01:00
126 lines
3.3 KiB
Bash
126 lines
3.3 KiB
Bash
# 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_completion_wrapper __git_recent_branches" git-switch-recent-branch
|
|
|
|
function git-commit-show() {
|
|
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
|
|
|
|
function git-fixup() {
|
|
git commit --fixup=$1
|
|
git -c sequence.editor=true rebase --interactive --autosquash $1^
|
|
}
|
|
compdef "__git_completion_wrapper __git_recent_commits" git-fixup
|
|
|
|
function git-diff-branch() {
|
|
git diff $@
|
|
}
|
|
compdef "__git_completion_wrapper __git_branch_names" git-diff-branch
|
|
|
|
|
|
function git-show-changes(){
|
|
git log --reverse HEAD^..
|
|
}
|
|
|
|
# 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
|
|
function gco() {
|
|
if [[ "$#" -ge 1 ]]; then
|
|
g checkout $@
|
|
elif [[ "$#" -eq 0 ]]; then
|
|
local selected
|
|
selected=$(git branch --format "%(refname)" | cut -d / -f 3- | fzf)
|
|
if [[ -n $selected ]]; then
|
|
git checkout "$selected"
|
|
else
|
|
echo "Nothing selected"
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
compdef "__git_completion_wrapper _git-checkout" gco
|
|
|
|
|
|
_disable_alias gcor
|
|
function gcor() {
|
|
if [[ "$#" -ge 1 ]]; then
|
|
git checkout $@
|
|
elif [[ "$#" -eq 0 ]]; then
|
|
git checkout --track $(gbr | fzf)
|
|
fi
|
|
|
|
}
|
|
|
|
_disable_alias gbd
|
|
function gbd() {
|
|
if [[ "$#" -ge 1 ]]; then
|
|
g branch -d $@
|
|
elif [[ "$#" -eq 0 ]]; then
|
|
g branch -d $(gb | fzf -m)
|
|
fi
|
|
}
|
|
compdef "__git_completion_wrapper __git_recent_branches" gbd
|
|
|
|
_disable_alias gbD
|
|
function gbD() {
|
|
if [[ "$#" -ge 1 ]]; then
|
|
git branch -D $@
|
|
elif [[ "$#" -eq 0 ]]; then
|
|
git branch -D $(git branch | fzf -m)
|
|
fi
|
|
}
|
|
compdef "__git_completion_wrapper __git_recent_branches" gbD
|