1 package org.apache.maven.scm.provider.git.gitexe.command.remove;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41
42
43 public class GitRemoveCommand
44 extends AbstractRemoveCommand
45 implements GitCommand
46 {
47
48
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
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
105 cl.createArg().setValue( "--" );
106
107 GitCommandLineUtils.addTarget( cl, files );
108
109 return cl;
110 }
111
112 }