View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  			// Unused
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 			// The tmp directory may be a symlink so the actual path
152 			// may not
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 			// The tmp directory may be a symlink
177 			assertEquals(repo1.getDirectory().getCanonicalPath(), repo2
178 					.getDirectory().getCanonicalPath());
179 			assertEquals(dir, repo2.getWorkTree());
180 		}
181 	}
182 }