Reputation: 11709
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.
Upvotes: 0
Views: 1980
Reputation: 21025
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.
Upvotes: 2