View Javadoc

1   /*
2   The MIT License
3   
4   Copyright (c) 2004, The Codehaus
5   
6   Permission is hereby granted, free of charge, to any person obtaining a copy of
7   this software and associated documentation files (the "Software"), to deal in
8   the Software without restriction, including without limitation the rights to
9   use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  of the Software, and to permit persons to whom the Software is furnished to do
11  so, subject to the following conditions:
12  
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15  
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23   */
24  package org.codehaus.mojo.scmchangelog.scm.svn;
25  
26  import java.io.File;
27  import java.text.SimpleDateFormat;
28  import java.util.Date;
29  import java.util.TimeZone;
30  
31  import org.apache.maven.scm.ScmBranch;
32  import org.apache.maven.scm.ScmException;
33  import org.apache.maven.scm.ScmFileSet;
34  import org.apache.maven.scm.ScmTag;
35  import org.apache.maven.scm.ScmVersion;
36  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
37  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
38  import org.apache.maven.scm.command.changelog.ChangeLogSet;
39  import org.apache.maven.scm.provider.ScmProviderRepository;
40  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
41  import org.apache.maven.scm.provider.svn.command.SvnCommand;
42  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
43  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
44  import org.codehaus.mojo.scmchangelog.changelog.log.grammar.GrammarEnum;
45  import org.codehaus.plexus.util.StringUtils;
46  import org.codehaus.plexus.util.cli.CommandLineException;
47  import org.codehaus.plexus.util.cli.CommandLineUtils;
48  import org.codehaus.plexus.util.cli.Commandline;
49  
50  /**
51   * Command to get log entries from the SVN ( <code>svn log --xml</code> command, then consume it with our parser.
52   * @author ehsavoie
53   * @version $Id: SvnChangeLogCommand.java 9275 2009-03-24 20:40:58Z ehsavoie $
54   *
55   * @see org.apache.maven.scm.provider.svn.svnexe.command.SvnChangeLogCommand
56   * @see org.codehaus.mojo.scmchangelog.changelog.SvnChangeLogConsumer
57   */
58  public class SvnChangeLogCommand
59      extends AbstractChangeLogCommand
60      implements SvnCommand
61  {
62  
63    /**
64     * Date format to be used when using a date with the command.
65     */
66    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
67    /**
68     * The grammar to be used to parse the comments.
69     */
70    private GrammarEnum grammar;
71  
72    /**
73     * Creates a new instance of SvnChangeLogCommand.
74     * @param commentGrammar the grammar to be used toparse the comments.
75     */
76    public SvnChangeLogCommand( GrammarEnum commentGrammar )
77    {
78      this.grammar = commentGrammar;
79    }
80  
81    /**
82     * Execute the command.
83     * @param repo the repository.
84     * @param fileSet the list of files.
85     * @param startVersion starting revision for the command.
86     * @param endVersion end revision for the comand.
87     * @param datePattern datePattern for formatting dates.
88     * @return the result of the command.
89     * @throws org.apache.maven.scm.ScmException in case of an error with the scm command.
90     */
91    protected ChangeLogScmResult executeChangeLogCommand(
92        ScmProviderRepository repo, ScmFileSet fileSet,
93        ScmVersion startVersion, ScmVersion endVersion, String datePattern )
94        throws ScmException
95    {
96      return executeChangeLogCommand( repo, fileSet, null, datePattern, startVersion, endVersion );
97    }
98  
99    /**
100    * Execute the command.
101    * @param repo the repository.
102    * @param fileSet the list of files.
103    * @param startDate starting date of the revision for the command.
104    * @param endDate end date of the revision for the comand.
105    * @param branch the selected branch/tag.
106    * @param datePattern datePattern for formatting dates.
107    * @return the result of the command.
108    * @throws org.apache.maven.scm.ScmException in case of an error with the scm command.
109    */
110   protected ChangeLogScmResult executeChangeLogCommand(
111       ScmProviderRepository repo, ScmFileSet fileSet, Date startDate,
112       Date endDate, ScmBranch branch, String datePattern )
113       throws ScmException
114   {
115     return executeChangeLogCommand( repo, fileSet, branch, datePattern, null, null );
116   }
117 
118   /**
119    * Execute the command.
120    * @param repo the repository.
121    * @param fileSet the list of files.
122    * @param branch the selected branch/tag.
123    * @param datePattern datePattern for formatting dates.
124    * @param startVersion starting revision for the command.
125    * @param endVersion end revision for the comand.
126    * @return the result of the command.
127    * @throws org.apache.maven.scm.ScmException in case of an error with the scm command.
128    */
129   protected ChangeLogScmResult executeChangeLogCommand(
130       ScmProviderRepository repo, ScmFileSet fileSet, ScmBranch branch,
131       String datePattern, ScmVersion startVersion, ScmVersion endVersion )
132       throws ScmException
133   {
134     Commandline cl = createCommandLine( ( SvnScmProviderRepository ) repo,
135         fileSet.getBasedir(), branch, startVersion, endVersion );
136     SvnChangeLogConsumer consumer = new SvnChangeLogConsumer( grammar );
137     consumer.setLogger( this.getLogger() );
138     CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
139     getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
140     getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
141     int exitCode;
142     try
143     {
144       exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
145     }
146     catch ( CommandLineException ex )
147     {
148       getLogger().error( "Error while executing command.", ex );
149       throw new ScmException( "Error while executing svn command.", ex );
150     }
151 
152     if ( exitCode != 0 )
153     {
154       getLogger().error( "The svn command failed." + stderr.getOutput() );
155       return new ChangeLogScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
156     }
157 
158     ChangeLogSet changeLogSet = new ChangeLogSet( consumer.analyse(), null, null );
159     changeLogSet.setStartVersion( startVersion );
160     changeLogSet.setEndVersion( endVersion );
161 
162     return new ChangeLogScmResult( cl.toString(), changeLogSet );
163   }
164 
165   /**
166    * Creates a command line for <code>svn log</code>.
167    * @param repository the subversion repository.
168    * @param workingDirectory the working directory.
169    * @param branch the branch to be used.
170    * @param startVersion starting revision for the command.
171    * @param endVersion end revision for the comand.
172    * @return the result of the command.
173    */
174   public static Commandline createCommandLine(
175       SvnScmProviderRepository repository, File workingDirectory,
176       ScmBranch branch, ScmVersion startVersion, ScmVersion endVersion )
177   {
178     SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
179     dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
180 
181     Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory,
182         repository );
183     cl.createArgument().setValue( "log" );
184     cl.createArgument().setValue( "--xml" );
185 
186     if ( startVersion != null )
187     {
188       cl.createArgument().setValue( "-r" );
189 
190       if ( endVersion != null )
191       {
192         if ( startVersion.getName().equals( endVersion.getName() ) )
193         {
194           cl.createArgument().setValue( startVersion.getName() );
195         } 
196         else
197         {
198           cl.createArgument().setValue( startVersion.getName()
199               + ':'
200               + endVersion.getName() );
201         }
202       } 
203       else
204       {
205         cl.createArgument().setValue( startVersion.getName() 
206             + ":HEAD" );
207       }
208     }
209 
210     if ( ( branch != null ) && StringUtils.isNotEmpty( branch.getName() ) )
211     {
212       // By specifying a branch and this repository url below, subversion should show
213       // the changelog of that branch, but limit it to paths that also occur in this repository.
214       if ( branch instanceof ScmTag )
215       {
216         cl.createArgument().setValue( SvnTagBranchUtils.resolveTagUrl( repository, ( ScmTag ) branch ) );
217       } 
218       else
219       {
220         cl.createArgument().setValue( SvnTagBranchUtils.resolveBranchUrl( repository, branch ) );
221       }
222     }
223     cl.createArgument().setValue( repository.getUrl() );
224 
225     return cl;
226   }
227 }