| @@ -19,6 +19,7 @@ import java.nio.file.Path; | |||
| import java.nio.file.Paths; | |||
| import java.nio.file.StandardCopyOption; | |||
| import java.util.*; | |||
| import java.util.concurrent.ExecutorCompletionService; | |||
| import java.util.concurrent.ExecutorService; | |||
| import java.util.concurrent.Executors; | |||
| import java.util.concurrent.TimeUnit; | |||
| @@ -26,7 +27,7 @@ import java.util.concurrent.TimeUnit; | |||
| public class DVCUtils { | |||
| private static final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); | |||
| private static final Logger log = LoggerFactory.getLogger(DVCUtils.class); | |||
| private static final ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<>(executorService); | |||
| private static void runCommand(String command, String workingDir) throws Exception { | |||
| ProcessBuilder processBuilder = new ProcessBuilder(command.split(" ")); | |||
| processBuilder.directory(new File(workingDir)); | |||
| @@ -161,6 +162,7 @@ public class DVCUtils { | |||
| log.error("Error occurred while updating all branches", e); | |||
| } | |||
| } | |||
| /** | |||
| * 更新本地仓库中的指定分支 | |||
| * | |||
| @@ -220,14 +222,12 @@ public class DVCUtils { | |||
| public static void createLocalBranchBasedOnMaster(String localPath, String branchName) { | |||
| try (Git git = Git.open(new File(localPath))) { | |||
| // 切换到 master 分支 | |||
| git.checkout().setName("master").call(); | |||
| // 创建新的本地分支 | |||
| git.branchCreate().setName(branchName).call(); | |||
| log.info("基于 master 的本地分支 " + branchName + " 创建成功。"); | |||
| git.checkout().setName(branchName).call(); | |||
| git.checkout() | |||
| .setCreateBranch(true) | |||
| .setName(branchName) | |||
| .setStartPoint("origin/master") | |||
| .call(); | |||
| log.info("基于 master 的远程分支 " + branchName + " 创建成功。"); | |||
| } catch (IOException | GitAPIException e) { | |||
| log.error("Exception occurred while creating local branch based on master",e); | |||
| } | |||
| @@ -326,9 +326,7 @@ public class DVCUtils { | |||
| return false; | |||
| } | |||
| public static void refreshRemoteBranches(String localPath, String username, String password, String branch) throws Exception { | |||
| long startTime = System.currentTimeMillis(); | |||
| try (Repository repository = new FileRepositoryBuilder() | |||
| .setGitDir(new File(localPath + "/.git")) | |||
| .readEnvironment() | |||
| @@ -343,37 +341,42 @@ public class DVCUtils { | |||
| log.info("Repository is in a merging state, please resolve conflicts manually."); | |||
| return; | |||
| } | |||
| // 设置凭证提供者 | |||
| UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); | |||
| // 获取远程分支 | |||
| long fetchStartTime = System.currentTimeMillis(); | |||
| FetchResult fetchResult = git.fetch() | |||
| .setRemote("origin") | |||
| .setCredentialsProvider(credentialsProvider) | |||
| .call(); | |||
| long fetchEndTime = System.currentTimeMillis(); | |||
| log.info("Fetch time: " + (fetchEndTime - fetchStartTime) + " ms"); | |||
| // 打印获取的远程分支 | |||
| Collection<Ref> fetchedRefs = fetchResult.getAdvertisedRefs(); | |||
| for (Ref ref : fetchedRefs) { | |||
| log.info("Fetched branch: " + ref.getName()); | |||
| } | |||
| // 更新本地分支信息 | |||
| long branchListStartTime = System.currentTimeMillis(); | |||
| git.branchList() | |||
| .setListMode(org.eclipse.jgit.api.ListBranchCommand.ListMode.REMOTE) | |||
| .call() | |||
| .forEach(ref -> executorService.submit(() -> { | |||
| .forEach(ref -> { | |||
| String fullBranchName = ref.getName(); | |||
| String branchName = fullBranchName.replace("refs/remotes/origin/", ""); | |||
| try { | |||
| processBranch(git, repository, credentialsProvider, fullBranchName, branchName); | |||
| completionService.submit(() -> { | |||
| try { | |||
| processBranch(git, repository, credentialsProvider, fullBranchName, branchName); | |||
| } catch (Exception e) { | |||
| log.error("Failed to process branch: " + branchName, e); | |||
| } | |||
| return null; | |||
| }); | |||
| } catch (Exception e) { | |||
| log.error("Failed to process branch: " + branchName, e); | |||
| log.error("Task submission rejected", e); | |||
| } | |||
| })); | |||
| }); | |||
| executorService.shutdown(); | |||
| try { | |||
| @@ -381,17 +384,14 @@ public class DVCUtils { | |||
| } catch (InterruptedException e) { | |||
| log.error("Executor service interrupted", e); | |||
| } | |||
| long branchListEndTime = System.currentTimeMillis(); | |||
| log.info("Branch list and update time: " + (branchListEndTime - branchListStartTime) + " ms"); | |||
| log.info("远程分支刷新到本地完成。"); | |||
| } catch (Exception e) { | |||
| log.error("Error occurred while refreshing remote branches ", e); ; | |||
| log.error("Error occurred while refreshing remote branches ", e); | |||
| } | |||
| // 切换分支 | |||
| gitCheckoutBranch(localPath, branch); | |||
| dvcCheckout(localPath); | |||
| long endTime = System.currentTimeMillis(); | |||
| log.info("Total execution time: " + (endTime - startTime) + " ms"); | |||
| } | |||
| private static void processBranch(Git git, Repository repository, UsernamePasswordCredentialsProvider credentialsProvider, String fullBranchName, String branchName) throws Exception { | |||
| @@ -401,43 +401,29 @@ public class DVCUtils { | |||
| // 如果存在,检查是否已经关联到远程分支 | |||
| if (!isBranchUpstreamSet(repository, branchName)) { | |||
| // 如果没有关联,设置上游分支 | |||
| long upstreamStartTime = System.currentTimeMillis(); | |||
| git.branchCreate() | |||
| .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) | |||
| .setStartPoint(fullBranchName) | |||
| .setName(branchName) | |||
| .call(); | |||
| long upstreamEndTime = System.currentTimeMillis(); | |||
| log.info("Set upstream for branch: " + branchName + ", time: " + (upstreamEndTime - upstreamStartTime) + " ms"); | |||
| } | |||
| } else { | |||
| // 如果不存在,创建本地分支并设置上游分支 | |||
| long createBranchStartTime = System.currentTimeMillis(); | |||
| git.branchCreate() | |||
| .setName(branchName) | |||
| .setStartPoint(fullBranchName) | |||
| .call(); | |||
| long createBranchEndTime = System.currentTimeMillis(); | |||
| log.info("Created local branch: " + branchName + ", time: " + (createBranchEndTime - createBranchStartTime) + " ms"); | |||
| // 设置上游分支 | |||
| long setUpstreamStartTime = System.currentTimeMillis(); | |||
| git.branchCreate() | |||
| .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) | |||
| .setStartPoint(fullBranchName) | |||
| .setName(branchName) | |||
| .call(); | |||
| long setUpstreamEndTime = System.currentTimeMillis(); | |||
| log.info("Set upstream for branch: " + branchName + ", time: " + (setUpstreamEndTime - setUpstreamStartTime) + " ms"); | |||
| } | |||
| // 执行 git pull | |||
| long pullStartTime = System.currentTimeMillis(); | |||
| PullCommand pullCommand = git.pull() | |||
| .setCredentialsProvider(credentialsProvider); | |||
| pullCommand.call(); | |||
| long pullEndTime = System.currentTimeMillis(); | |||
| log.info("Updated local branch: " + branchName + ", time: " + (pullEndTime - pullStartTime) + " ms"); | |||
| } | |||
| /** | |||