View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.branch;
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 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.Os;
44  import org.codehaus.plexus.util.StringUtils;
45  import org.codehaus.plexus.util.cli.CommandLineException;
46  import org.codehaus.plexus.util.cli.CommandLineUtils;
47  import org.codehaus.plexus.util.cli.Commandline;
48  
49  /**
50   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
51   * @author Olivier Lamy
52   *
53   * @todo since this is just a copy, use that instead.
54   */
55  public class SvnBranchCommand
56      extends AbstractBranchCommand
57      implements SvnCommand
58  {
59  
60      public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch,
61                                             ScmBranchParameters scmBranchParameters )
62      throws ScmException
63      {
64          if ( branch == null || StringUtils.isEmpty( branch.trim() ) )
65          {
66              throw new ScmException( "branch name must be specified" );
67          }
68  
69          if ( !fileSet.getFileList().isEmpty() )
70          {
71              throw new ScmException( "This provider doesn't support branching subsets of a directory" );
72          }
73  
74          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
75  
76          File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
77  
78          try
79          {
80              FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", scmBranchParameters.getMessage() );
81          }
82          catch ( IOException ex )
83          {
84              return new BranchScmResult( null, "Error while making a temporary file for the commit message: "
85                  + ex.getMessage(), null, false );
86          }
87  
88          Commandline cl = createCommandLine( repository, fileSet.getBasedir(), branch, messageFile,
89                                              scmBranchParameters );
90  
91          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
92  
93          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
94  
95          if ( getLogger().isInfoEnabled() )
96          {
97              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
98  
99              if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
100             {
101                 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
102             }
103         }
104 
105         int exitCode;
106 
107         try
108         {
109             exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
110         }
111         catch ( CommandLineException ex )
112         {
113             throw new ScmException( "Error while executing command.", ex );
114         }
115         finally
116         {
117             try
118             {
119                 FileUtils.forceDelete( messageFile );
120             }
121             catch ( IOException ex )
122             {
123                 // ignore
124             }
125         }
126 
127         if ( exitCode != 0 )
128         {
129             return new BranchScmResult( cl.toString(), "The svn branch command failed.", stderr.getOutput(), false );
130         }
131 
132         List<ScmFile> fileList = new ArrayList<ScmFile>();
133 
134         List<File> files = null;
135 
136         try
137         {
138             List<File> listFiles = FileUtils.getFiles( fileSet.getBasedir(), "**", "**/.svn/**", false );
139             files = listFiles;
140         }
141         catch ( IOException e )
142         {
143             throw new ScmException( "Error while executing command.", e );
144         }
145 
146         for ( File f : files )
147         {
148             fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
149         }
150 
151         return new BranchScmResult( cl.toString(), fileList );
152     }
153 
154     /** {@inheritDoc} */
155     public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch,
156                                            String message )
157         throws ScmException
158     {
159         ScmBranchParameters scmBranchParameters = new ScmBranchParameters( message );
160         return executeBranchCommand( repo, fileSet, branch, scmBranchParameters );
161     }
162 
163     // ----------------------------------------------------------------------
164     //
165     // ----------------------------------------------------------------------
166 
167     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
168                                                  String branch, File messageFile )
169     {
170         ScmBranchParameters scmBranchParameters = new ScmBranchParameters();
171         scmBranchParameters.setRemoteBranching( false );
172         scmBranchParameters.setPinExternals( false );
173         return createCommandLine( repository, workingDirectory, branch, messageFile, scmBranchParameters );
174     }
175 
176     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
177                                                  String branch, File messageFile,
178                                                  ScmBranchParameters scmBranchParameters )
179     {
180         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
181 
182         cl.createArg().setValue( "copy" );
183 
184         cl.createArg().setValue( "--parents" );
185 
186         cl.createArg().setValue( "--file" );
187 
188         cl.createArg().setValue( messageFile.getAbsolutePath() );
189 
190         cl.createArg().setValue( "--encoding" );
191 
192         cl.createArg().setValue( "UTF-8" );
193 
194         if ( scmBranchParameters != null && scmBranchParameters.isPinExternals() )
195         {
196             cl.createArg().setValue( "--pin-externals" );
197         }
198 
199         if ( scmBranchParameters != null && scmBranchParameters.isRemoteBranching() )
200         {
201             if ( StringUtils.isNotBlank( scmBranchParameters.getScmRevision() ) )
202             {
203                 cl.createArg().setValue( "--revision" );
204                 cl.createArg().setValue( scmBranchParameters.getScmRevision() );
205             }
206             String url = SvnCommandUtils.fixUrl( repository.getUrl(), repository.getUser() );
207             cl.createArg().setValue( url + "@" );
208         }
209         else
210         {
211             cl.createArg().setValue( "." );
212         }
213         // Note: this currently assumes you have the branch base checked out too
214         String branchUrl = SvnTagBranchUtils.resolveBranchUrl( repository, new ScmBranch( branch ) );
215         branchUrl = SvnCommandUtils.fixUrl( branchUrl, repository.getUser() );
216         cl.createArg().setValue( branchUrl + "@" );
217 
218         return cl;
219     }
220 }