Skip to content

Instantly share code, notes, and snippets.

@ichizok
Last active November 16, 2022 18:33
Show Gist options
  • Save ichizok/5e7c6a0223546ee58ad188933010553c to your computer and use it in GitHub Desktop.
Save ichizok/5e7c6a0223546ee58ad188933010553c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
if ! command -v fzf &>/dev/null; then
echo >&2 '*** you need install fzf cmd ***'
exit 1
fi
export FZF_DEFAULT_OPTS='--exact --layout=reverse --info=inline'
if [[ -n ${TMUX} ]] && command -v fzf-tmux &>/dev/null; then
fzf() {
fzf-tmux -p -w 100% -h 80%
}
else
export FZF_DEFAULT_OPTS="${FZF_DEFAULT_OPTS} --height=50%"
fi
git-fzf-branch() {
git branch "$@"
}
git-fzf-branch-parse() {
cut -c3-
}
git-fzf-log() {
git log --graph --oneline --decorate=full "$@"
}
git-fzf-log-parse() {
grep -o '[[:xdigit:]]\+' | head -n1
}
git-fzf-reflog() {
git reflog "$@"
}
git-fzf-reflog-parse() {
grep -o '^[[:xdigit:]]\+'
}
git-fzf-status() {
git status --short "$@"
}
git-fzf-status-parse() {
cut -c3-
}
git-fzf-tag() {
git for-each-ref \
--sort='-creatordate' \
--format='%(creatordate:short) %(objectname:short) %(refname:short)' \
refs/tags "$@"
}
git-fzf-tag-parse() {
awk '{print$3}'
}
git-fzf-help() {
cat <<EOT
usage:
$(
for k in "${!cmds[@]}"; do
echo " git-fzf ${k} ${cmds[${k}]}"
done
)
EOT
}
declare -A cmds=(
[help]=''
[branch]='[opts...]'
[log]='[opts...]'
[reflog]='[opts...]'
[status]='[opts...]'
[tag]='[opts...]'
)
cmd=$1
if ! [[ ${cmd} ]] || ! [[ ${cmds[${cmd}]} ]]; then
if [[ ${cmd} ]]; then
echo >&2 "invalid command: ${cmd}"
fi
git-fzf-help >&2
exit 1
fi
shift
"git-fzf-${cmd}" "$@" | fzf | "git-fzf-${cmd}-parse"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment