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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com> 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. package org.eclipse.jgit.api;
  11. import static org.junit.Assert.assertNotNull;
  12. import static org.junit.Assert.assertNull;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import org.eclipse.jgit.api.errors.GitAPIException;
  17. import org.eclipse.jgit.api.errors.JGitInternalException;
  18. import org.eclipse.jgit.errors.NoWorkTreeException;
  19. import org.eclipse.jgit.junit.MockSystemReader;
  20. import org.eclipse.jgit.junit.RepositoryTestCase;
  21. import org.eclipse.jgit.lib.Constants;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.util.SystemReader;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. public class InitCommandTest extends RepositoryTestCase {
  27. @Override
  28. @Before
  29. public void setUp() throws Exception {
  30. super.setUp();
  31. }
  32. @Test
  33. public void testInitRepository()
  34. throws IOException, JGitInternalException, GitAPIException {
  35. File directory = createTempDirectory("testInitRepository");
  36. InitCommand command = new InitCommand();
  37. command.setDirectory(directory);
  38. try (Git git = command.call()) {
  39. assertNotNull(git.getRepository());
  40. }
  41. }
  42. @Test
  43. public void testInitNonEmptyRepository() throws IOException,
  44. JGitInternalException, GitAPIException {
  45. File directory = createTempDirectory("testInitRepository2");
  46. File someFile = new File(directory, "someFile");
  47. someFile.createNewFile();
  48. assertTrue(someFile.exists());
  49. assertTrue(directory.listFiles().length > 0);
  50. InitCommand command = new InitCommand();
  51. command.setDirectory(directory);
  52. try (Git git = command.call()) {
  53. assertNotNull(git.getRepository());
  54. }
  55. }
  56. @Test
  57. public void testInitBareRepository() throws IOException,
  58. JGitInternalException, GitAPIException {
  59. File directory = createTempDirectory("testInitBareRepository");
  60. InitCommand command = new InitCommand();
  61. command.setDirectory(directory);
  62. command.setBare(true);
  63. try (Git git = command.call()) {
  64. Repository repository = git.getRepository();
  65. assertNotNull(repository);
  66. assertTrue(repository.isBare());
  67. }
  68. }
  69. // non-bare repos where gitDir and directory is set. Same as
  70. // "git init --separate-git-dir /tmp/a /tmp/b"
  71. @Test
  72. public void testInitWithExplicitGitDir() throws IOException,
  73. JGitInternalException, GitAPIException {
  74. File wt = createTempDirectory("testInitRepositoryWT");
  75. File gitDir = createTempDirectory("testInitRepositoryGIT");
  76. InitCommand command = new InitCommand();
  77. command.setDirectory(wt);
  78. command.setGitDir(gitDir);
  79. try (Git git = command.call()) {
  80. Repository repository = git.getRepository();
  81. assertNotNull(repository);
  82. assertEqualsFile(wt, repository.getWorkTree());
  83. assertEqualsFile(gitDir, repository.getDirectory());
  84. }
  85. }
  86. // non-bare repos where only gitDir is set. Same as
  87. // "git init --separate-git-dir /tmp/a"
  88. @Test
  89. public void testInitWithOnlyExplicitGitDir() throws IOException,
  90. JGitInternalException, GitAPIException {
  91. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  92. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  93. .getAbsolutePath());
  94. File gitDir = createTempDirectory("testInitRepository/.git");
  95. InitCommand command = new InitCommand();
  96. command.setGitDir(gitDir);
  97. try (Git git = command.call()) {
  98. Repository repository = git.getRepository();
  99. assertNotNull(repository);
  100. assertEqualsFile(gitDir, repository.getDirectory());
  101. assertEqualsFile(new File(reader.getProperty("user.dir")),
  102. repository.getWorkTree());
  103. }
  104. }
  105. // Bare repos where gitDir and directory is set will only work if gitDir and
  106. // directory is pointing to same dir. Same as
  107. // "git init --bare --separate-git-dir /tmp/a /tmp/b"
  108. // (works in native git but I guess that's more a bug)
  109. @Test(expected = IllegalStateException.class)
  110. public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
  111. JGitInternalException, GitAPIException {
  112. File gitDir = createTempDirectory("testInitRepository.git");
  113. InitCommand command = new InitCommand();
  114. command.setBare(true);
  115. command.setDirectory(gitDir);
  116. command.setGitDir(new File(gitDir, ".."));
  117. command.call();
  118. }
  119. // If neither directory nor gitDir is set in a non-bare repo make sure
  120. // worktree and gitDir are set correctly. Standard case. Same as
  121. // "git init"
  122. @Test
  123. public void testInitWithDefaultsNonBare() throws JGitInternalException,
  124. GitAPIException, IOException {
  125. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  126. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  127. .getAbsolutePath());
  128. InitCommand command = new InitCommand();
  129. command.setBare(false);
  130. try (Git git = command.call()) {
  131. Repository repository = git.getRepository();
  132. assertNotNull(repository);
  133. assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
  134. repository.getDirectory());
  135. assertEqualsFile(new File(reader.getProperty("user.dir")),
  136. repository.getWorkTree());
  137. }
  138. }
  139. // If neither directory nor gitDir is set in a bare repo make sure
  140. // worktree and gitDir are set correctly. Standard case. Same as
  141. // "git init --bare"
  142. @Test(expected = NoWorkTreeException.class)
  143. public void testInitWithDefaultsBare() throws JGitInternalException,
  144. GitAPIException, IOException {
  145. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  146. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  147. .getAbsolutePath());
  148. InitCommand command = new InitCommand();
  149. command.setBare(true);
  150. try (Git git = command.call()) {
  151. Repository repository = git.getRepository();
  152. assertNotNull(repository);
  153. assertEqualsFile(new File(reader.getProperty("user.dir")),
  154. repository.getDirectory());
  155. assertNull(repository.getWorkTree());
  156. }
  157. }
  158. // In a non-bare repo when directory and gitDir is set then they shouldn't
  159. // point to the same dir. Same as
  160. // "git init --separate-git-dir /tmp/a /tmp/a"
  161. // (works in native git but I guess that's more a bug)
  162. @Test(expected = IllegalStateException.class)
  163. public void testInitNonBare_GitdirAndDirShouldntBeSame()
  164. throws JGitInternalException, GitAPIException, IOException {
  165. File gitDir = createTempDirectory("testInitRepository.git");
  166. InitCommand command = new InitCommand();
  167. command.setBare(false);
  168. command.setGitDir(gitDir);
  169. command.setDirectory(gitDir);
  170. command.call().getRepository();
  171. }
  172. }