Skip to content

Commit b60dcbd

Browse files
committed
2 parents 95f63d3 + cb39bbd commit b60dcbd

File tree

5 files changed

+170
-61
lines changed

5 files changed

+170
-61
lines changed

lib/utils/DiffUtil.java

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

3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
35
import java.util.ArrayList;
46
import java.util.List;
57

@@ -29,27 +31,32 @@ public static List<Integer> getChangedLineNumListInOldVersion(String diffStr) {
2931
return changedLineNumList;
3032
}
3133

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

3443
int count = 0;
3544
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("+++")) {
45+
if ("delete".equals(mode)) {
46+
symbol = "-";
47+
} else if ("add".equals(mode)) {
48+
symbol = "+";
49+
}
50+
for (String line : diffStr.split("\n")) {
51+
if (line.startsWith(symbol) && !line.startsWith("---") && !line.startsWith("+++")) {
52+
// if the line is comment
53+
if (line.substring(1).trim().startsWith("*") || line.substring(1).trim().startsWith("/*")
54+
|| line.substring(1).trim().startsWith("//")) {
55+
continue;
56+
} else {
4657
count++;
4758
}
4859
}
49-
} catch (Exception e) {
50-
// TODO: handle exception
51-
e.printStackTrace();
52-
return null;
5360
}
5461
return count;
5562
}
@@ -77,10 +84,11 @@ public static List<Integer> getChangedLineNumListInNewVersion(String diffStr) {
7784
return changedLineNumList;
7885
}
7986

80-
public static List<String> getModifiedFileList(String diffFilePath) {
87+
public static List<String> getModifiedFileList(String diffFilePathString) {
8188
/**
8289
* can to be further improved in future
8390
*/
91+
Path diffFilePath = Paths.get(diffFilePathString);
8492
List<String> modifiedFileRelPathList = new ArrayList<String>();
8593
for (String line : FileUtil.readFileToLineList(diffFilePath)) {
8694
if (line.startsWith("diff --git ")) {

lib/utils/FileUtil.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public boolean accept(File current, String name) {
5252
return directories;
5353
}
5454

55-
public static String readFile2Str(String fpath) {
55+
public static String readFile2Str(Path fpath) {
5656
String content = "";
5757
try {
58-
content = new String(Files.readAllBytes(Paths.get(fpath)));
58+
content = new String(Files.readAllBytes(fpath));
5959
} catch (IOException e) {
6060
System.out.printf("%s not found!", fpath);
6161
e.printStackTrace();
@@ -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
@@ -139,20 +155,20 @@ public static Boolean copyFile2File(File srcFile, File dstFile) {
139155
return true;
140156
}
141157

142-
public static boolean deleteDirectory(String dirPath) {
143-
File directoryToBeDeleted = new File(dirPath);
158+
public static boolean deleteDirectory(Path dirPath) {
159+
File directoryToBeDeleted = dirPath.toFile();
144160
File[] allContents = directoryToBeDeleted.listFiles();
145161
if (allContents != null) {
146162
for (File file : allContents) {
147-
deleteDirectory(file.getAbsolutePath());
163+
deleteDirectory(file.toPath());
148164
}
149165
}
150166
return directoryToBeDeleted.delete();
151167
}
152168

153-
public static void copyFolder(String srcDirStr, String destDirStr) {
154-
File source = new File(srcDirStr);
155-
File dest = new File(destDirStr);
169+
public static void copyDirectory(Path srcDirPath, Path destDirPath) {
170+
File source = srcDirPath.toFile();
171+
File dest = destDirPath.toFile();
156172
try {
157173
FileUtils.copyDirectory(source, dest);
158174
} catch (IOException e) {
@@ -219,14 +235,14 @@ public static String readStringFromFile(String filePath) {
219235
* @param filePath
220236
* @return
221237
*/
222-
public static List<String> readFileToLineList(String filePath) {
238+
public static List<String> readFileToLineList(Path filePath) {
223239
ArrayList<String> strList = new ArrayList<String>();
224240
try {
225-
if (!new File(filePath).exists()) {
241+
if (!filePath.toFile().exists()) {
226242
System.out.println("File not exists! " + filePath);
227243
return null;
228244
}
229-
BufferedReader reader = new BufferedReader(new FileReader(filePath));
245+
BufferedReader reader = new BufferedReader(new FileReader(filePath.toString()));
230246
String line = reader.readLine();
231247
while (line != null) {
232248
strList.add(line);
@@ -247,7 +263,7 @@ public static List<String> readFileToLineList(String filePath) {
247263
public static void createFolder(String dirStr, Boolean deleteIfExist) {
248264
File dir = new File(dirStr);
249265
if (deleteIfExist && dir.exists()) {
250-
deleteDirectory(dir.getAbsolutePath());
266+
deleteDirectory(dir.toPath());
251267
dir.mkdirs();
252268
} else if (!dir.exists()) {
253269
dir.mkdirs();
@@ -264,11 +280,11 @@ public static boolean deleteDirectory(File directoryToBeDeleted) {
264280
return directoryToBeDeleted.delete();
265281
}
266282

267-
public static List<File> findFilePathofSpecifcTypeRecusive(String tarDir, String extension) {
283+
public static List<File> findFilePathofSpecifcTypeRecusive(Path tarDir, String extension) {
268284

269285
List<File> fileList = new ArrayList<File>();
270286
try {
271-
Files.walk(Paths.get(tarDir)).filter(Files::isRegularFile).forEach((f) -> {
287+
Files.walk(tarDir).filter(Files::isRegularFile).forEach((f) -> {
272288
String filepath = f.toString();
273289
if (filepath.endsWith(extension))
274290
// System.out.println(file + " found!");

lib/utils/GitUtil.java

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,18 @@
33
import java.nio.file.Path;
44
import java.nio.file.Paths;
55
import java.util.Arrays;
6-
import java.util.Date;
7-
import java.util.HashSet;
86
import java.util.List;
9-
import java.util.Set;
107

118
public class GitUtil {
129

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-
31-
public static Boolean clone(String repoName, String usrName, Path targetDir) {
10+
public static Boolean clone(String usrName, String repoName, Path targetDir) {
3211

3312
if (targetDir.toFile().exists()) {
3413
FileUtil.deleteDirectory(targetDir.toFile());
3514
}
3615
targetDir.toFile().mkdirs();
3716

38-
String cmd = "timeout 300 git clone https://github.com/" + repoName + "/" + usrName + " " + targetDir;
17+
String cmd = "timeout 600 git clone https://github.com/" + usrName + "/" + repoName + " " + targetDir;
3918

4019
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, targetDir, 0);
4120
if (pr.exitCode == 0) {
@@ -85,6 +64,24 @@ public static List<String> getComsWithSingleWordMatch(Path repoDir, String word)
8564
}
8665
}
8766

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

9087
String cmd = "timeout 300 git log --format=%B -n 1 " + com;
@@ -164,23 +161,77 @@ public static List<String> getChangedFileList(Path repoDir, String com) {
164161
}
165162
}
166163

167-
public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
164+
public static Boolean checkoutDefaultBranch(Path repoDir, Boolean ifForce) {
168165

169166
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
170167

171168
if (ifForce) {
172-
String resetCMD = "timeout 300 git reset --hard";
169+
String resetCMD = "timeout 600 git reset --hard";
173170
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
174171
}
175172

173+
String defaultBranchNameString = getDefaultBranch(repoDir);
174+
176175
String checkoutCMD = null;
177-
if (com == null) { // checkout latest if null
178-
checkoutCMD = "timeout 300 git checkout master";
176+
checkoutCMD = "timeout 300 git checkout " + defaultBranchNameString;
177+
178+
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
179+
180+
if (pr.err.contains("fatal: index file smaller than expected")) {
181+
Path lockFilePath = Paths.get(repoDir.toString() + "/.git/index.lock");
182+
if (lockFilePath.toFile().exists()) {
183+
lockFilePath.toFile().delete();
184+
}
185+
Path indexFilePath = Paths.get(repoDir.toString() + "/.git/index");
186+
if (indexFilePath.toFile().exists()) {
187+
indexFilePath.toFile().delete();
188+
}
189+
}
190+
191+
if (pr.exitCode == 0) {
192+
return true;
179193
} else {
180-
checkoutCMD = "timeout 300 git checkout " + com;
194+
String cleanCMD = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree "
195+
+ repoDir.toString() + " clean -dfx .";
196+
pr = ProcessUtil.executeCMD(cleanCMD, null, repoDir, 0);
197+
String resetCMD = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree "
198+
+ repoDir.toString() + " reset --hard";
199+
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
200+
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
201+
if (pr.exitCode == 0) {
202+
return true;
203+
} else {
204+
return false;
205+
}
206+
}
207+
}
208+
209+
public static Boolean checkoutCommit(Path repoDir, String com, Boolean ifForce) {
210+
211+
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
212+
213+
if (ifForce) {
214+
String resetCMD = "timeout 600 git reset --hard";
215+
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
181216
}
217+
218+
String defaultBranchNameString = getDefaultBranch(repoDir);
219+
220+
String checkoutCMD = null;
221+
checkoutCMD = "timeout 300 git checkout " + com;
182222
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
183223

224+
if (pr.err.contains("fatal: index file smaller than expected")) {
225+
Path lockFilePath = Paths.get(repoDir.toString() + "/.git/index.lock");
226+
if (lockFilePath.toFile().exists()) {
227+
lockFilePath.toFile().delete();
228+
}
229+
Path indexFilePath = Paths.get(repoDir.toString() + "/.git/index");
230+
if (indexFilePath.toFile().exists()) {
231+
indexFilePath.toFile().delete();
232+
}
233+
}
234+
184235
if (pr.exitCode == 0) {
185236
return true;
186237
} else {
@@ -199,4 +250,18 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
199250
}
200251
}
201252

253+
private static String getDefaultBranch(Path gitDirPath) {
254+
// TODO Auto-generated method stub
255+
String cmdString = "git symbolic-ref refs/remotes/origin/HEAD";
256+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmdString, null, gitDirPath, 0);
257+
258+
// output refs/remotes/origin/master
259+
if (pr.out.trim().contains("refs/remotes/origin/")) {
260+
return pr.out.trim().split("refs/remotes/origin/")[1].trim();
261+
} else {
262+
return "master";
263+
}
264+
265+
}
266+
202267
}

lib/utils/ProcessUtil.java

Lines changed: 3 additions & 2 deletions
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,13 +68,16 @@ 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) {
7676
throw new Exception();
7777
}
7878
} catch (Exception e) {
7979
TimeUtil.printCurTimewithMsg(pr.toString());
80+
TimeUtil.printCurTimewithMsg("WorkingDir " + workDir.toString());
8081
e.printStackTrace();
8182
}
8283

0 commit comments

Comments
 (0)