-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDiffParser.java
More file actions
166 lines (131 loc) · 4.63 KB
/
DiffParser.java
File metadata and controls
166 lines (131 loc) · 4.63 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DiffParser {
private ArrayList<String> listOfFiles = new ArrayList<>();
private ArrayList<String> listOfUniqueFiles = new ArrayList<>();
private int numberOfFiles;
private ArrayList<String> listOfFunctions = new ArrayList<>();
private ArrayList<String> listOfUniqueFunctions = new ArrayList<>();
private int numberOfFunctions, numberOfFunctionsCalls;
private String folderPath;
private int numberOfRegions, insertions, deletions;
public DiffParser(String folderPath) {
this.folderPath = folderPath;
}
public void executeParser() {
File folder = new File(folderPath);
for (File file : folder.listFiles()) {
Scanner input = null;
try {
input = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(input.hasNextLine()) {
String line = input.nextLine();
if (line.startsWith("+++")){
listOfFiles.add(line.substring(4));
continue;
}
// deletions
if (line.startsWith("-") && !line.startsWith("--- ")) {
deletions++;
continue;
}
// regions
if (line.startsWith("@@")) {
numberOfRegions++;
continue;
}
// insertions
if (line.startsWith("+")) {
insertions++;
// functions
//TODO remove it
Pattern p = Pattern.compile("\\b[A-Za-z_]([A-Za-z0-9_]+)\\(");
Matcher m = p.matcher(line);
while (m.find()) {
String functionName = m.group();
listOfFunctions.add(functionName.substring(0, functionName.length()-1));
}
}
}
input.close();
}
// files
//sort
listOfFiles.sort(String::compareToIgnoreCase);
//remove duplicates if any
Set uniqueFiles = new LinkedHashSet(listOfFiles);
listOfUniqueFiles.addAll(uniqueFiles);
numberOfFiles = listOfUniqueFiles.size();
// functions
//sort
listOfFunctions.sort(String::compareToIgnoreCase);
numberOfFunctionsCalls = listOfFunctions.size();
//remove duplicates if any
Set uniqueFunctions = new LinkedHashSet(listOfFunctions);
listOfUniqueFunctions.addAll(uniqueFunctions);
numberOfFunctions = listOfUniqueFunctions.size();
}
public void listFiles(){
System.out.println("> Listing changed files:");
for (String f : this.listOfUniqueFiles){
int n = Collections.frequency(listOfFiles, f);
System.out.printf("%02d | %s\n", n, f);
}
}
public void listFunctions(){
System.out.println("> Listing functions:");
for (String f: listOfUniqueFunctions) {
int numCalls = Collections.frequency(listOfFunctions, f);
System.out.printf("%02d | %s\n", numCalls, f);
}
}
// getters
public ArrayList<String> getListOfFiles() {
return listOfFiles;
}
public int getNumberOfFiles() {
return numberOfFiles;
}
public ArrayList<String> getListOfUniqueFunctions() {
return listOfUniqueFunctions;
}
public int getNumberOfFunctions() {
return numberOfFunctions;
}
public int getNumberOfFunctionsCalls() {
return numberOfFunctionsCalls;
}
public int getNumberOfRegions() {
return numberOfRegions;
}
public int getInsertions() {
return insertions;
}
public int getDeletions() {
return deletions;
}
public ArrayList<String> getListOfFunctions() {
return listOfFunctions;
}
public ArrayList<String> getListOfUniqueFiles() {
return listOfUniqueFiles;
}
@Override
public String toString() {
return "DiffParser{" +
"folderPath='" + folderPath + '\'' +
", numberOfFiles=" + numberOfFiles +
", numberOfFunctions=" + numberOfFunctions +
", numberOfFunctionsCalls=" + numberOfFunctionsCalls +
", numberOfRegions=" + numberOfRegions +
", insertions=" + insertions +
", deletions=" + deletions +
'}';
}
}