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