# vim: set syn=sh: if [ $BASH_VERSINFO -gt 2 ]; then if [ ! -z "$SUDO_USER" ]; then __p4_cache_owner="$SUDO_USER" else __p4_cache_owner="$USER" fi __p4_cache="${TMPDIR:-/tmp}/.p4completion.$__p4_cache_owner" # Set up commands for completion. Assume the set of commands won't change # within the session, as p4 help commands requires a network round trip. function _p4() { __p4_rebuild_cache local cmd=1 while true; do case ${COMP_WORDS[$cmd]} in -G | -s | -??*) cmd=$((cmd+1));; -?) cmd=$((cmd+2));; *) break;; esac done if [ $COMP_CWORD = $cmd ] ; then COMPREPLY=($(compgen -W "$__p4_all_commandlist" $2)) elif [ $COMP_CWORD -gt $cmd ] ; then COMPREPLY=($(compgen -f $2)) fi } complete -F _p4 p4 # Check if we have p4. function __p4check() { if [ -z "$P4_EXISTS" ]; then if [ -z "$(p4 -V 2>/dev/null)" ]; then P4_EXISTS=no else P4_EXISTS=yes fi fi [ "$P4_EXISTS" = "yes" ] return $? } # Rebuild cache only if a p4 command was run or the cache is empty. function __p4_dirty_cache() { # Cache is dirty if it is empty. [ -z "$__p4_all_commandlist" ] && return 0 # Cache is clean if no command was run. [ "$__p4_histcmd" = "$hist" ] && return 1 # Cache is dirty if the last command was p4. [ ! "${cmd/p4/}" = "$cmd" ] && return 0 # Cache is dirty if we changed directory. [ ! "$PWD" = "$__p4_pwd" ] && return 0 # Cache is clean. return 1 } function __p4_rebuild_cache() { __p4check || return __p4_load_cache local cmd=$(fc -l -1) local hist=${cmd## *} if __p4_dirty_cache; then __p4_all_commandlist="$(p4 help commands | awk 'NF > 3 { print $1 }')" __p4_client __p4_flags fi __p4_histcmd="$hist" __p4_pwd="$PWD" __p4_save_cache } # Find the current client. function __p4_client() { local output="$(p4 -Ztag info)" __p4_client=$(echo "$output" | sed -n 's/^....clientName //p') [ "$__p4_client" = "*unknown*" ] && __p4_client= __p4_pwd=$(echo "$output" | sed -n 's/^....clientCwd //p') } # Look for opened and/or changed files. function __p4_flags() { __p4_flags= # Opened files? if [ $(p4 opened 2>/dev/null | wc -l) -gt 0 ]; then __p4_flags="$__p4_flags*" fi # Changed files? if [ $(p4 diff -sa -se 2>/dev/null | wc -l) -gt 0 ]; then __p4_flags="$__p4_flags+" fi } function __p4_load_cache() { [ -f "$__p4_cache" ] && . "$__p4_cache" } function __p4_save_cache() { touch "$__p4_cache" chown "$__p4_cache_owner" "$__p4_cache" chmod 600 "$__p4_cache" set | grep "^__p4.*=" > "$__p4_cache" } function __p4_ps1() { __p4_rebuild_cache if [ -n "$__p4_client" ]; then if [ -n "${1-}" ]; then printf "$1" "$__p4_client$__p4_flags" else printf " (%s)" "$__p4_client$__p4_flags" fi fi } fi