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