public
class
SvnClientUtil {
private
SvnConfig config;
private
ISVNClientAdapter svnClient;
private
SvnClientUtil(){}
public
SvnClientUtil(SvnConfig config)
throws
SVNClientException{
this
.config
=
config;
getClient();
}
/**
* 本地路径转为svn路径
*
@param
file 本地路径文件
*
@return
SVNUrl
*/
public
SVNUrl localPath2SVNUrl(File file)
throws
MalformedURLException{
String absltPath
=
file.getAbsolutePath();
absltPath
=
absltPath.replaceAll(
"
//
"
,
"
/
"
);
String tmpPath
=
this
.config.getRootDir().replaceAll(
"
//
"
,
"
/
"
);
absltPath
=
absltPath.replaceAll(tmpPath,
""
);
return
new
SVNUrl(
this
.config.getRootUrl()
+
absltPath);
}
/**
* checkout from svn server
*
@param
moduleName svnserver 上的URL
*
@param
destPath 本地目标路径
*
@param
revision 版本
*
@param
recurse 是否递归
*
@throws
SVNClientException
*/
public
void
checkOut(
SVNUrl moduleName,
File destPath,
SVNRevision revision,
boolean
recurse)
throws
SVNClientException{
this
.svnClient.checkout(moduleName, destPath, revision, recurse);
}
/**
* 添加文件到svn
*
@param
file 待添加的File
*
@param
url 添加到的SVNUrl
*
@param
message 备注(必须有中文)
*
@throws
SVNClientException
*/
public
void
add(File file,SVNUrl url,String message)
throws
SVNClientException{
this
.svnClient.addFile(file);
File [] paths
=
new
File[]{file};
this
.svnClient.commit(paths, message,
false
);
}
/**
* 添加目录
*
@param
dir 目录File
*
@param
message 备注(中文)
*
@param
recurse 是否递归
*
@throws
SVNClientException
*/
public
void
addDir(File dir, String message,
boolean
recurse)
throws
SVNClientException{
this
.svnClient.addDirectory(dir, recurse);
File [] paths
=
new
File[]{dir};
this
.svnClient.commit(paths, message, recurse);
}
/**
* 获得某版本的内容
*
@param
url svnURL
*
@param
revision 版本号
*
@return
InputStream
*
@throws
SVNClientException
*/
public
InputStream getHistoryContent(SVNUrl url, SVNRevision revision)
throws
SVNClientException{
return
this
.svnClient.getContent(url, revision);
}
/**
* 提交
*
@param
path 提交的文件
*
@param
message 备注(必须含中文)
*
@throws
SVNClientException
*/
public
void
commit(File path, String message)
throws
SVNClientException{
File [] paths
=
new
File[]{path};
this
.svnClient.commit(paths, message,
false
);
}
/**
* 删除
*
@param
url 删除文件的URL
*
@param
message 备注(必须含中文)
*
@throws
SVNClientException
*/
public
void
delete(SVNUrl url,String message)
throws
SVNClientException{
SVNUrl[] urls
=
new
SVNUrl[]{url};
this
.svnClient.remove(urls, message);
}
/**
* 获取历史纪录
*
@param
url 文件对应的svn url
*
@return
ISVNLogMessage[]
*
@throws
SVNClientException
*/
public
ISVNLogMessage[] getHistory(SVNUrl url)
throws
SVNClientException{
return
this
.svnClient.getLogMessages(url, SVNRevision.START, SVNRevision.HEAD);
}
/**
* 复原为某一个版本
*
@param
path 路径
*
@param
revision 版本号
*
@param
recurse 是否递归
*
@throws
SVNClientException
*/
public
void
update(File path, SVNRevision revision,
boolean
recurse)
throws
SVNClientException{
this
.svnClient.update(path, revision, recurse);
}
public
SvnConfig getConfig() {
return
config;
}
public
void
setup() {
try
{
JhlClientAdapterFactory.setup();
}
catch
(SVNClientException e) {
//
can't register this factory
}
try
{
CmdLineClientAdapterFactory.setup();
}
catch
(SVNClientException e1) {
//
can't register this factory
}
try
{
SvnKitClientAdapterFactory.setup();
}
catch
(SVNClientException e1) {
//
can't register this factory
}
}
public
void
getClient()
throws
SVNClientException{
setup();
try
{
String bestClientType
=
SVNClientAdapterFactory.getPreferredSVNClientType();
System.out.println(
"
Using
"
+
bestClientType
+
"
factory
"
);
svnClient
=
SVNClientAdapterFactory.createSVNClient(bestClientType);
}
catch
(SVNClientException e) {
System.out.println(e.getMessage());
throw
e;
}
//
set username and password if necessary
svnClient.setUsername(
this
.config.getSvnUser());
svnClient.setPassword(
this
.config.getSvnPwd());
//
add a listener if you wish
NotifyListener listener
=
new
SvnClientUtil.NotifyListener();
svnClient.addNotifyListener(listener);
//
初始化工作目录
SVNUrl moduleName;
File destPath
=
new
File(
this
.config.getRootDir());
try
{
if
(
!
destPath.exists()){
destPath.mkdirs();
System.out.println(
"
create local SVN work space!
"
);
}
moduleName
=
new
SVNUrl(
this
.config.getRootUrl());
System.out.println(
"
update SVN work space from server!
"
);
this
.checkOut(moduleName, destPath, SVNRevision.HEAD,
true
);
}
catch
(Exception e){
System.out.println(
"
update SVN work space from server fail!
"
);
e.printStackTrace();
throw
new
SVNClientException(e);
}
}
public
static
class
NotifyListener
implements
ISVNNotifyListener {
public
void
setCommand(
int
cmd) {
//
the command that is being executed. See ISVNNotifyListener.Command
//
ISVNNotifyListener.Command.ADD for example
}
public
void
logMessage(String message) {
System.out.println(message);
}
public
void
logCommandLine(String message) {
//
the command line used
System.out.println(message);
}
public
void
logError(String message) {
//
when an error occurs
System.out.println(
"
error :
"
+
message);
}
public
void
logRevision(
long
revision, String path) {
//
when command completes against revision
System.out.println(
"
revision :
"
+
revision);
}
public
void
logCompleted(String message) {
//
when command completed
System.out.println(message);
}
public
void
onNotify(File path, SVNNodeKind nodeKind) {
//
each time the status of a file or directory changes (file added, reverted ...)
//
nodeKind is SVNNodeKind.FILE or SVNNodeKind.DIR
//
this is the function we use in subclipse to know which files need to be refreshed
System.out.println(
"
Status of
"
+
path.toString()
+
"
has changed
"
);
}
}
}