#!/bin/bash MYP4BIN=$HOME/bin MYPLAT="bin.darwin90x86_64" P4BIN=$MYP4BIN/p4.bin P4DBIN=$MYP4BIN/p4d.bin if [ ! -f $P4BIN ]; then echo "Downloading required Perforce binaries for $MYPLAT ..." which wget > /dev/null if [ -z $? ]; then wget -q -O $P4BIN ftp://ftp.perforce.com/perforce/r14.1/$MYPLAT/p4 wget -q -O $P4DBIN ftp://ftp.perforce.com/perforce/r14.1/$MYPLAT/p4d else curl -q -o $P4BIN ftp://ftp.perforce.com/perforce/r14.1/$MYPLAT/p4 curl -q -o $P4DBIN ftp://ftp.perforce.com/perforce/r14.1/$MYPLAT/p4d fi chmod a+x $P4BIN chmod a+x $P4DBIN echo "Done." fi export P4CONFIG=".myp4" rootDir=`$P4BIN -ztag set | \ egrep '^P4CONFIG=.myp4' | \ sed -e 's/^P4CONFIG=.myp4..config..//' -e "s/')//" -e 's/myp4$/myp4d/'` if [ "$rootDir" == 'noconfig' ]; then if [ "$1" != 'init' ]; then $P4BIN "$@" exit $? fi echo 'Initializing...' baseDir="$PWD" if [ "$2" != '' ]; then baseDir="$PWD/$2" fi rootDir="$baseDir/.myp4d" if [ -d $rootDir ]; then echo "Perforce storage already maps to $rootDir" exit 1 fi mkdir -p $rootDir cat > $baseDir/.myp4 < $baseDir/.myp4ignore < $baseDir/.gitignore < /dev/null | wc -l` if [ $work -eq 0 ]; then work=`$P4BIN -ztag status //$P4CLIENT/... 2> /dev/null | wc -l` fi if [ $work -gt 0 ]; then echo "There are pending changes, how do you want to proceed?" # One day add in (m) Move changes read -p '(s) Shelve changes for later (r) Revert changes (q) Quit? ' \ answer case $answer in s) newChange=`$P4BIN change -o | \ sed -e "s//Shelf for $curBranch/" | \ $P4BIN change -i | sed -e 's/^Change //' -e 's/ .*//'` $P4BIN reconcile -c $newChange //$P4CLIENT/... $P4BIN shelve -c $newChange > /dev/null $P4BIN key "branchShelf-$curBranch" $newChange > /dev/null $P4BIN revert -w //depot/$curBranch/... > /dev/null ;; r) read -p 'New files will be deleted, edits discarded, etc. Are you sure? ' \ answer if [ "$answer" == "y" ]; then $P4BIN reconcile //$P4CLIENT/... > /dev/null 2> /dev/null $P4BIN revert -w //depot/$curBranch/... else echo "Aborting..." exit 1 fi ;; q) exit 0 ;; esac fi echo "Switch from $curBranch to $toBranch" $P4BIN client -o | \ sed -e "s=//depot/[^/]*/... //$USER/...=//depot/$toBranch/... //$USER/...=" | \ $P4BIN client -i > /dev/null $P4BIN sync -q //$P4CLIENT/... 2> /dev/null curShelf=`$P4BIN key "branchShelf-$toBranch"` if [ $curShelf != 0 ]; then echo "Unshelving $curShelf" $P4BIN unshelve -s $curShelf $P4BIN shelve -d -c $curShelf > /dev/null $P4BIN change -d $curShelf > /dev/null $P4BIN key -d "branchShelf-$toBranch" > /dev/null fi exit 0 ;; branch) newBranch=$2 if [ "$newBranch" == "" ]; then echo "Current branch $curBranch maps to //depot/$curBranch" exit 0 fi if [ -d $rootDir/depot/$newBranch ]; then echo "Existing branch $newBranch maps to //depot/$newBranch" echo "Perhaps you meant to say: p4 switch $newBranch" exit 1 fi work=`$P4BIN -ztag opened //$P4CLIENT/... 2> /dev/null | wc -l` if [ $work -eq 0 ]; then work=`$P4BIN -ztag status //$P4CLIENT/... 2> /dev/null | wc -l` fi if [ $work -gt 0 ]; then echo "There are pending changes, how do you want to proceed?" # One day add in (m) Move changes read -p '(s) Shelve changes for later (r) Revert changes (q) Quit? ' \ answer case $answer in s) newChange=`$P4BIN change -o | \ sed -e "s//Shelf for $curBranch/" | \ $P4BIN change -i | sed -e 's/^Change //' -e 's/ .*//'` $P4BIN reconcile -c $newChange //$P4CLIENT/... $P4BIN shelve -c $newChange > /dev/null $P4BIN key "branchShelf-$curBranch" $newChange > /dev/null $P4BIN revert -w //depot/$curBranch/... > /dev/null ;; r) read -p 'New files will be deleted, edits discarded, etc. Are you sure? ' \ answer if [ "$answer" == "y" ]; then $P4BIN reconcile //$P4CLIENT/... > /dev/null 2> /dev/null $P4BIN revert -w //depot/$curBranch/... else echo "Aborting..." exit 1 fi ;; q) exit 0 ;; esac fi emptyBranch=`$P4BIN -ztag files -m1 ... 2> /dev/null | egrep '^[.][.][.]' | wc -l` if [ $emptyBranch == 0 ]; then echo "No files to branch" else $P4BIN populate -d "Branching $curBranch to $newBranch" \ //depot/$curBranch/... //depot/$newBranch/... if [ $? != 0 ]; then echo "Failed to create branch $2 mapping to //depot/$newBranch" exit 1 fi fi mkdir -p $rootDir/depot/$newBranch echo "Switch from $curBranch to $newBranch" $P4BIN client -o | \ sed -e "s=//depot/[^/]*/... //$USER/...=//depot/$newBranch/... //$USER/...=" | \ $P4BIN client -i > /dev/null $P4BIN sync -q //$P4CLIENT/... 2> /dev/null exit 0 ;; *) $P4BIN $P4ENVARGS "$@" ;; esac