diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java index 888c84de..40aa8810 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java @@ -599,6 +599,8 @@ public class ModelsServiceImpl implements ModelsService { String relatePath = ci4sUsername + "/model/" + gitlinIid + "/" + repositoryName + "/model"; dvcUtils.gitClone(rootPath, projectUrl, branchName, gitLinkUsername, gitLinkPassword); + //干掉目标文件夹 + dvcUtils.deleteDirectory(modelPath); dvcUtils.moveFiles(sourcePath, modelPath); //拼接生产的元数据后写入yaml文件 @@ -701,10 +703,16 @@ public class ModelsServiceImpl implements ModelsService { Map stringObjectMap = YamlUtils.loadYamlFile(metaPath + "/" + "metadata.yaml"); ModelMetaVo oldModelVo = ConvertUtil.convertMapToObject(stringObjectMap, ModelMetaVo.class); + //检查是否存在本地重名分支,有的话干掉 + dvcUtils.deleteLocalBranch(rootPath, branchName); // 创建本地分支 dvcUtils.createLocalBranchBasedOnMaster(rootPath, branchName); //dvc checkout + dvcUtils.dvcPull(rootPath); dvcUtils.dvcCheckout(rootPath); + //干掉目标文件夹 + dvcUtils.deleteDirectory(modelPath); + dvcUtils.moveFiles(sourcePath, modelPath); if (modelsVo.getModelVersionVos().size() != 0) { String sourcePath = modelsVo.getModelVersionVos().get(0).getUrl(); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java index 48b0033d..cd2f20ef 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java @@ -117,7 +117,8 @@ public class NewDatasetServiceImpl implements NewDatasetService { // 命令行操作 git clone 项目地址 dvcUtils.gitClone(localPath, projectUrl, branchName, gitLinkUsername, gitLinkPassword); String s3Path = "management-platform-files" + "/" + relatePath + "/" + branchName; - + //干掉目标文件夹 + dvcUtils.deleteDirectory(datasetPath); dvcUtils.moveFiles(sourcePath, datasetPath); // 拼接生产的元数据后写入yaml文件 datasetVo.setCreateBy(String.valueOf(StringUtils.isNotEmpty((String) userInfo.get("nickname")) ? userInfo.get("nickname") : userInfo.get("login"))); @@ -183,16 +184,19 @@ public class NewDatasetServiceImpl implements NewDatasetService { //部分信息在前面的版本里面,从那边取过来 Map stringObjectMap = YamlUtils.loadYamlFile(localPath + "/" + "dataset.yaml"); NewDatasetVo newDatasetVo = ConvertUtil.convertMapToObject(stringObjectMap, NewDatasetVo.class); - + //检查是否存在本地重名分支,有的话干掉 + dvcUtils.deleteLocalBranch(localPath, branchName); // 创建本地分支 dvcUtils.createLocalBranchBasedOnMaster(localPath, branchName); //dvc checkout + dvcUtils.dvcPull(localPath); dvcUtils.dvcCheckout(localPath); // 准备数据 String s3Path = "management-platform-files" + "/" + relatePath + "/" + branchName; - + //干掉目标文件夹 + dvcUtils.deleteDirectory(datasetPath); dvcUtils.moveFiles(sourcePath, datasetPath); // 拼接生产的元数据后写入yaml文件 datasetVo.setCreateBy(String.valueOf(StringUtils.isNotEmpty((String) userInfo.get("nickname")) ? userInfo.get("nickname") : userInfo.get("login"))); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DVCUtils.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DVCUtils.java index 93d2a9db..e9445a24 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DVCUtils.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DVCUtils.java @@ -80,6 +80,26 @@ public class DVCUtils { Files.move(sourceDir, targetDir, StandardCopyOption.REPLACE_EXISTING); } + public void deleteDirectory(String dirPath) throws IOException { + Path directory = Paths.get(dirPath); + + // 检查是否目录存在 + if (Files.exists(directory)) { + // 使用Files.walk来删除目录及其内容 + Files.walk(directory) + .sorted((path1, path2) -> path2.compareTo(path1)) // 先删除子文件夹,再删除父文件夹 + .forEach(this::deletePath); + } + } + + private void deletePath(Path path) { + try { + Files.delete(path); + } catch (IOException e) { + log.error("Unable to delete: " + path + " " + e.getMessage()); + } + } + public void gitClone(String localPath, String repoUrl, String branch, String username, String password) throws GitAPIException { CloneCommand cloneCommand = Git.cloneRepository() .setURI(repoUrl) @@ -240,6 +260,33 @@ public class DVCUtils { log.error("Error occurred while creating local branch", e); } } + /** + * 删除本地分支 + * + * @param localPath 本地仓库路径 + * @param branchName 要删除的分支名称 + */ + public void deleteLocalBranch(String localPath, String branchName) throws IOException, GitAPIException { + FileRepositoryBuilder builder = new FileRepositoryBuilder(); + Repository repository = builder.setGitDir(new File(localPath + "/.git")) + .readEnvironment() + .findGitDir() + .build(); + + try (Git git = new Git(repository)) { + // 获取所有本地分支 + for (Ref ref : git.branchList().call()) { + String refName = ref.getName(); + if (refName.endsWith("/" + branchName)) { + // 删除本地分支 + git.branchDelete().setBranchNames(refName).setForce(true).call(); + log.info("Deleted branch: " + branchName); + return; + } + } + log.info("Branch not found: " + branchName); + } + } /** * 基于 master 创建本地分支 @@ -421,6 +468,7 @@ public class DVCUtils { // 切换分支 gitCheckoutBranch(localPath, branch); + dvcPull(localPath); dvcCheckout(localPath); } @@ -555,7 +603,7 @@ public class DVCUtils { // 更新的 dvcPull 方法 public void dvcPull(String localPath) throws Exception { - String command = "dvc pull"; + String command = "dvc pull -v"; runCommand(command, localPath); }