我该如何使用JGit进行git push操作?

27
我正在尝试构建一个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

这使我能够在本地仓库中创建、添加和提交内容,并将内容推送到远程仓库。现在,我正在尝试在我的Java代码中使用JGit执行相同的操作。使用JGit API,我可以轻松地执行git init、add和commit。
Repository localRepo = new FileRepository(localPath);
this.git = new Git(localRepo);        
localRepo.create();  
git.add().addFilePattern(".").call();
git.commit().setMessage("test message").call();

再次说明,所有这些都很好运作。我找不到任何关于 git remote addgit push 的示例或等效代码。我看了这个 SO question

testPush() 失败,并显示错误信息 TransportException: origin not found。在我看到的其他示例中https://gist.github.com/2487157git push 之前先执行 git clone,我不明白为什么需要这样做。

如果有任何指导,将不胜感激。

2个回答

41

最简单的方法是使用 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();

3
不错的替代方案,比我五年前提供的答案更好。+1 - VonC
setName("origin") 究竟是做什么的? - Jsleshem
1
设置新添加的远程仓库的名称,类似于在终端中调用 git remote add [name] [uri] 时传递名称。 - OlgaMaciaszek
2
我该如何将这样的代码推送到特定的分支? - Jsleshem
1
这对我不起作用。我得到了 org.eclipse.jgit.errors.NoRemoteRepositoryException: 的错误。我先创建了存储库,然后运行我的Java代码上传文件。它没有工作。 - Arun

18

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

  • RemoteconfigTest.java uses 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 illustrates various push scenario, using RemoteConfig.
    See testTrackingUpdate() for a complete example pushing an tracking 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();
    

谢谢您的输入。我确实看了PushCommandTest.java,但是没有理解足够使用它。我会尝试一下并更新。再次感谢。 - John Smith
1
现在尝试了一下,效果非常好!非常感谢你的帮助! - John Smith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接