3
3
import java .nio .file .Path ;
4
4
import java .nio .file .Paths ;
5
5
import java .util .Arrays ;
6
- import java .util .Date ;
7
- import java .util .HashSet ;
8
6
import java .util .List ;
9
- import java .util .Set ;
10
7
11
8
public class GitUtil {
12
9
13
- public static void main (String [] args ) {
14
- Path repoDir = Paths .get ("/data/bowen/data/PTBench-Data/data/2019/quarkusio#gizmo/raw_github" );
15
- Set <String > mergeSet = new HashSet <>();
16
- Set <String > bugComs = new HashSet <String >(getComsWithSingleWordMatch (repoDir , "bug" ));
17
- System .out .println ("# bug coms " + String .valueOf (bugComs .size ()));
18
- mergeSet .addAll (bugComs );
19
- Set <String > BugComs = new HashSet <String >(getComsWithSingleWordMatch (repoDir , "Bug" ));
20
- System .out .println ("# Bug coms " + String .valueOf (BugComs .size ()));
21
- mergeSet .addAll (BugComs );
22
- Set <String > fixComs = new HashSet <String >(getComsWithSingleWordMatch (repoDir , "fix" ));
23
- System .out .println ("# fix coms " + String .valueOf (fixComs .size ()));
24
- mergeSet .addAll (fixComs );
25
- Set <String > FixComs = new HashSet <String >(getComsWithSingleWordMatch (repoDir , "Fix" ));
26
- System .out .println ("# Fix coms " + String .valueOf (FixComs .size ()));
27
- mergeSet .addAll (FixComs );
28
- System .out .println ("# merge coms " + String .valueOf (mergeSet .size ()));
29
- }
30
-
31
- public static Boolean clone (String repoName , String usrName , Path targetDir ) {
10
+ public static Boolean clone (String usrName , String repoName , Path targetDir ) {
32
11
33
12
if (targetDir .toFile ().exists ()) {
34
13
FileUtil .deleteDirectory (targetDir .toFile ());
35
14
}
36
15
targetDir .toFile ().mkdirs ();
37
16
38
- String cmd = "timeout 300 git clone https://github.com/" + repoName + "/" + usrName + " " + targetDir ;
17
+ String cmd = "timeout 600 git clone https://github.com/" + usrName + "/" + repoName + " " + targetDir ;
39
18
40
19
ProcessUtil .ProcessReporter pr = ProcessUtil .executeCMD (cmd , null , targetDir , 0 );
41
20
if (pr .exitCode == 0 ) {
@@ -85,6 +64,24 @@ public static List<String> getComsWithSingleWordMatch(Path repoDir, String word)
85
64
}
86
65
}
87
66
67
+ public static String getDiff4SingleFileNCommit (Path repoDir , String parCom , String curCom , String oldRelFilePath ,
68
+ String newRelFilePath ) {
69
+
70
+ String cmd = "timeout 300 git --no-pager diff --unified=0 " + parCom + ":" + oldRelFilePath + " " + curCom + ":"
71
+ + newRelFilePath ;
72
+
73
+ ProcessUtil .ProcessReporter pr = ProcessUtil .executeCMD (cmd , null , repoDir , 0 );
74
+ if (pr .exitCode == 0 ) {
75
+ return pr .out .trim ();
76
+ } else {
77
+ FileUtil .writeStr2File (pr .out , Paths .get (repoDir .toString (), "getDiff4SingleFileNCommit.txt" ));
78
+ FileUtil .writeStr2File (pr .err , Paths .get (repoDir .toString (), "getDiff4SingleFileNCommit.txt" ));
79
+ // System.out.println("cmd " + cmd + "\n");
80
+ // System.out.println("report \n" + pr.toString());
81
+ return null ;
82
+ }
83
+ }
84
+
88
85
public static String getCommitMsg (Path repoDir , String com ) {
89
86
90
87
String cmd = "timeout 300 git log --format=%B -n 1 " + com ;
@@ -164,23 +161,77 @@ public static List<String> getChangedFileList(Path repoDir, String com) {
164
161
}
165
162
}
166
163
167
- public static Boolean checkout (Path repoDir , String com , Boolean ifForce ) {
164
+ public static Boolean checkoutDefaultBranch (Path repoDir , Boolean ifForce ) {
168
165
169
166
ProcessUtil .ProcessReporter pr = new ProcessUtil .ProcessReporter ();
170
167
171
168
if (ifForce ) {
172
- String resetCMD = "timeout 300 git reset --hard" ;
169
+ String resetCMD = "timeout 600 git reset --hard" ;
173
170
pr = ProcessUtil .executeCMD (resetCMD , null , repoDir , 0 );
174
171
}
175
172
173
+ String defaultBranchNameString = getDefaultBranch (repoDir );
174
+
176
175
String checkoutCMD = null ;
177
- if (com == null ) { // checkout latest if null
178
- checkoutCMD = "timeout 300 git checkout master" ;
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 ;
179
193
} else {
180
- 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 );
181
216
}
217
+
218
+ String defaultBranchNameString = getDefaultBranch (repoDir );
219
+
220
+ String checkoutCMD = null ;
221
+ checkoutCMD = "timeout 300 git checkout " + com ;
182
222
pr = ProcessUtil .executeCMD (checkoutCMD , null , repoDir , 0 );
183
223
224
+ if (pr .err .contains ("fatal: index file smaller than expected" )) {
225
+ Path lockFilePath = Paths .get (repoDir .toString () + "/.git/index.lock" );
226
+ if (lockFilePath .toFile ().exists ()) {
227
+ lockFilePath .toFile ().delete ();
228
+ }
229
+ Path indexFilePath = Paths .get (repoDir .toString () + "/.git/index" );
230
+ if (indexFilePath .toFile ().exists ()) {
231
+ indexFilePath .toFile ().delete ();
232
+ }
233
+ }
234
+
184
235
if (pr .exitCode == 0 ) {
185
236
return true ;
186
237
} else {
@@ -199,4 +250,18 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
199
250
}
200
251
}
201
252
253
+ private static String getDefaultBranch (Path gitDirPath ) {
254
+ // TODO Auto-generated method stub
255
+ String cmdString = "git symbolic-ref refs/remotes/origin/HEAD" ;
256
+ ProcessUtil .ProcessReporter pr = ProcessUtil .executeCMD (cmdString , null , gitDirPath , 0 );
257
+
258
+ // output refs/remotes/origin/master
259
+ if (pr .out .trim ().contains ("refs/remotes/origin/" )) {
260
+ return pr .out .trim ().split ("refs/remotes/origin/" )[1 ].trim ();
261
+ } else {
262
+ return "master" ;
263
+ }
264
+
265
+ }
266
+
202
267
}
0 commit comments