Skip to content

Commit 1ef1cd2

Browse files
committed
fix git get diff
1 parent fbb6c40 commit 1ef1cd2

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

lib/utils/FileUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.ArrayList;
1818
import java.util.List;
1919

20+
import javax.annotation.Nonnull;
21+
2022
import org.apache.commons.io.FileUtils;
2123

2224
public class FileUtil {
@@ -78,7 +80,7 @@ public static Boolean writeStr2File(String wStr, String fPath) {
7880
return true;
7981
}
8082

81-
public static Boolean writeStr2File(String wStr, Path fPath) {
83+
public static Boolean writeStr2File(@Nonnull String wStr, @Nonnull Path fPath) {
8284
try {
8385
Path parentDir = fPath.getParent();
8486
if (!Files.exists(parentDir))

lib/utils/GitUtil.java

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package javaToolkit.lib.utils;
22

33
import java.nio.file.Path;
4+
import java.nio.file.Paths;
45
import java.sql.Array;
56
import java.util.Arrays;
67
import java.util.List;
@@ -20,8 +21,10 @@ public static Boolean clone(String repoName, String usrName, Path targetDir) {
2021
if (pr.exitCode == 0) {
2122
return true;
2223
} else {
23-
System.out.println("cmd " + cmd + "\n");
24-
System.out.println("report \n" + pr.toString());
24+
FileUtil.writeStr2File(pr.out, Paths.get(targetDir.toString(), "clone_out.txt"));
25+
FileUtil.writeStr2File(pr.err, Paths.get(targetDir.toString(), "clone_err.txt"));
26+
// System.out.println("cmd " + cmd + "\n");
27+
// System.out.println("report \n" + pr.toString());
2528
FileUtil.deleteDirectory(targetDir.toFile());
2629
return false;
2730
}
@@ -35,8 +38,10 @@ public static List<String> getAllCommitsSha(Path repoDir) {
3538
if (pr.exitCode == 0) {
3639
return Arrays.asList(pr.out.replace("\"", "").split("\n"));
3740
} else {
38-
System.out.println("cmd " + cmd + "\n");
39-
System.out.println("report \n" + pr.toString());
41+
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getAllCommitsSha_out.txt"));
42+
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getAllCommitsSha_err.txt"));
43+
// System.out.println("cmd " + cmd + "\n");
44+
// System.out.println("report \n" + pr.toString());
4045
return null;
4146
}
4247
}
@@ -49,8 +54,10 @@ public static String getCommitMsg(Path repoDir, String com) {
4954
if (pr.exitCode == 0) {
5055
return pr.out.trim();
5156
} else {
52-
System.out.println("cmd " + cmd + "\n");
53-
System.out.println("report \n" + pr.toString());
57+
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getCommitMsg_out.txt"));
58+
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getCommitMsg_err.txt"));
59+
// System.out.println("cmd " + cmd + "\n");
60+
// System.out.println("report \n" + pr.toString());
5461
return null;
5562
}
5663
}
@@ -63,8 +70,10 @@ public static String getParentCommit(Path repoDir, String com) {
6370
if (pr.exitCode == 0) {
6471
return pr.out.replace("\"", "").trim();
6572
} else {
66-
System.out.println("cmd " + cmd + "\n");
67-
System.out.println("report \n" + pr.toString());
73+
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getParentCommit_out.txt"));
74+
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getParentCommit_err.txt"));
75+
// System.out.println("cmd " + cmd + "\n");
76+
// System.out.println("report \n" + pr.toString());
6877
return null;
6978
}
7079
}
@@ -81,19 +90,21 @@ public static String getDiffBetween2Commits(Path repoDir, String oldCom, String
8190

8291
String cmd = null;
8392
if (diffMode != null) {
84-
cmd = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
93+
cmd = "timeout 60 git --git-dir " + repoDir.toString() + "/.git --work-tree " + repoDir.toString()
8594
+ " diff " + diffMode + " --unified=0 " + oldCom + " " + newCom;
8695
} else {
87-
cmd = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
96+
cmd = "timeout 60 git --git-dir " + repoDir.toString() + "/.git --work-tree " + repoDir.toString()
8897
+ " diff --unified=0 " + oldCom + " " + newCom;
8998
}
9099

91100
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
92101
if (pr.exitCode == 0) {
93102
return pr.out.trim();
94103
} else {
95-
System.out.println("cmd " + cmd + "\n");
96-
System.out.println("report \n" + pr.toString());
104+
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getDiffBetween2Commits_out.txt"));
105+
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getDiffBetween2Commits_err.txt"));
106+
// System.out.println("cmd " + cmd + "\n");
107+
// System.out.println("report \n" + pr.toString());
97108
return null;
98109
}
99110
}
@@ -106,8 +117,10 @@ public static List<String> getChangedFileList(Path repoDir, String com) {
106117
if (pr.exitCode == 0) {
107118
return Arrays.asList(pr.out.split("\n"));
108119
} else {
109-
System.out.println("cmd " + cmd + "\n");
110-
System.out.println("report \n" + pr.toString());
120+
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getChangedFileList_out.txt"));
121+
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getChangedFileList_err.txt"));
122+
// System.out.println("cmd " + cmd + "\n");
123+
// System.out.println("report \n" + pr.toString());
111124
return null;
112125
}
113126
}

lib/utils/ProcessUtil.java

+50
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,54 @@ public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir
8383
return pr;
8484
}
8585

86+
/**
87+
* execute cmd
88+
*
89+
* @param cmd
90+
* @param envp
91+
* @param workDir
92+
* @return
93+
*/
94+
public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir) {
95+
ProcessReporter pr = new ProcessReporter();
96+
pr.cmd = cmd;
97+
try {
98+
Runtime rt = Runtime.getRuntime();
99+
Process proc = rt.exec(cmd, envp, workDir.toFile());
100+
101+
pr.exitCode = proc.waitFor();
102+
103+
// Read the output from the command
104+
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
105+
// System.out.println("Here is the standard output of the
106+
// command:\n");
107+
String out = "";
108+
String s = null;
109+
while ((s = stdInput.readLine()) != null) {
110+
// System.out.println(s);
111+
out += (s + "\n");
112+
}
113+
pr.out = out.trim();
114+
115+
// Read any errors from the attempted command
116+
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
117+
// System.out.println("Here is the standard error of the command (if
118+
// any):\n");
119+
String err = "";
120+
while ((s = stdError.readLine()) != null) {
121+
// System.out.println(s);
122+
err += (s + "\n");
123+
}
124+
pr.err = err.trim();
125+
126+
proc.destroy();
127+
128+
} catch (Exception e) {
129+
TimeUtil.printCurTimewithMsg(pr.toString());
130+
e.printStackTrace();
131+
}
132+
133+
return pr;
134+
}
135+
86136
}

0 commit comments

Comments
 (0)