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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (C) 2011-2012, 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.assertNull;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.File;
  49. import java.util.List;
  50. import org.eclipse.jgit.diff.DiffEntry;
  51. import org.eclipse.jgit.dircache.DirCache;
  52. import org.eclipse.jgit.lib.ConfigConstants;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.FileMode;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.RefUpdate;
  57. import org.eclipse.jgit.lib.RefUpdate.Result;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.lib.RepositoryTestCase;
  60. import org.eclipse.jgit.lib.StoredConfig;
  61. import org.eclipse.jgit.revwalk.RevCommit;
  62. import org.eclipse.jgit.submodule.SubmoduleWalk;
  63. import org.eclipse.jgit.treewalk.TreeWalk;
  64. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  65. import org.eclipse.jgit.util.FS;
  66. import org.junit.Test;
  67. /**
  68. * Unit tests of {@link CommitCommand}.
  69. */
  70. public class CommitCommandTest extends RepositoryTestCase {
  71. @Test
  72. public void testExecutableRetention() throws Exception {
  73. StoredConfig config = db.getConfig();
  74. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  75. ConfigConstants.CONFIG_KEY_FILEMODE, true);
  76. config.save();
  77. FS executableFs = new FS() {
  78. public boolean supportsExecute() {
  79. return true;
  80. }
  81. public boolean setExecute(File f, boolean canExec) {
  82. return true;
  83. }
  84. public ProcessBuilder runInShell(String cmd, String[] args) {
  85. return null;
  86. }
  87. public boolean retryFailedLockFileCommit() {
  88. return false;
  89. }
  90. public FS newInstance() {
  91. return this;
  92. }
  93. protected File discoverGitPrefix() {
  94. return null;
  95. }
  96. public boolean canExecute(File f) {
  97. return true;
  98. }
  99. @Override
  100. public boolean isCaseSensitive() {
  101. return true;
  102. }
  103. };
  104. Git git = Git.open(db.getDirectory(), executableFs);
  105. String path = "a.txt";
  106. writeTrashFile(path, "content");
  107. git.add().addFilepattern(path).call();
  108. RevCommit commit1 = git.commit().setMessage("commit").call();
  109. TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree());
  110. assertNotNull(walk);
  111. assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
  112. FS nonExecutableFs = new FS() {
  113. public boolean supportsExecute() {
  114. return false;
  115. }
  116. public boolean setExecute(File f, boolean canExec) {
  117. return false;
  118. }
  119. public ProcessBuilder runInShell(String cmd, String[] args) {
  120. return null;
  121. }
  122. public boolean retryFailedLockFileCommit() {
  123. return false;
  124. }
  125. public FS newInstance() {
  126. return this;
  127. }
  128. protected File discoverGitPrefix() {
  129. return null;
  130. }
  131. public boolean canExecute(File f) {
  132. return false;
  133. }
  134. @Override
  135. public boolean isCaseSensitive() {
  136. return true;
  137. }
  138. };
  139. config = db.getConfig();
  140. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  141. ConfigConstants.CONFIG_KEY_FILEMODE, false);
  142. config.save();
  143. Git git2 = Git.open(db.getDirectory(), nonExecutableFs);
  144. writeTrashFile(path, "content2");
  145. RevCommit commit2 = git2.commit().setOnly(path).setMessage("commit2")
  146. .call();
  147. walk = TreeWalk.forPath(db, path, commit2.getTree());
  148. assertNotNull(walk);
  149. assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
  150. }
  151. @Test
  152. public void commitNewSubmodule() throws Exception {
  153. Git git = new Git(db);
  154. writeTrashFile("file.txt", "content");
  155. git.add().addFilepattern("file.txt").call();
  156. RevCommit commit = git.commit().setMessage("create file").call();
  157. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  158. String path = "sub";
  159. command.setPath(path);
  160. String uri = db.getDirectory().toURI().toString();
  161. command.setURI(uri);
  162. Repository repo = command.call();
  163. assertNotNull(repo);
  164. addRepoToClose(repo);
  165. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  166. assertTrue(generator.next());
  167. assertEquals(path, generator.getPath());
  168. assertEquals(commit, generator.getObjectId());
  169. assertEquals(uri, generator.getModulesUrl());
  170. assertEquals(path, generator.getModulesPath());
  171. assertEquals(uri, generator.getConfigUrl());
  172. Repository subModRepo = generator.getRepository();
  173. addRepoToClose(subModRepo);
  174. assertNotNull(subModRepo);
  175. assertEquals(commit, repo.resolve(Constants.HEAD));
  176. RevCommit submoduleCommit = git.commit().setMessage("submodule add")
  177. .setOnly(path).call();
  178. assertNotNull(submoduleCommit);
  179. TreeWalk walk = new TreeWalk(db);
  180. walk.addTree(commit.getTree());
  181. walk.addTree(submoduleCommit.getTree());
  182. walk.setFilter(TreeFilter.ANY_DIFF);
  183. List<DiffEntry> diffs = DiffEntry.scan(walk);
  184. assertEquals(1, diffs.size());
  185. DiffEntry subDiff = diffs.get(0);
  186. assertEquals(FileMode.MISSING, subDiff.getOldMode());
  187. assertEquals(FileMode.GITLINK, subDiff.getNewMode());
  188. assertEquals(ObjectId.zeroId(), subDiff.getOldId().toObjectId());
  189. assertEquals(commit, subDiff.getNewId().toObjectId());
  190. assertEquals(path, subDiff.getNewPath());
  191. }
  192. @Test
  193. public void commitSubmoduleUpdate() throws Exception {
  194. Git git = new Git(db);
  195. writeTrashFile("file.txt", "content");
  196. git.add().addFilepattern("file.txt").call();
  197. RevCommit commit = git.commit().setMessage("create file").call();
  198. writeTrashFile("file.txt", "content2");
  199. git.add().addFilepattern("file.txt").call();
  200. RevCommit commit2 = git.commit().setMessage("edit file").call();
  201. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  202. String path = "sub";
  203. command.setPath(path);
  204. String uri = db.getDirectory().toURI().toString();
  205. command.setURI(uri);
  206. Repository repo = command.call();
  207. assertNotNull(repo);
  208. addRepoToClose(repo);
  209. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  210. assertTrue(generator.next());
  211. assertEquals(path, generator.getPath());
  212. assertEquals(commit2, generator.getObjectId());
  213. assertEquals(uri, generator.getModulesUrl());
  214. assertEquals(path, generator.getModulesPath());
  215. assertEquals(uri, generator.getConfigUrl());
  216. Repository subModRepo = generator.getRepository();
  217. addRepoToClose(subModRepo);
  218. assertNotNull(subModRepo);
  219. assertEquals(commit2, repo.resolve(Constants.HEAD));
  220. RevCommit submoduleAddCommit = git.commit().setMessage("submodule add")
  221. .setOnly(path).call();
  222. assertNotNull(submoduleAddCommit);
  223. RefUpdate update = repo.updateRef(Constants.HEAD);
  224. update.setNewObjectId(commit);
  225. assertEquals(Result.FORCED, update.forceUpdate());
  226. RevCommit submoduleEditCommit = git.commit()
  227. .setMessage("submodule add").setOnly(path).call();
  228. assertNotNull(submoduleEditCommit);
  229. TreeWalk walk = new TreeWalk(db);
  230. walk.addTree(submoduleAddCommit.getTree());
  231. walk.addTree(submoduleEditCommit.getTree());
  232. walk.setFilter(TreeFilter.ANY_DIFF);
  233. List<DiffEntry> diffs = DiffEntry.scan(walk);
  234. assertEquals(1, diffs.size());
  235. DiffEntry subDiff = diffs.get(0);
  236. assertEquals(FileMode.GITLINK, subDiff.getOldMode());
  237. assertEquals(FileMode.GITLINK, subDiff.getNewMode());
  238. assertEquals(commit2, subDiff.getOldId().toObjectId());
  239. assertEquals(commit, subDiff.getNewId().toObjectId());
  240. assertEquals(path, subDiff.getNewPath());
  241. assertEquals(path, subDiff.getOldPath());
  242. }
  243. @Test
  244. public void commitUpdatesSmudgedEntries() throws Exception {
  245. Git git = new Git(db);
  246. File file1 = writeTrashFile("file1.txt", "content1");
  247. assertTrue(file1.setLastModified(file1.lastModified() - 5000));
  248. File file2 = writeTrashFile("file2.txt", "content2");
  249. assertTrue(file2.setLastModified(file2.lastModified() - 5000));
  250. File file3 = writeTrashFile("file3.txt", "content3");
  251. assertTrue(file3.setLastModified(file3.lastModified() - 5000));
  252. assertNotNull(git.add().addFilepattern("file1.txt")
  253. .addFilepattern("file2.txt").addFilepattern("file3.txt").call());
  254. RevCommit commit = git.commit().setMessage("add files").call();
  255. assertNotNull(commit);
  256. DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
  257. int file1Size = cache.getEntry("file1.txt").getLength();
  258. int file2Size = cache.getEntry("file2.txt").getLength();
  259. int file3Size = cache.getEntry("file3.txt").getLength();
  260. ObjectId file2Id = cache.getEntry("file2.txt").getObjectId();
  261. ObjectId file3Id = cache.getEntry("file3.txt").getObjectId();
  262. assertTrue(file1Size > 0);
  263. assertTrue(file2Size > 0);
  264. assertTrue(file3Size > 0);
  265. // Smudge entries
  266. cache = DirCache.lock(db.getIndexFile(), db.getFS());
  267. cache.getEntry("file1.txt").setLength(0);
  268. cache.getEntry("file2.txt").setLength(0);
  269. cache.getEntry("file3.txt").setLength(0);
  270. cache.write();
  271. assertTrue(cache.commit());
  272. // Verify entries smudged
  273. cache = DirCache.read(db.getIndexFile(), db.getFS());
  274. assertEquals(0, cache.getEntry("file1.txt").getLength());
  275. assertEquals(0, cache.getEntry("file2.txt").getLength());
  276. assertEquals(0, cache.getEntry("file3.txt").getLength());
  277. long indexTime = db.getIndexFile().lastModified();
  278. db.getIndexFile().setLastModified(indexTime - 5000);
  279. write(file1, "content4");
  280. assertTrue(file1.setLastModified(file1.lastModified() + 1000));
  281. assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
  282. .call());
  283. cache = db.readDirCache();
  284. assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
  285. assertEquals(file2Size, cache.getEntry("file2.txt").getLength());
  286. assertEquals(file3Size, cache.getEntry("file3.txt").getLength());
  287. assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId());
  288. assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId());
  289. }
  290. @Test
  291. public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception {
  292. Git git = new Git(db);
  293. File file1 = writeTrashFile("file1.txt", "content1");
  294. assertTrue(file1.setLastModified(file1.lastModified() - 5000));
  295. File file2 = writeTrashFile("file2.txt", "content2");
  296. assertTrue(file2.setLastModified(file2.lastModified() - 5000));
  297. assertNotNull(git.add().addFilepattern("file1.txt")
  298. .addFilepattern("file2.txt").call());
  299. RevCommit commit = git.commit().setMessage("add files").call();
  300. assertNotNull(commit);
  301. DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
  302. int file1Size = cache.getEntry("file1.txt").getLength();
  303. int file2Size = cache.getEntry("file2.txt").getLength();
  304. assertTrue(file1Size > 0);
  305. assertTrue(file2Size > 0);
  306. writeTrashFile("file2.txt", "content3");
  307. assertNotNull(git.add().addFilepattern("file2.txt").call());
  308. writeTrashFile("file2.txt", "content4");
  309. // Smudge entries
  310. cache = DirCache.lock(db.getIndexFile(), db.getFS());
  311. cache.getEntry("file1.txt").setLength(0);
  312. cache.getEntry("file2.txt").setLength(0);
  313. cache.write();
  314. assertTrue(cache.commit());
  315. // Verify entries smudged
  316. cache = db.readDirCache();
  317. assertEquals(0, cache.getEntry("file1.txt").getLength());
  318. assertEquals(0, cache.getEntry("file2.txt").getLength());
  319. long indexTime = db.getIndexFile().lastModified();
  320. db.getIndexFile().setLastModified(indexTime - 5000);
  321. write(file1, "content5");
  322. assertTrue(file1.setLastModified(file1.lastModified() + 1000));
  323. assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
  324. .call());
  325. cache = db.readDirCache();
  326. assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
  327. assertEquals(0, cache.getEntry("file2.txt").getLength());
  328. }
  329. @Test
  330. public void commitAfterSquashMerge() throws Exception {
  331. Git git = new Git(db);
  332. writeTrashFile("file1", "file1");
  333. git.add().addFilepattern("file1").call();
  334. RevCommit first = git.commit().setMessage("initial commit").call();
  335. assertTrue(new File(db.getWorkTree(), "file1").exists());
  336. createBranch(first, "refs/heads/branch1");
  337. checkoutBranch("refs/heads/branch1");
  338. writeTrashFile("file2", "file2");
  339. git.add().addFilepattern("file2").call();
  340. git.commit().setMessage("second commit").call();
  341. assertTrue(new File(db.getWorkTree(), "file2").exists());
  342. checkoutBranch("refs/heads/master");
  343. MergeResult result = git.merge().include(db.getRef("branch1"))
  344. .setSquash(true).call();
  345. assertTrue(new File(db.getWorkTree(), "file1").exists());
  346. assertTrue(new File(db.getWorkTree(), "file2").exists());
  347. assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED,
  348. result.getMergeStatus());
  349. // comment not set, should be inferred from SQUASH_MSG
  350. RevCommit squashedCommit = git.commit().call();
  351. assertEquals(1, squashedCommit.getParentCount());
  352. assertNull(db.readSquashCommitMsg());
  353. assertEquals("commit: Squashed commit of the following:", db
  354. .getReflogReader(Constants.HEAD).getLastEntry().getComment());
  355. assertEquals("commit: Squashed commit of the following:", db
  356. .getReflogReader(db.getBranch()).getLastEntry().getComment());
  357. }
  358. }