Skip to content

Commit 296e82f

Browse files
committed
add gitutil
1 parent 23a3ee1 commit 296e82f

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed

Diff for: lib/utils/CSVUtil.java

+65
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
11
package javaToolkit.lib.utils;
22

3+
import java.io.FileWriter;
4+
import java.nio.file.Path;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
38
public class CSVUtil {
49

10+
public static void writeLine2Csv(List<String> strList, Path filePath) {
11+
try {
12+
FileWriter writer = new FileWriter(filePath.toString());
13+
14+
String collect = strList.stream().collect(Collectors.joining(","));
15+
System.out.println(collect);
16+
17+
writer.write(collect);
18+
writer.close();
19+
} catch (Exception e) {
20+
// TODO: handle exception
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
public static void appendLine2Csv(List<String> strList, Path filePath) {
26+
try {
27+
String collect = strList.stream().collect(Collectors.joining(","));
28+
// System.out.println(collect);
29+
30+
// pass true for appending
31+
if (filePath.toFile().exists()) {
32+
FileWriter writer = new FileWriter(filePath.toString(), true);
33+
writer.write(collect);
34+
writer.close();
35+
} else {
36+
FileWriter writer = new FileWriter(filePath.toString());
37+
writer.write(collect);
38+
writer.close();
39+
}
40+
41+
} catch (Exception e) {
42+
// TODO: handle exception
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
public static void write2DArray2Csv(List<List<String>> strList, Path filePath) {
48+
try {
49+
FileWriter writer = null;
50+
for (List<String> line : strList) {
51+
String collect = line.stream().collect(Collectors.joining(","));
52+
// System.out.println(collect);
53+
54+
// pass true for appending
55+
if (filePath.toFile().exists()) {
56+
writer = new FileWriter(filePath.toString(), true);
57+
writer.write(collect.trim() + "\n");
58+
} else {
59+
writer = new FileWriter(filePath.toString());
60+
writer.write(collect.trim() + "\n");
61+
}
62+
writer.close();
63+
}
64+
} catch (Exception e) {
65+
// TODO: handle exception
66+
e.printStackTrace();
67+
}
68+
}
69+
570
}

Diff for: lib/utils/GitUtil.java

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package javaToolkit.lib.utils;
2+
3+
import java.nio.file.Path;
4+
import java.sql.Array;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
public class GitUtil {
9+
10+
public static Boolean clone(String repoName, String usrName, Path targetDir) {
11+
12+
String cmd = "timeout 600 git clone https://github.com/" + repoName + "/" + usrName + " " + targetDir;
13+
14+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, targetDir);
15+
if (pr.exitCode == 0) {
16+
return true;
17+
} else {
18+
System.out.println("cmd " + cmd + "\n");
19+
System.out.println("report \n" + pr.toString());
20+
return false;
21+
}
22+
}
23+
24+
public static List<String> getAllCommits(Path repoDir) {
25+
26+
String cmd = "timeout 600 git log --pretty=format:\"%H\"";
27+
28+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
29+
if (pr.exitCode == 0) {
30+
return Arrays.asList(pr.out.split("\n"));
31+
} else {
32+
System.out.println("cmd " + cmd + "\n");
33+
System.out.println("report \n" + pr.toString());
34+
return null;
35+
}
36+
}
37+
38+
public static String getCommitMsg(Path repoDir, String com) {
39+
40+
String cmd = "git log --format=%%B -n 1 " + com;
41+
42+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
43+
if (pr.exitCode == 0) {
44+
return pr.out.trim();
45+
} else {
46+
System.out.println("cmd " + cmd + "\n");
47+
System.out.println("report \n" + pr.toString());
48+
return null;
49+
}
50+
}
51+
52+
public static List<String> getChangedFileList(Path repoDir, String com) {
53+
54+
String cmd = "git diff-tree --no-commit-id --name-only -r "+ com;
55+
56+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
57+
if (pr.exitCode == 0) {
58+
return Arrays.asList(pr.out.split("\n"));
59+
} else {
60+
System.out.println("cmd " + cmd + "\n");
61+
System.out.println("report \n" + pr.toString());
62+
return null;
63+
}
64+
}
65+
66+
public static Boolean checkout(Path repoDir, String com,Boolean ifForce) {
67+
68+
69+
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
70+
71+
if(ifForce){
72+
String resetCMD="git reset --hard";
73+
pr=ProcessUtil.executeCMD(resetCMD, null, repoDir);
74+
}
75+
76+
String checkoutCMD=null;
77+
if(com==null){ // checkout latest if null
78+
checkoutCMD="git --git-dir "+repoDir.toString()+" /.git --work-tree "+repoDir.toString()+" checkout master";
79+
}else{
80+
checkoutCMD="git --git-dir "+repoDir.toString()+" /.git --work-tree "+repoDir.toString()+" checkout "+com;
81+
}
82+
pr=ProcessUtil.executeCMD(checkoutCMD, null, repoDir);
83+
84+
if (pr.exitCode == 0) {
85+
return true;
86+
} else {
87+
String cleanCMD="git --git-dir "+repoDir.toString()+" /.git --work-tree "+repoDir.toString()+" clean -dfx .";
88+
pr=ProcessUtil.executeCMD(cleanCMD, null, repoDir);
89+
String resetCMD = "git --git-dir "+repoDir.toString()+ " /.git --work-tree "+repoDir.toString()+" reset --hard";
90+
pr=ProcessUtil.executeCMD(resetCMD, null, repoDir);
91+
pr=ProcessUtil.executeCMD(checkoutCMD, null, repoDir);
92+
if(pr.exitCode==0){
93+
return true;
94+
}else{
95+
return false;
96+
}
97+
}
98+
}
99+
100+
}

Diff for: lib/utils/ProcessUtil.java

+78
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,85 @@
11
package javaToolkit.lib.utils;
22

3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.nio.file.Path;
6+
37
public class ProcessUtil {
48

9+
public static class ProcessReporter {
10+
public String cmd;
11+
public int exitCode;
12+
public String out;
13+
public String err;
14+
15+
public ProcessReporter() {
16+
this.cmd = null;
17+
this.exitCode = -1;
18+
this.out = null;
19+
this.err = null;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
String res = "Process Report:\n";
25+
res += ("cmd:\n" + this.cmd + "\n");
26+
res += ("exit code:\n" + this.exitCode + "\n");
27+
res += ("out:\n" + this.out + "\n");
28+
res += ("err:\n" + this.err + "\n");
29+
return res;
30+
}
31+
}
32+
33+
/**
34+
* execute cmd
35+
* @param cmd
36+
* @param envp
37+
* @param workDir
38+
* @return
39+
*/
40+
public static ProcessReporter executeCMD(String cmd, String[] envp, Path workDir) {
41+
ProcessReporter pr = new ProcessReporter();
42+
pr.cmd = cmd;
43+
try {
44+
Runtime rt = Runtime.getRuntime();
45+
Process proc = rt.exec(cmd, envp, workDir.toFile());
46+
47+
pr.exitCode = proc.waitFor();
48+
49+
// Read the output from the command
50+
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
51+
// System.out.println("Here is the standard output of the
52+
// command:\n");
53+
String out = "";
54+
String s = null;
55+
while ((s = stdInput.readLine()) != null) {
56+
// System.out.println(s);
57+
out += (s + "\n");
58+
}
59+
pr.out = out;
60+
61+
// Read any errors from the attempted command
62+
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
63+
// System.out.println("Here is the standard error of the command (if
64+
// any):\n");
65+
String err = "";
66+
while ((s = stdError.readLine()) != null) {
67+
// System.out.println(s);
68+
err += (s + "\n");
69+
}
70+
pr.err = err;
71+
72+
proc.destroy();
73+
74+
if (pr.exitCode != 0) {
75+
throw new Exception();
76+
}
77+
} catch (Exception e) {
78+
pr.toString();
79+
e.printStackTrace();
80+
}
581

82+
return pr;
83+
}
684

785
}

0 commit comments

Comments
 (0)