-
Notifications
You must be signed in to change notification settings - Fork 0
/
_hsh
238 lines (228 loc) · 5.91 KB
/
_hsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/env bash -i
if ! declare -F _git > /dev/null || \
! declare -F __git_wrap__git_main > /dev/null; then
if declare -F _completion_loader > /dev/null; then
_completion_loader git
fi
fi
_hsh_find_root() {
local root_dir="$(pwd)"
while [ $i -lt $COMP_CWORD ]
do
case "${COMP_WORDS[$i]}" in
-C)
i=$((i + 1))
root_dir="${COMP_WORDS[$i]}"
break
;;
-*)
;;
*)
break
;;
esac
i=$((i + 1))
done
while [ "$root_dir" != '/' ] && [ "$root_dir" != '.' ]
do
if [ -e "$root_dir/.hsh" ]
then
echo "$root_dir"
break
fi
root_dir="$(dirname "$root_dir")"
done
}
_hsh_repo_dir() {
local repo_dir="$(_hsh_find_root)/.hsh/repos"
}
_hsh_default() {
local cmd="$1"
shift
local cmd_idx="$1"
shift
local cword="$COMP_CWORD"
# at this point
if [ "$cword" -eq "$((cmd_idx + 1))" ]
then
local repo_dir="$(_hsh_find_root)/.hsh/repos"
[ -e "$repo_dir" ] || return 0
COMPREPLY=($(compgen -W "all $(ls -1 "$repo_dir")" -- "$cur"))
return 0
fi
COMP_WORDS=( git --git-dir "$(_hsh_find_root)/.hsh/repos/${COMP_WORDS[cmd_idx+1]}" ${COMP_WORDS[cmd_idx]} ${COMP_WORDS[@]:$((cmd_idx+2))} )
if [ "${COMP_LINE[@]: -1}" = " " ]
then
COMP_WORDS+=( "" )
COMP_CWORD=$(( ${#COMP_WORDS[@]} ))
else
COMP_CWORD=$(( ${#COMP_WORDS[@]} - 1 ))
fi
COMP_LINE="${COMP_WORDS[*]}"
COMP_POINT="${#COMP_LINE}"
if declare -F _git >/dev/null; then
_git
elif declare -F __git_wrap__git_main >/dev/null; then
__git_wrap__git_main
fi
}
_hsh_dependency() {
local cmd_idx="$1"
shift
local cword="$COMP_CWORD" local cur="${COMP_WORDS[cword]}"
if [ "$cword" -eq "$((cmd_idx + 1))" ]
then
local repo_dir="$(_hsh_find_root)/.hsh/repos"
[ -e "$repo_dir" ] || return 0
COMPREPLY=( $(compgen -W "$(ls -1 "$repo_dir")" -- "$cur") )
return 0
fi
if [ "$cword" -eq "$((cmd_idx + 2))" ]
then
COMPREPLY=( $(compgen -W "add rm" -- "$cur") )
return 0
fi
if [ "$cword" -eq "$((cmd_idx + 3))" ]
then
local prev="${COMP_WORDS[cmd_idx + 2]}"
if [ "$prev" = "rm" ]
then
if git --git-dir "$(_hsh_find_root)/.hsh/repos/${COMP_WORDS[cmd_idx+1]}" ls-files | grep -q ".hshdependencies"
then
COMPREPLY=( $(compgen -W "$(git --git-dir "$(_hsh_find_root)/.hsh/repos/${COMP_WORDS[cmd_idx+1]}" show HEAD:.hshdependencies)" -- "$cur") )
fi
fi
return 0
fi
}
_hsh_edit() {
local cmd_idx="$1"
shift
local cword="$COMP_CWORD"
local cur="${COMP_WORDS[cword]}"
if [ "$cword" -eq "$((cmd_idx + 1))" ]
then
local repo_dir="$(_hsh_find_root)/.hsh/repos"
[ -e "$repo_dir" ] || return 0
COMPREPLY=($(compgen -W "$(ls -1 "$repo_dir")" -- "$cur"))
return 0
fi
if [ "$cword" -eq "$((cmd_idx + 2))" ]
then
COMPREPLY=($(compgen -W "readme license gitignore gitattributes h.sh" -- "$cur"))
fi
}
_hsh_run_hook() {
local cmd_idx="$1"
shift
local cword="$COMP_CWORD"
local cur="${COMP_WORDS[cword]}"
if [ "$cword" -eq "$((cmd_idx + 1))" ]
then
local repo_dir="$(_hsh_find_root)/.hsh/repos"
[ -e "$repo_dir" ] || return 0
COMPREPLY=($(compgen -W "$(ls -1 "$repo_dir")" -- "$cur"))
return 0
fi
if [ "$cword" -eq "$((cmd_idx + 2))" ]
then
COMPREPLY=($(compgen -W "$(git --list-cmds=main,alias,nohelpers)" -- "$cur"))
fi
}
_hsh_sync() {
local cmd_idx="$1"
shift
local cword="$COMP_CWORD"
local cur="${COMP_WORDS[cword]}"
if [ "$cword" -eq "$((cmd_idx + 1))" ]
then
local repo_dir="$(_hsh_find_root)/.hsh/repos"
[ -e "$repo_dir" ] || return 0
COMPREPLY=($(compgen -W "$(ls -1 "$repo_dir")" -- "$cur"))
return 0
fi
if [ "$cword" -eq "$((cmd_idx + 2))" ]
then
COMPREPLY=($(compgen -W "--message" -- "$cur"))
fi
}
_hsh() {
local cword="$COMP_CWORD"
local cur=""
if [ $cword -ne 0 ]
then
cur="${COMP_WORDS[cword]}"
fi
local prev=""
if [ $cword -gt 1 ]
then
prev="${COMP_WORDS[cword-1]}"
fi
local words=("${COMP_WORDS[@]}")
local loc=""
local cmd=
local cmd_idx=
local i=1
local c_idx=
while [ $i -lt $cword ]
do
local wrd="${words[$i]}"
case "$wrd" in
-h|--help)
COMPREPLY=()
return 0
;;
-C)
c_idx=$i
;;
-*)
continue
;;
*)
cmd="$wrd"
cmd_idx=$i
break
;;
esac
i=$((i+1))
done
if [ -z "${cmd-}" ]
then
if [ "$prev" = "-C" ]
then
COMPREPLY=( $(compgen -o dirnames -- "$cur") )
else
local choice="$(git --list-cmds=main,alias,nohelpers)"
choice="$choice edit bundle dependency run_hook sync dotgit ls -h --help --version"
if [ -z "$has_c" ]
then
choice="$choice -C"
fi
COMPREPLY=($(compgen -W "$choice" -- "$cur"))
return 0
fi
fi
case "$cmd" in
init)
COMPREPLY=()
return 0
;;
clone)
COMPREPLY=()
return 0
;;
dependency)
_hsh_dependency "$cmd_idx"
;;
edit)
_hsh_edit "$cmd_idx"
;;
run_hook)
_hsh_run_hook "$cmd_idx"
;;
*)
_hsh_default "$cmd" "$cmd_idx"
;;
esac
}
complete -F _hsh hsh