package boa.datagen.forges.github;

import java.io.File;
import java.io.IOException;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;


/**
 * Simple snippet which shows how to clone a repository from a remote source
 *
 * @author dominik.stadler at gmx.at
 */
public class RepositoryCloner {

	public static void clone(String[] args)
			throws IOException, InvalidRemoteException, TransportException, GitAPIException {
		// prepare a new folder for the cloned repository
		String localPath = args[1];
		String url = args[0];
		File localGitDir = new File(localPath + "/.git");
		// then clone
		Git result = null;
		
	
		java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
		try {
			result = Git.cloneRepository().setURI(url).setBare(true).setDirectory(localGitDir).call();
			// Note: the call() returns an opened repository already which needs
			// to be closed to avoid file handle leaks!
			// workaround for
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=474093
			result.getRepository().close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (result != null && result.getRepository() != null)
				result.getRepository().close();
		}
	}

}
@@ -77,16 +77,41 @@
 ception;
+%0Aimport java.util.Random;
 %0A%0Aimport
@@ -304,17 +304,16 @@
 ption;%0A%0A
-%0A
 /**%0A * S
@@ -785,18 +785,90 @@
 null;%0A%09%09
+int low = 1;%0A%09%09int high = 1000;%0A%09%09Random r = new Random();%0A%09%09int pow = 1;
 %0A
-%09
 %0A%09%09java.
@@ -942,14 +942,97 @@
 ;%0A%09%09
-try %7B%0A
+boolean success = false;%0A%09%09int trys = 0;%0A%09%09while (success)%0A%09%09%09try %7B%0A%09%09%09%09success = true;%0A%09
 %09%09%09r
@@ -1124,16 +1124,17 @@
 l();%0A%09%09%09
+%09
 // Note:
@@ -1191,15 +1191,23 @@
 hich
+%0A%09%09%09%09//
  needs%0A
+%09
 %09%09%09/
@@ -1252,16 +1252,17 @@
 aks!%0A%09%09%09
+%09
 // worka
@@ -1271,16 +1271,17 @@
 und for%0A
+%09
 %09%09%09// ht
@@ -1334,16 +1334,17 @@
 4093%0A%09%09%09
+%09
 result.g
@@ -1369,16 +1369,17 @@
 se();%0A%09%09
+%09
 %7D catch 
@@ -1401,29 +1401,385 @@
 %0A%09%09%09
-e.printStackTrace();%0A
+%09if (e.getMessage().contains(%22Authentication is required but no CredentialsProvider%22)) %7B%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D%0A%09%09%09%09if (trys %3C 5) %7B%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09pow *=2;%0A%09%09%09%09%09%09Thread.sleep(((int) Math.round(pow * 1000) + (r.nextInt(high - low) + low)));%0A%09%09%09%09%09%09success = false;%0A%09%09%09%09%09%7D catch (InterruptedException ie) %7B%0A%09%09%09%09%09%09// ie.printStackTrace();%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09// e.printStackTrace();%0A%09
 %09%09%7D 
@@ -1788,16 +1788,18 @@
 nally %7B%0A
+%0A%09
 %09%09%09if (r
@@ -1847,24 +1847,25 @@
 = null)%0A%09%09%09%09
+%09
 result.getRe
@@ -1890,11 +1890,11 @@
 ;%0A%09%09
+%09
 %7D%0A%09%7D%0A
-%0A
 %7D

use exponential backoff when there is a exception during cloning