001    package org.apache.maven.scm.provider.git.gitexe.command.add;
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    
022    import java.io.File;
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.apache.maven.scm.ScmException;
027    import org.apache.maven.scm.ScmFile;
028    import org.apache.maven.scm.ScmFileSet;
029    import org.apache.maven.scm.ScmResult;
030    import org.apache.maven.scm.command.add.AbstractAddCommand;
031    import org.apache.maven.scm.command.add.AddScmResult;
032    import org.apache.maven.scm.provider.ScmProviderRepository;
033    import org.apache.maven.scm.provider.git.command.GitCommand;
034    import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
035    import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
036    import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer;
037    import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
038    import org.codehaus.plexus.util.cli.CommandLineUtils;
039    import org.codehaus.plexus.util.cli.Commandline;
040    
041    /**
042     * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
043     * @version $Id: GitAddCommand.java 1054126 2010-12-31 15:18:11Z olamy $
044     */
045    public class GitAddCommand
046        extends AbstractAddCommand
047        implements GitCommand
048    {
049        /** {@inheritDoc} */
050        protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
051                                               boolean binary )
052            throws ScmException
053        {
054            GitScmProviderRepository repository = (GitScmProviderRepository) repo;
055    
056            if ( fileSet.getFileList().isEmpty() )
057            {
058                throw new ScmException( "You must provide at least one file/directory to add" );
059            }
060    
061            Commandline cl = createCommandLine( fileSet.getBasedir(), fileSet.getFileList() );
062    
063            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
064            CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
065    
066            int exitCode;
067    
068            exitCode = GitCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
069            if ( exitCode != 0 )
070            {
071                return new AddScmResult( cl.toString(), "The git-add command failed.", stderr.getOutput(), false );
072            }
073    
074            // git-add doesn't show single files, but only summary :/
075            // so we must run git-status and consume the output
076            // borrow a few things from the git-status command
077            Commandline clStatus = GitStatusCommand.createCommandLine( repository, fileSet );
078    
079            GitStatusConsumer statusConsumer = new GitStatusConsumer( getLogger(), fileSet.getBasedir() );
080            exitCode = GitCommandLineUtils.execute( clStatus, statusConsumer, stderr, getLogger() );
081            if ( exitCode != 0 )
082            {
083                // git-status returns non-zero if nothing to do
084                if ( getLogger().isInfoEnabled() )
085                {
086                    getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to track)" );
087                }
088            }
089    
090            List<ScmFile> changedFiles = new ArrayList<ScmFile>();
091    
092            // rewrite all detected files to now have status 'checked_in'
093            for ( ScmFile scmfile : statusConsumer.getChangedFiles() )
094            {
095                // if a specific fileSet is given, we have to check if the file is really tracked
096                for ( File f : fileSet.getFileList() )
097                {
098                    if ( f.toString().equals( scmfile.getPath() ) )
099                    {
100                        changedFiles.add( scmfile );
101                    }
102                }
103            }
104            return new AddScmResult( cl.toString(), changedFiles );
105        }
106    
107        public static Commandline createCommandLine( File workingDirectory, List<File> files )
108            throws ScmException
109        {
110            Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "add" );
111    
112            // use this separator to make clear that the following parameters are files and not revision info.
113            cl.createArg().setValue( "--" );
114            
115            GitCommandLineUtils.addTarget( cl, files );
116    
117            return cl;
118        }
119    
120    }