Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DiffEntryTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2011, Dariusz Luksza <dariusz@luksza.org>
  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.diff;
  44. import static org.eclipse.jgit.diff.DiffEntry.DEV_NULL;
  45. import static org.eclipse.jgit.util.FileUtils.delete;
  46. import static org.hamcrest.CoreMatchers.is;
  47. import static org.hamcrest.CoreMatchers.notNullValue;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.junit.Assert.assertThat;
  50. import static org.junit.Assert.assertTrue;
  51. import java.io.File;
  52. import java.util.List;
  53. import org.eclipse.jgit.api.Git;
  54. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  55. import org.eclipse.jgit.dircache.DirCache;
  56. import org.eclipse.jgit.dircache.DirCacheEditor;
  57. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  58. import org.eclipse.jgit.dircache.DirCacheEntry;
  59. import org.eclipse.jgit.lib.FileMode;
  60. import org.eclipse.jgit.lib.RepositoryTestCase;
  61. import org.eclipse.jgit.revwalk.RevCommit;
  62. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  63. import org.eclipse.jgit.treewalk.FileTreeIterator;
  64. import org.eclipse.jgit.treewalk.TreeWalk;
  65. import org.eclipse.jgit.util.FileUtils;
  66. import org.junit.Test;
  67. public class DiffEntryTest extends RepositoryTestCase {
  68. @Test
  69. public void shouldListAddedFileInInitialCommit() throws Exception {
  70. // given
  71. writeTrashFile("a.txt", "content");
  72. Git git = new Git(db);
  73. git.add().addFilepattern("a.txt").call();
  74. RevCommit c = git.commit().setMessage("initial commit").call();
  75. // when
  76. TreeWalk walk = new TreeWalk(db);
  77. walk.addTree(new EmptyTreeIterator());
  78. walk.addTree(c.getTree());
  79. List<DiffEntry> result = DiffEntry.scan(walk);
  80. // then
  81. assertThat(result, notNullValue());
  82. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  83. DiffEntry entry = result.get(0);
  84. assertThat(entry.getChangeType(), is(ChangeType.ADD));
  85. assertThat(entry.getNewPath(), is("a.txt"));
  86. assertThat(entry.getOldPath(), is(DEV_NULL));
  87. }
  88. @Test
  89. public void shouldListAddedFileBetweenTwoCommits() throws Exception {
  90. // given
  91. Git git = new Git(db);
  92. RevCommit c1 = git.commit().setMessage("initial commit").call();
  93. writeTrashFile("a.txt", "content");
  94. git.add().addFilepattern("a.txt").call();
  95. RevCommit c2 = git.commit().setMessage("second commit").call();
  96. // when
  97. TreeWalk walk = new TreeWalk(db);
  98. walk.addTree(c1.getTree());
  99. walk.addTree(c2.getTree());
  100. List<DiffEntry> result = DiffEntry.scan(walk);
  101. // then
  102. assertThat(result, notNullValue());
  103. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  104. DiffEntry entry = result.get(0);
  105. assertThat(entry.getChangeType(), is(ChangeType.ADD));
  106. assertThat(entry.getNewPath(), is("a.txt"));
  107. assertThat(entry.getOldPath(), is(DEV_NULL));
  108. }
  109. @Test
  110. public void shouldListModificationBetweenTwoCommits() throws Exception {
  111. // given
  112. Git git = new Git(db);
  113. File file = writeTrashFile("a.txt", "content");
  114. git.add().addFilepattern("a.txt").call();
  115. RevCommit c1 = git.commit().setMessage("initial commit").call();
  116. write(file, "new content");
  117. RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
  118. .call();
  119. // when
  120. TreeWalk walk = new TreeWalk(db);
  121. walk.addTree(c1.getTree());
  122. walk.addTree(c2.getTree());
  123. List<DiffEntry> result = DiffEntry.scan(walk);
  124. // then
  125. assertThat(result, notNullValue());
  126. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  127. DiffEntry entry = result.get(0);
  128. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  129. assertThat(entry.getNewPath(), is("a.txt"));
  130. }
  131. @Test
  132. public void shouldListDeletionBetweenTwoCommits() throws Exception {
  133. // given
  134. Git git = new Git(db);
  135. File file = writeTrashFile("a.txt", "content");
  136. git.add().addFilepattern("a.txt").call();
  137. RevCommit c1 = git.commit().setMessage("initial commit").call();
  138. delete(file);
  139. RevCommit c2 = git.commit().setAll(true).setMessage("delete a.txt")
  140. .call();
  141. // when
  142. TreeWalk walk = new TreeWalk(db);
  143. walk.addTree(c1.getTree());
  144. walk.addTree(c2.getTree());
  145. List<DiffEntry> result = DiffEntry.scan(walk);
  146. // then
  147. assertThat(result, notNullValue());
  148. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  149. DiffEntry entry = result.get(0);
  150. assertThat(entry.getOldPath(), is("a.txt"));
  151. assertThat(entry.getNewPath(), is(DEV_NULL));
  152. assertThat(entry.getChangeType(), is(ChangeType.DELETE));
  153. }
  154. @Test
  155. public void shouldListModificationInDirWithoutModifiedTrees()
  156. throws Exception {
  157. // given
  158. Git git = new Git(db);
  159. File tree = new File(new File(db.getWorkTree(), "a"), "b");
  160. FileUtils.mkdirs(tree);
  161. File file = new File(tree, "c.txt");
  162. FileUtils.createNewFile(file);
  163. write(file, "content");
  164. git.add().addFilepattern("a").call();
  165. RevCommit c1 = git.commit().setMessage("initial commit").call();
  166. write(file, "new line");
  167. RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
  168. .call();
  169. // when
  170. TreeWalk walk = new TreeWalk(db);
  171. walk.addTree(c1.getTree());
  172. walk.addTree(c2.getTree());
  173. walk.setRecursive(true);
  174. List<DiffEntry> result = DiffEntry.scan(walk);
  175. // then
  176. assertThat(result, notNullValue());
  177. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  178. DiffEntry entry = result.get(0);
  179. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  180. assertThat(entry.getNewPath(), is("a/b/c.txt"));
  181. }
  182. @Test
  183. public void shouldListModificationInDirWithModifiedTrees() throws Exception {
  184. // given
  185. Git git = new Git(db);
  186. File tree = new File(new File(db.getWorkTree(), "a"), "b");
  187. FileUtils.mkdirs(tree);
  188. File file = new File(tree, "c.txt");
  189. FileUtils.createNewFile(file);
  190. write(file, "content");
  191. git.add().addFilepattern("a").call();
  192. RevCommit c1 = git.commit().setMessage("initial commit").call();
  193. write(file, "new line");
  194. RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
  195. .call();
  196. // when
  197. TreeWalk walk = new TreeWalk(db);
  198. walk.addTree(c1.getTree());
  199. walk.addTree(c2.getTree());
  200. List<DiffEntry> result = DiffEntry.scan(walk, true);
  201. // then
  202. assertThat(result, notNullValue());
  203. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(3)));
  204. DiffEntry entry = result.get(0);
  205. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  206. assertThat(entry.getNewPath(), is("a"));
  207. entry = result.get(1);
  208. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  209. assertThat(entry.getNewPath(), is("a/b"));
  210. entry = result.get(2);
  211. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  212. assertThat(entry.getNewPath(), is("a/b/c.txt"));
  213. }
  214. @Test
  215. public void shouldListChangesInWorkingTree() throws Exception {
  216. // given
  217. writeTrashFile("a.txt", "content");
  218. Git git = new Git(db);
  219. git.add().addFilepattern("a.txt").call();
  220. RevCommit c = git.commit().setMessage("initial commit").call();
  221. writeTrashFile("b.txt", "new line");
  222. // when
  223. TreeWalk walk = new TreeWalk(db);
  224. walk.addTree(c.getTree());
  225. walk.addTree(new FileTreeIterator(db));
  226. List<DiffEntry> result = DiffEntry.scan(walk, true);
  227. // then
  228. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  229. DiffEntry entry = result.get(0);
  230. assertThat(entry.getChangeType(), is(ChangeType.ADD));
  231. assertThat(entry.getNewPath(), is("b.txt"));
  232. }
  233. @Test(expected = IllegalArgumentException.class)
  234. public void shouldThrowIAEWhenTreeWalkHasLessThanTwoTrees()
  235. throws Exception {
  236. // given - we don't need anything here
  237. // when
  238. TreeWalk walk = new TreeWalk(db);
  239. walk.addTree(new EmptyTreeIterator());
  240. DiffEntry.scan(walk);
  241. }
  242. @Test(expected = IllegalArgumentException.class)
  243. public void shouldThrowIAEWhenTreeWalkHasMoreThanTwoTrees()
  244. throws Exception {
  245. // given - we don't need anything here
  246. // when
  247. TreeWalk walk = new TreeWalk(db);
  248. walk.addTree(new EmptyTreeIterator());
  249. walk.addTree(new EmptyTreeIterator());
  250. walk.addTree(new EmptyTreeIterator());
  251. DiffEntry.scan(walk);
  252. }
  253. @Test(expected = IllegalArgumentException.class)
  254. public void shouldThrowIAEWhenScanShouldIncludeTreesAndWalkIsRecursive()
  255. throws Exception {
  256. // given - we don't need anything here
  257. // when
  258. TreeWalk walk = new TreeWalk(db);
  259. walk.addTree(new EmptyTreeIterator());
  260. walk.addTree(new EmptyTreeIterator());
  261. walk.setRecursive(true);
  262. DiffEntry.scan(walk, true);
  263. }
  264. @Test
  265. public void shouldReportFileModeChange() throws Exception {
  266. writeTrashFile("a.txt", "content");
  267. Git git = new Git(db);
  268. git.add().addFilepattern("a.txt").call();
  269. RevCommit c1 = git.commit().setMessage("initial commit").call();
  270. DirCache cache = db.lockDirCache();
  271. DirCacheEditor editor = cache.editor();
  272. final TreeWalk walk = new TreeWalk(db);
  273. walk.addTree(c1.getTree());
  274. walk.setRecursive(true);
  275. assertTrue(walk.next());
  276. editor.add(new PathEdit("a.txt") {
  277. public void apply(DirCacheEntry ent) {
  278. ent.setFileMode(FileMode.EXECUTABLE_FILE);
  279. ent.setObjectId(walk.getObjectId(0));
  280. }
  281. });
  282. assertTrue(editor.commit());
  283. RevCommit c2 = git.commit().setMessage("second commit").call();
  284. walk.reset();
  285. walk.addTree(c1.getTree());
  286. walk.addTree(c2.getTree());
  287. List<DiffEntry> diffs = DiffEntry.scan(walk, false);
  288. assertEquals(1, diffs.size());
  289. DiffEntry diff = diffs.get(0);
  290. assertEquals(ChangeType.MODIFY,diff.getChangeType());
  291. assertEquals(diff.getOldId(), diff.getNewId());
  292. assertEquals("a.txt", diff.getOldPath());
  293. assertEquals(diff.getOldPath(), diff.getNewPath());
  294. assertEquals(FileMode.EXECUTABLE_FILE, diff.getNewMode());
  295. assertEquals(FileMode.REGULAR_FILE, diff.getOldMode());
  296. }
  297. }