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  
43  /**
44   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
45   * @author Olivier Lamy
46   *
47   */
48  public class GitChangeLogCommand
49      extends AbstractChangeLogCommand
50      implements GitCommand
51  {
52      private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
53  
54      /** {@inheritDoc} */
55      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
56                                                            ScmVersion startVersion, ScmVersion endVersion,
57                                                            String datePattern )
58          throws ScmException
59      {
60          return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
61      }
62  
63      /** {@inheritDoc} */
64      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
65                                                            Date startDate, Date endDate, ScmBranch branch,
66                                                            String datePattern )
67          throws ScmException
68      {
69          return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
70      }
71  
72      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
73                                                            Date startDate, Date endDate, ScmBranch branch,
74                                                            String datePattern, ScmVersion startVersion,
75                                                            ScmVersion endVersion )
76          throws ScmException
77      {
78          return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, startVersion,
79                                          endVersion, null );
80      }
81  
82      @Override
83      protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
84          throws ScmException
85      {
86          final ScmVersion startVersion = request.getStartRevision();
87          final ScmVersion endVersion = request.getEndRevision();
88          final ScmFileSet fileSet = request.getScmFileSet();
89          final String datePattern = request.getDatePattern();
90          return executeChangeLogCommand( request.getScmRepository().getProviderRepository(), fileSet,
91              request.getStartDate(), request.getEndDate(), request.getScmBranch(), datePattern, startVersion,
92                  endVersion, request.getLimit() );
93      }
94  
95      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
96                                                            Date startDate, Date endDate, ScmBranch branch,
97                                                            String datePattern, ScmVersion startVersion,
98                                                            ScmVersion endVersion, Integer limit )
99          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 }