1212import java .net .URI ;
1313import java .nio .charset .StandardCharsets ;
1414import java .util .ArrayList ;
15- import java .util .Arrays ;
15+ import java .util .Collections ;
1616import java .util .HashSet ;
1717import java .util .List ;
1818import java .util .Set ;
3030import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
3131import org .eclipse .jgit .diff .DiffEntry .Side ;
3232import org .eclipse .jgit .diff .DiffFormatter ;
33+ import org .eclipse .jgit .lib .AnyObjectId ;
3334import org .eclipse .jgit .lib .Constants ;
35+ import org .eclipse .jgit .lib .ObjectId ;
3436import org .eclipse .jgit .lib .ObjectReader ;
3537import org .eclipse .jgit .lib .Ref ;
3638import org .eclipse .jgit .lib .Repository ;
3739import org .eclipse .jgit .revwalk .RevCommit ;
40+ import org .eclipse .jgit .revwalk .RevObject ;
41+ import org .eclipse .jgit .revwalk .RevSort ;
3842import org .eclipse .jgit .revwalk .RevWalk ;
3943import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
4044import org .eclipse .jgit .transport .CredentialsProvider ;
4448
4549import com .projectkaiser .scm .vcs .api .IVCS ;
4650import com .projectkaiser .scm .vcs .api .VCSChangeType ;
51+ import com .projectkaiser .scm .vcs .api .VCSCommit ;
4752import com .projectkaiser .scm .vcs .api .VCSDiffEntry ;
4853import com .projectkaiser .scm .vcs .api .VCSMergeResult ;
4954import com .projectkaiser .scm .vcs .api .exceptions .EVCSBranchExists ;
@@ -59,7 +64,7 @@ public class GitVCS implements IVCS {
5964 private static final String REFS_REMOTES_ORIGIN = Constants .R_REMOTES + Constants .DEFAULT_REMOTE_NAME + "/" ;
6065
6166 private CredentialsProvider credentials ;
62- private IVCSRepositoryWorkspace repo ;
67+ private final IVCSRepositoryWorkspace repo ;
6368
6469 public CredentialsProvider getCredentials () {
6570 return credentials ;
@@ -72,6 +77,56 @@ public GitVCS(IVCSRepositoryWorkspace repo) {
7277 public void setCredentials (CredentialsProvider credentials ) {
7378 this .credentials = credentials ;
7479 }
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+ }
75130
76131 @ Override
77132 public void createBranch (String srcBranchName , String newBranchName , String commitMessage ) {
@@ -116,10 +171,6 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
116171 }
117172 }
118173
119- private String getRealBranchName (String branchName ) {
120- return branchName == null ? MASTER_BRANCH_NAME : branchName ;
121- }
122-
123174 @ Override
124175 public void deleteBranch (String branchName , String commitMessage ) {
125176 try {
@@ -161,39 +212,6 @@ public void deleteBranch(String branchName, String commitMessage) {
161212 }
162213 }
163214
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-
197215 @ Override
198216 public VCSMergeResult merge (String srcBranchName , String dstBranchName , String commitMessage ) {
199217 try {
@@ -269,10 +287,10 @@ public void setProxy(final String host, final int port, String proxyUser, String
269287 @ Override
270288 public List <Proxy > select (URI uri ) {
271289 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 )));
274292 } else {
275- return delegate == null ? Arrays . asList (Proxy .NO_PROXY )
293+ return delegate == null ? Collections . singletonList (Proxy .NO_PROXY )
276294 : delegate .select (uri );
277295 }
278296 }
@@ -327,7 +345,7 @@ public String getFileContent(String branchName, String fileRelativePath, String
327345 }
328346
329347 @ 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 ) {
331349 try {
332350 try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
333351 try (Git git = getLocalGit (wc )) {
@@ -359,7 +377,7 @@ public void setFileContent(String branchName, String filePath, String content, S
359377 fw .write (content );
360378 fw .close ();
361379
362- git
380+ RevCommit res = git
363381 .commit ()
364382 .setOnly (filePath )
365383 .setMessage (commitMessage )
@@ -374,6 +392,8 @@ public void setFileContent(String branchName, String filePath, String content, S
374392 .setCredentialsProvider (credentials )
375393 .call ();
376394 git .getRepository ().close ();
395+
396+ return res .getName ();
377397 }
378398 }
379399 } catch (GitAPIException e ) {
@@ -449,19 +469,6 @@ public List<VCSDiffEntry> getBranchesDiff(String srcBranchName, String dstBranch
449469 throw new RuntimeException (e );
450470 }
451471 }
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- }
465472
466473 @ Override
467474 public Set <String > getBranches () {
@@ -498,6 +505,7 @@ public List<String> getCommitMessages(String branchName, Integer limit) {
498505
499506 List <String > res = new ArrayList <>();
500507 for (RevCommit commit : logs ) {
508+ commit .getId ().getName ();
501509 res .add (commit .getFullMessage ());
502510 }
503511 git .getRepository ().close ();
@@ -516,7 +524,7 @@ public String getVCSTypeString() {
516524 }
517525
518526 @ Override
519- public void removeFile (String branchName , String filePath , String commitMessage ) {
527+ public String removeFile (String branchName , String filePath , String commitMessage ) {
520528 try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
521529 try (Git git = getLocalGit (wc )) {
522530 String bn = getRealBranchName (branchName );
@@ -537,7 +545,7 @@ public void removeFile(String branchName, String filePath, String commitMessage)
537545 .setCached (false )
538546 .call ();
539547
540- git
548+ RevCommit res = git
541549 .commit ()
542550 .setMessage (commitMessage )
543551 .setAll (true )
@@ -550,11 +558,68 @@ public void removeFile(String branchName, String filePath, String commitMessage)
550558 .call ();
551559
552560 git .getRepository ().close ();
561+ return res .getName ();
553562 }
554563 } catch (GitAPIException e ) {
555564 throw new EVCSException (e );
556565 } catch (Exception e ) {
557566 throw new RuntimeException (e );
558567 }
559568 }
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+ }
560625}
0 commit comments