heppokoactの技術メモ

※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

@ECHO OFF

set HOOKDIR="E:\repository\hooks"
set TMPDIR="%HOOKDIR%\.svnhook"
set SVNLOOKDIR="E:\subversion\bin"

set REPOS=%1
set TXN=%2
set RET=1

set LOG=%TMPDIR%\%TXN%.log
set CHANGED=%TMPDIR%\%TXN%.changed

if not exist %TMPDIR% mkdir %TMPDIR%

call %SVNLOOKDIR%\svnlook log -t %TXN% %REPOS% >> %LOG%
call %SVNLOOKDIR%\svnlook changed -t %TXN% %REPOS% >> %CHANGED%

cd %HOOKDIR%
java -classpath %HOOKDIR% SvnToCsv %LOG% %CHANGED%
set RET=%ERRORLEVEL%

del %LOG%
del %CHANGED%

exit %RET%



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* svn logの実行結果からCSVを作成します。
*
*/
public class SvnToCsv {

private String log;
private String changed;

public static void main(String[] args) {
	if (args.length != 2) {
		System.err.println("引数の数が変ですよ。(" + args.length + "個)");
		System.exit(1);
	}

	int result;
	try {
		result = new SvnToCsv(args[0], args[1]).toCsv();
	} catch (Exception e) {
		System.err.print(e.getMessage());
		result = 1;
	}

	System.exit(result);
}

public SvnToCsv(String logPath, String changedPath) throws IOException {
	log = read(logPath);
	changed = read(changedPath);
}

public int toCsv() throws IllegalFormatException, IOException {
	// 特定のディレクトリへの変更をしなければスルー
	if (!needToCsv()) {
		return 0;
	}

	String bugNo = getFirstGroup(log, "課題管理番号:(\\d+)");
	String comment = getFirstGroup(log, "コメント:(.+)");
	String[] changedPathes = getChangdPathes();
	write(toCsvString(bugNo, comment, changedPathes));

	return 0;
}

private void write(String csv) throws IOException {
	FileOutputStream out = null;
	PrintWriter writer = null;
	try {
		out = new FileOutputStream(new File("C:/release.log"), true);
		FileChannel channel = out.getChannel();
		FileLock lock = getLock(channel);

		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
		writer.print(csv);

		lock.release();

	} finally {
		if (writer != null) writer.close();
		if (out != null) out.close();
	}
}

private FileLock getLock(FileChannel channel) throws IOException {
	for (int i=0; i<5; i++) {
		FileLock lock = channel.tryLock();
		if (lock == null) {
			try {
				Thread.sleep(1000);
				continue;
			} catch (InterruptedException e) {}
		}
		return lock;
	}

	throw new IOException("C:/release.logのロックが取得できませんでした。");
}

private String toCsvString(String bugNo, String comment, String[] changedPathes) {
	StringBuilder builder = new StringBuilder();
	for (String path: changedPathes) {
		builder.append(bugNo).append(",").append(path).append(",").append(comment).append("\r\n");
	}
	return builder.toString();
}

private String[] getChangdPathes() throws IllegalFormatException {
	String[] changedArray = changed.split("\\r\\n");
	String[] changedPathes = new String[changedArray.length];
	for (int i=0; i<changedArray.length; i++) {
		String changedElement = changedArray[i];
		changedPathes[i] = getFirstGroup(changedElement, ".+/ほげ/(ふが/.+)");
	}
	return changedPathes;
}

private String getFirstGroup(String target, String regexp) throws IllegalFormatException {
	Pattern pattern = Pattern.compile(regexp);
	Matcher matcher = pattern.matcher(target);
	if (!matcher.find()) {
		throw new IllegalFormatException("正規表現「" + regexp + "」が見つかりません。\r\n対象:" + target);
	}
	return matcher.group(1);
}

private boolean needToCsv() {
	Pattern pattern = Pattern.compile("/ほげ/");
	return pattern.matcher(changed).find();
}

private String read(String path) throws IOException {
	BufferedReader reader = null;
	StringBuilder builder = new StringBuilder();

	try {
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "MS932"));
		while (true) {
			String line = reader.readLine();
			if (line == null) break;
			builder.append(line).append("\r\n");
		}

	} finally {
		if (reader != null) reader.close();
	}

	return builder.toString();
}

private class IllegalFormatException extends Exception {
	IllegalFormatException(String message) {
		super(message);
	}
}

}