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.

SubmoduleUpdateTest.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.assertTrue;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.util.Collection;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.api.SubmoduleUpdateCommand;
  52. import org.eclipse.jgit.dircache.DirCache;
  53. import org.eclipse.jgit.dircache.DirCacheEditor;
  54. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  55. import org.eclipse.jgit.dircache.DirCacheEntry;
  56. import org.eclipse.jgit.lib.ConfigConstants;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.FileMode;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.lib.RepositoryTestCase;
  62. import org.eclipse.jgit.lib.StoredConfig;
  63. import org.eclipse.jgit.revwalk.RevCommit;
  64. import org.eclipse.jgit.storage.file.FileBasedConfig;
  65. import org.junit.Test;
  66. /**
  67. * Unit tests of {@link SubmoduleUpdateCommand}
  68. */
  69. public class SubmoduleUpdateTest extends RepositoryTestCase {
  70. @Test
  71. public void repositoryWithNoSubmodules() {
  72. SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
  73. Collection<String> modules = command.call();
  74. assertNotNull(modules);
  75. assertTrue(modules.isEmpty());
  76. }
  77. @Test
  78. public void repositoryWithSubmodule() throws Exception {
  79. writeTrashFile("file.txt", "content");
  80. Git git = Git.wrap(db);
  81. git.add().addFilepattern("file.txt").call();
  82. final RevCommit commit = git.commit().setMessage("create file").call();
  83. final String path = "sub";
  84. DirCache cache = db.lockDirCache();
  85. DirCacheEditor editor = cache.editor();
  86. editor.add(new PathEdit(path) {
  87. public void apply(DirCacheEntry ent) {
  88. ent.setFileMode(FileMode.GITLINK);
  89. ent.setObjectId(commit);
  90. }
  91. });
  92. editor.commit();
  93. StoredConfig config = db.getConfig();
  94. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  95. ConfigConstants.CONFIG_KEY_URL, db.getDirectory().toURI()
  96. .toString());
  97. config.save();
  98. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  99. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  100. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  101. ConfigConstants.CONFIG_KEY_PATH, path);
  102. modulesConfig.save();
  103. SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
  104. Collection<String> updated = command.call();
  105. assertNotNull(updated);
  106. assertEquals(1, updated.size());
  107. assertEquals(path, updated.iterator().next());
  108. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  109. assertTrue(generator.next());
  110. Repository subRepo = generator.getRepository();
  111. assertNotNull(subRepo);
  112. assertEquals(commit, subRepo.resolve(Constants.HEAD));
  113. }
  114. @Test
  115. public void repositoryWithUnconfiguredSubmodule() throws IOException {
  116. final ObjectId id = ObjectId
  117. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  118. final String path = "sub";
  119. DirCache cache = db.lockDirCache();
  120. DirCacheEditor editor = cache.editor();
  121. editor.add(new PathEdit(path) {
  122. public void apply(DirCacheEntry ent) {
  123. ent.setFileMode(FileMode.GITLINK);
  124. ent.setObjectId(id);
  125. }
  126. });
  127. editor.commit();
  128. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  129. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  130. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  131. ConfigConstants.CONFIG_KEY_PATH, path);
  132. String url = "git://server/repo.git";
  133. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  134. ConfigConstants.CONFIG_KEY_URL, url);
  135. String update = "rebase";
  136. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  137. ConfigConstants.CONFIG_KEY_UPDATE, update);
  138. modulesConfig.save();
  139. SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
  140. Collection<String> updated = command.call();
  141. assertNotNull(updated);
  142. assertTrue(updated.isEmpty());
  143. }
  144. @Test
  145. public void repositoryWithInitializedSubmodule() throws IOException {
  146. final ObjectId id = ObjectId
  147. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  148. final String path = "sub";
  149. DirCache cache = db.lockDirCache();
  150. DirCacheEditor editor = cache.editor();
  151. editor.add(new PathEdit(path) {
  152. public void apply(DirCacheEntry ent) {
  153. ent.setFileMode(FileMode.GITLINK);
  154. ent.setObjectId(id);
  155. }
  156. });
  157. editor.commit();
  158. Repository subRepo = Git.init().setBare(false)
  159. .setDirectory(new File(db.getWorkTree(), path)).call()
  160. .getRepository();
  161. assertNotNull(subRepo);
  162. SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
  163. Collection<String> updated = command.call();
  164. assertNotNull(updated);
  165. assertTrue(updated.isEmpty());
  166. }
  167. }