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