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
68
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
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
151
152 changes.add( ce );
153 }
154
155 return changes;
156 }
157
158
159
160
161 public static final class ChangeEntry
162 {
163 private String commitHash;
164
165 private String treeHash;
166
167 private String authorName;
168
169 private String authorEmail;
170
171 private Date authorDate;
172
173 private String committerName;
174
175 private String committerEmail;
176
177 private Date committerDate;
178
179 private String subject;
180
181 private String body;
182
183 private List<File> files;
184
185 public String getCommitHash()
186 {
187 return commitHash;
188 }
189
190 public void setCommitHash( String commitHash )
191 {
192 this.commitHash = commitHash;
193 }
194
195 public String getTreeHash()
196 {
197 return treeHash;
198 }
199
200 public void setTreeHash( String treeHash )
201 {
202 this.treeHash = treeHash;
203 }
204
205 public String getAuthorName()
206 {
207 return authorName;
208 }
209
210 public void setAuthorName( String authorName )
211 {
212 this.authorName = authorName;
213 }
214
215 public String getAuthorEmail()
216 {
217 return authorEmail;
218 }
219
220 public void setAuthorEmail( String authorEmail )
221 {
222 this.authorEmail = authorEmail;
223 }
224
225 public Date getAuthorDate()
226 {
227 return authorDate;
228 }
229
230 public void setAuthorDate( Date authorDate )
231 {
232 this.authorDate = authorDate;
233 }
234
235 public String getCommitterName()
236 {
237 return committerName;
238 }
239
240 public void setCommitterName( String committerName )
241 {
242 this.committerName = committerName;
243 }
244
245 public String getCommitterEmail()
246 {
247 return committerEmail;
248 }
249
250 public void setCommitterEmail( String committerEmail )
251 {
252 this.committerEmail = committerEmail;
253 }
254
255 public Date getCommitterDate()
256 {
257 return committerDate;
258 }
259
260 public void setCommitterDate( Date committerDate )
261 {
262 this.committerDate = committerDate;
263 }
264
265 public String getSubject()
266 {
267 return subject;
268 }
269
270 public void setSubject( String subject )
271 {
272 this.subject = subject;
273 }
274
275 public String getBody()
276 {
277 return body;
278 }
279
280 public void setBody( String body )
281 {
282 this.body = body;
283 }
284
285 public List<File> getFiles()
286 {
287 return files;
288 }
289
290 public void setFiles( List<File> files )
291 {
292 this.files = files;
293 }
294 }
295 }