查看末提交文件的SVN状态

     使用SVNKit.jar提供的API,我们可以很轻松的查看没有提交的Working Copy中的文件的SVN状态,主要的做法是使用SVNClientManager实例的getStatusClient().doStatus()方法来调用自定义的ISVNStatusHandler的handleStatus(SVNStatus status)方法查看文件的状态,从而来进行判断。

     主要代码如下:

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

public static void main(String[] args) throws SVNException {
    setupLibrary();

    SVNURL repositoryURL = null;
    try {
     //设置SVN服务器的URL
      repositoryURL =
          SVNURL.parseURIEncoded("https://192.168.88.88:8443/svn/test");
    } catch (SVNException e) {
      error("get respository url error!", e);
    }
   //设置登录用户与密码
    String name = "king";
    String password = "88888888";
    String myWorkingCopyPath = "F:\\arc";

    ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
    ISVNAuthenticationManager authManager =
      SVNWCUtil.createDefaultAuthenticationManager(name, password);
    File wcDir = new File(myWorkingCopyPath);
    if (!wcDir.exists()) {
      error("the work copy directory '" + wcDir.getAbsolutePath() +
            "' was not exists!", null);
      return;
    }
    ourClientManager = SVNClientManager.newInstance(options, authManager);

    //查询状态
    boolean isRecursive = true;
    boolean isRemote = true;
    boolean isReportAll = false;
    boolean isIncludeIgnored = true;
    boolean isCollectParentExternals = false;
    File[] unVF = null;
    System.out.println("Status for '" + wcDir.getAbsolutePath() + "':");
    try {
      List unVerFiles =
        showStatus(wcDir, isRecursive, isRemote, isReportAll, isIncludeIgnored,
                   isCollectParentExternals);
    } catch (SVNException svne) {
      error("error while recursively performing status for '" +
            wcDir.getAbsolutePath() + "'", svne);
    }

    //copy to temp file
    if (unVF != null) {
      File anImportDir = new File(importFile);
      createLocalDir(anImportDir, unVF);
    }
  }
/**
*主要是查询状态
*/
private static void showStatus(File wcPath, boolean isRecursive,
                                 boolean isRemote, boolean isReportAll,
                                 boolean isIncludeIgnored,
                                 boolean isCollectParentExternals) throws SVNException {
   //定义状态管理类
    StatusHandler sh = new StatusHandler(isRemote);
    ourClientManager.getStatusClient().doStatus(wcPath, isRecursive, isRemote,
                                                isReportAll, isIncludeIgnored,
                                                isCollectParentExternals, sh);
  }
 

StatusHandler类的主要方法:

public void handleStatus(SVNStatus status) {
        /*
         * Gets  the  status  of  file/directory/symbolic link  text  contents. 
         * It is  SVNStatusType  who  contains  information on the state of  an 
         * item. 
         */
        SVNStatusType contentsStatus = status.getContentsStatus();

        String pathChangeType = " ";
        
        boolean isAddedWithHistory = status.isCopied();
        if (contentsStatus == SVNStatusType.STATUS_MODIFIED) {
            /*
             * The contents of the file have been Modified.
             */
            pathChangeType = "M";
        } else if (contentsStatus == SVNStatusType.STATUS_CONFLICTED) {
            /*
             * The  file  item  is  in a state of  Conflict. That  is,  changes 
             * received from the server during an  update  overlap  with  local 
             * changes the user has in his working copy. 
             */
            pathChangeType = "C";
        } else if (contentsStatus == SVNStatusType.STATUS_DELETED) {
            /*
             * The file, directory or symbolic link item has been scheduled for 
             * Deletion from the repository.
             */
            pathChangeType = "D";
        } else if (contentsStatus == SVNStatusType.STATUS_ADDED) {
            /*
             * The file, directory or symbolic link item has been scheduled for 
             * Addition to the repository.
             */
            pathChangeType = "A";
        } else if (contentsStatus == SVNStatusType.STATUS_UNVERSIONED) {
            /*
             * The file, directory or symbolic link item is not  under  version 
             * control.
             */
            pathChangeType = "?";
        } else if (contentsStatus == SVNStatusType.STATUS_EXTERNAL) {
            /*
             * The item is unversioned, but is used by an eXternals definition.
             */
            pathChangeType = "X";
        } else if (contentsStatus == SVNStatusType.STATUS_IGNORED) {
            /*
             * The file, directory or symbolic link item is not  under  version 
             * control, and is configured to be Ignored during 'add',  'import' 
             * and 'status' operations. 
             */
            pathChangeType = "I";
        } else if (contentsStatus == SVNStatusType.STATUS_MISSING
                || contentsStatus == SVNStatusType.STATUS_INCOMPLETE) {
            /*
             * The file, directory or  symbolic  link  item  is  under  version 
             * control but is missing or somehow incomplete. The  item  can  be 
             * missing if it is removed using a command incompatible  with  the 
             * native Subversion command line client (for example, just removed 
             * from the filesystem). In the case the item is  a  directory,  it 
             * can  be  incomplete if the user happened to interrupt a checkout 
             * or update.
             */
            pathChangeType = "!";
        } else if (contentsStatus == SVNStatusType.STATUS_OBSTRUCTED) {
            /*
             * The file, directory or symbolic link item is in  the  repository 
             * as one kind of object, but what's actually in the user's working 
             * copy is some other kind. For example, Subversion  might  have  a 
             * file in the repository,  but  the  user  removed  the  file  and 
             * created a directory in its place, without using the 'svn delete' 
             * or 'svn add' command (or SVNKit analogues for them).
             */
            pathChangeType = "~";
        } else if (contentsStatus == SVNStatusType.STATUS_REPLACED) {
            /*
             * The file, directory or symbolic link item was  Replaced  in  the 
             * user's working copy; that is, the item was deleted,  and  a  new 
             * item with the same name was added (within  a  single  revision). 
             * While they may have the same name, the repository considers them 
             * to be distinct objects with distinct histories.
             */
            pathChangeType = "R";
        } else if (contentsStatus == SVNStatusType.STATUS_NONE
                || contentsStatus == SVNStatusType.STATUS_NORMAL) {
            /*
             * The item was not modified (normal).
             */
            pathChangeType = " ";
        }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值