001package org.apache.maven.scm.provider.git.gitexe.command.status;
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 java.io.File;
023import java.net.URI;
024
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.command.status.AbstractStatusCommand;
028import org.apache.maven.scm.command.status.StatusScmResult;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.git.command.GitCommand;
031import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
032import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
033import org.codehaus.plexus.util.cli.CommandLineUtils;
034import org.codehaus.plexus.util.cli.Commandline;
035
036/**
037 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
038 *
039 */
040public class GitStatusCommand
041    extends AbstractStatusCommand
042    implements GitCommand
043{
044    /** {@inheritDoc} */
045    protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet fileSet )
046        throws ScmException
047    {
048        Commandline clRevparse = createRevparseShowToplevelCommand(fileSet);
049        
050        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
051        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
052
053        URI relativeRepositoryPath = null;
054        
055        int exitCode;
056
057        exitCode = GitCommandLineUtils.execute( clRevparse, stdout, stderr, getLogger() );
058        if ( exitCode != 0 )
059        {
060            // git-status returns non-zero if nothing to do
061            if ( getLogger().isInfoEnabled() )
062            {
063                getLogger().info( "Could not resolve toplevel" );
064            }
065        }
066        else
067        {
068            relativeRepositoryPath = GitStatusConsumer.resolveURI( stdout.getOutput().trim(), fileSet.getBasedir().toURI() ); 
069        }
070        
071        Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet );
072
073        GitStatusConsumer consumer = new GitStatusConsumer( getLogger(), fileSet.getBasedir(), relativeRepositoryPath );
074
075        stderr = new CommandLineUtils.StringStreamConsumer();
076
077        exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
078        if ( exitCode != 0 )
079        {
080            // git-status returns non-zero if nothing to do
081            if ( getLogger().isInfoEnabled() )
082            {
083                getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to track)" );
084            }
085        }
086
087        return new StatusScmResult( cl.toString(), consumer.getChangedFiles() );
088    }
089
090    // ----------------------------------------------------------------------
091    //
092    // ----------------------------------------------------------------------
093
094    public static Commandline createCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet )
095    {
096        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "status" );
097        cl.addArguments( new String[] { "--porcelain", "." } );
098        return cl;
099    }
100    
101    public static Commandline createRevparseShowToplevelCommand( ScmFileSet fileSet )
102    {
103        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "rev-parse" );
104        cl.addArguments( new String[] { "--show-toplevel" } );
105        return cl;
106    }
107}