【问题标题】:Retrieving Branches from GIT url using java code使用 java 代码从 GIT url 检索分支
【发布时间】:2014-05-20 09:22:28
【问题描述】:

目前我正在使用 Jgit API 从源代码创建一个分支。我已经成功创建了分支,当我创建分支时,我需要验证当前 GIT 存储库中不应该存在分支名称(在线 URL:Ex.https://github.com/varunkumar-s/test.git)。

在这里,我想从上面的 url 而不是本地 git 存储库中列出分支名称。那么我该怎么做呢?当我提供 git 存储库时,我想要输出分支列表。

【问题讨论】:

  • 评论因为我没有使用过这个API。但是使用 git CLI,我可能会从 git ls-remote 开始,所以 LsRemoteCommand 可能是一个开始的地方。

标签: java eclipse git github jgit


【解决方案1】:

jgit-cookbookListRemoteRepository 有一个片段,基本上你只需要:

    Collection<Ref> refs = Git.lsRemoteRepository()
            .setHeads(true)
            .setRemote(REMOTE_URL)
            .call();

    for (Ref ref : refs) {
        System.out.println("Ref: " + ref);
    }

您可以从 ref 中使用 ref.getName() 来获取类似于“refs/heads/”的名称。

【讨论】:

  • 这看起来很棒。但是我收到了编译错误状态,即 lsRemoteRepository() 方法对于 Git 类型未定义。我想我错误地使用了 Jgit JAR 版本。请让我知道哪个版本有 lsRemoteRepository 方法可以使用。我目前正在使用 JAR for api.git v3.0.0
  • jgit-cookbook 目前使用的是 3.3.2,详见github.com/centic9/jgit-cookbook/blob/master/pom.xml,但它应该在早期版本中可用,至少从 2.0.0 开始,所以你可能需要看看如何添加 JGit Jar。
  • 更人性化的输出:String branchName = ref.getName().substring(ref.getName().lastIndexOf("/") + 1, ref.getName().length()); System.out.println("Branch: " + branchName + " (" + ref.getObjectId().getName() + ")");
猜你喜欢
  • 2015-03-29
  • 2014-05-31
  • 2011-09-09
  • 2017-11-28
  • 2011-07-06
  • 2019-08-16
  • 2011-03-16
  • 2014-04-20
  • 2010-09-26
相关资源
最近更新 更多