NOTE : Get the Korn shell /bin/ksh by installing pdksh*.rpm from the Linux contrib cdrom
Save this file as a text file and chmod a+rx on it.
#!/bin/ksh
# CVS program scommit
# Program to commit the changes and check in the file into CVS
# Every filename is composed of 3 parts - Home directory, sub-directory
# and the filename. The full-path is $HOME/$subdir/$fname
# And in CVS the same directory structure is maintained (by
# variable $subdir) therefore in cvs we will have $CVSROOT/$subdir/$fname
# In this program these 4 variables $HOME, $CVSROOT, $subdir and $fname
# play a important role. For example, sample values can be like
# HOME=/home/aldev, subdir=myproject/src CVSROOT=/home/cvsroot
# and fname=foo.cpp
# Caution: Put double-quotes to protect the variables having
# spaces, like "$HOME/$subdir" if subdir is 'some foo.cpp'
cmdname=`basename $0`
Usage()
{
print "\nUsage: $cmdname [-r revision_number] <filename>"
print "The options -r are optional "
print "For example - "
print " $cmdname -r 1.1 foo.cpp"
print " $cmdname foo.cpp "
print " "
}
# Command getopt will not supported in next major release.
# Use getopts instead.
while getopts r: ii
do
case $ii in
r) FLAG1=$ii; OARG1="$OPTARG";;
?) Usage; exit 2;;
esac
done
shift ` expr $OPTIND - 1 `
#echo FLAG1 = $FLAG1 , OARG1 = $OARG1
if [ $# -lt 1 ]; then
Usage
exit 2
fi
if [ -d $1 ]; then
Usage
exit 2
fi
homedir=` echo $HOME | cut -f1 -d' ' `
if [ "$homedir" = "" ]; then
print "\nError: \$HOME is not set!!\n"
exit
fi
# Find sub-directory
cur_dir=`pwd`
#echo $cur_dir
len=${#homedir}
len=$(($len + 2))
#echo $len
subdir=` echo $cur_dir | cut -b $len-2000 `
tmpaa=`dirname $1`
if [ "$tmpaa" = "." ]; then
fname=$1
if [ "$subdir" = "" ]; then
subdir=$tmpaa
fi
else
fname=`basename $1`
if [ "$subdir" = "" ]; then
subdir=$tmpaa
else
subdir="$subdir/$tmpaa"
fi
fi
# echo "subdir is : " $subdir
# echo "fname is : " $fname
# If file is already checked out by another user....
cvs_root=` echo $CVSROOT | cut -f1 -d' ' `
if [ "$cvs_root" = "" ]; then
print "\nError: \$CVSROOT is not set!!\n"
exit
fi
mkdir -p "$CVSROOT/$subdir/Locks" 2>/dev/null
# CVS directory in your local directory is required for all commands..
if [ ! -d "$homedir/$subdir/CVS" ]; then
tmpaa=` (cd "$CVSROOT/$subdir"; find * -prune -type f -print | head -1 ) `
tmpbb=`basename $tmpaa | cut -d',' -f1 `
if [ "$tmpaa" = "" -o ! -f "$CVSROOT/$subdir/$tmpbb,v" ]; then
print "\nThe directory $homedir/$subdir/CVS does not exist"
print "You must do a sget on `basename $subdir` directory. Give -"
print " cd $homedir/`dirname $subdir` "
print " sget `basename $subdir` "
exit
else
# Now try to create CVS in local dir by sget
(
cd "$homedir"
if [ "$subdir" = "." ]; then # don't use dot, will mess up cvs
cvs -r checkout -A $tmpbb
else
cvs -r checkout -A "$subdir/$tmpbb"
fi
)
fi
fi
# Get the working revision number of the file....
# Use tmpfile as the arg cannot be set inside the sub-shell
tmpfile=$homedir/sedit-lock.tmp
\rm -f $tmpfile 2>/dev/null
if [ "$FLAG1" = "" ]; then
(
cd $homedir
if [ "$subdir" = "." ]; then # don't use dot, will mess up cvs
cvs status $fname 2>/dev/null | grep "Working revision:" | awk '{print $3}' >$tmpfile
else
cvs status "$subdir/$fname" 2>/dev/null | grep "Working revision:" | awk '{print $3}' >$tmpfile
fi
)
OARG1=`cat $tmpfile`
\rm -f $tmpfile 2>/dev/null
fi
if [ "$OARG1" = "" -o "$OARG1" = "New" -o "$OARG1" = "NEW" ]; then
print "The file $subdir/$fname is NEW, it is not in the CVS repository"
print "The OARG1 is $OARG1"
else
lockfile="$CVSROOT/$subdir/Locks/$fname-$OARG1"
if [ -e $lockfile ]; then
# Check if this revision is owned by you...
aa=` ls -l $lockfile | awk '{print $3}' `
userid=`id | cut -d'(' -f2 | cut -d')' -f1 `
if [ "$aa" != "$userid" ]; then
print " "
print "The file $subdir/$fname is NOT locked by you!!"
print "It is locked by unix user name $aa and your login name is $userid"
# print "If you are working concurrently with other developer"
# print "and you used -F option with sedit."
print "You need to wait untill other developer does scommit"
print "or sunlock"
print "Aborting the $cmdname ...."
print " "
exit 2
fi
else
# The file must exist in cvs
if [ -f "$CVSROOT/$subdir/$fname,v" ]; then
print "You did not lock the file $subdir/$fname with sedit!!"
print "Aborting the $cmdname ...."
exit 2
else
print "\nThe file $subdir/$fname does not exist in CVS repository yet!!"
print "You should have done sadd on $subdir/$fname ...."
exit 2
fi
fi
fi
# Operate inside sub-shell - and operate from root directory
(
cd $homedir
# Do not allow directory commits for now ...
#if [ -d "$subdir/$fname" ]; then
# cvs commit "$subdir/$fname"
#fi
if [ "$subdir" = "." ]; then # don't use dot, will mess up cvs
cvs commit $fname
else
cvs commit "$subdir/$fname"
fi
exit_status=$?
if [ $exit_status -eq 0 ]; then
lockfile="$CVSROOT/$subdir/Locks/$fname-$OARG1"
if [ -e $lockfile ]; then
\rm -f $lockfile
fi
# Must change the permissions on file in case
# there are no changes to file
chmod a-w "$HOME/$subdir/$fname"
print "\nDone $cmdname. $cmdname successful"
#print "\nTip (Usage): $cmdname <filename/directory name>\n"
fi
)