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.JGitUtils;
032import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
033import org.eclipse.jgit.api.Git;
034import org.eclipse.jgit.lib.Ref;
035import org.eclipse.jgit.transport.CredentialsProvider;
036
037import java.util.ArrayList;
038import java.util.Collection;
039import java.util.List;
040
041/**
042 * @author Dominik Bartholdi (imod)
043 * @since 1.9
044 */
045public class JGitListCommand
046    extends AbstractListCommand
047    implements GitCommand
048{
049
050    @Override
051    protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
052                                                ScmVersion scmVersion )
053        throws ScmException
054    {
055
056        Git git = null;
057        try
058        {
059            git = JGitUtils.openRepo( fileSet.getBasedir() );
060            CredentialsProvider credentials =
061                JGitUtils.prepareSession( getLogger(), git, (GitScmProviderRepository) repo );
062
063            List<ScmFile> list = new ArrayList<ScmFile>();
064            Collection<Ref> lsResult = git.lsRemote().setCredentialsProvider( credentials ).call();
065            for ( Ref ref : lsResult )
066            {
067                getLogger().debug( ref.getObjectId().getName() + "  " + ref.getTarget().getName() );
068                list.add( new ScmFile( ref.getName(), ScmFileStatus.CHECKED_IN ) );
069            }
070
071            return new ListScmResult( "JGit ls-remote", list );
072        }
073        catch ( Exception e )
074        {
075            throw new ScmException( "JGit ls-remote failure!", e );
076        }
077        finally
078        {
079            JGitUtils.closeRepo( git );
080        }
081    }
082}