32092

How to use jgit to clone the existing repositories to new github instance?

Question:

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.

Answer1:

A viable approach would be to clone the repositoy from the <em>source</em> server to a temporary location and from there push it to the <em>destination</em> 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 <a href="https://developer.github.com/v3/repos/" rel="nofollow">create repositories</a>. The <a href="https://developer.github.com/libraries/" rel="nofollow">developer pages</a> 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 <a href="http://www.codeaffine.com/2014/12/09/jgit-authentication/" rel="nofollow">here</a>

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

Recommend

  • Google Drive SDK Mobile integration query
  • Prepared Statements with dynamic WHERE clause [duplicate]
  • MYSQL: Two fields as PRIMARY KEYs + 1 Field as UNIQUE, a question
  • Object property instance on class?
  • Reading Win32 variables
  • How to parse Selenium driver elements?
  • Programmatically getting SQL Cluster Virtual Name
  • How to use AWS roles with Packer to create AMIs
  • Basic Augmenting Types in Javascript
  • Novacode Determine If Word Style Is A Table
  • Error at typescript compilation with webpack
  • Django : Model Instance history
  • Client-Side: Accessing Windows Azure Drive?
  • Serializing socket
  • In a MEAN stack, how can I do one-time MongoDB indexing?
  • Python PIL remove sections of an image based on its colour
  • a concept similar to pointers in as3?
  • Consuming a web service with the Netbeans Platform
  • LNK1104: cannot open file 'kernel32.lib'
  • Invert string in Rust
  • Less Conflicting Session Manager for Zope 2
  • calculate gradient output for Theta update rule
  • Xamarin Forms - UWP Fonts
  • output of program is not same as passed argument
  • sending/ receiving email in Java
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Suggestions to manage Login/Logout transitions
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?