1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.diff;
11
12 import static org.junit.Assert.assertEquals;
13
14 import java.io.File;
15
16 import org.eclipse.jgit.api.Git;
17 import org.eclipse.jgit.dircache.DirCacheIterator;
18 import org.eclipse.jgit.junit.RepositoryTestCase;
19 import org.eclipse.jgit.treewalk.FileTreeIterator;
20 import org.eclipse.jgit.treewalk.filter.PathFilter;
21 import org.junit.Test;
22
23 public class PatchIdDiffFormatterTest extends RepositoryTestCase {
24
25 @Test
26 public void testDiff() throws Exception {
27 write(new File(db.getDirectory().getParent(), "test.txt"), "test");
28 File folder = new File(db.getDirectory().getParent(), "folder");
29 folder.mkdir();
30 write(new File(folder, "folder.txt"), "folder");
31 try (Git git = new Git(db);
32 PatchIdDiffFormatter df = new PatchIdDiffFormatter()) {
33 git.add().addFilepattern(".").call();
34 git.commit().setMessage("Initial commit").call();
35 write(new File(folder, "folder.txt"), "folder change");
36
37 df.setRepository(db);
38 df.setPathFilter(PathFilter.create("folder"));
39 DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
40 FileTreeIterator newTree = new FileTreeIterator(db);
41 df.format(oldTree, newTree);
42 df.flush();
43
44 assertEquals("1ff64e0f9333e9b81967c3e8d7a81362b14d5441", df
45 .getCalulatedPatchId().name());
46 }
47 }
48
49 @Test
50 public void testSameDiff() throws Exception {
51 write(new File(db.getDirectory().getParent(), "test.txt"), "test");
52 File folder = new File(db.getDirectory().getParent(), "folder");
53 folder.mkdir();
54 write(new File(folder, "folder.txt"), "\n\n\n\nfolder");
55 try (Git git = new Git(db)) {
56 git.add().addFilepattern(".").call();
57 git.commit().setMessage("Initial commit").call();
58 write(new File(folder, "folder.txt"), "\n\n\n\nfolder change");
59
60 try (PatchIdDiffFormatter df = new PatchIdDiffFormatter()) {
61 df.setRepository(db);
62 df.setPathFilter(PathFilter.create("folder"));
63 DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
64 FileTreeIterator newTree = new FileTreeIterator(db);
65 df.format(oldTree, newTree);
66 df.flush();
67
68 assertEquals("08fca5ac531383eb1da8bf6b6f7cf44411281407", df
69 .getCalulatedPatchId().name());
70 }
71
72 write(new File(folder, "folder.txt"), "a\n\n\n\nfolder");
73 git.add().addFilepattern(".").call();
74 git.commit().setMessage("Initial commit").call();
75 write(new File(folder, "folder.txt"), "a\n\n\n\nfolder change");
76
77 try (PatchIdDiffFormatter df = new PatchIdDiffFormatter()) {
78 df.setRepository(db);
79 df.setPathFilter(PathFilter.create("folder"));
80 DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
81 FileTreeIterator newTree = new FileTreeIterator(db);
82 df.format(oldTree, newTree);
83 df.flush();
84
85 assertEquals("08fca5ac531383eb1da8bf6b6f7cf44411281407", df
86 .getCalulatedPatchId().name());
87 }
88 }
89 }
90
91 }