|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
| 6 | +import org.jaxen.function.StartsWithFunction; |
| 7 | + |
6 | 8 | public class DiffUtil {
|
7 |
| - |
8 |
| - |
9 |
| - public static List<Integer> getChangedLineNumListInOldVersion(String diffFilePath) { |
10 | 9 |
|
11 |
| - List<Integer> changedLineNumList = new ArrayList<>(); |
12 |
| - try { |
13 |
| - List<String> diffFileStrList = FileUtil.readFileToLineList(diffFilePath); |
14 |
| - for (String line : diffFileStrList) { |
15 |
| - if (line.startsWith("@@")) { |
16 |
| - for (String tmp : line.split(" ")) { |
17 |
| - if (tmp.trim().startsWith("-")) { |
18 |
| - if (tmp.contains(",")) { |
19 |
| - changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "").split(",")[0])); |
20 |
| - } else { |
21 |
| - changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", ""))); |
22 |
| - } |
23 |
| - } |
24 |
| - } |
25 |
| - } |
26 |
| - } |
27 |
| - } catch (Exception e) { |
28 |
| - // TODO: handle exception |
29 |
| - System.out.println(diffFilePath); |
30 |
| - System.exit(0); |
31 |
| - } |
32 |
| - return changedLineNumList; |
33 |
| - } |
| 10 | + public static List<Integer> getChangedLineNumListInOldVersion(String diffStr) { |
| 11 | + |
| 12 | + List<Integer> changedLineNumList = new ArrayList<>(); |
| 13 | + try { |
| 14 | + for (String line : diffStr.split("\n")) { |
| 15 | + if (line.startsWith("@@")) { |
| 16 | + for (String tmp : line.split(" ")) { |
| 17 | + if (tmp.trim().startsWith("-")) { |
| 18 | + if (tmp.contains(",")) { |
| 19 | + changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "").split(",")[0])); |
| 20 | + } else { |
| 21 | + changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", ""))); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + } catch (Exception e) { |
| 28 | + // TODO: handle exception |
| 29 | + e.printStackTrace(); |
| 30 | + } |
| 31 | + return changedLineNumList; |
| 32 | + } |
| 33 | + |
| 34 | + public static Integer getChangedLineCount(String diffStr, String mode) { |
| 35 | + |
| 36 | + int count = 0; |
| 37 | + String symbol = null; |
| 38 | + try { |
| 39 | + if ("delete".equals(mode)) { |
| 40 | + symbol = "-"; |
| 41 | + } else if ("add".equals(mode)) { |
| 42 | + symbol = "+"; |
| 43 | + } else { |
| 44 | + throw new Exception("Only have delete or add mode!"); |
| 45 | + } |
| 46 | + for (String line : diffStr.split("\n")) { |
| 47 | + if (line.startsWith(symbol) && !line.startsWith("---") && !line.startsWith("+++")) { |
| 48 | + count++; |
| 49 | + } |
| 50 | + } |
| 51 | + } catch (Exception e) { |
| 52 | + // TODO: handle exception |
| 53 | + e.printStackTrace(); |
| 54 | + return null; |
| 55 | + } |
| 56 | + return count; |
| 57 | + } |
34 | 58 |
|
35 |
| - public static List<Integer> getChangedLineNumListInNewVersion(String diffFilePath) { |
| 59 | + public static List<Integer> getChangedLineNumListInNewVersion(String diffFilePath) { |
36 | 60 |
|
37 |
| - List<Integer> changedLineNumList = new ArrayList<>(); |
38 |
| - try { |
39 |
| - List<String> diffFileStrList = FileUtil.readFileToLineList(diffFilePath); |
40 |
| - for (String line : diffFileStrList) { |
41 |
| - if (line.startsWith("@@")) { |
42 |
| - for (String tmp : line.split(" ")) { |
43 |
| - if (tmp.trim().startsWith("+")) { |
44 |
| - if (tmp.contains(",")) { |
45 |
| - changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "").split(",")[0])); |
46 |
| - } else { |
47 |
| - changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", ""))); |
48 |
| - } |
49 |
| - } |
50 |
| - } |
51 |
| - } |
52 |
| - } |
53 |
| - } catch (Exception e) { |
54 |
| - // TODO: handle exception |
55 |
| - System.out.println(diffFilePath); |
56 |
| - System.exit(0); |
57 |
| - } |
58 |
| - return changedLineNumList; |
59 |
| - } |
| 61 | + List<Integer> changedLineNumList = new ArrayList<>(); |
| 62 | + try { |
| 63 | + List<String> diffFileStrList = FileUtil.readFileToLineList(diffFilePath); |
| 64 | + for (String line : diffFileStrList) { |
| 65 | + if (line.startsWith("@@")) { |
| 66 | + for (String tmp : line.split(" ")) { |
| 67 | + if (tmp.trim().startsWith("+")) { |
| 68 | + if (tmp.contains(",")) { |
| 69 | + changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "").split(",")[0])); |
| 70 | + } else { |
| 71 | + changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", ""))); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } catch (Exception e) { |
| 78 | + // TODO: handle exception |
| 79 | + System.out.println(diffFilePath); |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + return changedLineNumList; |
| 83 | + } |
60 | 84 |
|
61 |
| - public static List<String> getModifiedFileList(String diffFilePath) { |
62 |
| - /** |
63 |
| - * can to be further improved in future |
64 |
| - */ |
65 |
| - List<String> modifiedFileRelPathList = new ArrayList<String>(); |
66 |
| - for (String line : FileUtil.readFileToLineList(diffFilePath)) { |
67 |
| - if (line.startsWith("diff --git ")) { |
68 |
| - // Heuristic |
69 |
| - String fileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim(); |
70 |
| - modifiedFileRelPathList.add(fileRelPath); |
71 |
| - } |
72 |
| - } |
73 |
| - return modifiedFileRelPathList; |
74 |
| - } |
| 85 | + public static List<String> getModifiedFileList(String diffFilePath) { |
| 86 | + /** |
| 87 | + * can to be further improved in future |
| 88 | + */ |
| 89 | + List<String> modifiedFileRelPathList = new ArrayList<String>(); |
| 90 | + for (String line : FileUtil.readFileToLineList(diffFilePath)) { |
| 91 | + if (line.startsWith("diff --git ")) { |
| 92 | + // Heuristic |
| 93 | + String fileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim(); |
| 94 | + modifiedFileRelPathList.add(fileRelPath); |
| 95 | + } |
| 96 | + } |
| 97 | + return modifiedFileRelPathList; |
| 98 | + } |
75 | 99 |
|
76 |
| - public static void main(String[] args) { |
77 |
| - String diffFilePath = "/media/sf__3_fix_groups/RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE/delete redundent null check/27769/diff.txt"; |
78 |
| - getModifiedFileList(diffFilePath); |
79 |
| - } |
| 100 | + public static void main(String[] args) { |
| 101 | + String diffFilePath = "/media/sf__3_fix_groups/RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE/delete redundent null check/27769/diff.txt"; |
| 102 | + getModifiedFileList(diffFilePath); |
| 103 | + } |
80 | 104 |
|
81 | 105 | }
|
0 commit comments