001package org.apache.maven.scm.provider.git.gitexe.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.ScmFileSet;
024import org.apache.maven.scm.ScmFileStatus;
025import org.apache.maven.scm.ScmVersion;
026import org.apache.maven.scm.command.list.AbstractListCommand;
027import org.apache.maven.scm.command.list.ListScmResult;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.git.command.GitCommand;
030import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
031import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.File;
036
037/**
038 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
039 *
040 */
041public class GitListCommand
042    extends AbstractListCommand
043    implements GitCommand
044{
045    /** {@inheritDoc} */
046    protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
047                                                ScmVersion scmVersion )
048        throws ScmException
049    {
050        GitScmProviderRepository repository = (GitScmProviderRepository) repo;
051
052        if ( GitScmProviderRepository.PROTOCOL_FILE.equals( repository.getFetchInfo().getProtocol() )
053            && repository.getFetchInfo().getPath().indexOf( fileSet.getBasedir().getPath() ) >= 0 )
054        {
055            throw new ScmException( "remote repository must not be the working directory" );
056        }
057
058        int exitCode;
059
060        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
061        GitListConsumer consumer = new GitListConsumer( getLogger(), fileSet.getBasedir().getParentFile(),
062                                                        ScmFileStatus.CHECKED_IN );
063
064        Commandline cl = createCommandLine( repository, fileSet.getBasedir() );
065
066        exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
067        if ( exitCode != 0 )
068        {
069            return new ListScmResult( cl.toString(), "The git-ls-files command failed.", stderr.getOutput(), false );
070        }
071
072        return new ListScmResult( cl.toString(), consumer.getListedFiles() );
073    }
074
075    // ----------------------------------------------------------------------
076    //
077    // ----------------------------------------------------------------------
078
079    public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory )
080    {
081        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "ls-files" );
082
083        return cl;
084    }
085
086}