Files
svn-manager/src/main/java/com/svnmanager/service/CommitService.java
2026-02-03 23:24:32 +08:00

135 lines
4.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.svnmanager.service;
import com.svnmanager.util.ProcessUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
/**
* Commit服务
*/
public class CommitService extends SvnService {
private static final Logger logger = LoggerFactory.getLogger(CommitService.class);
/**
* 提交修改
*
* @param workingDirectory 工作目录
* @param message 提交消息
* @param files 要提交的文件列表null表示提交所有修改
* @return 提交结果
* @throws IOException IO异常
* @throws InterruptedException 中断异常
* @throws TimeoutException 超时异常
*/
public CommitResult commit(String workingDirectory, String message, List<String> files)
throws IOException, InterruptedException, TimeoutException {
logger.info("提交修改: {}", workingDirectory);
if (!isValidWorkingCopy(workingDirectory)) {
throw new IllegalArgumentException("无效的SVN工作副本: " + workingDirectory);
}
if (message == null || message.trim().isEmpty()) {
throw new IllegalArgumentException("提交消息不能为空");
}
List<String> args = new ArrayList<>();
args.add("-m");
args.add(message);
if (files != null && !files.isEmpty()) {
args.addAll(files);
}
ProcessUtil.ProcessResult result = executeSvnCommand("commit", args, workingDirectory);
CommitResult commitResult = new CommitResult();
commitResult.setSuccess(result.isSuccess());
commitResult.setOutput(result.getOutputAsString());
commitResult.setError(result.getErrorAsString());
if (result.isSuccess()) {
// 解析提交后的版本号
String output = result.getOutputAsString();
String revisionLine = output.lines()
.filter(line -> line.contains("Committed revision"))
.findFirst()
.orElse("");
if (!revisionLine.isEmpty()) {
String[] parts = revisionLine.split(" ");
if (parts.length > 0) {
String rev = parts[parts.length - 1].replace(".", "");
commitResult.setRevision(rev);
}
}
logger.info("提交成功,版本: {}", commitResult.getRevision());
} else {
logger.error("提交失败: {}", result.getErrorAsString());
}
return commitResult;
}
/**
* 提交所有修改
*
* @param workingDirectory 工作目录
* @param message 提交消息
* @return 提交结果
* @throws IOException IO异常
* @throws InterruptedException 中断异常
* @throws TimeoutException 超时异常
*/
public CommitResult commit(String workingDirectory, String message)
throws IOException, InterruptedException, TimeoutException {
return commit(workingDirectory, message, null);
}
/**
* 提交结果
*/
public static class CommitResult {
private boolean success;
private String revision;
private String output;
private String error;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getRevision() {
return revision;
}
public void setRevision(String revision) {
this.revision = revision;
}
public String getOutput() {
return output;
}
public void setOutput(String output) {
this.output = output;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
}