View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import static java.nio.charset.StandardCharsets.UTF_8;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertTrue;
17  import static org.junit.Assert.fail;
18  
19  import java.io.BufferedWriter;
20  import java.io.File;
21  import java.io.IOException;
22  import java.nio.file.Files;
23  
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.lib.StoredConfig;
29  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
30  import org.eclipse.jgit.util.FileUtils;
31  import org.junit.Test;
32  
33  public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
34  	@Test
35  	public void testShouldAutomagicallyDetectGitDirectory() throws Exception {
36  		Repository r = createWorkRepository();
37  		File d = new File(r.getDirectory(), "sub-dir");
38  		FileUtils.mkdir(d);
39  
40  		assertEquals(r.getDirectory(), new FileRepositoryBuilder()
41  				.findGitDir(d).getGitDir());
42  	}
43  
44  	@Test
45  	public void emptyRepositoryFormatVersion() throws Exception {
46  		Repository r = createWorkRepository();
47  		StoredConfig config = r.getConfig();
48  		config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
49  				ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "");
50  		config.save();
51  
52  		try (FileRepository repo = new FileRepository(r.getDirectory())) {
53  			// Unused
54  		}
55  	}
56  
57  	@Test
58  	public void invalidRepositoryFormatVersion() throws Exception {
59  		Repository r = createWorkRepository();
60  		StoredConfig config = r.getConfig();
61  		config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
62  				ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber");
63  		config.save();
64  
65  		try (FileRepository repo = new FileRepository(r.getDirectory())) {
66  			fail("IllegalArgumentException not thrown");
67  		} catch (IllegalArgumentException e) {
68  			assertNotNull(e.getMessage());
69  		}
70  	}
71  
72  	@Test
73  	public void unknownRepositoryFormatVersion() throws Exception {
74  		Repository r = createWorkRepository();
75  		StoredConfig config = r.getConfig();
76  		config.setLong(ConfigConstants.CONFIG_CORE_SECTION, null,
77  				ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999);
78  		config.save();
79  
80  		try (FileRepository repo = new FileRepository(r.getDirectory())) {
81  			fail("IOException not thrown");
82  		} catch (IOException e) {
83  			assertNotNull(e.getMessage());
84  		}
85  	}
86  
87  	@Test
88  	public void absoluteGitDirRef() throws Exception {
89  		Repository repo1 = createWorkRepository();
90  		File dir = createTempDirectory("dir");
91  		File dotGit = new File(dir, Constants.DOT_GIT);
92  		try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
93  				UTF_8)) {
94  			writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath());
95  		}
96  		FileRepositoryBuilder builder = new FileRepositoryBuilder();
97  
98  		builder.setWorkTree(dir);
99  		builder.setMustExist(true);
100 		Repository repo2 = builder.build();
101 
102 		assertEquals(repo1.getDirectory().getAbsolutePath(),
103 				repo2.getDirectory().getAbsolutePath());
104 		assertEquals(dir, repo2.getWorkTree());
105 	}
106 
107 	@Test
108 	public void relativeGitDirRef() throws Exception {
109 		Repository repo1 = createWorkRepository();
110 		File dir = new File(repo1.getWorkTree(), "dir");
111 		assertTrue(dir.mkdir());
112 		File dotGit = new File(dir, Constants.DOT_GIT);
113 		try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
114 				UTF_8)) {
115 			writer.append("gitdir: ../" + Constants.DOT_GIT);
116 		}
117 		FileRepositoryBuilder builder = new FileRepositoryBuilder();
118 		builder.setWorkTree(dir);
119 		builder.setMustExist(true);
120 		Repository repo2 = builder.build();
121 
122 		// The tmp directory may be a symlink so the actual path
123 		// may not
124 		assertEquals(repo1.getDirectory().getCanonicalPath(),
125 				repo2.getDirectory().getCanonicalPath());
126 		assertEquals(dir, repo2.getWorkTree());
127 	}
128 
129 	@Test
130 	public void scanWithGitDirRef() throws Exception {
131 		Repository repo1 = createWorkRepository();
132 		File dir = createTempDirectory("dir");
133 		File dotGit = new File(dir, Constants.DOT_GIT);
134 		try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
135 				UTF_8)) {
136 			writer.append(
137 					"gitdir: " + repo1.getDirectory().getAbsolutePath());
138 		}
139 		FileRepositoryBuilder builder = new FileRepositoryBuilder();
140 
141 		builder.setWorkTree(dir);
142 		builder.findGitDir(dir);
143 		assertEquals(repo1.getDirectory().getAbsolutePath(),
144 				builder.getGitDir().getAbsolutePath());
145 		builder.setMustExist(true);
146 		Repository repo2 = builder.build();
147 
148 		// The tmp directory may be a symlink
149 		assertEquals(repo1.getDirectory().getCanonicalPath(),
150 				repo2.getDirectory().getCanonicalPath());
151 		assertEquals(dir, repo2.getWorkTree());
152 	}
153 }