|
|
|
@@ -2,9 +2,12 @@ package git4idea.repo;
|
|
|
|
|
|
|
|
|
|
import com.intellij.openapi.application.PluginPathManager;
|
|
|
|
|
import com.intellij.openapi.util.io.FileUtil;
|
|
|
|
|
import com.intellij.openapi.util.text.StringUtil;
|
|
|
|
|
import com.intellij.openapi.vcs.VcsTestUtil;
|
|
|
|
|
import com.intellij.util.Processor;
|
|
|
|
|
import com.intellij.util.containers.ContainerUtil;
|
|
|
|
|
import com.intellij.vcs.log.impl.HashImpl;
|
|
|
|
|
import git4idea.GitBranch;
|
|
|
|
|
import git4idea.GitLocalBranch;
|
|
|
|
|
import git4idea.branch.GitBranchesCollection;
|
|
|
|
|
import git4idea.test.GitPlatformTest;
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
@@ -12,28 +15,34 @@ import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class GitRepositoryReaderTest extends GitPlatformTest {
|
|
|
|
|
|
|
|
|
|
private File myDataDir;
|
|
|
|
|
private File myTempDir;
|
|
|
|
|
|
|
|
|
|
private GitRepositoryReader myRepositoryReader;
|
|
|
|
|
private File myGitDir;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
|
super.setUp();
|
|
|
|
|
|
|
|
|
|
File myTempDir = new File(myProjectRoot.getPath(), "test");
|
|
|
|
|
myTempDir = new File(myProjectRoot.getPath(), "test");
|
|
|
|
|
myTempDir.mkdir();
|
|
|
|
|
|
|
|
|
|
File pluginRoot = new File(PluginPathManager.getPluginHomePath("git4idea"));
|
|
|
|
|
File dataDir = new File(new File(pluginRoot, "testData"), "repo");
|
|
|
|
|
myDataDir = new File(new File(pluginRoot, "testData"), "repo");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileUtil.copyDir(dataDir, myTempDir);
|
|
|
|
|
private void prepareTest(File testDir) throws IOException {
|
|
|
|
|
FileUtil.copyDir(testDir, myTempDir);
|
|
|
|
|
myGitDir = new File(myTempDir, ".git");
|
|
|
|
|
FileUtil.rename(new File(myTempDir, "dot_git"), myGitDir);
|
|
|
|
|
TestCase.assertTrue(myGitDir.exists());
|
|
|
|
|
myRepositoryReader = new GitRepositoryReader(myGitDir);
|
|
|
|
|
|
|
|
|
|
myLocalBranches = readBranches(true);
|
|
|
|
|
myRemoteBranches = readBranches(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@@ -41,135 +50,97 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
|
|
|
|
|
super.tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testHEAD() {
|
|
|
|
|
TestCase.assertEquals("0e1d130689bc52f140c5c374aa9cc2b8916c0ad7", myRepositoryReader.readCurrentRevision());
|
|
|
|
|
private void doTest(@NotNull ResultConsumer test) throws Exception {
|
|
|
|
|
for (File dir : myDataDir.listFiles()) {
|
|
|
|
|
if (!dir.isDirectory()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
prepareTest(dir);
|
|
|
|
|
test.consume(dir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testCurrentBranch() {
|
|
|
|
|
assertBranch(myRepositoryReader.readCurrentBranch(), new GitTestBranch("master", "0e1d130689bc52f140c5c374aa9cc2b8916c0ad7"));
|
|
|
|
|
@NotNull
|
|
|
|
|
private static String readHead(@NotNull File dir) throws IOException {
|
|
|
|
|
return FileUtil.loadFile(new File(dir, "head.txt")).trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testBranches() {
|
|
|
|
|
Collection<GitRemote> remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes();
|
|
|
|
|
GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes);
|
|
|
|
|
GitBranch currentBranch = myRepositoryReader.readCurrentBranch();
|
|
|
|
|
Collection<? extends GitBranch> localBranches = branchesCollection.getLocalBranches();
|
|
|
|
|
Collection<? extends GitBranch> remoteBranches = branchesCollection.getRemoteBranches();
|
|
|
|
|
|
|
|
|
|
assertBranch(currentBranch, new GitTestBranch("master", "0e1d130689bc52f140c5c374aa9cc2b8916c0ad7"));
|
|
|
|
|
assertBranches(localBranches, myLocalBranches);
|
|
|
|
|
assertBranches(remoteBranches, myRemoteBranches);
|
|
|
|
|
@NotNull
|
|
|
|
|
private static GitLocalBranch readCurrentBranch(@NotNull File resultDir) throws IOException {
|
|
|
|
|
String branch = FileUtil.loadFile(new File(resultDir, "current-branch.txt")).trim();
|
|
|
|
|
return readBranchFromLine(branch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void assertBranches(Collection<? extends GitBranch> actualBranches, Collection<GitTestBranch> expectedBranches) {
|
|
|
|
|
VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker<GitBranch, GitTestBranch>() {
|
|
|
|
|
@NotNull
|
|
|
|
|
private static GitLocalBranch readBranchFromLine(@NotNull String branch) {
|
|
|
|
|
List<String> branchAndHash = StringUtil.split(branch, " ");
|
|
|
|
|
return new GitLocalBranch(branchAndHash.get(1), HashImpl.build(branchAndHash.get(0)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testHEAD() throws Exception {
|
|
|
|
|
doTest(new ResultConsumer() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean areEqual(@NotNull GitBranch actual, @NotNull GitTestBranch expected) {
|
|
|
|
|
public void consume(@NotNull File resultDir) throws Exception {
|
|
|
|
|
assertEquals("HEAD is incorrect", readHead(resultDir), myRepositoryReader.readCurrentRevision());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testCurrentBranch() throws Exception {
|
|
|
|
|
doTest(new ResultConsumer() {
|
|
|
|
|
@Override
|
|
|
|
|
public void consume(@NotNull File resultDir) throws Exception {
|
|
|
|
|
assertEqualBranches(readCurrentBranch(resultDir), myRepositoryReader.readCurrentBranch());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void assertEqualBranches(@NotNull GitLocalBranch expected, @NotNull GitLocalBranch actual) {
|
|
|
|
|
assertEquals(expected.getName(), actual.getName());
|
|
|
|
|
assertEquals(expected.getHash(), actual.getHash());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testBranches() throws Exception {
|
|
|
|
|
doTest(new ResultConsumer() {
|
|
|
|
|
@Override
|
|
|
|
|
public void consume(@NotNull File resultDir) throws Exception {
|
|
|
|
|
Collection<GitRemote> remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes();
|
|
|
|
|
GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes);
|
|
|
|
|
GitLocalBranch currentBranch = myRepositoryReader.readCurrentBranch();
|
|
|
|
|
Collection<? extends GitBranch> localBranches = branchesCollection.getLocalBranches();
|
|
|
|
|
Collection<? extends GitBranch> remoteBranches = branchesCollection.getRemoteBranches();
|
|
|
|
|
|
|
|
|
|
assertEqualBranches(readCurrentBranch(resultDir), currentBranch);
|
|
|
|
|
assertBranches(localBranches, readBranches(resultDir, true));
|
|
|
|
|
assertBranches(remoteBranches, readBranches(resultDir, false));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void assertBranches(Collection<? extends GitBranch> actualBranches, Collection<? extends GitBranch> expectedBranches) {
|
|
|
|
|
VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker<GitBranch, GitBranch>() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean areEqual(@NotNull GitBranch actual, @NotNull GitBranch expected) {
|
|
|
|
|
return branchesAreEqual(actual, expected);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Collection<GitTestBranch> readBranches(boolean local) throws IOException {
|
|
|
|
|
final Collection<GitTestBranch> branches = new ArrayList<GitTestBranch>();
|
|
|
|
|
final File refsHeads = new File(new File(myGitDir, "refs"), local ? "heads" : "remotes");
|
|
|
|
|
FileUtil.processFilesRecursively(refsHeads, new Processor<File>() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean process(File file) {
|
|
|
|
|
if (FileUtil.filesEqual(file, refsHeads)) {// don't process the root
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (file.isDirectory()) {// don't process dirs
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String relativePath = FileUtil.getRelativePath(refsHeads, file);
|
|
|
|
|
if (relativePath == null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String name = FileUtil.toSystemIndependentName(relativePath);
|
|
|
|
|
GitTestBranch branch = null;
|
|
|
|
|
try {
|
|
|
|
|
branch = new GitTestBranch(name, FileUtil.loadFile(file));
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
TestCase.fail(e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!branches.contains(branch)) {
|
|
|
|
|
branches.add(branch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// read from packed-refs, these have less priority, so the won't overwrite hashes from branch files
|
|
|
|
|
String packedRefs = FileUtil.loadFile(new File(myGitDir, "packed-refs"));
|
|
|
|
|
for (String ref : packedRefs.split("\n")) {
|
|
|
|
|
String[] refAndName = ref.split(" ");
|
|
|
|
|
String name = refAndName[1];
|
|
|
|
|
String prefix = local ? "refs/heads/" : "refs/remotes/";
|
|
|
|
|
if (name.startsWith(prefix)) {
|
|
|
|
|
GitTestBranch branch = new GitTestBranch(name.substring(prefix.length()), refAndName[0]);
|
|
|
|
|
if (!branches.contains(branch)) {
|
|
|
|
|
branches.add(branch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@NotNull
|
|
|
|
|
private static Collection<GitBranch> readBranches(@NotNull File resultDir, boolean local) throws IOException {
|
|
|
|
|
String content = FileUtil.loadFile(new File(resultDir, local ? "local-branches.txt" : "remote-branches.txt"));
|
|
|
|
|
Collection<GitBranch> branches = ContainerUtil.newArrayList();
|
|
|
|
|
for (String line : StringUtil.splitByLines(content)) {
|
|
|
|
|
branches.add(readBranchFromLine(line));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return branches;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void assertBranch(GitBranch actual, GitTestBranch expected) {
|
|
|
|
|
TestCase.assertTrue(String.format("Branches are not equal. Actual: %s:%sExpected: %s", actual.getName(), actual.getHash(), expected),
|
|
|
|
|
branchesAreEqual(actual, expected));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean branchesAreEqual(GitBranch actual, GitTestBranch expected) {
|
|
|
|
|
private static boolean branchesAreEqual(GitBranch actual, GitBranch expected) {
|
|
|
|
|
return actual.getName().equals(expected.getName()) && actual.getHash().equals(expected.getHash());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GitRepositoryReader myRepositoryReader;
|
|
|
|
|
private File myGitDir;
|
|
|
|
|
private Collection<GitTestBranch> myLocalBranches;
|
|
|
|
|
private Collection<GitTestBranch> myRemoteBranches;
|
|
|
|
|
|
|
|
|
|
private static class GitTestBranch {
|
|
|
|
|
private GitTestBranch(String name, String hash) {
|
|
|
|
|
myName = name.trim();
|
|
|
|
|
myHash = hash.trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
|
return myName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getHash() {
|
|
|
|
|
return myHash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return myName + ":" + myHash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
GitTestBranch branch = (GitTestBranch)o;
|
|
|
|
|
if (!myName.equals(branch.myName)) return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
return myName.hashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final String myName;
|
|
|
|
|
private final String myHash;
|
|
|
|
|
private abstract static class ResultConsumer {
|
|
|
|
|
public abstract void consume(@NotNull File resultDir) throws Exception;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|