|  | 
| 47 | 47 | 
 | 
| 48 | 48 | import java.io.ByteArrayOutputStream; | 
| 49 | 49 | import java.io.File; | 
|  | 50 | +import java.util.regex.Pattern; | 
| 50 | 51 | 
 | 
| 51 | 52 | import org.eclipse.jgit.api.Git; | 
| 52 | 53 | import org.eclipse.jgit.diff.DiffEntry.ChangeType; | 
| @@ -343,6 +344,187 @@ public void testDiff() throws Exception { | 
| 343 | 344 | 		assertEquals(expected, actual); | 
| 344 | 345 | 	} | 
| 345 | 346 | 
 | 
|  | 347 | +	@Test | 
|  | 348 | +	public void testDiffDeltaFilter_emptyFilter() throws Exception { | 
|  | 349 | +		write(new File(db.getDirectory().getParent(), "test.txt"), "test"); | 
|  | 350 | +		File folder = new File(db.getDirectory().getParent(), "folder"); | 
|  | 351 | +		FileUtils.mkdir(folder); | 
|  | 352 | +		write(new File(folder, "folder.txt"), "folder"); | 
|  | 353 | +		Git git = new Git(db); | 
|  | 354 | +		git.add().addFilepattern(".").call(); | 
|  | 355 | +		git.commit().setMessage("Initial commit").call(); | 
|  | 356 | +		write(new File(folder, "folder.txt"), "folder change"); | 
|  | 357 | + | 
|  | 358 | +		ByteArrayOutputStream os = new ByteArrayOutputStream(); | 
|  | 359 | +		DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os)); | 
|  | 360 | +		dfmt.setRepository(db); | 
|  | 361 | +		dfmt.setPathFilter(PathFilter.create("folder")); | 
|  | 362 | +		DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); | 
|  | 363 | +		FileTreeIterator newTree = new FileTreeIterator(db); | 
|  | 364 | + | 
|  | 365 | +		//testing an empty delta filter | 
|  | 366 | +		Pattern deltaFilterPattern = Pattern.compile(""); | 
|  | 367 | +		dfmt.format(dfmt.scan(oldTree, newTree), deltaFilterPattern); | 
|  | 368 | +		dfmt.flush(); | 
|  | 369 | + | 
|  | 370 | +		String actual = os.toString("UTF-8"); | 
|  | 371 | +		String expected = | 
|  | 372 | +				"diff --git a/folder/folder.txt b/folder/folder.txt\n" | 
|  | 373 | +						+ "index 0119635..95c4c65 100644\n" | 
|  | 374 | +						+ "--- a/folder/folder.txt\n" + "+++ b/folder/folder.txt\n" | 
|  | 375 | +						+ "@@ -1 +1 @@\n" + "-folder\n" | 
|  | 376 | +						+ "\\ No newline at end of file\n" + "+folder change\n" | 
|  | 377 | +						+ "\\ No newline at end of file\n"; | 
|  | 378 | + | 
|  | 379 | +		assertEquals(expected, actual); | 
|  | 380 | +	} | 
|  | 381 | + | 
|  | 382 | +	@Test | 
|  | 383 | +	/** | 
|  | 384 | +	 * This is an ADD file, the file content matches the filter: diff unchanged. | 
|  | 385 | +	 */ | 
|  | 386 | +	public void testDiffDeltaFilter_addFile() throws Exception { | 
|  | 387 | +		write(new File(db.getDirectory().getParent(), "test.txt"), "test"); | 
|  | 388 | +		File folder = new File(db.getDirectory().getParent(), "folder"); | 
|  | 389 | +		FileUtils.mkdir(folder); | 
|  | 390 | +		Git git = new Git(db); | 
|  | 391 | +		git.add().addFilepattern(".").call(); | 
|  | 392 | +		git.commit().setMessage("Initial commit").call(); | 
|  | 393 | +		write(new File(folder, "folder.txt"), "change"); | 
|  | 394 | + | 
|  | 395 | +		ByteArrayOutputStream os = new ByteArrayOutputStream(); | 
|  | 396 | +		DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os)); | 
|  | 397 | +		dfmt.setRepository(db); | 
|  | 398 | +		dfmt.setPathFilter(PathFilter.create("folder")); | 
|  | 399 | +		DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); | 
|  | 400 | +		FileTreeIterator newTree = new FileTreeIterator(db); | 
|  | 401 | + | 
|  | 402 | +		//testing a delta filter with one regex | 
|  | 403 | +		Pattern deltaFilterPattern = Pattern.compile("change"); | 
|  | 404 | +		dfmt.format(dfmt.scan(oldTree, newTree), deltaFilterPattern); | 
|  | 405 | +		dfmt.flush(); | 
|  | 406 | + | 
|  | 407 | +		String actual = os.toString("UTF-8"); | 
|  | 408 | +		String expected = | 
|  | 409 | +				"diff --git a/folder/folder.txt b/folder/folder.txt\n" | 
|  | 410 | +						+ "new file mode 100644\n" | 
|  | 411 | +						+ "index 0000000..8013df8\n" | 
|  | 412 | +						+ "--- /dev/null\n" | 
|  | 413 | +						+ "+++ b/folder/folder.txt\n" | 
|  | 414 | +						+ "@@ -0,0 +1 @@\n" | 
|  | 415 | +						+ "+change\n" | 
|  | 416 | +						+ "\\ No newline at end of file\n"; | 
|  | 417 | + | 
|  | 418 | +		assertEquals(expected, actual); | 
|  | 419 | +	} | 
|  | 420 | + | 
|  | 421 | +	@Test | 
|  | 422 | +	/** | 
|  | 423 | +	 * This is an DELETE file, the file content matches the filter: diff unchanged. | 
|  | 424 | +	 */ | 
|  | 425 | +	public void testDiffDeltaFilter_deleteFile() throws Exception { | 
|  | 426 | +		write(new File(db.getDirectory().getParent(), "test.txt"), "test"); | 
|  | 427 | +		File folder = new File(db.getDirectory().getParent(), "folder"); | 
|  | 428 | +		FileUtils.mkdir(folder); | 
|  | 429 | +		write(new File(folder, "folder.txt"), "change"); | 
|  | 430 | +		Git git = new Git(db); | 
|  | 431 | +		git.add().addFilepattern(".").call(); | 
|  | 432 | +		git.commit().setMessage("Initial commit").call(); | 
|  | 433 | +		new File(folder, "folder.txt").delete(); | 
|  | 434 | + | 
|  | 435 | +		ByteArrayOutputStream os = new ByteArrayOutputStream(); | 
|  | 436 | +		DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os)); | 
|  | 437 | +		dfmt.setRepository(db); | 
|  | 438 | +		dfmt.setPathFilter(PathFilter.create("folder")); | 
|  | 439 | +		DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); | 
|  | 440 | +		FileTreeIterator newTree = new FileTreeIterator(db); | 
|  | 441 | + | 
|  | 442 | +		//testing a delta filter with one regex | 
|  | 443 | +		Pattern deltaFilterPattern = Pattern.compile("change"); | 
|  | 444 | +		dfmt.format(dfmt.scan(oldTree, newTree), deltaFilterPattern); | 
|  | 445 | +		dfmt.flush(); | 
|  | 446 | + | 
|  | 447 | +		String actual = os.toString("UTF-8"); | 
|  | 448 | +		String expected = | 
|  | 449 | +				"diff --git a/folder/folder.txt b/folder/folder.txt\n" | 
|  | 450 | +						+ "deleted file mode 100644\n" | 
|  | 451 | +						+ "index 8013df8..0000000\n" | 
|  | 452 | +						+ "--- a/folder/folder.txt\n" | 
|  | 453 | +						+ "+++ /dev/null\n" | 
|  | 454 | +						+ "@@ -1 +0,0 @@\n" | 
|  | 455 | +						+ "-change\n" | 
|  | 456 | +						+ "\\ No newline at end of file\n"; | 
|  | 457 | + | 
|  | 458 | +		assertEquals(expected, actual); | 
|  | 459 | +	} | 
|  | 460 | + | 
|  | 461 | +	@Test | 
|  | 462 | +	/** | 
|  | 463 | +	 * Filter for any file matches the content of the changed file: diff skipped. | 
|  | 464 | +	 */ | 
|  | 465 | +	public void testDiffDeltaFilter_filteredModifiedFile() throws Exception { | 
|  | 466 | +		write(new File(db.getDirectory().getParent(), "test.txt"), "test"); | 
|  | 467 | +		File folder = new File(db.getDirectory().getParent(), "folder"); | 
|  | 468 | +		FileUtils.mkdir(folder); | 
|  | 469 | +		write(new File(folder, "folder.txt"), "folder"); | 
|  | 470 | +		Git git = new Git(db); | 
|  | 471 | +		git.add().addFilepattern(".").call(); | 
|  | 472 | +		git.commit().setMessage("Initial commit").call(); | 
|  | 473 | +		write(new File(folder, "folder.txt"), "folderchange"); | 
|  | 474 | + | 
|  | 475 | +		ByteArrayOutputStream os = new ByteArrayOutputStream(); | 
|  | 476 | +		DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os)); | 
|  | 477 | +		dfmt.setRepository(db); | 
|  | 478 | +		dfmt.setPathFilter(PathFilter.create("folder")); | 
|  | 479 | +		DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); | 
|  | 480 | +		FileTreeIterator newTree = new FileTreeIterator(db); | 
|  | 481 | + | 
|  | 482 | +		//testing a delta filter with one regex (ANY) | 
|  | 483 | +		Pattern deltaFilterPattern = Pattern.compile("change"); | 
|  | 484 | +		dfmt.format(dfmt.scan(oldTree, newTree), deltaFilterPattern); | 
|  | 485 | +		dfmt.flush(); | 
|  | 486 | +		 | 
|  | 487 | +		assertEquals("", os.toString("UTF-8")); | 
|  | 488 | +	} | 
|  | 489 | + | 
|  | 490 | +	@Test | 
|  | 491 | +	/** | 
|  | 492 | +	 * The filter doesn't match any change: diff unchanged. | 
|  | 493 | +	 */ | 
|  | 494 | +	public void testDiffDeltaFilter_filterNoMatch() throws Exception { | 
|  | 495 | +		write(new File(db.getDirectory().getParent(), "test.txt"), "test"); | 
|  | 496 | +		File folder = new File(db.getDirectory().getParent(), "folder"); | 
|  | 497 | +		FileUtils.mkdir(folder); | 
|  | 498 | +		write(new File(folder, "folder.txt"), "folder"); | 
|  | 499 | +		Git git = new Git(db); | 
|  | 500 | +		git.add().addFilepattern(".").call(); | 
|  | 501 | +		git.commit().setMessage("Initial commit").call(); | 
|  | 502 | +		write(new File(folder, "folder.txt"), "folderchange"); | 
|  | 503 | + | 
|  | 504 | +		ByteArrayOutputStream os = new ByteArrayOutputStream(); | 
|  | 505 | +		DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os)); | 
|  | 506 | +		dfmt.setRepository(db); | 
|  | 507 | +		dfmt.setPathFilter(PathFilter.create("folder")); | 
|  | 508 | +		DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); | 
|  | 509 | +		FileTreeIterator newTree = new FileTreeIterator(db); | 
|  | 510 | + | 
|  | 511 | +		//testing a delta filter with one regex (ANY) | 
|  | 512 | +		Pattern deltaFilterPattern = Pattern.compile("xxxx"); | 
|  | 513 | +		dfmt.format(dfmt.scan(oldTree, newTree), deltaFilterPattern); | 
|  | 514 | +		dfmt.flush(); | 
|  | 515 | + | 
|  | 516 | +		String actual = os.toString("UTF-8"); | 
|  | 517 | +		String expected = | 
|  | 518 | +				"diff --git a/folder/folder.txt b/folder/folder.txt\n" | 
|  | 519 | +						+ "index 0119635..0b099ef 100644\n" | 
|  | 520 | +						+ "--- a/folder/folder.txt\n" + "+++ b/folder/folder.txt\n" | 
|  | 521 | +						+ "@@ -1 +1 @@\n" + "-folder\n" | 
|  | 522 | +						+ "\\ No newline at end of file\n" + "+folderchange\n" | 
|  | 523 | +						+ "\\ No newline at end of file\n"; | 
|  | 524 | + | 
|  | 525 | +		assertEquals(expected, actual); | 
|  | 526 | +	} | 
|  | 527 | + | 
| 346 | 528 | 	@Test | 
| 347 | 529 | 	public void testDiffRootNullToTree() throws Exception { | 
| 348 | 530 | 		write(new File(db.getDirectory().getParent(), "test.txt"), "test"); | 
|  | 
0 commit comments