Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b00c0cb7224e620e08f63d71f6bdb282a210aaf0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.core.synchronize;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData;
import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet;
import org.eclipse.egit.core.test.GitTestCase;
import org.eclipse.egit.core.test.TestRepository;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.variants.IResourceVariant;
import org.eclipse.team.core.variants.IResourceVariantTree;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class GitResourceVariantTreeSubscriberTest2 extends GitTestCase {

	private Repository repo;

	private IProject iProject;

	private TestRepository testRepo;

	@Before
	public void setUp() throws Exception {
		super.setUp();
		iProject = project.getProject();
		testRepo = new TestRepository(gitDir);
		testRepo.connect(iProject);
		repo = RepositoryMapping.getMapping(iProject).getRepository();
	}

	@After
	public void clearGitResources() throws Exception {
		testRepo.disconnect(iProject);
		testRepo.dispose();
		repo = null;
		super.tearDown();
	}

	/**
	 * Both source and destination branches has some different commits but they
	 * has also common ancestor. This situation is described more detailed in
	 * bug #317934
	 *
	 * This test passes when it is run as a stand alone test case, but it fails
	 * when it is run as part of test suite. It throws NPE when it try's to
	 * checkout master branch.
	 *
	 * @throws Exception
	 */
	@Test
	public void shouldReturnCommonAncestorAsBase() throws Exception {
		// when
		String fileName = "Main.java";
		File file = testRepo.createFile(iProject, fileName);
		RevCommit commit = testRepo.appendContentAndCommit(iProject, file,
				"class Main {}", "initial commit");
		IFile mainJava = testRepo.getIFile(iProject, file);
		// this should be our common ancestor
		ObjectId fileId = findFileId(commit, mainJava);

		testRepo.createAndCheckoutBranch(Constants.HEAD, Constants.R_HEADS
				+ "test");
		testRepo.appendContentAndCommit(iProject, file, "// test 1",
				"second commit");

		testRepo.checkoutBranch(Constants.R_HEADS + Constants.MASTER);
		testRepo.appendContentAndCommit(iProject, file, "// test 2",
				"third commit");

		// given
		GitResourceVariantTreeSubscriber grvts = createGitResourceVariantTreeSubscriber(
				Constants.HEAD, Constants.R_HEADS + "test");
		grvts.getBaseTree();
		IResourceVariantTree baseTree = grvts.getBaseTree();

		// then
		IResourceVariant actual = commonAssertionsForBaseTree(baseTree,
				mainJava);
		assertEquals(fileId.getName(), actual.getContentIdentifier());
	}

	private GitResourceVariantTreeSubscriber createGitResourceVariantTreeSubscriber(
			String src, String dst) throws IOException {
		GitSynchronizeData gsd = new GitSynchronizeData(repo, src, dst, false);
		GitSynchronizeDataSet gsds = new GitSynchronizeDataSet(gsd);
		new GitResourceVariantTreeSubscriber(gsds);
		return new GitResourceVariantTreeSubscriber(gsds);
	}

	private ObjectId findFileId(RevCommit commit, IFile mainJava)
			throws Exception {
		TreeWalk tw = new TreeWalk(repo);
		tw.reset();
		tw.setRecursive(true);
		String path = Repository.stripWorkDir(repo.getWorkTree(), mainJava
				.getLocation().toFile());
		tw.setFilter(PathFilter.create(path));
		int nth = tw.addTree(commit.getTree());
		tw.next();

		return tw.getObjectId(nth);
	}

	private IResourceVariant commonAssertionsForBaseTree(
			IResourceVariantTree baseTree, IResource resource)
			throws TeamException {
		assertNotNull(baseTree);
		assertTrue(baseTree instanceof GitBaseResourceVariantTree);
		IResourceVariant resourceVariant = baseTree
				.getResourceVariant(resource);
		assertNotNull(resourceVariant);
		assertTrue(resourceVariant instanceof GitResourceVariant);
		return resourceVariant;
	}

}

Back to the top