使用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 = " ";
}
}