【问题标题】:Implementing gerrit command in Jgit [duplicate]在 Jgit 中实现 gerrit 命令 [重复]
【发布时间】:2021-12-25 12:32:13
【问题描述】:

我想在Git中实现这个命令,但是不知道怎么办?

push origin HEAD:refs/for/master



    

【问题讨论】:

标签: git gerrit jgit


【解决方案1】:

试试下面的例子:

public static void push(String remoteUrl, Repository repo, StandardCredentials credential, String localBranchRef, String remoteBranchRef) {
    try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
        String pushSpec = "+" + localBranchRef + ":" + remoteBranchRef;
        PushCommand pushCommand = git.push();

        addCredential(repo, pushCommand, credential);

        Iterable<PushResult> resultIterable = pushCommand
            .setRefSpecs(new RefSpec(pushSpec))
            .setRemote(remoteUrl)
            .call();
        PushResult result = resultIterable.iterator().next();
        if (result.getRemoteUpdates().isEmpty()) {
            throw new RuntimeException("No remote updates occurred");
        } else {
            for (RemoteRefUpdate update : result.getRemoteUpdates()) {
                if (!RemoteRefUpdate.Status.OK.equals(update.getStatus())) {
                    throw new ServiceException.UnexpectedErrorException("Remote update failed: " + update.getStatus().name() + ": " + update.getMessage());
                }
            }
        }
    } catch (GitAPIException e) {
        if (e.getMessage().toLowerCase().contains("auth")) {
            throw new ServiceException.UnauthorizedException(e.getMessage(), e);
        }
        throw new ServiceException.UnexpectedErrorException("Unable to save and push to: " + remoteUrl + " - " + e.getMessage(), e);
    }
}

您可以找到更多信息和示例here

【讨论】:

  • 对不起,你知道如何在JGIT中实现这个命令吗?
  • 谢谢。尝试后发现缺少changeId,所以没有成功!
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • @NicoHaase 我在答案中添加了相关部分。
猜你喜欢
  • 2012-09-07
  • 2012-11-04
  • 2013-12-28
  • 1970-01-01
  • 2013-06-14
  • 2013-01-02
  • 2018-03-14
  • 2021-10-02
相关资源
最近更新 更多