12
12
import java .net .URI ;
13
13
import java .nio .charset .StandardCharsets ;
14
14
import java .util .ArrayList ;
15
- import java .util .Arrays ;
15
+ import java .util .Collections ;
16
16
import java .util .HashSet ;
17
17
import java .util .List ;
18
18
import java .util .Set ;
30
30
import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
31
31
import org .eclipse .jgit .diff .DiffEntry .Side ;
32
32
import org .eclipse .jgit .diff .DiffFormatter ;
33
+ import org .eclipse .jgit .lib .AnyObjectId ;
33
34
import org .eclipse .jgit .lib .Constants ;
35
+ import org .eclipse .jgit .lib .ObjectId ;
34
36
import org .eclipse .jgit .lib .ObjectReader ;
35
37
import org .eclipse .jgit .lib .Ref ;
36
38
import org .eclipse .jgit .lib .Repository ;
37
39
import org .eclipse .jgit .revwalk .RevCommit ;
40
+ import org .eclipse .jgit .revwalk .RevObject ;
41
+ import org .eclipse .jgit .revwalk .RevSort ;
38
42
import org .eclipse .jgit .revwalk .RevWalk ;
39
43
import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
40
44
import org .eclipse .jgit .transport .CredentialsProvider ;
44
48
45
49
import com .projectkaiser .scm .vcs .api .IVCS ;
46
50
import com .projectkaiser .scm .vcs .api .VCSChangeType ;
51
+ import com .projectkaiser .scm .vcs .api .VCSCommit ;
47
52
import com .projectkaiser .scm .vcs .api .VCSDiffEntry ;
48
53
import com .projectkaiser .scm .vcs .api .VCSMergeResult ;
49
54
import com .projectkaiser .scm .vcs .api .exceptions .EVCSBranchExists ;
@@ -59,7 +64,7 @@ public class GitVCS implements IVCS {
59
64
private static final String REFS_REMOTES_ORIGIN = Constants .R_REMOTES + Constants .DEFAULT_REMOTE_NAME + "/" ;
60
65
61
66
private CredentialsProvider credentials ;
62
- private IVCSRepositoryWorkspace repo ;
67
+ private final IVCSRepositoryWorkspace repo ;
63
68
64
69
public CredentialsProvider getCredentials () {
65
70
return credentials ;
@@ -72,6 +77,56 @@ public GitVCS(IVCSRepositoryWorkspace repo) {
72
77
public void setCredentials (CredentialsProvider credentials ) {
73
78
this .credentials = credentials ;
74
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
+ }
75
130
76
131
@ Override
77
132
public void createBranch (String srcBranchName , String newBranchName , String commitMessage ) {
@@ -116,10 +171,6 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
116
171
}
117
172
}
118
173
119
- private String getRealBranchName (String branchName ) {
120
- return branchName == null ? MASTER_BRANCH_NAME : branchName ;
121
- }
122
-
123
174
@ Override
124
175
public void deleteBranch (String branchName , String commitMessage ) {
125
176
try {
@@ -161,39 +212,6 @@ public void deleteBranch(String branchName, String commitMessage) {
161
212
}
162
213
}
163
214
164
- public Git getLocalGit (IVCSLockedWorkingCopy wc ) {
165
- Repository gitRepo ;
166
- try {
167
- gitRepo = new FileRepositoryBuilder ()
168
- .setGitDir (new File (wc .getFolder (), ".git" ))
169
- .build ();
170
- } catch (IOException e ) {
171
- throw new RuntimeException (e );
172
- }
173
- Boolean repoInited = gitRepo
174
- .getObjectDatabase ()
175
- .exists ();
176
- Git git = new Git (gitRepo );
177
- if (!repoInited ) {
178
- try {
179
- Git
180
- .cloneRepository ()
181
- .setDirectory (wc .getFolder ())
182
- .setURI (repo .getRepoUrl ())
183
- .setCredentialsProvider (credentials )
184
- .setNoCheckout (true )
185
- .setBranch (Constants .R_HEADS + Constants .MASTER )
186
- .call ()
187
- .close ();
188
- return git ;
189
- } catch (Exception e ) {
190
- throw new EVCSException (e );
191
-
192
- }
193
- }
194
- return git ;
195
- }
196
-
197
215
@ Override
198
216
public VCSMergeResult merge (String srcBranchName , String dstBranchName , String commitMessage ) {
199
217
try {
@@ -269,10 +287,10 @@ public void setProxy(final String host, final int port, String proxyUser, String
269
287
@ Override
270
288
public List <Proxy > select (URI uri ) {
271
289
if (uri .toString ().toLowerCase ().contains (repo .getRepoUrl ().toLowerCase ())) {
272
- return Arrays . asList (new Proxy (Type .HTTP , InetSocketAddress
273
- .createUnresolved (host , port )));
290
+ return Collections . singletonList (new Proxy (Type .HTTP , InetSocketAddress
291
+ .createUnresolved (host , port )));
274
292
} else {
275
- return delegate == null ? Arrays . asList (Proxy .NO_PROXY )
293
+ return delegate == null ? Collections . singletonList (Proxy .NO_PROXY )
276
294
: delegate .select (uri );
277
295
}
278
296
}
@@ -327,7 +345,7 @@ public String getFileContent(String branchName, String fileRelativePath, String
327
345
}
328
346
329
347
@ Override
330
- public void setFileContent (String branchName , String filePath , String content , String commitMessage ) {
348
+ public String setFileContent (String branchName , String filePath , String content , String commitMessage ) {
331
349
try {
332
350
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
333
351
try (Git git = getLocalGit (wc )) {
@@ -359,7 +377,7 @@ public void setFileContent(String branchName, String filePath, String content, S
359
377
fw .write (content );
360
378
fw .close ();
361
379
362
- git
380
+ RevCommit res = git
363
381
.commit ()
364
382
.setOnly (filePath )
365
383
.setMessage (commitMessage )
@@ -374,6 +392,8 @@ public void setFileContent(String branchName, String filePath, String content, S
374
392
.setCredentialsProvider (credentials )
375
393
.call ();
376
394
git .getRepository ().close ();
395
+
396
+ return res .getName ();
377
397
}
378
398
}
379
399
} catch (GitAPIException e ) {
@@ -449,19 +469,6 @@ public List<VCSDiffEntry> getBranchesDiff(String srcBranchName, String dstBranch
449
469
throw new RuntimeException (e );
450
470
}
451
471
}
452
-
453
- private VCSChangeType gitChangeTypeToVCSChangeType (ChangeType changeType ) {
454
- switch (changeType ) {
455
- case ADD :
456
- return VCSChangeType .ADD ;
457
- case DELETE :
458
- return VCSChangeType .DELETE ;
459
- case MODIFY :
460
- return VCSChangeType .MODIFY ;
461
- default :
462
- return VCSChangeType .UNKNOWN ;
463
- }
464
- }
465
472
466
473
@ Override
467
474
public Set <String > getBranches () {
@@ -498,6 +505,7 @@ public List<String> getCommitMessages(String branchName, Integer limit) {
498
505
499
506
List <String > res = new ArrayList <>();
500
507
for (RevCommit commit : logs ) {
508
+ commit .getId ().getName ();
501
509
res .add (commit .getFullMessage ());
502
510
}
503
511
git .getRepository ().close ();
@@ -516,7 +524,7 @@ public String getVCSTypeString() {
516
524
}
517
525
518
526
@ Override
519
- public void removeFile (String branchName , String filePath , String commitMessage ) {
527
+ public String removeFile (String branchName , String filePath , String commitMessage ) {
520
528
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
521
529
try (Git git = getLocalGit (wc )) {
522
530
String bn = getRealBranchName (branchName );
@@ -537,7 +545,7 @@ public void removeFile(String branchName, String filePath, String commitMessage)
537
545
.setCached (false )
538
546
.call ();
539
547
540
- git
548
+ RevCommit res = git
541
549
.commit ()
542
550
.setMessage (commitMessage )
543
551
.setAll (true )
@@ -550,11 +558,68 @@ public void removeFile(String branchName, String filePath, String commitMessage)
550
558
.call ();
551
559
552
560
git .getRepository ().close ();
561
+ return res .getName ();
553
562
}
554
563
} catch (GitAPIException e ) {
555
564
throw new EVCSException (e );
556
565
} catch (Exception e ) {
557
566
throw new RuntimeException (e );
558
567
}
559
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
+ }
560
625
}
0 commit comments