Add zsh anything.el plugin
[zsh.git] / .zsh / zaw / sources / git-recent-branches.zsh
1 # zaw source for git branches sorted by commit date
2
3 function zaw-src-git-recent-branches () {
4 git rev-parse --git-dir >/dev/null 2>&1
5 if [[ $? == 0 ]]; then
6 local branches="$(git for-each-ref --format='%(refname)' --sort=-committerdate refs/heads)"
7 : ${(A)candidates::=${${(f)${branches}}#refs/}}
8 : ${(A)cand_descriptions::=${${(f)${branches}}#refs/(remotes|heads|tags)/}}
9 fi
10
11 actions=( \
12 zaw-src-git-branches-checkout \
13 zaw-src-git-branches-simple-checkout \
14 zaw-callback-append-to-buffer \
15 zaw-src-git-branches-merge \
16 zaw-src-git-branches-merge-rebase \
17 zaw-src-git-branches-merge-no-ff \
18 zaw-src-git-branches-diff \
19 zaw-src-git-branches-diff-stat \
20 zaw-src-git-branches-reset \
21 zaw-src-git-branches-rebase \
22 zaw-src-git-branches-rebase-interactive \
23 zaw-src-git-branches-create \
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 "diff" \
35 "diff stat" \
36 "reset" \
37 "rebase" \
38 "rebase interactive from..." \
39 "create new branch from..." \
40 "reset hard" \
41 "delete" \
42 "delete force")
43 options=()
44 }
45
46 function zaw-src-git-recent-all-branches () {
47 git rev-parse --git-dir >/dev/null 2>&1
48 if [[ $? == 0 ]]; then
49 local branches="$(git for-each-ref --format='%(refname)' --sort=-committerdate refs/heads refs/remotes)"
50 : ${(A)candidates::=${${(f)${branches}}#refs/}}
51 : ${(A)cand_descriptions::=${${(f)${branches}}#refs/(remotes|heads|tags)/}}
52 fi
53
54 actions=( \
55 zaw-src-git-branches-checkout \
56 zaw-src-git-branches-simple-checkout \
57 zaw-callback-append-to-buffer \
58 zaw-src-git-branches-merge \
59 zaw-src-git-branches-merge-rebase \
60 zaw-src-git-branches-merge-no-ff \
61 zaw-src-git-branches-diff \
62 zaw-src-git-branches-diff-stat \
63 zaw-src-git-branches-reset \
64 zaw-src-git-branches-rebase \
65 zaw-src-git-branches-rebase-interactive \
66 zaw-src-git-branches-create \
67 zaw-src-git-branches-reset-hard \
68 zaw-src-git-branches-delete \
69 zaw-src-git-branches-delete-force)
70 act_descriptions=(
71 "check out locally" \
72 "check out" \
73 "append to edit buffer" \
74 "merge" \
75 "merge rebase" \
76 "merge no ff" \
77 "diff" \
78 "diff stat" \
79 "reset" \
80 "rebase" \
81 "rebase interactive from..." \
82 "create new branch from..." \
83 "reset hard" \
84 "delete" \
85 "delete force")
86 options=()
87 }
88
89 function zaw-src-git-recent-branches-checkout () {
90 local b_type=${1%%/*}
91 local b_name=${1#(heads|remotes|tags)/}
92 case "$b_type" in
93 "heads"|"tags")
94 BUFFER="git checkout $b_name"
95 zle accept-line
96 ;;
97 "remotes")
98 BUFFER="git checkout -t $b_name"
99 zle accept-line
100 ;;
101 esac
102 }
103
104 function zaw-src-git-branches-diff() {
105 local b_name=${1#(heads|remotes|tags)/}
106 BUFFER="git diff $b_name"
107 zle accept-line
108 }
109
110 function zaw-src-git-branches-diff-stat() {
111 local b_name=${1#(heads|remotes|tags)/}
112 BUFFER="git diff --stat $b_name"
113 zle accept-line
114 }
115
116 zaw-register-src -n git-recent-branches zaw-src-git-recent-branches
117 zaw-register-src -n git-recent-all-branches zaw-src-git-recent-all-branches