|
|
|
@@ -9,6 +9,8 @@ import org.eclipse.jgit.storage.file.FileRepositoryBuilder; |
|
|
|
import org.eclipse.jgit.transport.*; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.File; |
|
|
|
@@ -24,10 +26,32 @@ import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@Component |
|
|
|
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); |
|
|
|
@Value("${proxy.useProxy:false}") |
|
|
|
private boolean useProxy; |
|
|
|
|
|
|
|
@Value("${proxy.host}") |
|
|
|
private String host; |
|
|
|
|
|
|
|
@Value("${proxy.port}") |
|
|
|
private Integer port; |
|
|
|
private class ProxyConfigCallback implements TransportConfigCallback { |
|
|
|
@Override |
|
|
|
public void configure(Transport transport) { |
|
|
|
if (useProxy) { |
|
|
|
System.setProperty("http.proxyHost", host); |
|
|
|
System.setProperty("http.proxyPort", String.valueOf(port)); |
|
|
|
System.setProperty("https.proxyHost", host); |
|
|
|
System.setProperty("https.proxyPort", String.valueOf(port)); |
|
|
|
log.info("Proxy configured: {}:{}", host, port); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static void runCommand(String command, String workingDir) throws Exception { |
|
|
|
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" ")); |
|
|
|
processBuilder.directory(new File(workingDir)); |
|
|
|
@@ -43,7 +67,7 @@ public class DVCUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void moveFiles(String sourcePath, String targetPath) throws Exception { |
|
|
|
public void moveFiles(String sourcePath, String targetPath) throws Exception { |
|
|
|
Path sourceDir = Paths.get(sourcePath); |
|
|
|
Path targetDir = Paths.get(targetPath); |
|
|
|
|
|
|
|
@@ -54,17 +78,18 @@ public class DVCUtils { |
|
|
|
Files.move(sourceDir, targetDir, StandardCopyOption.REPLACE_EXISTING); |
|
|
|
} |
|
|
|
|
|
|
|
public static void gitClone(String localPath, String repoUrl, String branch, String username, String password) throws GitAPIException { |
|
|
|
public void gitClone(String localPath, String repoUrl, String branch, String username, String password) throws GitAPIException { |
|
|
|
CloneCommand cloneCommand = Git.cloneRepository() |
|
|
|
.setURI(repoUrl) |
|
|
|
.setBranch(branch) |
|
|
|
.setDirectory(new java.io.File(localPath)) |
|
|
|
.setTransportConfigCallback(new ProxyConfigCallback()) |
|
|
|
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password)); |
|
|
|
|
|
|
|
cloneCommand.call(); |
|
|
|
} |
|
|
|
|
|
|
|
public static void gitClone(String localPath, String repoUrl, String username, String password) throws GitAPIException { |
|
|
|
public void gitClone(String localPath, String repoUrl, String username, String password) throws GitAPIException { |
|
|
|
CloneCommand cloneCommand = Git.cloneRepository() |
|
|
|
.setURI(repoUrl) |
|
|
|
.setDirectory(new java.io.File(localPath)) |
|
|
|
@@ -74,7 +99,7 @@ public class DVCUtils { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void gitAdd(String localPath, String filePath) throws IOException, GitAPIException { |
|
|
|
public void gitAdd(String localPath, String filePath) throws IOException, GitAPIException { |
|
|
|
FileRepositoryBuilder builder = new FileRepositoryBuilder(); |
|
|
|
Repository repository = builder.setGitDir(new File(localPath, ".git")) |
|
|
|
.readEnvironment() |
|
|
|
@@ -87,7 +112,7 @@ public class DVCUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void gitCommit(String localPath, String commitMessage) throws IOException, GitAPIException { |
|
|
|
public void gitCommit(String localPath, String commitMessage) throws IOException, GitAPIException { |
|
|
|
FileRepositoryBuilder builder = new FileRepositoryBuilder(); |
|
|
|
Repository repository = builder.setGitDir(new File(localPath, ".git")) |
|
|
|
.readEnvironment() |
|
|
|
@@ -105,7 +130,7 @@ public class DVCUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void gitPush(String localPath, String username, String password) throws IOException, GitAPIException { |
|
|
|
public void gitPush(String localPath, String username, String password) throws IOException, GitAPIException { |
|
|
|
FileRepositoryBuilder builder = new FileRepositoryBuilder(); |
|
|
|
Repository repository = builder.setGitDir(new File(localPath, ".git")) |
|
|
|
.readEnvironment() |
|
|
|
@@ -116,6 +141,7 @@ public class DVCUtils { |
|
|
|
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); |
|
|
|
PushCommand pushCommand = git.push(); |
|
|
|
pushCommand.setCredentialsProvider(credentialsProvider) |
|
|
|
.setTransportConfigCallback(new ProxyConfigCallback()) |
|
|
|
.setForce(true) |
|
|
|
.call(); |
|
|
|
} |
|
|
|
@@ -129,7 +155,7 @@ public class DVCUtils { |
|
|
|
* @param username 远程仓库用户名 |
|
|
|
* @param password 远程仓库密码 |
|
|
|
*/ |
|
|
|
public static void updateAllBranches(String localPath, String username, String password) { |
|
|
|
public void updateAllBranches(String localPath, String username, String password) { |
|
|
|
try (Git git = Git.open(new File(localPath))) { |
|
|
|
// 设置凭证,用于远程仓库的认证 |
|
|
|
UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); |
|
|
|
@@ -171,7 +197,7 @@ public class DVCUtils { |
|
|
|
* @param password 远程仓库密码 |
|
|
|
* @param branchName 需要更新的分支名称 |
|
|
|
*/ |
|
|
|
public static void updateBranch(String localPath, String username, String password, String branchName) { |
|
|
|
public void updateBranch(String localPath, String username, String password, String branchName) { |
|
|
|
try (Git git = Git.open(new File(localPath))) { |
|
|
|
// 设置凭证,用于远程仓库的认证 |
|
|
|
UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); |
|
|
|
@@ -202,7 +228,7 @@ public class DVCUtils { |
|
|
|
* @param localPath 本地仓库路径 |
|
|
|
* @param branchName 要创建的分支名称 |
|
|
|
*/ |
|
|
|
public static void createLocalBranch(String localPath, String branchName) { |
|
|
|
public void createLocalBranch(String localPath, String branchName) { |
|
|
|
try (Git git = Git.open(new File(localPath))) { |
|
|
|
// 创建本地分支 |
|
|
|
git.branchCreate().setName(branchName).call(); |
|
|
|
@@ -219,7 +245,7 @@ public class DVCUtils { |
|
|
|
* @param localPath 本地仓库路径 |
|
|
|
* @param branchName 要创建的分支名称 |
|
|
|
*/ |
|
|
|
public static void createLocalBranchBasedOnMaster(String localPath, String branchName) { |
|
|
|
public void createLocalBranchBasedOnMaster(String localPath, String branchName) { |
|
|
|
try (Git git = Git.open(new File(localPath))) { |
|
|
|
// 切换到 master 分支 |
|
|
|
git.checkout() |
|
|
|
@@ -241,7 +267,7 @@ public class DVCUtils { |
|
|
|
* @param password 远程仓库密码 |
|
|
|
* @param branchName 要推送的分支名称 |
|
|
|
*/ |
|
|
|
public static void pushLocalBranchToRemote(String localPath, String username, String password, String branchName) { |
|
|
|
public void pushLocalBranchToRemote(String localPath, String username, String password, String branchName) { |
|
|
|
try (Git git = Git.open(new File(localPath))) { |
|
|
|
// 设置凭证,用于远程仓库的认证 |
|
|
|
UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); |
|
|
|
@@ -279,7 +305,7 @@ public class DVCUtils { |
|
|
|
* @throws IOException 如果仓库路径无效 |
|
|
|
* @throws GitAPIException 如果Git操作失败 |
|
|
|
*/ |
|
|
|
public static void pushNewBranchToRemote(String localPath, String username, String password, String branchName) throws IOException, GitAPIException { |
|
|
|
public void pushNewBranchToRemote(String localPath, String username, String password, String branchName) throws IOException, GitAPIException { |
|
|
|
try (Git git = Git.open(new File(localPath))) { |
|
|
|
// 设置凭证,用于远程仓库的认证 |
|
|
|
UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); |
|
|
|
@@ -300,6 +326,7 @@ public class DVCUtils { |
|
|
|
.setRemote("origin") |
|
|
|
.setRefSpecs(refSpec) |
|
|
|
.setCredentialsProvider(credentialsProvider) |
|
|
|
.setTransportConfigCallback(new ProxyConfigCallback()) |
|
|
|
.call(); |
|
|
|
|
|
|
|
// 打印结果 |
|
|
|
@@ -326,7 +353,7 @@ public class DVCUtils { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public static void refreshRemoteBranches(String localPath, String username, String password, String branch) throws Exception { |
|
|
|
public void refreshRemoteBranches(String localPath, String username, String password, String branch) throws Exception { |
|
|
|
try (Repository repository = new FileRepositoryBuilder() |
|
|
|
.setGitDir(new File(localPath + "/.git")) |
|
|
|
.readEnvironment() |
|
|
|
@@ -349,6 +376,7 @@ public class DVCUtils { |
|
|
|
FetchResult fetchResult = git.fetch() |
|
|
|
.setRemote("origin") |
|
|
|
.setCredentialsProvider(credentialsProvider) |
|
|
|
.setTransportConfigCallback(new ProxyConfigCallback()) // 设置代理 |
|
|
|
.call(); |
|
|
|
|
|
|
|
// 打印获取的远程分支 |
|
|
|
@@ -394,7 +422,7 @@ public class DVCUtils { |
|
|
|
dvcCheckout(localPath); |
|
|
|
} |
|
|
|
|
|
|
|
private static void processBranch(Git git, Repository repository, UsernamePasswordCredentialsProvider credentialsProvider, String fullBranchName, String branchName) throws Exception { |
|
|
|
private void processBranch(Git git, Repository repository, UsernamePasswordCredentialsProvider credentialsProvider, String fullBranchName, String branchName) throws Exception { |
|
|
|
// 检查本地分支是否存在 |
|
|
|
Ref localRef = repository.findRef("refs/heads/" + branchName); |
|
|
|
if (localRef != null) { |
|
|
|
@@ -422,6 +450,7 @@ public class DVCUtils { |
|
|
|
} |
|
|
|
// 执行 git pull |
|
|
|
PullCommand pullCommand = git.pull() |
|
|
|
.setTransportConfigCallback(new ProxyConfigCallback()) |
|
|
|
.setCredentialsProvider(credentialsProvider); |
|
|
|
pullCommand.call(); |
|
|
|
} |
|
|
|
@@ -447,7 +476,7 @@ public class DVCUtils { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void gitFetch(String localPath, String username, String password) throws IOException, GitAPIException { |
|
|
|
public void gitFetch(String localPath, String username, String password) throws IOException, GitAPIException { |
|
|
|
FileRepositoryBuilder builder = new FileRepositoryBuilder(); |
|
|
|
Repository repository = builder.setGitDir(new File(localPath, ".git")) |
|
|
|
.readEnvironment() |
|
|
|
@@ -460,7 +489,7 @@ public class DVCUtils { |
|
|
|
fetchCommand.setCredentialsProvider(credentialsProvider).call(); |
|
|
|
} |
|
|
|
} |
|
|
|
public static void gitCheckoutBranch(String localPath, String branchName) throws IOException, GitAPIException { |
|
|
|
public void gitCheckoutBranch(String localPath, String branchName) throws IOException, GitAPIException { |
|
|
|
FileRepositoryBuilder builder = new FileRepositoryBuilder(); |
|
|
|
Repository repository = builder.setGitDir(new File(localPath, ".git")) |
|
|
|
.readEnvironment() |
|
|
|
@@ -473,7 +502,7 @@ public class DVCUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void gitPull(String localPath, String username, String password) throws IOException, GitAPIException { |
|
|
|
public void gitPull(String localPath, String username, String password) throws IOException, GitAPIException { |
|
|
|
FileRepositoryBuilder builder = new FileRepositoryBuilder(); |
|
|
|
Repository repository = builder.setGitDir(new File(localPath, ".git")) |
|
|
|
.readEnvironment() |
|
|
|
@@ -487,50 +516,50 @@ public class DVCUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void dvcInit(String localPath) throws Exception { |
|
|
|
public void dvcInit(String localPath) throws Exception { |
|
|
|
String command = "dvc init"; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
|
|
|
|
public static void dvcAdd(String localPath, String filePath) throws Exception { |
|
|
|
public void dvcAdd(String localPath, String filePath) throws Exception { |
|
|
|
String command = "dvc add " + filePath; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
|
|
|
|
public static void dvcRemoteAdd(String localPath, String s3RemoteUrl) throws Exception { |
|
|
|
public void dvcRemoteAdd(String localPath, String s3RemoteUrl) throws Exception { |
|
|
|
String command = "dvc remote add -d myremote s3://" + s3RemoteUrl; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
|
|
|
|
public static void dvcConfigS3Credentials(String localPath, String endpointurl) throws Exception { |
|
|
|
public void dvcConfigS3Credentials(String localPath, String endpointurl) throws Exception { |
|
|
|
String command = "dvc remote modify myremote endpointurl " + endpointurl; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
|
|
|
|
public static void dvcConfigS3Credentials2(String localPath, String accessKeyId) throws Exception { |
|
|
|
public void dvcConfigS3Credentials2(String localPath, String accessKeyId) throws Exception { |
|
|
|
String command = "dvc remote modify myremote access_key_id " + accessKeyId; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
|
|
|
|
public static void dvcConfigS3Credentials3(String localPath, String secretAccessKey) throws Exception { |
|
|
|
public void dvcConfigS3Credentials3(String localPath, String secretAccessKey) throws Exception { |
|
|
|
String command = "dvc remote modify myremote secret_access_key " + secretAccessKey; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
|
|
|
|
public static void dvcPush(String localPath) throws Exception { |
|
|
|
public void dvcPush(String localPath) throws Exception { |
|
|
|
String command = "dvc push -v"; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新的 dvcPull 方法 |
|
|
|
public static void dvcPull(String localPath) throws Exception { |
|
|
|
public void dvcPull(String localPath) throws Exception { |
|
|
|
String command = "dvc pull"; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 方法 |
|
|
|
public static void dvcCheckout(String localPath) throws Exception { |
|
|
|
public void dvcCheckout(String localPath) throws Exception { |
|
|
|
String command = "dvc checkout"; |
|
|
|
runCommand(command, localPath); |
|
|
|
} |
|
|
|
@@ -544,7 +573,7 @@ public class DVCUtils { |
|
|
|
* @param branch 分支名称 |
|
|
|
* @return 包含文件路径、名称和大小的List<Map<String, Object>> |
|
|
|
*/ |
|
|
|
public static List<Map<String, Object>> getFileDetailsAfterGitPull(String localPath, String repoFolder, String branch, String filePath , String username, String password) { |
|
|
|
public List<Map<String, Object>> getFileDetailsAfterGitPull(String localPath, String repoFolder, String branch, String filePath , String username, String password) { |
|
|
|
List<Map<String, Object>> fileInfoList = new ArrayList<>(); |
|
|
|
|
|
|
|
try { |
|
|
|
|