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