@@ -161,7 +161,7 @@ public static List<String> getChangedFileList(Path repoDir, String com) {
161
161
}
162
162
}
163
163
164
- public static Boolean checkout (Path repoDir , String com , Boolean ifForce ) {
164
+ public static Boolean checkoutDefaultBranch (Path repoDir , Boolean ifForce ) {
165
165
166
166
ProcessUtil .ProcessReporter pr = new ProcessUtil .ProcessReporter ();
167
167
@@ -173,11 +173,52 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
173
173
String defaultBranchNameString = getDefaultBranch (repoDir );
174
174
175
175
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 ;
178
193
} 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 );
180
216
}
217
+
218
+ String defaultBranchNameString = getDefaultBranch (repoDir );
219
+
220
+ String checkoutCMD = null ;
221
+ checkoutCMD = "timeout 300 git checkout " + com ;
181
222
pr = ProcessUtil .executeCMD (checkoutCMD , null , repoDir , 0 );
182
223
183
224
if (pr .err .contains ("fatal: index file smaller than expected" )) {
0 commit comments