View Javadoc
1   /*
2    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
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  package org.eclipse.jgit.api;
44  
45  import static org.junit.Assert.assertNotNull;
46  import static org.junit.Assert.assertNull;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.api.errors.GitAPIException;
53  import org.eclipse.jgit.api.errors.JGitInternalException;
54  import org.eclipse.jgit.errors.NoWorkTreeException;
55  import org.eclipse.jgit.junit.MockSystemReader;
56  import org.eclipse.jgit.junit.RepositoryTestCase;
57  import org.eclipse.jgit.lib.Constants;
58  import org.eclipse.jgit.lib.Repository;
59  import org.eclipse.jgit.util.SystemReader;
60  import org.junit.Before;
61  import org.junit.Test;
62  
63  public class InitCommandTest extends RepositoryTestCase {
64  
65  	@Override
66  	@Before
67  	public void setUp() throws Exception {
68  		super.setUp();
69  	}
70  
71  	@Test
72  	public void testInitRepository()
73  			throws IOException, JGitInternalException, GitAPIException {
74  		File directory = createTempDirectory("testInitRepository");
75  		InitCommand command = new InitCommand();
76  		command.setDirectory(directory);
77  		try (Git git = command.call()) {
78  			assertNotNull(git.getRepository());
79  		}
80  	}
81  
82  	@Test
83  	public void testInitNonEmptyRepository() throws IOException,
84  			JGitInternalException, GitAPIException {
85  		File directory = createTempDirectory("testInitRepository2");
86  		File someFile = new File(directory, "someFile");
87  		someFile.createNewFile();
88  		assertTrue(someFile.exists());
89  		assertTrue(directory.listFiles().length > 0);
90  		InitCommand command = new InitCommand();
91  		command.setDirectory(directory);
92  		try (Git git = command.call()) {
93  			assertNotNull(git.getRepository());
94  		}
95  	}
96  
97  	@Test
98  	public void testInitBareRepository() throws IOException,
99  			JGitInternalException, GitAPIException {
100 		File directory = createTempDirectory("testInitBareRepository");
101 		InitCommand command = new InitCommand();
102 		command.setDirectory(directory);
103 		command.setBare(true);
104 		try (Git git = command.call()) {
105 			Repository repository = git.getRepository();
106 			assertNotNull(repository);
107 			assertTrue(repository.isBare());
108 		}
109 	}
110 
111 	// non-bare repos where gitDir and directory is set. Same as
112 	// "git init --separate-git-dir /tmp/a /tmp/b"
113 	@Test
114 	public void testInitWithExplicitGitDir() throws IOException,
115 			JGitInternalException, GitAPIException {
116 		File wt = createTempDirectory("testInitRepositoryWT");
117 		File gitDir = createTempDirectory("testInitRepositoryGIT");
118 		InitCommand command = new InitCommand();
119 		command.setDirectory(wt);
120 		command.setGitDir(gitDir);
121 		try (Git git = command.call()) {
122 			Repository repository = git.getRepository();
123 			assertNotNull(repository);
124 			assertEqualsFile(wt, repository.getWorkTree());
125 			assertEqualsFile(gitDir, repository.getDirectory());
126 		}
127 	}
128 
129 	// non-bare repos where only gitDir is set. Same as
130 	// "git init --separate-git-dir /tmp/a"
131 	@Test
132 	public void testInitWithOnlyExplicitGitDir() throws IOException,
133 			JGitInternalException, GitAPIException {
134 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
135 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
136 				.getAbsolutePath());
137 		File gitDir = createTempDirectory("testInitRepository/.git");
138 		InitCommand command = new InitCommand();
139 		command.setGitDir(gitDir);
140 		try (Git git = command.call()) {
141 			Repository repository = git.getRepository();
142 			assertNotNull(repository);
143 			assertEqualsFile(gitDir, repository.getDirectory());
144 			assertEqualsFile(new File(reader.getProperty("user.dir")),
145 					repository.getWorkTree());
146 		}
147 	}
148 
149 	// Bare repos where gitDir and directory is set will only work if gitDir and
150 	// directory is pointing to same dir. Same as
151 	// "git init --bare --separate-git-dir /tmp/a /tmp/b"
152 	// (works in native git but I guess that's more a bug)
153 	@Test(expected = IllegalStateException.class)
154 	public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
155 			JGitInternalException, GitAPIException {
156 		File gitDir = createTempDirectory("testInitRepository.git");
157 		InitCommand command = new InitCommand();
158 		command.setBare(true);
159 		command.setDirectory(gitDir);
160 		command.setGitDir(new File(gitDir, ".."));
161 		command.call();
162 	}
163 
164 	// If neither directory nor gitDir is set in a non-bare repo make sure
165 	// worktree and gitDir are set correctly. Standard case. Same as
166 	// "git init"
167 	@Test
168 	public void testInitWithDefaultsNonBare() throws JGitInternalException,
169 			GitAPIException, IOException {
170 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
171 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
172 				.getAbsolutePath());
173 		InitCommand command = new InitCommand();
174 		command.setBare(false);
175 		try (Git git = command.call()) {
176 			Repository repository = git.getRepository();
177 			assertNotNull(repository);
178 			assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
179 					repository.getDirectory());
180 			assertEqualsFile(new File(reader.getProperty("user.dir")),
181 					repository.getWorkTree());
182 		}
183 	}
184 
185 	// If neither directory nor gitDir is set in a bare repo make sure
186 	// worktree and gitDir are set correctly. Standard case. Same as
187 	// "git init --bare"
188 	@Test(expected = NoWorkTreeException.class)
189 	public void testInitWithDefaultsBare() throws JGitInternalException,
190 			GitAPIException, IOException {
191 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
192 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
193 				.getAbsolutePath());
194 		InitCommand command = new InitCommand();
195 		command.setBare(true);
196 		try (Git git = command.call()) {
197 			Repository repository = git.getRepository();
198 			assertNotNull(repository);
199 			assertEqualsFile(new File(reader.getProperty("user.dir")),
200 					repository.getDirectory());
201 			assertNull(repository.getWorkTree());
202 		}
203 	}
204 
205 	// In a non-bare repo when directory and gitDir is set then they shouldn't
206 	// point to the same dir. Same as
207 	// "git init --separate-git-dir /tmp/a /tmp/a"
208 	// (works in native git but I guess that's more a bug)
209 	@Test(expected = IllegalStateException.class)
210 	public void testInitNonBare_GitdirAndDirShouldntBeSame()
211 			throws JGitInternalException, GitAPIException, IOException {
212 		File gitDir = createTempDirectory("testInitRepository.git");
213 		InitCommand command = new InitCommand();
214 		command.setBare(false);
215 		command.setGitDir(gitDir);
216 		command.setDirectory(gitDir);
217 		command.call().getRepository();
218 	}
219 }