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.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
31  import org.codehaus.plexus.util.cli.CommandLineUtils;
32  import org.codehaus.plexus.util.cli.Commandline;
33  
34  import java.io.File;
35  import java.net.URI;
36  import java.util.List;
37  
38  /**
39   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
40   * @author Olivier Lamy
41   *
42   */
43  public class GitRemoveCommand
44      extends AbstractRemoveCommand
45      implements GitCommand
46  {
47      /**
48       * {@inheritDoc}
49       */
50      protected ScmResult executeRemoveCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message )
51          throws ScmException
52      {
53  
54          if ( fileSet.getFileList().isEmpty() )
55          {
56              throw new ScmException( "You must provide at least one file/directory to remove" );
57          }
58  
59          Commandline cl = createCommandLine( fileSet.getBasedir(), fileSet.getFileList() );
60  
61          // git-rm uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath
62          URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD( logger, fileSet );
63          GitRemoveConsumer consumer = new GitRemoveConsumer( relativeRepositoryPath );
64  
65          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
66  
67          int exitCode;
68  
69          exitCode = GitCommandLineUtils.execute( cl, consumer, stderr );
70          if ( exitCode != 0 )
71          {
72              return new RemoveScmResult( cl.toString(), "The git command failed.", stderr.getOutput(), false );
73          }
74  
75          return new RemoveScmResult( cl.toString(), consumer.getRemovedFiles() );
76      }
77  
78      public static Commandline createCommandLine( File workingDirectory, List<File> files )
79          throws ScmException
80      {
81          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "rm" );
82  
83          for ( File file : files )
84          {
85              if ( file.isAbsolute() )
86              {
87                  if ( file.isDirectory() )
88                  {
89                      cl.createArg().setValue( "-r" );
90                      break;
91                  }
92              }
93              else
94              {
95                  File absFile = new File( workingDirectory, file.getPath() );
96                  if ( absFile.isDirectory() )
97                  {
98                      cl.createArg().setValue( "-r" );
99                      break;
100                 }
101             }
102         }
103 
104         // use this separator to make clear that the following parameters are files and options.
105         cl.createArg().setValue( "--" );
106 
107         GitCommandLineUtils.addTarget( cl, files );
108 
109         return cl;
110     }
111 
112 }