1 package org.apache.maven.scm.provider.svn.svnexe.command.branch;
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 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.maven.scm.ScmBranch;
28 import org.apache.maven.scm.ScmBranchParameters;
29 import org.apache.maven.scm.ScmException;
30 import org.apache.maven.scm.ScmFile;
31 import org.apache.maven.scm.ScmFileSet;
32 import org.apache.maven.scm.ScmFileStatus;
33 import org.apache.maven.scm.ScmResult;
34 import org.apache.maven.scm.command.branch.AbstractBranchCommand;
35 import org.apache.maven.scm.command.branch.BranchScmResult;
36 import org.apache.maven.scm.provider.ScmProviderRepository;
37 import org.apache.maven.scm.provider.svn.SvnCommandUtils;
38 import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
39 import org.apache.maven.scm.provider.svn.command.SvnCommand;
40 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
41 import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
42 import org.codehaus.plexus.util.FileUtils;
43 import org.codehaus.plexus.util.StringUtils;
44 import org.codehaus.plexus.util.cli.CommandLineException;
45 import org.codehaus.plexus.util.cli.CommandLineUtils;
46 import org.codehaus.plexus.util.cli.Commandline;
47
48
49
50
51
52
53
54 public class SvnBranchCommand
55 extends AbstractBranchCommand
56 implements SvnCommand
57 {
58
59 public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch,
60 ScmBranchParameters scmBranchParameters )
61 throws ScmException
62 {
63 if ( branch == null || StringUtils.isEmpty( branch.trim() ) )
64 {
65 throw new ScmException( "branch name must be specified" );
66 }
67
68 if ( !fileSet.getFileList().isEmpty() )
69 {
70 throw new ScmException( "This provider doesn't support branching subsets of a directory" );
71 }
72
73 SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
74
75 File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
76
77 try
78 {
79 FileUtils.fileWrite( messageFile.getAbsolutePath(), scmBranchParameters.getMessage() );
80 }
81 catch ( IOException ex )
82 {
83 return new BranchScmResult( null, "Error while making a temporary file for the commit message: "
84 + ex.getMessage(), null, false );
85 }
86
87 Commandline cl = createCommandLine( repository, fileSet.getBasedir(), branch, messageFile,
88 scmBranchParameters );
89
90 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
91
92 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
93
94 if ( getLogger().isInfoEnabled() )
95 {
96 getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
97 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
98 }
99
100 int exitCode;
101
102 try
103 {
104 exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
105 }
106 catch ( CommandLineException ex )
107 {
108 throw new ScmException( "Error while executing command.", ex );
109 }
110 finally
111 {
112 try
113 {
114 FileUtils.forceDelete( messageFile );
115 }
116 catch ( IOException ex )
117 {
118
119 }
120 }
121
122 if ( exitCode != 0 )
123 {
124 return new BranchScmResult( cl.toString(), "The svn branch command failed.", stderr.getOutput(), false );
125 }
126
127 List<ScmFile> fileList = new ArrayList<ScmFile>();
128
129 List<File> files = null;
130
131 try
132 {
133 @SuppressWarnings( "unchecked" )
134 List<File> listFiles = FileUtils.getFiles( fileSet.getBasedir(), "**", "**/.svn/**", false );
135 files = listFiles;
136 }
137 catch ( IOException e )
138 {
139 throw new ScmException( "Error while executing command.", e );
140 }
141
142 for ( File f : files )
143 {
144 fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
145 }
146
147 return new BranchScmResult( cl.toString(), fileList );
148 }
149
150
151 public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch,
152 String message )
153 throws ScmException
154 {
155 ScmBranchParameters scmBranchParameters = new ScmBranchParameters( message );
156 return executeBranchCommand( repo, fileSet, branch, scmBranchParameters );
157 }
158
159
160
161
162
163 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
164 String branch, File messageFile )
165 {
166 ScmBranchParameters scmBranchParameters = new ScmBranchParameters();
167 scmBranchParameters.setRemoteBranching( false );
168 return createCommandLine( repository, workingDirectory, branch, messageFile, scmBranchParameters );
169 }
170
171 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
172 String branch, File messageFile,
173 ScmBranchParameters scmBranchParameters )
174 {
175 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
176
177 cl.createArg().setValue( "copy" );
178
179 cl.createArg().setValue( "--file" );
180
181 cl.createArg().setValue( messageFile.getAbsolutePath() );
182
183 if ( scmBranchParameters != null && scmBranchParameters.isRemoteBranching() )
184 {
185 if ( StringUtils.isNotBlank( scmBranchParameters.getScmRevision() ) )
186 {
187 cl.createArg().setValue( "--revision" );
188 cl.createArg().setValue( scmBranchParameters.getScmRevision() );
189 }
190 cl.createArg().setValue( repository.getUrl() );
191 }
192 else
193 {
194 cl.createArg().setValue( "." );
195 }
196
197 String branchUrl = SvnTagBranchUtils.resolveBranchUrl( repository, new ScmBranch( branch ) );
198 cl.createArg().setValue( SvnCommandUtils.fixUrl( branchUrl, repository.getUser() ) );
199
200 return cl;
201 }
202 }