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  package org.eclipse.jgit.internal.storage.file;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNotNull;
48  import static org.junit.Assert.assertTrue;
49  import static org.junit.Assert.fail;
50  
51  import java.io.File;
52  import java.io.FileWriter;
53  import java.io.IOException;
54  
55  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
56  import org.eclipse.jgit.lib.ConfigConstants;
57  import org.eclipse.jgit.lib.Constants;
58  import org.eclipse.jgit.lib.Repository;
59  import org.eclipse.jgit.lib.StoredConfig;
60  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
61  import org.eclipse.jgit.util.FileUtils;
62  import org.junit.Test;
63  
64  public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
65  	@Test
66  	public void testShouldAutomagicallyDetectGitDirectory() throws Exception {
67  		Repository r = createWorkRepository();
68  		File d = new File(r.getDirectory(), "sub-dir");
69  		FileUtils.mkdir(d);
70  
71  		assertEquals(r.getDirectory(), new FileRepositoryBuilder()
72  				.findGitDir(d).getGitDir());
73  	}
74  
75  	@Test
76  	public void emptyRepositoryFormatVersion() throws Exception {
77  		Repository r = createWorkRepository();
78  		StoredConfig config = r.getConfig();
79  		config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
80  				ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "");
81  		config.save();
82  
83  		try (FileRepository repo = new FileRepository(r.getDirectory())) {
84  			
85  		}
86  	}
87  
88  	@Test
89  	public void invalidRepositoryFormatVersion() throws Exception {
90  		Repository r = createWorkRepository();
91  		StoredConfig config = r.getConfig();
92  		config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
93  				ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber");
94  		config.save();
95  
96  		try (FileRepository repo = new FileRepository(r.getDirectory())) {
97  			fail("IllegalArgumentException not thrown");
98  		} catch (IllegalArgumentException e) {
99  			assertNotNull(e.getMessage());
100 		}
101 	}
102 
103 	@Test
104 	public void unknownRepositoryFormatVersion() throws Exception {
105 		Repository r = createWorkRepository();
106 		StoredConfig config = r.getConfig();
107 		config.setLong(ConfigConstants.CONFIG_CORE_SECTION, null,
108 				ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999);
109 		config.save();
110 
111 		try (FileRepository repo = new FileRepository(r.getDirectory())) {
112 			fail("IOException not thrown");
113 		} catch (IOException e) {
114 			assertNotNull(e.getMessage());
115 		}
116 	}
117 
118 	@Test
119 	public void absoluteGitDirRef() throws Exception {
120 		Repository repo1 = createWorkRepository();
121 		File dir = createTempDirectory("dir");
122 		File dotGit = new File(dir, Constants.DOT_GIT);
123 		try (FileWriter writer = new FileWriter(dotGit)) {
124 			writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath()).close();
125 			FileRepositoryBuilder builder = new FileRepositoryBuilder();
126 
127 			builder.setWorkTree(dir);
128 			builder.setMustExist(true);
129 			Repository repo2 = builder.build();
130 
131 			assertEquals(repo1.getDirectory().getAbsolutePath(), repo2
132 					.getDirectory().getAbsolutePath());
133 			assertEquals(dir, repo2.getWorkTree());
134 		}
135 	}
136 
137 	@Test
138 	public void relativeGitDirRef() throws Exception {
139 		Repository repo1 = createWorkRepository();
140 		File dir = new File(repo1.getWorkTree(), "dir");
141 		assertTrue(dir.mkdir());
142 		File dotGit = new File(dir, Constants.DOT_GIT);
143 		try (FileWriter writer = new FileWriter(dotGit)) {
144 			writer.append("gitdir: ../" + Constants.DOT_GIT).close();
145 
146 			FileRepositoryBuilder builder = new FileRepositoryBuilder();
147 			builder.setWorkTree(dir);
148 			builder.setMustExist(true);
149 			Repository repo2 = builder.build();
150 
151 			
152 			
153 			assertEquals(repo1.getDirectory().getCanonicalPath(), repo2
154 					.getDirectory().getCanonicalPath());
155 			assertEquals(dir, repo2.getWorkTree());
156 		}
157 	}
158 
159 	@Test
160 	public void scanWithGitDirRef() throws Exception {
161 		Repository repo1 = createWorkRepository();
162 		File dir = createTempDirectory("dir");
163 		File dotGit = new File(dir, Constants.DOT_GIT);
164 		try (FileWriter writer = new FileWriter(dotGit)) {
165 			writer.append(
166 					"gitdir: " + repo1.getDirectory().getAbsolutePath()).close();
167 			FileRepositoryBuilder builder = new FileRepositoryBuilder();
168 
169 			builder.setWorkTree(dir);
170 			builder.findGitDir(dir);
171 			assertEquals(repo1.getDirectory().getAbsolutePath(), builder
172 					.getGitDir().getAbsolutePath());
173 			builder.setMustExist(true);
174 			Repository repo2 = builder.build();
175 
176 			
177 			assertEquals(repo1.getDirectory().getCanonicalPath(), repo2
178 					.getDirectory().getCanonicalPath());
179 			assertEquals(dir, repo2.getWorkTree());
180 		}
181 	}
182 }