<commit_before>package hu.sebcsaba.gitrelocate;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class GitRelocate {
	
	private final GitRunner git;
	private final GraphBuilder builder;
	
	public GitRelocate(GitRunner git, GraphBuilder builder) {
		this.git = git;
		this.builder = builder;
	}

	public void relocate(CommitID cutPoint, CommitID newBase) {
		GitSubGraph fullTree = builder.buildFullTree();
		GitSubGraph subTree = builder.getSubTree(fullTree, cutPoint);
		String previousHead = git.getActualHeadName();
		relocate(subTree, cutPoint, newBase);
		git.checkOut(previousHead);
	}

	private void relocate(GitSubGraph subTree, CommitID cutPoint, CommitID newBase) {
		Queue<Commit> commitsToClone = new LinkedList<Commit>();
		commitsToClone.addAll(subTree.getCommits());
		
		Map<CommitID, CommitID> cloneMap = new HashMap<CommitID, CommitID>();
		cloneMap.put(cutPoint, newBase);
		
		while (!commitsToClone.isEmpty()) {
			Commit commitToClone = commitsToClone.poll();
			if (hasAllParentsCloned(subTree, commitToClone, cloneMap)) {
				clone(commitToClone, cloneMap);
			} else {
				commitsToClone.add(commitToClone);
			}
		}
		for (String branch : subTree.getBranches().keySet()) {
			CommitID oldId = subTree.getBranches().get(branch);
			if (cloneMap.containsKey(oldId)) {
				git.moveBranch(branch, cloneMap.get(oldId));
			}
		}
	}

	private boolean hasAllParentsCloned(GitSubGraph treeToClone, Commit commit, Map<CommitID, CommitID> cloneMap) {
		for (CommitID parentID : commit.getParents()) {
			if (treeToClone.hasCommit(parentID) && !cloneMap.containsKey(parentID)) {
				return false;
			}
		}
		return true;
	}
<commit_msg>fix relocate: prevent cloning cutPoint commit<commit_after>package hu.sebcsaba.gitrelocate;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class GitRelocate {
	
	private final GitRunner git;
	private final GraphBuilder builder;
	
	public GitRelocate(GitRunner git, GraphBuilder builder) {
		this.git = git;
		this.builder = builder;
	}

	public void relocate(CommitID cutPoint, CommitID newBase) {
		GitSubGraph fullTree = builder.buildFullTree();
		GitSubGraph subTree = builder.getSubTree(fullTree, cutPoint);
		String previousHead = git.getActualHeadName();
		relocate(subTree, cutPoint, newBase);
		git.checkOut(previousHead);
	}

	private void relocate(GitSubGraph subTree, CommitID cutPoint, CommitID newBase) {
		Queue<Commit> commitsToClone = new LinkedList<Commit>();
		commitsToClone.addAll(subTree.getCommits());
		
		Map<CommitID, CommitID> cloneMap = new HashMap<CommitID, CommitID>();
		cloneMap.put(cutPoint, newBase);
		
		while (!commitsToClone.isEmpty()) {
			Commit commitToClone = commitsToClone.poll();
			if (commitToClone.getId().equals(cutPoint)) {
				// this is in the queue, but not need to clone
			} else if (hasAllParentsCloned(subTree, commitToClone, cloneMap)) {
				clone(commitToClone, cloneMap);
			} else {
				commitsToClone.add(commitToClone);
			}
		}
		for (String branch : subTree.getBranches().keySet()) {
			CommitID oldId = subTree.getBranches().get(branch);
			if (cloneMap.containsKey(oldId)) {
				git.moveBranch(branch, cloneMap.get(oldId));
			}
		}
	}

	private boolean hasAllParentsCloned(GitSubGraph treeToClone, Commit commit, Map<CommitID, CommitID> cloneMap) {
		for (CommitID parentID : commit.getParents()) {
			if (treeToClone.hasCommit(parentID) && !cloneMap.containsKey(parentID)) {
				return false;
			}
		}
		return true;
	}

{'fraction_non_alphanumeric': 0.11338759472354758, 'fraction_numerical': 0.0, 'mean_word_length': 4.890909090909091, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4225918', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1188, 'n_words': 248}