View Javadoc
1   /*
2    * Copyright (C) 2008, Google 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  
44  package org.eclipse.jgit.patch;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertNull;
50  import static org.junit.Assert.assertSame;
51  import static org.junit.Assert.assertTrue;
52  
53  import org.eclipse.jgit.diff.DiffEntry;
54  import org.eclipse.jgit.lib.Constants;
55  import org.eclipse.jgit.lib.FileMode;
56  import org.eclipse.jgit.lib.ObjectId;
57  import org.junit.Test;
58  
59  public class FileHeaderTest {
60  	@Test
61  	public void testParseGitFileName_Empty() {
62  		final FileHeader fh = data("");
63  		assertEquals(-1, fh.parseGitFileName(0, fh.buf.length));
64  		assertNotNull(fh.getHunks());
65  		assertTrue(fh.getHunks().isEmpty());
66  		assertFalse(fh.hasMetaDataChanges());
67  	}
68  
69  	@Test
70  	public void testParseGitFileName_NoLF() {
71  		final FileHeader fh = data("a/ b/");
72  		assertEquals(-1, fh.parseGitFileName(0, fh.buf.length));
73  	}
74  
75  	@Test
76  	public void testParseGitFileName_NoSecondLine() {
77  		final FileHeader fh = data("\n");
78  		assertEquals(-1, fh.parseGitFileName(0, fh.buf.length));
79  	}
80  
81  	@Test
82  	public void testParseGitFileName_EmptyHeader() {
83  		final FileHeader fh = data("\n\n");
84  		assertEquals(1, fh.parseGitFileName(0, fh.buf.length));
85  	}
86  
87  	@Test
88  	public void testParseGitFileName_Foo() {
89  		final String name = "foo";
90  		final FileHeader fh = header(name);
91  		assertEquals(gitLine(name).length(), fh.parseGitFileName(0,
92  				fh.buf.length));
93  		assertEquals(name, fh.getOldPath());
94  		assertSame(fh.getOldPath(), fh.getNewPath());
95  		assertFalse(fh.hasMetaDataChanges());
96  	}
97  
98  	@Test
99  	public void testParseGitFileName_FailFooBar() {
100 		final FileHeader fh = data("a/foo b/bar\n-");
101 		assertTrue(fh.parseGitFileName(0, fh.buf.length) > 0);
102 		assertNull(fh.getOldPath());
103 		assertNull(fh.getNewPath());
104 		assertFalse(fh.hasMetaDataChanges());
105 	}
106 
107 	@Test
108 	public void testParseGitFileName_FooSpBar() {
109 		final String name = "foo bar";
110 		final FileHeader fh = header(name);
111 		assertEquals(gitLine(name).length(), fh.parseGitFileName(0,
112 				fh.buf.length));
113 		assertEquals(name, fh.getOldPath());
114 		assertSame(fh.getOldPath(), fh.getNewPath());
115 		assertFalse(fh.hasMetaDataChanges());
116 	}
117 
118 	@Test
119 	public void testParseGitFileName_DqFooTabBar() {
120 		final String name = "foo\tbar";
121 		final String dqName = "foo\\tbar";
122 		final FileHeader fh = dqHeader(dqName);
123 		assertEquals(dqGitLine(dqName).length(), fh.parseGitFileName(0,
124 				fh.buf.length));
125 		assertEquals(name, fh.getOldPath());
126 		assertSame(fh.getOldPath(), fh.getNewPath());
127 		assertFalse(fh.hasMetaDataChanges());
128 	}
129 
130 	@Test
131 	public void testParseGitFileName_DqFooSpLfNulBar() {
132 		final String name = "foo \n\0bar";
133 		final String dqName = "foo \\n\\0bar";
134 		final FileHeader fh = dqHeader(dqName);
135 		assertEquals(dqGitLine(dqName).length(), fh.parseGitFileName(0,
136 				fh.buf.length));
137 		assertEquals(name, fh.getOldPath());
138 		assertSame(fh.getOldPath(), fh.getNewPath());
139 		assertFalse(fh.hasMetaDataChanges());
140 	}
141 
142 	@Test
143 	public void testParseGitFileName_SrcFooC() {
144 		final String name = "src/foo/bar/argh/code.c";
145 		final FileHeader fh = header(name);
146 		assertEquals(gitLine(name).length(), fh.parseGitFileName(0,
147 				fh.buf.length));
148 		assertEquals(name, fh.getOldPath());
149 		assertSame(fh.getOldPath(), fh.getNewPath());
150 		assertFalse(fh.hasMetaDataChanges());
151 	}
152 
153 	@Test
154 	public void testParseGitFileName_SrcFooCNonStandardPrefix() {
155 		final String name = "src/foo/bar/argh/code.c";
156 		final String header = "project-v-1.0/" + name + " mydev/" + name + "\n";
157 		final FileHeader fh = data(header + "-");
158 		assertEquals(header.length(), fh.parseGitFileName(0, fh.buf.length));
159 		assertEquals(name, fh.getOldPath());
160 		assertSame(fh.getOldPath(), fh.getNewPath());
161 		assertFalse(fh.hasMetaDataChanges());
162 	}
163 
164 	@Test
165 	public void testParseUnicodeName_NewFile() {
166 		final FileHeader fh = data("diff --git \"a/\\303\\205ngstr\\303\\266m\" \"b/\\303\\205ngstr\\303\\266m\"\n"
167 				+ "new file mode 100644\n"
168 				+ "index 0000000..7898192\n"
169 				+ "--- /dev/null\n"
170 				+ "+++ \"b/\\303\\205ngstr\\303\\266m\"\n"
171 				+ "@@ -0,0 +1 @@\n" + "+a\n");
172 		assertParse(fh);
173 
174 		assertEquals("/dev/null", fh.getOldPath());
175 		assertSame(DiffEntry.DEV_NULL, fh.getOldPath());
176 		assertEquals("\u00c5ngstr\u00f6m", fh.getNewPath());
177 
178 		assertSame(FileHeader.ChangeType.ADD, fh.getChangeType());
179 		assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
180 		assertTrue(fh.hasMetaDataChanges());
181 
182 		assertSame(FileMode.MISSING, fh.getOldMode());
183 		assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
184 
185 		assertEquals("0000000", fh.getOldId().name());
186 		assertEquals("7898192", fh.getNewId().name());
187 		assertEquals(0, fh.getScore());
188 	}
189 
190 	@Test
191 	public void testParseUnicodeName_DeleteFile() {
192 		final FileHeader fh = data("diff --git \"a/\\303\\205ngstr\\303\\266m\" \"b/\\303\\205ngstr\\303\\266m\"\n"
193 				+ "deleted file mode 100644\n"
194 				+ "index 7898192..0000000\n"
195 				+ "--- \"a/\\303\\205ngstr\\303\\266m\"\n"
196 				+ "+++ /dev/null\n"
197 				+ "@@ -1 +0,0 @@\n" + "-a\n");
198 		assertParse(fh);
199 
200 		assertEquals("\u00c5ngstr\u00f6m", fh.getOldPath());
201 		assertEquals("/dev/null", fh.getNewPath());
202 		assertSame(DiffEntry.DEV_NULL, fh.getNewPath());
203 
204 		assertSame(FileHeader.ChangeType.DELETE, fh.getChangeType());
205 		assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
206 		assertTrue(fh.hasMetaDataChanges());
207 
208 		assertSame(FileMode.REGULAR_FILE, fh.getOldMode());
209 		assertSame(FileMode.MISSING, fh.getNewMode());
210 
211 		assertEquals("7898192", fh.getOldId().name());
212 		assertEquals("0000000", fh.getNewId().name());
213 		assertEquals(0, fh.getScore());
214 	}
215 
216 	@Test
217 	public void testParseModeChange() {
218 		final FileHeader fh = data("diff --git a/a b b/a b\n"
219 				+ "old mode 100644\n" + "new mode 100755\n");
220 		assertParse(fh);
221 		assertEquals("a b", fh.getOldPath());
222 		assertEquals("a b", fh.getNewPath());
223 
224 		assertSame(FileHeader.ChangeType.MODIFY, fh.getChangeType());
225 		assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
226 		assertTrue(fh.hasMetaDataChanges());
227 
228 		assertNull(fh.getOldId());
229 		assertNull(fh.getNewId());
230 
231 		assertSame(FileMode.REGULAR_FILE, fh.getOldMode());
232 		assertSame(FileMode.EXECUTABLE_FILE, fh.getNewMode());
233 		assertEquals(0, fh.getScore());
234 	}
235 
236 	@Test
237 	public void testParseRename100_NewStyle() {
238 		final FileHeader fh = data("diff --git a/a b/ c/\\303\\205ngstr\\303\\266m\n"
239 				+ "similarity index 100%\n"
240 				+ "rename from a\n"
241 				+ "rename to \" c/\\303\\205ngstr\\303\\266m\"\n");
242 		int ptr = fh.parseGitFileName(0, fh.buf.length);
243 		assertTrue(ptr > 0);
244 		assertNull(fh.getOldPath()); // can't parse names on a rename
245 		assertNull(fh.getNewPath());
246 
247 		ptr = fh.parseGitHeaders(ptr, fh.buf.length);
248 		assertTrue(ptr > 0);
249 
250 		assertEquals("a", fh.getOldPath());
251 		assertEquals(" c/\u00c5ngstr\u00f6m", fh.getNewPath());
252 
253 		assertSame(FileHeader.ChangeType.RENAME, fh.getChangeType());
254 		assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
255 		assertTrue(fh.hasMetaDataChanges());
256 
257 		assertNull(fh.getOldId());
258 		assertNull(fh.getNewId());
259 
260 		assertNull(fh.getOldMode());
261 		assertNull(fh.getNewMode());
262 
263 		assertEquals(100, fh.getScore());
264 	}
265 
266 	@Test
267 	public void testParseRename100_OldStyle() {
268 		final FileHeader fh = data("diff --git a/a b/ c/\\303\\205ngstr\\303\\266m\n"
269 				+ "similarity index 100%\n"
270 				+ "rename old a\n"
271 				+ "rename new \" c/\\303\\205ngstr\\303\\266m\"\n");
272 		int ptr = fh.parseGitFileName(0, fh.buf.length);
273 		assertTrue(ptr > 0);
274 		assertNull(fh.getOldPath()); // can't parse names on a rename
275 		assertNull(fh.getNewPath());
276 
277 		ptr = fh.parseGitHeaders(ptr, fh.buf.length);
278 		assertTrue(ptr > 0);
279 
280 		assertEquals("a", fh.getOldPath());
281 		assertEquals(" c/\u00c5ngstr\u00f6m", fh.getNewPath());
282 
283 		assertSame(FileHeader.ChangeType.RENAME, fh.getChangeType());
284 		assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
285 		assertTrue(fh.hasMetaDataChanges());
286 
287 		assertNull(fh.getOldId());
288 		assertNull(fh.getNewId());
289 
290 		assertNull(fh.getOldMode());
291 		assertNull(fh.getNewMode());
292 
293 		assertEquals(100, fh.getScore());
294 	}
295 
296 	@Test
297 	public void testParseCopy100() {
298 		final FileHeader fh = data("diff --git a/a b/ c/\\303\\205ngstr\\303\\266m\n"
299 				+ "similarity index 100%\n"
300 				+ "copy from a\n"
301 				+ "copy to \" c/\\303\\205ngstr\\303\\266m\"\n");
302 		int ptr = fh.parseGitFileName(0, fh.buf.length);
303 		assertTrue(ptr > 0);
304 		assertNull(fh.getOldPath()); // can't parse names on a copy
305 		assertNull(fh.getNewPath());
306 
307 		ptr = fh.parseGitHeaders(ptr, fh.buf.length);
308 		assertTrue(ptr > 0);
309 
310 		assertEquals("a", fh.getOldPath());
311 		assertEquals(" c/\u00c5ngstr\u00f6m", fh.getNewPath());
312 
313 		assertSame(FileHeader.ChangeType.COPY, fh.getChangeType());
314 		assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
315 		assertTrue(fh.hasMetaDataChanges());
316 
317 		assertNull(fh.getOldId());
318 		assertNull(fh.getNewId());
319 
320 		assertNull(fh.getOldMode());
321 		assertNull(fh.getNewMode());
322 
323 		assertEquals(100, fh.getScore());
324 	}
325 
326 	@Test
327 	public void testParseFullIndexLine_WithMode() {
328 		final String oid = "78981922613b2afb6025042ff6bd878ac1994e85";
329 		final String nid = "61780798228d17af2d34fce4cfbdf35556832472";
330 		final FileHeader fh = data("diff --git a/a b/a\n" + "index " + oid
331 				+ ".." + nid + " 100644\n" + "--- a/a\n" + "+++ b/a\n");
332 		assertParse(fh);
333 
334 		assertEquals("a", fh.getOldPath());
335 		assertEquals("a", fh.getNewPath());
336 
337 		assertSame(FileMode.REGULAR_FILE, fh.getOldMode());
338 		assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
339 		assertFalse(fh.hasMetaDataChanges());
340 
341 		assertNotNull(fh.getOldId());
342 		assertNotNull(fh.getNewId());
343 
344 		assertTrue(fh.getOldId().isComplete());
345 		assertTrue(fh.getNewId().isComplete());
346 
347 		assertEquals(ObjectId.fromString(oid), fh.getOldId().toObjectId());
348 		assertEquals(ObjectId.fromString(nid), fh.getNewId().toObjectId());
349 	}
350 
351 	@Test
352 	public void testParseFullIndexLine_NoMode() {
353 		final String oid = "78981922613b2afb6025042ff6bd878ac1994e85";
354 		final String nid = "61780798228d17af2d34fce4cfbdf35556832472";
355 		final FileHeader fh = data("diff --git a/a b/a\n" + "index " + oid
356 				+ ".." + nid + "\n" + "--- a/a\n" + "+++ b/a\n");
357 		assertParse(fh);
358 
359 		assertEquals("a", fh.getOldPath());
360 		assertEquals("a", fh.getNewPath());
361 		assertFalse(fh.hasMetaDataChanges());
362 
363 		assertNull(fh.getOldMode());
364 		assertNull(fh.getNewMode());
365 
366 		assertNotNull(fh.getOldId());
367 		assertNotNull(fh.getNewId());
368 
369 		assertTrue(fh.getOldId().isComplete());
370 		assertTrue(fh.getNewId().isComplete());
371 
372 		assertEquals(ObjectId.fromString(oid), fh.getOldId().toObjectId());
373 		assertEquals(ObjectId.fromString(nid), fh.getNewId().toObjectId());
374 	}
375 
376 	@Test
377 	public void testParseAbbrIndexLine_WithMode() {
378 		final int a = 7;
379 		final String oid = "78981922613b2afb6025042ff6bd878ac1994e85";
380 		final String nid = "61780798228d17af2d34fce4cfbdf35556832472";
381 		final FileHeader fh = data("diff --git a/a b/a\n" + "index "
382 				+ oid.substring(0, a - 1) + ".." + nid.substring(0, a - 1)
383 				+ " 100644\n" + "--- a/a\n" + "+++ b/a\n");
384 		assertParse(fh);
385 
386 		assertEquals("a", fh.getOldPath());
387 		assertEquals("a", fh.getNewPath());
388 
389 		assertSame(FileMode.REGULAR_FILE, fh.getOldMode());
390 		assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
391 		assertFalse(fh.hasMetaDataChanges());
392 
393 		assertNotNull(fh.getOldId());
394 		assertNotNull(fh.getNewId());
395 
396 		assertFalse(fh.getOldId().isComplete());
397 		assertFalse(fh.getNewId().isComplete());
398 
399 		assertEquals(oid.substring(0, a - 1), fh.getOldId().name());
400 		assertEquals(nid.substring(0, a - 1), fh.getNewId().name());
401 
402 		assertTrue(ObjectId.fromString(oid).startsWith(fh.getOldId()));
403 		assertTrue(ObjectId.fromString(nid).startsWith(fh.getNewId()));
404 	}
405 
406 	@Test
407 	public void testParseAbbrIndexLine_NoMode() {
408 		final int a = 7;
409 		final String oid = "78981922613b2afb6025042ff6bd878ac1994e85";
410 		final String nid = "61780798228d17af2d34fce4cfbdf35556832472";
411 		final FileHeader fh = data("diff --git a/a b/a\n" + "index "
412 				+ oid.substring(0, a - 1) + ".." + nid.substring(0, a - 1)
413 				+ "\n" + "--- a/a\n" + "+++ b/a\n");
414 		assertParse(fh);
415 
416 		assertEquals("a", fh.getOldPath());
417 		assertEquals("a", fh.getNewPath());
418 
419 		assertNull(fh.getOldMode());
420 		assertNull(fh.getNewMode());
421 		assertFalse(fh.hasMetaDataChanges());
422 
423 		assertNotNull(fh.getOldId());
424 		assertNotNull(fh.getNewId());
425 
426 		assertFalse(fh.getOldId().isComplete());
427 		assertFalse(fh.getNewId().isComplete());
428 
429 		assertEquals(oid.substring(0, a - 1), fh.getOldId().name());
430 		assertEquals(nid.substring(0, a - 1), fh.getNewId().name());
431 
432 		assertTrue(ObjectId.fromString(oid).startsWith(fh.getOldId()));
433 		assertTrue(ObjectId.fromString(nid).startsWith(fh.getNewId()));
434 	}
435 
436 	private static void assertParse(final FileHeader fh) {
437 		int ptr = fh.parseGitFileName(0, fh.buf.length);
438 		assertTrue(ptr > 0);
439 		ptr = fh.parseGitHeaders(ptr, fh.buf.length);
440 		assertTrue(ptr > 0);
441 	}
442 
443 	private static FileHeader data(final String in) {
444 		return new FileHeader(Constants.encodeASCII(in), 0);
445 	}
446 
447 	private static FileHeader header(final String path) {
448 		return data(gitLine(path) + "--- " + path + "\n");
449 	}
450 
451 	private static String gitLine(final String path) {
452 		return "a/" + path + " b/" + path + "\n";
453 	}
454 
455 	private static FileHeader dqHeader(final String path) {
456 		return data(dqGitLine(path) + "--- " + path + "\n");
457 	}
458 
459 	private static String dqGitLine(final String path) {
460 		return "\"a/" + path + "\" \"b/" + path + "\"\n";
461 	}
462 }