#!/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 ..." wget -q -O $P4BIN ftp://ftp.perforce.com/perforce/r14.1/$MYPLAT/p4 chmod a+x $P4BIN wget -q -O $P4DBIN ftp://ftp.perforce.com/perforce/r14.1/$MYPLAT/p4d chmod a+x $P4DBIN echo "Done." fi P4CONFIG=".myp4" P4USER="$USER" P4CLIENT="$USER" 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 echo "Not within a Perforce storage system, perhaps you meant to run init" exit 1 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 $P4BIN key "branchId-1" master > /dev/null $P4BIN key "branch-master" 1 > /dev/null exit 0 fi export P4ROOT=$rootDir export P4IGNORE=.myp4ignore export P4PORT="rsh:$P4DBIN -Llog_file -i -vserver=3" case $1 in init) echo "Already within a Perforce storage structure: $rootDir" exit 1 ;; branches) $P4BIN keys -e 'branchId-*' | sed -e 's/branchId-[0-9]* = //' exit 0 ;; switch) curBranchId=`$P4BIN client -o | \ egrep "//depot/[0-9]+/... " | \ sed -e 's=^.*//depot/==' -e 's=/[.][.][.].*$=='` curBranch=`$P4BIN key "branchId-$curBranchId"` if [ "$2" == "" ]; then echo "Current branch $curBranch maps to //depot/$curBranchId" exit 0 fi branchId=`$P4BIN key "branch-$2"` if [ "$branchId" == '0' ]; then echo "Branch $2 does not exist, yet" exit 1 fi if [ "$branchId" == "$curBranchId" ]; then echo "Already there, wasn't that fast?" exit 0 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?" read -p '(s) Shelve changes for later (r) Revert changes (m) Move changes (q) Quit? ' \ answer case $answer in s) newChange=`$P4BIN change -o | \ sed -e "s//$curBranch shelf/" | \ $P4BIN change -i | sed -e 's/^Change //' -e 's/ .*//'` $P4BIN reconcile -c $newChange //$P4CLIENT/... $P4BIN shelve -c $newChange > /dev/null $P4BIN key "branchIdShelf-$curBranchId" $newChange > /dev/null $P4BIN revert -w //depot/$curBranchId/... > /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/$curBranchId/... else echo "Aborting..." exit 1 fi ;; m) echo MOVE exit 1 # TBD ;; q) exit 0 ;; esac fi echo "Switch from $curBranch to $2" $P4BIN client -o | \ sed -e "s=//depot/[0-9][0-9]*/... //$USER/...=//depot/$branchId/... //$USER/...=" | \ $P4BIN client -i > /dev/null $P4BIN sync -q //$P4CLIENT/... 2> /dev/null curShelf=`$P4BIN key "branchIdShelf-$branchId"` 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 "branchIdShelf-$branchId" > /dev/null fi exit 0 ;; branch) curBranchId=`$P4BIN client -o | \ egrep "//depot/[0-9]+/... " | \ sed -e 's=^.*//depot/==' -e 's=/[.][.][.].*$=='` curBranch=`$P4BIN key "branchId-$curBranchId"` if [ "$2" == "" ]; then echo "Current branch $curBranch maps to //depot/$curBranchId" exit 0 fi branchId=`$P4BIN key "branch-$2"` if [ "$branchId" != '0' ]; then echo "Existing branch $2 maps to //depot/$branchId" echo "Perhaps you meant to say: p4 switch $2" 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?" read -p '(s) Shelve changes for later (r) Revert changes (m) Move changes (q) Quit? ' \ answer case $answer in s) newChange=`$P4BIN change -o | \ sed -e "s//$curBranch shelf/" | \ $P4BIN change -i | sed -e 's/^Change //' -e 's/ .*//'` $P4BIN reconcile -c $newChange //$P4CLIENT/... $P4BIN shelve -c $newChange > /dev/null $P4BIN key "branchIdShelf-$curBranchId" $newChange > /dev/null $P4BIN revert -w //depot/$curBranchId/... > /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/$curBranchId/... else echo "Aborting..." exit 1 fi ;; m) echo MOVE ;; q) exit 0 ;; esac fi emptyBranch=`$P4BIN -ztag files -m1 ... 2> /dev/null | egrep '^[.][.][.]' | wc -l` branchId=`$P4BIN key -i "branchId"` if [ $emptyBranch == 0 ]; then echo "No files to branch" else $P4BIN populate -d "Branching $curBranch ($curBranchId) to $2 ($branchId)" \ //depot/$curBranchId/... //depot/$branchId/... if [ $? != 0 ]; then echo "Failed to create branch $2 mapping to //depot/$branchId" exit 1 fi fi echo "Switch from $curBranch to $2" $P4BIN key "branch-$2" "$branchId" > /dev/null $P4BIN key "branchId-$branchId" "$2" > /dev/null $P4BIN client -o | \ sed -e "s=//depot/[0-9][0-9]*/... //$USER/...=//depot/$branchId/... //$USER/...=" | \ $P4BIN client -i > /dev/null $P4BIN sync -q //$P4CLIENT/... 2> /dev/null exit 0 ;; *) $P4BIN $P4TAGGED "$@" ;; esac