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.jgit.command.ScmProviderAwareSshdSessionFactory;
32  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
33  import org.eclipse.jgit.api.Git;
34  import org.eclipse.jgit.api.LsRemoteCommand;
35  import org.eclipse.jgit.lib.Ref;
36  import org.eclipse.jgit.lib.Repository;
37  import org.eclipse.jgit.transport.CredentialsProvider;
38  import org.slf4j.Logger;
39  
40  import java.util.Collection;
41  import java.util.HashMap;
42  import java.util.Map;
43  import java.util.function.BiFunction;
44  
45  /**
46   * @author Dominik Bartholdi (imod)
47   * @since 1.9
48   */
49  public class JGitRemoteInfoCommand
50      extends AbstractRemoteInfoCommand
51      implements GitCommand
52  {
53  
54      private BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory> sshSessionFactorySupplier;
55  
56      public JGitRemoteInfoCommand()
57      {
58          sshSessionFactorySupplier = ScmProviderAwareSshdSessionFactory::new;
59      }
60  
61      public void setSshSessionFactorySupplier(
62          BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory> sshSessionFactorySupplier )
63      {
64          this.sshSessionFactorySupplier = sshSessionFactorySupplier;
65      }
66  
67      @Override
68      public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet,
69                                                           CommandParameters parameters )
70          throws ScmException
71      {
72  
73          GitScmProviderRepository repo = (GitScmProviderRepository) repository;
74          Git git = null;
75          try
76          {
77              git = JGitUtils.openRepo( fileSet.getBasedir() );
78              CredentialsProvider credentials = JGitUtils.getCredentials( repo );
79  
80              LsRemoteCommand lsCommand =
81                  git.lsRemote().setRemote( repo.getPushUrl() ).setCredentialsProvider( credentials )
82                          .setTransportConfigCallback(
83                              new JGitTransportConfigCallback( sshSessionFactorySupplier.apply( repo, logger ) )
84                          );
85  
86              Map<String, String> tag = new HashMap<>();
87              Collection<Ref> allTags = lsCommand.setHeads( false ).setTags( true ).call();
88              for ( Ref ref : allTags )
89              {
90                  tag.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() );
91              }
92  
93              Map<String, String> heads = new HashMap<>();
94              Collection<Ref> allHeads = lsCommand.setHeads( true ).setTags( false ).call();
95              for ( Ref ref : allHeads )
96              {
97                  heads.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() );
98              }
99  
100             return new RemoteInfoScmResult( "JGit remoteinfo", heads, tag );
101         }
102         catch ( Exception e )
103         {
104             throw new ScmException( "JGit remoteinfo failure!", e );
105         }
106         finally
107         {
108             JGitUtils.closeRepo( git );
109         }
110     }
111 }