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
43
44
45
46
47
48 public class GitChangeLogCommand
49 extends AbstractChangeLogCommand
50 implements GitCommand
51 {
52 private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
53
54
55 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
56 ScmVersion startVersion, ScmVersion endVersion,
57 String datePattern )
58 throws ScmException
59 {
60 return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
61 }
62
63
64 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
65 Date startDate, Date endDate, ScmBranch branch,
66 String datePattern )
67 throws ScmException
68 {
69 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
70 }
71
72 @Override
73 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
74 ScmVersion version, String datePattern )
75 throws ScmException
76 {
77 return executeChangeLogCommand( repository, fileSet, null, null, null, datePattern, null, null, null, version );
78 }
79
80 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
81 Date startDate, Date endDate, ScmBranch branch,
82 String datePattern, ScmVersion startVersion,
83 ScmVersion endVersion )
84 throws ScmException
85 {
86 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, startVersion,
87 endVersion, null, null );
88 }
89
90 @Override
91 protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
92 throws ScmException
93 {
94 final ScmVersion startVersion = request.getStartRevision();
95 final ScmVersion endVersion = request.getEndRevision();
96 final ScmVersion revision = request.getRevision();
97 final ScmFileSet fileSet = request.getScmFileSet();
98 final String datePattern = request.getDatePattern();
99 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
152
153
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
196
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
234 cl.createArg().setValue( "--" );
235
236
237
238
239 cl.createArg().setFile( workingDirectory );
240
241 return cl;
242 }
243 }