chore: initial project setup
This commit is contained in:
126
src/main/java/com/svnmanager/service/UpdateService.java
Normal file
126
src/main/java/com/svnmanager/service/UpdateService.java
Normal file
@@ -0,0 +1,126 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* Update服务
|
||||
*/
|
||||
public class UpdateService extends SvnService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(UpdateService.class);
|
||||
|
||||
/**
|
||||
* 更新工作副本
|
||||
*
|
||||
* @param workingDirectory 工作目录
|
||||
* @param revision 版本号(可选,null表示最新版本)
|
||||
* @return 更新结果
|
||||
* @throws IOException IO异常
|
||||
* @throws InterruptedException 中断异常
|
||||
* @throws TimeoutException 超时异常
|
||||
*/
|
||||
public UpdateResult update(String workingDirectory, String revision)
|
||||
throws IOException, InterruptedException, TimeoutException {
|
||||
logger.info("更新工作副本: {}", workingDirectory);
|
||||
|
||||
if (!isValidWorkingCopy(workingDirectory)) {
|
||||
throw new IllegalArgumentException("无效的SVN工作副本: " + workingDirectory);
|
||||
}
|
||||
|
||||
List<String> args = new ArrayList<>();
|
||||
if (revision != null && !revision.isEmpty()) {
|
||||
args.add("-r");
|
||||
args.add(revision);
|
||||
}
|
||||
|
||||
ProcessUtil.ProcessResult result = executeSvnCommand("update", args, workingDirectory);
|
||||
|
||||
UpdateResult updateResult = new UpdateResult();
|
||||
updateResult.setSuccess(result.isSuccess());
|
||||
updateResult.setOutput(result.getOutputAsString());
|
||||
updateResult.setError(result.getErrorAsString());
|
||||
|
||||
if (result.isSuccess()) {
|
||||
// 解析更新后的版本号
|
||||
String output = result.getOutputAsString();
|
||||
String revisionLine = output.lines()
|
||||
.filter(line -> line.contains("Updated to revision"))
|
||||
.findFirst()
|
||||
.orElse("");
|
||||
if (!revisionLine.isEmpty()) {
|
||||
String[] parts = revisionLine.split(" ");
|
||||
if (parts.length > 0) {
|
||||
String rev = parts[parts.length - 1].replace(".", "");
|
||||
updateResult.setRevision(rev);
|
||||
}
|
||||
}
|
||||
logger.info("更新成功,版本: {}", updateResult.getRevision());
|
||||
} else {
|
||||
logger.error("更新失败: {}", result.getErrorAsString());
|
||||
}
|
||||
|
||||
return updateResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新工作副本到最新版本
|
||||
*
|
||||
* @param workingDirectory 工作目录
|
||||
* @return 更新结果
|
||||
* @throws IOException IO异常
|
||||
* @throws InterruptedException 中断异常
|
||||
* @throws TimeoutException 超时异常
|
||||
*/
|
||||
public UpdateResult update(String workingDirectory)
|
||||
throws IOException, InterruptedException, TimeoutException {
|
||||
return update(workingDirectory, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新结果
|
||||
*/
|
||||
public static class UpdateResult {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user