Skip to content

Commit 2afe75e

Browse files
committed
improve
1 parent 5e89eac commit 2afe75e

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

Diff for: lib/utils/FileUtil.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ public static Boolean writeStr2File(String wStr, Path fPath) {
8484
return true;
8585
}
8686

87+
public static Boolean writeStrList2File(List<String> strList, Path fPath, Boolean ifTrim) {
88+
try {
89+
String wStr = "";
90+
for (String line : strList) {
91+
if (ifTrim) {
92+
line = line.trim();
93+
}
94+
wStr += (line + "\n");
95+
}
96+
Files.write(fPath, wStr.getBytes());
97+
} catch (IOException e) {
98+
e.printStackTrace();
99+
return false;
100+
}
101+
return true;
102+
}
103+
87104
public static Boolean copyFile2Dir(File f, String DestDir) {
88105
try {
89106
File DestDirFile = new File(DestDir);
@@ -93,7 +110,7 @@ public static Boolean copyFile2Dir(File f, String DestDir) {
93110
String targetFilePath = DestDirFile.getAbsoluteFile() + "/" + f.getName();
94111
FileUtils.copyFile(f, new File(targetFilePath));
95112
} catch (IOException e) {
96-
113+
97114
e.printStackTrace();
98115
return false;
99116
}
@@ -104,7 +121,7 @@ public static Boolean copyFile2File(File srcFile, File dstFile) {
104121
try {
105122
FileUtils.copyFile(srcFile, dstFile);
106123
} catch (IOException e) {
107-
124+
108125
e.printStackTrace();
109126
return false;
110127
}
@@ -206,7 +223,7 @@ public static List<String> readFileToLineList(String filePath) {
206223
}
207224
reader.close();
208225
} catch (FileNotFoundException e) {
209-
226+
210227
System.out.println("filePath : " + filePath);
211228
e.printStackTrace();
212229
} catch (IOException e) {
@@ -247,7 +264,7 @@ public static List<File> findFilePathofSpecifcTypeRecusive(String tarDir, String
247264
fileList.add(new File(filepath));
248265
});
249266
} catch (IOException e) {
250-
267+
251268
e.printStackTrace();
252269
}
253270
return fileList;
@@ -264,7 +281,7 @@ public static List<String> findRelativeFilePathofSpecifcTypeRecusive(String tarD
264281
relPathList.add(filepath.replace(tarDir, ""));
265282
});
266283
} catch (IOException e) {
267-
284+
268285
e.printStackTrace();
269286
}
270287
return relPathList;

Diff for: lib/utils/GitUtil.java

+19-13
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,28 @@
88
public class GitUtil {
99

1010
public static Boolean clone(String repoName, String usrName, Path targetDir) {
11+
12+
if(targetDir.toFile().exists()){
13+
FileUtil.deleteDirectory(targetDir.toFile());
14+
}
15+
targetDir.toFile().mkdirs();
1116

12-
String cmd = "timeout 600 git clone https://github.com/" + repoName + "/" + usrName + " " + targetDir;
13-
17+
String cmd = "timeout 300 git clone https://github.com/" + repoName + "/" + usrName + " " + targetDir;
18+
1419
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, targetDir);
1520
if (pr.exitCode == 0) {
1621
return true;
1722
} else {
1823
System.out.println("cmd " + cmd + "\n");
1924
System.out.println("report \n" + pr.toString());
25+
FileUtil.deleteDirectory(targetDir.toFile());
2026
return false;
2127
}
2228
}
2329

2430
public static List<String> getAllCommitsSha(Path repoDir) {
2531

26-
String cmd = "timeout 600 git log --pretty=format:\"%H\"";
32+
String cmd = "timeout 300 git log --pretty=format:\"%H\"";
2733

2834
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
2935
if (pr.exitCode == 0) {
@@ -37,7 +43,7 @@ public static List<String> getAllCommitsSha(Path repoDir) {
3743

3844
public static String getCommitMsg(Path repoDir, String com) {
3945

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

4248
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
4349
if (pr.exitCode == 0) {
@@ -51,7 +57,7 @@ public static String getCommitMsg(Path repoDir, String com) {
5157

5258
public static String getParentCommit(Path repoDir, String com) {
5359

54-
String cmd = "git log --pretty=%P -n 1 " + com;
60+
String cmd = "timeout 300 git log --pretty=%P -n 1 " + com;
5561

5662
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
5763
if (pr.exitCode == 0) {
@@ -75,10 +81,10 @@ public static String getDiffBetween2Commits(Path repoDir, String oldCom, String
7581

7682
String cmd = null;
7783
if (diffMode != null) {
78-
cmd = "git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString() + " diff "
84+
cmd = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString() + " diff "
7985
+ diffMode + " --unified=0 " + oldCom + " " + newCom;
8086
} else {
81-
cmd = "git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
87+
cmd = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
8288
+ " diff --unified=0 " + oldCom + " " + newCom;
8389
}
8490

@@ -94,7 +100,7 @@ public static String getDiffBetween2Commits(Path repoDir, String oldCom, String
94100

95101
public static List<String> getChangedFileList(Path repoDir, String com) {
96102

97-
String cmd = "git diff-tree --no-commit-id --name-only -r " + com;
103+
String cmd = "timeout 300 git diff-tree --no-commit-id --name-only -r " + com;
98104

99105
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
100106
if (pr.exitCode == 0) {
@@ -111,25 +117,25 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
111117
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
112118

113119
if (ifForce) {
114-
String resetCMD = "git reset --hard";
120+
String resetCMD = "timeout 300 git reset --hard";
115121
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir);
116122
}
117123

118124
String checkoutCMD = null;
119125
if (com == null) { // checkout latest if null
120-
checkoutCMD = "git checkout master";
126+
checkoutCMD = "timeout 300 git checkout master";
121127
} else {
122-
checkoutCMD = "git checkout " + com;
128+
checkoutCMD = "timeout 300 git checkout " + com;
123129
}
124130
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir);
125131

126132
if (pr.exitCode == 0) {
127133
return true;
128134
} else {
129-
String cleanCMD = "git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
135+
String cleanCMD = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
130136
+ " clean -dfx .";
131137
pr = ProcessUtil.executeCMD(cleanCMD, null, repoDir);
132-
String resetCMD = "git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
138+
String resetCMD = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
133139
+ " reset --hard";
134140
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir);
135141
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir);

Diff for: lib/utils/ProcessUtil.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public String toString() {
3232

3333
/**
3434
* execute cmd
35+
*
3536
* @param cmd
3637
* @param envp
3738
* @param workDir
@@ -75,7 +76,7 @@ public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir
7576
throw new Exception();
7677
}
7778
} catch (Exception e) {
78-
pr.toString();
79+
TimeUtil.printCurTimewithMsg(pr.toString());
7980
e.printStackTrace();
8081
}
8182

Diff for: lib/utils/TimeUtil.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public static void printCurTime() {
1414
public static void printCurTimewithMsg(String msg) {
1515
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
1616
LocalDateTime now = LocalDateTime.now();
17-
System.out.println("Processing " + msg + " " + dtf.format(now));
17+
System.out.println(msg + "\t" + dtf.format(now));
18+
}
19+
20+
public static void main(String[] args) {
21+
printCurTimewithMsg("test");
1822
}
1923
}

0 commit comments

Comments
 (0)