SRCS – check out file

I just started up writing a script to simulate a basic Source Version Control tool. In http://luohuahuang.com/2014/05/17/srcs-startup/ I prepared an experimental script to do initial and checkin activities. Now in this post I will try the check out function.
Basically the check out function will support:
1# If the version is the latest, just copy the latest from repository to local.
2# otherwise check out the specified version.

I will use Linux patch command to apply the diff content to the latest file to get the specified version.
The logic:
1# copy the latest file to local.
2# run patch command to apply diff in a loop way until it reaches the specified version. For example,

-bash-3.2$ cat 2.sql
hello world
no wrong v4
new line in v4
select * from table2;
-bash-3.2$ vi diff.txt
-bash-3.2$ cat diff.txt | patch 2.sql -i -
patching file 2.sql
-bash-3.2$ cat 2.sql
hello world
no wrong
new line in v3
select * from table2;

Note:
-i patchfile or –input=patchfile
Read the patch from patchfile. If patchfile is -, read from standard input, the default.

So in the loop, I will extract the diff content from filename,v and output it into standard input for patch command.

The logic:

1. if it checks out a file doesn’t have history in repository. Failed.

2. if it checks out the file without specifying a version or with the latest version. Check out the latest version.

3. if it chceks out the file by specifying a version. If the version doesn’t exist, Failed. Otherwise check out the specified version.

 


_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" ] && $@ || :
}

if [ $1 == "co" ]; then
	echo "Checkout $2 $3 from SRCS..." | tee -a $_LOG
	if [ ! -f "$fver" ]; then
		echo "$fname doesn't exist in SRCS. Run init and ci command to version control it firstly...<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. Run ci command to version control it firstly...<FAILED>" | tee -a $_LOG
		elif ! cat $fver | egrep "^version#$3" > /dev/null ; then
			echo "Version $3 doesn't exist...<FAILED>" | tee -a $_LOG
			exit 1
		elif [ "$#" -lt 3 ]; then
			cp $fname_srcs $fname
			echo "Checkout $2 $3 from SRCS...<SUCCESSED>" | tee -a $_LOG
		else
			version=`tail -3 $fver | egrep "^version#" | sed 's/^version#//'`
			cp $fname_srcs $fname
			for (( v=$version; v>$3; 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 -i - >> /dev/null
			done
			echo "Checkout $2 $3 from SRCS...<SUCCESSED>" | tee -a $_LOG
		fi
	fi
fi

Test data:

Below data is recorded by check in script posted in http://luohuahuang.com/2014/05/17/srcs-startup/ in the filename,v file. It basically records all of the diff changes.
####################################################################

### This is test1.sql file
### I am Version 1
### This line should be the same
###

comment: initial checkin for test1.sql

####################################################################

### This is test1.sql file
### I am Version 2
### This line should be the same
this line is new in version 2
###

comment: hello version 2

####################################################################

### This is test1.sql file
I am Version 3
### This line should be the different
this line is new in version 3
###

comment: okie, version 3

####################################################################

### This is test1.sql file
I am Version 4
hello version4
this line is new in version 4
###

comment: i am version 4.

####################################################################

### This is test1.sql file
I am Version 5
hello version5
haxialg xn
this line is new in version 5
###

comment: hello hello version 5

####################################################################

### This is test1.sql file
I am Version 6
ahoghgoh
gegoruaon
hello version6
haxialg xn
this line is new in version 6
###

version 6 here…

Test Result:

-bash-3.2$ sh srcs.sh co admin/sql/test1.sql 4
Checkout admin/sql/test1.sql 4 from SRCS…
Checkout admin/sql/test1.sql 4 from SRCS…<SUCCESSED>
-bash-3.2$ cat test1.sql
### This is test1.sql file
I am Version 4
hello version4
this line is new in version 4
###
-bash-3.2$ rm test1.sql
-bash-3.2$ sh srcs.sh co admin/sql/test1.sql 2
Checkout admin/sql/test1.sql 2 from SRCS…
Checkout admin/sql/test1.sql 2 from SRCS…<SUCCESSED>
-bash-3.2$ cat test1.sql
### This is test1.sql file
### I am Version 2
### This line should be the same
this line is new in version 2
###
-bash-3.2$ sh srcs.sh co admin/sql/test1.sql 1
Checkout admin/sql/test1.sql 1 from SRCS…
Checkout admin/sql/test1.sql 1 from SRCS…<SUCCESSED>
-bash-3.2$ cat test1.sql
### This is test1.sql file
### I am Version 1
### This line should be the same
###
-bash-3.2$ sh srcs.sh co admin/sql/test1.sql 0
Checkout admin/sql/test1.sql 0 from SRCS…
Version 0 doesn’t exist…<FAILED>

From the test result, the check out script is working!

So far, the scripts can simulate below version control functions:
1. initial a file in the version repository.
2. check in a file into the repository and the repository records the changes.
3. check out a file by specifying a version.
4. It is only for text-based files.

 

Todo list:

1. Lock and unlock
2. Access control
3. branching
4. .etc

I should complete this in later posts.