Skip to content

Commit 328d72b

Browse files
committed
add expected exit code support
1 parent b25ec0b commit 328d72b

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

lib/utils/GitUtil.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
public class GitUtil {
99

1010
public static Boolean clone(String repoName, String usrName, Path targetDir) {
11-
12-
if(targetDir.toFile().exists()){
11+
12+
if (targetDir.toFile().exists()) {
1313
FileUtil.deleteDirectory(targetDir.toFile());
1414
}
1515
targetDir.toFile().mkdirs();
1616

1717
String cmd = "timeout 300 git clone https://github.com/" + repoName + "/" + usrName + " " + targetDir;
18-
19-
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, targetDir);
18+
19+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, targetDir, 0);
2020
if (pr.exitCode == 0) {
2121
return true;
2222
} else {
@@ -31,7 +31,7 @@ public static List<String> getAllCommitsSha(Path repoDir) {
3131

3232
String cmd = "timeout 60 git log --pretty=format:\"%H\"";
3333

34-
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
34+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
3535
if (pr.exitCode == 0) {
3636
return Arrays.asList(pr.out.replace("\"", "").split("\n"));
3737
} else {
@@ -45,7 +45,7 @@ public static String getCommitMsg(Path repoDir, String com) {
4545

4646
String cmd = "timeout 60 git log --format=%B -n 1 " + com;
4747

48-
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
48+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
4949
if (pr.exitCode == 0) {
5050
return pr.out.trim();
5151
} else {
@@ -59,7 +59,7 @@ public static String getParentCommit(Path repoDir, String com) {
5959

6060
String cmd = "timeout 60 git log --pretty=%P -n 1 " + com;
6161

62-
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
62+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
6363
if (pr.exitCode == 0) {
6464
return pr.out.replace("\"", "").trim();
6565
} else {
@@ -81,14 +81,14 @@ public static String getDiffBetween2Commits(Path repoDir, String oldCom, String
8181

8282
String cmd = null;
8383
if (diffMode != null) {
84-
cmd = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString() + " diff "
85-
+ diffMode + " --unified=0 " + oldCom + " " + newCom;
84+
cmd = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
85+
+ " diff " + diffMode + " --unified=0 " + oldCom + " " + newCom;
8686
} else {
8787
cmd = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
8888
+ " diff --unified=0 " + oldCom + " " + newCom;
8989
}
9090

91-
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
91+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
9292
if (pr.exitCode == 0) {
9393
return pr.out.trim();
9494
} else {
@@ -102,7 +102,7 @@ public static List<String> getChangedFileList(Path repoDir, String com) {
102102

103103
String cmd = "timeout 30 git diff-tree --no-commit-id --name-only -r " + com;
104104

105-
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
105+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
106106
if (pr.exitCode == 0) {
107107
return Arrays.asList(pr.out.split("\n"));
108108
} else {
@@ -118,7 +118,7 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
118118

119119
if (ifForce) {
120120
String resetCMD = "timeout 60 git reset --hard";
121-
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir);
121+
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
122122
}
123123

124124
String checkoutCMD = null;
@@ -127,18 +127,18 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
127127
} else {
128128
checkoutCMD = "timeout 60 git checkout " + com;
129129
}
130-
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir);
130+
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
131131

132132
if (pr.exitCode == 0) {
133133
return true;
134134
} else {
135-
String cleanCMD = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
136-
+ " clean -dfx .";
137-
pr = ProcessUtil.executeCMD(cleanCMD, null, repoDir);
138-
String resetCMD = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
139-
+ " reset --hard";
140-
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir);
141-
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir);
135+
String cleanCMD = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree "
136+
+ repoDir.toString() + " clean -dfx .";
137+
pr = ProcessUtil.executeCMD(cleanCMD, null, repoDir, 0);
138+
String resetCMD = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree "
139+
+ repoDir.toString() + " reset --hard";
140+
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
141+
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
142142
if (pr.exitCode == 0) {
143143
return true;
144144
} else {

lib/utils/ProcessUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public String toString() {
3838
* @param workDir
3939
* @return
4040
*/
41-
public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir) {
41+
public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir, int expectedExitCode) {
4242
ProcessReporter pr = new ProcessReporter();
4343
pr.cmd = cmd;
4444
try {
@@ -72,7 +72,7 @@ public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir
7272

7373
proc.destroy();
7474

75-
if (pr.exitCode != 0) {
75+
if (pr.exitCode != expectedExitCode) {
7676
throw new Exception();
7777
}
7878
} catch (Exception e) {

0 commit comments

Comments
 (0)