View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command.changelog;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmBranch;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmVersion;
26  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
27  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
28  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
29  import org.apache.maven.scm.command.changelog.ChangeLogSet;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.git.command.GitCommand;
32  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
33  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.cli.CommandLineUtils;
36  import org.codehaus.plexus.util.cli.Commandline;
37  
38  import java.io.File;
39  import java.text.SimpleDateFormat;
40  import java.util.Date;
41  import java.util.TimeZone;
42  import org.apache.maven.scm.CommandParameter;
43  import org.apache.maven.scm.CommandParameters;
44  import org.apache.maven.scm.ScmResult;
45  
46  /**
47   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
48   * @author Olivier Lamy
49   *
50   */
51  public class GitChangeLogCommand
52      extends AbstractChangeLogCommand
53      implements GitCommand
54  {
55      private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
56  
57      @Override
58      public ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
59                                       CommandParameters parameters )
60          throws ScmException
61      {
62          return executeChangeLogCommand( repository, fileSet,
63                  parameters.getDate( CommandParameter.START_DATE, null ),
64                  parameters.getDate( CommandParameter.END_DATE, null ),
65                  (ScmBranch) parameters.getScmVersion( CommandParameter.BRANCH, null ),
66                  parameters.getString( CommandParameter.CHANGELOG_DATE_PATTERN, null ),
67                  parameters.getScmVersion( CommandParameter.START_SCM_VERSION, null ),
68                  parameters.getScmVersion( CommandParameter.END_SCM_VERSION, null ),
69                  parameters.getInt( CommandParameter.LIMIT, -1 ),
70                  parameters.getScmVersion( CommandParameter.SCM_VERSION, null ) );
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
76                                                            ScmVersion startVersion, ScmVersion endVersion,
77                                                            String datePattern )
78          throws ScmException
79      {
80          return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
86                                                            Date startDate, Date endDate, ScmBranch branch,
87                                                            String datePattern )
88          throws ScmException
89      {
90          return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
91      }
92  
93      @Override
94      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
95                                                            ScmVersion version, String datePattern )
96          throws ScmException
97      {
98          return executeChangeLogCommand( repository, fileSet, null, null, null, datePattern, null, null, null, version );
99      }
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         cl.createArg().setValue( "--format=medium" );
212 
213         if ( startDate != null || endDate != null )
214         {
215             if ( startDate != null )
216             {
217                 cl.createArg().setValue( "--since=" + StringUtils.escape( dateFormat.format( startDate ) ) );
218             }
219 
220             if ( endDate != null )
221             {
222                 cl.createArg().setValue( "--until=" + StringUtils.escape( dateFormat.format( endDate ) ) );
223             }
224 
225         }
226 
227         // since this parameter is also used for the output formatting, we need it also if no start nor end date is
228         // given
229         cl.createArg().setValue( "--date=iso" );
230 
231         if ( startVersion != null || endVersion != null )
232         {
233             StringBuilder versionRange = new StringBuilder();
234 
235             if ( startVersion != null )
236             {
237                 versionRange.append( StringUtils.escape( startVersion.getName() ) );
238             }
239 
240             versionRange.append( ".." );
241 
242             if ( endVersion != null )
243             {
244                 versionRange.append( StringUtils.escape( endVersion.getName() ) );
245             }
246 
247             cl.createArg().setValue( versionRange.toString() );
248 
249         }
250         else if ( version != null )
251         {
252             cl.createArg().setValue( StringUtils.escape( version.getName() ) );
253         }
254 
255         if ( limit != null && limit > 0 )
256         {
257             cl.createArg().setValue( "--max-count=" + limit );
258         }
259 
260         if ( branch != null && branch.getName() != null && branch.getName().length() > 0 )
261         {
262             cl.createArg().setValue( branch.getName() );
263         }
264 
265         // Insert a separator to make sure that files aren't interpreted as part of the version spec
266         cl.createArg().setValue( "--" );
267         
268         // We have to report only the changes of the current project.
269         // This is needed for child projects, otherwise we would get the changelog of the 
270         // whole parent-project including all childs.
271         cl.createArg().setValue( "." );
272         
273         return cl;
274     }
275 }