Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0d22fdfc573953219d0e339d7e29d22a861376de (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
 * Copyright (C) 2012, 2013 Robin Stocker <robin@nibor.org> and others.
 *
 * 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.project;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.test.GitTestCase;
import org.eclipse.egit.core.test.TestRepository;
import org.eclipse.jgit.lib.Repository;
import org.junit.Before;
import org.junit.Test;

public class RepositoryMappingTest extends GitTestCase {

	private Repository repository;

	@Before
	@Override
	public void setUp() throws Exception {
		super.setUp();

		TestRepository testRepo = new TestRepository(gitDir);
		testRepo.connect(project.project);
		repository = testRepo.getRepository();
	}

	@Test
	public void shouldReturnMappingForResourceInProject() throws Exception {
		IFile file = project.createFile("inproject.txt", new byte[] {});

		RepositoryMapping mapping = RepositoryMapping.getMapping(file);

		assertNotNull(mapping);
		assertEquals(repository, mapping.getRepository());
		assertEquals(project.getProject(), mapping.getContainer());
		assertEquals(project.getProject().getName() + "/inproject.txt", mapping.getRepoRelativePath(file));
	}

	@Test
	public void shouldNotReturnMappingForResourceOutsideOfProject() {
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IPath filePath = getWorkTreePath().append("outside.txt");
		IFile file = root.getFile(filePath);
		assertFalse(file.exists());

		RepositoryMapping mapping = RepositoryMapping.getMapping(file);

		assertNull(mapping);
	}

	@Test
	public void shouldReturnMappingForPathOutsideOfProject() {
		IPath filePath = getWorkTreePath().append("outside.txt");

		RepositoryMapping mapping = RepositoryMapping.getMapping(filePath);

		assertNotNull(mapping);
		assertEquals(repository, mapping.getRepository());
		assertEquals("outside.txt", mapping.getRepoRelativePath(filePath));
	}

	@Test
	public void shouldReturnMappingWhenPathIsRepository() {
		IPath workTreePath = getWorkTreePath();

		RepositoryMapping mapping = RepositoryMapping.getMapping(workTreePath);

		assertNotNull(mapping);
		assertEquals(repository, mapping.getRepository());
		assertEquals("", mapping.getRepoRelativePath(workTreePath));
	}

	@Test
	public void shouldNotReturnMappingWhenPathIsOutsideRepository() {
		IPath workTreePath = getWorkTreePath();

		assertNull(RepositoryMapping
				.getMapping(new Path("D:/some/made/up/path")));
		assertNull(RepositoryMapping.getMapping(new Path("/some/made/up/path")));
		assertNull(RepositoryMapping.getMapping(new Path(
				"/thereshouldnever/be/something/here")));

		if (workTreePath.getDevice() == null)
			assertNull(RepositoryMapping.getMapping(workTreePath
					.setDevice("C:")));
	}

	@Test
	public void shouldFindRepositoryMappingForRepository() {
		RepositoryMapping mapping = RepositoryMapping.findRepositoryMapping(repository);

		assertNotNull(mapping);
		assertEquals(repository, mapping.getRepository());
	}

	@Test
	public void shouldResolveRelativePathRelativeToContainer() {
		IPath projectPath = project.getProject().getLocation();
		RepositoryMapping mapping = RepositoryMapping
				.create(project.getProject(), new File(".git"));
		assertEquals(projectPath.append(".git"),
				mapping.getGitDirAbsolutePath());
	}

	/**
	 * Tests that a {@link RepositoryMapping} internally uses a relative path if
	 * at all possible.
	 */
	@Test
	public void shouldResolveAsRelativePath() {
		IProject proj = project.getProject();
		IPath projectPath = proj.getLocation()
				.removeTrailingSeparator();
		String gitHereTest = ".git";
		String gitTest = "../../.git";
		String gitSubdirTest = "foobar/.git";
		String gitSubmoduleTest = "../../.git/modules/submodule";
		// Construct an absolute path different from the project location:
		// should be preserved. upToSegment ensures we don't loose the root
		// component.
		String gitAbsolute = projectPath.uptoSegment(0)
				.append(projectPath.segment(0) + "fake").append(".git")
				.toOSString();
		String parents = "";
		while (projectPath.segmentCount() > 2) {
			String pathString = projectPath.toOSString();
			assertRepoMappingPath(proj, pathString, gitHereTest, parents);
			assertRepoMappingPath(proj, pathString, gitTest, parents);
			assertRepoMappingPath(proj, pathString, gitSubdirTest, parents);
			assertRepoMappingPath(proj, pathString, gitSubmoduleTest, parents);
			assertRepoMappingPath(proj, pathString, gitAbsolute, "");
			projectPath = projectPath.removeLastSegments(1);
			parents += "../";
		}
	}

	private void assertRepoMappingPath(IProject testProject, String path,
			String testDir, String parents) {
		String testDirOS = testDir.replace('/', File.separatorChar);
		File testFile = new File(testDirOS);
		if (!testFile.isAbsolute()) {
			testFile = new File(new File(path), testDirOS);
		}
		RepositoryMapping mapping = RepositoryMapping.create(testProject,
				testFile);
		assertEquals(parents + testDir,
				mapping.getGitDirPath().toPortableString());
	}

	private IPath getWorkTreePath() {
		return new Path(repository.getWorkTree().getAbsolutePath());
	}
}

Back to the top