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.svn.svnexe.command.remove;
20  
21  import java.io.File;
22  import java.io.IOException;
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.svn.command.SvnCommand;
32  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
33  import org.codehaus.plexus.util.Os;
34  import org.codehaus.plexus.util.cli.CommandLineException;
35  import org.codehaus.plexus.util.cli.CommandLineUtils;
36  import org.codehaus.plexus.util.cli.Commandline;
37  
38  /**
39   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40   * @author Olivier Lamy
41   */
42  public class SvnRemoveCommand extends AbstractRemoveCommand implements SvnCommand {
43      /**
44       * {@inheritDoc}
45       */
46      protected ScmResult executeRemoveCommand(ScmProviderRepository repository, ScmFileSet fileSet, String message)
47              throws ScmException {
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          SvnRemoveConsumer consumer = new SvnRemoveConsumer();
55  
56          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
57  
58          if (logger.isInfoEnabled()) {
59              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
60  
61              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
62                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
63              }
64          }
65  
66          int exitCode;
67  
68          try {
69              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
70          } catch (CommandLineException ex) {
71              throw new ScmException("Error while executing command.", ex);
72          }
73  
74          if (exitCode != 0) {
75              return new RemoveScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
76          }
77  
78          return new RemoveScmResult(cl.toString(), consumer.getRemovedFiles());
79      }
80  
81      private static Commandline createCommandLine(File workingDirectory, List<File> files) throws ScmException {
82          // Base command line doesn't make sense here - username/password not needed, and non-interactive/non-recusive is
83          // not valid
84  
85          Commandline cl = new Commandline();
86  
87          cl.setExecutable("svn");
88  
89          cl.setWorkingDirectory(workingDirectory.getAbsolutePath());
90  
91          cl.createArg().setValue("remove");
92  
93          try {
94              SvnCommandLineUtils.addTarget(cl, files);
95          } catch (IOException e) {
96              throw new ScmException("Can't create the targets file", e);
97          }
98  
99          return cl;
100     }
101 }