package boa.datagen.forges.github;

import java.io.File;
import java.io.IOException;
import java.util.Random;

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;
		int low = 1;
		int high = 1000;
		Random r = new Random();
		int pow = 1;

		java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
		boolean success = false;
		int trys = 0;
	//	while (success) {
			try {
				success = true;
				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) {
				/*
				if (e.getMessage().contains("Authentication is required but no CredentialsProvider")) {
					continue;
				}
				if (trys < 5) {
					try {
						pow *= 2;
						Thread.sleep(((int) Math.round(pow * 1000) + (r.nextInt(high - low) + low)));
						success = false;
					} catch (InterruptedException ie) {
						// ie.printStackTrace();
					}
				} */
				// e.printStackTrace();
			} finally {

				if (result != null && result.getRepository() != null)
					result.getRepository().close();
			}
		}
//	}
}
@@ -786,84 +786,8 @@
 ull;
-%0A%09%09int low = 1;%0A%09%09int high = 1000;%0A%09%09Random r = new Random();%0A%09%09int pow = 1;
 %0A%0A%09%09
@@ -866,101 +866,14 @@
 ;%0A%09%09
-boolean success = false;%0A%09%09int trys = 0;%0A%09//%09while (success) %7B%0A%09%09%09try %7B%0A%09%09%09%09success = true;%0A%09
+try %7B%0A
 %09%09%09r
@@ -965,17 +965,16 @@
 l();%0A%09%09%09
-%09
 // Note:
@@ -1028,17 +1028,16 @@
 y which%0A
-%09
 %09%09%09// ne
@@ -1043,17 +1043,16 @@
 eeds%0A%09%09%09
-%09
 // to be
@@ -1090,17 +1090,16 @@
 aks!%0A%09%09%09
-%09
 // worka
@@ -1108,17 +1108,16 @@
 und for%0A
-%09
 %09%09%09// ht
@@ -1170,17 +1170,16 @@
 4093%0A%09%09%09
-%09
 result.g
@@ -1204,17 +1204,16 @@
 se();%0A%09%09
-%09
 %7D catch 
@@ -1235,374 +1235,55 @@
 %0A%09%09%09
-%09/*%0A%09%09%09%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(
+System.err.println(%22Error cloning %22 + url
 );%0A%09%09%09
-%09
-%09%7D%0A%09%09%09%09%7D */%0A%09%09%09%09// 
 e.pr
@@ -1301,17 +1301,16 @@
 ce();%0A%09%09
-%09
 %7D finall
@@ -1313,21 +1313,19 @@
 nally %7B%0A
-%0A
 %09%09%09
-%09
 if (resu
@@ -1374,17 +1374,16 @@
 ll)%0A%09%09%09%09
-%09
 result.g
@@ -1410,19 +1410,12 @@
 ();%0A
-%09%09%09%7D%0A
 %09%09%7D%0A
-//
 %09%7D%0A%7D

revert changes and add logging code to clone repos