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