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.

RepositorySetupWorkDirTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. * and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.internal.storage.file;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import org.eclipse.jgit.errors.ConfigInvalidException;
  19. import org.eclipse.jgit.errors.NoWorkTreeException;
  20. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  21. import org.eclipse.jgit.lib.ConfigConstants;
  22. import org.eclipse.jgit.lib.Constants;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.eclipse.jgit.storage.file.FileBasedConfig;
  25. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  26. import org.eclipse.jgit.util.FS;
  27. import org.eclipse.jgit.util.FileUtils;
  28. import org.junit.Test;
  29. /**
  30. * Tests for setting up the working directory when creating a Repository
  31. */
  32. public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
  33. @Test
  34. public void testIsBare_CreateRepositoryFromArbitraryGitDir()
  35. throws Exception {
  36. File gitDir = getFile("workdir");
  37. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  38. assertTrue(repo.isBare());
  39. }
  40. @Test
  41. public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
  42. File gitDir = getFile("workdir", Constants.DOT_GIT);
  43. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  44. assertFalse(repo.isBare());
  45. assertWorkdirPath(repo, "workdir");
  46. assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
  47. }
  48. @Test
  49. public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
  50. throws Exception {
  51. File gitDir = getFile("workdir", Constants.DOT_GIT);
  52. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  53. String workdir = repo.getWorkTree().getName();
  54. assertEquals(workdir, "workdir");
  55. }
  56. @Test
  57. public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
  58. File workdir = getFile("workdir", "repo");
  59. Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
  60. .build();
  61. assertFalse(repo.isBare());
  62. assertWorkdirPath(repo, "workdir", "repo");
  63. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  64. }
  65. @Test
  66. public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
  67. throws Exception {
  68. File workdir = getFile("workdir", "repo");
  69. Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
  70. .build();
  71. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  72. }
  73. @Test
  74. public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
  75. throws Exception {
  76. File gitDir = getFile("workdir", "repoWithConfig");
  77. File workTree = getFile("workdir", "treeRoot");
  78. setWorkTree(gitDir, workTree);
  79. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  80. assertFalse(repo.isBare());
  81. assertWorkdirPath(repo, "workdir", "treeRoot");
  82. assertGitdirPath(repo, "workdir", "repoWithConfig");
  83. }
  84. @Test
  85. public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
  86. throws Exception {
  87. File gitDir = getFile("workdir", "repoWithConfig");
  88. setBare(gitDir, true);
  89. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  90. assertTrue(repo.isBare());
  91. }
  92. @Test
  93. public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  94. throws Exception {
  95. File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
  96. setBare(gitDir, false);
  97. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  98. assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
  99. }
  100. @Test
  101. public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  102. throws Exception {
  103. File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
  104. setBare(gitDir, false);
  105. Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  106. assertFalse(repo.isBare());
  107. assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
  108. assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");
  109. }
  110. @Test
  111. public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
  112. File gitDir = getFile("workdir");
  113. try (Repository repo = new FileRepository(gitDir)) {
  114. repo.getWorkTree();
  115. fail("Expected NoWorkTreeException missing");
  116. } catch (NoWorkTreeException e) {
  117. // expected
  118. }
  119. }
  120. @Test
  121. public void testExceptionThrown_BareRepoGetIndex() throws Exception {
  122. File gitDir = getFile("workdir");
  123. try (Repository repo = new FileRepository(gitDir)) {
  124. repo.readDirCache();
  125. fail("Expected NoWorkTreeException missing");
  126. } catch (NoWorkTreeException e) {
  127. // expected
  128. }
  129. }
  130. @Test
  131. public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
  132. File gitDir = getFile("workdir");
  133. try (Repository repo = new FileRepository(gitDir)) {
  134. repo.getIndexFile();
  135. fail("Expected NoWorkTreeException missing");
  136. } catch (NoWorkTreeException e) {
  137. // expected
  138. }
  139. }
  140. private File getFile(String... pathComponents) throws IOException {
  141. File dir = getTemporaryDirectory();
  142. for (String pathComponent : pathComponents)
  143. dir = new File(dir, pathComponent);
  144. FileUtils.mkdirs(dir, true);
  145. return dir;
  146. }
  147. private void setBare(File gitDir, boolean bare) throws IOException,
  148. ConfigInvalidException {
  149. FileBasedConfig cfg = configFor(gitDir);
  150. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  151. ConfigConstants.CONFIG_KEY_BARE, bare);
  152. cfg.save();
  153. }
  154. private void setWorkTree(File gitDir, File workTree)
  155. throws IOException,
  156. ConfigInvalidException {
  157. String path = workTree.getAbsolutePath();
  158. FileBasedConfig cfg = configFor(gitDir);
  159. cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  160. ConfigConstants.CONFIG_KEY_WORKTREE, path);
  161. cfg.save();
  162. }
  163. private FileBasedConfig configFor(File gitDir) throws IOException,
  164. ConfigInvalidException {
  165. File configPath = new File(gitDir, Constants.CONFIG);
  166. FileBasedConfig cfg = new FileBasedConfig(configPath, FS.DETECTED);
  167. cfg.load();
  168. return cfg;
  169. }
  170. private void assertGitdirPath(Repository repo, String... expected)
  171. throws IOException {
  172. File exp = getFile(expected).getCanonicalFile();
  173. File act = repo.getDirectory().getCanonicalFile();
  174. assertEquals("Wrong Git Directory", exp, act);
  175. }
  176. private void assertWorkdirPath(Repository repo, String... expected)
  177. throws IOException {
  178. File exp = getFile(expected).getCanonicalFile();
  179. File act = repo.getWorkTree().getCanonicalFile();
  180. assertEquals("Wrong working Directory", exp, act);
  181. }
  182. }