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.blame;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
48
49 import org.eclipse.jgit.api.Git;
50 import org.eclipse.jgit.blame.BlameGenerator;
51 import org.eclipse.jgit.blame.BlameResult;
52 import org.eclipse.jgit.junit.RepositoryTestCase;
53 import org.eclipse.jgit.lib.Constants;
54 import org.eclipse.jgit.revwalk.RevCommit;
55 import org.junit.Test;
56
57
58 public class BlameGeneratorTest extends RepositoryTestCase {
59 @Test
60 public void testBoundLineDelete() throws Exception {
61 try (Git git = new Git(db)) {
62 String[] content1 = new String[] { "first", "second" };
63 writeTrashFile("file.txt", join(content1));
64 git.add().addFilepattern("file.txt").call();
65 RevCommit c1 = git.commit().setMessage("create file").call();
66
67 String[] content2 = new String[] { "third", "first", "second" };
68 writeTrashFile("file.txt", join(content2));
69 git.add().addFilepattern("file.txt").call();
70 RevCommit c2 = git.commit().setMessage("create file").call();
71
72 try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
73 generator.push(null, db.resolve(Constants.HEAD));
74 assertEquals(3, generator.getResultContents().size());
75
76 assertTrue(generator.next());
77 assertEquals(c2, generator.getSourceCommit());
78 assertEquals(1, generator.getRegionLength());
79 assertEquals(0, generator.getResultStart());
80 assertEquals(1, generator.getResultEnd());
81 assertEquals(0, generator.getSourceStart());
82 assertEquals(1, generator.getSourceEnd());
83 assertEquals("file.txt", generator.getSourcePath());
84
85 assertTrue(generator.next());
86 assertEquals(c1, generator.getSourceCommit());
87 assertEquals(2, generator.getRegionLength());
88 assertEquals(1, generator.getResultStart());
89 assertEquals(3, generator.getResultEnd());
90 assertEquals(0, generator.getSourceStart());
91 assertEquals(2, generator.getSourceEnd());
92 assertEquals("file.txt", generator.getSourcePath());
93
94 assertFalse(generator.next());
95 }
96 }
97 }
98
99 @Test
100 public void testRenamedBoundLineDelete() throws Exception {
101 try (Git git = new Git(db)) {
102 final String FILENAME_1 = "subdir/file1.txt";
103 final String FILENAME_2 = "subdir/file2.txt";
104
105 String[] content1 = new String[] { "first", "second" };
106 writeTrashFile(FILENAME_1, join(content1));
107 git.add().addFilepattern(FILENAME_1).call();
108 RevCommit c1 = git.commit().setMessage("create file1").call();
109
110
111 writeTrashFile(FILENAME_2, join(content1));
112 git.add().addFilepattern(FILENAME_2).call();
113 deleteTrashFile(FILENAME_1);
114 git.rm().addFilepattern(FILENAME_1).call();
115 git.commit().setMessage("rename file1.txt to file2.txt").call();
116
117
118 String[] content2 = new String[] { "third", "first", "second" };
119 writeTrashFile(FILENAME_2, join(content2));
120 git.add().addFilepattern(FILENAME_2).call();
121 RevCommit c2 = git.commit().setMessage("change file2").call();
122
123 try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
124 generator.push(null, db.resolve(Constants.HEAD));
125 assertEquals(3, generator.getResultContents().size());
126
127 assertTrue(generator.next());
128 assertEquals(c2, generator.getSourceCommit());
129 assertEquals(1, generator.getRegionLength());
130 assertEquals(0, generator.getResultStart());
131 assertEquals(1, generator.getResultEnd());
132 assertEquals(0, generator.getSourceStart());
133 assertEquals(1, generator.getSourceEnd());
134 assertEquals(FILENAME_2, generator.getSourcePath());
135
136 assertTrue(generator.next());
137 assertEquals(c1, generator.getSourceCommit());
138 assertEquals(2, generator.getRegionLength());
139 assertEquals(1, generator.getResultStart());
140 assertEquals(3, generator.getResultEnd());
141 assertEquals(0, generator.getSourceStart());
142 assertEquals(2, generator.getSourceEnd());
143 assertEquals(FILENAME_1, generator.getSourcePath());
144
145 assertFalse(generator.next());
146 }
147
148
149 try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
150 generator.push(null, db.resolve(Constants.HEAD));
151 BlameResult result = generator.computeBlameResult();
152
153 assertEquals(3, result.getResultContents().size());
154
155 assertEquals(c2, result.getSourceCommit(0));
156 assertEquals(FILENAME_2, result.getSourcePath(0));
157
158 assertEquals(c1, result.getSourceCommit(1));
159 assertEquals(FILENAME_1, result.getSourcePath(1));
160
161 assertEquals(c1, result.getSourceCommit(2));
162 assertEquals(FILENAME_1, result.getSourcePath(2));
163 }
164 }
165 }
166
167 @Test
168 public void testLinesAllDeletedShortenedWalk() throws Exception {
169 try (Git git = new Git(db)) {
170 String[] content1 = new String[] { "first", "second", "third" };
171
172 writeTrashFile("file.txt", join(content1));
173 git.add().addFilepattern("file.txt").call();
174 git.commit().setMessage("create file").call();
175
176 String[] content2 = new String[] { "" };
177
178 writeTrashFile("file.txt", join(content2));
179 git.add().addFilepattern("file.txt").call();
180 git.commit().setMessage("create file").call();
181
182 writeTrashFile("file.txt", join(content1));
183 git.add().addFilepattern("file.txt").call();
184 RevCommit c3 = git.commit().setMessage("create file").call();
185
186 try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
187 generator.push(null, db.resolve(Constants.HEAD));
188 assertEquals(3, generator.getResultContents().size());
189
190 assertTrue(generator.next());
191 assertEquals(c3, generator.getSourceCommit());
192 assertEquals(0, generator.getResultStart());
193 assertEquals(3, generator.getResultEnd());
194
195 assertFalse(generator.next());
196 }
197 }
198 }
199
200 private static String join(String... lines) {
201 StringBuilder joined = new StringBuilder();
202 for (String line : lines)
203 joined.append(line).append('\n');
204 return joined.toString();
205 }
206 }