1 package org.apache.maven.scm.provider.git.gitexe.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.ScmBranch;
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.ScmVersion;
26 import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
27 import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
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.repository.GitScmProviderRepository;
33 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.codehaus.plexus.util.cli.CommandLineUtils;
36 import org.codehaus.plexus.util.cli.Commandline;
37
38 import java.io.File;
39 import java.text.SimpleDateFormat;
40 import java.util.Date;
41 import java.util.TimeZone;
42 import org.apache.maven.scm.CommandParameter;
43 import org.apache.maven.scm.CommandParameters;
44 import org.apache.maven.scm.ScmResult;
45
46
47
48
49
50
51 public class GitChangeLogCommand
52 extends AbstractChangeLogCommand
53 implements GitCommand
54 {
55 private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
56
57 @Override
58 public ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
59 CommandParameters parameters )
60 throws ScmException
61 {
62 return executeChangeLogCommand( repository, fileSet,
63 parameters.getDate( CommandParameter.START_DATE, null ),
64 parameters.getDate( CommandParameter.END_DATE, null ),
65 (ScmBranch) parameters.getScmVersion( CommandParameter.BRANCH, null ),
66 parameters.getString( CommandParameter.CHANGELOG_DATE_PATTERN, null ),
67 parameters.getScmVersion( CommandParameter.START_SCM_VERSION, null ),
68 parameters.getScmVersion( CommandParameter.END_SCM_VERSION, null ),
69 parameters.getInt( CommandParameter.LIMIT, -1 ),
70 parameters.getScmVersion( CommandParameter.SCM_VERSION, null ) );
71 }
72
73
74 @Override
75 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
76 ScmVersion startVersion, ScmVersion endVersion,
77 String datePattern )
78 throws ScmException
79 {
80 return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
81 }
82
83
84 @Override
85 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
86 Date startDate, Date endDate, ScmBranch branch,
87 String datePattern )
88 throws ScmException
89 {
90 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
91 }
92
93 @Override
94 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
95 ScmVersion version, String datePattern )
96 throws ScmException
97 {
98 return executeChangeLogCommand( repository, fileSet, null, null, null, datePattern, null, null, null, version );
99 }
100
101 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
102 Date startDate, Date endDate, ScmBranch branch,
103 String datePattern, ScmVersion startVersion,
104 ScmVersion endVersion )
105 throws ScmException
106 {
107 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, startVersion,
108 endVersion, null, null );
109 }
110
111 @Override
112 protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
113 throws ScmException
114 {
115 final ScmVersion startVersion = request.getStartRevision();
116 final ScmVersion endVersion = request.getEndRevision();
117 final ScmVersion revision = request.getRevision();
118 final ScmFileSet fileSet = request.getScmFileSet();
119 final String datePattern = request.getDatePattern();
120 final ScmProviderRepository providerRepository = request.getScmRepository().getProviderRepository();
121 final Date startDate = request.getStartDate();
122 final Date endDate = request.getEndDate();
123 final ScmBranch branch = request.getScmBranch();
124 final Integer limit = request.getLimit();
125
126 return executeChangeLogCommand( providerRepository, fileSet, startDate, endDate, branch, datePattern,
127 startVersion, endVersion, limit, revision );
128 }
129
130 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
131 Date startDate, Date endDate, ScmBranch branch,
132 String datePattern, ScmVersion startVersion,
133 ScmVersion endVersion, Integer limit )
134 throws ScmException
135 {
136 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern,
137 startVersion, endVersion, limit, null );
138 }
139
140 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
141 Date startDate, Date endDate, ScmBranch branch,
142 String datePattern, ScmVersion startVersion,
143 ScmVersion endVersion, Integer limit, ScmVersion version )
144 throws ScmException
145 {
146 Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet.getBasedir(), branch, startDate,
147 endDate, startVersion, endVersion, limit, version );
148
149 GitChangeLogConsumer consumer = new GitChangeLogConsumer( datePattern );
150
151 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
152
153 int exitCode;
154
155 exitCode = GitCommandLineUtils.execute( cl, consumer, stderr );
156 if ( exitCode != 0 )
157 {
158 return new ChangeLogScmResult( cl.toString(), "The git-log command failed.", stderr.getOutput(), false );
159 }
160 ChangeLogSet changeLogSet = new ChangeLogSet( consumer.getModifications(), startDate, endDate );
161 changeLogSet.setStartVersion( startVersion );
162 changeLogSet.setEndVersion( endVersion );
163
164 return new ChangeLogScmResult( cl.toString(), changeLogSet );
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
187 ScmBranch branch, Date startDate, Date endDate,
188 ScmVersion startVersion, ScmVersion endVersion )
189 {
190 return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion,
191 null );
192 }
193
194 static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
195 ScmBranch branch, Date startDate, Date endDate,
196 ScmVersion startVersion, ScmVersion endVersion, Integer limit )
197 {
198 return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion,
199 limit, null );
200 }
201
202 static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
203 ScmBranch branch, Date startDate, Date endDate,
204 ScmVersion startVersion, ScmVersion endVersion, Integer limit,
205 ScmVersion version )
206 {
207 SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
208 dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
209
210 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "whatchanged" );
211 cl.createArg().setValue( "--format=medium" );
212 cl.createArg().setValue( "--decorate=short" );
213 cl.createArg().setValue( "--raw" );
214 cl.createArg().setValue( "--no-merges" );
215
216 if ( startDate != null || endDate != null )
217 {
218 if ( startDate != null )
219 {
220 cl.createArg().setValue( "--since=" + StringUtils.escape( dateFormat.format( startDate ) ) );
221 }
222
223 if ( endDate != null )
224 {
225 cl.createArg().setValue( "--until=" + StringUtils.escape( dateFormat.format( endDate ) ) );
226 }
227
228 }
229
230
231
232 cl.createArg().setValue( "--date=iso" );
233
234 if ( startVersion != null || endVersion != null )
235 {
236 StringBuilder versionRange = new StringBuilder();
237
238 if ( startVersion != null )
239 {
240 versionRange.append( StringUtils.escape( startVersion.getName() ) );
241 }
242
243 versionRange.append( ".." );
244
245 if ( endVersion != null )
246 {
247 versionRange.append( StringUtils.escape( endVersion.getName() ) );
248 }
249
250 cl.createArg().setValue( versionRange.toString() );
251
252 }
253 else if ( version != null )
254 {
255 cl.createArg().setValue( StringUtils.escape( version.getName() ) );
256 }
257
258 if ( limit != null && limit > 0 )
259 {
260 cl.createArg().setValue( "--max-count=" + limit );
261 }
262
263 if ( branch != null && branch.getName() != null && branch.getName().length() > 0 )
264 {
265 cl.createArg().setValue( branch.getName() );
266 }
267
268
269 cl.createArg().setValue( "--" );
270
271
272
273
274 cl.createArg().setValue( "." );
275
276 return cl;
277 }
278 }