# Adjust PATH:
# * Current directory ('.') at front.
# * Personal ~/bin near front
# * /usr/local/bin early in PATH, to get software installed w/brew (e.g. bash).
# * X11 stuff late in PATH.
export PATH=~/bin:/usr/local/bin:/usr/local/sbin:$PATH:/usr/X11/bin:.
# Function cleanpath($pathVar)
# Removes duplicate entries from PATH, keeping only the first of duplicates.
# Sample Usage:
# export PATH=$(cleanpath "$PATH")
# export YourPathVar=$(cleanpath "$YourPathVar")
function cleanpath () {
local oldpath="${1:-}"
local -A pA=
local -a pa=
local -i pi=0
local tmpFile=$(mktemp)
local newPath=
echo $oldpath | tr ':' '\n' > "$tmpFile"
# Eliminate duplicate path entries.
while read -r pe; do
[[ -z "$pe" ]] && continue
if [[ -z "${pA[$pe]}" ]]; then
pA[$pe]=1
pa[$pi]="$pe"
pi+=1
if [[ -n "$newPath" ]]; then
newPath="$newPath:$pe"
else
newPath="$pe"
fi
fi
done < "$tmpFile"
newPath=${newPath#:}
newPath=${newPath%:}
echo $newPath
}
export PATH=$(cleanpath "$PATH")
# Some shells need single quotes around $PWD:$USER, while others
# need double quotes.
export PS1='$USER:$PWD '
# Set VI command line editing mode.
set -o vi
# Apply a title to an Xterm window frame.
function xtitle ()
{
case $TERM in
(*term | rxvt) echo -n -e "\033]0;$*\007";;
(*) ;;
esac
}
# Shorthand for 'p4 help <topic> | less'
function p4h { p4 help $1 | less; }
# General Purpose Aliases.
alias dir='ls -lrt'
alias md='/bin/mkdir -p'
alias rd=/bin/rmdir
alias cls=clear
alias pat='echo $(p4 -ztag -F %depotFile% where)@$(p4 -ztag -F %change% changes -m 1 ...)'
alias vsc="open -a 'Visual Studio Code'"
# P4 Aliases.
alias o='echo p4 -s opened ... && p4 -s opened ...'
alias ol='p4 -F %clientFile% fstat -Ro -F "^action=delete" ...'
alias h='p4 filelog -t -m 5'
# sudo alias the become root but use /usr/local/bin/bash (i.e. bash4),
# nice since the default bash on OSX is still, as of El Capitan,
# old bash3.
alias sr='sudo su root -c /usr/local/bin/bash'
alias srl='sudo su - root -c "BASH_ENV=~/.profile; export BASH_ENV; /usr/local/bin/bash -l"'
# Get revision of a Perforce executable, even if it's not executable
# on the current platform (e.g. Linux executable and we're on a Mac),
# Use the 'what' app, and extract the revion part we want. If the
# specified exe doesn't exist, try using 'which' to find it on PATH,
# and get the version of the one on the PATH.
function p4r () {
exe=${1:-p4};
[[ -r "$exe" ]] || exe=$(which "$exe")
what "$exe" | head -2 | tail -1;
}
export P4CONFIG=.p4config
export P4EDITOR=/usr/bin/vi
alias cdpub='cd ~/p4/ppd'
alias cdg='cd ~/p4/psi/depot/intranet/consulting/Guides'
alias cdp='cd ~/p4/psi'
alias cdb='cd ~/p4/psi/depot/intranet/consulting/sw/DEV/bbi'
alias cdc='cd ~/p4/psi/depot/intranet/consulting'
alias cdd='cd ~/p4/psi/depot/intranet/consulting/sw/DEV'
alias cds='cd ~/p4/spv'
alias cdt='cd ~/p4/ctt/depot/Pictures/APOD'
alias cdu='cd ~/p4/psi/usr/ttyler'
alias cdsp='cd ~/p4/psi/depot/intranet/consulting/projects/Cordance/server_package'
alias ko='open -a "Komodo IDE 9"'
# alias a='open -a AsciidocFX'
alias a='asciidoctor'
# Set 'm' alias for my favoriate Markdown editor. Use the
# MWeb if avaialbe, else MWeb Lite if available, else vi.
if [[ -d "/Applications/MWeb.app" ]]; then
alias m='open -a "MWeb"'
elif [[ -d "/Applications/MWeb Lite.app" ]]; then
alias m='open -a "MWeb Lite"'
else
alias m=vim
fi
# Git, Perl and Python settings
export PERLHOME=/usr/local/ActivePerl-5.20
[[ -d /usr/local/git/bin ]] && export PATH="/usr/local/git/bin:$PATH"
if [[ -d "$PERLHOME" ]]; then
export PATH="$PERLHOME/bin:$PATH"
export MANPATH="$PERLHOME/man:$MANPATH"
fi
PYHOME=/usr/local/Cellar/python/3.7.4
[[ -d "$PYHOME" ]] && export PATH="$PYHOME/bin:$PATH"
# aliases...
alias title=xtitle
alias top='xtitle Processes on $HOST && top'
#alias make='xtitle Making $(basename $PWD) ; make'
#------------------------------------------------------------------------------
# Function: p4li
#
# Login to Perforce using a '.p4passwd' file found anywhere along the
# path to the current directory.
#
# If the 'super' option is added, an account named 'super-$P4USER' is
# logged in instead with the same password. This is for cases where
# an admin wants to protect themselves from accidentally running super
# user operations against the wrong Perforce server, so they create
# as separate account for only doing super user things.
# The 'p4sli' alias defined below just passes the 'super' option to
# 'p4li'
#------------------------------------------------------------------------------
function p4li () {
super=${1:-Unset}
passwd_file=".p4passwd"
fq_passwd_file=""
dir="$PWD"
while [[ $dir != "" ]]; do
fq_passwd_file="$dir/$passwd_file"
#echo "... Checking for password file [$fq_passwd_file]."
[[ -e "$fq_passwd_file" ]] && break
dir=${dir%\/*}
done
# Special last-resort case: Password file in root dir, '/':
if [[ ! -e "$fq_passwd_file" ]]; then
fq_passwd_file="/$passwd_file"
#echo "... Last Resort: Checking for password file [$fq_passwd_file]."
fi
p4user=$(p4 set P4USER 2>/dev/null)
if [[ -z "$p4user" ]]; then
p4user=$USER
else
p4user=${p4user##*=}
p4user=${p4user%% *}
fi
if [[ "$super" == super ]]; then
port=$(p4 set -q P4PORT)
port=${port##*=}
case "$port" in
(*perforce.perforce.com*) p4user=su-$p4user;;
(*) p4user=super-$p4user;;
esac
fi
if [[ -e "$fq_passwd_file" ]]; then
echo "Attempting login using [$fq_passwd_file]."
p4 -u $p4user login -a < $fq_passwd_file
echo "Ticket duration:"
p4 -u $p4user login -s
else
echo -e "\nError: Could not find password file [$passwd_file].\n"
fi
}
# Usage: p4jobs things to search on
# E.G. jobs ldap journal edge
# Usage: p4jobs Things I want to search on
# Quotes are not needed for "Things to search on".
function p4jobs () {
p4li > /dev/null
for job in $(p4 -ztag -F %Job% jobs -e "$*"); do
Issue=$(p4 -ztag -F %DTG_DTISSUE% job -o $job)
if [[ -n "$Issue" ]]; then
Summary=$(p4 -ztag -F %JIRASummary% job -o $job)
echo -e "$job: $Summary\nhttps://jira.perforce.com:8443/browse/$Issue\n"
else
Desc=$(p4 -ztag -F %Description% job -o $job|head -1)
echo -e "$job: $Desc\n"
fi
done
}
alias p4sli='p4li super'
# Display a job with 'less', sans the big comment block.
function jo () {
p4li > /dev/null
p4 job -o $1 | grep -v "^#" | less
}
# Display the URL of the JIRA issue for a job.
function jou () {
p4li > /dev/null
for job in $*; do
Issue=$(p4 -ztag -F %DTG_DTISSUE% job -o $job)
Summary=$(p4 -ztag -F %JIRASummary% job -o $job)
echo -e "$job - $Summary\\nhttps://jira.perforce.com:8443/browse/$Issue"
done
}
# Create a new empty pending changelist.
# Usage: c Your description goes here.
# The description needs quotes if there are special chars like ')'
# or '!' that have meaning to the shell.
function c () {
p4li > /dev/null
echo -e "Change: new\n\nDescription:\n\t$*\n" | p4 change -i
}
# DNS Flush on OSX Sierra
function flushdns () {
sudo dscacheutil -flushcache ; sudo killall -HUP mDNSResponder
}
# 'p4 opened ...', similiar to 'o' alias above, but listing files in
# absolute local OS syntax.
function ofa () {
cRoot=$(p4 -ztag -F %clientRoot% info)
cName=$(p4 -ztag -F %clientName% info -s)
p4 -ztag -F '%clientFile%' opened ... | sed s:^//$cName:$cRoot:g
}
# 'p4 opened ...', similiar to 'o' alias above, but listing files in
# relative local OS syntax.
function of () {
cRoot=$(p4 -ztag -F %clientRoot% info)
cName=$(p4 -ztag -F %clientName% info -s)
p4 -ztag -F '%clientFile%' opened ... | sed s:^//$cName:$cRoot:g|sed s:$PWD:.:g
}
alias cdp='cd ~/p4/psi/depot/dev/ttyler/OnTour2017'
# kd() - known_hosts delete by line numbers
# Usage Example: kd 37 92 18 5
# Alias from Bob Arnold to "fix" (delete) problematic entries issues in
# ~/.ssh/known_hosts files. Sample Usage: kd 37 92 18 5
# That would delete those line numbers, which generally come from a
# error message after attempting an 'ssh' call.
function kd () { [[ -n "$1" ]] && sed -i -e ${1}d ~/.ssh/known_hosts; }
# Save some typing; I use this P4PORT a lot:
export P=public.perforce.com:1666
# Per-shell history.
[[ ! -d ~/.hist ]] && mkdir ~/.hist
export HISTFILESIZE=2500
export HISTFILE=~/.hist/history.$$.$RANDOM
# Generate a Swarm viewer URL
function surl () {
local depotPath=
local swarmBaseURL=
local swarmURL=
depotPath=${1:-Unset}
if [[ "$depotPath" != "//"* ]]; then
depotPath="$(p4 -ztag -F %depotFile% where "$depotPath")"
fi
swarmBaseURL="$(p4 -ztag -F %value% property -n P4.Swarm.URL -l)"
[[ -n "$swarmBaseURL" ]] || swarmBaseURL="https://swarm.perforce.com"
case "$depotPath" in
(*.md) swarmURL="$swarmBaseURL/files/${depotPath#//}#markdown";;
(*.html) swarmURL="$swarmBaseURL/view/${depotPath#//}";;
(*) swarmURL="$swarmBaseURL/files/${depotPath#//}";;
esac
echo "$swarmURL"
}
#------------------------------------------------------------------------------
# Function vshelf() - Verify shelf. Display diffs between current numbered
# pending changelist and shelved changelist of the same number.
#
# Usage Example: vshelf 27800
#
#------------------------------------------------------------------------------
function vshelf () {
local shelf="${1:-}"
local -i diffCount=0
if [[ -z "$shelf" ]]; then
echo -e "\\nUsage: vshelf CL\\n\\nwhere CL is the shelved changelist."
return
fi
for df in $(p4 -ztag -F %depotFile% files @=$shelf); do
path="$(p4 -ztag -F %path% where $df)"
echo "Checking: $path"
rm -f "${path}.${shelf}.diff.tmp"
p4 print -q -o "${path}.${shelf}.diff.tmp" "${path}@=${shelf}"
if ! diff -q "${path}.$shelf.diff.tmp" "${path}"; then
echo "Warning: Differences detected for: $path"
diff "${path}.$shelf.diff.tmp" "${path}"
diffCount+=1
fi
done
if [[ "$diffCount" -eq 0 ]]; then
echo -e "\\nNo diffs found.\\n"
else
echo -e "\\nWarning: found $diffCount diffs. Maybe force-reshelve or replace?\\n"
fi
}
__LocalCompletionDir="/usr/local/etc/bash_completion.d"
if [[ -d "$__LocalCompletionDir" ]]; then
# echo "Sourcing files in $__LocalCompletionDir:"
for f in $(cd "$__LocalCompletionDir"; ls); do
# echo "Sourcing: $__LocalCompletionDir/$f"
source "$__LocalCompletionDir/$f"
done
fi