Remove absolute paths from ts.
[profile.git] / .profile.d / p4-completion.bashrc
1 # vim: set syn=sh: 
2 if [ $BASH_VERSINFO -gt 2 ]; then
3 if [ ! -z "$SUDO_USER" ]; then
4   __p4_cache_owner="$SUDO_USER"
5 else
6   __p4_cache_owner="$USER"
7 fi
8 __p4_cache="${TMPDIR:-/tmp}/.p4completion.$__p4_cache_owner"
9
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.
12 function _p4() {
13   __p4_rebuild_cache
14
15   local cmd=1
16   while true; do
17     case ${COMP_WORDS[$cmd]} in
18       -G | -s | -??*) cmd=$((cmd+1));;
19       -?) cmd=$((cmd+2));;
20       *) break;;
21     esac
22   done
23
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))
28   fi
29 }
30 complete -F _p4 p4
31
32 # Check if we have p4.
33 function __p4check() {
34   if [ -z "$P4_EXISTS" ]; then
35     if [ -z "$(p4 -V 2>/dev/null)" ]; then
36       P4_EXISTS=no
37     else
38       P4_EXISTS=yes
39     fi
40   fi
41
42   [ "$P4_EXISTS" = "yes" ]
43   return $?
44 }
45
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
50
51   # Cache is clean if no command was run.
52   [ "$__p4_histcmd" = "$hist" ] && return 1
53
54   # Cache is dirty if the last command was p4.
55   [ ! "${cmd/p4/}" = "$cmd" ] && return 0
56
57   # Cache is dirty if we changed directory.
58   [ ! "$PWD" = "$__p4_pwd" ] && return 0
59
60   # Cache is clean.
61   return 1
62 }
63
64 function __p4_rebuild_cache() {
65   __p4check || return
66
67   __p4_load_cache
68
69   local cmd=$(fc -l -1)
70   local hist=${cmd##    *}
71
72   if __p4_dirty_cache; then
73     __p4_all_commandlist="$(p4 help commands | awk 'NF > 3 { print $1 }')"
74     __p4_client
75     __p4_flags
76   fi
77
78   __p4_histcmd="$hist"
79   __p4_pwd="$PWD"
80
81   __p4_save_cache
82 }
83
84 # Find the current client.
85 function __p4_client() {
86   local output="$(p4 -Ztag info)"
87
88   __p4_client=$(echo "$output" | sed -n 's/^....clientName //p')
89   [ "$__p4_client" = "*unknown*" ] && __p4_client=
90
91   __p4_pwd=$(echo "$output" | sed -n 's/^....clientCwd //p')
92 }
93
94 # Look for opened and/or changed files.
95 function __p4_flags() {
96   __p4_flags=
97
98   # Opened files?
99   if [ $(p4 opened 2>/dev/null | wc -l) -gt 0 ]; then
100     __p4_flags="$__p4_flags*"
101   fi
102
103   # Changed files?
104   if [ $(p4 diff -sa -se 2>/dev/null | wc -l) -gt 0 ]; then
105     __p4_flags="$__p4_flags+"
106   fi
107 }
108
109 function __p4_load_cache() {
110   [ -f "$__p4_cache" ] && . "$__p4_cache"
111 }
112
113 function __p4_save_cache() {
114   touch "$__p4_cache"
115   chown "$__p4_cache_owner" "$__p4_cache"
116   chmod 600 "$__p4_cache"
117   set | grep "^__p4.*=" > "$__p4_cache"
118 }
119
120 function __p4_ps1() {
121   __p4_rebuild_cache
122   if [ -n "$__p4_client" ]; then
123     if [ -n "${1-}" ]; then
124       printf "$1" "$__p4_client$__p4_flags"
125     else
126       printf " (%s)" "$__p4_client$__p4_flags"
127     fi
128   fi
129 }
130 fi