mirror of
https://git.decapod.one/brethil/dotfiles
synced 2024-11-21 19:11:30 +01:00
zsh: extract git functions to git.zsh
This commit is contained in:
parent
21d9660e64
commit
0c95eef23a
71
functions/git.zsh
Normal file
71
functions/git.zsh
Normal file
|
@ -0,0 +1,71 @@
|
|||
# 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() {
|
||||
git log --oneline $@ | fzf --multi --preview 'git -p show --color=always {+1}' --preview-window=right,60%
|
||||
}
|
||||
compdef _git fgitlog=git-log
|
||||
|
||||
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
|
||||
|
||||
git-fixup() {
|
||||
git commit --fixup=$1
|
||||
git -c sequence.editor=true rebase --interactive --autosquash $1^
|
||||
}
|
||||
compdef "__git_completion_wrapper __git_recent_commits" git-fixup
|
||||
|
||||
git-diff-branch() {
|
||||
git diff $@
|
||||
}
|
||||
compdef "__git_completion_wrapper __git_branch_names" git-diff-branch
|
|
@ -228,12 +228,6 @@ function unzipd {
|
|||
unzip -d "$name" "$zip_file"
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
fvim() {
|
||||
if [[ -n "$@" ]]; then
|
||||
vim `fzf -q $@`
|
||||
|
@ -249,8 +243,6 @@ function retry() {
|
|||
until $@; do sleep $_retry_interval; done
|
||||
}
|
||||
|
||||
# Creats a gitignore for the given argument (e.g. python, cpp, etc)
|
||||
function gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@; }
|
||||
|
||||
|
||||
__completion_wrapper(){
|
||||
|
@ -258,8 +250,6 @@ __completion_wrapper(){
|
|||
# These can be used to force loading of all completions
|
||||
# for the given command in order to access specialized
|
||||
# completion functions
|
||||
#
|
||||
# See `__git_completion_wrapper()` below for usage
|
||||
local _completion_function=$1
|
||||
local _completion_base=$2
|
||||
if ! command -v $_completion_function; then
|
||||
|
@ -268,58 +258,6 @@ __completion_wrapper(){
|
|||
$_completion_function
|
||||
}
|
||||
|
||||
__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
|
||||
}
|
||||
|
||||
|
||||
# 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() {
|
||||
git log --oneline $@ | fzf --multi --preview 'git -p show --color=always {+1}' --preview-window=right,60%
|
||||
}
|
||||
compdef _git fgitlog=git-log
|
||||
|
||||
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
|
||||
|
||||
git-fixup() {
|
||||
git commit --fixup=$1
|
||||
git -c sequence.editor=true rebase --interactive --autosquash $1^
|
||||
}
|
||||
compdef "__git_completion_wrapper __git_recent_commits" git-fixup
|
||||
|
||||
git-diff-branch() {
|
||||
git diff $@
|
||||
}
|
||||
compdef "__git_completion_wrapper __git_branch_names" git-diff-branch
|
||||
|
||||
if command -v pacman &>/dev/null ; then
|
||||
pacbins() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user