1 /*
2 * #%L
3 * JavaHg
4 * %%
5 * Copyright (C) 2011 aragost Trifork ag
6 * %%
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 * #L%
25 */
26 package com.aragost.javahg.commands;
27
28 import com.aragost.javahg.HgVersion;
29 import com.aragost.javahg.Repository;
30 import com.aragost.javahg.commands.flags.VersionCommandFlags;
31 import com.aragost.javahg.internals.UnexpectedCommandOutputException;
32
33 /**
34 * Command class for executing <tt>hg version</tt>. Set flags from
35 * {@link VersionCommandFlags} and call the {@link #execute} method.
36 */
37 public class VersionCommand extends VersionCommandFlags {
38
39 private static final String PREFIX = "Mercurial Distributed SCM (version ";
40
41 /**
42 * @param repository
43 * the repository associated with this command.
44 */
45 public VersionCommand(Repository repository) {
46 super(repository);
47 }
48
49 /**
50 * Retrieve the Mercurial version number.
51 *
52 * @return the Mercurial version of the command server. This is a
53 * string since it can include extra information such as
54 * the exact changeset ID used to build Mercurial.
55 */
56 public HgVersion execute() {
57 String s = launchString();
58 if (s.startsWith(PREFIX)) {
59 s = s.substring(PREFIX.length());
60 int n = s.indexOf(')');
61 return HgVersion.fromString(new String(s.substring(0, n)));
62 } else {
63 throw new UnexpectedCommandOutputException(this, s);
64 }
65 }
66
67 }