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 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
73 Date startDate, Date endDate, ScmBranch branch,
74 String datePattern, ScmVersion startVersion,
75 ScmVersion endVersion )
76 throws ScmException
77 {
78 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, startVersion,
79 endVersion, null );
80 }
81
82 @Override
83 protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
84 throws ScmException
85 {
86 final ScmVersion startVersion = request.getStartRevision();
87 final ScmVersion endVersion = request.getEndRevision();
88 final ScmFileSet fileSet = request.getScmFileSet();
89 final String datePattern = request.getDatePattern();
90 return executeChangeLogCommand( request.getScmRepository().getProviderRepository(), fileSet,
91 request.getStartDate(), request.getEndDate(), request.getScmBranch(), datePattern, startVersion,
92 endVersion, request.getLimit() );
93 }
94
95 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
96 Date startDate, Date endDate, ScmBranch branch,
97 String datePattern, ScmVersion startVersion,
98 ScmVersion endVersion, Integer limit )
99 throws ScmException
100 {
101 Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet.getBasedir(), branch, startDate,
102 endDate, startVersion, endVersion, limit );
103
104 GitChangeLogConsumer consumer = new GitChangeLogConsumer( getLogger(), datePattern );
105
106 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
107
108 int exitCode;
109
110 exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
111 if ( exitCode != 0 )
112 {
113 return new ChangeLogScmResult( cl.toString(), "The git-log command failed.", stderr.getOutput(), false );
114 }
115 ChangeLogSet changeLogSet = new ChangeLogSet( consumer.getModifications(), startDate, endDate );
116 changeLogSet.setStartVersion( startVersion );
117 changeLogSet.setEndVersion( endVersion );
118
119 return new ChangeLogScmResult( cl.toString(), changeLogSet );
120 }
121
122
123
124
125
126
127
128
129
130
131 public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
132 ScmBranch branch, Date startDate, Date endDate,
133 ScmVersion startVersion, ScmVersion endVersion )
134 {
135 return createCommandLine( repository, workingDirectory, branch, startDate, endDate, startVersion, endVersion,
136 null );
137 }
138
139 static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
140 ScmBranch branch, Date startDate, Date endDate,
141 ScmVersion startVersion, ScmVersion endVersion, Integer limit )
142 {
143 SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
144 dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
145
146 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "whatchanged" );
147
148 if ( startDate != null || endDate != null )
149 {
150 if ( startDate != null )
151 {
152 cl.createArg().setValue( "--since=" + StringUtils.escape( dateFormat.format( startDate ) ) );
153 }
154
155 if ( endDate != null )
156 {
157 cl.createArg().setValue( "--until=" + StringUtils.escape( dateFormat.format( endDate ) ) );
158 }
159
160 }
161
162
163
164 cl.createArg().setValue( "--date=iso" );
165
166 if ( startVersion != null || endVersion != null )
167 {
168 StringBuilder versionRange = new StringBuilder();
169
170 if ( startVersion != null )
171 {
172 versionRange.append( StringUtils.escape( startVersion.getName() ) );
173 }
174
175 versionRange.append( ".." );
176
177 if ( endVersion != null )
178 {
179 versionRange.append( StringUtils.escape( endVersion.getName() ) );
180 }
181
182 cl.createArg().setValue( versionRange.toString() );
183
184 }
185
186 if ( limit != null && limit > 0 )
187 {
188 cl.createArg().setValue( "--max-count=" + limit );
189 }
190
191 if ( branch != null && branch.getName() != null && branch.getName().length() > 0 )
192 {
193 cl.createArg().setValue( branch.getName() );
194 }
195
196
197 cl.createArg().setValue( "--" );
198
199
200
201
202 cl.createArg().setFile( workingDirectory );
203
204 return cl;
205 }
206 }