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

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