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;
042
043/**
044 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
045 * @author Olivier Lamy
046 *
047 */
048public class GitChangeLogCommand
049    extends AbstractChangeLogCommand
050    implements GitCommand
051{
052    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
053
054    /** {@inheritDoc} */
055    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
056                                                          ScmVersion startVersion, ScmVersion endVersion,
057                                                          String datePattern )
058        throws ScmException
059    {
060        return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
061    }
062
063    /** {@inheritDoc} */
064    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
065                                                          Date startDate, Date endDate, ScmBranch branch,
066                                                          String datePattern )
067        throws ScmException
068    {
069        return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
070    }
071
072    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
073                                                          Date startDate, Date endDate, ScmBranch branch,
074                                                          String datePattern, ScmVersion startVersion,
075                                                          ScmVersion endVersion )
076        throws ScmException
077    {
078        return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, startVersion,
079                                        endVersion, null );
080    }
081
082    @Override
083    protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
084        throws ScmException
085    {
086        final ScmVersion startVersion = request.getStartRevision();
087        final ScmVersion endVersion = request.getEndRevision();
088        final ScmFileSet fileSet = request.getScmFileSet();
089        final String datePattern = request.getDatePattern();
090        return executeChangeLogCommand( request.getScmRepository().getProviderRepository(), fileSet,
091            request.getStartDate(), request.getEndDate(), request.getScmBranch(), datePattern, startVersion,
092                endVersion, request.getLimit() );
093    }
094
095    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
096                                                          Date startDate, Date endDate, ScmBranch branch,
097                                                          String datePattern, ScmVersion startVersion,
098                                                          ScmVersion endVersion, Integer limit )
099        throws ScmException
100    {
101        Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet.getBasedir(), branch, startDate,
102                                            endDate, startVersion, endVersion, limit );
103
104        GitChangeLogConsumer consumer = new GitChangeLogConsumer( getLogger(), datePattern );
105
106        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
107
108        int exitCode;
109
110        exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
111        if ( exitCode != 0 )
112        {
113            return new ChangeLogScmResult( cl.toString(), "The git-log command failed.", stderr.getOutput(), false );
114        }
115        ChangeLogSet changeLogSet = new ChangeLogSet( consumer.getModifications(), startDate, endDate );
116        changeLogSet.setStartVersion( startVersion );
117        changeLogSet.setEndVersion( endVersion );
118
119        return new ChangeLogScmResult( cl.toString(), changeLogSet );
120    }
121
122    // ----------------------------------------------------------------------
123    //
124    // ----------------------------------------------------------------------
125
126    /**
127     * this constructs creates the commandline for the git-whatchanged command.
128     * Since it uses --since and --until for the start and end date, the branch
129     * and version parameters can be used simultanously. 
130     */
131    public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
132                                                 ScmBranch branch, Date startDate, Date endDate,
133                                                 ScmVersion startVersion, ScmVersion endVersion )
134    {
135        return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion,
136                                  null );
137    }
138
139    static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
140                                                 ScmBranch branch, Date startDate, Date endDate,
141                                                 ScmVersion startVersion, ScmVersion endVersion, Integer limit )
142    {
143        SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
144        dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
145
146        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "whatchanged" );
147
148        if ( startDate != null || endDate != null )
149        {
150            if ( startDate != null )
151            {
152                cl.createArg().setValue( "--since=" + StringUtils.escape( dateFormat.format( startDate ) ) );
153            }
154
155            if ( endDate != null )
156            {
157                cl.createArg().setValue( "--until=" + StringUtils.escape( dateFormat.format( endDate ) ) );
158            }
159
160        }
161
162        // since this parameter is also used for the output formatting, we need it also if no start nor end date is
163        // given
164        cl.createArg().setValue( "--date=iso" );
165
166        if ( startVersion != null || endVersion != null )
167        {
168            StringBuilder versionRange = new StringBuilder();
169            
170            if ( startVersion != null )
171            {
172                versionRange.append( StringUtils.escape( startVersion.getName() ) );
173            }
174
175            versionRange.append( ".." );
176            
177            if ( endVersion != null )
178            {
179                versionRange.append( StringUtils.escape( endVersion.getName() ) );
180            }
181            
182            cl.createArg().setValue( versionRange.toString() ); 
183
184        }
185
186        if ( limit != null && limit > 0 )
187        {
188            cl.createArg().setValue( "--max-count=" + limit );
189        }
190
191        if ( branch != null && branch.getName() != null && branch.getName().length() > 0 )
192        {
193            cl.createArg().setValue( branch.getName() );
194        }
195
196        // Insert a separator to make sure that files aren't interpreted as part of the version spec
197        cl.createArg().setValue( "--" );
198        
199        // We have to report only the changes of the current project.
200        // This is needed for child projects, otherwise we would get the changelog of the 
201        // whole parent-project including all childs.
202        cl.createArg().setFile( workingDirectory );
203        
204        return cl;
205    }
206}