如何使用jgit将现有的存储库克隆到新的github实例? [英] How to use jgit to clone the existing repositories to new github instance?

查看:201
本文介绍了如何使用jgit将现有的存储库克隆到新的github实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有两个独立的GitHub实例正在运行。一个GitHub实例是 https://github.dev.host.com ,其他github实例是 https://github.host.com code>。我在 https://github.dev.host.com 中有各种版本库,我需要迁移到这个新的github实例 https:// github .host.com



我正在使用JGit,因为我正在使用Java。例如 - 下面是存在于 https://github.dev.host.com 实例中的存储库,我需要迁移到新的github实例 https://github.host.com

  https://github.dev.host .com / Database / ClientService 
https://github.dev.host.com/Database/Interest

当我使用JGit时,所以我想通过Java代码在新的GitHub实例中创建以上两个存储库。在运行我的Java代码之后,我应该将我的 https://github.dev.host.com 中的所有上述存储库及其所有分支和内容看到我的新Github实例 https://github.host.com ,如下所示 -

  https://github.host.com/Database/ClientService 
https://github.host.com/Database/Interest

我只需要迭代我在旧的github实例中的所有存储库列表,并创建它们,如果它们不在我的新github实例中退出并显示所有内容和分支。如果它们已经存在,则覆盖从旧到新实例的所有更改。



这是否可以使用JGit执行此操作?我还有 https 通过我的用户名和密码访问我的两个github实例。



截至目前,我可以

  public class CreateNewRepository {

public static void main(String [] args)throws IOException {
//准备一个新文件夹
文件localPath = File.createTempFile(TestGitRepository,);
localPath.delete();

//创建目录
版本库= FileRepositoryBuilder.create(new File(localPath,.git));
repository.create();

System.out.println(Having repository:+ repository.getDirectory());

repository.close();

FileUtils.deleteDirectory(localPath);




$ b $ p
$ b

任何建议都会有很大的帮助,因为这是我的第一次与JGit合作。

解决方案

可行的方法是将repositoy从服务器复制到临时位置,从那里将它推送到目的地服务器。



您可以像这样克隆一个存储库,使用JGit:

  Git.cloneRepository()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(user,password));
.setURI(remoteRepoUrl)
.setDirectory(localDirectory)
.setCloneAllBranches(true)
.call();

要将刚刚克隆的repositoy传输到目标,您必须先在目标服务器上创建一个存储库。 JGit和Git都不支持这一步。 GitHub提供了一个REST API,可让您创建存储库开发者页面还列出了Java中可用于此API的语言绑定。



一旦(空)存储库存在,您可以从临时副本推送到远程:

  Git git = Git.open(localDirectory); 
git.push()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(user,password));
.setRemote(newRemoteRepoUrl)
.setForce(true)
.setPushAll()
.setPushTags()
.call()

有关身份验证的更多信息,请参阅 here

注意,如果源存储库包含标签,那么必须将这些标签单独提取到临时存储库中克隆。


We have two separate GitHub instance running. One GitHub instance is https://github.dev.host.com and other github instance is https://github.host.com. I have various repositories in https://github.dev.host.com which I need to migrate to this new github instance https://github.host.com.

I am using JGit as I am working with Java. For example - Below are the repositories that are present in https://github.dev.host.com instance which I need to migrate to new github instance https://github.host.com

https://github.dev.host.com/Database/ClientService
https://github.dev.host.com/Database/Interest

As I am using JGit so I want to create these two above repositories in my new GitHub instance through Java code. And after running my Java code, I should see all the above respositories and all its branches and contents from my https://github.dev.host.com into my new Github instance https://github.host.com as shown something like below -

https://github.host.com/Database/ClientService
https://github.host.com/Database/Interest

I just need to iterate the all the repositories list which I have in my old github instance and create those if they don't exit with all its contents and branches in my new github instance. And if they already exists, then overwrite all the changes from old to new instance.

Is this possible to do this using JGit? I also have https access to my both the github instances through my username and password.

As of now I can only do basic stuff as shown below which I learnt going through the tutorial.

public class CreateNewRepository {

    public static void main(String[] args) throws IOException {
        // prepare a new folder
        File localPath = File.createTempFile("TestGitRepository", "");
        localPath.delete();

        // create the directory
        Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
        repository.create();

        System.out.println("Having repository: " + repository.getDirectory());

        repository.close();

        FileUtils.deleteDirectory(localPath);
    }
}

Any suggestions will be of great help as this is my first time working with JGit.

解决方案

A viable approach would be to clone the repositoy from the source server to a temporary location and from there push it to the destination server.

You can clone a repository with JGit like this:

Git.cloneRepository()
  .setCredentialsProvider( new UsernamePasswordCredentialsProvider( "user", "password" ) );
  .setURI( remoteRepoUrl )
  .setDirectory( localDirectory )
  .setCloneAllBranches( true )
  .call();

To transfer the just cloned repositoy to the destination, you have to create a repository on the destination server first. Neither JGit nor Git support this step. GitHub offers a REST API that lets you create repositories. The developer pages also list the language bindings that are available for this API with Java among them.

Once the (empty) repository is there, you can push from the temporary copy to the remote:

  Git git = Git.open( localDirectory );
  git.push()
    .setCredentialsProvider( new UsernamePasswordCredentialsProvider( "user",  "password" ) );
    .setRemote( newRemoteRepoUrl )
    .setForce( true )
    .setPushAll()
    .setPushTags()
    .call()

More information about authentication can be found here

Note that if the source repository contains tags, these have to be fetched into the temporary repository separately after cloning.

这篇关于如何使用jgit将现有的存储库克隆到新的github实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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