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