javasvn

最近发现了一个纯java实现的svn。

把它用到了当前项目中用于管理指定项目的代码。

废话不多说了,帖代码吧。一目了然

package me.jor.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCClient;

public class SVN {
	
	static{
//在javasvn api内没有公开这三个类,但这是初始化svn repository最简单的方法
                SVNRepositoryFactoryImpl.setup();//初始化svn/svn+ssh协议
		DAVRepositoryFactory.setup();//初始化http/https协议
		FSRepositoryFactory.setup();//初始化本地文件系统file://协议
	}
	
	private static final SVNClientManager svn=SVNClientManager.newInstance();
	
	public static final ReentrantReadWriteLock.ReadLock SVN_READ_LOCK = LockMap
			.getReadLock("common_svn_lock");
	public static final ReentrantReadWriteLock.WriteLock SVN_WRITE_LOCK = LockMap
			.getWriteLock("common_svn_lock");

	/**
	 * @throws SVNException 
	 * 
	 * @throws InterruptedException
	 * @throws IOException
	 * */
	public static void ci(String target, ReentrantReadWriteLock.WriteLock lock)
			throws FileNotFoundException, SVNException {
		ci(new File(target), lock);
	}

	/**
	 * @throws SVNException 
	 * 
	 * @throws InterruptedException
	 * @throws IOException
	 * */
	public static void ci(File target, ReentrantReadWriteLock.WriteLock lock)
			throws FileNotFoundException, SVNException {
		try {
			lock.lock();
			SVNWCClient wc=add(target);
			svn.getCommitClient().doCommit(
				new File[] { target },
				false,
				"commit " + target.toString() + " on " + new Date(),
				new SVNProperties(), null, false, false,
				SVNDepth.INFINITY);
			wc.doCleanup(target);
		} finally {
			lock.unlock();
		}
	}
	
	private static SVNWCClient add(File path) throws SVNException {
		SVNWCClient wc=svn.getWCClient();
		if(path.isDirectory()){
			wc.doAdd(path, true, true, true, SVNDepth.INFINITY, true, true);
			for(File f:path.listFiles()){
				if(f.toString().indexOf(".svn")<0){
					add(f);
				}
			}
		}else{
			wc.doAdd(path, true, false, true, SVNDepth.INFINITY, true, true);
		}
		
		return wc;
	}

	/**
	 * @param target
	 *            接受Set<File>或Set<String>
	 * @param filePath
	 *            true:targets是目录路径,false:是目录文件对象
	 * */
	public static enum CommitType {
		ABSOLUTE_PATH, FILE
	}

	@SuppressWarnings("unchecked")
	public static void ci(Set<?> targets, CommitType type,
			ReentrantReadWriteLock.WriteLock lock) throws IOException,
			InterruptedException, SVNException {
		switch (type) {
		case FILE:
			for (File target : (Set<File>) targets) {
				ci(target, lock);
			}
			break;
		case ABSOLUTE_PATH:
			for (String target : (Set<String>) targets) {
				ci(target, lock);
			}
			break;
		}
	}

	public static void up(String target, ReentrantReadWriteLock.WriteLock lock)
			throws FileNotFoundException, SVNException, InterruptedException {
		up(new File(target),lock);
	}
	
	private static Thread upThread;
	public static void up(File target, ReentrantReadWriteLock.WriteLock lock)
			throws FileNotFoundException, SVNException, InterruptedException {
		if (target.exists()) {
			if(upThread==null){
				lock.lock();
				upThread=Thread.currentThread();
				svn.getUpdateClient().doUpdate(target, SVNRevision.HEAD, SVNDepth.INFINITY, true, true);
				upThread=null;
				lock.unlock();
			}
			if(upThread!=null){
				upThread.join();
			}
		} else {
			throw new FileNotFoundException(target.toString());
		}
	}

//	public static void main(String[] args) throws SVNException,
//			FileNotFoundException, InterruptedException {
//		SVN.up("E:\\workspace\\dev_processDef\\");
//	}
}

你可能感兴趣的:(java,SVN,版本控制,javasvn)