Skip to content

Commit 1d34c59

Browse files
committed
2 parents c85a928 + 0d7c910 commit 1d34c59

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

lib/utils/DiffUtil.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,26 @@ public static List<Integer> getChangedLineNumListInOldVersion(String diffStr) {
2929
return changedLineNumList;
3030
}
3131

32-
public static Integer getChangedLineCount(String diffStr, String mode) {
32+
33+
/**
34+
* the mode can only be "add" or "delete"
35+
* @param diffStr
36+
* @param mode
37+
* @return
38+
*/
39+
public static int getChangedLineCount(String diffStr, String mode) {
3340

3441
int count = 0;
3542
String symbol = null;
36-
try {
37-
if ("delete".equals(mode)) {
38-
symbol = "-";
39-
} else if ("add".equals(mode)) {
40-
symbol = "+";
41-
} else {
42-
throw new Exception("Only have delete or add mode!");
43-
}
44-
for (String line : diffStr.split("\n")) {
45-
if (line.startsWith(symbol) && !line.startsWith("---") && !line.startsWith("+++")) {
46-
count++;
47-
}
43+
if ("delete".equals(mode)) {
44+
symbol = "-";
45+
} else if ("add".equals(mode)) {
46+
symbol = "+";
47+
}
48+
for (String line : diffStr.split("\n")) {
49+
if (line.startsWith(symbol) && !line.startsWith("---") && !line.startsWith("+++")) {
50+
count++;
4851
}
49-
} catch (Exception e) {
50-
// TODO: handle exception
51-
e.printStackTrace();
52-
return null;
5352
}
5453
return count;
5554
}

lib/utils/FileUtil.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ public static Boolean copyFile2Dir(File f, String DestDir) {
127127
return true;
128128
}
129129

130+
public static Boolean copyFile2Dir(Path fPath, Path DestDir) {
131+
try {
132+
File DestDirFile = DestDir.toFile();
133+
if (!DestDirFile.exists()) {
134+
DestDirFile.mkdirs();
135+
}
136+
Path targetFilePath = Paths.get(DestDirFile.getAbsolutePath(), fPath.toFile().getName());
137+
FileUtils.copyFile(fPath.toFile(), targetFilePath.toFile());
138+
} catch (IOException e) {
139+
140+
e.printStackTrace();
141+
return false;
142+
}
143+
return true;
144+
}
145+
130146
public static Boolean copyFile2File(File srcFile, File dstFile) {
131147
try {
132148
// dstfile will be overwrited if exist
@@ -150,7 +166,7 @@ public static boolean deleteDirectory(String dirPath) {
150166
return directoryToBeDeleted.delete();
151167
}
152168

153-
public static void copyFolder(String srcDirStr, String destDirStr) {
169+
public static void copyDirectory(String srcDirStr, String destDirStr) {
154170
File source = new File(srcDirStr);
155171
File dest = new File(destDirStr);
156172
try {

lib/utils/GitUtil.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,6 @@
1010

1111
public class GitUtil {
1212

13-
public static void main(String[] args) {
14-
Path repoDir = Paths.get("/data/bowen/data/PTBench-Data/data/2019/quarkusio#gizmo/raw_github");
15-
Set<String> mergeSet = new HashSet<>();
16-
Set<String> bugComs = new HashSet<String>(getComsWithSingleWordMatch(repoDir, "bug"));
17-
System.out.println("# bug coms " + String.valueOf(bugComs.size()));
18-
mergeSet.addAll(bugComs);
19-
Set<String> BugComs = new HashSet<String>(getComsWithSingleWordMatch(repoDir, "Bug"));
20-
System.out.println("# Bug coms " + String.valueOf(BugComs.size()));
21-
mergeSet.addAll(BugComs);
22-
Set<String> fixComs = new HashSet<String>(getComsWithSingleWordMatch(repoDir, "fix"));
23-
System.out.println("# fix coms " + String.valueOf(fixComs.size()));
24-
mergeSet.addAll(fixComs);
25-
Set<String> FixComs = new HashSet<String>(getComsWithSingleWordMatch(repoDir, "Fix"));
26-
System.out.println("# Fix coms " + String.valueOf(FixComs.size()));
27-
mergeSet.addAll(FixComs);
28-
System.out.println("# merge coms " + String.valueOf(mergeSet.size()));
29-
}
30-
3113
public static Boolean clone(String repoName, String usrName, Path targetDir) {
3214

3315
if (targetDir.toFile().exists()) {
@@ -85,6 +67,24 @@ public static List<String> getComsWithSingleWordMatch(Path repoDir, String word)
8567
}
8668
}
8769

70+
public static String getDiff4SingleFileNCommit(Path repoDir, String parCom, String curCom, String oldRelFilePath,
71+
String newRelFilePath) {
72+
73+
String cmd = "timeout 300 git --no-pager diff --unified=0 " + parCom + ":" + oldRelFilePath + " " + curCom + ":"
74+
+ newRelFilePath;
75+
76+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
77+
if (pr.exitCode == 0) {
78+
return pr.out.trim();
79+
} else {
80+
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getDiff4SingleFileNCommit.txt"));
81+
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getDiff4SingleFileNCommit.txt"));
82+
// System.out.println("cmd " + cmd + "\n");
83+
// System.out.println("report \n" + pr.toString());
84+
return null;
85+
}
86+
}
87+
8888
public static String getCommitMsg(Path repoDir, String com) {
8989

9090
String cmd = "timeout 300 git log --format=%B -n 1 " + com;

lib/utils/ProcessUtil.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir
4545
Runtime rt = Runtime.getRuntime();
4646
Process proc = rt.exec(cmd, envp, workDir.toFile());
4747

48-
pr.exitCode = proc.waitFor();
49-
5048
// Read the output from the command
5149
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
5250
// System.out.println("Here is the standard output of the
@@ -70,6 +68,8 @@ public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir
7068
}
7169
pr.err = err.trim();
7270

71+
pr.exitCode = proc.waitFor();
72+
7373
proc.destroy();
7474

7575
if (pr.exitCode != expectedExitCode) {

0 commit comments

Comments
 (0)