package hu.sebcsaba.gitrelocate;

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

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class IntegrationTest {

	private File baseDir;
	private CmdLineTool tool;
	private GitCmdLineRunner git;
	
	@Before
	public void initializeGitFolder() throws IOException {
		baseDir = new File(FileUtils.getTempDirectory(), "git-relocate-integration-test-data");
		FileUtils.deleteDirectory(baseDir);
		baseDir.mkdirs();
		tool = new CmdLineTool(baseDir);
		git = new GitCmdLineRunner(tool);
	}

	@Test
	public void testRealGitImpl() throws IOException {
		tool.run("git", "init");
		doCommit(0);
		git.createBranch("B0");
		git.checkOut("B0");
		doCommit(1);
		git.createTag("T1");
		doCommit(2);
		git.createBranch("B1");
		doCommit(3);
		doCommit(5);
		doCommit(7); // TODO not merge yet!
		git.checkOut("B1");
		doCommit(4);
		git.createTag("T0");
		doCommit(6);
		git.checkOut("master");
		doCommit(1); // commit-8
		tool.run("git", "commit", "--amend", "-m", "commit-8");
		git.createTag("T2");
		git.checkOut("B0");
		git.removeBranch("master");
		
		GitRelocate relocator = new GitRelocate(git, new GraphBuilder(git));
		CommitID cutPoint = git.getCommitId("T1");
		CommitID newBase = git.getCommitId("T2");
		relocator.relocate(cutPoint,newBase);
	}
	
	@After
	public void destroyGitFolder() throws IOException {
		FileUtils.deleteDirectory(baseDir);
	}

	private CommitID doCommit(int i) throws IOException {
		FileUtils.write(new File(baseDir, "data.txt"), Integer.toString(i));
		tool.run("git", "add", "data.txt");
		tool.run("git", "commit", "-m", "commit-"+i);
		return git.getCommitId("HEAD");
	}

}

@@ -649,19 +649,41 @@
 xception
+, InterruptedException
  %7B%0A
-
 %09%09tool.r
@@ -1018,32 +1018,127 @@
 ;%0A%09%09
-doCommit(1); // commit-8
+%0A%09%09// have to sleep to prevent reusing the #1 commit for the next%0A%09%09Thread.sleep(1000);%0A%09%09// commit-8%0A%09%09doCommit(1);%0A%09%09
 %0A%09%09t
@@ -1587,24 +1587,20 @@
 private 
-CommitID
+void
  doCommi
@@ -1738,16 +1738,16 @@
 .txt%22);%0A
+
 %09%09tool.r
@@ -1790,42 +1790,8 @@
 i);%0A
-%09%09return git.getCommitId(%22HEAD%22);%0A
 %09%7D%0A%0A

add sleep to prevent reusing the #1 commit in integration test