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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertNull;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import org.eclipse.jgit.api.errors.GitAPIException;
  18. import org.eclipse.jgit.api.errors.JGitInternalException;
  19. import org.eclipse.jgit.errors.NoWorkTreeException;
  20. import org.eclipse.jgit.junit.MockSystemReader;
  21. import org.eclipse.jgit.junit.RepositoryTestCase;
  22. import org.eclipse.jgit.lib.ConfigConstants;
  23. import org.eclipse.jgit.lib.Constants;
  24. import org.eclipse.jgit.lib.Repository;
  25. import org.eclipse.jgit.lib.StoredConfig;
  26. import org.eclipse.jgit.util.SystemReader;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. public class InitCommandTest extends RepositoryTestCase {
  30. @Override
  31. @Before
  32. public void setUp() throws Exception {
  33. super.setUp();
  34. }
  35. @Test
  36. public void testInitRepository()
  37. throws IOException, JGitInternalException, GitAPIException {
  38. File directory = createTempDirectory("testInitRepository");
  39. InitCommand command = new InitCommand();
  40. command.setDirectory(directory);
  41. try (Git git = command.call()) {
  42. Repository r = git.getRepository();
  43. assertNotNull(r);
  44. assertEquals("refs/heads/master", r.getFullBranch());
  45. }
  46. }
  47. @Test
  48. public void testInitRepositoryMainInitialBranch()
  49. throws IOException, JGitInternalException, GitAPIException {
  50. File directory = createTempDirectory("testInitRepository");
  51. InitCommand command = new InitCommand();
  52. command.setDirectory(directory);
  53. command.setInitialBranch("main");
  54. try (Git git = command.call()) {
  55. Repository r = git.getRepository();
  56. assertNotNull(r);
  57. assertEquals("refs/heads/main", r.getFullBranch());
  58. }
  59. }
  60. @Test
  61. public void testInitRepositoryCustomDefaultBranch()
  62. throws Exception {
  63. File directory = createTempDirectory("testInitRepository");
  64. InitCommand command = new InitCommand();
  65. command.setDirectory(directory);
  66. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  67. StoredConfig c = reader.getUserConfig();
  68. String old = c.getString(ConfigConstants.CONFIG_INIT_SECTION, null,
  69. ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH);
  70. c.setString(ConfigConstants.CONFIG_INIT_SECTION, null,
  71. ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH, "main");
  72. try (Git git = command.call()) {
  73. Repository r = git.getRepository();
  74. assertNotNull(r);
  75. assertEquals("refs/heads/main", r.getFullBranch());
  76. } finally {
  77. c.setString(ConfigConstants.CONFIG_INIT_SECTION, null,
  78. ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH, old);
  79. }
  80. }
  81. @Test
  82. public void testInitRepositoryNullInitialBranch() throws Exception {
  83. File directory = createTempDirectory("testInitRepository");
  84. InitCommand command = new InitCommand();
  85. command.setDirectory(directory);
  86. command.setInitialBranch("main");
  87. command.setInitialBranch(null);
  88. try (Git git = command.call()) {
  89. Repository r = git.getRepository();
  90. assertNotNull(r);
  91. assertEquals("refs/heads/master", r.getFullBranch());
  92. }
  93. }
  94. @Test
  95. public void testInitRepositoryEmptyInitialBranch() throws Exception {
  96. File directory = createTempDirectory("testInitRepository");
  97. InitCommand command = new InitCommand();
  98. command.setDirectory(directory);
  99. command.setInitialBranch("main");
  100. command.setInitialBranch("");
  101. try (Git git = command.call()) {
  102. Repository r = git.getRepository();
  103. assertNotNull(r);
  104. assertEquals("refs/heads/master", r.getFullBranch());
  105. }
  106. }
  107. @Test
  108. public void testInitNonEmptyRepository() throws IOException,
  109. JGitInternalException, GitAPIException {
  110. File directory = createTempDirectory("testInitRepository2");
  111. File someFile = new File(directory, "someFile");
  112. someFile.createNewFile();
  113. assertTrue(someFile.exists());
  114. assertTrue(directory.listFiles().length > 0);
  115. InitCommand command = new InitCommand();
  116. command.setDirectory(directory);
  117. try (Git git = command.call()) {
  118. assertNotNull(git.getRepository());
  119. }
  120. }
  121. @Test
  122. public void testInitBareRepository() throws IOException,
  123. JGitInternalException, GitAPIException {
  124. File directory = createTempDirectory("testInitBareRepository");
  125. InitCommand command = new InitCommand();
  126. command.setDirectory(directory);
  127. command.setBare(true);
  128. try (Git git = command.call()) {
  129. Repository repository = git.getRepository();
  130. assertNotNull(repository);
  131. assertTrue(repository.isBare());
  132. assertEquals("refs/heads/master", repository.getFullBranch());
  133. }
  134. }
  135. @Test
  136. public void testInitBareRepositoryMainInitialBranch()
  137. throws IOException, JGitInternalException, GitAPIException {
  138. File directory = createTempDirectory("testInitBareRepository");
  139. InitCommand command = new InitCommand();
  140. command.setDirectory(directory);
  141. command.setBare(true);
  142. command.setInitialBranch("main");
  143. try (Git git = command.call()) {
  144. Repository repository = git.getRepository();
  145. assertNotNull(repository);
  146. assertTrue(repository.isBare());
  147. assertEquals("refs/heads/main", repository.getFullBranch());
  148. }
  149. }
  150. // non-bare repos where gitDir and directory is set. Same as
  151. // "git init --separate-git-dir /tmp/a /tmp/b"
  152. @Test
  153. public void testInitWithExplicitGitDir() throws IOException,
  154. JGitInternalException, GitAPIException {
  155. File wt = createTempDirectory("testInitRepositoryWT");
  156. File gitDir = createTempDirectory("testInitRepositoryGIT");
  157. InitCommand command = new InitCommand();
  158. command.setDirectory(wt);
  159. command.setGitDir(gitDir);
  160. try (Git git = command.call()) {
  161. Repository repository = git.getRepository();
  162. assertNotNull(repository);
  163. assertEqualsFile(wt, repository.getWorkTree());
  164. assertEqualsFile(gitDir, repository.getDirectory());
  165. }
  166. }
  167. // non-bare repos where only gitDir is set. Same as
  168. // "git init --separate-git-dir /tmp/a"
  169. @Test
  170. public void testInitWithOnlyExplicitGitDir() throws IOException,
  171. JGitInternalException, GitAPIException {
  172. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  173. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  174. .getAbsolutePath());
  175. File gitDir = createTempDirectory("testInitRepository/.git");
  176. InitCommand command = new InitCommand();
  177. command.setGitDir(gitDir);
  178. try (Git git = command.call()) {
  179. Repository repository = git.getRepository();
  180. assertNotNull(repository);
  181. assertEqualsFile(gitDir, repository.getDirectory());
  182. assertEqualsFile(new File(reader.getProperty("user.dir")),
  183. repository.getWorkTree());
  184. }
  185. }
  186. // Bare repos where gitDir and directory is set will only work if gitDir and
  187. // directory is pointing to same dir. Same as
  188. // "git init --bare --separate-git-dir /tmp/a /tmp/b"
  189. // (works in native git but I guess that's more a bug)
  190. @Test(expected = IllegalStateException.class)
  191. public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
  192. JGitInternalException, GitAPIException {
  193. File gitDir = createTempDirectory("testInitRepository.git");
  194. InitCommand command = new InitCommand();
  195. command.setBare(true);
  196. command.setDirectory(gitDir);
  197. command.setGitDir(new File(gitDir, ".."));
  198. command.call();
  199. }
  200. // If neither directory nor gitDir is set in a non-bare repo make sure
  201. // worktree and gitDir are set correctly. Standard case. Same as
  202. // "git init"
  203. @Test
  204. public void testInitWithDefaultsNonBare() throws JGitInternalException,
  205. GitAPIException, IOException {
  206. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  207. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  208. .getAbsolutePath());
  209. InitCommand command = new InitCommand();
  210. command.setBare(false);
  211. try (Git git = command.call()) {
  212. Repository repository = git.getRepository();
  213. assertNotNull(repository);
  214. assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
  215. repository.getDirectory());
  216. assertEqualsFile(new File(reader.getProperty("user.dir")),
  217. repository.getWorkTree());
  218. }
  219. }
  220. // If neither directory nor gitDir is set in a bare repo make sure
  221. // worktree and gitDir are set correctly. Standard case. Same as
  222. // "git init --bare"
  223. @Test(expected = NoWorkTreeException.class)
  224. public void testInitWithDefaultsBare() throws JGitInternalException,
  225. GitAPIException, IOException {
  226. MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
  227. reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
  228. .getAbsolutePath());
  229. InitCommand command = new InitCommand();
  230. command.setBare(true);
  231. try (Git git = command.call()) {
  232. Repository repository = git.getRepository();
  233. assertNotNull(repository);
  234. assertEqualsFile(new File(reader.getProperty("user.dir")),
  235. repository.getDirectory());
  236. assertNull(repository.getWorkTree());
  237. }
  238. }
  239. // In a non-bare repo when directory and gitDir is set then they shouldn't
  240. // point to the same dir. Same as
  241. // "git init --separate-git-dir /tmp/a /tmp/a"
  242. // (works in native git but I guess that's more a bug)
  243. @Test(expected = IllegalStateException.class)
  244. public void testInitNonBare_GitdirAndDirShouldntBeSame()
  245. throws JGitInternalException, GitAPIException, IOException {
  246. File gitDir = createTempDirectory("testInitRepository.git");
  247. InitCommand command = new InitCommand();
  248. command.setBare(false);
  249. command.setGitDir(gitDir);
  250. command.setDirectory(gitDir);
  251. command.call().getRepository();
  252. }
  253. }