Posts tagged ‘SVN’

Nant SVN changes between two revisions checkout

I went looking for a nant script to checkout changed files between two revisions from SVN. I found some Ant / Java based solutions. As we are using nant I figured I would give it a shot writing it myself. It is easy to get a list of files from SVN using diff. However that doesn’t do you any good if you need to get them from SVN by hand one by one. The trick is to use the list you get from SVN diff and then loop each file and bring it down to the local machine. The biggest gotcha was of course space in file or directory names. If you notice in the script that’s taken care of by inserting %20 in the URL location and local file names are quoted with double quote when doing the SVN export. List of deleted files are written to deletedfiles.txt in the checkout directory, Enjoy !

<?xml version=“1.0”?>
<!–
  Exports changes between revision from the given repository URL to checkout Directory
–>
<project name=“SVN_changes”
   default=“getChanges”
   basedir=“.”>

  <!–
   Register the sys.* properties; most specifically, sys.env.* for all environment vars
  –>
  <sysinfo/>

  <!– Properties / defaults –>
  <property name=“checkoutDir” value=“svnchanges”/>
  <property name=“changelog” value=“${checkoutDir}\changelog.txt”/>
  <property name=“delLog” value=“${checkoutDir}\deletedfiles.txt”/>
  <property name=“delFile” value=“”/>
  <property name=“deleteIndicator” value=“D      “/>

  <!– url to svn repository –>
  <property name=“repoURL” value=“http://10.10.10.10/svn/myproject/trunk”/>
  <property name=“repoUser” value=“testUser” dynamic=“true” />
  <property name=“repoPassword” value=“testPassword” dynamic=“true” />
  <property name=“startingRevision” value=“29455” />
  <property name=“endingRevision” value=“30692” />

  <target name=“getChanges” description=“Get changes from Subversion given two revisions “  >

    <echo message=“Exporting files between revisions ${startingRevision} and ${endingRevision}” />
    <echo message=“from ${repoURL}” />
    <echo message=“to ${checkoutDir}/” />
    <echo message=“——-“ />
    <echo message=“stand by….. “ />
    <echo message=“——-“ />

    <!– Start with a fresh local checkout directory –>
    <mkdir dir=“${checkoutDir}” />

    <!– creates log file comparison from start to end revision –>
    <exec program=“svn”
      commandline=“diff -r ${startingRevision}:${endingRevision} –summarize ${repoURL}”
      output=“${changelog}” />

    <!– Loop the files in the changelog and process –>
    <foreach item=“Line” in=“${changelog}” delim=“,” property=“svnfile”>

      <!– Extract the file name –>
      <property name=“startpos” value=“${string::index-of(svnfile, repoURL) +  string::get-length(repoURL) }” />
      <property name=“length” value=“${string::get-length(svnfile) – int::parse(startpos)}” />
      <property name=“tmpFile” value=“${string::substring( svnfile, startpos, length)}” />
      <!– Space in URL covered –>
      <property name=“repoFile” value=“${string::replace( tmpFile, ‘ ‘, ‘%20’)}” />
      <!– Extract the dir –>
      <property name=“endpos” value=“${string::last-index-of( tmpFile, ‘/’)}” />
      <property name=“localDir” value=“${string::substring( tmpFile, 0, int::parse(endpos))}” />

      <!– Sep –>
      <echo message=“—“/>
     
      <!– Advertise deleted files between revisions –>
      <if test=“${ string::contains(svnfile, deleteIndicator)}”>
          <echo message=“Deleted file=${tmpFile}”/>
          <property name=“delFile” value=“${tmpFile}”/>
          <call target=“WriteToTextFile” />
      </if>

      <!– Files to bring down from repository  –>
      <if test=“${ not string::contains(svnfile, deleteIndicator)}”>
          <!–
            Create the dir before checkout, this might happen multible times
            for files in the same dir, don‘t fail it.
          –>
          <mkdir dir="${checkoutDir}${localDir}" failonerror="false"/>
       
          <!– Check out the file –>
          <echo message="svn export ${repoURL}${repoFile} &quot;${checkoutDir}${tmpFile}&quot; -r ${endingRevision} –force " />
          <exec program="svn"
                commandline="export ${repoURL}${repoFile} &quot;${checkoutDir}${tmpFile}&quot; -r ${endingRevision} –force"
                            failonerror="true"
          />
      </if>
    </foreach>

    <!– Show user that it’s all done –>
    <echo message=”   “ />
    <echo message=“——-“ />
    <echo message=“Changed and new files have been exported to the ${checkoutDir} directory” />
    <echo message=“List of deleted files can be found in the ${delLog} file” />
    <echo message=“——-“ />
    <echo message=“Done !” />
  </target>

  <!– Appends deleted files to text file –>
  <target name=“WriteToTextFile” >
    <script language=“C#” mainclass=“writeToDelLog”>
      <references>
        <include name=“System.Management.dll” />
      </references>
      <div class=“codesnip-container” ><![CDATA[
            class writeToDelLog
            {       
                public static void ScriptMain(Project project)
                {       
                                      PropertyDictionary pd = project.Properties;       
                    string sDeletedFile = pd[“delLog”];
                    string sDeletedEntry = pd[“delFile”];
                   
                    FileStream stream = new FileStream(sDeletedFile, FileMode.Append );
                    StreamWriter writer = new StreamWriter(stream);

                    try
                    {
                      writer.WriteLine(sDeletedEntry);
                    }
                    catch( Exception ex )
                    {
                      Console.WriteLine( “NOT able to write to file, error=” + ex );
                    }
                    finally
                    {
                      if( writer != null ) writer.Close();
                      if( stream != null ) stream.Close();
                    }
                           
                }
            }
        ]]></div>
    </script>
  </target>

</project>

Bundled Apache + SVN

I finally installed Subversion as my local repository. I had used the windows CollabNet server at work it works great, but I decided to try VisualSVN at home. I have to say I like VisualSVN better. With the latest version both of them give you the ability to install Apache during install, but VisualSVN takes the win with their management console. Very easy to use GUI, similar to the Microsoft Sql Server. From the console you can setup up your repositories, users and groups. It is also very convenient to setup group permissions for your repositories.

visualsvn

Note, when you install the Apache server wants to use port 80 or 443 / SSL port. Of course if you have IIS running you will most likely be using those ports for IIS. So either shut down IIS, or figure out which ones gets to use the default port.

Installing on Windows and using the VisualSVN console I had one user and two repositories up in about 5 minutes, very snappy indeed.

Apache http access for SVN Repository

I had to add http access for our SVN repository at work, which was happily serving on the default SVN port. However after changing to our new network the SVN port has been shut down and the IT guys are in no hurry to open it up. I thought I remembered Apache can be chained with SVN for http access, went looking and sure enough. So here is a quick step tutorial with minimum configuration if you need to do the same.



1 – Download and install Apache 2.2
http://httpd.apache.org/download.cgi
I’m installing at c:\apps\apache2.



2 – Copy modules from Subversion to Apache modules
c:\apps\subversion\bin\mod_dav_svn.so
c:\apps\subversion\bin\mod_authz_svn.so
to
c:\apps\apach2\modules

and
c:\apps\subversion\bin\libdb44.dll
c:\apps\subversion\bin\intl3_svn.dll
to
c:\apps\apache2\bin
if needed



3 – Modify the Apache httpd.conf to load the SVN modules
Add the following two lines to your
c:\apps\apache2\conf\httpd.conf
file, just look for all of the lines that start with LoadModule, put them at the end of the list

LoadModule dav_svn_module modules/mod_dav_svn.so
LoadModule authz_svn_module modules/mod_authz_svn.so

Now un-comment this line in httpd.conf, it will look like this
LoadModule dav_module modules/mod_dav.so



4 – Add your svn location info to httpd.conf
At the very bottom of your httpd.conf file add these lines:

<Location /svn>
DAV svn
SVNParentPath  c:\apps\subversion\svn_repos

AuthType Basic
AuthName "Subversion Repository"
AuthUserFile conf/password.pass
Require valid-user
</Location>



5 – Create an Apache password file
To allow Apache to handle authentication for you, you’ll need to create a password file. Open a command prompt,
cd c:\apps\apache2\bin
then
htpasswd –c passwords.pass newuser

Enter the password for newuser, to add additional users

htpasswd passwords.pass anotherUser

When you have added all the users copy the passwords.pass file over to the
c:\apps\apache2\conf
directory



6 – Start the Apache service from Services
If you get errors open dos window and run Apache manually
c:\apps\apache2\bin\httpd.exe
You should see the errors reported.



7 – Access your repository over http
With Tortiese goto the repo browser, enter the appropriate path, something like the following.
http:///10.0.0.1/svn/yourRepo
You will be prompted for the user and password and should be all good to go. You should also be able to open the location in a web browser.



This quick step guide is based on the more comprehensive guide at inetsolution. That guide is great and covers setup of SVN+Apache, SSL access and group permissions.