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 package org.eclipse.jgit.internal.storage.file;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertFalse;
49 import static org.junit.Assert.assertTrue;
50 import static org.junit.Assert.fail;
51
52 import java.io.File;
53 import java.io.IOException;
54
55 import org.eclipse.jgit.errors.ConfigInvalidException;
56 import org.eclipse.jgit.errors.NoWorkTreeException;
57 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
58 import org.eclipse.jgit.lib.ConfigConstants;
59 import org.eclipse.jgit.lib.Constants;
60 import org.eclipse.jgit.lib.Repository;
61 import org.eclipse.jgit.storage.file.FileBasedConfig;
62 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
63 import org.eclipse.jgit.util.FS;
64 import org.eclipse.jgit.util.FileUtils;
65 import org.junit.Test;
66
67
68
69
70 public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
71
72 @Test
73 public void testIsBare_CreateRepositoryFromArbitraryGitDir()
74 throws Exception {
75 File gitDir = getFile("workdir");
76 Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
77 assertTrue(repo.isBare());
78 }
79
80 @Test
81 public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
82 File gitDir = getFile("workdir", Constants.DOT_GIT);
83 Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
84 assertFalse(repo.isBare());
85 assertWorkdirPath(repo, "workdir");
86 assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
87 }
88
89 @Test
90 public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
91 throws Exception {
92 File gitDir = getFile("workdir", Constants.DOT_GIT);
93 Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
94 String workdir = repo.getWorkTree().getName();
95 assertEquals(workdir, "workdir");
96 }
97
98 @Test
99 public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
100 File workdir = getFile("workdir", "repo");
101 Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
102 .build();
103 assertFalse(repo.isBare());
104 assertWorkdirPath(repo, "workdir", "repo");
105 assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
106 }
107
108 @Test
109 public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
110 throws Exception {
111 File workdir = getFile("workdir", "repo");
112 Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
113 .build();
114 assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
115 }
116
117 @Test
118 public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
119 throws Exception {
120 File gitDir = getFile("workdir", "repoWithConfig");
121 File workTree = getFile("workdir", "treeRoot");
122 setWorkTree(gitDir, workTree);
123 Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
124 assertFalse(repo.isBare());
125 assertWorkdirPath(repo, "workdir", "treeRoot");
126 assertGitdirPath(repo, "workdir", "repoWithConfig");
127 }
128
129 @Test
130 public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
131 throws Exception {
132 File gitDir = getFile("workdir", "repoWithConfig");
133 setBare(gitDir, true);
134 Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
135 assertTrue(repo.isBare());
136 }
137
138 @Test
139 public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
140 throws Exception {
141 File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
142 setBare(gitDir, false);
143 Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
144 assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
145 }
146
147 @Test
148 public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
149 throws Exception {
150 File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
151 setBare(gitDir, false);
152 Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
153 assertFalse(repo.isBare());
154 assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
155 assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");
156 }
157
158 @Test
159 public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
160 File gitDir = getFile("workdir");
161 try (Repository repo = new FileRepository(gitDir)) {
162 repo.getWorkTree();
163 fail("Expected NoWorkTreeException missing");
164 } catch (NoWorkTreeException e) {
165
166 }
167 }
168
169 @Test
170 public void testExceptionThrown_BareRepoGetIndex() throws Exception {
171 File gitDir = getFile("workdir");
172 try (Repository repo = new FileRepository(gitDir)) {
173 repo.readDirCache();
174 fail("Expected NoWorkTreeException missing");
175 } catch (NoWorkTreeException e) {
176
177 }
178 }
179
180 @Test
181 public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
182 File gitDir = getFile("workdir");
183 try (Repository repo = new FileRepository(gitDir)) {
184 repo.getIndexFile();
185 fail("Expected NoWorkTreeException missing");
186 } catch (NoWorkTreeException e) {
187
188 }
189 }
190
191 private File getFile(String... pathComponents) throws IOException {
192 File dir = getTemporaryDirectory();
193 for (String pathComponent : pathComponents)
194 dir = new File(dir, pathComponent);
195 FileUtils.mkdirs(dir, true);
196 return dir;
197 }
198
199 private void setBare(File gitDir, boolean bare) throws IOException,
200 ConfigInvalidException {
201 FileBasedConfig cfg = configFor(gitDir);
202 cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
203 ConfigConstants.CONFIG_KEY_BARE, bare);
204 cfg.save();
205 }
206
207 private void setWorkTree(File gitDir, File workTree)
208 throws IOException,
209 ConfigInvalidException {
210 String path = workTree.getAbsolutePath();
211 FileBasedConfig cfg = configFor(gitDir);
212 cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
213 ConfigConstants.CONFIG_KEY_WORKTREE, path);
214 cfg.save();
215 }
216
217 private FileBasedConfig configFor(File gitDir) throws IOException,
218 ConfigInvalidException {
219 File configPath = new File(gitDir, Constants.CONFIG);
220 FileBasedConfig cfg = new FileBasedConfig(configPath, FS.DETECTED);
221 cfg.load();
222 return cfg;
223 }
224
225 private void assertGitdirPath(Repository repo, String... expected)
226 throws IOException {
227 File exp = getFile(expected).getCanonicalFile();
228 File act = repo.getDirectory().getCanonicalFile();
229 assertEquals("Wrong Git Directory", exp, act);
230 }
231
232 private void assertWorkdirPath(Repository repo, String... expected)
233 throws IOException {
234 File exp = getFile(expected).getCanonicalFile();
235 File act = repo.getWorkTree().getCanonicalFile();
236 assertEquals("Wrong working Directory", exp, act);
237 }
238 }