001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.hg;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import java.io.File;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.apache.maven.scm.CommandParameters;
029import org.apache.maven.scm.ScmException;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.command.add.AddScmResult;
032import org.apache.maven.scm.command.blame.BlameScmResult;
033import org.apache.maven.scm.command.branch.BranchScmResult;
034import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
035import org.apache.maven.scm.command.checkin.CheckInScmResult;
036import org.apache.maven.scm.command.checkout.CheckOutScmResult;
037import org.apache.maven.scm.command.diff.DiffScmResult;
038import org.apache.maven.scm.command.info.InfoScmResult;
039import org.apache.maven.scm.command.list.ListScmResult;
040import org.apache.maven.scm.command.remove.RemoveScmResult;
041import org.apache.maven.scm.command.status.StatusScmResult;
042import org.apache.maven.scm.command.tag.TagScmResult;
043import org.apache.maven.scm.command.update.UpdateScmResult;
044import org.apache.maven.scm.provider.AbstractScmProvider;
045import org.apache.maven.scm.provider.ScmProviderRepository;
046import org.apache.maven.scm.provider.hg.command.add.HgAddCommand;
047import org.apache.maven.scm.provider.hg.command.blame.HgBlameCommand;
048import org.apache.maven.scm.provider.hg.command.branch.HgBranchCommand;
049import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand;
050import org.apache.maven.scm.provider.hg.command.checkin.HgCheckInCommand;
051import org.apache.maven.scm.provider.hg.command.checkout.HgCheckOutCommand;
052import org.apache.maven.scm.provider.hg.command.diff.HgDiffCommand;
053import org.apache.maven.scm.provider.hg.command.info.HgInfoCommand;
054import org.apache.maven.scm.provider.hg.command.inventory.HgListCommand;
055import org.apache.maven.scm.provider.hg.command.remove.HgRemoveCommand;
056import org.apache.maven.scm.provider.hg.command.status.HgStatusCommand;
057import org.apache.maven.scm.provider.hg.command.tag.HgTagCommand;
058import org.apache.maven.scm.provider.hg.command.update.HgUpdateCommand;
059import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
060import org.apache.maven.scm.repository.ScmRepositoryException;
061import org.apache.maven.scm.repository.UnknownRepositoryStructure;
062
063/**
064 * Mercurial (HG) is a decentralized revision control system.
065 * <a href="http://www.selenic.com/mercurial">http://www.selenic.com/mercurial</a>
066 *
067 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
068 */
069@Singleton
070@Named("hg")
071public class HgScmProvider extends AbstractScmProvider {
072    /** {@inheritDoc} */
073    public String getScmSpecificFilename() {
074        return ".hg";
075    }
076
077    private static class HgUrlParserResult {
078        private final List<String> messages = new ArrayList<>();
079
080        private ScmProviderRepository repository;
081    }
082
083    /** {@inheritDoc} */
084    public ScmProviderRepository makeProviderScmRepository(String scmSpecificUrl, char delimiter)
085            throws ScmRepositoryException {
086        HgUrlParserResult result = parseScmUrl(scmSpecificUrl);
087
088        if (result.messages.size() > 0) {
089            throw new ScmRepositoryException("The scm url is invalid.", result.messages);
090        }
091
092        return result.repository;
093    }
094
095    private HgUrlParserResult parseScmUrl(String scmSpecificUrl) {
096        HgUrlParserResult result = new HgUrlParserResult();
097
098        // ----------------------------------------------------------------------
099        // 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                new File(scmSpecificUrl);
123            } catch (Throwable e) {
124                result.messages.add("The filename provided is not valid");
125
126                return result;
127            }
128        }
129
130        result.repository = new HgScmProviderRepository(scmSpecificUrl);
131
132        return result;
133    }
134
135    /** {@inheritDoc} */
136    public ScmProviderRepository makeProviderScmRepository(File path)
137            throws ScmRepositoryException, UnknownRepositoryStructure {
138        if (path == null) {
139            throw new NullPointerException("Path argument is null");
140        }
141
142        if (!path.isDirectory()) {
143            throw new ScmRepositoryException(path.getAbsolutePath() + " isn't a valid directory.");
144        }
145
146        File hgDir = new File(path, ".hg");
147
148        if (!hgDir.exists()) {
149            throw new ScmRepositoryException(path.getAbsolutePath() + " isn't a hg directory.");
150        }
151
152        return makeProviderScmRepository(path.getAbsolutePath(), ':');
153    }
154
155    /** {@inheritDoc} */
156    public List<String> validateScmUrl(String scmSpecificUrl, char delimiter) {
157        HgUrlParserResult result = parseScmUrl(scmSpecificUrl);
158
159        return result.messages;
160    }
161
162    /** {@inheritDoc} */
163    public String getScmType() {
164        return "hg";
165    }
166
167    /** {@inheritDoc} */
168    public AddScmResult add(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
169            throws ScmException {
170        HgAddCommand command = new HgAddCommand();
171
172        return (AddScmResult) command.execute(repository, fileSet, parameters);
173    }
174
175    /** {@inheritDoc} */
176    public ChangeLogScmResult changelog(
177            ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
178        HgChangeLogCommand command = new HgChangeLogCommand();
179
180        return (ChangeLogScmResult) command.execute(repository, fileSet, parameters);
181    }
182
183    /** {@inheritDoc} */
184    public CheckInScmResult checkin(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
185            throws ScmException {
186        HgCheckInCommand command = new HgCheckInCommand();
187
188        return (CheckInScmResult) command.execute(repository, fileSet, parameters);
189    }
190
191    /** {@inheritDoc} */
192    public CheckOutScmResult checkout(
193            ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
194        HgCheckOutCommand command = new HgCheckOutCommand();
195
196        return (CheckOutScmResult) command.execute(repository, fileSet, parameters);
197    }
198
199    /** {@inheritDoc} */
200    public TagScmResult tag(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
201            throws ScmException {
202        HgTagCommand command = new HgTagCommand();
203
204        return (TagScmResult) command.execute(repository, fileSet, parameters);
205    }
206
207    /** {@inheritDoc} */
208    public DiffScmResult diff(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
209            throws ScmException {
210        HgDiffCommand command = new HgDiffCommand();
211
212        return (DiffScmResult) command.execute(repository, fileSet, parameters);
213    }
214
215    /** {@inheritDoc} */
216    public RemoveScmResult remove(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
217            throws ScmException {
218        HgRemoveCommand command = new HgRemoveCommand();
219
220        return (RemoveScmResult) command.execute(repository, fileSet, parameters);
221    }
222
223    /** {@inheritDoc} */
224    public StatusScmResult status(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
225            throws ScmException {
226        HgStatusCommand command = new HgStatusCommand();
227
228        return (StatusScmResult) command.execute(repository, fileSet, parameters);
229    }
230
231    /** {@inheritDoc} */
232    public UpdateScmResult update(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
233            throws ScmException {
234        HgUpdateCommand command = new HgUpdateCommand();
235
236        return (UpdateScmResult) command.execute(repository, fileSet, parameters);
237    }
238
239    /** {@inheritDoc} */
240    protected BlameScmResult blame(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
241            throws ScmException {
242        HgBlameCommand command = new HgBlameCommand();
243
244        return (BlameScmResult) command.execute(repository, fileSet, parameters);
245    }
246
247    /** {@inheritDoc} */
248    public BranchScmResult branch(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
249            throws ScmException {
250        HgBranchCommand command = new HgBranchCommand();
251
252        return (BranchScmResult) command.execute(repository, fileSet, parameters);
253    }
254
255    /**
256     * @since 1.5
257     */
258    @Override
259    protected ListScmResult list(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
260            throws ScmException {
261        HgListCommand hgListCommand = new HgListCommand();
262
263        return (ListScmResult) hgListCommand.executeCommand(repository, fileSet, parameters);
264    }
265
266    /**
267     * returns result of hg id -i
268     * @since 1.5
269     * @see org.apache.maven.scm.provider.AbstractScmProvider#info(org.apache.maven.scm.provider.ScmProviderRepository, org.apache.maven.scm.ScmFileSet, org.apache.maven.scm.CommandParameters)
270     */
271    @Override
272    public InfoScmResult info(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
273            throws ScmException {
274        HgInfoCommand infoCommand = new HgInfoCommand();
275
276        return (InfoScmResult) infoCommand.execute(repository, fileSet, parameters);
277    }
278}