-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffUtil.java
103 lines (92 loc) · 2.82 KB
/
DiffUtil.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package javaToolkit.lib.utils;
import java.util.ArrayList;
import java.util.List;
public class DiffUtil {
public static List<Integer> getChangedLineNumListInOldVersion(String diffStr) {
List<Integer> changedLineNumList = new ArrayList<>();
try {
for (String line : diffStr.split("\n")) {
if (line.startsWith("@@")) {
for (String tmp : line.split(" ")) {
if (tmp.trim().startsWith("-")) {
if (tmp.contains(",")) {
changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "").split(",")[0]));
} else {
changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "")));
}
}
}
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return changedLineNumList;
}
public static Integer getChangedLineCount(String diffStr, String mode) {
int count = 0;
String symbol = null;
try {
if ("delete".equals(mode)) {
symbol = "-";
} else if ("add".equals(mode)) {
symbol = "+";
} else {
throw new Exception("Only have delete or add mode!");
}
for (String line : diffStr.split("\n")) {
if (line.startsWith(symbol) && !line.startsWith("---") && !line.startsWith("+++")) {
count++;
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
return count;
}
public static List<Integer> getChangedLineNumListInNewVersion(String diffFilePath) {
List<Integer> changedLineNumList = new ArrayList<>();
try {
List<String> diffFileStrList = FileUtil.readFileToLineList(diffFilePath);
for (String line : diffFileStrList) {
if (line.startsWith("@@")) {
for (String tmp : line.split(" ")) {
if (tmp.trim().startsWith("+")) {
if (tmp.contains(",")) {
changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "").split(",")[0]));
} else {
changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "")));
}
}
}
}
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(diffFilePath);
e.printStackTrace();
}
return changedLineNumList;
}
public static List<String> getModifiedFileList(String diffFilePath) {
/**
* can to be further improved in future
*/
List<String> modifiedFileRelPathList = new ArrayList<String>();
for (String line : FileUtil.readFileToLineList(diffFilePath)) {
if (line.startsWith("diff --git ")) {
// Heuristic
String fileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim();
modifiedFileRelPathList.add(fileRelPath);
}
}
return modifiedFileRelPathList;
}
public static void main(String[] args) {
String diffFilePath = "/media/sf__3_fix_groups/RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE/delete redundent null check/27769/diff.txt";
getModifiedFileList(diffFilePath);
}
}