1 package org.apache.maven.scm.provider.git.jgit.command.changelog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51 public class JGitChangeLogCommand
52 extends AbstractChangeLogCommand
53 implements GitCommand
54 {
55
56
57
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 @Override
68 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
69 ScmVersion version, String datePattern )
70 throws ScmException
71 {
72 return executeChangeLogCommand( repository, fileSet, null, null, null, datePattern, null, null, version );
73 }
74
75
76
77
78 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
79 Date startDate, Date endDate, ScmBranch branch,
80 String datePattern )
81 throws ScmException
82 {
83 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
84 }
85
86 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
87 Date startDate, Date endDate, ScmBranch branch,
88 String datePattern, ScmVersion startVersion,
89 ScmVersion endVersion )
90 throws ScmException
91 {
92 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern,
93 startVersion, endVersion, null );
94 }
95
96 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
97 Date startDate, Date endDate, ScmBranch branch,
98 String datePattern, ScmVersion startVersion,
99 ScmVersion endVersion, ScmVersion version )
100 throws ScmException
101 {
102 Git git = null;
103 boolean isARangeChangeLog = startVersion != null || endVersion != null;
104
105 try
106 {
107 git = JGitUtils.openRepo( fileSet.getBasedir() );
108
109 boolean versionOnly = startVersion == null && endVersion == null && version != null;
110
111 String startRev = null;
112 String endRev = null;
113
114 if ( versionOnly )
115 {
116 startRev = null;
117 endRev = version.getName();
118 }
119 else
120 {
121 startRev = startVersion != null ? startVersion.getName() : ( isARangeChangeLog ? "HEAD" : null );
122 endRev = endVersion != null ? endVersion.getName() : ( isARangeChangeLog ? "HEAD" : null );
123 }
124
125 List<ChangeEntry> gitChanges =
126 this.whatchanged( git.getRepository(), null, startRev, endRev, startDate, endDate, -1 );
127
128 List<ChangeSet> modifications = new ArrayList<ChangeSet>( gitChanges.size() );
129
130 for ( ChangeEntry change : gitChanges )
131 {
132 ChangeSet scmChange = new ChangeSet();
133
134 scmChange.setAuthor( change.getAuthorName() );
135 scmChange.setComment( change.getBody() );
136 scmChange.setDate( change.getAuthorDate() );
137 scmChange.setRevision( change.getCommitHash() );
138
139
140 modifications.add( scmChange );
141 }
142
143 ChangeLogSet changeLogSet = new ChangeLogSet( modifications, startDate, endDate );
144 changeLogSet.setStartVersion( startVersion );
145 changeLogSet.setEndVersion( endVersion );
146
147 return new ChangeLogScmResult( "JGit changelog", changeLogSet );
148 }
149 catch ( Exception e )
150 {
151 throw new ScmException( "JGit changelog failure!", e );
152 }
153 finally
154 {
155 JGitUtils.closeRepo( git );
156 }
157 }
158
159 public List<ChangeEntry> whatchanged( Repository repo, RevSort[] sortings, String fromRev, String toRev,
160 Date fromDate, Date toDate, int maxLines )
161 throws MissingObjectException, IncorrectObjectTypeException, IOException
162 {
163 List<RevCommit> revs = JGitUtils.getRevCommits( repo, sortings, fromRev, toRev, fromDate, toDate, maxLines );
164 List<ChangeEntry> changes = new ArrayList<ChangeEntry>( revs.size() );
165
166 if ( fromRev != null && fromRev.equals( toRev ) )
167 {
168
169 return changes;
170 }
171
172 for ( RevCommit c : revs )
173 {
174 ChangeEntry ce = new ChangeEntry();
175
176 ce.setAuthorDate( c.getAuthorIdent().getWhen() );
177 ce.setAuthorEmail( c.getAuthorIdent().getEmailAddress() );
178 ce.setAuthorName( c.getAuthorIdent().getName() );
179 ce.setCommitterDate( c.getCommitterIdent().getWhen() );
180 ce.setCommitterEmail( c.getCommitterIdent().getEmailAddress() );
181 ce.setCommitterName( c.getCommitterIdent().getName() );
182
183 ce.setSubject( c.getShortMessage() );
184 ce.setBody( c.getFullMessage() );
185
186 ce.setCommitHash( c.getId().name() );
187 ce.setTreeHash( c.getTree().getId().name() );
188
189
190
191 changes.add( ce );
192 }
193
194 return changes;
195 }
196
197
198
199
200 public static final class ChangeEntry
201 {
202 private String commitHash;
203
204 private String treeHash;
205
206 private String authorName;
207
208 private String authorEmail;
209
210 private Date authorDate;
211
212 private String committerName;
213
214 private String committerEmail;
215
216 private Date committerDate;
217
218 private String subject;
219
220 private String body;
221
222 private List<File> files;
223
224 public String getCommitHash()
225 {
226 return commitHash;
227 }
228
229 public void setCommitHash( String commitHash )
230 {
231 this.commitHash = commitHash;
232 }
233
234 public String getTreeHash()
235 {
236 return treeHash;
237 }
238
239 public void setTreeHash( String treeHash )
240 {
241 this.treeHash = treeHash;
242 }
243
244 public String getAuthorName()
245 {
246 return authorName;
247 }
248
249 public void setAuthorName( String authorName )
250 {
251 this.authorName = authorName;
252 }
253
254 public String getAuthorEmail()
255 {
256 return authorEmail;
257 }
258
259 public void setAuthorEmail( String authorEmail )
260 {
261 this.authorEmail = authorEmail;
262 }
263
264 public Date getAuthorDate()
265 {
266 return authorDate;
267 }
268
269 public void setAuthorDate( Date authorDate )
270 {
271 this.authorDate = authorDate;
272 }
273
274 public String getCommitterName()
275 {
276 return committerName;
277 }
278
279 public void setCommitterName( String committerName )
280 {
281 this.committerName = committerName;
282 }
283
284 public String getCommitterEmail()
285 {
286 return committerEmail;
287 }
288
289 public void setCommitterEmail( String committerEmail )
290 {
291 this.committerEmail = committerEmail;
292 }
293
294 public Date getCommitterDate()
295 {
296 return committerDate;
297 }
298
299 public void setCommitterDate( Date committerDate )
300 {
301 this.committerDate = committerDate;
302 }
303
304 public String getSubject()
305 {
306 return subject;
307 }
308
309 public void setSubject( String subject )
310 {
311 this.subject = subject;
312 }
313
314 public String getBody()
315 {
316 return body;
317 }
318
319 public void setBody( String body )
320 {
321 this.body = body;
322 }
323
324 public List<File> getFiles()
325 {
326 return files;
327 }
328
329 public void setFiles( List<File> files )
330 {
331 this.files = files;
332 }
333 }
334 }