View Javadoc
1   /*
2    * Copyright (C) 2011, Tomasz Zarna <Tomasz.Zarna@pl.ibm.com>
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  
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  		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.toString(), actual);
99  	}
100 
101 	@Test
102 	public void testDiffCached() throws Exception {
103 		write(new File(db.getWorkTree(), "test.txt"), "test");
104 		File folder = new File(db.getWorkTree(), "folder");
105 		folder.mkdir();
106 		Git git = new Git(db);
107 		git.add().addFilepattern(".").call();
108 		git.commit().setMessage("Initial commit").call();
109 		write(new File(folder, "folder.txt"), "folder");
110 		git.add().addFilepattern(".").call();
111 
112 		OutputStream out = new ByteArrayOutputStream();
113 		List<DiffEntry> entries = git.diff().setOutputStream(out)
114 				.setCached(true).call();
115 		assertEquals(1, entries.size());
116 		assertEquals(ChangeType.ADD, entries.get(0)
117 				.getChangeType());
118 		assertEquals("/dev/null", entries.get(0)
119 				.getOldPath());
120 		assertEquals("folder/folder.txt", entries.get(0)
121 				.getNewPath());
122 
123 		String actual = out.toString();
124 		String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
125 				+ "new file mode 100644\n"
126 				+ "index 0000000..0119635\n"
127 				+ "--- /dev/null\n"
128 				+ "+++ b/folder/folder.txt\n"
129 				+ "@@ -0,0 +1 @@\n"
130 				+ "+folder\n"
131 				+ "\\ No newline at end of file\n";
132 		assertEquals(expected.toString(), actual);
133 	}
134 
135 	@Test
136 	public void testDiffTwoCommits() throws Exception {
137 		write(new File(db.getWorkTree(), "test.txt"), "test");
138 		File folder = new File(db.getWorkTree(), "folder");
139 		folder.mkdir();
140 		write(new File(folder, "folder.txt"), "folder");
141 		Git git = new Git(db);
142 		git.add().addFilepattern(".").call();
143 		git.commit().setMessage("Initial commit").call();
144 		write(new File(folder, "folder.txt"), "folder change");
145 		git.add().addFilepattern(".").call();
146 		git.commit().setMessage("second commit").call();
147 		write(new File(folder, "folder.txt"), "second folder change");
148 		git.add().addFilepattern(".").call();
149 		git.commit().setMessage("third commit").call();
150 
151 		// bad filter
152 		DiffCommand diff = git.diff().setShowNameAndStatusOnly(true)
153 				.setPathFilter(PathFilter.create("test.txt"))
154 				.setOldTree(getTreeIterator("HEAD^^"))
155 				.setNewTree(getTreeIterator("HEAD^"));
156 		List<DiffEntry> entries = diff.call();
157 		assertEquals(0, entries.size());
158 
159 		// no filter, two commits
160 		OutputStream out = new ByteArrayOutputStream();
161 		diff = git.diff().setOutputStream(out)
162 				.setOldTree(getTreeIterator("HEAD^^"))
163 				.setNewTree(getTreeIterator("HEAD^"));
164 		entries = diff.call();
165 		assertEquals(1, entries.size());
166 		assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType());
167 		assertEquals("folder/folder.txt", entries.get(0).getOldPath());
168 		assertEquals("folder/folder.txt", entries.get(0).getNewPath());
169 
170 		String actual = out.toString();
171 		String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
172 				+ "index 0119635..95c4c65 100644\n"
173 				+ "--- a/folder/folder.txt\n"
174 				+ "+++ b/folder/folder.txt\n"
175 				+ "@@ -1 +1 @@\n"
176 				+ "-folder\n"
177 				+ "\\ No newline at end of file\n"
178 				+ "+folder change\n"
179 				+ "\\ No newline at end of file\n";
180 		assertEquals(expected.toString(), actual);
181 	}
182 
183 	@Test
184 	public void testDiffWithPrefixes() throws Exception {
185 		write(new File(db.getWorkTree(), "test.txt"), "test");
186 		Git git = new Git(db);
187 		git.add().addFilepattern(".").call();
188 		git.commit().setMessage("Initial commit").call();
189 		write(new File(db.getWorkTree(), "test.txt"), "test change");
190 
191 		OutputStream out = new ByteArrayOutputStream();
192 		git.diff().setOutputStream(out).setSourcePrefix("old/")
193 				.setDestinationPrefix("new/")
194 				.call();
195 
196 		String actual = out.toString();
197 		String expected = "diff --git old/test.txt new/test.txt\n"
198 				+ "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\n"
199 				+ "+++ new/test.txt\n" + "@@ -1 +1 @@\n" + "-test\n"
200 				+ "\\ No newline at end of file\n" + "+test change\n"
201 				+ "\\ No newline at end of file\n";
202 		assertEquals(expected.toString(), actual);
203 	}
204 
205 	@Test
206 	public void testDiffWithNegativeLineCount() throws Exception {
207 		write(new File(db.getWorkTree(), "test.txt"),
208 				"0\n1\n2\n3\n4\n5\n6\n7\n8\n9");
209 		Git git = new Git(db);
210 		git.add().addFilepattern(".").call();
211 		git.commit().setMessage("Initial commit").call();
212 		write(new File(db.getWorkTree(), "test.txt"),
213 				"0\n1\n2\n3\n4a\n5\n6\n7\n8\n9");
214 
215 		OutputStream out = new ByteArrayOutputStream();
216 		git.diff().setOutputStream(out).setContextLines(1)
217 				.call();
218 
219 		String actual = out.toString();
220 		String expected = "diff --git a/test.txt b/test.txt\n"
221 				+ "index f55b5c9..c5ec8fd 100644\n" + "--- a/test.txt\n"
222 				+ "+++ b/test.txt\n" + "@@ -4,3 +4,3 @@\n" + " 3\n" + "-4\n"
223 				+ "+4a\n" + " 5\n";
224 		assertEquals(expected.toString(), actual);
225 	}
226 
227 	@Test
228 	public void testNoOutputStreamSet() throws Exception {
229 		File file = writeTrashFile("test.txt", "a");
230 		assertTrue(file.setLastModified(file.lastModified() - 5000));
231 		Git git = new Git(db);
232 		git.add().addFilepattern(".").call();
233 		write(file, "b");
234 
235 		List<DiffEntry> diffs = git.diff().call();
236 		assertNotNull(diffs);
237 		assertEquals(1, diffs.size());
238 		DiffEntry diff = diffs.get(0);
239 		assertEquals(ChangeType.MODIFY, diff.getChangeType());
240 		assertEquals("test.txt", diff.getOldPath());
241 		assertEquals("test.txt", diff.getNewPath());
242 	}
243 
244 	private AbstractTreeIterator getTreeIterator(String name)
245 			throws IOException {
246 		final ObjectId id = db.resolve(name);
247 		if (id == null)
248 			throw new IllegalArgumentException(name);
249 		final CanonicalTreeParser p = new CanonicalTreeParser();
250 		try (ObjectReader or = db.newObjectReader()) {
251 			p.reset(or, new RevWalk(db).parseTree(id));
252 			return p;
253 		}
254 	}
255 }