1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.api;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48
49 import java.io.ByteArrayOutputStream;
50 import java.io.File;
51 import java.io.IOException;
52 import java.io.OutputStream;
53 import java.util.List;
54
55 import org.eclipse.jgit.diff.DiffEntry;
56 import org.eclipse.jgit.diff.DiffEntry.ChangeType;
57 import org.eclipse.jgit.junit.RepositoryTestCase;
58 import org.eclipse.jgit.lib.ObjectId;
59 import org.eclipse.jgit.lib.ObjectReader;
60 import org.eclipse.jgit.revwalk.RevWalk;
61 import org.eclipse.jgit.treewalk.AbstractTreeIterator;
62 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
63 import org.eclipse.jgit.treewalk.filter.PathFilter;
64 import org.junit.Test;
65
66 public class DiffCommandTest extends RepositoryTestCase {
67 @Test
68 public void testDiffModified() throws Exception {
69 write(new File(db.getWorkTree(), "test.txt"), "test");
70 File folder = new File(db.getWorkTree(), "folder");
71 folder.mkdir();
72 write(new File(folder, "folder.txt"), "folder");
73 try (Git git = new Git(db)) {
74 git.add().addFilepattern(".").call();
75 git.commit().setMessage("Initial commit").call();
76 write(new File(folder, "folder.txt"), "folder change");
77
78 OutputStream out = new ByteArrayOutputStream();
79 List<DiffEntry> entries = git.diff().setOutputStream(out).call();
80 assertEquals(1, entries.size());
81 assertEquals(ChangeType.MODIFY, entries.get(0)
82 .getChangeType());
83 assertEquals("folder/folder.txt", entries.get(0)
84 .getOldPath());
85 assertEquals("folder/folder.txt", entries.get(0)
86 .getNewPath());
87
88 String actual = out.toString();
89 String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
90 + "index 0119635..95c4c65 100644\n"
91 + "--- a/folder/folder.txt\n"
92 + "+++ b/folder/folder.txt\n"
93 + "@@ -1 +1 @@\n"
94 + "-folder\n"
95 + "\\ No newline at end of file\n"
96 + "+folder change\n"
97 + "\\ No newline at end of file\n";
98 assertEquals(expected, actual);
99 }
100 }
101
102 @Test
103 public void testDiffCached() throws Exception {
104 write(new File(db.getWorkTree(), "test.txt"), "test");
105 File folder = new File(db.getWorkTree(), "folder");
106 folder.mkdir();
107 try (Git git = new Git(db)) {
108 git.add().addFilepattern(".").call();
109 git.commit().setMessage("Initial commit").call();
110 write(new File(folder, "folder.txt"), "folder");
111 git.add().addFilepattern(".").call();
112
113 OutputStream out = new ByteArrayOutputStream();
114 List<DiffEntry> entries = git.diff().setOutputStream(out)
115 .setCached(true).call();
116 assertEquals(1, entries.size());
117 assertEquals(ChangeType.ADD, entries.get(0)
118 .getChangeType());
119 assertEquals("/dev/null", entries.get(0)
120 .getOldPath());
121 assertEquals("folder/folder.txt", entries.get(0)
122 .getNewPath());
123
124 String actual = out.toString();
125 String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
126 + "new file mode 100644\n"
127 + "index 0000000..0119635\n"
128 + "--- /dev/null\n"
129 + "+++ b/folder/folder.txt\n"
130 + "@@ -0,0 +1 @@\n"
131 + "+folder\n"
132 + "\\ No newline at end of file\n";
133 assertEquals(expected, actual);
134 }
135 }
136
137 @Test
138 public void testDiffTwoCommits() throws Exception {
139 write(new File(db.getWorkTree(), "test.txt"), "test");
140 File folder = new File(db.getWorkTree(), "folder");
141 folder.mkdir();
142 write(new File(folder, "folder.txt"), "folder");
143 try (Git git = new Git(db)) {
144 git.add().addFilepattern(".").call();
145 git.commit().setMessage("Initial commit").call();
146 write(new File(folder, "folder.txt"), "folder change");
147 git.add().addFilepattern(".").call();
148 git.commit().setMessage("second commit").call();
149 write(new File(folder, "folder.txt"), "second folder change");
150 git.add().addFilepattern(".").call();
151 git.commit().setMessage("third commit").call();
152
153
154 DiffCommand diff = git.diff().setShowNameAndStatusOnly(true)
155 .setPathFilter(PathFilter.create("test.txt"))
156 .setOldTree(getTreeIterator("HEAD^^"))
157 .setNewTree(getTreeIterator("HEAD^"));
158 List<DiffEntry> entries = diff.call();
159 assertEquals(0, entries.size());
160
161
162 OutputStream out = new ByteArrayOutputStream();
163 diff = git.diff().setOutputStream(out)
164 .setOldTree(getTreeIterator("HEAD^^"))
165 .setNewTree(getTreeIterator("HEAD^"));
166 entries = diff.call();
167 assertEquals(1, entries.size());
168 assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType());
169 assertEquals("folder/folder.txt", entries.get(0).getOldPath());
170 assertEquals("folder/folder.txt", entries.get(0).getNewPath());
171
172 String actual = out.toString();
173 String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
174 + "index 0119635..95c4c65 100644\n"
175 + "--- a/folder/folder.txt\n"
176 + "+++ b/folder/folder.txt\n"
177 + "@@ -1 +1 @@\n"
178 + "-folder\n"
179 + "\\ No newline at end of file\n"
180 + "+folder change\n"
181 + "\\ No newline at end of file\n";
182 assertEquals(expected, actual);
183 }
184 }
185
186 @Test
187 public void testDiffWithPrefixes() throws Exception {
188 write(new File(db.getWorkTree(), "test.txt"), "test");
189 try (Git git = new Git(db)) {
190 git.add().addFilepattern(".").call();
191 git.commit().setMessage("Initial commit").call();
192 write(new File(db.getWorkTree(), "test.txt"), "test change");
193
194 OutputStream out = new ByteArrayOutputStream();
195 git.diff().setOutputStream(out).setSourcePrefix("old/")
196 .setDestinationPrefix("new/").call();
197
198 String actual = out.toString();
199 String expected = "diff --git old/test.txt new/test.txt\n"
200 + "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\n"
201 + "+++ new/test.txt\n" + "@@ -1 +1 @@\n" + "-test\n"
202 + "\\ No newline at end of file\n" + "+test change\n"
203 + "\\ No newline at end of file\n";
204 assertEquals(expected, actual);
205 }
206 }
207
208 @Test
209 public void testDiffWithNegativeLineCount() throws Exception {
210 write(new File(db.getWorkTree(), "test.txt"),
211 "0\n1\n2\n3\n4\n5\n6\n7\n8\n9");
212 try (Git git = new Git(db)) {
213 git.add().addFilepattern(".").call();
214 git.commit().setMessage("Initial commit").call();
215 write(new File(db.getWorkTree(), "test.txt"),
216 "0\n1\n2\n3\n4a\n5\n6\n7\n8\n9");
217
218 OutputStream out = new ByteArrayOutputStream();
219 git.diff().setOutputStream(out).setContextLines(1).call();
220
221 String actual = out.toString();
222 String expected = "diff --git a/test.txt b/test.txt\n"
223 + "index f55b5c9..c5ec8fd 100644\n" + "--- a/test.txt\n"
224 + "+++ b/test.txt\n" + "@@ -4,3 +4,3 @@\n" + " 3\n" + "-4\n"
225 + "+4a\n" + " 5\n";
226 assertEquals(expected, actual);
227 }
228 }
229
230 @Test
231 public void testNoOutputStreamSet() throws Exception {
232 File file = writeTrashFile("test.txt", "a");
233 assertTrue(file.setLastModified(file.lastModified() - 5000));
234 try (Git git = new Git(db)) {
235 git.add().addFilepattern(".").call();
236 write(file, "b");
237
238 List<DiffEntry> diffs = git.diff().call();
239 assertNotNull(diffs);
240 assertEquals(1, diffs.size());
241 DiffEntry diff = diffs.get(0);
242 assertEquals(ChangeType.MODIFY, diff.getChangeType());
243 assertEquals("test.txt", diff.getOldPath());
244 assertEquals("test.txt", diff.getNewPath());
245 }
246 }
247
248 private AbstractTreeIterator getTreeIterator(String name)
249 throws IOException {
250 final ObjectId id = db.resolve(name);
251 if (id == null)
252 throw new IllegalArgumentException(name);
253 final CanonicalTreeParser p = new CanonicalTreeParser();
254 try (ObjectReader or = db.newObjectReader();
255 RevWalk rw = new RevWalk(db)) {
256 p.reset(or, rw.parseTree(id));
257 return p;
258 }
259 }
260 }