-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitUtil.java
150 lines (123 loc) · 4.32 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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) {
if(targetDir.toFile().exists()){
FileUtil.deleteDirectory(targetDir.toFile());
}
targetDir.toFile().mkdirs();
String cmd = "timeout 300 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());
FileUtil.deleteDirectory(targetDir.toFile());
return false;
}
}
public static List<String> getAllCommitsSha(Path repoDir) {
String cmd = "timeout 300 git log --pretty=format:\"%H\"";
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
if (pr.exitCode == 0) {
return Arrays.asList(pr.out.replace("\"", "").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 = "timeout 300 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 String getParentCommit(Path repoDir, String com) {
String cmd = "timeout 300 git log --pretty=%P -n 1 " + com;
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
if (pr.exitCode == 0) {
return pr.out.replace("\"", "").trim();
} else {
System.out.println("cmd " + cmd + "\n");
System.out.println("report \n" + pr.toString());
return null;
}
}
/**
*
* @param repoDir
* @param oldCom
* @param newCom
* @param diffMode
* @return
*/
public static String getDiffBetween2Commits(Path repoDir, String oldCom, String newCom, String diffMode) {
String cmd = null;
if (diffMode != null) {
cmd = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString() + " diff "
+ diffMode + " --unified=0 " + oldCom + " " + newCom;
} else {
cmd = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
+ " diff --unified=0 " + oldCom + " " + newCom;
}
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 = "timeout 300 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 = "timeout 300 git reset --hard";
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir);
}
String checkoutCMD = null;
if (com == null) { // checkout latest if null
checkoutCMD = "timeout 300 git checkout master";
} else {
checkoutCMD = "timeout 300 git checkout " + com;
}
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir);
if (pr.exitCode == 0) {
return true;
} else {
String cleanCMD = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
+ " clean -dfx .";
pr = ProcessUtil.executeCMD(cleanCMD, null, repoDir);
String resetCMD = "timeout 300 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;
}
}
}
}