1
1
package org .scm4j .vcs ;
2
2
3
- import java .io .ByteArrayOutputStream ;
4
- import java .io .File ;
5
- import java .io .FileWriter ;
6
- import java .io .IOException ;
7
- import java .net .Authenticator ;
8
- import java .net .InetSocketAddress ;
9
- import java .net .PasswordAuthentication ;
10
- import java .net .Proxy ;
11
- import java .net .Proxy .Type ;
12
- import java .net .ProxySelector ;
13
- import java .net .SocketAddress ;
14
- import java .net .URI ;
15
- import java .nio .charset .StandardCharsets ;
16
- import java .util .ArrayList ;
17
- import java .util .Collections ;
18
- import java .util .HashSet ;
19
- import java .util .List ;
20
- import java .util .Set ;
21
-
22
3
import org .apache .commons .io .FileUtils ;
23
4
import org .apache .commons .io .IOUtils ;
24
- import org .eclipse .jgit .api .CheckoutCommand ;
5
+ import org .eclipse .jgit .api .* ;
25
6
import org .eclipse .jgit .api .CreateBranchCommand .SetupUpstreamMode ;
26
- import org .eclipse .jgit .api .Git ;
27
- import org .eclipse .jgit .api .ListBranchCommand .ListMode ;
28
- import org .eclipse .jgit .api .LogCommand ;
29
- import org .eclipse .jgit .api .MergeResult ;
30
- import org .eclipse .jgit .api .PushCommand ;
31
7
import org .eclipse .jgit .api .ResetCommand .ResetType ;
32
8
import org .eclipse .jgit .api .errors .GitAPIException ;
33
9
import org .eclipse .jgit .api .errors .RefAlreadyExistsException ;
34
10
import org .eclipse .jgit .diff .DiffEntry ;
35
11
import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
36
12
import org .eclipse .jgit .diff .DiffEntry .Side ;
37
13
import org .eclipse .jgit .diff .DiffFormatter ;
38
- import org .eclipse .jgit .lib .Constants ;
39
- import org .eclipse .jgit .lib .ObjectId ;
40
- import org .eclipse .jgit .lib .ObjectReader ;
41
- import org .eclipse .jgit .lib .Ref ;
42
- import org .eclipse .jgit .lib .Repository ;
43
- import org .eclipse .jgit .revwalk .RevCommit ;
44
- import org .eclipse .jgit .revwalk .RevObject ;
45
- import org .eclipse .jgit .revwalk .RevSort ;
46
- import org .eclipse .jgit .revwalk .RevTag ;
47
- import org .eclipse .jgit .revwalk .RevWalk ;
14
+ import org .eclipse .jgit .lib .*;
15
+ import org .eclipse .jgit .revwalk .*;
48
16
import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
49
17
import org .eclipse .jgit .transport .CredentialsProvider ;
50
18
import org .eclipse .jgit .transport .RefSpec ;
51
19
import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
52
20
import org .eclipse .jgit .treewalk .CanonicalTreeParser ;
53
- import org .scm4j .vcs .api .IVCS ;
54
- import org .scm4j .vcs .api .VCSChangeType ;
55
- import org .scm4j .vcs .api .VCSCommit ;
56
- import org .scm4j .vcs .api .VCSDiffEntry ;
57
- import org .scm4j .vcs .api .VCSMergeResult ;
58
- import org .scm4j .vcs .api .VCSTag ;
59
- import org .scm4j .vcs .api .WalkDirection ;
21
+ import org .eclipse .jgit .treewalk .TreeWalk ;
22
+ import org .eclipse .jgit .treewalk .filter .PathFilter ;
23
+ import org .scm4j .vcs .api .*;
60
24
import org .scm4j .vcs .api .exceptions .EVCSBranchExists ;
61
25
import org .scm4j .vcs .api .exceptions .EVCSException ;
62
26
import org .scm4j .vcs .api .exceptions .EVCSFileNotFound ;
65
29
import org .scm4j .vcs .api .workingcopy .IVCSRepositoryWorkspace ;
66
30
import org .scm4j .vcs .api .workingcopy .IVCSWorkspace ;
67
31
32
+ import java .io .*;
33
+ import java .net .*;
34
+ import java .net .Proxy .Type ;
35
+ import java .nio .charset .StandardCharsets ;
36
+ import java .util .*;
37
+
68
38
public class GitVCS implements IVCS {
69
39
70
40
public static final String GIT_VCS_TYPE_STRING = "git" ;
@@ -319,25 +289,30 @@ public String getFileContent(String branchName, String fileRelativePath, String
319
289
Git git = getLocalGit (wc );
320
290
Repository gitRepo = git .getRepository ()) {
321
291
322
- checkout (git , gitRepo , branchName , revision );
323
- File file = new File (wc .getFolder (), fileRelativePath );
324
- if (!file .exists ()) {
292
+ git
293
+ .fetch ()
294
+ .setRefSpecs (new RefSpec ("+refs/heads/*:refs/heads/*" ))
295
+ //.setRemoveDeletedRefs(true)
296
+ .setCredentialsProvider (credentials )
297
+ .call ();
298
+ git .pull ().call (); //TODO: add test when we receive correct file version if we change it from another LWC
299
+ RevWalk revWalk = new RevWalk (gitRepo );
300
+ TreeWalk treeWalk = new TreeWalk (gitRepo );
301
+ ObjectId revisionCommitId = gitRepo .resolve (revision == null ? "refs/heads/" + getRealBranchName (branchName ) : revision );
302
+ RevCommit commit = revWalk .parseCommit (revisionCommitId );
303
+ RevTree tree = commit .getTree ();
304
+ treeWalk .addTree (tree );
305
+ treeWalk .setRecursive (true );
306
+ treeWalk .setFilter (PathFilter .create (fileRelativePath ));
307
+ if (!treeWalk .next ()) {
325
308
throw new EVCSFileNotFound (String .format ("File %s is not found" , fileRelativePath ));
326
309
}
327
- String res = IOUtils .toString (file .toURI (), StandardCharsets .UTF_8 );
328
-
329
- if (revision != null ) {
330
- // leaving Detached HEAD state
331
- String bn = getRealBranchName (branchName );
332
- git
333
- .checkout ()
334
- .setStartPoint ("origin/" + bn )
335
- .setCreateBranch (gitRepo .exactRef ("refs/heads/" + bn ) == null )
336
- .setUpstreamMode (SetupUpstreamMode .TRACK )
337
- .setName (bn )
338
- .call ();
339
- }
340
- return res ;
310
+ ObjectId objectId = treeWalk .getObjectId (0 );
311
+
312
+ ObjectLoader loader = gitRepo .open (objectId );
313
+ InputStream in = loader .openStream ();
314
+ return IOUtils .toString (in , StandardCharsets .UTF_8 );
315
+
341
316
} catch (EVCSFileNotFound e ) {
342
317
throw e ;
343
318
} catch (GitAPIException e ) {
@@ -469,15 +444,9 @@ public Set<String> getBranches(String path) {
469
444
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
470
445
Git git = getLocalGit (wc );
471
446
Repository gitRepo = git .getRepository ()) {
472
-
473
- git
474
- .pull ()
475
- .call ();
476
447
477
- List <Ref > refs = git
478
- .branchList ()
479
- .setListMode (ListMode .REMOTE )
480
- .call ();
448
+ git .pull ().call (); //TODO: add test when we receive correct branches list if we change it from another LWC
449
+ Collection <Ref > refs = gitRepo .getRefDatabase ().getRefs (REFS_REMOTES_ORIGIN ).values ();
481
450
Set <String > res = new HashSet <>();
482
451
String bn ;
483
452
for (Ref ref : refs ) {
@@ -728,11 +697,9 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
728
697
Git git = getLocalGit (wc );
729
698
Repository gitRepo = git .getRepository ();
730
699
RevWalk rw = new RevWalk (gitRepo )) {
731
-
732
- git
733
- .pull ()
734
- .call ();
735
-
700
+
701
+ updateLocalTags (git );
702
+
736
703
RevCommit commitToTag = revisionToTag == null ? null : rw .parseCommit (ObjectId .fromString (revisionToTag ));
737
704
738
705
Ref ref = git
@@ -758,14 +725,28 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
758
725
}
759
726
}
760
727
728
+ private void updateLocalTags (Git git ) throws Exception {
729
+ // need to remove tags from local repo which are removed in origin
730
+ git
731
+ .fetch ()
732
+ .setRefSpecs (new RefSpec ("+refs/tags/*:refs/tags/*" ))
733
+ .setRemoveDeletedRefs (true )
734
+ .setCredentialsProvider (credentials )
735
+ .call ();
736
+ git
737
+ .pull ()
738
+ .call ();
739
+ }
740
+
761
741
@ Override
762
742
public List <VCSTag > getTags () {
763
743
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
764
744
Git git = getLocalGit (wc );
765
745
Repository gitRepo = git .getRepository ();
766
746
RevWalk rw = new RevWalk (gitRepo )) {
767
-
768
- List <Ref > tagRefs = getTagRefs (git );
747
+
748
+ updateLocalTags (git );
749
+ Collection <Ref > tagRefs = gitRepo .getTags ().values ();
769
750
List <VCSTag > res = new ArrayList <>();
770
751
RevCommit revCommit ;
771
752
for (Ref ref : tagRefs ) {
@@ -789,36 +770,35 @@ public List<VCSTag> getTags() {
789
770
}
790
771
}
791
772
792
- private List <Ref > getTagRefs (Git git ) throws Exception {
793
- // need to remove tags from local repo which are removed in origin
794
- git
795
- .fetch ()
796
- .setRefSpecs (new RefSpec ("+refs/tags/*:refs/tags/*" ))
797
- .setRemoveDeletedRefs (true )
798
- .setCredentialsProvider (credentials )
799
- .call ();
800
- return git
801
- .tagList ()
802
- .call ();
803
- }
773
+ // private List<Ref> getTagRefs(Git git) throws Exception {
774
+ // // need to remove tags from local repo which are removed in origin
775
+ // git
776
+ // .fetch()
777
+ // .setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*"))
778
+ // .setRemoveDeletedRefs(true)
779
+ // .setCredentialsProvider(credentials)
780
+ // .call();
781
+ // return git
782
+ // .tagList()
783
+ // .call();
784
+ // }
804
785
805
786
@ Override
806
787
public void removeTag (String tagName ) {
807
788
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
808
789
Git git = getLocalGit (wc );
809
790
Repository gitRepo = git .getRepository ();
810
791
RevWalk rw = new RevWalk (gitRepo )) {
811
-
812
- git . pull (). call ( );
792
+
793
+ updateLocalTags ( git );
813
794
814
795
git
815
796
.tagDelete ()
816
797
.setTags (tagName )
817
798
.call ();
818
799
819
800
push (git , new RefSpec (":refs/tags/" + tagName ));
820
-
821
-
801
+
822
802
} catch (GitAPIException e ) {
823
803
throw new EVCSException (e );
824
804
} catch (Exception e ) {
@@ -846,10 +826,11 @@ public List<VCSTag> getTagsOnRevision(String revision) {
846
826
Git git = getLocalGit (wc );
847
827
Repository gitRepo = git .getRepository ();
848
828
RevWalk rw = new RevWalk (gitRepo )) {
849
-
829
+
830
+ updateLocalTags (git );
831
+
850
832
List <VCSTag > res = new ArrayList <>();
851
-
852
- List <Ref > tagRefs = getTagRefs (git );
833
+ Collection <Ref > tagRefs = gitRepo .getTags ().values ();
853
834
RevCommit revCommit ;
854
835
for (Ref ref : tagRefs ) {
855
836
ObjectId relatedCommitObjectId = ref .getPeeledObjectId () == null ? ref .getObjectId () : ref .getPeeledObjectId ();
0 commit comments