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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2011, GitHub Inc. 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.submodule;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.util.Collection;
  17. import org.eclipse.jgit.api.Git;
  18. import org.eclipse.jgit.api.SubmoduleUpdateCommand;
  19. import org.eclipse.jgit.api.errors.GitAPIException;
  20. import org.eclipse.jgit.dircache.DirCache;
  21. import org.eclipse.jgit.dircache.DirCacheEditor;
  22. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  23. import org.eclipse.jgit.dircache.DirCacheEntry;
  24. import org.eclipse.jgit.junit.RepositoryTestCase;
  25. import org.eclipse.jgit.lib.ConfigConstants;
  26. import org.eclipse.jgit.lib.Constants;
  27. import org.eclipse.jgit.lib.FileMode;
  28. import org.eclipse.jgit.lib.ObjectId;
  29. import org.eclipse.jgit.lib.Repository;
  30. import org.eclipse.jgit.lib.StoredConfig;
  31. import org.eclipse.jgit.revwalk.RevCommit;
  32. import org.eclipse.jgit.storage.file.FileBasedConfig;
  33. import org.junit.Test;
  34. /**
  35. * Unit tests of {@link SubmoduleUpdateCommand}
  36. */
  37. public class SubmoduleUpdateTest extends RepositoryTestCase {
  38. @Test
  39. public void repositoryWithNoSubmodules() throws GitAPIException {
  40. SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
  41. Collection<String> modules = command.call();
  42. assertNotNull(modules);
  43. assertTrue(modules.isEmpty());
  44. }
  45. @Test
  46. public void repositoryWithSubmodule() throws Exception {
  47. writeTrashFile("file.txt", "content");
  48. Git git = Git.wrap(db);
  49. git.add().addFilepattern("file.txt").call();
  50. final RevCommit commit = git.commit().setMessage("create file").call();
  51. final String path = "sub";
  52. DirCache cache = db.lockDirCache();
  53. DirCacheEditor editor = cache.editor();
  54. editor.add(new PathEdit(path) {
  55. @Override
  56. public void apply(DirCacheEntry ent) {
  57. ent.setFileMode(FileMode.GITLINK);
  58. ent.setObjectId(commit);
  59. }
  60. });
  61. editor.commit();
  62. StoredConfig config = db.getConfig();
  63. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  64. ConfigConstants.CONFIG_KEY_URL, db.getDirectory().toURI()
  65. .toString());
  66. config.save();
  67. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  68. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  69. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  70. ConfigConstants.CONFIG_KEY_PATH, path);
  71. modulesConfig.save();
  72. SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
  73. Collection<String> updated = command.call();
  74. assertNotNull(updated);
  75. assertEquals(1, updated.size());
  76. assertEquals(path, updated.iterator().next());
  77. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  78. assertTrue(generator.next());
  79. try (Repository subRepo = generator.getRepository()) {
  80. assertNotNull(subRepo);
  81. assertEquals(commit, subRepo.resolve(Constants.HEAD));
  82. }
  83. }
  84. }
  85. @Test
  86. public void repositoryWithUnconfiguredSubmodule() throws IOException,
  87. GitAPIException {
  88. final ObjectId id = ObjectId
  89. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  90. final String path = "sub";
  91. DirCache cache = db.lockDirCache();
  92. DirCacheEditor editor = cache.editor();
  93. editor.add(new PathEdit(path) {
  94. @Override
  95. public void apply(DirCacheEntry ent) {
  96. ent.setFileMode(FileMode.GITLINK);
  97. ent.setObjectId(id);
  98. }
  99. });
  100. editor.commit();
  101. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  102. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  103. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  104. ConfigConstants.CONFIG_KEY_PATH, path);
  105. String url = "git://server/repo.git";
  106. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  107. ConfigConstants.CONFIG_KEY_URL, url);
  108. String update = "rebase";
  109. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  110. ConfigConstants.CONFIG_KEY_UPDATE, update);
  111. modulesConfig.save();
  112. SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
  113. Collection<String> updated = command.call();
  114. assertNotNull(updated);
  115. assertTrue(updated.isEmpty());
  116. }
  117. @Test
  118. public void repositoryWithInitializedSubmodule() throws IOException,
  119. GitAPIException {
  120. final ObjectId id = ObjectId
  121. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  122. final String path = "sub";
  123. DirCache cache = db.lockDirCache();
  124. DirCacheEditor editor = cache.editor();
  125. editor.add(new PathEdit(path) {
  126. @Override
  127. public void apply(DirCacheEntry ent) {
  128. ent.setFileMode(FileMode.GITLINK);
  129. ent.setObjectId(id);
  130. }
  131. });
  132. editor.commit();
  133. try (Repository subRepo = Git.init().setBare(false)
  134. .setDirectory(new File(db.getWorkTree(), path)).call()
  135. .getRepository()) {
  136. assertNotNull(subRepo);
  137. }
  138. SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
  139. Collection<String> updated = command.call();
  140. assertNotNull(updated);
  141. assertTrue(updated.isEmpty());
  142. }
  143. }