View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.command.remoteinfo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Dominik Bartholdi (imod)
44   * @since 1.9
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  }