0

I use for my smal project Java SVNKit (for tagging):

at the moment and its work:

public void copy(String branchName, String dstTag, boolean isMove, String msg) throws SVNException, IOException {
    String finalURL = getSvnUrl() + "tags/" + dstTag;
    URL url = new URL(finalURL);
    String loginPassword = getUsername() + ":" + getPassword();

    String encoded = EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes(loginPassword)));
    URLConnection conn = url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + encoded);
    HttpURLConnection urlConnect = null;
    try {
        urlConnect = (HttpURLConnection)conn;
        if (urlConnect.getResponseCode() != HttpURLConnection.HTTP_OK) {
            ourClientManager.getCopyClient().doCopy(SVNURL.parseURIDecoded(getSvnUrl() + branchName),
                    SVNRevision.HEAD, SVNURL.parseURIDecoded(finalURL), isMove, msg);
            LOGGER.info("svn-tagging " + dstTag);
        } else
            LOGGER.info(dstTag + " Tag exists.");

    } finally {
        if (urlConnect != null) {
            urlConnect.disconnect();
        }
    }
}

I want to check if tag exists or not and I want to do/use with SVNRepository and SVNClientManager and not HttpURLConnection.HTTP_OK, does anyone have any idea?

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
Fawi
  • 473
  • 2
  • 7
  • 21

2 Answers2

0

I would use the new 'operations' API:

public static void main(String[] args) throws SVNException {
    final SvnList list = new SvnOperationFactory().createList();
    list.setSingleTarget(SvnTarget.fromURL(SVNURL.parseURIEncoded("http://svn.apache.org/repos/asf/spamassassin/tags")));
    list.setReceiver(new ISvnObjectReceiver<SVNDirEntry>() {
        public void receive(SvnTarget target, SVNDirEntry object) throws SVNException {
            System.out.println(object.getName());
        }
    });
    list.run();
}
real_paul
  • 574
  • 2
  • 22
mstrap
  • 16,808
  • 10
  • 56
  • 86
  • hi! thanks, but whats new 'operations' API? can you send me please more information? – Fawi Oct 08 '12 at 13:02
0

As @mstrap suggested, I also favor using the operations API that uses the SvnOperationFactory to create operations that are then run. Here is the Groovy code that I use in my gradle script to tag projects, it can be easily adapted to Java code.

I used a try/catch construct to avoid overwriting a tag since SvnList gave me an SVNException with FS_NOT_FOUND error rather than an empty list or null. This way I save myself a separate operation.

The workingCopyUrl is the url on the SVN server of the current working copy's project folder e.g. myhost/svnprojects/projectName. Be sure to use the server version as source otherwise you might end up copying a Revision that does not exist on the server. However if you don't check that you committed everything beforehand you will tag a version different f4rom your working copy!

The subDir is the sub-directory within the project e.g. projectName/branches/Featurebranch or projectName/trunk.

import org.tmatesoft.svn.core.wc.*
import org.tmatesoft.svn.core.wc2.*
import org.tmatesoft.svn.core.*
def tagSVN(projectName, workingCopyUrl, subDir, tagName) {
    final SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
    final defaultAuthManager =  SVNWCUtil.createDefaultAuthenticationManager("username", "password")
    svnOperationFactory.setAuthenticationManager(defaultAuthManager)

    try {
        final SvnRemoteCopy tagOperation = svnOperationFactory.createRemoteCopy();      

        // Setup source (Working copy + subDir which may be trunk or a tag or a branch or any other sub-directory) 
        def sourceFolder = workingCopyUrl + '/' + subDir
        def SVNURL sourceURL = SVNURL.parseURIEncoded(sourceFolder)
        def SvnCopySource source = SvnCopySource.create(SvnTarget.fromURL(sourceURL), SVNRevision.HEAD)
        tagOperation.addCopySource(source)

        // Setup destination (SVN Server)
        def tagDestinationPath = workingCopyUrl + "/tags/" + tagName
        def SVNURL destinationURL = SVNURL.parseURIEncoded(tagDestinationPath)                  
        tagOperation.setSingleTarget(SvnTarget.fromURL(destinationURL))

        // Make the operation fail when no 
        tagOperation.setFailWhenDstExists(true)  
        tagOperation.setCommitMessage("Tagging the ${projectName}")
        tagOperation.run()
    } catch (SVNException e) {
        if (e.getErrorMessage() == SVNErrorCode.ENTRY_EXISTS) {
            logger.info("Entry exists")
        }

    } finally {
        svnOperationFactory.dispose()
    }
}
real_paul
  • 574
  • 2
  • 22