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 * 041 */ 042public class GitRemoveCommand extends AbstractRemoveCommand implements GitCommand { 043 /** 044 * {@inheritDoc} 045 */ 046 protected ScmResult executeRemoveCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message) 047 throws ScmException { 048 049 if (fileSet.getFileList().isEmpty()) { 050 throw new ScmException("You must provide at least one file/directory to remove"); 051 } 052 053 Commandline cl = createCommandLine(fileSet.getBasedir(), fileSet.getFileList()); 054 055 // git-rm uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath 056 URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD(logger, fileSet); 057 GitRemoveConsumer consumer = new GitRemoveConsumer(relativeRepositoryPath); 058 059 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 060 061 int exitCode; 062 063 exitCode = GitCommandLineUtils.execute(cl, consumer, stderr); 064 if (exitCode != 0) { 065 return new RemoveScmResult(cl.toString(), "The git command failed.", stderr.getOutput(), false); 066 } 067 068 return new RemoveScmResult(cl.toString(), consumer.getRemovedFiles()); 069 } 070 071 public static Commandline createCommandLine(File workingDirectory, List<File> files) throws ScmException { 072 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "rm"); 073 074 for (File file : files) { 075 if (file.isAbsolute()) { 076 if (file.isDirectory()) { 077 cl.createArg().setValue("-r"); 078 break; 079 } 080 } else { 081 File absFile = new File(workingDirectory, file.getPath()); 082 if (absFile.isDirectory()) { 083 cl.createArg().setValue("-r"); 084 break; 085 } 086 } 087 } 088 089 // use this separator to make clear that the following parameters are files and options. 090 cl.createArg().setValue("--"); 091 092 GitCommandLineUtils.addTarget(cl, files); 093 094 return cl; 095 } 096}