0

我用于我的小型项目 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();
        }
    }
}

我想检查标签是否存在,我想用SVNRepositoryandSVNClientManager和 not做/使用HttpURLConnection.HTTP_OK,有人知道吗?

4

2 回答 2

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();
}
于 2012-10-08T12:54:27.823 回答
0

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

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

workingCopyUrl是当前工作副本项目文件夹的 SVN 服务器上的 url,例如 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()
    }
}
于 2016-03-23T12:28:18.490 回答