如果标记不存在,Java SVNKit标记?

时间:2012-10-08 12:44:51

标签: java tags svnkit

我用于我的小项目Java SVNKit(用于标记):

此刻及其工作:

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();
        }
    }
}

我想检查标记是否存在,我想与SVNRepositorySVNClientManager而不是HttpURLConnection.HTTP_OK一起使用,是否有人有任何想法?

2 个答案:

答案 0 :(得分:0)

我会使用新的'操作'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();
}

答案 1 :(得分:0)

正如@mstrap建议的那样,我也赞成使用使用SvnOperationFactory的操作API来创建随后运行的操作。 这是我在gradle脚本中用来标记项目的Groovy代码,它可以很容易地适应Java代码。

我使用了try / catch构造来避免覆盖标记,因为SvnList给了我带有 FS_NOT_FOUND 错误的SVNException而不是空列表或null。这样我就可以省去一个单独的操作。

workingCopyUrl 是当前工作副本项目文件夹的SVN服务器上的网址,例如为myhost / svnprojects / PROJECTNAME。请务必使用服务器版本作为源,否则您最终可能会复制服务器上不存在的版本。但是,如果您事先没有检查是否已经提交了所有内容,那么您将标记与您的工作副本不同的版本!

subDir 是项目中的子目录,例如projectName / branches / Featurebranch或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()
    }
}