SVN – some quick commands during merge.

Following are some of the frequently used svn commands during merging and branching. I used to work with tortoise for this but as soon as I learned these, it feels like a more easier space to be in. Although no points taken away from tortoise, it still works pretty good for the gui part, This article is more targeted towards dark screen lovers.

Create a new branch from trunk:
If you want to create a branch from a specific revision of Trunk following command is handy. It does a remote copy. which means the machine you are on does not need a copy of the whole tree.

$ svn copy -r REVISION TRUNK_URL NEW_BRANCH_URL --username USERNAME --password PASSWORD -m MESSAGE

An example

$ svn copy -r 1234 http://shaafshah.com/trunk http://shaafshah.com/branches/MY_BRANCH --username foo --password bar -m "Remote copy"

When was this branch created?
If you want to know the day branch was created.

$ svn log -v --stop-on-copy BRANCH_URL

The last record will show you the day the branch as created.

List all the branches:
If you want to take a listing of branches or a tree

$ svn ls BRANCHES_URL

e.g. http://shaafshah.com/branches

Merge from Branch to Trunk:

Browse to where you have checkedout trunk in the local directory.

$ cd shaafshah.com/trunk

Update trunk to HEAD.

$ svn update
At revision 1234.

Following will merge from branch to trunk but will not commit.

$ svn merge -r BRANCH_REVISION:TRUNK_REVISION BRANCH_URL

Branch_REVISION will be the revision branch was created if this is the first time you are doing the merge.

$ svn merge -r 1233:1234 http://shaafshah.com/branches/my_new_branch

After this you should do an

svn status

to check the status of the files. the files will be marked with following Characters.

‘A’ Added
‘C’ Conflicted
‘D’ Deleted
‘I’ Ignored
‘M’ Modified

If there is any ‘C’ in the status the files will not be committed if you try an svn commit to save the merge to the trunk.

Merge from Trunk to Branch:

To merge from Trunk to branch you would need to browse to the branch checked out in the local direcotry.

$ cd shaafshah.com/branches/mybranch
$ svn update
At revision 1234.

The following command will try merging trunk from revision 1233 i.e. the day branch was created to branch head.

$ svn merge -r 1233:HEAD TrunkURL BRANCH_URL

Hopefully this should help. However you should definitely refer to SVN Book for more detail.

svnbook at http://svnbook.red-bean.com/

Creating an SVN wrap for your build using Ant

Today after along break I would jump right on to one of the interesting topics in my den these days.

One of my friends was lately troubled with doing some SVN stuff like merging etc. And a lot of people will agree with me on their first experiences. 🙂 I think.

While Automated builds take a lot of our time I thought I could plug in with some automated merging and a few other tasks. Its hard to go over all of that in one post but I will try putting in some basic stuff to get us started.

I call it the SVN Wrap.

Step 1.
Create a simple script file for wrapping svn and our environment.

A simple bat script could look like the following

@echo off
svn %*

However some people might want to add some environment variables to it. And that is where the strength of the this file comes in. You can tailor the environment dynamically!
e.g.

set LC_ALL=C
set SVN_HOME=svn-win32-1.4.6
set PATH=%SVN_HOME%\bin;%PATH%;

FYI: By setting LC_ALL I am telling the system I disregard the default locale. Its just used as an example here. for more information refer to the svnbook at http://svnbook.red-bean.com/

Step 2.
Create the build.xml
It doesnt get simpler then this.

I have created a project with the name CI-Test

<project name="CI-Test" default="status" basedir=".">
	<description>
		This is a POC for SVN Wrap.
	</description>
</project>

Importantly I am setting a property local.branch so that I can tell svn where my code has been checked out locally.

And finally the target that will take a status of the branch. for more details on the status command you could go here.

In general this target will give a general overview of the files and thier state at the moment.

	<target name="status">
		<echo message="Following is the status for this tree."/>
		<echo message="output is logged here: status.out" />
	          <exec dir="${local.branch}" executable="ci-svn.bat" output="status.out">
            		<arg line="status"/>
          	</exec>
	</target>

With in the target there are a few echo commands but the key construct is the exec.
The exec is going to do the following

dir=”${local.branch}” – would meant execute the command in this directory
executable=”ci-svn.bat” – Identifies the executable
and finally the output attribute to show where the output of this activity goes.
The following line will pass the parameters to the executable
< arg line=”status” />
And in this case its sending a status command to svn.

For more details on exec goto. http://ant.apache.org/manual/

Now I would presume both these files in saved in one direcotry as

1. ci-svn.bat
2. build.xml

To run this simply goto your command console and once in the same directory execute by running
ant

It is assumed that all paths to java,ant and svn are set on your console or system.

As an output you should be able to see a status.out in the same directory from where you executed ant.

Hopefully this should get you started with doing some bit of svn commands from ant. And that just opens a lot of more possibilities in your build environment.

The complete code listing of the build file is as follows.

<project name="CI-Test" default="status" basedir=".">
	<description>
		This is a POC for SVN Wrap.
	</description>
 
	<property name="local.branch" value="C:\branches\my-branch"/>
 
	<target name="status">
		<echo message="Following is the status for this tree."/>
		<echo message="output is logged here: status.out" />
	          <exec dir="${local.branch}" executable="ci-svn.bat" output="status.out">
            		<arg line="status"/>
          	</exec>
	</target>
 
</project>