- add username/password fields to project dialog and model - pass optional auth to SVN info/status/log/diff/update/commit services - centralize SVN CLI auth flags in SvnService and fix header text Co-authored-by: Cursor <cursoragent@cursor.com>
133 lines
4.1 KiB
Java
133 lines
4.1 KiB
Java
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, String username, String password)
|
||
throws IOException, InterruptedException, TimeoutException {
|
||
logger.info("更新工作副本: {}", workingDirectory);
|
||
|
||
if (!isValidWorkingCopy(workingDirectory)) {
|
||
throw new IllegalArgumentException("无效的SVN工作副本: " + workingDirectory);
|
||
}
|
||
|
||
List<String> args = new ArrayList<>();
|
||
addAuthArgs(args, username, password);
|
||
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, String username, String password)
|
||
throws IOException, InterruptedException, TimeoutException {
|
||
return update(workingDirectory, null, username, password);
|
||
}
|
||
|
||
public UpdateResult update(String workingDirectory)
|
||
throws IOException, InterruptedException, TimeoutException {
|
||
return update(workingDirectory, null, null, 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;
|
||
}
|
||
}
|
||
}
|