001package org.apache.maven.scm.provider.git.jgit.command.changelog; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.scm.ChangeSet; 023import org.apache.maven.scm.ScmBranch; 024import org.apache.maven.scm.ScmException; 025import org.apache.maven.scm.ScmFileSet; 026import org.apache.maven.scm.ScmVersion; 027import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand; 028import org.apache.maven.scm.command.changelog.ChangeLogScmResult; 029import org.apache.maven.scm.command.changelog.ChangeLogSet; 030import org.apache.maven.scm.provider.ScmProviderRepository; 031import org.apache.maven.scm.provider.git.command.GitCommand; 032import org.apache.maven.scm.provider.git.jgit.command.JGitUtils; 033import org.eclipse.jgit.api.Git; 034import org.eclipse.jgit.errors.IncorrectObjectTypeException; 035import org.eclipse.jgit.errors.MissingObjectException; 036import org.eclipse.jgit.lib.Repository; 037import org.eclipse.jgit.revwalk.RevCommit; 038import org.eclipse.jgit.revwalk.RevSort; 039 040import java.io.File; 041import java.io.IOException; 042import java.util.ArrayList; 043import java.util.Date; 044import java.util.List; 045 046/** 047 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a> 048 * @author Dominik Bartholdi (imod) 049 * @since 1.9 050 */ 051public class JGitChangeLogCommand 052 extends AbstractChangeLogCommand 053 implements GitCommand 054{ 055 056 /** 057 * {@inheritDoc} 058 */ 059 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, 060 ScmVersion startVersion, ScmVersion endVersion, 061 String datePattern ) 062 throws ScmException 063 { 064 return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion ); 065 } 066 067 /** 068 * {@inheritDoc} 069 */ 070 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, 071 Date startDate, Date endDate, ScmBranch branch, 072 String datePattern ) 073 throws ScmException 074 { 075 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null ); 076 } 077 078 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, 079 Date startDate, Date endDate, ScmBranch branch, 080 String datePattern, ScmVersion startVersion, 081 ScmVersion endVersion ) 082 throws ScmException 083 { 084 Git git = null; 085 try 086 { 087 git = Git.open( fileSet.getBasedir() ); 088 089 String startRev = startVersion != null ? startVersion.getName() : null; 090 String endRev = endVersion != null ? endVersion.getName() : null; 091 092 List<ChangeEntry> gitChanges = 093 this.whatchanged( git.getRepository(), null, startRev, endRev, startDate, endDate, -1 ); 094 095 List<ChangeSet> modifications = new ArrayList<ChangeSet>( gitChanges.size() ); 096 097 for ( ChangeEntry change : gitChanges ) 098 { 099 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 /** 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}