001package org.apache.maven.scm.provider.svn.svnexe.command.changelog;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmBranch;
023import org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.ScmTag;
026import org.apache.maven.scm.ScmVersion;
027import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
028import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
029import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
030import org.apache.maven.scm.command.changelog.ChangeLogSet;
031import org.apache.maven.scm.provider.ScmProviderRepository;
032import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
033import org.apache.maven.scm.provider.svn.command.SvnCommand;
034import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
035import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
036import org.codehaus.plexus.util.Os;
037import org.codehaus.plexus.util.StringUtils;
038import org.codehaus.plexus.util.cli.CommandLineException;
039import org.codehaus.plexus.util.cli.CommandLineUtils;
040import org.codehaus.plexus.util.cli.Commandline;
041
042import java.io.File;
043import java.text.SimpleDateFormat;
044import java.util.Date;
045import java.util.TimeZone;
046
047/**
048 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
049 * @author Olivier Lamy
050 *
051 */
052public class SvnChangeLogCommand
053    extends AbstractChangeLogCommand
054    implements SvnCommand
055{
056    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
057
058    /** {@inheritDoc} */
059    @Deprecated
060    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
061                                                          ScmVersion startVersion, ScmVersion endVersion,
062                                                          String datePattern )
063        throws ScmException
064    {
065        return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion, null );
066    }
067
068    /** {@inheritDoc} */
069    @Deprecated
070    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
071                                                          Date startDate, Date endDate, ScmBranch branch,
072                                                          String datePattern )
073        throws ScmException
074    {
075        return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null, null );
076    }
077
078    @Override
079    protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
080        throws ScmException
081    {
082        final ScmVersion startVersion = request.getStartRevision();
083        final ScmVersion endVersion = request.getEndRevision();
084        final ScmFileSet fileSet = request.getScmFileSet();
085        final String datePattern = request.getDatePattern();
086        return executeChangeLogCommand( request.getScmRepository().getProviderRepository(), fileSet,
087            request.getStartDate(), request.getEndDate(), request.getScmBranch(), datePattern, startVersion,
088                endVersion, request.getLimit() );
089    }
090
091    private ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
092                                                          Date startDate, Date endDate, ScmBranch branch,
093                                                          String datePattern, ScmVersion startVersion,
094                                                          ScmVersion endVersion, Integer limit )
095        throws ScmException
096    {
097        Commandline cl = createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), branch, startDate,
098                                            endDate, startVersion, endVersion, limit );
099
100        SvnChangeLogConsumer consumer = new SvnChangeLogConsumer( getLogger(), datePattern );
101
102        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
103
104        if ( getLogger().isInfoEnabled() )
105        {
106            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
107
108            if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
109            {
110                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
111            }
112        }
113
114        int exitCode;
115
116        try
117        {
118            exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
119        }
120        catch ( CommandLineException ex )
121        {
122            throw new ScmException( "Error while executing svn command.", ex );
123        }
124
125        if ( exitCode != 0 )
126        {
127            return new ChangeLogScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
128        }
129        ChangeLogSet changeLogSet = new ChangeLogSet( consumer.getModifications(), startDate, endDate );
130        changeLogSet.setStartVersion( startVersion );
131        changeLogSet.setEndVersion( endVersion );
132
133        return new ChangeLogScmResult( cl.toString(), changeLogSet );
134    }
135
136    // ----------------------------------------------------------------------
137    //
138    // ----------------------------------------------------------------------
139
140    public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
141                                                 ScmBranch branch, Date startDate, Date endDate,
142                                                 ScmVersion startVersion, ScmVersion endVersion )
143    {
144        return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion,
145                                  null );
146    }
147
148    public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
149                                                 ScmBranch branch, Date startDate, Date endDate,
150                                                 ScmVersion startVersion, ScmVersion endVersion, Integer limit )
151    {
152        SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
153
154        dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
155
156        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
157
158        cl.createArg().setValue( "log" );
159
160        cl.createArg().setValue( "-v" );
161
162        // TODO: May want to add some kind of support for --stop-on-copy and --limit NUM
163
164        if ( limit != null && limit > 0 )
165        {
166            cl.createArg().setValue( "--limit" );
167            cl.createArg().setValue( Integer.toString( limit ) );
168        }
169
170        if ( startDate != null )
171        {
172            cl.createArg().setValue( "-r" );
173
174            if ( endDate != null )
175            {
176                cl.createArg().setValue(
177                    "{" + dateFormat.format( startDate ) + "}" + ":" + "{" + dateFormat.format( endDate ) + "}" );
178            }
179            else
180            {
181                cl.createArg().setValue( "{" + dateFormat.format( startDate ) + "}:HEAD" );
182            }
183        }
184
185        if ( startVersion != null )
186        {
187            cl.createArg().setValue( "-r" );
188
189            if ( endVersion != null )
190            {
191                if ( startVersion.getName().equals( endVersion.getName() ) )
192                {
193                    cl.createArg().setValue( startVersion.getName() );
194                }
195                else
196                {
197                    cl.createArg().setValue( startVersion.getName() + ":" + endVersion.getName() );
198                }
199            }
200            else
201            {
202                cl.createArg().setValue( startVersion.getName() + ":HEAD" );
203            }
204        }
205
206        if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
207        {
208            // By specifying a branch and this repository url below, subversion should show
209            // the changelog of that branch, but limit it to paths that also occur in this repository.
210            if ( branch instanceof ScmTag )
211            {
212                cl.createArg().setValue( SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) branch ) );
213            }
214            else
215            {
216                cl.createArg().setValue( SvnTagBranchUtils.resolveBranchUrl( repository, branch ) );
217            }
218        }
219
220        if ( endVersion == null || !StringUtils.equals( "BASE", endVersion.getName() ) )
221        {
222            cl.createArg().setValue( repository.getUrl() );
223        }
224
225        return cl;
226    }
227}