11
11
import java .net .SocketAddress ;
12
12
import java .net .URI ;
13
13
import java .nio .charset .StandardCharsets ;
14
- import java .util .*;
14
+ import java .util .ArrayList ;
15
+ import java .util .Collections ;
16
+ import java .util .HashSet ;
17
+ import java .util .List ;
18
+ import java .util .Set ;
15
19
16
20
import org .apache .commons .io .FileUtils ;
17
21
import org .apache .commons .io .IOUtils ;
26
30
import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
27
31
import org .eclipse .jgit .diff .DiffEntry .Side ;
28
32
import org .eclipse .jgit .diff .DiffFormatter ;
33
+ import org .eclipse .jgit .lib .AnyObjectId ;
29
34
import org .eclipse .jgit .lib .Constants ;
35
+ import org .eclipse .jgit .lib .ObjectId ;
30
36
import org .eclipse .jgit .lib .ObjectReader ;
31
37
import org .eclipse .jgit .lib .Ref ;
32
38
import org .eclipse .jgit .lib .Repository ;
33
39
import org .eclipse .jgit .revwalk .RevCommit ;
40
+ import org .eclipse .jgit .revwalk .RevObject ;
41
+ import org .eclipse .jgit .revwalk .RevSort ;
34
42
import org .eclipse .jgit .revwalk .RevWalk ;
35
43
import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
36
44
import org .eclipse .jgit .transport .CredentialsProvider ;
40
48
41
49
import com .projectkaiser .scm .vcs .api .IVCS ;
42
50
import com .projectkaiser .scm .vcs .api .VCSChangeType ;
51
+ import com .projectkaiser .scm .vcs .api .VCSCommit ;
43
52
import com .projectkaiser .scm .vcs .api .VCSDiffEntry ;
44
53
import com .projectkaiser .scm .vcs .api .VCSMergeResult ;
45
54
import com .projectkaiser .scm .vcs .api .exceptions .EVCSBranchExists ;
@@ -68,6 +77,56 @@ public GitVCS(IVCSRepositoryWorkspace repo) {
68
77
public void setCredentials (CredentialsProvider credentials ) {
69
78
this .credentials = credentials ;
70
79
}
80
+
81
+ private String getRealBranchName (String branchName ) {
82
+ return branchName == null ? MASTER_BRANCH_NAME : branchName ;
83
+ }
84
+
85
+ public Git getLocalGit (IVCSLockedWorkingCopy wc ) {
86
+ Repository gitRepo ;
87
+ try {
88
+ gitRepo = new FileRepositoryBuilder ()
89
+ .setGitDir (new File (wc .getFolder (), ".git" ))
90
+ .build ();
91
+ } catch (IOException e ) {
92
+ throw new RuntimeException (e );
93
+ }
94
+ Boolean repoInited = gitRepo
95
+ .getObjectDatabase ()
96
+ .exists ();
97
+ Git git = new Git (gitRepo );
98
+ if (!repoInited ) {
99
+ try {
100
+ Git
101
+ .cloneRepository ()
102
+ .setDirectory (wc .getFolder ())
103
+ .setURI (repo .getRepoUrl ())
104
+ .setCredentialsProvider (credentials )
105
+ .setNoCheckout (true )
106
+ .setBranch (Constants .R_HEADS + Constants .MASTER )
107
+ .call ()
108
+ .close ();
109
+ return git ;
110
+ } catch (Exception e ) {
111
+ throw new EVCSException (e );
112
+
113
+ }
114
+ }
115
+ return git ;
116
+ }
117
+
118
+ private VCSChangeType gitChangeTypeToVCSChangeType (ChangeType changeType ) {
119
+ switch (changeType ) {
120
+ case ADD :
121
+ return VCSChangeType .ADD ;
122
+ case DELETE :
123
+ return VCSChangeType .DELETE ;
124
+ case MODIFY :
125
+ return VCSChangeType .MODIFY ;
126
+ default :
127
+ return VCSChangeType .UNKNOWN ;
128
+ }
129
+ }
71
130
72
131
@ Override
73
132
public void createBranch (String srcBranchName , String newBranchName , String commitMessage ) {
@@ -112,10 +171,6 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
112
171
}
113
172
}
114
173
115
- private String getRealBranchName (String branchName ) {
116
- return branchName == null ? MASTER_BRANCH_NAME : branchName ;
117
- }
118
-
119
174
@ Override
120
175
public void deleteBranch (String branchName , String commitMessage ) {
121
176
try {
@@ -157,39 +212,6 @@ public void deleteBranch(String branchName, String commitMessage) {
157
212
}
158
213
}
159
214
160
- public Git getLocalGit (IVCSLockedWorkingCopy wc ) {
161
- Repository gitRepo ;
162
- try {
163
- gitRepo = new FileRepositoryBuilder ()
164
- .setGitDir (new File (wc .getFolder (), ".git" ))
165
- .build ();
166
- } catch (IOException e ) {
167
- throw new RuntimeException (e );
168
- }
169
- Boolean repoInited = gitRepo
170
- .getObjectDatabase ()
171
- .exists ();
172
- Git git = new Git (gitRepo );
173
- if (!repoInited ) {
174
- try {
175
- Git
176
- .cloneRepository ()
177
- .setDirectory (wc .getFolder ())
178
- .setURI (repo .getRepoUrl ())
179
- .setCredentialsProvider (credentials )
180
- .setNoCheckout (true )
181
- .setBranch (Constants .R_HEADS + Constants .MASTER )
182
- .call ()
183
- .close ();
184
- return git ;
185
- } catch (Exception e ) {
186
- throw new EVCSException (e );
187
-
188
- }
189
- }
190
- return git ;
191
- }
192
-
193
215
@ Override
194
216
public VCSMergeResult merge (String srcBranchName , String dstBranchName , String commitMessage ) {
195
217
try {
@@ -323,7 +345,7 @@ public String getFileContent(String branchName, String fileRelativePath, String
323
345
}
324
346
325
347
@ Override
326
- public void setFileContent (String branchName , String filePath , String content , String commitMessage ) {
348
+ public String setFileContent (String branchName , String filePath , String content , String commitMessage ) {
327
349
try {
328
350
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
329
351
try (Git git = getLocalGit (wc )) {
@@ -355,7 +377,7 @@ public void setFileContent(String branchName, String filePath, String content, S
355
377
fw .write (content );
356
378
fw .close ();
357
379
358
- git
380
+ RevCommit res = git
359
381
.commit ()
360
382
.setOnly (filePath )
361
383
.setMessage (commitMessage )
@@ -370,6 +392,8 @@ public void setFileContent(String branchName, String filePath, String content, S
370
392
.setCredentialsProvider (credentials )
371
393
.call ();
372
394
git .getRepository ().close ();
395
+
396
+ return res .getName ();
373
397
}
374
398
}
375
399
} catch (GitAPIException e ) {
@@ -445,19 +469,6 @@ public List<VCSDiffEntry> getBranchesDiff(String srcBranchName, String dstBranch
445
469
throw new RuntimeException (e );
446
470
}
447
471
}
448
-
449
- private VCSChangeType gitChangeTypeToVCSChangeType (ChangeType changeType ) {
450
- switch (changeType ) {
451
- case ADD :
452
- return VCSChangeType .ADD ;
453
- case DELETE :
454
- return VCSChangeType .DELETE ;
455
- case MODIFY :
456
- return VCSChangeType .MODIFY ;
457
- default :
458
- return VCSChangeType .UNKNOWN ;
459
- }
460
- }
461
472
462
473
@ Override
463
474
public Set <String > getBranches () {
@@ -494,6 +505,7 @@ public List<String> getCommitMessages(String branchName, Integer limit) {
494
505
495
506
List <String > res = new ArrayList <>();
496
507
for (RevCommit commit : logs ) {
508
+ commit .getId ().getName ();
497
509
res .add (commit .getFullMessage ());
498
510
}
499
511
git .getRepository ().close ();
@@ -512,7 +524,7 @@ public String getVCSTypeString() {
512
524
}
513
525
514
526
@ Override
515
- public void removeFile (String branchName , String filePath , String commitMessage ) {
527
+ public String removeFile (String branchName , String filePath , String commitMessage ) {
516
528
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
517
529
try (Git git = getLocalGit (wc )) {
518
530
String bn = getRealBranchName (branchName );
@@ -533,7 +545,7 @@ public void removeFile(String branchName, String filePath, String commitMessage)
533
545
.setCached (false )
534
546
.call ();
535
547
536
- git
548
+ RevCommit res = git
537
549
.commit ()
538
550
.setMessage (commitMessage )
539
551
.setAll (true )
@@ -546,11 +558,68 @@ public void removeFile(String branchName, String filePath, String commitMessage)
546
558
.call ();
547
559
548
560
git .getRepository ().close ();
561
+ return res .getName ();
549
562
}
550
563
} catch (GitAPIException e ) {
551
564
throw new EVCSException (e );
552
565
} catch (Exception e ) {
553
566
throw new RuntimeException (e );
554
567
}
555
568
}
569
+
570
+
571
+
572
+ public List <VCSCommit > getCommitsRange (String branchName , String afterCommitId , String untilCommitId ) {
573
+ try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
574
+ try (Git git = getLocalGit (wc )) {
575
+ String bn = getRealBranchName (branchName );
576
+ git
577
+ .checkout ()
578
+ .setCreateBranch (git .getRepository ().exactRef ("refs/heads/" + bn ) == null )
579
+ .setName (bn )
580
+ .call ();
581
+
582
+ ObjectId sinceCommit = afterCommitId == null ?
583
+ getInitialCommit (git ).getId () :
584
+ ObjectId .fromString (afterCommitId );
585
+
586
+ ObjectId untilCommit = untilCommitId == null ?
587
+ git .getRepository ().exactRef ("refs/heads/" + bn ).getObjectId () :
588
+ ObjectId .fromString (untilCommitId );
589
+
590
+ Iterable <RevCommit > commits ;
591
+ commits = git
592
+ .log ()
593
+ .addRange (sinceCommit , untilCommit )
594
+ .call ();
595
+
596
+ List <VCSCommit > res = new ArrayList <>();
597
+ for (RevCommit commit : commits ) {
598
+ VCSCommit vcsCommit = new VCSCommit (commit .getName (), commit .getFullMessage (),
599
+ commit .getAuthorIdent ().getName ());
600
+ res .add (vcsCommit );
601
+ }
602
+
603
+ Collections .reverse (res );
604
+ git .getRepository ().close ();
605
+ return res ;
606
+ }
607
+ } catch (GitAPIException e ) {
608
+ throw new EVCSException (e );
609
+ } catch (Exception e ) {
610
+ throw new RuntimeException (e );
611
+ }
612
+ }
613
+
614
+ private RevObject getInitialCommit (Git git ) throws Exception {
615
+ try (RevWalk rw = new RevWalk (git .getRepository ())) {
616
+ AnyObjectId headId ;
617
+ headId = git .getRepository ().resolve (Constants .HEAD );
618
+ RevCommit root = rw .parseCommit (headId );
619
+ rw .sort (RevSort .REVERSE );
620
+ rw .markStart (root );
621
+ RevCommit res = rw .next ();
622
+ return res ;
623
+ }
624
+ }
556
625
}
0 commit comments