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.net.URI;
023
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.command.status.AbstractStatusCommand;
027import org.apache.maven.scm.command.status.StatusScmResult;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.git.command.GitCommand;
030import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
031import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035/**
036 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
037 *
038 */
039public class GitStatusCommand
040    extends AbstractStatusCommand
041    implements GitCommand
042{
043    /** {@inheritDoc} */
044    protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet fileSet )
045        throws ScmException
046    {
047        Commandline clRevparse = createRevparseShowToplevelCommand( fileSet );
048
049        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
050        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
051
052        URI relativeRepositoryPath = null;
053        
054        int exitCode;
055
056        exitCode = GitCommandLineUtils.execute( clRevparse, stdout, stderr, getLogger() );
057        if ( exitCode != 0 )
058        {
059            // git-status returns non-zero if nothing to do
060            if ( getLogger().isInfoEnabled() )
061            {
062                getLogger().info( "Could not resolve toplevel" );
063            }
064        }
065        else
066        {
067            relativeRepositoryPath =
068                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}