001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.gitexe.command.remove;
020
021import java.io.File;
022import java.net.URI;
023import java.util.List;
024
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
029import org.apache.maven.scm.command.remove.RemoveScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.git.command.GitCommand;
032import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
033import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
034import org.codehaus.plexus.util.cli.CommandLineUtils;
035import org.codehaus.plexus.util.cli.Commandline;
036
037/**
038 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
039 * @author Olivier Lamy
040 */
041public class GitRemoveCommand extends AbstractRemoveCommand implements GitCommand {
042    /**
043     * {@inheritDoc}
044     */
045    protected ScmResult executeRemoveCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message)
046            throws ScmException {
047
048        if (fileSet.getFileList().isEmpty()) {
049            throw new ScmException("You must provide at least one file/directory to remove");
050        }
051
052        Commandline cl = createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
053
054        // git-rm uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath
055        URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD(logger, fileSet);
056        GitRemoveConsumer consumer = new GitRemoveConsumer(relativeRepositoryPath);
057
058        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
059
060        int exitCode;
061
062        exitCode = GitCommandLineUtils.execute(cl, consumer, stderr);
063        if (exitCode != 0) {
064            return new RemoveScmResult(cl.toString(), "The git command failed.", stderr.getOutput(), false);
065        }
066
067        return new RemoveScmResult(cl.toString(), consumer.getRemovedFiles());
068    }
069
070    public static Commandline createCommandLine(File workingDirectory, List<File> files) throws ScmException {
071        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "rm");
072
073        for (File file : files) {
074            if (file.isAbsolute()) {
075                if (file.isDirectory()) {
076                    cl.createArg().setValue("-r");
077                    break;
078                }
079            } else {
080                File absFile = new File(workingDirectory, file.getPath());
081                if (absFile.isDirectory()) {
082                    cl.createArg().setValue("-r");
083                    break;
084                }
085            }
086        }
087
088        // use this separator to make clear that the following parameters are files and options.
089        cl.createArg().setValue("--");
090
091        GitCommandLineUtils.addTarget(cl, files);
092
093        return cl;
094    }
095}