001    package 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    
022    import org.apache.maven.scm.ScmBranch;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.ScmTag;
026    import org.apache.maven.scm.ScmVersion;
027    import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
028    import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
029    import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
030    import org.apache.maven.scm.command.changelog.ChangeLogSet;
031    import org.apache.maven.scm.provider.ScmProviderRepository;
032    import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
033    import org.apache.maven.scm.provider.svn.command.SvnCommand;
034    import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
035    import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
036    import org.codehaus.plexus.util.StringUtils;
037    import org.codehaus.plexus.util.cli.CommandLineException;
038    import org.codehaus.plexus.util.cli.CommandLineUtils;
039    import org.codehaus.plexus.util.cli.Commandline;
040    
041    import java.io.File;
042    import java.text.SimpleDateFormat;
043    import java.util.Date;
044    import java.util.TimeZone;
045    
046    /**
047     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
048     * @author Olivier Lamy
049     * @version $Id: SvnChangeLogCommand.java 1353204 2012-06-23 21:45:17Z hboutemy $
050     */
051    public class SvnChangeLogCommand
052        extends AbstractChangeLogCommand
053        implements SvnCommand
054    {
055        private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
056    
057        /** {@inheritDoc} */
058        @Deprecated
059        protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
060                                                              ScmVersion startVersion, ScmVersion endVersion,
061                                                              String datePattern )
062            throws ScmException
063        {
064            return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion, null );
065        }
066    
067        /** {@inheritDoc} */
068        @Deprecated
069        protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
070                                                              Date startDate, Date endDate, ScmBranch branch,
071                                                              String datePattern )
072            throws ScmException
073        {
074            return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null, null );
075        }
076    
077        @Override
078        protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
079            throws ScmException
080        {
081            final ScmVersion startVersion = request.getStartRevision();
082            final ScmVersion endVersion = request.getEndRevision();
083            final ScmFileSet fileSet = request.getScmFileSet();
084            final String datePattern = request.getDatePattern();
085            return executeChangeLogCommand( request.getScmRepository().getProviderRepository(), fileSet,
086                request.getStartDate(), request.getEndDate(), request.getScmBranch(), datePattern, startVersion,
087                    endVersion, request.getLimit() );
088        }
089    
090        private ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
091                                                              Date startDate, Date endDate, ScmBranch branch,
092                                                              String datePattern, ScmVersion startVersion,
093                                                              ScmVersion endVersion, Integer limit )
094            throws ScmException
095        {
096            Commandline cl = createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), branch, startDate,
097                                                endDate, startVersion, endVersion, limit );
098    
099            SvnChangeLogConsumer consumer = new SvnChangeLogConsumer( getLogger(), datePattern );
100    
101            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
102    
103            if ( getLogger().isInfoEnabled() )
104            {
105                getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
106                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
107            }
108    
109            int exitCode;
110    
111            try
112            {
113                exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
114            }
115            catch ( CommandLineException ex )
116            {
117                throw new ScmException( "Error while executing svn command.", ex );
118            }
119    
120            if ( exitCode != 0 )
121            {
122                return new ChangeLogScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
123            }
124            ChangeLogSet changeLogSet = new ChangeLogSet( consumer.getModifications(), startDate, endDate );
125            changeLogSet.setStartVersion( startVersion );
126            changeLogSet.setEndVersion( endVersion );
127    
128            return new ChangeLogScmResult( cl.toString(), changeLogSet );
129        }
130    
131        // ----------------------------------------------------------------------
132        //
133        // ----------------------------------------------------------------------
134    
135        public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
136                                                     ScmBranch branch, Date startDate, Date endDate,
137                                                     ScmVersion startVersion, ScmVersion endVersion )
138        {
139            return createCommandLine(repository, workingDirectory, branch,
140                startDate, endDate, startVersion, endVersion, null);
141        }
142    
143        public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
144                                                     ScmBranch branch, Date startDate, Date endDate,
145                                                     ScmVersion startVersion, ScmVersion endVersion, Integer limit )
146        {
147            SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
148    
149            dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
150    
151            Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
152    
153            cl.createArg().setValue( "log" );
154    
155            cl.createArg().setValue( "-v" );
156    
157            // TODO: May want to add some kind of support for --stop-on-copy and --limit NUM
158    
159            if ( limit != null && limit > 0 )
160            {
161                cl.createArg().setValue( "--limit" );
162                cl.createArg().setValue( Integer.toString( limit ) );
163            }
164    
165            if ( startDate != null )
166            {
167                cl.createArg().setValue( "-r" );
168    
169                if ( endDate != null )
170                {
171                    cl.createArg().setValue(
172                        "{" + dateFormat.format( startDate ) + "}" + ":" + "{" + dateFormat.format( endDate ) + "}" );
173                }
174                else
175                {
176                    cl.createArg().setValue( "{" + dateFormat.format( startDate ) + "}:HEAD" );
177                }
178            }
179    
180            if ( startVersion != null )
181            {
182                cl.createArg().setValue( "-r" );
183    
184                if ( endVersion != null )
185                {
186                    if ( startVersion.getName().equals( endVersion.getName() ) )
187                    {
188                        cl.createArg().setValue( startVersion.getName() );
189                    }
190                    else
191                    {
192                        cl.createArg().setValue( startVersion.getName() + ":" + endVersion.getName() );
193                    }
194                }
195                else
196                {
197                    cl.createArg().setValue( startVersion.getName() + ":HEAD" );
198                }
199            }
200    
201            if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
202            {
203                // By specifying a branch and this repository url below, subversion should show
204                // the changelog of that branch, but limit it to paths that also occur in this repository.
205                if ( branch instanceof ScmTag )
206                {
207                    cl.createArg().setValue( SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) branch ) );
208                }
209                else
210                {
211                    cl.createArg().setValue( SvnTagBranchUtils.resolveBranchUrl( repository, branch ) );
212                }
213            }
214    
215            if ( endVersion == null || !StringUtils.equals( "BASE", endVersion.getName() ) )
216            {
217                cl.createArg().setValue( repository.getUrl() );
218            }
219    
220            return cl;
221        }
222    }