15
15
import java .nio .charset .StandardCharsets ;
16
16
import java .util .ArrayList ;
17
17
import java .util .Collections ;
18
- import java .util .Comparator ;
19
- import java .util .Date ;
20
18
import java .util .HashSet ;
21
19
import java .util .List ;
22
20
import java .util .Set ;
@@ -286,34 +284,40 @@ public String getRepoUrl() {
286
284
return repo .getRepoUrl ();
287
285
}
288
286
289
- private File getFileFromRepo (String branchName , String fileRelativePath ) {
287
+ @ Override
288
+ public String getFileContent (String branchName , String fileRelativePath , String revision ) {
290
289
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
291
290
Git git = getLocalGit (wc );
292
291
Repository gitRepo = git .getRepository ()) {
293
292
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 ;
297
314
} catch (GitAPIException e ) {
298
315
throw new EVCSException (e );
299
316
} catch (Exception e ) {
300
317
throw new RuntimeException (e );
301
318
}
302
319
}
303
320
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
-
317
321
@ Override
318
322
public VCSCommit setFileContent (String branchName , String filePath , String content , String commitMessage ) {
319
323
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
@@ -357,15 +361,16 @@ void checkout(Git git, Repository gitRepo, String branchName, String revision) t
357
361
String bn = getRealBranchName (branchName );
358
362
CheckoutCommand cmd = git .checkout ();
359
363
if (revision == null ) {
364
+ git
365
+ .pull ()
366
+ .call ();
360
367
cmd
361
368
.setStartPoint ("origin/" + bn )
362
369
.setCreateBranch (gitRepo .exactRef ("refs/heads/" + bn ) == null )
363
370
.setUpstreamMode (SetupUpstreamMode .TRACK )
364
371
.setName (bn )
365
372
.call ();
366
- git
367
- .pull ()
368
- .call ();
373
+
369
374
} else {
370
375
try (RevWalk walk = new RevWalk (gitRepo )) {
371
376
RevCommit commit = walk .parseCommit (RevCommit .fromString (revision ));
@@ -377,11 +382,6 @@ void checkout(Git git, Repository gitRepo, String branchName, String revision) t
377
382
}
378
383
}
379
384
380
- @ Override
381
- public String getFileContent (String branchName , String filePath ) {
382
- return getFileContent (branchName , filePath , StandardCharsets .UTF_8 .name ());
383
- }
384
-
385
385
@ Override
386
386
public List <VCSDiffEntry > getBranchesDiff (String srcBranchName , String dstBranchName ) {
387
387
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
@@ -450,8 +450,16 @@ public Set<String> getBranches(String path) {
450
450
.setListMode (ListMode .REMOTE )
451
451
.call ();
452
452
Set <String > res = new HashSet <>();
453
+ String bn ;
453
454
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
+ }
455
463
}
456
464
return res ;
457
465
} catch (GitAPIException e ) {
@@ -672,7 +680,18 @@ public String toString() {
672
680
673
681
@ Override
674
682
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
+ }
676
695
}
677
696
678
697
@ Override
@@ -716,6 +735,8 @@ public List<VCSTag> getTags() {
716
735
Repository gitRepo = git .getRepository ();
717
736
RevWalk rw = new RevWalk (gitRepo )) {
718
737
738
+ git .pull ().call ();
739
+
719
740
List <Ref > tagRefs = getTagRefs ();
720
741
List <VCSTag > res = new ArrayList <>();
721
742
RevCommit revCommit ;
@@ -754,50 +775,6 @@ List<Ref> getTagRefs() throws Exception {
754
775
}
755
776
}
756
777
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
-
801
778
@ Override
802
779
public void removeTag (String tagName ) {
803
780
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
@@ -836,62 +813,34 @@ public void checkout(String branchName, String targetPath, String revision) {
836
813
}
837
814
838
815
@ 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 ) {
869
817
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
870
818
Git git = getLocalGit (wc );
871
819
Repository gitRepo = git .getRepository ();
872
820
RevWalk rw = new RevWalk (gitRepo )) {
873
821
822
+ List <VCSTag > res = new ArrayList <>();
823
+
874
824
git .pull ().call ();
875
825
876
826
List <Ref > tagRefs = getTagRefs ();
877
827
RevCommit revCommit ;
878
828
for (Ref ref : tagRefs ) {
879
829
ObjectId relatedCommitObjectId = ref .getPeeledObjectId () == null ? ref .getObjectId () : ref .getPeeledObjectId ();
880
830
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
+ }
893
841
}
894
- return null ;
842
+
843
+ return res ;
895
844
} catch (GitAPIException e ) {
896
845
throw new EVCSException (e );
897
846
} catch (Exception e ) {
0 commit comments