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