SRCS – diff

I am writing a script to simulate a Version Control System.
In SRCS – Check in file – It implements the initial and check in a file.
In SRCS – check out file – It implements check out a file with a specified version.
In SRCS – log – It implements a ‘log’ command to allow user to print all the metadata for a file.

In this post I will implement a diff command lists file changes.

For example,
The following command will provide a list of differences between file admin/sql/test1.sql revisions 5 and 6:
srcs diff admin/sql/test1.sql 5 6
The following example, you receive a list of differences between revision 5 and the local copy:
srcs diff admin/sql/test1.sql 5


_PROD="/u01/camel/12.0"
_DEBUG="on"
_LOG="_srcs.log"

fname_srcs="$_PROD/$2"
fname=$(basename "${fname_srcs}")
fdir=`echo "${fname_srcs}" | sed 's/'"$fname"'$//'`
fver="$fdir/RCS/${fname},v"
	
function DEBUG()
{
	[ "$_DEBUG" == "on" ] && $@ || :
}

function checkout()
{
	version=`tail -3 $fver | egrep "^version#" | sed 's/^version#//'`
	cp $fname_srcs ${fname}.tmp.$1
	for (( v=$version; v>$1; v-- ))
	do
		let delta=$v-1
		_l1=`nl "$fver" | egrep "version#$delta" | sed "s/version#$delta//" | sed 's/^\s*|\s*$//g'`
		_l2=`nl "$fver" | egrep "version#$v" | sed "s/version#$v//" | sed 's/^\s*|\s*$//g'`
		sed -n "${_l1},${_l2}p" "$fver" | patch ${fname}.tmp.$1 -i - >> /dev/null
	done
}

if [ $1 == "diff" ]; then
	echo "Diff $2 $3 $4 from SRCS..." | tee -a $_LOG
	if [ "$#" -lt 3 ]; then
		echo "Please provide at least one revision for diff...<FAILED>" | tee -a $_LOG
		exit 1
	fi
	if [ ! -f "$fver" ]; then
		echo "$fname doesn't exist in SRCS...<FAILED>" | tee -a $_LOG
		exit 1
	else
		if egrep -v "^\s*$" $fver | wc -l | egrep "^0" > /dev/null ; then 
			echo "$fname is just initialled in SRCS...<FAILED>" | tee -a $_LOG
			exit 1
		else
			if ! cat $fver | egrep "^version#$3" > /dev/null ; then
				echo "Version $3 doesn't exist...<FAILED>" | tee -a $_LOG
				exit 1
			else
				checkout $3
			fi
		fi
		if [ "$#" -lt 4 ]; then 	
			echo "Diff $2 $3 from SRCS with the local copy..." | tee -a $_LOG
			if [ ! -f "$fname" ]; then
				echo "$fname doesn't exist in local directory...<FAILED>" | tee -a $_LOG
				exit 1
			fi
			diff ${fname}.tmp.$3 $fname | tee -a $_LOG
			echo "Diff $2 $3 from SRCS with the local copy...<SUCCESSED>" | tee -a $_LOG
		else 
			if ! cat $fver | egrep "^version#$4" > /dev/null ; then
				echo "Version $4 doesn't exist...<FAILED>" | tee -a $_LOG
				exit 1
			fi
			checkout $4
			diff ${fname}.tmp.$3 ${fname}.tmp.$4 | tee -a $_LOG
			echo "Diff $2 $3 $4 from SRCS...<SUCCESSED>" | tee -a $_LOG
		fi
	fi
fi

Test Result:

-bash-3.2$ sh srcs.sh diff admin/sql/test2.sql
Diff admin/sql/test2.sql   from SRCS...
Please provide at least one revision for diff...<FAILED>
-bash-3.2$ sh srcs.sh diff admin/sql/test1.sql
Diff admin/sql/test1.sql   from SRCS...
Please provide at least one revision for diff...<FAILED>
-bash-3.2$ sh srcs.sh diff admin/sql/test2.sql
Diff admin/sql/test2.sql   from SRCS...
Please provide at least one revision for diff...<FAILED>
-bash-3.2$ sh srcs.sh diff admin/sql/test2.sql 3
Diff admin/sql/test2.sql 3  from SRCS...
test2.sql is just initialled in SRCS...<FAILED>
-bash-3.2$ sh srcs.sh diff admin/sql/test2.sql 3 5
Diff admin/sql/test2.sql 3 5 from SRCS...
test2.sql is just initialled in SRCS...<FAILED>
-bash-3.2$ sh srcs.sh diff admin/sql/test1.sql 6
Diff admin/sql/test1.sql 6  from SRCS...
Diff admin/sql/test1.sql 6 from SRCS with the local copy...
2,7c2,3
<  I am Version 6
< ahoghgoh
< gegoruaon
<  hello version6
< haxialg xn
< this line is new in version 6
---
> ### I am Version 1
> ### This line should be the same
Diff admin/sql/test1.sql 6 from SRCS with the local copy...<SUCCESSED>
-bash-3.2$ sh srcs.sh diff admin/sql/test1.sql 5
Diff admin/sql/test1.sql 5  from SRCS...
Diff admin/sql/test1.sql 5 from SRCS with the local copy...
2,5c2,3
<  I am Version 5
<  hello version5
< haxialg xn
< this line is new in version 5
---
> ### I am Version 1
> ### This line should be the same
Diff admin/sql/test1.sql 5 from SRCS with the local copy...<SUCCESSED>
-bash-3.2$ sh srcs.sh diff admin/sql/test1.sql 53
Diff admin/sql/test1.sql 53  from SRCS...
Version 53 doesn't exist...<FAILED>
-bash-3.2$ sh srcs.sh diff admin/sql/test1.sql 3
Diff admin/sql/test1.sql 3  from SRCS...
Diff admin/sql/test1.sql 3 from SRCS with the local copy...
2,4c2,3
<  I am Version 3
< ### This line should be the different
< this line is new in version 3
---
> ### I am Version 1
> ### This line should be the same
Diff admin/sql/test1.sql 3 from SRCS with the local copy...<SUCCESSED>
-bash-3.2$ sh srcs.sh diff admin/sql/test1.sql 2
Diff admin/sql/test1.sql 2  from SRCS...
Diff admin/sql/test1.sql 2 from SRCS with the local copy...
2c2
< ### I am Version 2
---
> ### I am Version 1
4d3
< this line is new in version 2
Diff admin/sql/test1.sql 2 from SRCS with the local copy...<SUCCESSED>
-bash-3.2$ sh srcs.sh diff admin/sql/test1.sql 1
Diff admin/sql/test1.sql 1  from SRCS...
Diff admin/sql/test1.sql 1 from SRCS with the local copy...
Diff admin/sql/test1.sql 1 from SRCS with the local copy...<SUCCESSED>
-bash-3.2$ sh srcs.sh diff admin/sql/test1.sql 6 4
Diff admin/sql/test1.sql 6 4 from SRCS...
2,7c2,4
<  I am Version 6
< ahoghgoh
< gegoruaon
<  hello version6
< haxialg xn
< this line is new in version 6
---
>  I am Version  4
>  hello version4
> this line is new in version 4
Diff admin/sql/test1.sql 6 4 from SRCS...<SUCCESSED>
-bash-3.2$