View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.jgit.command.remoteinfo;
20  
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.function.BiFunction;
25  
26  import org.apache.maven.scm.CommandParameters;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand;
30  import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  import org.apache.maven.scm.provider.git.command.GitCommand;
33  import org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback;
34  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
35  import org.apache.maven.scm.provider.git.jgit.command.ScmProviderAwareSshdSessionFactory;
36  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
37  import org.eclipse.jgit.api.Git;
38  import org.eclipse.jgit.api.LsRemoteCommand;
39  import org.eclipse.jgit.lib.Ref;
40  import org.eclipse.jgit.lib.Repository;
41  import org.eclipse.jgit.transport.CredentialsProvider;
42  import org.slf4j.Logger;
43  
44  /**
45   * @author Dominik Bartholdi (imod)
46   * @since 1.9
47   */
48  public class JGitRemoteInfoCommand extends AbstractRemoteInfoCommand implements GitCommand {
49  
50      private BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory> sshSessionFactorySupplier;
51  
52      public JGitRemoteInfoCommand() {
53          sshSessionFactorySupplier = ScmProviderAwareSshdSessionFactory::new;
54      }
55  
56      public void setSshSessionFactorySupplier(
57              BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory>
58                      sshSessionFactorySupplier) {
59          this.sshSessionFactorySupplier = sshSessionFactorySupplier;
60      }
61  
62      @Override
63      public RemoteInfoScmResult executeRemoteInfoCommand(
64              ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
65  
66          GitScmProviderRepository repo = (GitScmProviderRepository) repository;
67          Git git = null;
68          try {
69              git = JGitUtils.openRepo(fileSet.getBasedir());
70              CredentialsProvider credentials = JGitUtils.getCredentials(repo);
71  
72              LsRemoteCommand lsCommand = git.lsRemote()
73                      .setRemote(repo.getPushUrl())
74                      .setCredentialsProvider(credentials)
75                      .setTransportConfigCallback(
76                              new JGitTransportConfigCallback(sshSessionFactorySupplier.apply(repo, logger)));
77  
78              Map<String, String> tag = new HashMap<>();
79              Collection<Ref> allTags = lsCommand.setHeads(false).setTags(true).call();
80              for (Ref ref : allTags) {
81                  tag.put(
82                          Repository.shortenRefName(ref.getName()),
83                          ref.getObjectId().name());
84              }
85  
86              Map<String, String> heads = new HashMap<>();
87              Collection<Ref> allHeads = lsCommand.setHeads(true).setTags(false).call();
88              for (Ref ref : allHeads) {
89                  heads.put(
90                          Repository.shortenRefName(ref.getName()),
91                          ref.getObjectId().name());
92              }
93  
94              return new RemoteInfoScmResult("JGit remoteinfo", heads, tag);
95          } catch (Exception e) {
96              throw new ScmException("JGit remoteinfo failure!", e);
97          } finally {
98              JGitUtils.closeRepo(git);
99          }
100     }
101 }