#!/bin/sh
#
# Objective:
# UPDATE_DEPOT will update the depot by adding, deleting, or editing files
# in the depot to match the client workspace.
#
# Parameters:
# -r will cause the update to update all sub directories as well.
# $1 is the P4 workspace location from the client perspective.
# $2 if given, is the client spec.
#
# Assumptions:
# The file creation and modification takes place in the P4 workspace prior
# to expruting this command.
prog=`basename $0`
usage()
{
echo
echo "usage: $prog [-r|-h] <perforce workspace> [client spec]"
echo
echo "-h : print this message."
echo "-r : recursively update files in all subdirectories."
echo "<perforce workspace> : the file location on the client."
echo "[client spec] : the client to use for update."
exit
}
echo "Executing $0 "$@""
if [ "$DEBUG" = "0" ]
then
set -vx
fi
if [ ! -d "$LOG_DIR" ]
then
LOG_DIR="."
fi
if [ $# -lt 1 -o $# -gt 3 ]
then
usage
fi
if [ "$1" = "-h" ]
then
echo
echo $prog will update the depot to match the client workspace.
usage
fi
p4_option="-x -"
p4_client=""
if [ $1 = "-c" ]
then
shift
p4_client="-c $1"
p4_option="$p4_client $p4_option"
shift
fi
descend=true
wspace=`echo $1 | sed 's/\\\/\//g'`
trail=$wspace
add_trail=`echo $wspace | sed 's/\.\.\.$//'`
if [ ! -f "$add_trail" -a ! -d "$add_trail" ]
then
echo "$prog, Error: Workspace must be a file or directory on the client"
exit 1
fi
# if it is a file then do not recurse directories
# else assume that it ends in ... for recurse option.
if [ "$add_trail" = "$trail" ]
then
descend=false
if [ -d "$add_trail" ]
then
tmp_trail=`echo $add_trail | grep /$` || tmp_trail="$add_trail/"
add_trail=$tmp_trail
trail=${add_trail}*
fi
fi
p4 -s $p4_client flush $trail > /dev/null
stat=$?
if [ $stat -ne 0 ]
then
echo "$prog, Error: command p4 $p4_client flush $trail failed"
exit 1
fi
echo "Looking for new files"
# add all new files
# ignore "can't add existing file message" if I eliminate it with grep
# then grep returns an exit code of 1"
ignore='t add existing file| not in client view.'
errmsg="$prog, Error: Failed trying to add new files."
if [ "$descend" = "true" ]
then
find $add_trail -type f -print | \
p4 -s $p4_option add | \
egrep -v "$ignore"
else
if [ -f "$add_trail" ]
then
p4 -s $p4_client add $add_trail | \
egrep -v "$ignore"
elif [ -d "$add_trail" ]
then
ls -p $add_trail | grep -v /$ | \
awk -v dir=$add_trail '{print dir$1}' | \
p4 -s $p4_option add | \
egrep -v "$ignore"
else
echo "$prog, Error: Workspace must be a file or directory"
exit 1
fi
fi
#grep sends a status of 2 on errors
if [ $? -gt 1 ]
then
echo $errmsg
stat=1
fi
# Perforce with -s option will echo to standard out
# error: a.a - file(s) not on client.
# error: update_depot - file(s) up-to-date.
# and exit with status 0.
# without the -s option it echos to standard error.
# These warnings can be ignored with p4 edit and delete.
ignore=' not on client.| up-to-date.|exit:'
echo "Looking for modified files"
errmsg="$prog, Error: Failed trying to edit files for update."
# add modified files
#p4 $p4_client diff -se $trail 2>$errors | p4 -s $p4_option edit 2>$errors
p4 -s $p4_client diff -se $trail | \
egrep -v "$ignore" | \
cut -d" " -f2- | \
p4 -s $p4_option edit
stat=`expr $stat + $?`
echo "Looking for deleted files"
errmsg="$prog, Error: Failed trying to edit files for delete."
# delete files
#p4 $p4_client diff -sd $trail 2>$errors | p4 -s $p4_option delete 2>$errors
p4 -s $p4_client diff -sd $trail |\
egrep -v "$ignore" | \
cut -d" " -f2- | \
p4 -s $p4_option delete
stat=`expr $stat + $?`
if [ $stat -ne 0 ]
then
echo $prog ended with error
fi
exit $stat
# The following is not used. This was the first implementation of this
# program. I'm leaving it here for reference.
##ADD_CHANGES()
##{
# Parameters:
# $1 Location of files to check in
# $2 Location of client files mapped from perforce.
# $3 Client
# Idea:
# 1. Output changes to $1 ( done prior to running this subroutine )
# 2. Sync files from Perforce to $2 (Done prior to running this subroutine )
# 3. Compute the difference between $1 and $2
# 4. Move $1 to $2
# 5. Check in files using result of #3.
## if [ $# -eq 3 ]
## then
## P4CLIENT=$3
## export P4CLIENT
## fi
## $work_dir/cmpdir $1 $2
## stat=$?
## mv -fr $1 $2
## stat=`expr $? + $stat`
## p4 -s -x $WORK_DIR/new.o add
## stat=`expr $? + $stat`
## p4 -s -x $WORK_DIR/del.o delete
## stat=`expr $? + $stat`
## p4 -s -x $WORK_DIR/mod.o edit
## stat=`expr $? + $stat`
## exit $stat