You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InitCommandTest.java 8.1KB

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