-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitUtil.java
218 lines (181 loc) · 7.54 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package javaToolkit.lib.utils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class GitUtil {
public static Boolean clone(String usrName, String repoName, Path targetDir) {
if (targetDir.toFile().exists()) {
FileUtil.deleteDirectory(targetDir.toFile());
}
targetDir.toFile().mkdirs();
String cmd = "timeout 600 git clone https://github.com/" + usrName + "/" + repoName + " " + targetDir;
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, targetDir, 0);
if (pr.exitCode == 0) {
return true;
} else {
FileUtil.writeStr2File(pr.out, Paths.get(targetDir.toString(), "clone_out.txt"));
FileUtil.writeStr2File(pr.err, Paths.get(targetDir.toString(), "clone_err.txt"));
// 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 --no-pager log --all --pretty=format:\"%H\"";
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
if (pr.exitCode == 0) {
return Arrays.asList(pr.out.replace("\"", "").split("\n"));
} else {
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getAllCommitsSha_out.txt"));
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getAllCommitsSha_err.txt"));
// System.out.println("cmd " + cmd + "\n");
// System.out.println("report \n" + pr.toString());
return null;
}
}
public static List<String> getComsWithSingleWordMatch(Path repoDir, String word) {
String cmd = "timeout 300 git --no-pager log --all --pretty=format:\"%H\" --grep=" + word.trim();
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
if (pr.exitCode == 0) {
if ("".equals(pr.out.trim())) {
return null;
}
return Arrays.asList(pr.out.replace("\"", "").split("\n"));
} else {
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getAllCommitsSha_out.txt"));
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getAllCommitsSha_err.txt"));
// System.out.println("cmd " + cmd + "\n");
// System.out.println("report \n" + pr.toString());
return null;
}
}
public static String getDiff4SingleFileNCommit(Path repoDir, String parCom, String curCom, String oldRelFilePath,
String newRelFilePath) {
String cmd = "timeout 300 git --no-pager diff --unified=0 " + parCom + ":" + oldRelFilePath + " " + curCom + ":"
+ newRelFilePath;
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir, 0);
if (pr.exitCode == 0) {
return pr.out.trim();
} else {
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getDiff4SingleFileNCommit.txt"));
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getDiff4SingleFileNCommit.txt"));
// 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, 0);
if (pr.exitCode == 0) {
return pr.out.trim();
} else {
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getCommitMsg_out.txt"));
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getCommitMsg_err.txt"));
// 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, 0);
if (pr.exitCode == 0) {
return pr.out.replace("\"", "").trim();
} else {
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getParentCommit_out.txt"));
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getParentCommit_err.txt"));
// 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, 0);
if (pr.exitCode == 0) {
return pr.out.trim();
} else {
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getDiffBetween2Commits_out.txt"));
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getDiffBetween2Commits_err.txt"));
// 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, 0);
if (pr.exitCode == 0) {
return Arrays.asList(pr.out.split("\n"));
} else {
FileUtil.writeStr2File(pr.out, Paths.get(repoDir.toString(), "getChangedFileList_out.txt"));
FileUtil.writeStr2File(pr.err, Paths.get(repoDir.toString(), "getChangedFileList_err.txt"));
// 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 600 git reset --hard";
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
}
String defaultBranchNameString = getDefaultBranch(repoDir);
String checkoutCMD = null;
if (com == null) { // checkout latest if null
checkoutCMD = "timeout 300 git checkout " + defaultBranchNameString;
} else {
checkoutCMD = "timeout 300 git checkout " + com;
}
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
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, 0);
String resetCMD = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree "
+ repoDir.toString() + " reset --hard";
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
if (pr.exitCode == 0) {
return true;
} else {
return false;
}
}
}
private static String getDefaultBranch(Path gitDirPath) {
// TODO Auto-generated method stub
String cmdString = "git symbolic-ref refs/remotes/origin/HEAD";
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmdString, null, gitDirPath, 0);
// output refs/remotes/origin/master
if (pr.out.trim().contains("refs/remotes/origin/")) {
return pr.out.trim().split("refs/remotes/origin/")[1].trim();
} else {
return "master";
}
}
}