Skip to content

Commit 978b617

Browse files
authored
Merge pull request #21 from ciaranmooney/pmd-path-mismatch
Path mis-match when git-dir with relative path elements provided
2 parents 62c2135 + cd97d2d commit 978b617

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

diff-core/src/main/java/io/github/yangziwen/diff/calculate/DiffEntryWrapper.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.yangziwen.diff.calculate;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.util.List;
56

67
import org.eclipse.jgit.diff.DiffEntry;
@@ -53,7 +54,14 @@ public String getAbsoluteNewPath() {
5354
}
5455

5556
public File getNewFile() {
56-
return new File(gitDir, diffEntry.getNewPath());
57+
return new File(normaliseParent(gitDir), diffEntry.getNewPath());
5758
}
5859

60+
private static String normaliseParent(File directory) {
61+
try {
62+
return directory.getCanonicalPath();
63+
} catch (IOException e) {
64+
return "";
65+
}
66+
}
5967
}

diff-core/src/test/java/io/github/yangziwen/diff/calculate/DiffCalculatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testCalculateDiff() throws Exception {
7777
Assert.assertEquals(1, wrappers.size());
7878

7979
DiffEntryWrapper wrapper = wrappers.get(0);
80-
Assert.assertEquals(fileToChange, wrapper.getNewFile());
80+
Assert.assertEquals(fileToChange.getAbsolutePath(), wrapper.getNewFile().getAbsolutePath());
8181

8282
List<Edit> edits = wrapper.getEditList();
8383

@@ -138,7 +138,7 @@ public void testDoCalculateCommitDiffWhenFileChanged() throws Exception {
138138
Assert.assertEquals(1, wrappers.size());
139139

140140
DiffEntryWrapper wrapper = wrappers.get(0);
141-
Assert.assertEquals(fileToChange, wrapper.getNewFile());
141+
Assert.assertEquals(fileToChange.getAbsolutePath(), wrapper.getNewFile().getAbsolutePath());
142142

143143
List<Edit> edits = wrapper.getEditList();
144144

@@ -238,7 +238,7 @@ public void testDoCalculateIndexedDiffWhenFileChanged() throws Exception {
238238
Assert.assertEquals(1, wrappers.size());
239239

240240
DiffEntryWrapper wrapper = wrappers.get(0);
241-
Assert.assertEquals(fileToChange, wrapper.getNewFile());
241+
Assert.assertEquals(fileToChange.getAbsolutePath(), wrapper.getNewFile().getAbsolutePath());
242242

243243
List<Edit> edits = wrapper.getEditList();
244244

@@ -281,7 +281,7 @@ public void testDoCalculateIndexedDiffWhenFileAdded() throws Exception {
281281
Assert.assertEquals(1, wrappers.size());
282282

283283
DiffEntryWrapper wrapper = wrappers.get(0);
284-
Assert.assertEquals(fileToAdd, wrapper.getNewFile());
284+
Assert.assertEquals(fileToAdd.getAbsolutePath(), wrapper.getAbsoluteNewPath());
285285

286286
List<Edit> edits = wrapper.getEditList();
287287

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
package io.github.yangziwen.diff.calculate;
22

3+
import java.io.File;
4+
import java.io.IOException;
35
import java.util.Arrays;
46

57
import org.eclipse.jgit.diff.DiffEntry;
68
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
79
import org.eclipse.jgit.diff.Edit;
8-
import org.eclipse.jgit.diff.Edit.Type;
910
import org.junit.Assert;
1011
import org.junit.Test;
11-
import org.junit.runner.RunWith;
1212
import org.mockito.Mockito;
13-
import org.powermock.api.mockito.PowerMockito;
14-
import org.powermock.core.classloader.annotations.PrepareForTest;
15-
import org.powermock.modules.junit4.PowerMockRunner;
1613

17-
@RunWith(PowerMockRunner.class)
18-
@PrepareForTest({ Edit.class })
1914
public class DiffEntryWrapperTest {
2015

2116
@Test
@@ -30,14 +25,64 @@ public void testIsDeleted() {
3025

3126
@Test
3227
public void testIsAllDeletedEdits() {
33-
Edit edit1 = PowerMockito.mock(Edit.class);
34-
Edit edit2 = PowerMockito.mock(Edit.class);
35-
PowerMockito.when(edit1.getType()).thenReturn(Type.DELETE);
36-
PowerMockito.when(edit2.getType()).thenReturn(Type.DELETE);
3728
DiffEntryWrapper wrapper = DiffEntryWrapper.builder()
38-
.editList(Arrays.asList(edit1, edit2))
29+
.editList(Arrays.asList(buildDelete(), buildDelete()))
3930
.build();
4031
Assert.assertTrue(wrapper.isAllDeletedEdits());
4132
}
4233

34+
@Test
35+
public void getAbsoluteNewPath() throws IOException {
36+
String file = "file";
37+
String directory = "/some/directory";
38+
DiffEntryWrapper wrapper = DiffEntryWrapper.builder()
39+
.gitDir(new File(directory))
40+
.diffEntry(new DummyDiffEntry(file))
41+
.build();
42+
String result = wrapper.getAbsoluteNewPath();
43+
Assert.assertEquals(new File(directory, file).getCanonicalPath(), result);
44+
}
45+
46+
@Test
47+
public void getAbsoluteNewPathWithRelativeGitDir() throws IOException {
48+
String file = "file";
49+
DiffEntryWrapper wrapper = DiffEntryWrapper.builder()
50+
.gitDir(new File("."))
51+
.diffEntry(new DummyDiffEntry(file))
52+
.build();
53+
String result = wrapper.getAbsoluteNewPath();
54+
Assert.assertEquals(new File(".", file).getCanonicalPath(), result);
55+
}
56+
57+
@Test
58+
public void getAbsoluteNewPathException() throws IOException {
59+
File mock = Mockito.mock(File.class);
60+
Mockito.when(mock.getCanonicalPath()).thenThrow(new IOException(""));
61+
DiffEntryWrapper wrapper = DiffEntryWrapper.builder()
62+
.gitDir(mock)
63+
.diffEntry(new DummyDiffEntry(""))
64+
.build();
65+
String result = wrapper.getAbsoluteNewPath();
66+
Assert.assertEquals("/", result);
67+
}
68+
69+
// A "delete" edit is one where: beginA < endA && beginB == endB
70+
private static Edit buildDelete() {
71+
return new Edit(5, 10, 8, 8);
72+
}
73+
74+
private class DummyDiffEntry extends DiffEntry {
75+
76+
private final String filePath;
77+
78+
DummyDiffEntry(String filePath) {
79+
super();
80+
this.filePath = filePath;
81+
}
82+
83+
@Override
84+
public String getNewPath() {
85+
return filePath;
86+
}
87+
}
4388
}

0 commit comments

Comments
 (0)