-
Notifications
You must be signed in to change notification settings - Fork 3
/
branch.plugin.zsh
145 lines (142 loc) · 4.51 KB
/
branch.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#
# Command to list branches, create a new branch or checkout to another existing branch. Examples:
# type branch -h for help
#
function branch() {
case $1 in
(help | -h | --help)
print "Wrapper for git branch management. Usage:
branch help #=> print this help
branch #=> list local branches (same as branch -l or branch --list)
branch current #=> display current branch
branch [tab] #=> autocomplete with local branches
branch <branch> #=> change current branch to <branch>
branch checkout <branch> #=> change current branch to <branch>
branch remotes #=> list remote branches (same as branch -r or branch --remotes)
branch all #=> list all branches (same as branch -a or branch --all)
branch set-upstream <b> #=> set upstream branch for <b> to origin/<b>
branch unset-upstream [<b>] #=> unset upstream branch for <b> or current branch
branch new <branch> #=> create and checkout <branch>
branch push [<branch>] #=> push <branch> [default current branch] commits to origin
branch pull [<branch>] #=> pull <branch> [default current branch] from origin
branch pullrebase [<b>] #=> same as pull, but rebase local commits on top of origin (git pull --rebase)
branch rm <branches...> #=> removes both local and remote versions of the listed branches
branch rmlocal <branches> #=> removes only local versions of the listed branches
branch RM <branches...> #=> same as rm but forces removal of local branch, even if is not fully merged
"
;;
(all | -a | --all)
git --no-pager branch -a
;;
('' | -l | --list)
git --no-pager branch
;;
(current)
git branch --show-current
;;
(move)
if [[ -z $2 ]]; then
print "error: please specify branch to rename"
else
printdo git branch -m $2
fi
;;
(MOVE)
if [[ -z $2 ]]; then
print "error: please specify branch to rename"
else
printdo git branch -M $2
fi
;;
(new)
if [[ -z $2 ]]; then
print "error: please specify branch to create"
else
printdo git checkout -b $2
fi
;;
(push)
local branch_to_push=$2; : ${branch_to_push:=$(branch current)}
printdo git push origin $branch_to_push
;;
(pull)
local branch_to_pull=$2; : ${branch_to_pull:=$(branch current)}
printdo git pull origin $branch_to_pull
;;
(pullrebase)
local branch_to_pull=$2; : ${branch_to_pull:=$(branch current)}
printdo git pull --rebase origin $branch_to_pull
;;
(remotes)
git --no-pager branch -r
;;
(rmlocal)
if [[ -z $2 ]]; then
print "error: please specify branch to remove"
else
for branch_to_remove; do
if [[ $branch_to_remove != "rmlocal" ]]; then
printdo git branch -d $branch_to_remove
fi
done
fi
;;
(rm)
if [[ -z $2 ]]; then
print "error: please specify branch to remove"
else
for branch_to_remove; do
if [[ $branch_to_remove != "rm" ]]; then
printdo git branch -d $branch_to_remove
printdo git push origin :$branch_to_remove
fi
done
fi
;;
(RM)
if [[ -z $2 ]]; then
print "error: please specify branch to remove"
else
for branch_to_remove; do
if [[ $branch_to_remove != "RM" ]]; then
printdo git branch -D $branch_to_remove
printdo git push origin :$branch_to_remove
fi
done
fi
;;
(set-upstream)
if [[ -z $2 ]]; then
print "error: please specify branch to set upstream"
else
printdo git branch --set-upstream-to=origin/$2
fi
;;
(unset-upstream)
if [[ -z $2 ]]; then
printdo git branch --unset-upstream
else
printdo git branch --unset-upstream $2
fi
;;
(checkout)
printdo git checkout $2
;;
(*)
printdo git checkout $1
;;
esac
}
#
# Prints a command and then execute it
#
function printdo() {
print "\033[1;35m> $*\033[0m"
$*
}
# Autocomplete for 'branch'
compdef _git branch=git-branch
# # Before each alias, print the original command
# for alias in ${(ok)aliases}; do
# aliases[$alias]="print -P \"\n%F{yellow} %F{green}${aliases[$alias]}\n%f\"; ${aliases[$alias]}"
# done