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.list;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.function.BiFunction;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmFileStatus;
31  import org.apache.maven.scm.ScmVersion;
32  import org.apache.maven.scm.command.list.AbstractListCommand;
33  import org.apache.maven.scm.command.list.ListScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.git.command.GitCommand;
36  import org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback;
37  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
38  import org.apache.maven.scm.provider.git.jgit.command.ScmProviderAwareSshdSessionFactory;
39  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
40  import org.eclipse.jgit.api.Git;
41  import org.eclipse.jgit.api.errors.GitAPIException;
42  import org.eclipse.jgit.lib.Ref;
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 JGitListCommand extends AbstractListCommand implements GitCommand {
51  
52      private BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory> sshSessionFactorySupplier;
53  
54      public JGitListCommand() {
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      protected ListScmResult executeListCommand(
66              ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive, ScmVersion scmVersion)
67              throws ScmException {
68  
69          Git git = null;
70          try {
71              git = JGitUtils.openRepo(fileSet.getBasedir());
72              CredentialsProvider credentials = JGitUtils.prepareSession(git, (GitScmProviderRepository) repo);
73  
74              List<ScmFile> list = new ArrayList<>();
75              Collection<Ref> lsResult = git.lsRemote()
76                      .setCredentialsProvider(credentials)
77                      .setTransportConfigCallback(new JGitTransportConfigCallback(
78                              sshSessionFactorySupplier.apply((GitScmProviderRepository) repo, logger)))
79                      .call();
80              for (Ref ref : lsResult) {
81                  logger.debug(
82                          ref.getObjectId().getName() + "  " + ref.getTarget().getName());
83                  list.add(new ScmFile(ref.getName(), ScmFileStatus.CHECKED_IN));
84              }
85  
86              return new ListScmResult("JGit ls-remote", list);
87          } catch (IOException | GitAPIException e) {
88              throw new ScmException("JGit ls-remote failure!", e);
89          } finally {
90              JGitUtils.closeRepo(git);
91          }
92      }
93  }