Show More
@@ -1,173 +1,171 b'' | |||||
1 | #!/bin/bash |
|
|||
2 |
|
||||
3 |
|
|
1 | shopt -s extglob | |
4 |
|
2 | |||
5 | _hg_commands() |
|
3 | _hg_commands() | |
6 | { |
|
4 | { | |
7 | local commands="$(hg -v help | sed -e '1,/^list of commands:/d' \ |
|
5 | local commands="$(hg -v help | sed -e '1,/^list of commands:/d' \ | |
8 | -e '/^global options:/,$d' \ |
|
6 | -e '/^global options:/,$d' \ | |
9 | -e '/^ [^ ]/!d; s/[,:]//g;')" |
|
7 | -e '/^ [^ ]/!d; s/[,:]//g;')" | |
10 |
|
8 | |||
11 | # hide debug commands from users, but complete them if |
|
9 | # hide debug commands from users, but complete them if | |
12 | # specifically asked for |
|
10 | # specifically asked for | |
13 | if [[ "$cur" == de* ]]; then |
|
11 | if [[ "$cur" == de* ]]; then | |
14 | commands="$commands debugcheckstate debugstate debugindex" |
|
12 | commands="$commands debugcheckstate debugstate debugindex" | |
15 | commands="$commands debugindexdot debugwalk debugdata" |
|
13 | commands="$commands debugindexdot debugwalk debugdata" | |
16 | commands="$commands debugancestor debugconfig debugrename" |
|
14 | commands="$commands debugancestor debugconfig debugrename" | |
17 | fi |
|
15 | fi | |
18 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$commands" -- "$cur") ) |
|
16 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$commands" -- "$cur") ) | |
19 | } |
|
17 | } | |
20 |
|
18 | |||
21 | _hg_paths() |
|
19 | _hg_paths() | |
22 | { |
|
20 | { | |
23 | local paths="$(hg paths | sed -e 's/ = .*$//')" |
|
21 | local paths="$(hg paths | sed -e 's/ = .*$//')" | |
24 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" )) |
|
22 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" )) | |
25 | } |
|
23 | } | |
26 |
|
24 | |||
27 | _hg_status() |
|
25 | _hg_status() | |
28 | { |
|
26 | { | |
29 | local files="$( hg status -$1 | cut -b 3- )" |
|
27 | local files="$( hg status -$1 | cut -b 3- )" | |
30 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$files" -- "$cur" )) |
|
28 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$files" -- "$cur" )) | |
31 | } |
|
29 | } | |
32 |
|
30 | |||
33 | _hg_tags() |
|
31 | _hg_tags() | |
34 | { |
|
32 | { | |
35 | local tags="$(hg tags | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')" |
|
33 | local tags="$(hg tags | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')" | |
36 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") ) |
|
34 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") ) | |
37 | } |
|
35 | } | |
38 |
|
36 | |||
39 | # this is "kind of" ugly... |
|
37 | # this is "kind of" ugly... | |
40 | _hg_count_non_option() |
|
38 | _hg_count_non_option() | |
41 | { |
|
39 | { | |
42 | local i count=0 |
|
40 | local i count=0 | |
43 | local filters="$1" |
|
41 | local filters="$1" | |
44 |
|
42 | |||
45 | for (( i=1; $i<=$COMP_CWORD; i++ )); do |
|
43 | for (( i=1; $i<=$COMP_CWORD; i++ )); do | |
46 | if [[ "${COMP_WORDS[i]}" != -* ]]; then |
|
44 | if [[ "${COMP_WORDS[i]}" != -* ]]; then | |
47 | if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then |
|
45 | if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then | |
48 | continue |
|
46 | continue | |
49 | fi |
|
47 | fi | |
50 | count=$(($count + 1)) |
|
48 | count=$(($count + 1)) | |
51 | fi |
|
49 | fi | |
52 | done |
|
50 | done | |
53 |
|
51 | |||
54 | echo $(($count - 1)) |
|
52 | echo $(($count - 1)) | |
55 | } |
|
53 | } | |
56 |
|
54 | |||
57 | _hg() |
|
55 | _hg() | |
58 | { |
|
56 | { | |
59 | local cur prev cmd opts i |
|
57 | local cur prev cmd opts i | |
60 | # global options that receive an argument |
|
58 | # global options that receive an argument | |
61 | local global_args='--cwd|-R|--repository' |
|
59 | local global_args='--cwd|-R|--repository' | |
62 |
|
60 | |||
63 | COMPREPLY=() |
|
61 | COMPREPLY=() | |
64 | cur="$2" |
|
62 | cur="$2" | |
65 | prev="$3" |
|
63 | prev="$3" | |
66 |
|
64 | |||
67 | # searching for the command |
|
65 | # searching for the command | |
68 | # (first non-option argument that doesn't follow a global option that |
|
66 | # (first non-option argument that doesn't follow a global option that | |
69 | # receives an argument) |
|
67 | # receives an argument) | |
70 | for (( i=1; $i<=$COMP_CWORD; i++ )); do |
|
68 | for (( i=1; $i<=$COMP_CWORD; i++ )); do | |
71 | if [[ ${COMP_WORDS[i]} != -* ]]; then |
|
69 | if [[ ${COMP_WORDS[i]} != -* ]]; then | |
72 | if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then |
|
70 | if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then | |
73 | cmd="${COMP_WORDS[i]}" |
|
71 | cmd="${COMP_WORDS[i]}" | |
74 | break |
|
72 | break | |
75 | fi |
|
73 | fi | |
76 | fi |
|
74 | fi | |
77 | done |
|
75 | done | |
78 |
|
76 | |||
79 | if [[ "$cur" == -* ]]; then |
|
77 | if [[ "$cur" == -* ]]; then | |
80 | # this assumes that there are no commands with spaces in the name |
|
78 | # this assumes that there are no commands with spaces in the name | |
81 | opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//') |
|
79 | opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//') | |
82 |
|
80 | |||
83 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") ) |
|
81 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") ) | |
84 | return |
|
82 | return | |
85 | fi |
|
83 | fi | |
86 |
|
84 | |||
87 | # global options |
|
85 | # global options | |
88 | case "$prev" in |
|
86 | case "$prev" in | |
89 | -R|--repository) |
|
87 | -R|--repository) | |
90 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" )) |
|
88 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" )) | |
91 | return |
|
89 | return | |
92 | ;; |
|
90 | ;; | |
93 | --cwd) |
|
91 | --cwd) | |
94 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" )) |
|
92 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" )) | |
95 | return |
|
93 | return | |
96 | ;; |
|
94 | ;; | |
97 | esac |
|
95 | esac | |
98 |
|
96 | |||
99 | if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then |
|
97 | if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then | |
100 | _hg_commands |
|
98 | _hg_commands | |
101 | return |
|
99 | return | |
102 | fi |
|
100 | fi | |
103 |
|
101 | |||
104 | # canonicalize command name |
|
102 | # canonicalize command name | |
105 | cmd=$(hg -q help "$cmd" | sed -e 's/^hg //; s/ .*//; 1q') |
|
103 | cmd=$(hg -q help "$cmd" | sed -e 's/^hg //; s/ .*//; 1q') | |
106 |
|
104 | |||
107 | if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then |
|
105 | if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then | |
108 | _hg_tags |
|
106 | _hg_tags | |
109 | return |
|
107 | return | |
110 | fi |
|
108 | fi | |
111 |
|
109 | |||
112 | case "$cmd" in |
|
110 | case "$cmd" in | |
113 | help) |
|
111 | help) | |
114 | _hg_commands |
|
112 | _hg_commands | |
115 | ;; |
|
113 | ;; | |
116 | export|manifest|update) |
|
114 | export|manifest|update) | |
117 | _hg_tags |
|
115 | _hg_tags | |
118 | ;; |
|
116 | ;; | |
119 | pull|push|outgoing|incoming) |
|
117 | pull|push|outgoing|incoming) | |
120 | _hg_paths |
|
118 | _hg_paths | |
121 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" )) |
|
119 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" )) | |
122 | ;; |
|
120 | ;; | |
123 | paths) |
|
121 | paths) | |
124 | _hg_paths |
|
122 | _hg_paths | |
125 | ;; |
|
123 | ;; | |
126 | add) |
|
124 | add) | |
127 | _hg_status "u" |
|
125 | _hg_status "u" | |
128 | ;; |
|
126 | ;; | |
129 | commit) |
|
127 | commit) | |
130 | _hg_status "mra" |
|
128 | _hg_status "mra" | |
131 | ;; |
|
129 | ;; | |
132 | remove) |
|
130 | remove) | |
133 | _hg_status "r" |
|
131 | _hg_status "r" | |
134 | ;; |
|
132 | ;; | |
135 | forget) |
|
133 | forget) | |
136 | _hg_status "a" |
|
134 | _hg_status "a" | |
137 | ;; |
|
135 | ;; | |
138 | diff) |
|
136 | diff) | |
139 | _hg_status "mra" |
|
137 | _hg_status "mra" | |
140 | ;; |
|
138 | ;; | |
141 | revert) |
|
139 | revert) | |
142 | _hg_status "mra" |
|
140 | _hg_status "mra" | |
143 | ;; |
|
141 | ;; | |
144 | clone) |
|
142 | clone) | |
145 | local count=$(_hg_count_non_option) |
|
143 | local count=$(_hg_count_non_option) | |
146 | if [ $count = 1 ]; then |
|
144 | if [ $count = 1 ]; then | |
147 | _hg_paths |
|
145 | _hg_paths | |
148 | fi |
|
146 | fi | |
149 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" )) |
|
147 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" )) | |
150 | ;; |
|
148 | ;; | |
151 | debugindex|debugindexdot) |
|
149 | debugindex|debugindexdot) | |
152 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" )) |
|
150 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" )) | |
153 | ;; |
|
151 | ;; | |
154 | debugdata) |
|
152 | debugdata) | |
155 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" )) |
|
153 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" )) | |
156 | ;; |
|
154 | ;; | |
157 | cat) |
|
155 | cat) | |
158 | local count=$(_hg_count_non_option '-o|--output') |
|
156 | local count=$(_hg_count_non_option '-o|--output') | |
159 | if [ $count = 2 ]; then |
|
157 | if [ $count = 2 ]; then | |
160 | _hg_tags |
|
158 | _hg_tags | |
161 | else |
|
159 | else | |
162 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" )) |
|
160 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" )) | |
163 | fi |
|
161 | fi | |
164 | ;; |
|
162 | ;; | |
165 | *) |
|
163 | *) | |
166 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" )) |
|
164 | COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" )) | |
167 | ;; |
|
165 | ;; | |
168 | esac |
|
166 | esac | |
169 |
|
167 | |||
170 | } |
|
168 | } | |
171 |
|
169 | |||
172 | complete -o bashdefault -o default -F _hg hg 2> /dev/null \ |
|
170 | complete -o bashdefault -o default -F _hg hg 2> /dev/null \ | |
173 | || complete -o default -F _hg hg |
|
171 | || complete -o default -F _hg hg |
General Comments 0
You need to be logged in to leave comments.
Login now