package org.jenkinsci.plugins.svn.upload.step;
import java.io.*;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
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.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
public class SvnUpload {
private SVNClientManager clientManager;
private ISVNAuthenticationManager authManager;
private SVNRepository repository;
public SvnUpload(String svnUrl, String svnUsername, String svnPasswd)throws SVNException{
try {
this.createDefaultAuthenticationManager(svnUsername, svnPasswd);
this.authSvn(svnUrl);
} catch (SVNException e) {
throw new RuntimeException(e.getMessage());
}
}
private void setupLibrary() {
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();
}
private void createDefaultAuthenticationManager(String username, String password)throws SVNException{
try {
// 身份驗證
authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
} catch (Exception e) {
throw new RuntimeException("SVN身份認證失敗:" + e.getMessage());
}
}
private void authSvn(String svnUrl) throws SVNException {
// 初始化版本庫
setupLibrary();
try {
repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnUrl));
} catch (SVNException e) {
throw new RuntimeException("SVN建立庫連接配接失敗:" + e.getMessage());
}
// 建立身份驗證管理器
repository.setAuthenticationManager(authManager);
DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
try {
//建立SVN執行個體管理器
clientManager = SVNClientManager.newInstance(options,authManager);
} catch (Exception e) {
throw new RuntimeException("SVN執行個體管理器建立失敗:" + e.getMessage());
}
}
private void addEntry(File wcPath) throws SVNException{
try {
clientManager.getWCClient().doAdd(new File[] { wcPath }, true,
false, false, SVNDepth.INFINITY, false, false, true);
} catch (SVNException e) {
throw new RuntimeException("SVN添加檔案到版本控制下失敗:" + e.getMessage());
}
}
private SVNCommitInfo commit(File wcPath, boolean keepLocks, String commitMessage) throws SVNException {
try {
return clientManager.getCommitClient().doCommit(
new File[] { wcPath }, keepLocks, commitMessage, null,
null, false, false, SVNDepth.INFINITY);
} catch (SVNException e) {
throw new RuntimeException("SVN送出失敗:" + e.getMessage());
}
}
private boolean isWorkingCopy(File path) throws SVNException{
if(!path.exists()){
return false;
}
try {
if(null == SVNWCUtil.getWorkingCopyRoot(path, false)){
return false;
}
} catch (SVNException e) {
throw new RuntimeException("确定path是否是一個工作空間 失敗:" + e.getMessage());
}
return true;
}
private boolean isURLExist(SVNURL url) throws SVNException{
try {
SVNRepository svnRepository = SVNRepositoryFactory.create(url);
svnRepository.setAuthenticationManager(authManager);
SVNNodeKind nodeKind = svnRepository.checkPath("", -1);
return nodeKind == SVNNodeKind.NONE ? false : true;
} catch (SVNException e) {
throw new RuntimeException("确定一個URL在SVN上是否存在失敗:" + e.getMessage());
}
}
private void checkVersiondDirectory(File wc) throws SVNException{
if(!SVNWCUtil.isVersionedDirectory(wc)){
this.addEntry(wc);
}
if(wc.isDirectory()){
for(File sub:wc.listFiles()){
if(sub.isDirectory() && sub.getName().equals(".svn")){
continue;
}
checkVersiondDirectory(sub);
}
}
}
private boolean deleteDirectory(String sPath) {
//如果sPath不以檔案分隔符結尾,自動添加檔案分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dir對應的檔案不存在,或者不是一個目錄,則退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
//删除檔案夾下的所有檔案(包括子目錄)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
//删除子檔案
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) break;
} //删除子目錄
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
//删除目前目錄
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
private boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路徑為檔案且不為空則進行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
private boolean DeleteFolder(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 判斷目錄或檔案是否存在
if (!file.exists()) { // 不存在傳回 false
return flag;
} else {
// 判斷是否為檔案
if (file.isFile()) { // 為檔案時調用删除檔案方法
return deleteFile(file.getAbsolutePath());
} else { // 為目錄時調用删除目錄方法
return deleteDirectory(file.getAbsolutePath());
}
}
}
private long update(File wcPath,SVNRevision updateToRevision, SVNDepth depth) throws SVNException{
SVNUpdateClient updateClient = clientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
try {
return updateClient.doUpdate(wcPath, updateToRevision,depth, false, false);
} catch (SVNException e) {
throw new RuntimeException("更新SVN工作區失敗:" + e.getMessage());
}
}
private long checkout(SVNURL url, SVNRevision revision, File destPath, SVNDepth depth) throws SVNException{
SVNUpdateClient updateClient = clientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
try {
return updateClient.doCheckout(url, destPath, revision, revision,depth, false);
} catch (SVNException e) {
throw new RuntimeException("檢出SVN倉庫失敗:" + e.getMessage());
}
}
private void checkWorkCopy(String svnUrl,String workspace,String filepath,String filename)throws SVNException{
SVNURL repositoryURL = null;
try {
repositoryURL = SVNURL.parseURIEncoded(svnUrl);
} catch (SVNException e) {
throw new RuntimeException("解析svnUrl失敗:" + e.getMessage());
}
String fPath = "";
if(filepath.indexOf("/") != -1) {
fPath = filepath.substring(0,filepath.lastIndexOf("/"));
}
File wc = new File(workspace+"/"+fPath);
File wc_project = new File( workspace + "/" + fPath);
SVNURL projectURL = null;
try {
projectURL = repositoryURL.appendPath(filename, false);
} catch (SVNException e) {
throw new RuntimeException("解析svnUrl檔案失敗:" + e.getMessage());
}
if(!this.isWorkingCopy(wc)){
if(!this.isURLExist(projectURL)){
this.checkout(repositoryURL, SVNRevision.HEAD, wc, SVNDepth.EMPTY);
}else{
this.checkout(projectURL, SVNRevision.HEAD, wc_project, SVNDepth.INFINITY);
}
}else{
this.update(wc, SVNRevision.HEAD, SVNDepth.INFINITY);
}
}
private void deletePointSVN(String spath){
File wc = new File(spath);
for(File sub:wc.listFiles()){
if(sub.isDirectory() && sub.getName().equals(".svn")){
this.deleteDirectory(sub.getAbsolutePath());
continue;
}
if(sub.isDirectory()){
deletePointSVN(sub.getAbsolutePath());
}
}
}
public void upload(String svnUrl,String workspace,String filepath,String filename,Boolean isOverwrite)throws SVNException{
String svnfilePath = svnUrl+"/"+filename;
//開始前删除以前的.svn檔案目錄
deletePointSVN(workspace);
boolean flag = this.isURLExist(SVNURL.parseURIDecoded(svnfilePath));
if(flag){
if(isOverwrite){
this.uploadFile(svnUrl, workspace, filepath,filename);
}
}else{
this.uploadFile(svnUrl, workspace, filepath,filename);
}
//結束後删除目前的.svn檔案目錄
deletePointSVN(workspace);
}
private void uploadFile(String svnUrl,String workspace,String filepath,String filename)throws SVNException{
this.checkWorkCopy(svnUrl, workspace, filepath,filename);
File file = new File(workspace+"/"+filepath);
this.checkVersiondDirectory(file);
this.commit(file, false, "commit file:"+file);
}
public static void main(String[] args) throws SVNException {
try {
String svnUrl = "http://192.168.18.200/svn/repository/hello";
String username = "admin";
String passwd = "admin";
String workspace = "C:\\Users\\kube\\Desktop\\test";
String upfile = "/q/w/a.txt,b.txt";
Boolean isOverwrite = true;
SvnUpload svnUpload = new SvnUpload(svnUrl,username, passwd);
String [] fileArray = upfile.split(",");
for(int i=0;i
String filePath = fileArray[i];
String filename = "";
if (filePath.indexOf("/") != -1) {
filename = filePath.substring(filePath.lastIndexOf("/"));
} else {
filename = filePath;
}
svnUpload.upload(svnUrl, workspace, filePath, filename,isOverwrite);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}