Move into an external dir
[zsh.git] / .zsh / external / zaw / sources / git-branches.zsh
1 # zaw source for git branch
2
3 function zaw-src-git-branches() {
4 git rev-parse --git-dir >/dev/null 2>&1
5 if [[ $? == 0 ]]; then
6 local branches_list="$(git show-ref | awk ' $2 != "refs/stash" { print $2 }' )"
7 : ${(A)candidates::=${${(f)${branches_list}}#refs/}}
8 : ${(A)cand_descriptions::=${${(f)${branches_list}}#refs/(remotes|heads|tags)/}}
9 fi
10 actions=( \
11 zaw-src-git-branches-checkout \
12 zaw-src-git-branches-simple-checkout \
13 zaw-callback-append-to-buffer \
14 zaw-src-git-branches-merge \
15 zaw-src-git-branches-merge-rebase \
16 zaw-src-git-branches-merge-no-ff \
17 zaw-src-git-branches-merge-to \
18 zaw-src-git-branches-reset \
19 zaw-src-git-branches-rebase \
20 zaw-src-git-branches-rebase-interactive \
21 zaw-src-git-branches-create \
22 zaw-src-git-branches-diff \
23 zaw-src-git-branches-diff-stat \
24 zaw-src-git-branches-reset-hard \
25 zaw-src-git-branches-delete \
26 zaw-src-git-branches-delete-force)
27 act_descriptions=(
28 "check out locally" \
29 "check out" \
30 "append to edit buffer" \
31 "merge" \
32 "merge rebase" \
33 "merge no ff" \
34 "merge to" \
35 "reset" \
36 "rebase" \
37 "rebase interactive from..." \
38 "create new branch from..." \
39 "diff" \
40 "diff statistics" \
41 "reset hard" \
42 "delete" \
43 "delete force")
44 options=()
45 }
46
47 function zaw-src-git-branches-checkout () {
48 local b_type=${1%%/*}
49 local b_name=${1#(heads|remotes|tags)/}
50 case "$b_type" in
51 "heads"|"tags")
52 BUFFER="git checkout $b_name"
53 zle accept-line
54 ;;
55 "remotes")
56 BUFFER="git checkout -t $b_name"
57 zle accept-line
58 ;;
59 esac
60 }
61
62 function zaw-src-git-branches-simple-checkout () {
63 local b_name=${1#(heads|remotes|tags)/}
64 BUFFER="git checkout $b_name"
65 zle accept-line
66 }
67
68 function zaw-src-git-branches-create () {
69 local b_name=${1#(heads|remotes|tags)/}
70 LBUFFER="git checkout -b "
71 RBUFFER=" $b_name"
72 }
73
74 function zaw-src-git-branches-merge () {
75 local b_type=${1%%/*}
76 local b_name=${1#(heads|remotes|tags)/}
77 BUFFER="git merge $b_name"
78 zle accept-line
79 }
80
81 function zaw-src-git-branches-merge-rebase () {
82 local b_type=${1%%/*}
83 local b_name=${1#(heads|remotes|tags)/}
84 BUFFER="git merge --rebase $b_name"
85 zle accept-line
86 }
87
88 function zaw-src-git-branches-merge-no-ff () {
89 local b_type=${1%%/*}
90 local b_name=${1#(heads|remotes|tags)/}
91 BUFFER="git merge --no-ff $b_name"
92 zle accept-line
93 }
94
95 function zaw-src-git-branches-merge-to () {
96 local b_type=${1%%/*}
97 local b_name=${1#(heads|remotes|tags)/}
98 local b_now=${$(git symbolic-ref HEAD)#refs/heads/}
99 if [[ "$b_type" == "heads" ]]; then
100 BUFFER="git checkout $b_name && git merge --no-ff $b_now"
101 zle accept-line
102 fi
103 }
104
105 function zaw-src-git-branches-reset () {
106 local b_type=${1%%/*}
107 local b_name=${1#(heads|remotes|tags)/}
108 BUFFER="git reset $b_name"
109 zle accept-line
110 }
111
112 function zaw-src-git-branches-diff() {
113 local b_name=${1#(heads|remotes|tags)/}
114 BUFFER="git diff $b_name"
115 zle accept-line
116 }
117
118 function zaw-src-git-branches-diff-stat() {
119 local b_name=${1#(heads|remotes|tags)/}
120 BUFFER="git diff --stat $b_name"
121 }
122
123 function zaw-src-git-branches-reset-hard () {
124 local b_type=${1%%/*}
125 local b_name=${1#(heads|remotes|tags)/}
126 BUFFER="git reset --hard $b_name"
127 zle accept-line
128 }
129
130 function zaw-src-git-branches-rebase () {
131 local b_type=${1%%/*}
132 local b_name=${1#(heads|remotes|tags)/}
133 BUFFER="git rebase $b_name"
134 zle accept-line
135 }
136
137 function zaw-src-git-branches-rebase-interactive () {
138 local b_type=${1%%/*}
139 local b_name=${1#(heads|remotes|tags)/}
140 BUFFER="git rebase -i $b_name"
141 zle accept-line
142 }
143
144 function zaw-src-git-branches-delete () {
145 local b_type=${1%%/*}
146 local b_name=${1#(heads|remotes|tags)/}
147 if [[ "$b_type" == "heads" ]] ; then
148 BUFFER="git branch -d $b_name"
149 zle accept-line
150 elif [[ "$b_type" == "remotes" ]] ; then
151 local b_loc=${b_name%%/*}
152 local b_base=${b_name#$b_loc/}
153 BUFFER="git push $b_loc :$b_base"
154 zle accept-line
155 fi
156 }
157
158 function zaw-src-git-branches-delete-force () {
159 local b_type=${1%%/*}
160 local b_name=${1#(heads|remotes|tags)/}
161 if [[ "$b_type" == "heads" ]] ; then
162 BUFFER="git branch -D $b_name"
163 zle accept-line
164 elif [[ "$b_type" == "remotes" ]] ; then
165 local b_loc=${b_name%%/*}
166 local b_base=${b_name#$b_loc/}
167 BUFFER="git push $b_loc :$b_base"
168 zle accept-line
169 fi
170 }
171
172 zaw-register-src -n git-branches zaw-src-git-branches