SVNKit getFileFromSVN

 

/*
 * ====================================================================
 * Copyright (c) 2004-2011 TMate Software Ltd.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://svnkit.com/license.html
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 * ====================================================================
 */
package org.tmatesoft.svn.examples.repository;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.SVNProperty;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
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.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

/*
 * This example shows how to fetch a file and its properties from the repository
 * at the latest (HEAD) revision . If the file is a text (either it has no
 * svn:mime-type property at all or if has and the property value is text/-like)
 * its contents as well as properties will be displayed in the console,
 * otherwise - only properties.
 * As an example here's a part of one of the
 * program layouts (for the default url and file path used in the program):
 *
 * File property: svn:entry:revision=2802
 * File property: svn:entry:checksum=435f2f0d33d12907ddb6dfd611825ec9
 * File property: svn:wc:ra_dav:version-url=/repos/svnkit/!svn/ver/2795/trunk/www/license.html
 * File property: svn:entry:last-author=alex
 * File property: svn:entry:committed-date=2006-11-13T21:34:27.908657Z
 * File property: svn:entry:committed-rev=2795
 * File contents:
 *
 * <html>
 * <head>
 * <link rel="shortcut icon" href="img/favicon.ico"/>
 * <title>SVNKit&nbsp;::&nbsp;License</title>
 * </head>
 * <body>
 * <h1>The TMate Open Source License.</h1>
 * <pre>
 * ......................................
 * ---------------------------------------------
 * Repository latest revision: 2802
 */
public class DisplayFile {
    /*
     * args parameter is used to obtain a repository location URL, user's
     * account name & password to authenticate him to the server, the file path
     * in the rpository (the file path should be relative to the the
     * path/to/repository part of the repository location URL).
     */
    public static void main(String[] args) {
        /*
         * Default values:
         */
        String url = "http://svn.svnkit.com/repos/svnkit/trunk";
        String name = "anonymous";
        String password = "anonymous";
        String filePath = "www/license.html";
        /*
         * Initializes the library (it must be done before ever using the
         * library itself)
         */
        setupLibrary();

        if (args != null) {
            /*
             * Obtains a repository location URL
             */
            url = (args.length >= 1) ? args[0] : url;
            /*
             * Obtains a file path
             */
            filePath = (args.length >= 2) ? args[1] : filePath;
            /*
             * Obtains an account name (will be used to authenticate the user to
             * the server)
             */
            name = (args.length >= 3) ? args[2] : name;
            /*
             * Obtains a password
             */
            password = (args.length >= 4) ? args[3] : password;
        }
        SVNRepository repository = null;
        try {
            /*
             * Creates an instance of SVNRepository to work with the repository.
             * All user's requests to the repository are relative to the
             * repository location used to create this SVNRepository.
             * SVNURL is a wrapper for URL strings that refer to repository locations.
             */
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        } catch (SVNException svne) {
            /*
             * Perhaps a malformed URL is the cause of this exception
             */
            System.err
                    .println("error while creating an SVNRepository for the location '"
                            + url + "': " + svne.getMessage());
            System.exit(1);
        }

        /*
         * User's authentication information (name/password) is provided via  an
         * ISVNAuthenticationManager  instance.  SVNWCUtil  creates  a   default
         * authentication manager given user's name and password.
         *
         * Default authentication manager first attempts to use provided user name
         * and password and then falls back to the credentials stored in the
         * default Subversion credentials storage that is located in Subversion
         * configuration area. If you'd like to use provided user name and password
         * only you may use BasicAuthenticationManager class instead of default
         * authentication manager:
         *
         *  authManager = new BasicAuthenticationsManager(userName, userPassword);
         *
         * You may also skip this point - anonymous access will be used.
         */
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
        repository.setAuthenticationManager(authManager);

        /*
         * This Map will be used to get the file properties. Each Map key is a
         * property name and the value associated with the key is the property
         * value.
         */
        SVNProperties fileProperties = new SVNProperties();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            /*
             * Checks up if the specified path really corresponds to a file. If
             * doesn't the program exits. SVNNodeKind is that one who says what is
             * located at a path in a revision. -1 means the latest revision.
             */
            SVNNodeKind nodeKind = repository.checkPath(filePath, -1);

            if (nodeKind == SVNNodeKind.NONE) {
                System.err.println("There is no entry at '" + url + "'.");
                System.exit(1);
            } else if (nodeKind == SVNNodeKind.DIR) {
                System.err.println("The entry at '" + url
                        + "' is a directory while a file was expected.");
                System.exit(1);
            }
            /*
             * Gets the contents and properties of the file located at filePath
             * in the repository at the latest revision (which is meant by a
             * negative revision number).
             */
            repository.getFile(filePath, -1, fileProperties, baos);

        } catch (SVNException svne) {
            System.err.println("error while fetching the file contents and properties: " + svne.getMessage());
            System.exit(1);
        }

        /*
         * Here the SVNProperty class is used to get the value of the
         * svn:mime-type property (if any). SVNProperty is used to facilitate
         * the work with versioned properties.
         */
        String mimeType = fileProperties.getStringValue(SVNProperty.MIME_TYPE);

        /*
         * SVNProperty.isTextMimeType(..) method checks up the value of the mime-type
         * file property and says if the file is a text (true) or not (false).
         */
        boolean isTextType = SVNProperty.isTextMimeType(mimeType);

        Iterator iterator = fileProperties.nameSet().iterator();
        /*
         * Displays file properties.
         */
        while (iterator.hasNext()) {
            String propertyName = (String) iterator.next();
            String propertyValue = fileProperties.getStringValue(propertyName);
            System.out.println("File property: " + propertyName + "="
                    + propertyValue);
        }
        /*
         * Displays the file contents in the console if the file is a text.
         */
        if (isTextType) {
            System.out.println("File contents:");
            System.out.println();
            try {
                baos.writeTo(System.out);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } else {
            System.out
                    .println("File contents can not be displayed in the console since the mime-type property says that it's not a kind of a text file.");
        }
        /*
         * Gets the latest revision number of the repository
         */
        long latestRevision = -1;
        try {
            latestRevision = repository.getLatestRevision();
        } catch (SVNException svne) {
            System.err.println("error while fetching the latest repository revision: " + svne.getMessage());
            System.exit(1);
        }
        System.out.println("");
        System.out.println("---------------------------------------------");
        System.out.println("Repository latest revision: " + latestRevision);
        System.exit(0);
    }

    /*
     * Initializes the library to work with a repository via
     * different protocols.
     */
    private static void setupLibrary() {
        /*
         * For using over http:// and https://
         */
        DAVRepositoryFactory.setup();
        /*
         * For using over svn:// and svn+xxx://
         */
        SVNRepositoryFactoryImpl.setup();

        /*
         * For using over file:///
         */
        FSRepositoryFactory.setup();
    }
}

 

http://wiki.svnkit.com/Printing_Out_File_Contents

http://svn.svnkit.com/repos/svnkit/tags/1.3.5/doc/examples/src/org/tmatesoft/svn/examples/repository/DisplayFile.java

 

时间: 2024-12-03 16:23:41

SVNKit getFileFromSVN的相关文章

使用svnkit 的相关实例及相关问题汇总

  SVNKIT操作SVN版本库的完整例子 http://www.cnblogs.com/wangjiyuan/p/svnkitwanchenglizi.html#!comments    2.SVNClientManager包括了各种Client来完成日常的SVN操作,如下图: 类 命令 备注 SVNLogClient.doLog() svn log [PATH|URL] 从库中显示log消息  SVNLogClient.doList() svn list [TARGET[@REV]...]

java-求一个导入项目即可运行的svnkit项目,实现checkout update

问题描述 求一个导入项目即可运行的svnkit项目,实现checkout update 搞了几天就是运行不了,实在是崩溃了,求大神给一个能用的,网上的也下载了,就是不行! 解决方案 自己搭一个简单的服务器,随你玩,很容易搭的

共享-询问有关java操作svnkit的代码实现

问题描述 询问有关java操作svnkit的代码实现 请问所需的包能共享下吗,SVNUtil.java没有问题,但下面的SvnProjectService.java类始终编译不过 解决方案 参考:http://blog.csdn.net/feiren127/article/details/7551831

关于svnkit创建user和设置user权限的问题

问题描述 关于svnkit创建user和设置user权限的问题 请问,svnkit能否创建.删除SVN服务端USER和设置user的权限? 解决方案 请问您解决了这个问题了吗

关于java问题-求助,关于svnkit的使用

问题描述 求助,关于svnkit的使用 最近毕设用到了,但完全不会,希望哪位大神能用5分钟教教我,必有重谢 解决方案 这不是什么重要的东西,只是一个工具,用来从svn服务器抓源代码下来. 参考:http://blog.163.com/tianshenglongchang@126/blog/static/164628503201431725915795/ 解决方案二: 由于需要用Java代码控制SVN,比方下载SVN的代码,网上找到了大牛直接徒手写的代码,但是后来发现了svnkit这么个东西,直接

使用svnkit获取版本之间差异

问题描述 最近需要使用svnkit统计每人每天提交的代码量,目前已经实现了使用ISVNLogEntryHandler获得某人,某日修改了哪些文件,已经修改后文件的版本号,不过每次修改的变化信息没有得到(比如添加,删除,修改了多少行等等),有什么方法可以获得这些信息吗?谢谢! 解决方案

java-求一个svnkit的从svn服务器下载文件的源代码

问题描述 求一个svnkit的从svn服务器下载文件的源代码 要求项目导入即可使用,本人完全不懂所以一定要可以用的(一定要是一个完整的项目)可发红包! 解决方案 环境变量,你会配吧,如果会配的话,给我回复,我给你我最近做的项目.如果不会的的,联系我,我帮你配,如果既不想配变量,也不想联系我,那么神仙也帮不了你了.

java-如何操作SVNkit获取文件关联的SVN地址、用户名和密码

问题描述 如何操作SVNkit获取文件关联的SVN地址.用户名和密码 本地的文件已经关联了SVN服务,请问如何通过SVNkit获取文件关联的SVN地址 如果能够解析出关联的SVN服务的用户名和密码就太好了 解决方案 获取密码这个不现实i 解决方案二: http://www.cnblogs.com/xiaobaihome/archive/2012/03/22/2411036.html 解决方案三: 这种事情有点不太现实

svnkit 提交代码报错

问题描述 org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for '<http://10.23.4.140:80> Subversion Repositoy of CSS-TAX' 问题补充:哪位大神能解决啊..如果信息不够请说..我把代码全粘上来 解决方案 svn权限有问题吧,你问问你们管理svn的人看下?###你把你代码库的ip都暴露了