View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.command.list;
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.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmFileStatus;
26  import org.apache.maven.scm.ScmVersion;
27  import org.apache.maven.scm.command.list.AbstractListCommand;
28  import org.apache.maven.scm.command.list.ListScmResult;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.git.command.GitCommand;
31  import org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback;
32  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
33  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
34  import org.eclipse.jgit.api.Git;
35  import org.eclipse.jgit.lib.Ref;
36  import org.eclipse.jgit.transport.CredentialsProvider;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.List;
41  
42  /**
43   * @author Dominik Bartholdi (imod)
44   * @since 1.9
45   */
46  public class JGitListCommand
47      extends AbstractListCommand
48      implements GitCommand
49  {
50  
51      @Override
52      protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
53                                                  ScmVersion scmVersion )
54          throws ScmException
55      {
56  
57          Git git = null;
58          try
59          {
60              git = JGitUtils.openRepo( fileSet.getBasedir() );
61              CredentialsProvider credentials =
62                  JGitUtils.prepareSession( getLogger(), git, (GitScmProviderRepository) repo );
63  
64              List<ScmFile> list = new ArrayList<ScmFile>();
65              Collection<Ref> lsResult = git.lsRemote().setCredentialsProvider( credentials )
66                      .setTransportConfigCallback(
67                              new JGitTransportConfigCallback( (GitScmProviderRepository) repo, getLogger() ) )
68                      .call();
69              for ( Ref ref : lsResult )
70              {
71                  getLogger().debug( ref.getObjectId().getName() + "  " + ref.getTarget().getName() );
72                  list.add( new ScmFile( ref.getName(), ScmFileStatus.CHECKED_IN ) );
73              }
74  
75              return new ListScmResult( "JGit ls-remote", list );
76          }
77          catch ( Exception e )
78          {
79              throw new ScmException( "JGit ls-remote failure!", e );
80          }
81          finally
82          {
83              JGitUtils.closeRepo( git );
84          }
85      }
86  }