1 package org.apache.maven.scm.provider.svn.svnexe.command.export;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
24 import org.apache.maven.scm.ScmBranch;
25 import org.apache.maven.scm.ScmException;
26 import org.apache.maven.scm.ScmFileSet;
27 import org.apache.maven.scm.ScmRevision;
28 import org.apache.maven.scm.ScmTag;
29 import org.apache.maven.scm.ScmVersion;
30 import org.apache.maven.scm.command.export.AbstractExportCommand;
31 import org.apache.maven.scm.command.export.ExportScmResult;
32 import org.apache.maven.scm.command.export.ExportScmResultWithRevision;
33 import org.apache.maven.scm.provider.ScmProviderRepository;
34 import org.apache.maven.scm.provider.svn.SvnCommandUtils;
35 import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
36 import org.apache.maven.scm.provider.svn.command.SvnCommand;
37 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
38 import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
39 import org.apache.maven.scm.provider.svn.svnexe.command.update.SvnUpdateConsumer;
40 import org.codehaus.plexus.util.StringUtils;
41 import org.codehaus.plexus.util.cli.CommandLineException;
42 import org.codehaus.plexus.util.cli.CommandLineUtils;
43 import org.codehaus.plexus.util.cli.Commandline;
44
45
46
47
48
49 public class SvnExeExportCommand
50 extends AbstractExportCommand
51 implements SvnCommand
52 {
53
54 protected ExportScmResult executeExportCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version,
55 String outputDirectory )
56 throws ScmException
57 {
58
59 if ( outputDirectory == null )
60 {
61 outputDirectory = fileSet.getBasedir().getAbsolutePath();
62 }
63
64 SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
65
66 String url = repository.getUrl();
67
68 if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
69 {
70 if ( version instanceof ScmTag )
71 {
72 url = SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version );
73 }
74 else if ( version instanceof ScmBranch )
75 {
76 url = SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version );
77 }
78 }
79
80 url = SvnCommandUtils.fixUrl( url, repository.getUser() );
81
82 Commandline cl =
83 createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), version, url, outputDirectory );
84
85 SvnUpdateConsumer consumer = new SvnUpdateConsumer( getLogger(), fileSet.getBasedir() );
86
87 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
88
89 if ( getLogger().isInfoEnabled() )
90 {
91 getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
92 if ( cl.getWorkingDirectory() != null )
93 {
94 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
95 }
96 }
97
98 int exitCode;
99
100 try
101 {
102 exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
103 }
104 catch ( CommandLineException ex )
105 {
106 throw new ScmException( "Error while executing command.", ex );
107 }
108
109 if ( exitCode != 0 )
110 {
111 return new ExportScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
112 }
113
114 return new ExportScmResultWithRevision( cl.toString(), consumer.getUpdatedFiles(),
115 String.valueOf( consumer.getRevision() ) );
116 }
117
118
119
120
121
122 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, ScmVersion version, String url, String outputSirectory )
123 {
124 if ( version != null && StringUtils.isEmpty( version.getName() ) )
125 {
126 version = null;
127 }
128
129 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
130
131 cl.createArg().setValue( "export" );
132
133 if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
134 {
135 if ( version instanceof ScmRevision )
136 {
137 cl.createArg().setValue( "-r" );
138
139 cl.createArg().setValue( version.getName() );
140 }
141 }
142
143
144 cl.createArg().setValue( "--force" );
145
146 cl.createArg().setValue( url );
147
148 if ( StringUtils.isNotEmpty( outputSirectory ) )
149 {
150 cl.createArg().setValue( outputSirectory );
151 }
152
153 return cl;
154 }
155 }