我如何使用 JGit 进行 git push?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13446842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:46:04  来源:igfitidea点击:

How do I do git push with JGit?

gitjgit

提问by John Smith

I'm trying to build a Java application that allows users to use Git based repositories. I was able to do this from the command-line, using the following commands:

我正在尝试构建一个 Java 应用程序,允许用户使用基于 Git 的存储库。我可以使用以下命令从命令行执行此操作:

git init
<create some files>
git add .
git commit
git remote add <remote repository name> <remote repository URI>
git push -u <remote repository name> master

This allowed me to create, add and commit content to my local repository and push contents to the remote repository. I am now trying to do the same thing in my Java code, using JGit. I was able to easily do git init, add and commit using JGit API.

这允许我创建、添加和提交内容到我的本地存储库并将内容推送到远程存储库。我现在正在尝试使用 JGit 在我的 Java 代码中做同样的事情。我能够使用 JGit API 轻松地执行 git init、添加和提交。

Repository localRepo = new FileRepository(localPath);
this.git = new Git(localRepo);        
localRepo.create();  
git.add().addFilePattern(".").call();
git.commit().setMessage("test message").call();

Again, all of this works fine. I couldn't find any example or equivalent code for git remote addand git push. I did look at this SO question.

同样,所有这些都可以正常工作。我找不到git remote addand 的任何示例或等效代码git push。我确实看过这个SO question

testPush()fails with the error message TransportException: origin not found. In the other examples I've seen https://gist.github.com/2487157do git clonebeforegit pushand I don't understand why that's necessary.

testPush()失败并显示错误消息TransportException: origin not found。在其他例子我见过https://gist.github.com/2487157git clone之前git push,我不明白为什么这是必要的。

Any pointers to how I can do this will be appreciated.

任何有关我如何做到这一点的指示将不胜感激。

采纳答案by VonC

You will find in org.eclipse.jgit.testall the example you need:

您会在org.eclipse.jgit.test所有需要的示例中找到:

  • RemoteconfigTest.javauses Config:

    config.setString("remote", "origin", "pushurl", "short:project.git");
    config.setString("url", "https://server/repos/", "name", "short:");
    RemoteConfig rc = new RemoteConfig(config, "origin");
    assertFalse(rc.getPushURIs().isEmpty());
    assertEquals("short:project.git", rc.getPushURIs().get(0).toASCIIString());
    
  • PushCommandTest.javaillustrates various push scenario, using RemoteConfig.
    See testTrackingUpdate()for a complete example pushing antracking a remote branch.
    Extracts:

    String trackingBranch = "refs/remotes/" + remote + "/master";
    RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch);
    trackingBranchRefUpdate.setNewObjectId(commit1.getId());
    trackingBranchRefUpdate.update();
    
    URIish uri = new URIish(db2.getDirectory().toURI().toURL());
    remoteConfig.addURI(uri);
    remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
        + remote + "/*"));
    remoteConfig.update(config);
    config.save();
    
    
    RevCommit commit2 = git.commit().setMessage("Commit to push").call();
    
    RefSpec spec = new RefSpec(branch + ":" + branch);
    Iterable<PushResult> resultIterable = git.push().setRemote(remote)
        .setRefSpecs(spec).call();
    
  • RemoteconfigTest.java用途Config

    config.setString("remote", "origin", "pushurl", "short:project.git");
    config.setString("url", "https://server/repos/", "name", "short:");
    RemoteConfig rc = new RemoteConfig(config, "origin");
    assertFalse(rc.getPushURIs().isEmpty());
    assertEquals("short:project.git", rc.getPushURIs().get(0).toASCIIString());
    
  • PushCommandTest.java说明了各种推送场景,使用RemoteConfig.
    testTrackingUpdate()一个完整的例子推动跟踪远程分支。
    摘录:

    String trackingBranch = "refs/remotes/" + remote + "/master";
    RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch);
    trackingBranchRefUpdate.setNewObjectId(commit1.getId());
    trackingBranchRefUpdate.update();
    
    URIish uri = new URIish(db2.getDirectory().toURI().toURL());
    remoteConfig.addURI(uri);
    remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
        + remote + "/*"));
    remoteConfig.update(config);
    config.save();
    
    
    RevCommit commit2 = git.commit().setMessage("Commit to push").call();
    
    RefSpec spec = new RefSpec(branch + ":" + branch);
    Iterable<PushResult> resultIterable = git.push().setRemote(remote)
        .setRefSpecs(spec).call();
    

回答by OlgaMaciaszek

The easiest way is to use the JGit Porcelain API:

最简单的方法是使用 JGit Porcelain API:

    Git git = Git.open(localPath); 

    // add remote repo:
    RemoteAddCommand remoteAddCommand = git.remoteAdd();
    remoteAddCommand.setName("origin");
    remoteAddCommand.setUri(new URIish(httpUrl));
    // you can add more settings here if needed
    remoteAddCommand.call();

    // push to remote:
    PushCommand pushCommand = git.push();
    pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"));
    // you can add more settings here if needed
    pushCommand.call();