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, endVersion, null );
79      }
80  
81      @Override
82      protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
83          throws ScmException
84      {
85          final ScmVersion startVersion = request.getStartRevision();
86          final ScmVersion endVersion = request.getEndRevision();
87          final ScmFileSet fileSet = request.getScmFileSet();
88          final String datePattern = request.getDatePattern();
89          return executeChangeLogCommand( request.getScmRepository().getProviderRepository(), fileSet,
90              request.getStartDate(), request.getEndDate(), request.getScmBranch(), datePattern, startVersion,
91                  endVersion, request.getLimit() );
92      }
93  
94      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
95                                                            Date startDate, Date endDate, ScmBranch branch,
96                                                            String datePattern, ScmVersion startVersion,
97                                                            ScmVersion endVersion, Integer limit )
98          throws ScmException
99      {
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 }