| 1 | # bash completion V2 for git-lfs -*- shell-script -*- |
| 2 | |
| 3 | __git-lfs_debug() |
| 4 | { |
| 5 | if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then |
| 6 | echo "$*" >> "${BASH_COMP_DEBUG_FILE}" |
| 7 | fi |
| 8 | } |
| 9 | |
| 10 | # Macs have bash3 for which the bash-completion package doesn't include |
| 11 | # _init_completion. This is a minimal version of that function. |
| 12 | __git-lfs_init_completion() |
| 13 | { |
| 14 | COMPREPLY=() |
| 15 | _get_comp_words_by_ref "$@" cur prev words cword |
| 16 | } |
| 17 | |
| 18 | # This function calls the git-lfs program to obtain the completion |
| 19 | # results and the directive. It fills the 'out' and 'directive' vars. |
| 20 | __git-lfs_get_completion_results() { |
| 21 | local requestComp lastParam lastChar args |
| 22 | |
| 23 | # Prepare the command to request completions for the program. |
| 24 | # Calling ${words[0]} instead of directly git-lfs allows handling aliases |
| 25 | args=("${words[@]:1}") |
| 26 | requestComp="${words[0]} __completeNoDesc ${args[*]}" |
| 27 | |
| 28 | lastParam=${words[$((${#words[@]}-1))]} |
| 29 | lastChar=${lastParam:$((${#lastParam}-1)):1} |
| 30 | __git-lfs_debug "lastParam ${lastParam}, lastChar ${lastChar}" |
| 31 | |
| 32 | if [[ -z ${cur} && ${lastChar} != = ]]; then |
| 33 | # If the last parameter is complete (there is a space following it) |
| 34 | # We add an extra empty parameter so we can indicate this to the go method. |
| 35 | __git-lfs_debug "Adding extra empty parameter" |
| 36 | requestComp="${requestComp} ''" |
| 37 | fi |
| 38 | |
| 39 | # When completing a flag with an = (e.g., git-lfs -n=<TAB>) |
| 40 | # bash focuses on the part after the =, so we need to remove |
| 41 | # the flag part from $cur |
| 42 | if [[ ${cur} == -*=* ]]; then |
| 43 | cur="${cur#*=}" |
| 44 | fi |
| 45 | |
| 46 | __git-lfs_debug "Calling ${requestComp}" |
| 47 | # Use eval to handle any environment variables and such |
| 48 | out=$(eval "${requestComp}" 2>/dev/null) |
| 49 | |
| 50 | # Extract the directive integer at the very end of the output following a colon (:) |
| 51 | directive=${out##*:} |
| 52 | # Remove the directive |
| 53 | out=${out%:*} |
| 54 | if [[ ${directive} == "${out}" ]]; then |
| 55 | # There is not directive specified |
| 56 | directive=0 |
| 57 | fi |
| 58 | __git-lfs_debug "The completion directive is: ${directive}" |
| 59 | __git-lfs_debug "The completions are: ${out}" |
| 60 | } |
| 61 | |
| 62 | __git-lfs_process_completion_results() { |
| 63 | local shellCompDirectiveError=1 |
| 64 | local shellCompDirectiveNoSpace=2 |
| 65 | local shellCompDirectiveNoFileComp=4 |
| 66 | local shellCompDirectiveFilterFileExt=8 |
| 67 | local shellCompDirectiveFilterDirs=16 |
| 68 | local shellCompDirectiveKeepOrder=32 |
| 69 | |
| 70 | if (((directive & shellCompDirectiveError) != 0)); then |
| 71 | # Error code. No completion. |
| 72 | __git-lfs_debug "Received error from custom completion go code" |
| 73 | return |
| 74 | else |
| 75 | if (((directive & shellCompDirectiveNoSpace) != 0)); then |
| 76 | if [[ $(type -t compopt) == builtin ]]; then |
| 77 | __git-lfs_debug "Activating no space" |
| 78 | compopt -o nospace |
| 79 | else |
| 80 | __git-lfs_debug "No space directive not supported in this version of bash" |
| 81 | fi |
| 82 | fi |
| 83 | if (((directive & shellCompDirectiveKeepOrder) != 0)); then |
| 84 | if [[ $(type -t compopt) == builtin ]]; then |
| 85 | # no sort isn't supported for bash less than < 4.4 |
| 86 | if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then |
| 87 | __git-lfs_debug "No sort directive not supported in this version of bash" |
| 88 | else |
| 89 | __git-lfs_debug "Activating keep order" |
| 90 | compopt -o nosort |
| 91 | fi |
| 92 | else |
| 93 | __git-lfs_debug "No sort directive not supported in this version of bash" |
| 94 | fi |
| 95 | fi |
| 96 | if (((directive & shellCompDirectiveNoFileComp) != 0)); then |
| 97 | if [[ $(type -t compopt) == builtin ]]; then |
| 98 | __git-lfs_debug "Activating no file completion" |
| 99 | compopt +o default |
| 100 | else |
| 101 | __git-lfs_debug "No file completion directive not supported in this version of bash" |
| 102 | fi |
| 103 | fi |
| 104 | fi |
| 105 | |
| 106 | # Separate activeHelp from normal completions |
| 107 | local completions=() |
| 108 | local activeHelp=() |
| 109 | __git-lfs_extract_activeHelp |
| 110 | |
| 111 | if (((directive & shellCompDirectiveFilterFileExt) != 0)); then |
| 112 | # File extension filtering |
| 113 | local fullFilter="" filter filteringCmd |
| 114 | |
| 115 | # Do not use quotes around the $completions variable or else newline |
| 116 | # characters will be kept. |
| 117 | for filter in ${completions[*]}; do |
| 118 | fullFilter+="$filter|" |
| 119 | done |
| 120 | |
| 121 | filteringCmd="_filedir $fullFilter" |
| 122 | __git-lfs_debug "File filtering command: $filteringCmd" |
| 123 | $filteringCmd |
| 124 | elif (((directive & shellCompDirectiveFilterDirs) != 0)); then |
| 125 | # File completion for directories only |
| 126 | |
| 127 | local subdir |
| 128 | subdir=${completions[0]} |
| 129 | if [[ -n $subdir ]]; then |
| 130 | __git-lfs_debug "Listing directories in $subdir" |
| 131 | pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return |
| 132 | else |
| 133 | __git-lfs_debug "Listing directories in ." |
| 134 | _filedir -d |
| 135 | fi |
| 136 | else |
| 137 | __git-lfs_handle_completion_types |
| 138 | fi |
| 139 | |
| 140 | __git-lfs_handle_special_char "$cur" : |
| 141 | __git-lfs_handle_special_char "$cur" = |
| 142 | |
| 143 | # Print the activeHelp statements before we finish |
| 144 | __git-lfs_handle_activeHelp |
| 145 | } |
| 146 | |
| 147 | __git-lfs_handle_activeHelp() { |
| 148 | # Print the activeHelp statements |
| 149 | if ((${#activeHelp[*]} != 0)); then |
| 150 | if [ -z $COMP_TYPE ]; then |
| 151 | # Bash v3 does not set the COMP_TYPE variable. |
| 152 | printf "\n"; |
| 153 | printf "%s\n" "${activeHelp[@]}" |
| 154 | printf "\n" |
| 155 | __git-lfs_reprint_commandLine |
| 156 | return |
| 157 | fi |
| 158 | |
| 159 | # Only print ActiveHelp on the second TAB press |
| 160 | if [ $COMP_TYPE -eq 63 ]; then |
| 161 | printf "\n" |
| 162 | printf "%s\n" "${activeHelp[@]}" |
| 163 | |
| 164 | if ((${#COMPREPLY[*]} == 0)); then |
| 165 | # When there are no completion choices from the program, file completion |
| 166 | # may kick in if the program has not disabled it; in such a case, we want |
| 167 | # to know if any files will match what the user typed, so that we know if |
| 168 | # there will be completions presented, so that we know how to handle ActiveHelp. |
| 169 | # To find out, we actually trigger the file completion ourselves; |
| 170 | # the call to _filedir will fill COMPREPLY if files match. |
| 171 | if (((directive & shellCompDirectiveNoFileComp) == 0)); then |
| 172 | __git-lfs_debug "Listing files" |
| 173 | _filedir |
| 174 | fi |
| 175 | fi |
| 176 | |
| 177 | if ((${#COMPREPLY[*]} != 0)); then |
| 178 | # If there are completion choices to be shown, print a delimiter. |
| 179 | # Re-printing the command-line will automatically be done |
| 180 | # by the shell when it prints the completion choices. |
| 181 | printf -- "--" |
| 182 | else |
| 183 | # When there are no completion choices at all, we need |
| 184 | # to re-print the command-line since the shell will |
| 185 | # not be doing it itself. |
| 186 | __git-lfs_reprint_commandLine |
| 187 | fi |
| 188 | elif [ $COMP_TYPE -eq 37 ] || [ $COMP_TYPE -eq 42 ]; then |
| 189 | # For completion type: menu-complete/menu-complete-backward and insert-completions |
| 190 | # the completions are immediately inserted into the command-line, so we first |
| 191 | # print the activeHelp message and reprint the command-line since the shell won't. |
| 192 | printf "\n" |
| 193 | printf "%s\n" "${activeHelp[@]}" |
| 194 | |
| 195 | __git-lfs_reprint_commandLine |
| 196 | fi |
| 197 | fi |
| 198 | } |
| 199 | |
| 200 | __git-lfs_reprint_commandLine() { |
| 201 | # The prompt format is only available from bash 4.4. |
| 202 | # We test if it is available before using it. |
| 203 | if (x=${PS1@P}) 2> /dev/null; then |
| 204 | printf "%s" "${PS1@P}${COMP_LINE[@]}" |
| 205 | else |
| 206 | # Can't print the prompt. Just print the |
| 207 | # text the user had typed, it is workable enough. |
| 208 | printf "%s" "${COMP_LINE[@]}" |
| 209 | fi |
| 210 | } |
| 211 | |
| 212 | # Separate activeHelp lines from real completions. |
| 213 | # Fills the $activeHelp and $completions arrays. |
| 214 | __git-lfs_extract_activeHelp() { |
| 215 | local activeHelpMarker="_activeHelp_ " |
| 216 | local endIndex=${#activeHelpMarker} |
| 217 | |
| 218 | while IFS='' read -r comp; do |
| 219 | [[ -z $comp ]] && continue |
| 220 | |
| 221 | if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then |
| 222 | comp=${comp:endIndex} |
| 223 | __git-lfs_debug "ActiveHelp found: $comp" |
| 224 | if [[ -n $comp ]]; then |
| 225 | activeHelp+=("$comp") |
| 226 | fi |
| 227 | else |
| 228 | # Not an activeHelp line but a normal completion |
| 229 | completions+=("$comp") |
| 230 | fi |
| 231 | done <<<"${out}" |
| 232 | } |
| 233 | |
| 234 | __git-lfs_handle_completion_types() { |
| 235 | __git-lfs_debug "__git-lfs_handle_completion_types: COMP_TYPE is $COMP_TYPE" |
| 236 | |
| 237 | case $COMP_TYPE in |
| 238 | 37|42) |
| 239 | # Type: menu-complete/menu-complete-backward and insert-completions |
| 240 | # If the user requested inserting one completion at a time, or all |
| 241 | # completions at once on the command-line we must remove the descriptions. |
| 242 | # https://github.com/spf13/cobra/issues/1508 |
| 243 | |
| 244 | # If there are no completions, we don't need to do anything |
| 245 | (( ${#completions[@]} == 0 )) && return 0 |
| 246 | |
| 247 | local tab=$'\t' |
| 248 | |
| 249 | # Strip any description and escape the completion to handled special characters |
| 250 | IFS=$'\n' read -ra completions -d '' < <(printf "%q\n" "${completions[@]%%$tab*}") |
| 251 | |
| 252 | # Only consider the completions that match |
| 253 | IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${completions[*]}" -- "${cur}") |
| 254 | |
| 255 | # compgen looses the escaping so we need to escape all completions again since they will |
| 256 | # all be inserted on the command-line. |
| 257 | IFS=$'\n' read -ra COMPREPLY -d '' < <(printf "%q\n" "${COMPREPLY[@]}") |
| 258 | ;; |
| 259 | |
| 260 | *) |
| 261 | # Type: complete (normal completion) |
| 262 | __git-lfs_handle_standard_completion_case |
| 263 | ;; |
| 264 | esac |
| 265 | } |
| 266 | |
| 267 | __git-lfs_handle_standard_completion_case() { |
| 268 | local tab=$'\t' |
| 269 | |
| 270 | # If there are no completions, we don't need to do anything |
| 271 | (( ${#completions[@]} == 0 )) && return 0 |
| 272 | |
| 273 | # Short circuit to optimize if we don't have descriptions |
| 274 | if [[ "${completions[*]}" != *$tab* ]]; then |
| 275 | # First, escape the completions to handle special characters |
| 276 | IFS=$'\n' read -ra completions -d '' < <(printf "%q\n" "${completions[@]}") |
| 277 | # Only consider the completions that match what the user typed |
| 278 | IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${completions[*]}" -- "${cur}") |
| 279 | |
| 280 | # compgen looses the escaping so, if there is only a single completion, we need to |
| 281 | # escape it again because it will be inserted on the command-line. If there are multiple |
| 282 | # completions, we don't want to escape them because they will be printed in a list |
| 283 | # and we don't want to show escape characters in that list. |
| 284 | if (( ${#COMPREPLY[@]} == 1 )); then |
| 285 | COMPREPLY[0]=$(printf "%q" "${COMPREPLY[0]}") |
| 286 | fi |
| 287 | return 0 |
| 288 | fi |
| 289 | |
| 290 | local longest=0 |
| 291 | local compline |
| 292 | # Look for the longest completion so that we can format things nicely |
| 293 | while IFS='' read -r compline; do |
| 294 | [[ -z $compline ]] && continue |
| 295 | |
| 296 | # Before checking if the completion matches what the user typed, |
| 297 | # we need to strip any description and escape the completion to handle special |
| 298 | # characters because those escape characters are part of what the user typed. |
| 299 | # Don't call "printf" in a sub-shell because it will be much slower |
| 300 | # since we are in a loop. |
| 301 | printf -v comp "%q" "${compline%%$tab*}" &>/dev/null || comp=$(printf "%q" "${compline%%$tab*}") |
| 302 | |
| 303 | # Only consider the completions that match |
| 304 | [[ $comp == "$cur"* ]] || continue |
| 305 | |
| 306 | # The completions matches. Add it to the list of full completions including |
| 307 | # its description. We don't escape the completion because it may get printed |
| 308 | # in a list if there are more than one and we don't want show escape characters |
| 309 | # in that list. |
| 310 | COMPREPLY+=("$compline") |
| 311 | |
| 312 | # Strip any description before checking the length, and again, don't escape |
| 313 | # the completion because this length is only used when printing the completions |
| 314 | # in a list and we don't want show escape characters in that list. |
| 315 | comp=${compline%%$tab*} |
| 316 | if ((${#comp}>longest)); then |
| 317 | longest=${#comp} |
| 318 | fi |
| 319 | done < <(printf "%s\n" "${completions[@]}") |
| 320 | |
| 321 | # If there is a single completion left, remove the description text and escape any special characters |
| 322 | if ((${#COMPREPLY[*]} == 1)); then |
| 323 | __git-lfs_debug "COMPREPLY[0]: ${COMPREPLY[0]}" |
| 324 | COMPREPLY[0]=$(printf "%q" "${COMPREPLY[0]%%$tab*}") |
| 325 | __git-lfs_debug "Removed description from single completion, which is now: ${COMPREPLY[0]}" |
| 326 | else |
| 327 | # Format the descriptions |
| 328 | __git-lfs_format_comp_descriptions $longest |
| 329 | fi |
| 330 | } |
| 331 | |
| 332 | __git-lfs_handle_special_char() |
| 333 | { |
| 334 | local comp="$1" |
| 335 | local char=$2 |
| 336 | if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then |
| 337 | local word=${comp%"${comp##*${char}}"} |
| 338 | local idx=${#COMPREPLY[*]} |
| 339 | while ((--idx >= 0)); do |
| 340 | COMPREPLY[idx]=${COMPREPLY[idx]#"$word"} |
| 341 | done |
| 342 | fi |
| 343 | } |
| 344 | |
| 345 | __git-lfs_format_comp_descriptions() |
| 346 | { |
| 347 | local tab=$'\t' |
| 348 | local comp desc maxdesclength |
| 349 | local longest=$1 |
| 350 | |
| 351 | local i ci |
| 352 | for ci in ${!COMPREPLY[*]}; do |
| 353 | comp=${COMPREPLY[ci]} |
| 354 | # Properly format the description string which follows a tab character if there is one |
| 355 | if [[ "$comp" == *$tab* ]]; then |
| 356 | __git-lfs_debug "Original comp: $comp" |
| 357 | desc=${comp#*$tab} |
| 358 | comp=${comp%%$tab*} |
| 359 | |
| 360 | # $COLUMNS stores the current shell width. |
| 361 | # Remove an extra 4 because we add 2 spaces and 2 parentheses. |
| 362 | maxdesclength=$(( COLUMNS - longest - 4 )) |
| 363 | |
| 364 | # Make sure we can fit a description of at least 8 characters |
| 365 | # if we are to align the descriptions. |
| 366 | if ((maxdesclength > 8)); then |
| 367 | # Add the proper number of spaces to align the descriptions |
| 368 | for ((i = ${#comp} ; i < longest ; i++)); do |
| 369 | comp+=" " |
| 370 | done |
| 371 | else |
| 372 | # Don't pad the descriptions so we can fit more text after the completion |
| 373 | maxdesclength=$(( COLUMNS - ${#comp} - 4 )) |
| 374 | fi |
| 375 | |
| 376 | # If there is enough space for any description text, |
| 377 | # truncate the descriptions that are too long for the shell width |
| 378 | if ((maxdesclength > 0)); then |
| 379 | if ((${#desc} > maxdesclength)); then |
| 380 | desc=${desc:0:$(( maxdesclength - 1 ))} |
| 381 | desc+="…" |
| 382 | fi |
| 383 | comp+=" ($desc)" |
| 384 | fi |
| 385 | COMPREPLY[ci]=$comp |
| 386 | __git-lfs_debug "Final comp: $comp" |
| 387 | fi |
| 388 | done |
| 389 | } |
| 390 | |
| 391 | __start_git-lfs() |
| 392 | { |
| 393 | local cur prev words cword split |
| 394 | |
| 395 | COMPREPLY=() |
| 396 | |
| 397 | # Call _init_completion from the bash-completion package |
| 398 | # to prepare the arguments properly |
| 399 | if declare -F _init_completion >/dev/null 2>&1; then |
| 400 | _init_completion -n =: || return |
| 401 | else |
| 402 | __git-lfs_init_completion -n =: || return |
| 403 | fi |
| 404 | |
| 405 | __git-lfs_debug |
| 406 | __git-lfs_debug "========= starting completion logic ==========" |
| 407 | __git-lfs_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword" |
| 408 | |
| 409 | # The user could have moved the cursor backwards on the command-line. |
| 410 | # We need to trigger completion from the $cword location, so we need |
| 411 | # to truncate the command-line ($words) up to the $cword location. |
| 412 | words=("${words[@]:0:$cword+1}") |
| 413 | __git-lfs_debug "Truncated words[*]: ${words[*]}," |
| 414 | |
| 415 | if [[ ${words[0]} == "git" && ${words[1]} == "lfs" ]]; then |
| 416 | words=("git-lfs" "${words[@]:2:${#words[@]}-2}") |
| 417 | __git-lfs_debug "Rewritten words[*]: ${words[*]}," |
| 418 | fi |
| 419 | |
| 420 | local out directive |
| 421 | __git-lfs_get_completion_results |
| 422 | __git-lfs_process_completion_results |
| 423 | } |
| 424 | |
| 425 | if [[ $(type -t compopt) = "builtin" ]]; then |
| 426 | complete -o default -F __start_git-lfs git-lfs |
| 427 | else |
| 428 | complete -o default -o nospace -F __start_git-lfs git-lfs |
| 429 | fi |
| 430 | |
| 431 | # ex: ts=4 sw=4 et filetype=sh |
| 432 | _git_lfs() { __start_git-lfs; } |