Skip to content

Commit cb39bbd

Browse files
committed
improve gitutil
1 parent afa8f51 commit cb39bbd

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

lib/utils/GitUtil.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static List<String> getChangedFileList(Path repoDir, String com) {
161161
}
162162
}
163163

164-
public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
164+
public static Boolean checkoutDefaultBranch(Path repoDir, Boolean ifForce) {
165165

166166
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
167167

@@ -173,11 +173,52 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
173173
String defaultBranchNameString = getDefaultBranch(repoDir);
174174

175175
String checkoutCMD = null;
176-
if (com == null) { // checkout latest if null
177-
checkoutCMD = "timeout 300 git checkout " + defaultBranchNameString;
176+
checkoutCMD = "timeout 300 git checkout " + defaultBranchNameString;
177+
178+
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
179+
180+
if (pr.err.contains("fatal: index file smaller than expected")) {
181+
Path lockFilePath = Paths.get(repoDir.toString() + "/.git/index.lock");
182+
if (lockFilePath.toFile().exists()) {
183+
lockFilePath.toFile().delete();
184+
}
185+
Path indexFilePath = Paths.get(repoDir.toString() + "/.git/index");
186+
if (indexFilePath.toFile().exists()) {
187+
indexFilePath.toFile().delete();
188+
}
189+
}
190+
191+
if (pr.exitCode == 0) {
192+
return true;
178193
} else {
179-
checkoutCMD = "timeout 300 git checkout " + com;
194+
String cleanCMD = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree "
195+
+ repoDir.toString() + " clean -dfx .";
196+
pr = ProcessUtil.executeCMD(cleanCMD, null, repoDir, 0);
197+
String resetCMD = "timeout 300 git --git-dir " + repoDir.toString() + "/.git --work-tree "
198+
+ repoDir.toString() + " reset --hard";
199+
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
200+
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
201+
if (pr.exitCode == 0) {
202+
return true;
203+
} else {
204+
return false;
205+
}
206+
}
207+
}
208+
209+
public static Boolean checkoutCommit(Path repoDir, String com, Boolean ifForce) {
210+
211+
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
212+
213+
if (ifForce) {
214+
String resetCMD = "timeout 600 git reset --hard";
215+
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
180216
}
217+
218+
String defaultBranchNameString = getDefaultBranch(repoDir);
219+
220+
String checkoutCMD = null;
221+
checkoutCMD = "timeout 300 git checkout " + com;
181222
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir, 0);
182223

183224
if (pr.err.contains("fatal: index file smaller than expected")) {

0 commit comments

Comments
 (0)