001    package 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    
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.ScmVersion;
026    import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
027    import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
028    import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
029    import org.apache.maven.scm.command.changelog.ChangeLogSet;
030    import org.apache.maven.scm.provider.ScmProviderRepository;
031    import org.apache.maven.scm.provider.git.command.GitCommand;
032    import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
033    import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
034    import org.codehaus.plexus.util.StringUtils;
035    import org.codehaus.plexus.util.cli.CommandLineUtils;
036    import org.codehaus.plexus.util.cli.Commandline;
037    
038    import java.io.File;
039    import java.text.SimpleDateFormat;
040    import java.util.Date;
041    import java.util.TimeZone;
042    
043    /**
044     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
045     * @author Olivier Lamy
046     * @version $Id: GitChangeLogCommand.java 1353204 2012-06-23 21:45:17Z hboutemy $
047     */
048    public 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, endVersion, null );
079        }
080    
081        @Override
082        protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
083            throws ScmException
084        {
085            final ScmVersion startVersion = request.getStartRevision();
086            final ScmVersion endVersion = request.getEndRevision();
087            final ScmFileSet fileSet = request.getScmFileSet();
088            final String datePattern = request.getDatePattern();
089            return executeChangeLogCommand( request.getScmRepository().getProviderRepository(), fileSet,
090                request.getStartDate(), request.getEndDate(), request.getScmBranch(), datePattern, startVersion,
091                    endVersion, request.getLimit() );
092        }
093    
094        protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
095                                                              Date startDate, Date endDate, ScmBranch branch,
096                                                              String datePattern, ScmVersion startVersion,
097                                                              ScmVersion endVersion, Integer limit )
098            throws ScmException
099        {
100            Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet.getBasedir(), branch, startDate,
101                                                endDate, startVersion, endVersion, limit );
102    
103            GitChangeLogConsumer consumer = new GitChangeLogConsumer( getLogger(), datePattern );
104    
105            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
106    
107            int exitCode;
108    
109            exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
110            if ( exitCode != 0 )
111            {
112                return new ChangeLogScmResult( cl.toString(), "The git-log command failed.", stderr.getOutput(), false );
113            }
114            ChangeLogSet changeLogSet = new ChangeLogSet( consumer.getModifications(), startDate, endDate );
115            changeLogSet.setStartVersion( startVersion );
116            changeLogSet.setEndVersion( endVersion );
117    
118            return new ChangeLogScmResult( cl.toString(), changeLogSet );
119        }
120    
121        // ----------------------------------------------------------------------
122        //
123        // ----------------------------------------------------------------------
124    
125        /**
126         * this constructs creates the commandline for the git-whatchanged command.
127         * Since it uses --since and --until for the start and end date, the branch
128         * and version parameters can be used simultanously. 
129         */
130        public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
131                                                     ScmBranch branch, Date startDate, Date endDate,
132                                                     ScmVersion startVersion, ScmVersion endVersion )
133        {
134            return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion, null );
135        }
136    
137        static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
138                                                     ScmBranch branch, Date startDate, Date endDate,
139                                                     ScmVersion startVersion, ScmVersion endVersion, Integer limit )
140        {
141            SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
142            dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
143    
144            Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "whatchanged" );
145    
146            if ( startDate != null || endDate != null )
147            {
148                if ( startDate != null )
149                {
150                    cl.createArg().setValue( "--since=" + StringUtils.escape( dateFormat.format( startDate ) ) );
151                }
152    
153                if ( endDate != null )
154                {
155                    cl.createArg().setValue( "--until=" + StringUtils.escape( dateFormat.format( endDate ) ) );
156                }
157    
158            }
159    
160            // since this parameter is also used for the output formatting, we need it also if no start nor end date is given
161            cl.createArg().setValue( "--date=iso" );
162    
163            if ( startVersion != null || endVersion != null )
164            {
165                StringBuilder versionRange = new StringBuilder();
166                
167                if ( startVersion != null )
168                {
169                    versionRange.append( StringUtils.escape( startVersion.getName() ) );
170                }
171    
172                versionRange.append( ".." );
173                
174                if ( endVersion != null )
175                {
176                    versionRange.append( StringUtils.escape( endVersion.getName() ) );
177                }
178                
179                cl.createArg().setValue( versionRange.toString() ); 
180    
181            }
182    
183            if ( limit != null && limit > 0 )
184            {
185                cl.createArg().setValue( "--max-count=" + limit );
186            }
187    
188            if ( branch != null && branch.getName() != null && branch.getName().length() > 0 )
189            {
190                cl.createArg().setValue( branch.getName() );
191            }
192    
193            // Insert a separator to make sure that files aren't interpreted as part of the version spec
194            cl.createArg().setValue( "--" );
195            
196            // We have to report only the changes of the current project.
197            // This is needed for child projects, otherwise we would get the changelog of the 
198            // whole parent-project including all childs.
199            cl.createArg().setFile( workingDirectory );
200            
201            return cl;
202        }
203    }