View Javadoc
1   package org.apache.maven.scm.provider.git;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * SCM Provider for git
53   *
54   *
55   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
56   *
57   */
58  public abstract class AbstractGitScmProvider
59      extends AbstractScmProvider
60  {
61  
62      // ----------------------------------------------------------------------
63      //
64      // ----------------------------------------------------------------------
65  
66      /**
67       * Internal class
68       */
69      private static class ScmUrlParserResult
70      {
71          private List<String> messages = new ArrayList<String>();
72  
73          private ScmProviderRepository repository;
74      }
75  
76      // ----------------------------------------------------------------------
77      // ScmProvider Implementation
78      // ----------------------------------------------------------------------
79  
80      /** {@inheritDoc} */
81      public String getScmSpecificFilename()
82      {
83          return ".git";
84      }
85  
86      /** {@inheritDoc} */
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             // XXX We should allow throwing of SCMException.
104             throw new ScmRepositoryException( "Error creating the scm repository", e );
105         }
106     }
107 
108     /** {@inheritDoc} */
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             // XXX We should allow throwing of SCMException.
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
157     public String getScmType()
158     {
159         return "git";
160     }
161 
162     // ----------------------------------------------------------------------
163     //
164     // ----------------------------------------------------------------------
165 
166     /**
167      * The git-submodule(1) command is available since Git 1.5.3, so modules will
168      * be activated in a later stage
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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 }