1515import java .nio .charset .StandardCharsets ;
1616import java .util .ArrayList ;
1717import java .util .Collections ;
18- import java .util .Comparator ;
19- import java .util .Date ;
2018import java .util .HashSet ;
2119import java .util .List ;
2220import java .util .Set ;
@@ -286,34 +284,40 @@ public String getRepoUrl() {
286284 return repo .getRepoUrl ();
287285 }
288286
289- private File getFileFromRepo (String branchName , String fileRelativePath ) {
287+ @ Override
288+ public String getFileContent (String branchName , String fileRelativePath , String revision ) {
290289 try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
291290 Git git = getLocalGit (wc );
292291 Repository gitRepo = git .getRepository ()) {
293292
294- checkout (git , gitRepo , branchName , null );
295-
296- return new File (wc .getFolder (), fileRelativePath );
293+ checkout (git , gitRepo , branchName , revision );
294+ File file = new File (wc .getFolder (), fileRelativePath );
295+ if (!file .exists ()) {
296+ throw new EVCSFileNotFound (String .format ("File %s is not found" , fileRelativePath ));
297+ }
298+ String res = IOUtils .toString (file .toURI (), StandardCharsets .UTF_8 );
299+
300+ if (revision != null ) {
301+ // leaving Detached HEAD state
302+ String bn = getRealBranchName (branchName );
303+ git
304+ .checkout ()
305+ .setStartPoint ("origin/" + bn )
306+ .setCreateBranch (gitRepo .exactRef ("refs/heads/" + bn ) == null )
307+ .setUpstreamMode (SetupUpstreamMode .TRACK )
308+ .setName (bn )
309+ .call ();
310+ }
311+ return res ;
312+ } catch (EVCSFileNotFound e ) {
313+ throw e ;
297314 } catch (GitAPIException e ) {
298315 throw new EVCSException (e );
299316 } catch (Exception e ) {
300317 throw new RuntimeException (e );
301318 }
302319 }
303320
304- @ Override
305- public String getFileContent (String branchName , String fileRelativePath , String encoding ) {
306- File file = getFileFromRepo (branchName , fileRelativePath );
307- if (!file .exists ()) {
308- throw new EVCSFileNotFound (String .format ("File %s is not found" , fileRelativePath ));
309- }
310- try {
311- return IOUtils .toString (file .toURI (), encoding );
312- } catch (Exception e ) {
313- throw new RuntimeException (e );
314- }
315- }
316-
317321 @ Override
318322 public VCSCommit setFileContent (String branchName , String filePath , String content , String commitMessage ) {
319323 try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
@@ -357,15 +361,16 @@ void checkout(Git git, Repository gitRepo, String branchName, String revision) t
357361 String bn = getRealBranchName (branchName );
358362 CheckoutCommand cmd = git .checkout ();
359363 if (revision == null ) {
364+ git
365+ .pull ()
366+ .call ();
360367 cmd
361368 .setStartPoint ("origin/" + bn )
362369 .setCreateBranch (gitRepo .exactRef ("refs/heads/" + bn ) == null )
363370 .setUpstreamMode (SetupUpstreamMode .TRACK )
364371 .setName (bn )
365372 .call ();
366- git
367- .pull ()
368- .call ();
373+
369374 } else {
370375 try (RevWalk walk = new RevWalk (gitRepo )) {
371376 RevCommit commit = walk .parseCommit (RevCommit .fromString (revision ));
@@ -377,11 +382,6 @@ void checkout(Git git, Repository gitRepo, String branchName, String revision) t
377382 }
378383 }
379384
380- @ Override
381- public String getFileContent (String branchName , String filePath ) {
382- return getFileContent (branchName , filePath , StandardCharsets .UTF_8 .name ());
383- }
384-
385385 @ Override
386386 public List <VCSDiffEntry > getBranchesDiff (String srcBranchName , String dstBranchName ) {
387387 try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
@@ -450,8 +450,16 @@ public Set<String> getBranches(String path) {
450450 .setListMode (ListMode .REMOTE )
451451 .call ();
452452 Set <String > res = new HashSet <>();
453+ String bn ;
453454 for (Ref ref : refs ) {
454- res .add (ref .getName ().replace (REFS_REMOTES_ORIGIN , "" ));
455+ bn = ref .getName ().replace (REFS_REMOTES_ORIGIN , "" );
456+ if (path == null ) {
457+ res .add (bn );
458+ } else {
459+ if (bn .startsWith (path )) {
460+ res .add (bn );
461+ }
462+ }
455463 }
456464 return res ;
457465 } catch (GitAPIException e ) {
@@ -672,7 +680,18 @@ public String toString() {
672680
673681 @ Override
674682 public Boolean fileExists (String branchName , String filePath ) {
675- return getFileFromRepo (branchName , filePath ).exists ();
683+ try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
684+ Git git = getLocalGit (wc );
685+ Repository gitRepo = git .getRepository ()) {
686+
687+ checkout (git , gitRepo , branchName , null );
688+
689+ return new File (wc .getFolder (), filePath ).exists ();
690+ } catch (GitAPIException e ) {
691+ throw new EVCSException (e );
692+ } catch (Exception e ) {
693+ throw new RuntimeException (e );
694+ }
676695 }
677696
678697 @ Override
@@ -716,6 +735,8 @@ public List<VCSTag> getTags() {
716735 Repository gitRepo = git .getRepository ();
717736 RevWalk rw = new RevWalk (gitRepo )) {
718737
738+ git .pull ().call ();
739+
719740 List <Ref > tagRefs = getTagRefs ();
720741 List <VCSTag > res = new ArrayList <>();
721742 RevCommit revCommit ;
@@ -754,50 +775,6 @@ List<Ref> getTagRefs() throws Exception {
754775 }
755776 }
756777
757- @ Override
758- public VCSTag getLastTag () {
759- List <Ref > tagRefs ;
760- try {
761- tagRefs = getTagRefs ();
762- if (tagRefs .isEmpty ()) {
763- return null ;
764- }
765- } catch (Exception e ) {
766- throw new RuntimeException (e );
767- }
768- try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
769- Git git = getLocalGit (wc );
770- Repository gitRepo = git .getRepository ();
771- RevWalk rw = new RevWalk (gitRepo )) {
772-
773- Collections .sort (tagRefs , new Comparator <Ref >() {
774- public int compare (Ref o1 , Ref o2 ) {
775- try (Repository gitRepo = git .getRepository ();
776- RevWalk rw = new RevWalk (gitRepo )) { // for exception rethrow test only
777- Date d1 = rw .parseTag (o1 .getObjectId ()).getTaggerIdent ().getWhen ();
778- Date d2 = rw .parseTag (o2 .getObjectId ()).getTaggerIdent ().getWhen ();
779- return d1 .compareTo (d2 );
780- } catch (Exception e ) {
781- throw new RuntimeException (e );
782- }
783- }
784- });
785-
786- Ref ref = tagRefs .get (tagRefs .size () - 1 );
787- RevCommit revCommit = rw .parseCommit (ref .getObjectId ());
788- VCSCommit relatedCommit = getVCSCommit (revCommit );
789- if (git .getRepository ().peel (ref ).getPeeledObjectId () == null ) {
790- return new VCSTag (ref .getName ().replace ("refs/tags/" , "" ), null , null , relatedCommit );
791- }
792- RevTag revTag = rw .parseTag (ref .getObjectId ());
793- return new VCSTag (revTag .getTagName (), revTag .getFullMessage (), revTag .getTaggerIdent ().getName (), relatedCommit );
794- } catch (GitAPIException e ) {
795- throw new EVCSException (e );
796- } catch (Exception e ) {
797- throw new RuntimeException (e );
798- }
799- }
800-
801778 @ Override
802779 public void removeTag (String tagName ) {
803780 try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
@@ -836,62 +813,34 @@ public void checkout(String branchName, String targetPath, String revision) {
836813 }
837814
838815 @ Override
839- public Boolean isRevisionTagged (String revision ) {
840- try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
841- Git git = getLocalGit (wc );
842- Repository gitRepo = git .getRepository ();
843- RevWalk rw = new RevWalk (gitRepo )) {
844-
845- checkout (git , gitRepo , MASTER_BRANCH_NAME , null );
846- List <Ref > tagRefs = getTagRefs ();
847- for (Ref ref : tagRefs ) {
848- RevObject revObject = rw .parseAny (ref .getObjectId ());
849- if (revObject instanceof RevTag ) {
850- if (((RevTag ) revObject ).getObject ().getName ().equals (revision )) {
851- return true ;
852- }
853- } else {
854- if (revObject .getName ().equals (revision )) {
855- return true ;
856- }
857- }
858- }
859- return false ;
860- } catch (GitAPIException e ) {
861- throw new EVCSException (e );
862- } catch (Exception e ) {
863- throw new RuntimeException (e );
864- }
865- }
866-
867- @ Override
868- public VCSTag getTagByName (String tagName ) {
816+ public List <VCSTag > getTagsOnRevision (String revision ) {
869817 try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
870818 Git git = getLocalGit (wc );
871819 Repository gitRepo = git .getRepository ();
872820 RevWalk rw = new RevWalk (gitRepo )) {
873821
822+ List <VCSTag > res = new ArrayList <>();
823+
874824 git .pull ().call ();
875825
876826 List <Ref > tagRefs = getTagRefs ();
877827 RevCommit revCommit ;
878828 for (Ref ref : tagRefs ) {
879829 ObjectId relatedCommitObjectId = ref .getPeeledObjectId () == null ? ref .getObjectId () : ref .getPeeledObjectId ();
880830 revCommit = rw .parseCommit (relatedCommitObjectId );
881- VCSCommit relatedCommit = getVCSCommit (revCommit );
882- RevObject revObject = rw .parseAny (ref .getObjectId ());
883- if (revObject instanceof RevTag ) {
884- RevTag revTag = (RevTag ) revObject ;
885- if (revTag .getTagName ().equals (tagName )) {
886- return new VCSTag (revTag .getTagName (), revTag .getFullMessage (), revTag .getTaggerIdent ().getName (), relatedCommit );
887- }
888- } else {
889- if (ref .getName ().replace ("refs/tags/" , "" ).equals (tagName )) {
890- return new VCSTag (ref .getName ().replace ("refs/tags/" , "" ), null , null , relatedCommit );
891- }
892- }
831+ if (revCommit .getName ().equals (revision )) {
832+ VCSCommit relatedCommit = getVCSCommit (revCommit );
833+ RevObject revObject = rw .parseAny (ref .getObjectId ());
834+ if (revObject instanceof RevTag ) {
835+ RevTag revTag = (RevTag ) revObject ;
836+ res .add (new VCSTag (revTag .getTagName (), revTag .getFullMessage (), revTag .getTaggerIdent ().getName (), relatedCommit ));
837+ } else {
838+ res .add (new VCSTag (ref .getName ().replace ("refs/tags/" , "" ), null , null , relatedCommit ));
839+ }
840+ }
893841 }
894- return null ;
842+
843+ return res ;
895844 } catch (GitAPIException e ) {
896845 throw new EVCSException (e );
897846 } catch (Exception e ) {
0 commit comments