View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.gitexe.command.remove;
20  
21  import java.io.File;
22  import java.net.URI;
23  import java.util.List;
24  
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmResult;
28  import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
29  import org.apache.maven.scm.command.remove.RemoveScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.git.command.GitCommand;
32  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
33  import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  /**
38   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
39   * @author Olivier Lamy
40   *
41   */
42  public class GitRemoveCommand extends AbstractRemoveCommand implements GitCommand {
43      /**
44       * {@inheritDoc}
45       */
46      protected ScmResult executeRemoveCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message)
47              throws ScmException {
48  
49          if (fileSet.getFileList().isEmpty()) {
50              throw new ScmException("You must provide at least one file/directory to remove");
51          }
52  
53          Commandline cl = createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
54  
55          // git-rm uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath
56          URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD(logger, fileSet);
57          GitRemoveConsumer consumer = new GitRemoveConsumer(relativeRepositoryPath);
58  
59          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
60  
61          int exitCode;
62  
63          exitCode = GitCommandLineUtils.execute(cl, consumer, stderr);
64          if (exitCode != 0) {
65              return new RemoveScmResult(cl.toString(), "The git command failed.", stderr.getOutput(), false);
66          }
67  
68          return new RemoveScmResult(cl.toString(), consumer.getRemovedFiles());
69      }
70  
71      public static Commandline createCommandLine(File workingDirectory, List<File> files) throws ScmException {
72          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "rm");
73  
74          for (File file : files) {
75              if (file.isAbsolute()) {
76                  if (file.isDirectory()) {
77                      cl.createArg().setValue("-r");
78                      break;
79                  }
80              } else {
81                  File absFile = new File(workingDirectory, file.getPath());
82                  if (absFile.isDirectory()) {
83                      cl.createArg().setValue("-r");
84                      break;
85                  }
86              }
87          }
88  
89          // use this separator to make clear that the following parameters are files and options.
90          cl.createArg().setValue("--");
91  
92          GitCommandLineUtils.addTarget(cl, files);
93  
94          return cl;
95      }
96  }