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 .io .InputStream ;
8
+ import java .net .Authenticator ;
9
+ import java .net .InetSocketAddress ;
10
+ import java .net .PasswordAuthentication ;
11
+ import java .net .Proxy ;
12
+ import java .net .Proxy .Type ;
13
+ import java .net .ProxySelector ;
14
+ import java .net .SocketAddress ;
15
+ import java .net .URI ;
16
+ import java .nio .charset .StandardCharsets ;
17
+ import java .util .ArrayList ;
18
+ import java .util .Collection ;
19
+ import java .util .Collections ;
20
+ import java .util .HashSet ;
21
+ import java .util .List ;
22
+ import java .util .Set ;
23
+
3
24
import org .apache .commons .io .FileUtils ;
4
25
import org .apache .commons .io .IOUtils ;
5
- import org .eclipse .jgit .api .* ;
26
+ import org .eclipse .jgit .api .CheckoutCommand ;
6
27
import org .eclipse .jgit .api .CreateBranchCommand .SetupUpstreamMode ;
28
+ import org .eclipse .jgit .api .Git ;
29
+ import org .eclipse .jgit .api .LogCommand ;
30
+ import org .eclipse .jgit .api .MergeResult ;
31
+ import org .eclipse .jgit .api .PushCommand ;
7
32
import org .eclipse .jgit .api .ResetCommand .ResetType ;
8
33
import org .eclipse .jgit .api .errors .GitAPIException ;
9
34
import org .eclipse .jgit .api .errors .RefAlreadyExistsException ;
10
35
import org .eclipse .jgit .diff .DiffEntry ;
11
36
import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
12
37
import org .eclipse .jgit .diff .DiffEntry .Side ;
13
38
import org .eclipse .jgit .diff .DiffFormatter ;
14
- import org .eclipse .jgit .lib .*;
15
- import org .eclipse .jgit .revwalk .*;
39
+ import org .eclipse .jgit .lib .Constants ;
40
+ import org .eclipse .jgit .lib .ObjectId ;
41
+ import org .eclipse .jgit .lib .ObjectLoader ;
42
+ import org .eclipse .jgit .lib .ObjectReader ;
43
+ import org .eclipse .jgit .lib .Ref ;
44
+ import org .eclipse .jgit .lib .Repository ;
45
+ import org .eclipse .jgit .revwalk .RevCommit ;
46
+ import org .eclipse .jgit .revwalk .RevObject ;
47
+ import org .eclipse .jgit .revwalk .RevSort ;
48
+ import org .eclipse .jgit .revwalk .RevTag ;
49
+ import org .eclipse .jgit .revwalk .RevTree ;
50
+ import org .eclipse .jgit .revwalk .RevWalk ;
16
51
import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
17
52
import org .eclipse .jgit .transport .CredentialsProvider ;
18
53
import org .eclipse .jgit .transport .RefSpec ;
19
54
import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
20
55
import org .eclipse .jgit .treewalk .CanonicalTreeParser ;
21
56
import org .eclipse .jgit .treewalk .TreeWalk ;
22
57
import org .eclipse .jgit .treewalk .filter .PathFilter ;
23
- import org .scm4j .vcs .api .*;
58
+ import org .scm4j .vcs .api .IVCS ;
59
+ import org .scm4j .vcs .api .VCSChangeType ;
60
+ import org .scm4j .vcs .api .VCSCommit ;
61
+ import org .scm4j .vcs .api .VCSDiffEntry ;
62
+ import org .scm4j .vcs .api .VCSMergeResult ;
63
+ import org .scm4j .vcs .api .VCSTag ;
64
+ import org .scm4j .vcs .api .WalkDirection ;
24
65
import org .scm4j .vcs .api .exceptions .EVCSBranchExists ;
25
66
import org .scm4j .vcs .api .exceptions .EVCSBranchNotFound ;
26
67
import org .scm4j .vcs .api .exceptions .EVCSException ;
30
71
import org .scm4j .vcs .api .workingcopy .IVCSRepositoryWorkspace ;
31
72
import org .scm4j .vcs .api .workingcopy .IVCSWorkspace ;
32
73
33
- import java .io .*;
34
- import java .net .*;
35
- import java .net .Proxy .Type ;
36
- import java .nio .charset .StandardCharsets ;
37
- import java .util .*;
38
-
39
74
public class GitVCS implements IVCS {
40
75
41
76
public static final String GIT_VCS_TYPE_STRING = "git" ;
@@ -73,7 +108,7 @@ protected Git getLocalGit(String folder) throws Exception {
73
108
.setDirectory (new File (folder ))
74
109
.setURI (repo .getRepoUrl ())
75
110
.setCredentialsProvider (credentials )
76
- .setNoCheckout (true )
111
+ .setNoCheckout (false )
77
112
.setCloneAllBranches (true )
78
113
.call ()
79
114
.close ();
@@ -322,7 +357,7 @@ public String getFileContent(String branchName, String fileRelativePath, String
322
357
InputStream in = loader .openStream ();
323
358
return IOUtils .toString (in , StandardCharsets .UTF_8 );
324
359
325
- } catch (EVCSFileNotFound e ) {
360
+ } catch (EVCSFileNotFound | EVCSBranchNotFound e ) {
326
361
throw e ;
327
362
} catch (GitAPIException e ) {
328
363
throw new EVCSException (e );
@@ -585,7 +620,7 @@ public List<VCSCommit> getCommitsRange(String branchName, String afterCommitId,
585
620
throw new RuntimeException (e );
586
621
}
587
622
}
588
-
623
+
589
624
private RevCommit getInitialCommit (Repository gitRepo , String branchName ) throws Exception {
590
625
try (RevWalk rw = new RevWalk (gitRepo )) {
591
626
Ref ref = gitRepo .exactRef ("refs/heads/" + branchName );
@@ -752,6 +787,8 @@ private void updateLocalTags(Git git) throws Exception {
752
787
.setCredentialsProvider (credentials )
753
788
.call ();
754
789
}
790
+
791
+
755
792
756
793
@ Override
757
794
public List <VCSTag > getTags () {
@@ -832,21 +869,23 @@ public List<VCSTag> getTagsOnRevision(String revision) {
832
869
updateLocalTags (git );
833
870
834
871
List <VCSTag > res = new ArrayList <>();
835
- Collection <Ref > tagRefs = gitRepo .getTags ().values ();
872
+
873
+ Collection <Ref > refs = gitRepo .getAllRefsByPeeledObjectId ().get (gitRepo .resolve (revision ));
836
874
RevCommit revCommit ;
837
- for (Ref ref : tagRefs ) {
875
+ for (Ref ref : refs == null ? new ArrayList <Ref >() : refs ) {
876
+ if (!ref .getName ().contains ("refs/tags/" )) {
877
+ continue ;
878
+ }
838
879
ObjectId relatedCommitObjectId = ref .getPeeledObjectId () == null ? ref .getObjectId () : ref .getPeeledObjectId ();
839
880
revCommit = rw .parseCommit (relatedCommitObjectId );
840
- if (revCommit .getName ().equals (revision )) {
841
- VCSCommit relatedCommit = getVCSCommit (revCommit );
842
- RevObject revObject = rw .parseAny (ref .getObjectId ());
843
- if (revObject instanceof RevTag ) {
844
- RevTag revTag = (RevTag ) revObject ;
845
- res .add (new VCSTag (revTag .getTagName (), revTag .getFullMessage (), revTag .getTaggerIdent ().getName (), relatedCommit ));
846
- } else {
847
- res .add (new VCSTag (ref .getName ().replace ("refs/tags/" , "" ), null , null , relatedCommit ));
848
- }
849
- }
881
+ VCSCommit relatedCommit = getVCSCommit (revCommit );
882
+ RevObject revObject = rw .parseAny (ref .getObjectId ());
883
+ if (revObject instanceof RevTag ) {
884
+ RevTag revTag = (RevTag ) revObject ;
885
+ res .add (new VCSTag (revTag .getTagName (), revTag .getFullMessage (), revTag .getTaggerIdent ().getName (), relatedCommit ));
886
+ } else {
887
+ res .add (new VCSTag (ref .getName ().replace ("refs/tags/" , "" ), null , null , relatedCommit ));
888
+ }
850
889
}
851
890
852
891
return res ;
@@ -856,4 +895,6 @@ public List<VCSTag> getTagsOnRevision(String revision) {
856
895
throw new RuntimeException (e );
857
896
}
858
897
}
898
+
899
+
859
900
}
0 commit comments