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