View Javadoc

1   package org.apache.maven.scm.provider.svn.svnexe.command.tag;
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 org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmFileStatus;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.ScmTag;
28  import org.apache.maven.scm.command.tag.AbstractTagCommand;
29  import org.apache.maven.scm.command.tag.TagScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.svn.SvnCommandUtils;
32  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
33  import org.apache.maven.scm.provider.svn.command.SvnCommand;
34  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
35  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
36  import org.codehaus.plexus.util.FileUtils;
37  import org.codehaus.plexus.util.StringUtils;
38  import org.codehaus.plexus.util.cli.CommandLineException;
39  import org.codehaus.plexus.util.cli.CommandLineUtils;
40  import org.codehaus.plexus.util.cli.Commandline;
41  
42  import java.io.File;
43  import java.io.IOException;
44  import java.util.ArrayList;
45  import java.util.Iterator;
46  import java.util.List;
47  
48  /**
49   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
50   * @version $Id: SvnTagCommand.java 531990 2007-04-24 15:55:06Z evenisse $
51   * @todo since this is just a copy, use that instead.
52   */
53  public class SvnTagCommand
54      extends AbstractTagCommand
55      implements SvnCommand
56  {
57      public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
58          throws ScmException
59      {
60          if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
61          {
62              throw new ScmException( "tag must be specified" );
63          }
64  
65          if ( fileSet.getFiles().length != 0 )
66          {
67              throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
68          }
69  
70          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
71  
72          File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
73  
74          try
75          {
76              FileUtils.fileWrite( messageFile.getAbsolutePath(), message );
77          }
78          catch ( IOException ex )
79          {
80              return new TagScmResult( null,
81                                       "Error while making a temporary file for the commit message: " + ex.getMessage(),
82                                       null, false );
83          }
84  
85          Commandline cl = createCommandLine( repository, fileSet.getBasedir(), tag, messageFile );
86  
87          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
88  
89          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
90  
91          getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
92          getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
93  
94          int exitCode;
95  
96          try
97          {
98              exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
99          }
100         catch ( CommandLineException ex )
101         {
102             throw new ScmException( "Error while executing command.", ex );
103         }
104         finally
105         {
106             try
107             {
108                 FileUtils.forceDelete( messageFile );
109             }
110             catch ( IOException ex )
111             {
112                 // ignore
113             }
114         }
115 
116         if ( exitCode != 0 )
117         {
118             // TODO: Improve this error message
119             return new TagScmResult( cl.toString(), "The svn tag command failed.", stderr.getOutput(), false );
120         }
121 
122         List fileList = new ArrayList();
123 
124         List files = null;
125 
126         try
127         {
128             files = FileUtils.getFiles( fileSet.getBasedir(), "**", "**/.svn/**", false );
129         }
130         catch ( IOException e )
131         {
132             throw new ScmException( "Error while executing command.", e );
133         }
134 
135         for ( Iterator i = files.iterator(); i.hasNext(); )
136         {
137             File f = (File) i.next();
138 
139             fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
140         }
141 
142         return new TagScmResult( cl.toString(), fileList );
143     }
144 
145     // ----------------------------------------------------------------------
146     //
147     // ----------------------------------------------------------------------
148 
149     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, String tag,
150                                                  File messageFile )
151     {
152         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
153 
154         cl.createArgument().setValue( "copy" );
155 
156         cl.createArgument().setValue( "--file" );
157 
158         cl.createArgument().setValue( messageFile.getAbsolutePath() );
159 
160         cl.createArgument().setValue( "." );
161 
162         // Note: this currently assumes you have the tag base checked out too
163         String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
164         cl.createArgument().setValue( SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ) );
165 
166         return cl;
167     }
168 }