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