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.

DiffEntryTest.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.assertThat;
  49. import java.io.File;
  50. import java.util.List;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  53. import org.eclipse.jgit.lib.RepositoryTestCase;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  56. import org.eclipse.jgit.treewalk.FileTreeIterator;
  57. import org.eclipse.jgit.treewalk.TreeWalk;
  58. import org.eclipse.jgit.util.FileUtils;
  59. import org.junit.Test;
  60. public class DiffEntryTest extends RepositoryTestCase {
  61. @Test
  62. public void shouldListAddedFileInInitialCommit() throws Exception {
  63. // given
  64. writeTrashFile("a.txt", "content");
  65. Git git = new Git(db);
  66. git.add().addFilepattern("a.txt").call();
  67. RevCommit c = git.commit().setMessage("initial commit").call();
  68. // when
  69. TreeWalk walk = new TreeWalk(db);
  70. walk.addTree(new EmptyTreeIterator());
  71. walk.addTree(c.getTree());
  72. List<DiffEntry> result = DiffEntry.scan(walk);
  73. // then
  74. assertThat(result, notNullValue());
  75. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  76. DiffEntry entry = result.get(0);
  77. assertThat(entry.getChangeType(), is(ChangeType.ADD));
  78. assertThat(entry.getNewPath(), is("a.txt"));
  79. assertThat(entry.getOldPath(), is(DEV_NULL));
  80. }
  81. @Test
  82. public void shouldListAddedFileBetweenTwoCommits() throws Exception {
  83. // given
  84. Git git = new Git(db);
  85. RevCommit c1 = git.commit().setMessage("initial commit").call();
  86. writeTrashFile("a.txt", "content");
  87. git.add().addFilepattern("a.txt").call();
  88. RevCommit c2 = git.commit().setMessage("second commit").call();
  89. // when
  90. TreeWalk walk = new TreeWalk(db);
  91. walk.addTree(c1.getTree());
  92. walk.addTree(c2.getTree());
  93. List<DiffEntry> result = DiffEntry.scan(walk);
  94. // then
  95. assertThat(result, notNullValue());
  96. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  97. DiffEntry entry = result.get(0);
  98. assertThat(entry.getChangeType(), is(ChangeType.ADD));
  99. assertThat(entry.getNewPath(), is("a.txt"));
  100. assertThat(entry.getOldPath(), is(DEV_NULL));
  101. }
  102. @Test
  103. public void shouldListModificationBetweenTwoCommits() throws Exception {
  104. // given
  105. Git git = new Git(db);
  106. File file = writeTrashFile("a.txt", "content");
  107. git.add().addFilepattern("a.txt").call();
  108. RevCommit c1 = git.commit().setMessage("initial commit").call();
  109. write(file, "new content");
  110. RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
  111. .call();
  112. // when
  113. TreeWalk walk = new TreeWalk(db);
  114. walk.addTree(c1.getTree());
  115. walk.addTree(c2.getTree());
  116. List<DiffEntry> result = DiffEntry.scan(walk);
  117. // then
  118. assertThat(result, notNullValue());
  119. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  120. DiffEntry entry = result.get(0);
  121. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  122. assertThat(entry.getNewPath(), is("a.txt"));
  123. }
  124. @Test
  125. public void shouldListDeletionBetweenTwoCommits() throws Exception {
  126. // given
  127. Git git = new Git(db);
  128. File file = writeTrashFile("a.txt", "content");
  129. git.add().addFilepattern("a.txt").call();
  130. RevCommit c1 = git.commit().setMessage("initial commit").call();
  131. delete(file);
  132. RevCommit c2 = git.commit().setAll(true).setMessage("delete a.txt")
  133. .call();
  134. // when
  135. TreeWalk walk = new TreeWalk(db);
  136. walk.addTree(c1.getTree());
  137. walk.addTree(c2.getTree());
  138. List<DiffEntry> result = DiffEntry.scan(walk);
  139. // then
  140. assertThat(result, notNullValue());
  141. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  142. DiffEntry entry = result.get(0);
  143. assertThat(entry.getOldPath(), is("a.txt"));
  144. assertThat(entry.getNewPath(), is(DEV_NULL));
  145. assertThat(entry.getChangeType(), is(ChangeType.DELETE));
  146. }
  147. @Test
  148. public void shouldListModificationInDirWithoutModifiedTrees()
  149. throws Exception {
  150. // given
  151. Git git = new Git(db);
  152. File tree = new File(new File(db.getWorkTree(), "a"), "b");
  153. FileUtils.mkdirs(tree);
  154. File file = new File(tree, "c.txt");
  155. FileUtils.createNewFile(file);
  156. write(file, "content");
  157. git.add().addFilepattern("a").call();
  158. RevCommit c1 = git.commit().setMessage("initial commit").call();
  159. write(file, "new line");
  160. RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
  161. .call();
  162. // when
  163. TreeWalk walk = new TreeWalk(db);
  164. walk.addTree(c1.getTree());
  165. walk.addTree(c2.getTree());
  166. walk.setRecursive(true);
  167. List<DiffEntry> result = DiffEntry.scan(walk);
  168. // then
  169. assertThat(result, notNullValue());
  170. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  171. DiffEntry entry = result.get(0);
  172. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  173. assertThat(entry.getNewPath(), is("a/b/c.txt"));
  174. }
  175. @Test
  176. public void shouldListModificationInDirWithModifiedTrees() throws Exception {
  177. // given
  178. Git git = new Git(db);
  179. File tree = new File(new File(db.getWorkTree(), "a"), "b");
  180. FileUtils.mkdirs(tree);
  181. File file = new File(tree, "c.txt");
  182. FileUtils.createNewFile(file);
  183. write(file, "content");
  184. git.add().addFilepattern("a").call();
  185. RevCommit c1 = git.commit().setMessage("initial commit").call();
  186. write(file, "new line");
  187. RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
  188. .call();
  189. // when
  190. TreeWalk walk = new TreeWalk(db);
  191. walk.addTree(c1.getTree());
  192. walk.addTree(c2.getTree());
  193. List<DiffEntry> result = DiffEntry.scan(walk, true);
  194. // then
  195. assertThat(result, notNullValue());
  196. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(3)));
  197. DiffEntry entry = result.get(0);
  198. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  199. assertThat(entry.getNewPath(), is("a"));
  200. entry = result.get(1);
  201. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  202. assertThat(entry.getNewPath(), is("a/b"));
  203. entry = result.get(2);
  204. assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
  205. assertThat(entry.getNewPath(), is("a/b/c.txt"));
  206. }
  207. @Test
  208. public void shouldListChangesInWorkingTree() throws Exception {
  209. // given
  210. writeTrashFile("a.txt", "content");
  211. Git git = new Git(db);
  212. git.add().addFilepattern("a.txt").call();
  213. RevCommit c = git.commit().setMessage("initial commit").call();
  214. writeTrashFile("b.txt", "new line");
  215. // when
  216. TreeWalk walk = new TreeWalk(db);
  217. walk.addTree(c.getTree());
  218. walk.addTree(new FileTreeIterator(db));
  219. List<DiffEntry> result = DiffEntry.scan(walk, true);
  220. // then
  221. assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
  222. DiffEntry entry = result.get(0);
  223. assertThat(entry.getChangeType(), is(ChangeType.ADD));
  224. assertThat(entry.getNewPath(), is("b.txt"));
  225. }
  226. @Test(expected = IllegalArgumentException.class)
  227. public void shouldThrowIAEWhenTreeWalkHasLessThanTwoTrees()
  228. throws Exception {
  229. // given - we don't need anything here
  230. // when
  231. TreeWalk walk = new TreeWalk(db);
  232. walk.addTree(new EmptyTreeIterator());
  233. DiffEntry.scan(walk);
  234. }
  235. @Test(expected = IllegalArgumentException.class)
  236. public void shouldThrowIAEWhenTreeWalkHasMoreThanTwoTrees()
  237. throws Exception {
  238. // given - we don't need anything here
  239. // when
  240. TreeWalk walk = new TreeWalk(db);
  241. walk.addTree(new EmptyTreeIterator());
  242. walk.addTree(new EmptyTreeIterator());
  243. walk.addTree(new EmptyTreeIterator());
  244. DiffEntry.scan(walk);
  245. }
  246. @Test(expected = IllegalArgumentException.class)
  247. public void shouldThrowIAEWhenScanShouldIncludeTreesAndWalkIsRecursive()
  248. throws Exception {
  249. // given - we don't need anything here
  250. // when
  251. TreeWalk walk = new TreeWalk(db);
  252. walk.addTree(new EmptyTreeIterator());
  253. walk.addTree(new EmptyTreeIterator());
  254. walk.setRecursive(true);
  255. DiffEntry.scan(walk, true);
  256. }
  257. }