001package 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
022import org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmResult;
025import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
026import org.apache.maven.scm.command.remove.RemoveScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.git.command.GitCommand;
029import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
030import org.codehaus.plexus.util.cli.CommandLineUtils;
031import org.codehaus.plexus.util.cli.Commandline;
032
033import java.io.File;
034import java.util.List;
035
036/**
037 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
038 * @author Olivier Lamy
039 *
040 */
041public 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}