我怎么用JGit做git推? [英] How do I do git push with JGit?

查看:199
本文介绍了我怎么用JGit做git推?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  git init 
git add。
git commit
git remote add<远程存储库名称> <远程资源库URI>
git push -u<远程存储库名称> master

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


 存储库localRepo = new FileRepository(localPath); 
this.git =新的Git(localRepo);
localRepo.create();
git.add()。addFilePattern(。)。call();
git.commit()。setMessage(test message)。call();

再一次,所有这些工作都很好。我无法找到 git remote add git push 的任何示例或等效代码。我确实看过这个 SO问题
$ b

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



任何指向我如何做到这一点的指针将不胜感激。

解决方案

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






  • RemoteconfigTest.java 使用< a href =https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java =nofollow noreferrer> Config

      config.setString (remote,origin,pushurl,short:project.git); 
    config.setString(url,https:// server / repos /,name,short:);
    RemoteConfig rc =新的RemoteConfig(config,origin);
    assertFalse(rc.getPushURIs()。isEmpty());
    assertEquals(short:project.git,rc.getPushURIs()。get(0).toASCIIString());


  • PushCommandTest.java 演示了各种推送方案, using RemoteConfig

    请参阅 testTrackingUpdate() 完整的例子推动 an 跟踪远程分支。

    提取:

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

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



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:

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.

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. I did look at this SO question.

testPush() fails with the error message 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.

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

解决方案

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

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

这篇关于我怎么用JGit做git推?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆