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