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  public class GitRemoveCommand extends AbstractRemoveCommand implements GitCommand {
42      /**
43       * {@inheritDoc}
44       */
45      protected ScmResult executeRemoveCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message)
46              throws ScmException {
47  
48          if (fileSet.getFileList().isEmpty()) {
49              throw new ScmException("You must provide at least one file/directory to remove");
50          }
51  
52          Commandline cl = createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
53  
54          // git-rm uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath
55          URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD(logger, fileSet);
56          GitRemoveConsumer consumer = new GitRemoveConsumer(relativeRepositoryPath);
57  
58          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
59  
60          int exitCode;
61  
62          exitCode = GitCommandLineUtils.execute(cl, consumer, stderr);
63          if (exitCode != 0) {
64              return new RemoveScmResult(cl.toString(), "The git command failed.", stderr.getOutput(), false);
65          }
66  
67          return new RemoveScmResult(cl.toString(), consumer.getRemovedFiles());
68      }
69  
70      public static Commandline createCommandLine(File workingDirectory, List<File> files) throws ScmException {
71          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "rm");
72  
73          for (File file : files) {
74              if (file.isAbsolute()) {
75                  if (file.isDirectory()) {
76                      cl.createArg().setValue("-r");
77                      break;
78                  }
79              } else {
80                  File absFile = new File(workingDirectory, file.getPath());
81                  if (absFile.isDirectory()) {
82                      cl.createArg().setValue("-r");
83                      break;
84                  }
85              }
86          }
87  
88          // use this separator to make clear that the following parameters are files and options.
89          cl.createArg().setValue("--");
90  
91          GitCommandLineUtils.addTarget(cl, files);
92  
93          return cl;
94      }
95  }