Skip to content

Instantly share code, notes, and snippets.

@kuy
Created October 2, 2014 04:57
Show Gist options
  • Save kuy/18d27ec12621177637dc to your computer and use it in GitHub Desktop.
Save kuy/18d27ec12621177637dc to your computer and use it in GitHub Desktop.
Detects placeholder in git command and start branch selector using peco/percol
#!/usr/bin/env bash
# NOTE: brought from rbenv https://github.com/sstephenson/rbenv/blob/master/libexec/rbenv-which
expand_path() {
if [ ! -d "$1" ]; then
return 1
fi
local cwd="$(pwd)"
cd "$1"
pwd
cd "$cwd"
}
remove_from_path() {
local path_to_remove="$(expand_path "$1")"
local result=""
if [ -z "$path_to_remove" ]; then
echo "${PATH}"
return
fi
local paths
IFS=: paths=($PATH)
for path in "${paths[@]}"; do
path="$(expand_path "$path" || true)"
if [ -n "$path" ] && [ "$path" != "$path_to_remove" ]; then
result="${result}${path}:"
fi
done
echo "${result%:}"
}
program="${0##*/}"
param="$@"
name=$(echo "$@" | sed -e "s/^[^{]*{\(.\+\)}[^}]*$/\1/")
paths=$(remove_from_path "$HOME/.funl/")
bin_path=$(PATH=$paths which $program)
if [ -z "$bin_path" ]; then
exit 1
fi
if [ "$param" = "$name" ] || [ "$name" != 'branch' ]; then
exec -a "$program" "$bin_path" "$@"
else
sel=$(git for-each-ref --sort=-committerdate refs/heads/ | cut -f2 | sed 's/refs\/heads\///g' | peco | xargs echo)
arg="s|{$name}|$sel|"
cmd=$(echo "$@" | sed "$arg")
exec -a "$program" $bin_path $cmd
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment