package hu.sebcsaba.gitrelocate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class MockedGitRunner implements GitRunner {
	
	private static final String ZERO = "0000000000000000000000000000000000000000";
	
	private final GitSubGraph graph;
	private String head;
	
	public MockedGitRunner(int[] branches, int[] tags, int[][] parents) {
		this.graph = new GitSubGraph();
		for (int i=0; i<branches.length; ++i) {
			this.graph.getBranches().put("B"+i, intToCommitID(branches[i]));
		}
		for (int i=0; i<tags.length; ++i) {
			this.graph.getTags().put("T"+i, intToCommitID(tags[i]));
		}
		for (int i=0; i<parents.length; ++i) {
			List<CommitID> parentIDs = new ArrayList<CommitID>();
			for (int j=0; j<parents[i].length; ++j) {
				parentIDs.add(intToCommitID(parents[i][j]));
			}
			Commit c = new Commit(intToCommitID(i), parentIDs);
			this.graph.getCommits().add(c );
		}
		if (branches.length>0) {
			head = "B0";
		}
	}

	public CommitID getCommitId(String anyGitCommitRef) {
		if (graph.getBranches().containsKey(anyGitCommitRef)) {
			return graph.getBranches().get(anyGitCommitRef);
		} else if (graph.getTags().containsKey(anyGitCommitRef)) {
			return graph.getTags().get(anyGitCommitRef);
		} else {
			throw new IllegalArgumentException("Unable to parse commit id for "+anyGitCommitRef);
		}
	}

	public List<CommitID> getCommitParentIds(CommitID commitId) {
		return graph.getCommit(commitId).getParents();
	}

	public Collection<String> getTagNames() {
		return new ArrayList<String>(this.graph.getTags().keySet());
	}

	public Collection<String> getBranchNames() {
		return new ArrayList<String>(this.graph.getBranches().keySet());
	}

	public static CommitID intToCommitID(int cid) {
		String cids = Integer.toString(cid);
		return new CommitID(ZERO.substring(0, ZERO.length()-cids.length()) + cids);
	}

	public String getActualHeadName() {
		return head;
	}
	
	public void createBranch(String branchName, CommitID commitId) {
		graph.getBranches().put(branchName, commitId);
	}
	
	public void removeBranch(String branchName) {
		graph.getBranches().remove(branchName);
	}

	public void checkOut(String branchName) {
		head = branchName;
	}

	public void createTag(String tagName, CommitID commitId) {
		graph.getTags().put(tagName, commitId);
	}
	
	public void removeTag(String tagName) {
		graph.getTags().remove(tagName);
	}

	public CommitID cherryPick(CommitID commitId) {
		CommitID newId = intToCommitID(graph.getCommits().size());
		List<CommitID> parents = Arrays.asList(getCommitId(head));
		Commit c = new Commit(newId, parents);
		graph.getCommits().add(c);
		if (isDetachedHead()) {
			head = newId.toString();
		} else {
			graph.getBranches().put(head, newId);
		}
		return newId;
	}
	
	private boolean isDetachedHead() {
		return !graph.getBranches().containsKey(head);
	}

}

@@ -126,19 +126,120 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Queue;%0Aimport java.util.Se
 t;%0A%0Apubl
@@ -456,62 +456,441 @@
 ner(
-int%5B%5D branches, int%5B%5D tags, int%5B%5D%5B%5D parents) %7B%0A%09%09this.
+GitSubGraph graph) %7B%0A%09%09this.graph = graph;%0A%09%09if (!this.graph.getBranches().isEmpty()) %7B%0A%09%09%09head = this.graph.getBranches().keySet().iterator().next();%0A%09%09%7D%0A%09%7D%0A%0A%09public MockedGitRunner(int%5B%5D branches, int%5B%5D tags, int%5B%5D%5B%5D parents) %7B%0A%09%09this.graph = buildGraph(branches, tags, parents);%0A%09%09if (branches.length%3E0) %7B%0A%09%09%09head = %22B0%22;%0A%09%09%7D%0A%09%7D%0A%0A%09private static GitSubGraph buildGraph(int%5B%5D branches, int%5B%5D tags, int%5B%5D%5B%5D parents) %7B%0A%09%09GitSubGraph 
 grap
@@ -949,37 +949,32 @@
 ngth; ++i) %7B%0A%09%09%09
-this.
 graph.getBranche
@@ -1058,29 +1058,24 @@
 ; ++i) %7B%0A%09%09%09
-this.
 graph.getTag
@@ -1373,21 +1373,16 @@
 Ds);%0A%09%09%09
-this.
 graph.ge
@@ -1397,17 +1397,16 @@
 ().add(c
- 
 );%0A%09%09%7D%0A%09
@@ -1410,52 +1410,21 @@
 %7D%0A%09%09
-if (branches.length%3E0) %7B%0A%09%09%09head = %22B0%22;%0A%09%09%7D
+return graph;
 %0A%09%7D%0A

add GitSubGraph-constructor to MockedGitRunner