简体   繁体   English

我如何使用 JGit 进行 git push?

[英]How do I do git push with JGit?

I'm trying to build a Java application that allows users to use Git based repositories.我正在尝试构建一个 Java 应用程序,允许用户使用基于 Git 的存储库。 I was able to do this from the command-line, using the following commands:我可以使用以下命令从命令行执行此操作:

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.我现在正在尝试使用 JGit 在我的 Java 代码中做同样的事情。 I was able to easily do git init, add and commit using JGit API.我能够使用 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 add and git push .我找不到git remote addgit push任何示例或等效代码。 I did look at this SO question .我确实看过这个SO question

testPush() fails with the error message TransportException: origin not found . testPush()失败并显示错误消息TransportException: origin not found In the other examples I've seen https://gist.github.com/2487157 do git clone before git push and I don't understand why that's necessary.在其他示例中,我看到https://gist.github.com/2487157git push之前执行git clone ,但我不明白为什么有必要这样做。

Any pointers to how I can do this will be appreciated.任何有关我如何做到这一点的指示将不胜感激。

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();

You will find in org.eclipse.jgit.test all the example you need:您将在org.eclipse.jgit.test找到您需要的所有示例:

  • RemoteconfigTest.java uses Config : 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 illustrates various push scenario, using RemoteConfig . PushCommandTest.java 使用RemoteConfig说明了各种推送场景。
    See testTrackingUpdate() for a complete example pushing an tracking a remote branch.testTrackingUpdate()为一个完整的例子推送跟踪远程分支。
    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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM