1 package org.apache.maven.scm.provider.git;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.scm.CommandParameters;
27 import org.apache.maven.scm.ScmException;
28 import org.apache.maven.scm.ScmFileSet;
29 import org.apache.maven.scm.ScmResult;
30 import org.apache.maven.scm.command.add.AddScmResult;
31 import org.apache.maven.scm.command.blame.BlameScmResult;
32 import org.apache.maven.scm.command.branch.BranchScmResult;
33 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
34 import org.apache.maven.scm.command.checkin.CheckInScmResult;
35 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
36 import org.apache.maven.scm.command.diff.DiffScmResult;
37 import org.apache.maven.scm.command.export.ExportScmResult;
38 import org.apache.maven.scm.command.info.InfoScmResult;
39 import org.apache.maven.scm.command.list.ListScmResult;
40 import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
41 import org.apache.maven.scm.command.remove.RemoveScmResult;
42 import org.apache.maven.scm.command.status.StatusScmResult;
43 import org.apache.maven.scm.command.tag.TagScmResult;
44 import org.apache.maven.scm.command.update.UpdateScmResult;
45 import org.apache.maven.scm.provider.AbstractScmProvider;
46 import org.apache.maven.scm.provider.ScmProviderRepository;
47 import org.apache.maven.scm.provider.git.command.GitCommand;
48 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
49 import org.apache.maven.scm.repository.ScmRepositoryException;
50 import org.apache.maven.scm.repository.UnknownRepositoryStructure;
51
52
53
54
55
56
57
58
59 public abstract class AbstractGitScmProvider
60 extends AbstractScmProvider
61 {
62
63
64
65
66
67
68
69
70 private static class ScmUrlParserResult
71 {
72 private List<String> messages = new ArrayList<String>();
73
74 private ScmProviderRepository repository;
75 }
76
77
78
79
80
81
82 public String getScmSpecificFilename()
83 {
84 return ".git";
85 }
86
87
88 public ScmProviderRepository makeProviderScmRepository( String scmSpecificUrl, char delimiter )
89 throws ScmRepositoryException
90 {
91 try
92 {
93 ScmUrlParserResult result = parseScmUrl( scmSpecificUrl, delimiter );
94
95 if ( result.messages.size() > 0 )
96 {
97 throw new ScmRepositoryException( "The scm url " + scmSpecificUrl + " is invalid.", result.messages );
98 }
99
100 return result.repository;
101 }
102 catch ( ScmException e )
103 {
104
105 throw new ScmRepositoryException( "Error creating the scm repository", e );
106 }
107 }
108
109
110 public ScmProviderRepository makeProviderScmRepository( File path )
111 throws ScmRepositoryException, UnknownRepositoryStructure
112 {
113 if ( path == null )
114 {
115 throw new NullPointerException( "Path argument is null" );
116 }
117
118 if ( !path.isDirectory() )
119 {
120 throw new ScmRepositoryException( path.getAbsolutePath() + " isn't a valid directory." );
121 }
122
123 if ( !new File( path, ".git" ).exists() )
124 {
125 throw new ScmRepositoryException( path.getAbsolutePath() + " isn't a git checkout directory." );
126 }
127
128 try
129 {
130 return makeProviderScmRepository( getRepositoryURL( path ), ':' );
131 }
132 catch ( ScmException e )
133 {
134
135 throw new ScmRepositoryException( "Error creating the scm repository", e );
136 }
137 }
138
139 protected abstract String getRepositoryURL( File path )
140 throws ScmException;
141
142
143 public List<String> validateScmUrl( String scmSpecificUrl, char delimiter )
144 {
145 List<String> messages = new ArrayList<String>();
146 try
147 {
148 makeProviderScmRepository( scmSpecificUrl, delimiter );
149 }
150 catch ( ScmRepositoryException e )
151 {
152 messages = e.getValidationMessages();
153 }
154 return messages;
155 }
156
157
158 public String getScmType()
159 {
160 return "git";
161 }
162
163
164
165
166
167
168
169
170
171 private ScmUrlParserResult parseScmUrl( String scmSpecificUrl, char delimiter )
172 throws ScmException
173 {
174 ScmUrlParserResult result = new ScmUrlParserResult();
175
176 result.repository = new GitScmProviderRepository( scmSpecificUrl );
177
178 return result;
179 }
180
181 protected abstract GitCommand getAddCommand();
182
183
184 public AddScmResult add( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
185 throws ScmException
186 {
187 return (AddScmResult) executeCommand( getAddCommand(), repository, fileSet, parameters );
188 }
189
190 protected abstract GitCommand getBranchCommand();
191
192
193 protected BranchScmResult branch( ScmProviderRepository repository, ScmFileSet fileSet,
194 CommandParameters parameters )
195 throws ScmException
196 {
197 return (BranchScmResult) executeCommand( getBranchCommand(), repository, fileSet, parameters );
198 }
199
200 protected abstract GitCommand getChangeLogCommand();
201
202
203 public ChangeLogScmResult changelog( ScmProviderRepository repository, ScmFileSet fileSet,
204 CommandParameters parameters )
205 throws ScmException
206 {
207 return (ChangeLogScmResult) executeCommand( getChangeLogCommand(), repository, fileSet, parameters );
208 }
209
210 protected abstract GitCommand getCheckInCommand();
211
212
213 public CheckInScmResult checkin( ScmProviderRepository repository, ScmFileSet fileSet,
214 CommandParameters parameters )
215 throws ScmException
216 {
217 return (CheckInScmResult) executeCommand( getCheckInCommand(), repository, fileSet, parameters );
218 }
219
220 protected abstract GitCommand getCheckOutCommand();
221
222
223 public CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet,
224 CommandParameters parameters )
225 throws ScmException
226 {
227 return (CheckOutScmResult) executeCommand( getCheckOutCommand(), repository, fileSet, parameters );
228 }
229
230 protected abstract GitCommand getDiffCommand();
231
232
233 public DiffScmResult diff( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
234 throws ScmException
235 {
236 return (DiffScmResult) executeCommand( getDiffCommand(), repository, fileSet, parameters );
237 }
238
239 protected abstract GitCommand getExportCommand();
240
241
242 protected ExportScmResult export( ScmProviderRepository repository, ScmFileSet fileSet,
243 CommandParameters parameters )
244 throws ScmException
245 {
246 return (ExportScmResult) executeCommand( getExportCommand(), repository, fileSet, parameters );
247 }
248
249 protected abstract GitCommand getRemoveCommand();
250
251
252 public RemoveScmResult remove( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
253 throws ScmException
254 {
255 return (RemoveScmResult) executeCommand( getRemoveCommand(), repository, fileSet, parameters );
256 }
257
258 protected abstract GitCommand getStatusCommand();
259
260
261 public StatusScmResult status( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
262 throws ScmException
263 {
264 return (StatusScmResult) executeCommand( getStatusCommand(), repository, fileSet, parameters );
265 }
266
267 protected abstract GitCommand getTagCommand();
268
269
270 public TagScmResult tag( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
271 throws ScmException
272 {
273 return (TagScmResult) executeCommand( getTagCommand(), repository, fileSet, parameters );
274 }
275
276 protected abstract GitCommand getUpdateCommand();
277
278
279 public UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
280 throws ScmException
281 {
282 return (UpdateScmResult) executeCommand( getUpdateCommand(), repository, fileSet, parameters );
283 }
284
285 protected ScmResult executeCommand( GitCommand command, ScmProviderRepository repository, ScmFileSet fileSet,
286 CommandParameters parameters )
287 throws ScmException
288 {
289 command.setLogger( getLogger() );
290
291 return command.execute( repository, fileSet, parameters );
292 }
293
294 protected abstract GitCommand getListCommand();
295
296
297 public ListScmResult list( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
298 throws ScmException
299 {
300 GitCommand cmd = getListCommand();
301
302 return (ListScmResult) executeCommand( cmd, repository, fileSet, parameters );
303 }
304
305 protected abstract GitCommand getInfoCommand();
306
307 public InfoScmResult info( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
308 throws ScmException
309 {
310 GitCommand cmd = getInfoCommand();
311
312 return (InfoScmResult) executeCommand( cmd, repository, fileSet, parameters );
313 }
314
315
316 protected BlameScmResult blame( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
317 throws ScmException
318 {
319 GitCommand cmd = getBlameCommand();
320
321 return (BlameScmResult) executeCommand( cmd, repository, fileSet, parameters );
322 }
323
324 protected abstract GitCommand getBlameCommand();
325
326
327 public RemoteInfoScmResult remoteInfo( ScmProviderRepository repository, ScmFileSet fileSet,
328 CommandParameters parameters )
329 throws ScmException
330 {
331 GitCommand cmd = getRemoteInfoCommand();
332
333 return (RemoteInfoScmResult) executeCommand( cmd, repository, fileSet, parameters );
334 }
335
336 protected abstract GitCommand getRemoteInfoCommand();
337
338 }