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.

CommitCommandTest.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.api;
  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.util.List;
  49. import org.eclipse.jgit.diff.DiffEntry;
  50. import org.eclipse.jgit.lib.ConfigConstants;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.FileMode;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.RefUpdate;
  55. import org.eclipse.jgit.lib.RefUpdate.Result;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.lib.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.StoredConfig;
  59. import org.eclipse.jgit.revwalk.RevCommit;
  60. import org.eclipse.jgit.submodule.SubmoduleWalk;
  61. import org.eclipse.jgit.treewalk.TreeWalk;
  62. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  63. import org.eclipse.jgit.util.FS;
  64. import org.junit.Test;
  65. /**
  66. * Unit tests of {@link CommitCommand}
  67. */
  68. public class CommitCommandTest extends RepositoryTestCase {
  69. @Test
  70. public void testExecutableRetention() throws Exception {
  71. StoredConfig config = db.getConfig();
  72. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  73. ConfigConstants.CONFIG_KEY_FILEMODE, true);
  74. config.save();
  75. FS executableFs = new FS() {
  76. public boolean supportsExecute() {
  77. return true;
  78. }
  79. public boolean setExecute(File f, boolean canExec) {
  80. return true;
  81. }
  82. public ProcessBuilder runInShell(String cmd, String[] args) {
  83. return null;
  84. }
  85. public boolean retryFailedLockFileCommit() {
  86. return false;
  87. }
  88. public FS newInstance() {
  89. return this;
  90. }
  91. protected File discoverGitPrefix() {
  92. return null;
  93. }
  94. public boolean canExecute(File f) {
  95. return true;
  96. }
  97. };
  98. Git git = Git.open(db.getDirectory(), executableFs);
  99. String path = "a.txt";
  100. writeTrashFile(path, "content");
  101. git.add().addFilepattern(path).call();
  102. RevCommit commit1 = git.commit().setMessage("commit").call();
  103. TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree());
  104. assertNotNull(walk);
  105. assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
  106. FS nonExecutableFs = new FS() {
  107. public boolean supportsExecute() {
  108. return false;
  109. }
  110. public boolean setExecute(File f, boolean canExec) {
  111. return false;
  112. }
  113. public ProcessBuilder runInShell(String cmd, String[] args) {
  114. return null;
  115. }
  116. public boolean retryFailedLockFileCommit() {
  117. return false;
  118. }
  119. public FS newInstance() {
  120. return this;
  121. }
  122. protected File discoverGitPrefix() {
  123. return null;
  124. }
  125. public boolean canExecute(File f) {
  126. return false;
  127. }
  128. };
  129. config = db.getConfig();
  130. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  131. ConfigConstants.CONFIG_KEY_FILEMODE, false);
  132. config.save();
  133. Git git2 = Git.open(db.getDirectory(), nonExecutableFs);
  134. writeTrashFile(path, "content2");
  135. RevCommit commit2 = git2.commit().setOnly(path).setMessage("commit2")
  136. .call();
  137. walk = TreeWalk.forPath(db, path, commit2.getTree());
  138. assertNotNull(walk);
  139. assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
  140. }
  141. @Test
  142. public void commitNewSubmodule() throws Exception {
  143. Git git = new Git(db);
  144. writeTrashFile("file.txt", "content");
  145. git.add().addFilepattern("file.txt").call();
  146. RevCommit commit = git.commit().setMessage("create file").call();
  147. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  148. String path = "sub";
  149. command.setPath(path);
  150. String uri = db.getDirectory().toURI().toString();
  151. command.setURI(uri);
  152. Repository repo = command.call();
  153. assertNotNull(repo);
  154. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  155. assertTrue(generator.next());
  156. assertEquals(path, generator.getPath());
  157. assertEquals(commit, generator.getObjectId());
  158. assertEquals(uri, generator.getModulesUrl());
  159. assertEquals(path, generator.getModulesPath());
  160. assertEquals(uri, generator.getConfigUrl());
  161. assertNotNull(generator.getRepository());
  162. assertEquals(commit, repo.resolve(Constants.HEAD));
  163. RevCommit submoduleCommit = git.commit().setMessage("submodule add")
  164. .setOnly(path).call();
  165. assertNotNull(submoduleCommit);
  166. TreeWalk walk = new TreeWalk(db);
  167. walk.addTree(commit.getTree());
  168. walk.addTree(submoduleCommit.getTree());
  169. walk.setFilter(TreeFilter.ANY_DIFF);
  170. List<DiffEntry> diffs = DiffEntry.scan(walk);
  171. assertEquals(1, diffs.size());
  172. DiffEntry subDiff = diffs.get(0);
  173. assertEquals(FileMode.MISSING, subDiff.getOldMode());
  174. assertEquals(FileMode.GITLINK, subDiff.getNewMode());
  175. assertEquals(ObjectId.zeroId(), subDiff.getOldId().toObjectId());
  176. assertEquals(commit, subDiff.getNewId().toObjectId());
  177. assertEquals(path, subDiff.getNewPath());
  178. }
  179. @Test
  180. public void commitSubmoduleUpdate() throws Exception {
  181. Git git = new Git(db);
  182. writeTrashFile("file.txt", "content");
  183. git.add().addFilepattern("file.txt").call();
  184. RevCommit commit = git.commit().setMessage("create file").call();
  185. writeTrashFile("file.txt", "content2");
  186. git.add().addFilepattern("file.txt").call();
  187. RevCommit commit2 = git.commit().setMessage("edit file").call();
  188. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  189. String path = "sub";
  190. command.setPath(path);
  191. String uri = db.getDirectory().toURI().toString();
  192. command.setURI(uri);
  193. Repository repo = command.call();
  194. assertNotNull(repo);
  195. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  196. assertTrue(generator.next());
  197. assertEquals(path, generator.getPath());
  198. assertEquals(commit2, generator.getObjectId());
  199. assertEquals(uri, generator.getModulesUrl());
  200. assertEquals(path, generator.getModulesPath());
  201. assertEquals(uri, generator.getConfigUrl());
  202. assertNotNull(generator.getRepository());
  203. assertEquals(commit2, repo.resolve(Constants.HEAD));
  204. RevCommit submoduleAddCommit = git.commit().setMessage("submodule add")
  205. .setOnly(path).call();
  206. assertNotNull(submoduleAddCommit);
  207. RefUpdate update = repo.updateRef(Constants.HEAD);
  208. update.setNewObjectId(commit);
  209. assertEquals(Result.FORCED, update.forceUpdate());
  210. RevCommit submoduleEditCommit = git.commit()
  211. .setMessage("submodule add").setOnly(path).call();
  212. assertNotNull(submoduleEditCommit);
  213. TreeWalk walk = new TreeWalk(db);
  214. walk.addTree(submoduleAddCommit.getTree());
  215. walk.addTree(submoduleEditCommit.getTree());
  216. walk.setFilter(TreeFilter.ANY_DIFF);
  217. List<DiffEntry> diffs = DiffEntry.scan(walk);
  218. assertEquals(1, diffs.size());
  219. DiffEntry subDiff = diffs.get(0);
  220. assertEquals(FileMode.GITLINK, subDiff.getOldMode());
  221. assertEquals(FileMode.GITLINK, subDiff.getNewMode());
  222. assertEquals(commit2, subDiff.getOldId().toObjectId());
  223. assertEquals(commit, subDiff.getNewId().toObjectId());
  224. assertEquals(path, subDiff.getNewPath());
  225. assertEquals(path, subDiff.getOldPath());
  226. }
  227. }