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.JGitUtils;
30 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
31 import org.eclipse.jgit.api.Git;
32 import org.eclipse.jgit.api.LsRemoteCommand;
33 import org.eclipse.jgit.lib.Ref;
34 import org.eclipse.jgit.lib.Repository;
35 import org.eclipse.jgit.transport.CredentialsProvider;
36
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.Map;
40
41
42
43
44
45 public class JGitRemoteInfoCommand
46 extends AbstractRemoteInfoCommand
47 implements GitCommand
48 {
49
50 @Override
51 public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet,
52 CommandParameters parameters )
53 throws ScmException
54 {
55
56 GitScmProviderRepository repo = (GitScmProviderRepository) repository;
57 Git git = null;
58 try
59 {
60 git = Git.open( fileSet.getBasedir() );
61 CredentialsProvider credentials = JGitUtils.getCredentials( repo );
62
63 LsRemoteCommand lsCommand =
64 git.lsRemote().setRemote( repo.getPushUrl() ).setCredentialsProvider( credentials );
65
66 Map<String, String> tag = new HashMap<String, String>();
67 Collection<Ref> allTags = lsCommand.setHeads( false ).setTags( true ).call();
68 for ( Ref ref : allTags )
69 {
70 tag.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() );
71 }
72
73 Map<String, String> heads = new HashMap<String, String>();
74 Collection<Ref> allHeads = lsCommand.setHeads( true ).setTags( false ).call();
75 for ( Ref ref : allHeads )
76 {
77 heads.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() );
78 }
79
80 return new RemoteInfoScmResult( "JGit remoteinfo", heads, tag );
81 }
82 catch ( Exception e )
83 {
84 throw new ScmException( "JGit remoteinfo failure!", e );
85 }
86 finally
87 {
88 JGitUtils.closeRepo( git );
89 }
90 }
91 }