001package org.apache.maven.scm.provider.git.jgit.command.list;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFile;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.ScmFileStatus;
026import org.apache.maven.scm.ScmVersion;
027import org.apache.maven.scm.command.list.AbstractListCommand;
028import org.apache.maven.scm.command.list.ListScmResult;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.git.command.GitCommand;
031import org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback;
032import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
033import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
034import org.eclipse.jgit.api.Git;
035import org.eclipse.jgit.lib.Ref;
036import org.eclipse.jgit.transport.CredentialsProvider;
037
038import java.util.ArrayList;
039import java.util.Collection;
040import java.util.List;
041
042/**
043 * @author Dominik Bartholdi (imod)
044 * @since 1.9
045 */
046public class JGitListCommand
047    extends AbstractListCommand
048    implements GitCommand
049{
050
051    @Override
052    protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
053                                                ScmVersion scmVersion )
054        throws ScmException
055    {
056
057        Git git = null;
058        try
059        {
060            git = JGitUtils.openRepo( fileSet.getBasedir() );
061            CredentialsProvider credentials =
062                JGitUtils.prepareSession( getLogger(), git, (GitScmProviderRepository) repo );
063
064            List<ScmFile> list = new ArrayList<ScmFile>();
065            Collection<Ref> lsResult = git.lsRemote().setCredentialsProvider( credentials )
066                    .setTransportConfigCallback(
067                            new JGitTransportConfigCallback( (GitScmProviderRepository) repo, getLogger() ) )
068                    .call();
069            for ( Ref ref : lsResult )
070            {
071                getLogger().debug( ref.getObjectId().getName() + "  " + ref.getTarget().getName() );
072                list.add( new ScmFile( ref.getName(), ScmFileStatus.CHECKED_IN ) );
073            }
074
075            return new ListScmResult( "JGit ls-remote", list );
076        }
077        catch ( Exception e )
078        {
079            throw new ScmException( "JGit ls-remote failure!", e );
080        }
081        finally
082        {
083            JGitUtils.closeRepo( git );
084        }
085    }
086}