001    package org.apache.maven.scm.provider.git.gitexe.command.remove;
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 org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmResult;
025    import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
026    import org.apache.maven.scm.command.remove.RemoveScmResult;
027    import org.apache.maven.scm.provider.ScmProviderRepository;
028    import org.apache.maven.scm.provider.git.command.GitCommand;
029    import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
030    import org.codehaus.plexus.util.cli.CommandLineUtils;
031    import org.codehaus.plexus.util.cli.Commandline;
032    
033    import java.io.File;
034    import java.util.List;
035    
036    /**
037     * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
038     * @author Olivier Lamy
039     * @version $Id: GitRemoveCommand.java 1379644 2012-08-31 22:23:39Z olamy $
040     */
041    public class GitRemoveCommand
042        extends AbstractRemoveCommand
043        implements GitCommand
044    {
045        /**
046         * {@inheritDoc}
047         */
048        protected ScmResult executeRemoveCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message )
049            throws ScmException
050        {
051    
052            if ( fileSet.getFileList().isEmpty() )
053            {
054                throw new ScmException( "You must provide at least one file/directory to remove" );
055            }
056    
057            Commandline cl = createCommandLine( fileSet.getBasedir(), fileSet.getFileList() );
058    
059            GitRemoveConsumer consumer = new GitRemoveConsumer( getLogger() );
060    
061            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
062    
063            int exitCode;
064    
065            exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
066            if ( exitCode != 0 )
067            {
068                return new RemoveScmResult( cl.toString(), "The git command failed.", stderr.getOutput(), false );
069            }
070    
071            return new RemoveScmResult( cl.toString(), consumer.getRemovedFiles() );
072        }
073    
074        public static Commandline createCommandLine( File workingDirectory, List<File> files )
075            throws ScmException
076        {
077            Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "rm" );
078    
079            for ( File file : files )
080            {
081                if ( file.isAbsolute() )
082                {
083                    if ( file.isDirectory() )
084                    {
085                        cl.createArg().setValue( "-r" );
086                        break;
087                    }
088                }
089                else
090                {
091                    File absFile = new File( workingDirectory, file.getPath() );
092                    if ( absFile.isDirectory() )
093                    {
094                        cl.createArg().setValue( "-r" );
095                        break;
096                    }
097                }
098            }
099    
100            GitCommandLineUtils.addTarget( cl, files );
101    
102            return cl;
103        }
104    
105    }