001package org.apache.maven.scm.provider.git.gitexe.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.ScmBranch; 023import org.apache.maven.scm.ScmException; 024import org.apache.maven.scm.ScmFileSet; 025import org.apache.maven.scm.ScmVersion; 026import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand; 027import org.apache.maven.scm.command.changelog.ChangeLogScmRequest; 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.repository.GitScmProviderRepository; 033import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 034import org.codehaus.plexus.util.StringUtils; 035import org.codehaus.plexus.util.cli.CommandLineUtils; 036import org.codehaus.plexus.util.cli.Commandline; 037 038import java.io.File; 039import java.text.SimpleDateFormat; 040import java.util.Date; 041import java.util.TimeZone; 042 043/** 044 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> 045 * @author Olivier Lamy 046 * 047 */ 048public class GitChangeLogCommand 049 extends AbstractChangeLogCommand 050 implements GitCommand 051{ 052 private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z"; 053 054 /** {@inheritDoc} */ 055 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, 056 ScmVersion startVersion, ScmVersion endVersion, 057 String datePattern ) 058 throws ScmException 059 { 060 return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion ); 061 } 062 063 /** {@inheritDoc} */ 064 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, 065 Date startDate, Date endDate, ScmBranch branch, 066 String datePattern ) 067 throws ScmException 068 { 069 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null ); 070 } 071 072 @Override 073 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet, 074 ScmVersion version, String datePattern ) 075 throws ScmException 076 { 077 return executeChangeLogCommand( repository, fileSet, null, null, null, datePattern, null, null, null, version ); 078 } 079 080 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, 081 Date startDate, Date endDate, ScmBranch branch, 082 String datePattern, ScmVersion startVersion, 083 ScmVersion endVersion ) 084 throws ScmException 085 { 086 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, startVersion, 087 endVersion, null, null ); 088 } 089 090 @Override 091 protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request ) 092 throws ScmException 093 { 094 final ScmVersion startVersion = request.getStartRevision(); 095 final ScmVersion endVersion = request.getEndRevision(); 096 final ScmVersion revision = request.getRevision(); 097 final ScmFileSet fileSet = request.getScmFileSet(); 098 final String datePattern = request.getDatePattern(); 099 final ScmProviderRepository providerRepository = request.getScmRepository().getProviderRepository(); 100 final Date startDate = request.getStartDate(); 101 final Date endDate = request.getEndDate(); 102 final ScmBranch branch = request.getScmBranch(); 103 final Integer limit = request.getLimit(); 104 105 return executeChangeLogCommand( providerRepository, fileSet, startDate, endDate, branch, datePattern, 106 startVersion, endVersion, limit, revision ); 107 } 108 109 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, 110 Date startDate, Date endDate, ScmBranch branch, 111 String datePattern, ScmVersion startVersion, 112 ScmVersion endVersion, Integer limit ) 113 throws ScmException 114 { 115 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, 116 startVersion, endVersion, limit, null ); 117 } 118 119 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, 120 Date startDate, Date endDate, ScmBranch branch, 121 String datePattern, ScmVersion startVersion, 122 ScmVersion endVersion, Integer limit, ScmVersion version ) 123 throws ScmException 124 { 125 Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet.getBasedir(), branch, startDate, 126 endDate, startVersion, endVersion, limit, version ); 127 128 GitChangeLogConsumer consumer = new GitChangeLogConsumer( getLogger(), datePattern ); 129 130 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 131 132 int exitCode; 133 134 exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() ); 135 if ( exitCode != 0 ) 136 { 137 return new ChangeLogScmResult( cl.toString(), "The git-log command failed.", stderr.getOutput(), false ); 138 } 139 ChangeLogSet changeLogSet = new ChangeLogSet( consumer.getModifications(), startDate, endDate ); 140 changeLogSet.setStartVersion( startVersion ); 141 changeLogSet.setEndVersion( endVersion ); 142 143 return new ChangeLogScmResult( cl.toString(), changeLogSet ); 144 } 145 146 // ---------------------------------------------------------------------- 147 // 148 // ---------------------------------------------------------------------- 149 150 /** 151 * this constructs creates the commandline for the git-whatchanged command. 152 * Since it uses --since and --until for the start and end date, the branch 153 * and version parameters can be used simultanously. 154 */ 155 public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory, 156 ScmBranch branch, Date startDate, Date endDate, 157 ScmVersion startVersion, ScmVersion endVersion ) 158 { 159 return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion, 160 null ); 161 } 162 163 static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory, 164 ScmBranch branch, Date startDate, Date endDate, 165 ScmVersion startVersion, ScmVersion endVersion, Integer limit ) 166 { 167 return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion, 168 limit, null ); 169 } 170 171 static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory, 172 ScmBranch branch, Date startDate, Date endDate, 173 ScmVersion startVersion, ScmVersion endVersion, Integer limit, 174 ScmVersion version ) 175 { 176 SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT ); 177 dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); 178 179 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "whatchanged" ); 180 181 if ( startDate != null || endDate != null ) 182 { 183 if ( startDate != null ) 184 { 185 cl.createArg().setValue( "--since=" + StringUtils.escape( dateFormat.format( startDate ) ) ); 186 } 187 188 if ( endDate != null ) 189 { 190 cl.createArg().setValue( "--until=" + StringUtils.escape( dateFormat.format( endDate ) ) ); 191 } 192 193 } 194 195 // since this parameter is also used for the output formatting, we need it also if no start nor end date is 196 // given 197 cl.createArg().setValue( "--date=iso" ); 198 199 if ( startVersion != null || endVersion != null ) 200 { 201 StringBuilder versionRange = new StringBuilder(); 202 203 if ( startVersion != null ) 204 { 205 versionRange.append( StringUtils.escape( startVersion.getName() ) ); 206 } 207 208 versionRange.append( ".." ); 209 210 if ( endVersion != null ) 211 { 212 versionRange.append( StringUtils.escape( endVersion.getName() ) ); 213 } 214 215 cl.createArg().setValue( versionRange.toString() ); 216 217 } 218 else if ( version != null ) 219 { 220 cl.createArg().setValue( StringUtils.escape( version.getName() ) ); 221 } 222 223 if ( limit != null && limit > 0 ) 224 { 225 cl.createArg().setValue( "--max-count=" + limit ); 226 } 227 228 if ( branch != null && branch.getName() != null && branch.getName().length() > 0 ) 229 { 230 cl.createArg().setValue( branch.getName() ); 231 } 232 233 // Insert a separator to make sure that files aren't interpreted as part of the version spec 234 cl.createArg().setValue( "--" ); 235 236 // We have to report only the changes of the current project. 237 // This is needed for child projects, otherwise we would get the changelog of the 238 // whole parent-project including all childs. 239 cl.createArg().setValue( "." ); 240 241 return cl; 242 } 243}