Playing with Java and SVN

Playing with Java and SVN:



package svn;

import java.io.File;



import org.tigris.subversion.javahl.ClientException;

import org.tmatesoft.svn.core.SVNCommitInfo;

import org.tmatesoft.svn.core.SVNDepth;

import org.tmatesoft.svn.core.SVNException;

import org.tmatesoft.svn.core.SVNURL;

import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;

import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;

import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;

import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;

import org.tmatesoft.svn.core.wc.SVNClientManager;

import org.tmatesoft.svn.core.wc.SVNRevision;

import org.tmatesoft.svn.core.wc.SVNUpdateClient;



public class WorkingCopy {


/**
*jar files required:svnkit.jar,svnkit-cli.jar,svnkit-jhl.jar
*tested o.k with jdk 1.5
*
*
*
*/


private static SVNClientManager ourClientManager;

/**

*

* @param url

* @param commitMessage

* @return

* @throws SVNException

* creates a new directory immediately in a repository:

*/

private static SVNCommitInfo makeDirectory( SVNURL url , String commitMessage ) throws SVNException {

return ourClientManager.getCommitClient( ).doMkDir( new SVNURL[] { url } , commitMessage );

}



private static void setUpFactory(){

/*

* For using over http:// and https://

*/

DAVRepositoryFactory.setup();

/*

* For using over svn:// and svn+xxx://

*/

SVNRepositoryFactoryImpl.setup();

/*

* For using over file:///

*/

//FSRepositoryFactory.setup();

}



/**

*

* @param localPath

* @param dstURL

* @param commitMessage

* @param isRecursive

* @return

* @throws SVNException

* imports a local directory to a repository:

*/

private static SVNCommitInfo importDirectory( File localPath , SVNURL dstURL , String commitMessage , boolean isRecursive ) throws SVNException {

return ourClientManager.getCommitClient( ).doImport( localPath , dstURL , commitMessage , isRecursive );

}

/**

*

* @param wcPath

* @param keepLocks

* @param commitMessage

* @return

* @throws SVNException

* recursively commits Working Copy modifications to a repository

*/

private static SVNCommitInfo commit( File wcPath , boolean keepLocks , String commitMessage ) throws SVNException {

return ourClientManager.getCommitClient().doCommit( new File[] { wcPath } , keepLocks , commitMessage , false , true );

}

/**

*

* @param url

* @param revision

* @param destPath

* @param isRecursive

* @return

* @throws SVNException

* checks out a Working Copy given a repository url

*/

private static long checkout( SVNURL url , SVNRevision revision , File destPath , boolean isRecursive ) throws SVNException {

SVNUpdateClient updateClient = ourClientManager.getUpdateClient( );

/*

* sets externals not to be ignored during the checkout

*/

updateClient.setIgnoreExternals( false );

/*

* returns the number of the revision at which the working copy is

*/

//return updateClient.doCheckout( url , destPath , revision , revision , isRecursive );

return updateClient.doCheckout(url, destPath, revision, revision, SVNDepth.INFINITY, true);

}

/**

*

* @param wcPath

* @param updateToRevision

* @param isRecursive

* @return

* @throws SVNException

* updates a Working Copy to a particular revision

*/

private static long update( File wcPath , SVNRevision updateToRevision , boolean isRecursive ) throws SVNException {



SVNUpdateClient updateClient = ourClientManager.getUpdateClient( );

/*

* sets externals not to be ignored during the update

*/

updateClient.setIgnoreExternals( false );

/*

* returns the number of the revision wcPath was updated to

*/

return updateClient.doUpdate( wcPath , updateToRevision , isRecursive );

}

/**

*

* @param wcPath

* @param url

* @param updateToRevision

* @param isRecursive

* @return

* @throws SVNException

* switches a Working Copy to another url

*/

private static long switchToURL( File wcPath , SVNURL url , SVNRevision updateToRevision , boolean isRecursive ) throws SVNException {

SVNUpdateClient updateClient = ourClientManager.getUpdateClient( );

/*

* sets externals not to be ignored during the switch

*/

updateClient.setIgnoreExternals( false );

/*

* returns the number of the revision wcPath was updated to

*/

return updateClient.doSwitch( wcPath , url , updateToRevision , isRecursive );

}



/**

*

* @param wcPath

* @throws SVNException

* recursively adds an existing local item under version control (schedules for addition)

*/

private static void addEntry( File wcPath ) throws SVNException {

ourClientManager.getWCClient( ).doAdd( wcPath , false , false , false , true );

}

/**

*

* @param wcPath

* @param isStealLock

* @param lockComment

* @throws SVNException

* locks a versioned item

*/

private static void lock( File wcPath , boolean isStealLock , String lockComment ) throws SVNException {

ourClientManager.getWCClient( ).doLock( new File[] { wcPath } , isStealLock , lockComment );

}

/**

*

* @param wcPath

* @param force

* @throws SVNException

* deletes a versioned item from version control (schedules for deletion)

*/

private static void delete( File wcPath , boolean force ) throws SVNException {

ourClientManager.getWCClient( ).doDelete( wcPath , force , false );

}

/**

*

* @param srcURL

* @param dstURL

* @param isMove

* @param commitMessage

* @return

* @throws SVNException

* copies or moves one location to another one within the same repository

*/

/* private static SVNCommitInfo copy( SVNURL srcURL , SVNURL dstURL , boolean isMove , String commitMessage ) throws SVNException {

return ourClientManager.getCopyClient().doCopy( srcURL , SVNRevision.HEAD , dstURL , isMove , commitMessage );

}*/





public static void main( String args[]){

String svnURL="https://rproject.svn.sourceforge.net/svnroot/rproject";

File filePath=new File("/home/D2HS/rbaral/Documents/rproject/firstfile1.java");

DefaultSVNOptions myOptions=new DefaultSVNOptions();

String username="username";

String pwd="password";

SVNRevision revision=SVNRevision.HEAD;

String destPath="destination";

File destFile=new File(destPath);

boolean isRecursive=true;

ourClientManager=SVNClientManager.newInstance(myOptions, username, pwd);

try {

setUpFactory();

//addEntry(filePath);

//commit(filePath, true, "test commit message");

SVNURL url=SVNURL.parseURIDecoded(svnURL);

System.out.println("files checking out ");

checkout(url, revision, destFile, isRecursive);

File updateFile=new File("filepath to be updated");

//update(updateFile, SVNRevision.HEAD, true);

System.out.println("file checked out from remote svn");

} catch (SVNException e) {

System.out.println("SVNException while checking out :"+e.getMessage());

e.printStackTrace();



}

}

}

thanks to the site:svnkit
osdir.com

Thanks,
Ramesh

No comments:

Post a Comment