Skip to content

Commit 07b19c0

Browse files
committed
improve GitUtil
1 parent f54b2f1 commit 07b19c0

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

Diff for: lib/utils/FileUtil.java

+16
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

Diff for: 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;

Diff for: 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)