Created
September 28, 2011 03:49
-
-
Save iwata/1246944 to your computer and use it in GitHub Desktop.
branch名指定でpullするshell function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#compdef gpull | |
# | |
# gpull ZSH completion function | |
# | |
# Installation | |
# ------------ | |
# Drop this somewhere in your $fpath (like /usr/share/zsh/site-functions) | |
# and rename to '_gpull' | |
# | |
__git_branch_names () { | |
local expl | |
declare -a branch_names | |
branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads 2>/dev/null)"}#refs/heads/}) | |
__git_command_successful || return | |
_wanted branch-names expl branch-name compadd $* - $branch_names | |
} | |
__git_command_successful () { | |
if (( ${#pipestatus:#0} > 0 )); then | |
_message 'not a git repository' | |
return 1 | |
fi | |
return 0 | |
} | |
local curcontext="$curcontext" state line | |
typeset -A opt_args | |
_arguments ':branch-name:__git_branch_names' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gpull() { | |
[[ $# -ne 1 ]] && echo 'Required a branch name' >&2 && return | |
local branch=$1 | |
local current=$(git branch -l | grep "*" | cut -d " " -f 2) | |
if [ "$branch" = "$current" ]; then | |
git pull | |
else | |
git checkout -q "$branch" | |
echo "Switched to branch '$branch'" | |
git pull | |
git checkout -q "$current" | |
echo "Switched to branch '$current'" | |
fi | |
} | |
gsync() { | |
local d_opt='-D' | |
local is_confirm=false | |
local is_exclude_current=false | |
if [ $# -gt 0 ]; then | |
# args loop | |
for arg in "$@" | |
do | |
if [ $arg = "--exclude-current-branch" ] || [ $arg = "-e" ]; then | |
is_exclude_current=true | |
local current=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g') | |
elif [ $arg = "-d" ]; then | |
d_opt='-d' | |
#elif [ $arg = "-c"] || [ $arg = "--confirm" ]; then | |
#is_confirm=true | |
fi | |
done | |
fi | |
echo 'fetch from all remote repositories...' | |
git fetch --quiet --all || exit 1 | |
echo 'prune remotes...' | |
git remote |xargs git remote prune || exit 1 | |
local remote_branches=$(git branch -r --no-color |sed 's/^[* ] //' |grep -v 'HEAD' |sed 's/^[^\/]\+\///' \ | |
|sed 's/\//\\\//g' |perl -pe 's/\n/\\|/' |sed 's/\\|$//') | |
local remove_branches=$(git branch --no-color |sed 's/^[* ] //' |grep -v "^\($remote_branches\)") | |
[ $is_exclude_current ] && remove_branches=$(echo $remove_branches |grep -v $current) | |
if [ -z $remove_branches ]; then | |
echo -n 'no local branches should remove' | |
else | |
echo "follow branches remove:" | |
echo $remove_branches | |
echo -n 'remove these branches ok?[y/n] ' | |
read confirm | |
[ ! $is_exclude_current ] && echo 'switch to master' && git checkout -q master | |
$(expr "$confirm" : '^[yY]' > /dev/null) && echo $remove_branches |tr '\n' ' ' |git branch $d_opt | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment