001package org.apache.maven.scm.provider.git.gitexe.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.ScmVersion;
026import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
027import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
028import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
029import org.apache.maven.scm.command.changelog.ChangeLogSet;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.git.command.GitCommand;
032import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
033import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
034import org.codehaus.plexus.util.StringUtils;
035import org.codehaus.plexus.util.cli.CommandLineUtils;
036import org.codehaus.plexus.util.cli.Commandline;
037
038import java.io.File;
039import java.text.SimpleDateFormat;
040import java.util.Date;
041import java.util.TimeZone;
042import org.apache.maven.scm.CommandParameter;
043import org.apache.maven.scm.CommandParameters;
044import org.apache.maven.scm.ScmResult;
045
046/**
047 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
048 * @author Olivier Lamy
049 *
050 */
051public class GitChangeLogCommand
052    extends AbstractChangeLogCommand
053    implements GitCommand
054{
055    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
056
057    @Override
058    public ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
059                                     CommandParameters parameters )
060        throws ScmException
061    {
062        return executeChangeLogCommand( repository, fileSet,
063                parameters.getDate( CommandParameter.START_DATE, null ),
064                parameters.getDate( CommandParameter.END_DATE, null ),
065                (ScmBranch) parameters.getScmVersion( CommandParameter.BRANCH, null ),
066                parameters.getString( CommandParameter.CHANGELOG_DATE_PATTERN, null ),
067                parameters.getScmVersion( CommandParameter.START_SCM_VERSION, null ),
068                parameters.getScmVersion( CommandParameter.END_SCM_VERSION, null ),
069                parameters.getInt( CommandParameter.LIMIT, -1 ),
070                parameters.getScmVersion( CommandParameter.SCM_VERSION, null ) );
071    }
072
073    /** {@inheritDoc} */
074    @Override
075    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
076                                                          ScmVersion startVersion, ScmVersion endVersion,
077                                                          String datePattern )
078        throws ScmException
079    {
080        return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
086                                                          Date startDate, Date endDate, ScmBranch branch,
087                                                          String datePattern )
088        throws ScmException
089    {
090        return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
091    }
092
093    @Override
094    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
095                                                          ScmVersion version, String datePattern )
096        throws ScmException
097    {
098        return executeChangeLogCommand( repository, fileSet, null, null, null, datePattern, null, null, null, version );
099    }
100
101    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
102                                                          Date startDate, Date endDate, ScmBranch branch,
103                                                          String datePattern, ScmVersion startVersion,
104                                                          ScmVersion endVersion )
105        throws ScmException
106    {
107        return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, startVersion,
108                                        endVersion, null, null );
109    }
110
111    @Override
112    protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
113        throws ScmException
114    {
115        final ScmVersion startVersion = request.getStartRevision();
116        final ScmVersion endVersion = request.getEndRevision();
117        final ScmVersion revision = request.getRevision();
118        final ScmFileSet fileSet = request.getScmFileSet();
119        final String datePattern = request.getDatePattern();
120        final ScmProviderRepository providerRepository = request.getScmRepository().getProviderRepository();
121        final Date startDate = request.getStartDate();
122        final Date endDate = request.getEndDate();
123        final ScmBranch branch = request.getScmBranch();
124        final Integer limit = request.getLimit();
125
126        return executeChangeLogCommand( providerRepository, fileSet, startDate, endDate, branch, datePattern,
127                                        startVersion, endVersion, limit, revision );
128    }
129
130    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
131                                                          Date startDate, Date endDate, ScmBranch branch,
132                                                          String datePattern, ScmVersion startVersion,
133                                                          ScmVersion endVersion, Integer limit )
134        throws ScmException
135    {
136        return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern,
137                                        startVersion, endVersion, limit, null );
138    }
139
140    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
141                                                          Date startDate, Date endDate, ScmBranch branch,
142                                                          String datePattern, ScmVersion startVersion,
143                                                          ScmVersion endVersion, Integer limit, ScmVersion version )
144        throws ScmException
145    {
146        Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet.getBasedir(), branch, startDate,
147                                            endDate, startVersion, endVersion, limit, version );
148
149        GitChangeLogConsumer consumer = new GitChangeLogConsumer( getLogger(), datePattern );
150
151        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
152
153        int exitCode;
154
155        exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
156        if ( exitCode != 0 )
157        {
158            return new ChangeLogScmResult( cl.toString(), "The git-log command failed.", stderr.getOutput(), false );
159        }
160        ChangeLogSet changeLogSet = new ChangeLogSet( consumer.getModifications(), startDate, endDate );
161        changeLogSet.setStartVersion( startVersion );
162        changeLogSet.setEndVersion( endVersion );
163
164        return new ChangeLogScmResult( cl.toString(), changeLogSet );
165    }
166
167    // ----------------------------------------------------------------------
168    //
169    // ----------------------------------------------------------------------
170
171    /**
172     * This method creates the commandline for the git-whatchanged command.
173     * <p>
174     * Since it uses --since and --until for the start and end date, the branch
175     * and version parameters can be used simultanously. 
176     *
177     * @param repository Provider repositry to use.
178     * @param workingDirectory Working copy directory.
179     * @param branch Branch to run command on.
180     * @param startDate Start date of log entries.
181     * @param endDate End date of log entries.
182     * @param startVersion Start version of log entries.
183     * @param endVersion End version of log entries.
184     * @return Command line.
185     */
186    public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
187                                                 ScmBranch branch, Date startDate, Date endDate,
188                                                 ScmVersion startVersion, ScmVersion endVersion )
189    {
190        return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion,
191                                  null );
192    }
193
194    static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
195                                          ScmBranch branch, Date startDate, Date endDate,
196                                          ScmVersion startVersion, ScmVersion endVersion, Integer limit )
197    {
198        return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion,
199                            limit, null );
200    }
201
202    static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
203                                                 ScmBranch branch, Date startDate, Date endDate,
204                                                 ScmVersion startVersion, ScmVersion endVersion, Integer limit,
205                                                 ScmVersion version )
206    {
207        SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
208        dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
209
210        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "whatchanged" );
211
212        if ( startDate != null || endDate != null )
213        {
214            if ( startDate != null )
215            {
216                cl.createArg().setValue( "--since=" + StringUtils.escape( dateFormat.format( startDate ) ) );
217            }
218
219            if ( endDate != null )
220            {
221                cl.createArg().setValue( "--until=" + StringUtils.escape( dateFormat.format( endDate ) ) );
222            }
223
224        }
225
226        // since this parameter is also used for the output formatting, we need it also if no start nor end date is
227        // given
228        cl.createArg().setValue( "--date=iso" );
229
230        if ( startVersion != null || endVersion != null )
231        {
232            StringBuilder versionRange = new StringBuilder();
233
234            if ( startVersion != null )
235            {
236                versionRange.append( StringUtils.escape( startVersion.getName() ) );
237            }
238
239            versionRange.append( ".." );
240
241            if ( endVersion != null )
242            {
243                versionRange.append( StringUtils.escape( endVersion.getName() ) );
244            }
245
246            cl.createArg().setValue( versionRange.toString() );
247
248        }
249        else if ( version != null )
250        {
251            cl.createArg().setValue( StringUtils.escape( version.getName() ) );
252        }
253
254        if ( limit != null && limit > 0 )
255        {
256            cl.createArg().setValue( "--max-count=" + limit );
257        }
258
259        if ( branch != null && branch.getName() != null && branch.getName().length() > 0 )
260        {
261            cl.createArg().setValue( branch.getName() );
262        }
263
264        // Insert a separator to make sure that files aren't interpreted as part of the version spec
265        cl.createArg().setValue( "--" );
266        
267        // We have to report only the changes of the current project.
268        // This is needed for child projects, otherwise we would get the changelog of the 
269        // whole parent-project including all childs.
270        cl.createArg().setValue( "." );
271        
272        return cl;
273    }
274}