Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RepositorySetupWorkDirTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. *
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.storage.file;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import org.eclipse.jgit.errors.ConfigInvalidException;
  48. import org.eclipse.jgit.errors.NoWorkTreeException;
  49. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  50. import org.eclipse.jgit.lib.ConfigConstants;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.Repository;
  53. /**
  54. * Tests for setting up the working directory when creating a Repository
  55. */
  56. public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
  57. public void testIsBare_CreateRepositoryFromArbitraryGitDir()
  58. throws Exception {
  59. File gitDir = getFile("workdir");
  60. assertTrue(new FileRepository(gitDir).isBare());
  61. }
  62. public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
  63. File gitDir = getFile("workdir", Constants.DOT_GIT);
  64. Repository repo = new FileRepository(gitDir);
  65. assertFalse(repo.isBare());
  66. assertWorkdirPath(repo, "workdir");
  67. assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
  68. }
  69. public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
  70. throws Exception {
  71. File gitDir = getFile("workdir", Constants.DOT_GIT);
  72. Repository repo = new FileRepository(gitDir);
  73. String workdir = repo.getWorkTree().getName();
  74. assertEquals(workdir, "workdir");
  75. }
  76. public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
  77. File workdir = getFile("workdir", "repo");
  78. FileRepository repo = new FileRepositoryBuilder().setWorkTree(workdir).build();
  79. assertFalse(repo.isBare());
  80. assertWorkdirPath(repo, "workdir", "repo");
  81. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  82. }
  83. public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
  84. throws Exception {
  85. File workdir = getFile("workdir", "repo");
  86. FileRepository repo = new FileRepositoryBuilder().setWorkTree(workdir).build();
  87. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  88. }
  89. public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
  90. throws Exception {
  91. File gitDir = getFile("workdir", "repoWithConfig");
  92. File workTree = getFile("workdir", "treeRoot");
  93. setWorkTree(gitDir, workTree);
  94. FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  95. assertFalse(repo.isBare());
  96. assertWorkdirPath(repo, "workdir", "treeRoot");
  97. assertGitdirPath(repo, "workdir", "repoWithConfig");
  98. }
  99. public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
  100. throws Exception {
  101. File gitDir = getFile("workdir", "repoWithConfig");
  102. setBare(gitDir, true);
  103. FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  104. assertTrue(repo.isBare());
  105. }
  106. public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  107. throws Exception {
  108. File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
  109. setBare(gitDir, false);
  110. FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  111. assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
  112. }
  113. public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  114. throws Exception {
  115. File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
  116. setBare(gitDir, false);
  117. FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
  118. assertFalse(repo.isBare());
  119. assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
  120. assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");
  121. }
  122. public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
  123. File gitDir = getFile("workdir");
  124. try {
  125. new FileRepository(gitDir).getWorkTree();
  126. fail("Expected NoWorkTreeException missing");
  127. } catch (NoWorkTreeException e) {
  128. // expected
  129. }
  130. }
  131. public void testExceptionThrown_BareRepoGetIndex() throws Exception {
  132. File gitDir = getFile("workdir");
  133. try {
  134. new FileRepository(gitDir).getIndex();
  135. fail("Expected NoWorkTreeException missing");
  136. } catch (NoWorkTreeException e) {
  137. // expected
  138. }
  139. }
  140. public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
  141. File gitDir = getFile("workdir");
  142. try {
  143. new FileRepository(gitDir).getIndexFile();
  144. fail("Expected NoWorkTreeException missing");
  145. } catch (NoWorkTreeException e) {
  146. // expected
  147. }
  148. }
  149. private File getFile(String... pathComponents) {
  150. String rootPath = new File(new File("target"), "trash").getPath();
  151. for (String pathComponent : pathComponents)
  152. rootPath = rootPath + File.separatorChar + pathComponent;
  153. File result = new File(rootPath);
  154. result.mkdir();
  155. return result;
  156. }
  157. private void setBare(File gitDir, boolean bare) throws IOException,
  158. ConfigInvalidException {
  159. FileBasedConfig cfg = configFor(gitDir);
  160. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  161. ConfigConstants.CONFIG_KEY_BARE, bare);
  162. cfg.save();
  163. }
  164. private void setWorkTree(File gitDir, File workTree) throws IOException,
  165. ConfigInvalidException {
  166. String path = workTree.getAbsolutePath();
  167. FileBasedConfig cfg = configFor(gitDir);
  168. cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  169. ConfigConstants.CONFIG_KEY_WORKTREE, path);
  170. cfg.save();
  171. }
  172. private FileBasedConfig configFor(File gitDir) throws IOException,
  173. ConfigInvalidException {
  174. FileBasedConfig cfg = new FileBasedConfig(new File(gitDir, "config"));
  175. cfg.load();
  176. return cfg;
  177. }
  178. private void assertGitdirPath(Repository repo, String... expected)
  179. throws IOException {
  180. File exp = getFile(expected).getCanonicalFile();
  181. File act = repo.getDirectory().getCanonicalFile();
  182. assertEquals("Wrong Git Directory", exp, act);
  183. }
  184. private void assertWorkdirPath(Repository repo, String... expected)
  185. throws IOException {
  186. File exp = getFile(expected).getCanonicalFile();
  187. File act = repo.getWorkTree().getCanonicalFile();
  188. assertEquals("Wrong working Directory", exp, act);
  189. }
  190. }