View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.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.ChangeSet;
23  import org.apache.maven.scm.ScmBranch;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmVersion;
27  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
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.jgit.command.JGitUtils;
33  import org.eclipse.jgit.api.Git;
34  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
35  import org.eclipse.jgit.errors.MissingObjectException;
36  import org.eclipse.jgit.lib.Repository;
37  import org.eclipse.jgit.revwalk.RevCommit;
38  import org.eclipse.jgit.revwalk.RevSort;
39  
40  import java.io.File;
41  import java.io.IOException;
42  import java.util.ArrayList;
43  import java.util.Date;
44  import java.util.List;
45  
46  /**
47   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
48   * @author Dominik Bartholdi (imod)
49   * @since 1.9
50   */
51  public class JGitChangeLogCommand
52      extends AbstractChangeLogCommand
53      implements GitCommand
54  {
55  
56      /**
57       * {@inheritDoc}
58       */
59      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
60                                                            ScmVersion startVersion, ScmVersion endVersion,
61                                                            String datePattern )
62          throws ScmException
63      {
64          return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
71                                                            Date startDate, Date endDate, ScmBranch branch,
72                                                            String datePattern )
73          throws ScmException
74      {
75          return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
76      }
77  
78      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
79                                                            Date startDate, Date endDate, ScmBranch branch,
80                                                            String datePattern, ScmVersion startVersion,
81                                                            ScmVersion endVersion )
82          throws ScmException
83      {
84          Git git = null;
85          try
86          {
87              git = Git.open( fileSet.getBasedir() );
88  
89              String startRev = startVersion != null ? startVersion.getName() : null;
90              String endRev = endVersion != null ? endVersion.getName() : null;
91  
92              List<ChangeEntry> gitChanges =
93                  this.whatchanged( git.getRepository(), null, startRev, endRev, startDate, endDate, -1 );
94  
95              List<ChangeSet> modifications = new ArrayList<ChangeSet>( gitChanges.size() );
96  
97              for ( ChangeEntry change : gitChanges )
98              {
99                  ChangeSet scmChange = new ChangeSet();
100 
101                 scmChange.setAuthor( change.getAuthorName() );
102                 scmChange.setComment( change.getBody() );
103                 scmChange.setDate( change.getAuthorDate() );
104                 scmChange.setRevision( change.getCommitHash() );
105                 // X TODO scmChange.setFiles( change.get )
106 
107                 modifications.add( scmChange );
108             }
109 
110             ChangeLogSet changeLogSet = new ChangeLogSet( modifications, startDate, endDate );
111             changeLogSet.setStartVersion( startVersion );
112             changeLogSet.setEndVersion( endVersion );
113 
114             return new ChangeLogScmResult( "JGit changelog", changeLogSet );
115         }
116         catch ( Exception e )
117         {
118             throw new ScmException( "JGit changelog failure!", e );
119         }
120         finally
121         {
122             JGitUtils.closeRepo( git );
123         }
124     }
125 
126     public List<ChangeEntry> whatchanged( Repository repo, RevSort[] sortings, String fromRev, String toRev,
127                                           Date fromDate, Date toDate, int maxLines )
128         throws MissingObjectException, IncorrectObjectTypeException, IOException
129     {
130         List<RevCommit> revs = JGitUtils.getRevCommits( repo, sortings, fromRev, toRev, fromDate, toDate, maxLines );
131         List<ChangeEntry> changes = new ArrayList<ChangeEntry>( revs.size() );
132 
133         for ( RevCommit c : revs )
134         {
135             ChangeEntry ce = new ChangeEntry();
136 
137             ce.setAuthorDate( c.getAuthorIdent().getWhen() );
138             ce.setAuthorEmail( c.getAuthorIdent().getEmailAddress() );
139             ce.setAuthorName( c.getAuthorIdent().getName() );
140             ce.setCommitterDate( c.getCommitterIdent().getWhen() );
141             ce.setCommitterEmail( c.getCommitterIdent().getEmailAddress() );
142             ce.setCommitterName( c.getCommitterIdent().getName() );
143 
144             ce.setSubject( c.getShortMessage() );
145             ce.setBody( c.getFullMessage() );
146 
147             ce.setCommitHash( c.getId().name() );
148             ce.setTreeHash( c.getTree().getId().name() );
149 
150             // X TODO missing: file list
151 
152             changes.add( ce );
153         }
154 
155         return changes;
156     }
157 
158     public static final class ChangeEntry
159     {
160         private String commitHash;
161 
162         private String treeHash;
163 
164         private String authorName;
165 
166         private String authorEmail;
167 
168         private Date authorDate;
169 
170         private String committerName;
171 
172         private String committerEmail;
173 
174         private Date committerDate;
175 
176         private String subject;
177 
178         private String body;
179 
180         private List<File> files;
181 
182         public String getCommitHash()
183         {
184             return commitHash;
185         }
186 
187         public void setCommitHash( String commitHash )
188         {
189             this.commitHash = commitHash;
190         }
191 
192         public String getTreeHash()
193         {
194             return treeHash;
195         }
196 
197         public void setTreeHash( String treeHash )
198         {
199             this.treeHash = treeHash;
200         }
201 
202         public String getAuthorName()
203         {
204             return authorName;
205         }
206 
207         public void setAuthorName( String authorName )
208         {
209             this.authorName = authorName;
210         }
211 
212         public String getAuthorEmail()
213         {
214             return authorEmail;
215         }
216 
217         public void setAuthorEmail( String authorEmail )
218         {
219             this.authorEmail = authorEmail;
220         }
221 
222         public Date getAuthorDate()
223         {
224             return authorDate;
225         }
226 
227         public void setAuthorDate( Date authorDate )
228         {
229             this.authorDate = authorDate;
230         }
231 
232         public String getCommitterName()
233         {
234             return committerName;
235         }
236 
237         public void setCommitterName( String committerName )
238         {
239             this.committerName = committerName;
240         }
241 
242         public String getCommitterEmail()
243         {
244             return committerEmail;
245         }
246 
247         public void setCommitterEmail( String committerEmail )
248         {
249             this.committerEmail = committerEmail;
250         }
251 
252         public Date getCommitterDate()
253         {
254             return committerDate;
255         }
256 
257         public void setCommitterDate( Date committerDate )
258         {
259             this.committerDate = committerDate;
260         }
261 
262         public String getSubject()
263         {
264             return subject;
265         }
266 
267         public void setSubject( String subject )
268         {
269             this.subject = subject;
270         }
271 
272         public String getBody()
273         {
274             return body;
275         }
276 
277         public void setBody( String body )
278         {
279             this.body = body;
280         }
281 
282         public List<File> getFiles()
283         {
284             return files;
285         }
286 
287         public void setFiles( List<File> files )
288         {
289             this.files = files;
290         }
291     }
292 }