package tasks.bench;

import java.io.File;
import java.util.concurrent.TimeoutException;

import data.RepoInfo;
import main.Config;
import tasks.util.Exec;

/**
 * Clones a GitHub repository, to the specified version
 * 
 * @author Lisa Nguyen Quang Do
 */

public class Download {

	private final File projectDir;
	private final RepoInfo repo;

	public Download(File projectDir, RepoInfo repo) {
		this.projectDir = projectDir;
		this.repo = repo;
	}

	public void downloadSource() {
		System.out.println("Downloading project: " + repo.url);
		try {
			boolean res = Exec.exec("cmd /c git clone " + repo.url, true, true, Config.downloadTimeout, projectDir);
			if (!res) {
				System.out.println("Error in cloning");
				return;
			}
			System.out.println("Cloning complete");
		} catch (TimeoutException e) {
			System.out.println("Cloning complete with timeout");
		}

		System.out.println("Checking out " + repo.sha);
		try {
			boolean res = Exec.exec("cmd /c git checkout " + repo.sha, true, true, Config.downloadTimeout, projectDir);
			if (!res) {
				System.out.println("Error in checkout");
				return;
			}
			System.out.println("Checkout complete");
		} catch (TimeoutException e) {
			System.out.println("Checkout complete with timeout");
		}
	}
}
