選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SubmoduleSyncTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2011, GitHub Inc.
  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.submodule;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.File;
  49. import java.util.Map;
  50. import java.util.Map.Entry;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.api.SubmoduleSyncCommand;
  53. import org.eclipse.jgit.api.errors.GitAPIException;
  54. import org.eclipse.jgit.dircache.DirCache;
  55. import org.eclipse.jgit.dircache.DirCacheEditor;
  56. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  57. import org.eclipse.jgit.dircache.DirCacheEntry;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.lib.ConfigConstants;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.FileMode;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.lib.StoredConfig;
  65. import org.eclipse.jgit.storage.file.FileBasedConfig;
  66. import org.junit.Test;
  67. /**
  68. * Unit tests of {@link SubmoduleSyncCommand}
  69. */
  70. public class SubmoduleSyncTest extends RepositoryTestCase {
  71. @Test
  72. public void repositoryWithNoSubmodules() throws GitAPIException {
  73. SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
  74. Map<String, String> modules = command.call();
  75. assertNotNull(modules);
  76. assertTrue(modules.isEmpty());
  77. }
  78. @Test
  79. public void repositoryWithSubmodule() throws Exception {
  80. writeTrashFile("file.txt", "content");
  81. Git git = Git.wrap(db);
  82. git.add().addFilepattern("file.txt").call();
  83. git.commit().setMessage("create file").call();
  84. final ObjectId id = ObjectId
  85. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  86. final String path = "sub";
  87. DirCache cache = db.lockDirCache();
  88. DirCacheEditor editor = cache.editor();
  89. editor.add(new PathEdit(path) {
  90. public void apply(DirCacheEntry ent) {
  91. ent.setFileMode(FileMode.GITLINK);
  92. ent.setObjectId(id);
  93. }
  94. });
  95. editor.commit();
  96. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  97. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  98. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  99. ConfigConstants.CONFIG_KEY_PATH, path);
  100. String url = "git://server/repo.git";
  101. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  102. ConfigConstants.CONFIG_KEY_URL, url);
  103. modulesConfig.save();
  104. Repository subRepo = Git.cloneRepository()
  105. .setURI(db.getDirectory().toURI().toString())
  106. .setDirectory(new File(db.getWorkTree(), path)).call()
  107. .getRepository();
  108. addRepoToClose(subRepo);
  109. assertNotNull(subRepo);
  110. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  111. assertTrue(generator.next());
  112. assertNull(generator.getConfigUrl());
  113. assertEquals(url, generator.getModulesUrl());
  114. SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
  115. Map<String, String> synced = command.call();
  116. assertNotNull(synced);
  117. assertEquals(1, synced.size());
  118. Entry<String, String> module = synced.entrySet().iterator().next();
  119. assertEquals(path, module.getKey());
  120. assertEquals(url, module.getValue());
  121. generator = SubmoduleWalk.forIndex(db);
  122. assertTrue(generator.next());
  123. assertEquals(url, generator.getConfigUrl());
  124. Repository subModRepository = generator.getRepository();
  125. StoredConfig submoduleConfig = subModRepository.getConfig();
  126. subModRepository.close();
  127. assertEquals(url, submoduleConfig.getString(
  128. ConfigConstants.CONFIG_REMOTE_SECTION,
  129. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
  130. }
  131. @Test
  132. public void repositoryWithRelativeUriSubmodule() throws Exception {
  133. writeTrashFile("file.txt", "content");
  134. Git git = Git.wrap(db);
  135. git.add().addFilepattern("file.txt").call();
  136. git.commit().setMessage("create file").call();
  137. final ObjectId id = ObjectId
  138. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  139. final String path = "sub";
  140. DirCache cache = db.lockDirCache();
  141. DirCacheEditor editor = cache.editor();
  142. editor.add(new PathEdit(path) {
  143. public void apply(DirCacheEntry ent) {
  144. ent.setFileMode(FileMode.GITLINK);
  145. ent.setObjectId(id);
  146. }
  147. });
  148. editor.commit();
  149. String base = "git://server/repo.git";
  150. FileBasedConfig config = db.getConfig();
  151. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  152. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  153. base);
  154. config.save();
  155. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  156. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  157. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  158. ConfigConstants.CONFIG_KEY_PATH, path);
  159. String current = "git://server/repo.git";
  160. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  161. ConfigConstants.CONFIG_KEY_URL, current);
  162. modulesConfig.save();
  163. Repository subRepo = Git.cloneRepository()
  164. .setURI(db.getDirectory().toURI().toString())
  165. .setDirectory(new File(db.getWorkTree(), path)).call()
  166. .getRepository();
  167. assertNotNull(subRepo);
  168. addRepoToClose(subRepo);
  169. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  170. assertTrue(generator.next());
  171. assertNull(generator.getConfigUrl());
  172. assertEquals(current, generator.getModulesUrl());
  173. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  174. ConfigConstants.CONFIG_KEY_URL, "../sub.git");
  175. modulesConfig.save();
  176. SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
  177. Map<String, String> synced = command.call();
  178. assertNotNull(synced);
  179. assertEquals(1, synced.size());
  180. Entry<String, String> module = synced.entrySet().iterator().next();
  181. assertEquals(path, module.getKey());
  182. assertEquals("git://server/sub.git", module.getValue());
  183. generator = SubmoduleWalk.forIndex(db);
  184. assertTrue(generator.next());
  185. assertEquals("git://server/sub.git", generator.getConfigUrl());
  186. Repository subModRepository1 = generator.getRepository();
  187. StoredConfig submoduleConfig = subModRepository1.getConfig();
  188. subModRepository1.close();
  189. assertEquals("git://server/sub.git", submoduleConfig.getString(
  190. ConfigConstants.CONFIG_REMOTE_SECTION,
  191. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
  192. }
  193. }