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
+
3
22
import org .apache .commons .io .FileUtils ;
4
23
import org .apache .commons .io .IOUtils ;
5
24
import org .eclipse .jgit .api .CreateBranchCommand .SetupUpstreamMode ;
13
32
import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
14
33
import org .eclipse .jgit .diff .DiffEntry .Side ;
15
34
import org .eclipse .jgit .diff .DiffFormatter ;
16
- import org .eclipse .jgit .lib .*;
35
+ import org .eclipse .jgit .lib .Constants ;
36
+ import org .eclipse .jgit .lib .ObjectId ;
37
+ import org .eclipse .jgit .lib .ObjectIdRef .PeeledNonTag ;
38
+ import org .eclipse .jgit .lib .ObjectReader ;
39
+ import org .eclipse .jgit .lib .Ref ;
40
+ import org .eclipse .jgit .lib .Repository ;
17
41
import org .eclipse .jgit .revwalk .RevCommit ;
18
42
import org .eclipse .jgit .revwalk .RevSort ;
19
43
import org .eclipse .jgit .revwalk .RevTag ;
23
47
import org .eclipse .jgit .transport .RefSpec ;
24
48
import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
25
49
import org .eclipse .jgit .treewalk .CanonicalTreeParser ;
26
- import org .scm4j .vcs .api .*;
50
+ import org .scm4j .vcs .api .IVCS ;
51
+ import org .scm4j .vcs .api .VCSChangeType ;
52
+ import org .scm4j .vcs .api .VCSCommit ;
53
+ import org .scm4j .vcs .api .VCSDiffEntry ;
54
+ import org .scm4j .vcs .api .VCSMergeResult ;
55
+ import org .scm4j .vcs .api .VCSTag ;
56
+ import org .scm4j .vcs .api .WalkDirection ;
27
57
import org .scm4j .vcs .api .exceptions .EVCSBranchExists ;
28
58
import org .scm4j .vcs .api .exceptions .EVCSException ;
29
59
import org .scm4j .vcs .api .exceptions .EVCSFileNotFound ;
32
62
import org .scm4j .vcs .api .workingcopy .IVCSRepositoryWorkspace ;
33
63
import org .scm4j .vcs .api .workingcopy .IVCSWorkspace ;
34
64
35
- import java .io .ByteArrayOutputStream ;
36
- import java .io .File ;
37
- import java .io .FileWriter ;
38
- import java .io .IOException ;
39
- import java .net .*;
40
- import java .net .Proxy .Type ;
41
- import java .nio .charset .StandardCharsets ;
42
- import java .util .*;
43
-
44
65
public class GitVCS implements IVCS {
45
66
46
67
private static final String MASTER_BRANCH_NAME = "master" ;
@@ -294,7 +315,7 @@ private File getFileFromRepo(String branchName, String fileRelativePath) {
294
315
git
295
316
.checkout ()
296
317
.setCreateBranch (gitRepo .exactRef ("refs/heads/" + bn ) == null )
297
- .addPath (fileRelativePath )
318
+ // .addPath(fileRelativePath)
298
319
.setName (bn )
299
320
.call ();
300
321
@@ -741,6 +762,18 @@ public List<VCSTag> getTags() {
741
762
Git git = getLocalGit (wc );
742
763
Repository gitRepo = git .getRepository ();
743
764
RevWalk rw = new RevWalk (gitRepo )) {
765
+
766
+ // git
767
+ // .checkout()
768
+ // .setCreateBranch(gitRepo.exactRef("refs/heads/master") == null)
769
+ // .setName("master")
770
+ // .call();
771
+ //
772
+ // git
773
+ // .pull()
774
+ // .setCredentialsProvider(credentials)
775
+ // .call();
776
+
744
777
List <Ref > tagRefs = getTagRefs ();
745
778
List <VCSTag > res = new ArrayList <>();
746
779
RevTag revTag ;
@@ -760,43 +793,38 @@ public List<VCSTag> getTags() {
760
793
}
761
794
762
795
private List <Ref > getTagRefs () throws Exception {
763
- try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
764
- Git git = getLocalGit (wc );
765
- Repository gitRepo = git .getRepository ();
766
- RevWalk rw = new RevWalk (gitRepo )) {
767
- git
768
- .checkout ()
769
- .setCreateBranch (gitRepo .exactRef ("refs/heads/" + MASTER_BRANCH_NAME ) == null )
770
- .setName (MASTER_BRANCH_NAME )
771
- .call ();
772
-
773
- git
774
- .pull ()
775
- .setCredentialsProvider (credentials )
776
- .call ();
777
-
778
- List <Ref > refs = git
779
- .tagList ()
780
- .call ();
781
-
782
- return refs ;
783
- }
796
+ return new ArrayList <>(Git
797
+ .lsRemoteRepository ()
798
+ .setTags (true )
799
+ .setHeads (false )
800
+ .setRemote (repo .getRepoUrl ())
801
+ .setCredentialsProvider (credentials )
802
+ .call ());
784
803
}
785
804
786
805
@ Override
787
806
public VCSTag getLastTag () {
807
+ List <Ref > tagRefs ;
808
+ try {
809
+ tagRefs = getTagRefs ();
810
+ if (tagRefs .isEmpty ()) {
811
+ return null ;
812
+ }
813
+ } catch (Exception e ) {
814
+ throw new RuntimeException (e );
815
+ }
788
816
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
789
817
Git git = getLocalGit (wc );
790
818
Repository gitRepo = git .getRepository ();
791
819
RevWalk rw = new RevWalk (gitRepo )) {
792
-
793
- List <Ref > tagRefs = getTagRefs ();
794
- RevTag revTag ;
795
- RevCommit revCommit ;
820
+
796
821
Ref ref = tagRefs .get (tagRefs .size () - 1 );
797
- revTag = rw .parseTag (ref .getObjectId ());
798
- revCommit = rw .parseCommit (ref .getObjectId ());
799
- VCSCommit relatedCommit = new VCSCommit (revCommit .getName (), revCommit .getFullMessage (), revCommit .getAuthorIdent ().getName ());
822
+ RevCommit revCommit = rw .parseCommit (ref .getObjectId ());
823
+ VCSCommit relatedCommit = new VCSCommit (revCommit .getName (), revCommit .getFullMessage (), revCommit .getAuthorIdent ().getName ());
824
+ if (ref instanceof PeeledNonTag ) {
825
+ return new VCSTag (ref .getName ().replace ("refs/tags/" , "" ), null , null , relatedCommit );
826
+ }
827
+ RevTag revTag = rw .parseTag (ref .getObjectId ());
800
828
return new VCSTag (revTag .getTagName (), revTag .getFullMessage (), revTag .getTaggerIdent ().getName (), relatedCommit );
801
829
} catch (GitAPIException e ) {
802
830
throw new EVCSException (e );
0 commit comments