contrib/bash_completion
author mpm@selenic.com
Tue, 16 Aug 2005 15:55:33 -0800
changeset 922 f4c7ad186983
parent 916 fe094cca9915
child 929 26bb67b7ec14
permissions -rw-r--r--
Teach bash_completion about outgoing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
916
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     1
_hg_commands()
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     2
{
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     3
    local commands="$(hg -v help | sed -e '1,/^list of commands:/d' \
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     4
                                       -e '/^global options:/Q' \
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     5
				       -e '/^ [^ ]/!d; s/[,:]//g;')"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     6
    
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     7
    # hide debug commands from users, but complete them if 
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     8
    # specifically asked for
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
     9
    if [[ "$cur" == de* ]]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    10
	commands="$commands debugcheckstate debugstate debugindex"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    11
	commands="$commands debugindexdot debugwalk"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    12
    fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    13
    COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$commands" -- "$cur") )
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    14
}
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    15
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    16
_hg_paths()
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    17
{
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    18
    local paths="$(hg paths | sed -e 's/ = .*$//')"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    19
    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" ))
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    20
}
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    21
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    22
_hg_tags()
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    23
{
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    24
    local tags="$(hg tags | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    25
    COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") )
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    26
}
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    27
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    28
# this is "kind of" ugly...
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    29
_hg_count_non_option()
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    30
{
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    31
    local i count=0
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    32
    local filters="$1"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    33
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    34
    for (( i=1; $i<=$COMP_CWORD; i++ )); do
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    35
	if [[ "${COMP_WORDS[i]}" != -* ]]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    36
	    for f in $filters; do
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    37
		if [[ ${COMP_WORDS[i-1]} == $f ]]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    38
		    continue 2
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    39
		fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    40
	    done
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    41
	    count=$(($count + 1))
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    42
	fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    43
    done
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    44
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    45
    echo $(($count - 1))
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    46
}
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    47
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    48
_hg()
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    49
{
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    50
    local cur prev cmd opts i
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    51
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    52
    COMPREPLY=()
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    53
    cur="$2"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    54
    prev="$3"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    55
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    56
    # searching for the command 
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    57
    # (first non-option argument that doesn't follow -R/--repository)
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    58
    for (( i=1; $i<=$COMP_CWORD; i++ )); do
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    59
	if [[ ${COMP_WORDS[i]} != -* ]] \
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    60
	   && [ "${COMP_WORDS[i-1]}" != -R ] \
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    61
	   && [ "${COMP_WORDS[i-1]}" != --repository ]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    62
	    cmd="${COMP_WORDS[i]}"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    63
	    break
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    64
	fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    65
    done
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    66
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    67
    if [[ "$cur" == -* ]]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    68
	opts="$(hg -v help | sed -e '1,/^global options/d; /^ -/!d')"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    69
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    70
	if [ -n "$cmd" ]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    71
	    opts="$opts $(hg help "$cmd" | sed -e '/^ -/!d; s/ [^-][^ ]*//')"
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    72
	fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    73
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    74
	COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    75
	return
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    76
    fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    77
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    78
    if [ "$prev" = -R ] || [ "$prev" = --repository ]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    79
        COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    80
	return
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    81
    fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    82
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    83
    if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    84
	_hg_commands
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    85
	return
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    86
    fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    87
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    88
    if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    89
	_hg_tags
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    90
	return
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    91
    fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    92
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    93
    case "$cmd" in
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    94
	help)
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    95
	    _hg_commands
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    96
	;;
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    97
	export|manifest|update|checkout|up|co)
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    98
	    _hg_tags
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
    99
	;;
922
f4c7ad186983 Teach bash_completion about outgoing
mpm@selenic.com
parents: 916
diff changeset
   100
	pull|push|outgoing)
916
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   101
	    _hg_paths
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   102
	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   103
	;;
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   104
	paths)
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   105
	    _hg_paths
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   106
	;;
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   107
	clone)
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   108
	    local count=$(_hg_count_non_option)
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   109
	    if [ $count = 1 ]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   110
		_hg_paths
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   111
	    fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   112
	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   113
	;;
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   114
	cat)
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   115
	    local count=$(_hg_count_non_option -o --output)
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   116
	    if [ $count = 2 ]; then
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   117
		_hg_tags
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   118
	    else
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   119
		COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   120
	    fi
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   121
	;;
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   122
	*) 
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   123
            COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   124
	;;
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   125
    esac
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   126
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   127
}
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   128
fe094cca9915 Add bash_completion to contrib
mpm@selenic.com
parents:
diff changeset
   129
complete -o filenames -F _hg hg