View Javadoc
1   /*
2    * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
3    * and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.internal.storage.file;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertFalse;
16  import static org.junit.Assert.assertTrue;
17  import static org.junit.Assert.fail;
18  
19  import java.io.File;
20  import java.io.IOException;
21  
22  import org.eclipse.jgit.errors.ConfigInvalidException;
23  import org.eclipse.jgit.errors.NoWorkTreeException;
24  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
25  import org.eclipse.jgit.lib.ConfigConstants;
26  import org.eclipse.jgit.lib.Constants;
27  import org.eclipse.jgit.lib.Repository;
28  import org.eclipse.jgit.storage.file.FileBasedConfig;
29  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
30  import org.eclipse.jgit.util.FS;
31  import org.eclipse.jgit.util.FileUtils;
32  import org.junit.Test;
33  
34  /**
35   * Tests for setting up the working directory when creating a Repository
36   */
37  public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
38  
39  	@Test
40  	public void testIsBare_CreateRepositoryFromArbitraryGitDir()
41  			throws Exception {
42  		File gitDir = getFile("workdir");
43  		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
44  		assertTrue(repo.isBare());
45  	}
46  
47  	@Test
48  	public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
49  		File gitDir = getFile("workdir", Constants.DOT_GIT);
50  		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
51  		assertFalse(repo.isBare());
52  		assertWorkdirPath(repo, "workdir");
53  		assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
54  	}
55  
56  	@Test
57  	public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
58  			throws Exception {
59  		File gitDir = getFile("workdir", Constants.DOT_GIT);
60  		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
61  		String workdir = repo.getWorkTree().getName();
62  		assertEquals(workdir, "workdir");
63  	}
64  
65  	@Test
66  	public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
67  		File workdir = getFile("workdir", "repo");
68  		Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
69  				.build();
70  		assertFalse(repo.isBare());
71  		assertWorkdirPath(repo, "workdir", "repo");
72  		assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
73  	}
74  
75  	@Test
76  	public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
77  			throws Exception {
78  		File workdir = getFile("workdir", "repo");
79  		Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
80  				.build();
81  		assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
82  	}
83  
84  	@Test
85  	public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
86  			throws Exception {
87  		File gitDir = getFile("workdir", "repoWithConfig");
88  		File workTree = getFile("workdir", "treeRoot");
89  		setWorkTree(gitDir, workTree);
90  		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
91  		assertFalse(repo.isBare());
92  		assertWorkdirPath(repo, "workdir", "treeRoot");
93  		assertGitdirPath(repo, "workdir", "repoWithConfig");
94  	}
95  
96  	@Test
97  	public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
98  			throws Exception {
99  		File gitDir = getFile("workdir", "repoWithConfig");
100 		setBare(gitDir, true);
101 		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
102 		assertTrue(repo.isBare());
103 	}
104 
105 	@Test
106 	public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
107 			throws Exception {
108 		File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
109 		setBare(gitDir, false);
110 		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
111 		assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
112 	}
113 
114 	@Test
115 	public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
116 			throws Exception {
117 		File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
118 		setBare(gitDir, false);
119 		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
120 		assertFalse(repo.isBare());
121 		assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
122 		assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");
123 	}
124 
125 	@Test
126 	public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
127 		File gitDir = getFile("workdir");
128 		try (Repository repo = new FileRepository(gitDir)) {
129 			repo.getWorkTree();
130 			fail("Expected NoWorkTreeException missing");
131 		} catch (NoWorkTreeException e) {
132 			// expected
133 		}
134 	}
135 
136 	@Test
137 	public void testExceptionThrown_BareRepoGetIndex() throws Exception {
138 		File gitDir = getFile("workdir");
139 		try (Repository repo = new FileRepository(gitDir)) {
140 			repo.readDirCache();
141 			fail("Expected NoWorkTreeException missing");
142 		} catch (NoWorkTreeException e) {
143 			// expected
144 		}
145 	}
146 
147 	@Test
148 	public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
149 		File gitDir = getFile("workdir");
150 		try (Repository repo = new FileRepository(gitDir)) {
151 			repo.getIndexFile();
152 			fail("Expected NoWorkTreeException missing");
153 		} catch (NoWorkTreeException e) {
154 			// expected
155 		}
156 	}
157 
158 	private File getFile(String... pathComponents) throws IOException {
159 		File dir = getTemporaryDirectory();
160 		for (String pathComponent : pathComponents)
161 			dir = new File(dir, pathComponent);
162 		FileUtils.mkdirs(dir, true);
163 		return dir;
164 	}
165 
166 	private void setBare(File gitDir, boolean bare) throws IOException,
167 			ConfigInvalidException {
168 		FileBasedConfig cfg = configFor(gitDir);
169 		cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
170 				ConfigConstants.CONFIG_KEY_BARE, bare);
171 		cfg.save();
172 	}
173 
174 	private void setWorkTree(File gitDir, File workTree)
175 			throws IOException,
176 			ConfigInvalidException {
177 		String path = workTree.getAbsolutePath();
178 		FileBasedConfig cfg = configFor(gitDir);
179 		cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
180 				ConfigConstants.CONFIG_KEY_WORKTREE, path);
181 		cfg.save();
182 	}
183 
184 	private FileBasedConfig configFor(File gitDir) throws IOException,
185 			ConfigInvalidException {
186 		File configPath = new File(gitDir, Constants.CONFIG);
187 		FileBasedConfig cfg = new FileBasedConfig(configPath, FS.DETECTED);
188 		cfg.load();
189 		return cfg;
190 	}
191 
192 	private void assertGitdirPath(Repository repo, String... expected)
193 			throws IOException {
194 		File exp = getFile(expected).getCanonicalFile();
195 		File act = repo.getDirectory().getCanonicalFile();
196 		assertEquals("Wrong Git Directory", exp, act);
197 	}
198 
199 	private void assertWorkdirPath(Repository repo, String... expected)
200 			throws IOException {
201 		File exp = getFile(expected).getCanonicalFile();
202 		File act = repo.getWorkTree().getCanonicalFile();
203 		assertEquals("Wrong working Directory", exp, act);
204 	}
205 }