Skip to content

Commit b796adb

Browse files
committed
enhancement
1 parent 1ef1cd2 commit b796adb

File tree

4 files changed

+119
-81
lines changed

4 files changed

+119
-81
lines changed

lib/utils/CSVUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package javaToolkit.lib.utils;
22

33
import java.io.FileWriter;
4+
import java.io.Writer;
45
import java.nio.file.Path;
56
import java.util.ArrayList;
67
import java.util.List;
@@ -45,8 +46,21 @@ public static void appendLine2Csv(List<String> strList, Path filePath) {
4546
}
4647
}
4748

49+
/**
50+
* if " (double quotation mark) exists in a cell, then the cell needs to be
51+
* replace("\"","\"\"") and add " before and after the cell
52+
*
53+
* the implementation is cell="\""+cellString.replace("\"","\"\"")+"\""
54+
*
55+
* @param strList
56+
* @param filePath
57+
*/
4858
public static void write2DArray2Csv(List<List<String>> strList, Path filePath) {
4959
try {
60+
if (filePath.toFile().exists()) {
61+
filePath.toFile().delete();
62+
}
63+
5064
FileWriter writer = null;
5165
for (List<String> line : strList) {
5266
String collect = line.stream().collect(Collectors.joining(","));

lib/utils/DiffUtil.java

Lines changed: 92 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,103 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import org.jaxen.function.StartsWithFunction;
7+
68
public class DiffUtil {
7-
8-
9-
public static List<Integer> getChangedLineNumListInOldVersion(String diffFilePath) {
109

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+
}
3458

35-
public static List<Integer> getChangedLineNumListInNewVersion(String diffFilePath) {
59+
public static List<Integer> getChangedLineNumListInNewVersion(String diffFilePath) {
3660

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+
}
6084

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+
}
7599

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+
}
80104

81105
}

lib/utils/FileUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public static Boolean copyFile2Dir(File f, String DestDir) {
131131

132132
public static Boolean copyFile2File(File srcFile, File dstFile) {
133133
try {
134+
// dstfile will be overwrited if exist
134135
FileUtils.copyFile(srcFile, dstFile);
135136
} catch (IOException e) {
136137

lib/utils/GitUtil.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.nio.file.Path;
44
import java.nio.file.Paths;
5-
import java.sql.Array;
65
import java.util.Arrays;
76
import java.util.List;
87

@@ -32,8 +31,8 @@ public static Boolean clone(String repoName, String usrName, Path targetDir) {
3231

3332
public static List<String> getAllCommitsSha(Path repoDir) {
3433

35-
String cmd = "timeout 60 git log --pretty=format:\"%H\"";
36-
34+
String cmd = "timeout 300 git --no-pager log --all --pretty=format:\"%H\"";
35+
3736
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
3837
if (pr.exitCode == 0) {
3938
return Arrays.asList(pr.out.replace("\"", "").split("\n"));
@@ -48,7 +47,7 @@ public static List<String> getAllCommitsSha(Path repoDir) {
4847

4948
public static String getCommitMsg(Path repoDir, String com) {
5049

51-
String cmd = "timeout 60 git log --format=%B -n 1 " + com;
50+
String cmd = "timeout 300 git log --format=%B -n 1 " + com;
5251

5352
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
5453
if (pr.exitCode == 0) {
@@ -64,7 +63,7 @@ public static String getCommitMsg(Path repoDir, String com) {
6463

6564
public static String getParentCommit(Path repoDir, String com) {
6665

67-
String cmd = "timeout 60 git log --pretty=%P -n 1 " + com;
66+
String cmd = "timeout 300 git log --pretty=%P -n 1 " + com;
6867

6968
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
7069
if (pr.exitCode == 0) {
@@ -90,10 +89,10 @@ public static String getDiffBetween2Commits(Path repoDir, String oldCom, String
9089

9190
String cmd = null;
9291
if (diffMode != null) {
93-
cmd = "timeout 60 git --git-dir " + repoDir.toString() + "/.git --work-tree " + repoDir.toString()
92+
cmd = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree " + repoDir.toString()
9493
+ " diff " + diffMode + " --unified=0 " + oldCom + " " + newCom;
9594
} else {
96-
cmd = "timeout 60 git --git-dir " + repoDir.toString() + "/.git --work-tree " + repoDir.toString()
95+
cmd = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree " + repoDir.toString()
9796
+ " diff --unified=0 " + oldCom + " " + newCom;
9897
}
9998

@@ -111,7 +110,7 @@ public static String getDiffBetween2Commits(Path repoDir, String oldCom, String
111110

112111
public static List<String> getChangedFileList(Path repoDir, String com) {
113112

114-
String cmd = "timeout 30 git diff-tree --no-commit-id --name-only -r " + com;
113+
String cmd = "timeout 300 git diff-tree --no-commit-id --name-only -r " + com;
115114

116115
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
117116
if (pr.exitCode == 0) {
@@ -130,25 +129,25 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
130129
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
131130

132131
if (ifForce) {
133-
String resetCMD = "timeout 60 git reset --hard";
132+
String resetCMD = "timeout 300 git reset --hard";
134133
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
135134
}
136135

137136
String checkoutCMD = null;
138137
if (com == null) { // checkout latest if null
139-
checkoutCMD = "timeout 60 git checkout master";
138+
checkoutCMD = "timeout 300 git checkout master";
140139
} else {
141-
checkoutCMD = "timeout 60 git checkout " + com;
140+
checkoutCMD = "timeout 300 git checkout " + com;
142141
}
143142
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
144143

145144
if (pr.exitCode == 0) {
146145
return true;
147146
} else {
148-
String cleanCMD = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree "
147+
String cleanCMD = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree "
149148
+ repoDir.toString() + " clean -dfx .";
150149
pr = ProcessUtil.executeCMD(cleanCMD, null, repoDir, 0);
151-
String resetCMD = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree "
150+
String resetCMD = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree "
152151
+ repoDir.toString() + " reset --hard";
153152
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
154153
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);

0 commit comments

Comments
 (0)