Ver Fonte

获取 git 仓库的所有远程分支

reghao há 4 anos atrás
pai
commit
101d9a4591

+ 31 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/util/buildtool/repo/GitImpl.java

@@ -25,6 +25,7 @@ import org.eclipse.jgit.util.FS;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.Properties;
 
@@ -243,4 +244,34 @@ public class GitImpl implements CodeUpdater {
         int lastIndex = gitUrl.lastIndexOf("/");
         return gitUrl.substring(lastIndex).split(".git")[0];
     }
+
+    public List<String> remoteBranches(String remote) {
+        List<String> branches = new ArrayList<>();
+        try {
+            Collection<Ref> refList = Git.lsRemoteRepository().setRemote(remote).setCredentialsProvider(credentials).call();
+            for (Ref ref : refList) {
+                String refName = ref.getName();
+                if (refName.startsWith("refs/heads/")) {                       //需要进行筛选
+                    String branchName = refName.replace("refs/heads/", "");
+                    branches.add(branchName);
+                }
+            }
+            return branches;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return branches;
+    }
+
+    public static void main(String[] args) {
+        RepoAuthConfig repoAuthConfig = new RepoAuthConfig();
+        repoAuthConfig.setAuthType(RepoAuthType.http.getName());
+        repoAuthConfig.setUsername("azygjs");
+        repoAuthConfig.setPassword("6wNSPZH4");
+        GitImpl git = new GitImpl(repoAuthConfig);
+
+        String gitUrl = "https://codeup.aliyun.com/5f1f8daf6207a1a8b17f6742/background/DataService.git";
+        List<String> list = git.remoteBranches(gitUrl);
+        System.out.println();
+    }
 }