1 package org.apache.maven.scm.provider.git.jgit.command.remoteinfo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.CommandParameters;
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand;
26 import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.git.command.GitCommand;
29 import org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback;
30 import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
31 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
32 import org.eclipse.jgit.api.Git;
33 import org.eclipse.jgit.api.LsRemoteCommand;
34 import org.eclipse.jgit.lib.Ref;
35 import org.eclipse.jgit.lib.Repository;
36 import org.eclipse.jgit.transport.CredentialsProvider;
37
38 import java.util.Collection;
39 import java.util.HashMap;
40 import java.util.Map;
41
42
43
44
45
46 public class JGitRemoteInfoCommand
47 extends AbstractRemoteInfoCommand
48 implements GitCommand
49 {
50
51 @Override
52 public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet,
53 CommandParameters parameters )
54 throws ScmException
55 {
56
57 GitScmProviderRepository repo = (GitScmProviderRepository) repository;
58 Git git = null;
59 try
60 {
61 git = JGitUtils.openRepo( fileSet.getBasedir() );
62 CredentialsProvider credentials = JGitUtils.getCredentials( repo );
63
64 LsRemoteCommand lsCommand =
65 git.lsRemote().setRemote( repo.getPushUrl() ).setCredentialsProvider( credentials )
66 .setTransportConfigCallback( new JGitTransportConfigCallback( repo, getLogger() ) );
67
68 Map<String, String> tag = new HashMap<String, String>();
69 Collection<Ref> allTags = lsCommand.setHeads( false ).setTags( true ).call();
70 for ( Ref ref : allTags )
71 {
72 tag.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() );
73 }
74
75 Map<String, String> heads = new HashMap<String, String>();
76 Collection<Ref> allHeads = lsCommand.setHeads( true ).setTags( false ).call();
77 for ( Ref ref : allHeads )
78 {
79 heads.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() );
80 }
81
82 return new RemoteInfoScmResult( "JGit remoteinfo", heads, tag );
83 }
84 catch ( Exception e )
85 {
86 throw new ScmException( "JGit remoteinfo failure!", e );
87 }
88 finally
89 {
90 JGitUtils.closeRepo( git );
91 }
92 }
93 }