2 if [ $BASH_VERSINFO -gt 2 ]; then
3 if [ ! -z "$SUDO_USER" ]; then
4 __p4_cache_owner="$SUDO_USER"
6 __p4_cache_owner="$USER"
8 __p4_cache="${TMPDIR:-/tmp}/.p4completion.$__p4_cache_owner"
10 # Set up commands for completion. Assume the set of commands won't change
11 # within the session, as p4 help commands requires a network round trip.
17 case ${COMP_WORDS[$cmd]} in
18 -G | -s | -??*) cmd=$((cmd+1));;
24 if [ $COMP_CWORD = $cmd ] ; then
25 COMPREPLY=($(compgen -W "$__p4_all_commandlist" $2))
26 elif [ $COMP_CWORD -gt $cmd ] ; then
27 COMPREPLY=($(compgen -f $2))
32 # Check if we have p4.
33 function __p4check() {
34 if [ -z "$P4_EXISTS" ]; then
35 if [ -z "$(p4 -V 2>/dev/null)" ]; then
42 [ "$P4_EXISTS" = "yes" ]
46 # Rebuild cache only if a p4 command was run or the cache is empty.
47 function __p4_dirty_cache() {
48 # Cache is dirty if it is empty.
49 [ -z "$__p4_all_commandlist" ] && return 0
51 # Cache is clean if no command was run.
52 [ "$__p4_histcmd" = "$hist" ] && return 1
54 # Cache is dirty if the last command was p4.
55 [ ! "${cmd/p4/}" = "$cmd" ] && return 0
57 # Cache is dirty if we changed directory.
58 [ ! "$PWD" = "$__p4_pwd" ] && return 0
64 function __p4_rebuild_cache() {
72 if __p4_dirty_cache; then
73 __p4_all_commandlist="$(p4 help commands | awk 'NF > 3 { print $1 }')"
84 # Find the current client.
85 function __p4_client() {
86 local output="$(p4 -Ztag info)"
88 __p4_client=$(echo "$output" | sed -n 's/^....clientName //p')
89 [ "$__p4_client" = "*unknown*" ] && __p4_client=
91 __p4_pwd=$(echo "$output" | sed -n 's/^....clientCwd //p')
94 # Look for opened and/or changed files.
95 function __p4_flags() {
99 if [ $(p4 opened 2>/dev/null | wc -l) -gt 0 ]; then
100 __p4_flags="$__p4_flags*"
104 if [ $(p4 diff -sa -se 2>/dev/null | wc -l) -gt 0 ]; then
105 __p4_flags="$__p4_flags+"
109 function __p4_load_cache() {
110 [ -f "$__p4_cache" ] && . "$__p4_cache"
113 function __p4_save_cache() {
115 chown "$__p4_cache_owner" "$__p4_cache"
116 chmod 600 "$__p4_cache"
117 set | grep "^__p4.*=" > "$__p4_cache"
120 function __p4_ps1() {
122 if [ -n "$__p4_client" ]; then
123 if [ -n "${1-}" ]; then
124 printf "$1" "$__p4_client$__p4_flags"
126 printf " (%s)" "$__p4_client$__p4_flags"