View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.hg;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.maven.scm.CommandParameters;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.command.add.AddScmResult;
32  import org.apache.maven.scm.command.blame.BlameScmResult;
33  import org.apache.maven.scm.command.branch.BranchScmResult;
34  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
35  import org.apache.maven.scm.command.checkin.CheckInScmResult;
36  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
37  import org.apache.maven.scm.command.diff.DiffScmResult;
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.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.hg.command.add.HgAddCommand;
47  import org.apache.maven.scm.provider.hg.command.blame.HgBlameCommand;
48  import org.apache.maven.scm.provider.hg.command.branch.HgBranchCommand;
49  import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand;
50  import org.apache.maven.scm.provider.hg.command.checkin.HgCheckInCommand;
51  import org.apache.maven.scm.provider.hg.command.checkout.HgCheckOutCommand;
52  import org.apache.maven.scm.provider.hg.command.diff.HgDiffCommand;
53  import org.apache.maven.scm.provider.hg.command.info.HgInfoCommand;
54  import org.apache.maven.scm.provider.hg.command.inventory.HgListCommand;
55  import org.apache.maven.scm.provider.hg.command.remove.HgRemoveCommand;
56  import org.apache.maven.scm.provider.hg.command.status.HgStatusCommand;
57  import org.apache.maven.scm.provider.hg.command.tag.HgTagCommand;
58  import org.apache.maven.scm.provider.hg.command.update.HgUpdateCommand;
59  import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
60  import org.apache.maven.scm.repository.ScmRepositoryException;
61  import org.apache.maven.scm.repository.UnknownRepositoryStructure;
62  
63  /**
64   * Mercurial (HG) is a decentralized revision control system.
65   * <a href="http://www.selenic.com/mercurial">http://www.selenic.com/mercurial</a>
66   *
67   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
68   */
69  @Singleton
70  @Named("hg")
71  public class HgScmProvider extends AbstractScmProvider {
72      /** {@inheritDoc} */
73      public String getScmSpecificFilename() {
74          return ".hg";
75      }
76  
77      private static class HgUrlParserResult {
78          private final List<String> messages = new ArrayList<>();
79  
80          private ScmProviderRepository repository;
81      }
82  
83      /** {@inheritDoc} */
84      public ScmProviderRepository makeProviderScmRepository(String scmSpecificUrl, char delimiter)
85              throws ScmRepositoryException {
86          HgUrlParserResult result = parseScmUrl(scmSpecificUrl);
87  
88          if (result.messages.size() > 0) {
89              throw new ScmRepositoryException("The scm url is invalid.", result.messages);
90          }
91  
92          return result.repository;
93      }
94  
95      private HgUrlParserResult parseScmUrl(String scmSpecificUrl) {
96          HgUrlParserResult result = new HgUrlParserResult();
97  
98          // ----------------------------------------------------------------------
99          // Do some sanity checking of the SVN url
100         // ----------------------------------------------------------------------
101 
102         if (scmSpecificUrl.startsWith("file")) {
103             if (!scmSpecificUrl.startsWith("file:///") && !scmSpecificUrl.startsWith("file://localhost/")) {
104                 result.messages.add("An hg 'file' url must be on the form 'file:///' or 'file://localhost/'.");
105 
106                 return result;
107             }
108         } else if (scmSpecificUrl.startsWith("https")) {
109             if (!scmSpecificUrl.startsWith("https://")) {
110                 result.messages.add("An hg 'http' url must be on the form 'https://'.");
111 
112                 return result;
113             }
114         } else if (scmSpecificUrl.startsWith("http")) {
115             if (!scmSpecificUrl.startsWith("http://")) {
116                 result.messages.add("An hg 'http' url must be on the form 'http://'.");
117 
118                 return result;
119             }
120         } else {
121             try {
122                 @SuppressWarnings("unused")
123                 File file = new File(scmSpecificUrl);
124             } catch (Throwable e) {
125                 result.messages.add("The filename provided is not valid");
126 
127                 return result;
128             }
129         }
130 
131         result.repository = new HgScmProviderRepository(scmSpecificUrl);
132 
133         return result;
134     }
135 
136     /** {@inheritDoc} */
137     public ScmProviderRepository makeProviderScmRepository(File path)
138             throws ScmRepositoryException, UnknownRepositoryStructure {
139         if (path == null) {
140             throw new NullPointerException("Path argument is null");
141         }
142 
143         if (!path.isDirectory()) {
144             throw new ScmRepositoryException(path.getAbsolutePath() + " isn't a valid directory.");
145         }
146 
147         File hgDir = new File(path, ".hg");
148 
149         if (!hgDir.exists()) {
150             throw new ScmRepositoryException(path.getAbsolutePath() + " isn't a hg directory.");
151         }
152 
153         return makeProviderScmRepository(path.getAbsolutePath(), ':');
154     }
155 
156     /** {@inheritDoc} */
157     public List<String> validateScmUrl(String scmSpecificUrl, char delimiter) {
158         HgUrlParserResult result = parseScmUrl(scmSpecificUrl);
159 
160         return result.messages;
161     }
162 
163     /** {@inheritDoc} */
164     public String getScmType() {
165         return "hg";
166     }
167 
168     /** {@inheritDoc} */
169     public AddScmResult add(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
170             throws ScmException {
171         HgAddCommand command = new HgAddCommand();
172 
173         return (AddScmResult) command.execute(repository, fileSet, parameters);
174     }
175 
176     /** {@inheritDoc} */
177     public ChangeLogScmResult changelog(
178             ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
179         HgChangeLogCommand command = new HgChangeLogCommand();
180 
181         return (ChangeLogScmResult) command.execute(repository, fileSet, parameters);
182     }
183 
184     /** {@inheritDoc} */
185     public CheckInScmResult checkin(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
186             throws ScmException {
187         HgCheckInCommand command = new HgCheckInCommand();
188 
189         return (CheckInScmResult) command.execute(repository, fileSet, parameters);
190     }
191 
192     /** {@inheritDoc} */
193     public CheckOutScmResult checkout(
194             ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
195         HgCheckOutCommand command = new HgCheckOutCommand();
196 
197         return (CheckOutScmResult) command.execute(repository, fileSet, parameters);
198     }
199 
200     /** {@inheritDoc} */
201     public TagScmResult tag(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
202             throws ScmException {
203         HgTagCommand command = new HgTagCommand();
204 
205         return (TagScmResult) command.execute(repository, fileSet, parameters);
206     }
207 
208     /** {@inheritDoc} */
209     public DiffScmResult diff(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
210             throws ScmException {
211         HgDiffCommand command = new HgDiffCommand();
212 
213         return (DiffScmResult) command.execute(repository, fileSet, parameters);
214     }
215 
216     /** {@inheritDoc} */
217     public RemoveScmResult remove(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
218             throws ScmException {
219         HgRemoveCommand command = new HgRemoveCommand();
220 
221         return (RemoveScmResult) command.execute(repository, fileSet, parameters);
222     }
223 
224     /** {@inheritDoc} */
225     public StatusScmResult status(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
226             throws ScmException {
227         HgStatusCommand command = new HgStatusCommand();
228 
229         return (StatusScmResult) command.execute(repository, fileSet, parameters);
230     }
231 
232     /** {@inheritDoc} */
233     public UpdateScmResult update(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
234             throws ScmException {
235         HgUpdateCommand command = new HgUpdateCommand();
236 
237         return (UpdateScmResult) command.execute(repository, fileSet, parameters);
238     }
239 
240     /** {@inheritDoc} */
241     protected BlameScmResult blame(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
242             throws ScmException {
243         HgBlameCommand command = new HgBlameCommand();
244 
245         return (BlameScmResult) command.execute(repository, fileSet, parameters);
246     }
247 
248     /** {@inheritDoc} */
249     public BranchScmResult branch(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
250             throws ScmException {
251         HgBranchCommand command = new HgBranchCommand();
252 
253         return (BranchScmResult) command.execute(repository, fileSet, parameters);
254     }
255 
256     /**
257      * @since 1.5
258      */
259     @Override
260     protected ListScmResult list(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
261             throws ScmException {
262         HgListCommand hgListCommand = new HgListCommand();
263 
264         return (ListScmResult) hgListCommand.executeCommand(repository, fileSet, parameters);
265     }
266 
267     /**
268      * returns result of hg id -i
269      * @since 1.5
270      * @see org.apache.maven.scm.provider.AbstractScmProvider#info(org.apache.maven.scm.provider.ScmProviderRepository, org.apache.maven.scm.ScmFileSet, org.apache.maven.scm.CommandParameters)
271      */
272     @Override
273     public InfoScmResult info(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
274             throws ScmException {
275         HgInfoCommand infoCommand = new HgInfoCommand();
276 
277         return (InfoScmResult) infoCommand.execute(repository, fileSet, parameters);
278     }
279 }