View Javadoc

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 java.io.IOException;
29  import java.io.InputStream;
30  
31  import com.aragost.javahg.Changeset;
32  import com.aragost.javahg.Repository;
33  import com.aragost.javahg.commands.flags.UpdateCommandFlags;
34  import com.aragost.javahg.internals.UpdateMergeHelper;
35  
36  /**
37   * Command class for executing <tt>hg update</tt>. Set flags from
38   * {@link UpdateCommandFlags} and call the {@link #execute} method.
39   */
40  public class UpdateCommand extends UpdateCommandFlags {
41  
42      /**
43       * @param repository
44       *            the repository associated with this command.
45       */
46      public UpdateCommand(Repository repository) {
47          super(repository);
48      }
49  
50      /**
51       * Run <tt>hg update</tt> on a clean working copy. This runs with
52       * no merge oracle and so the caller must ensure that there are no
53       * merge conflicts.
54       * 
55       * @return the update result.
56       * @throws IOException
57       */
58      public UpdateResult execute() throws IOException {
59          return executeHelper(null);
60      }
61  
62      /**
63       * Run <tt>hg update</tt> with a merge oracle.
64       * 
65       * @param oracle
66       *            the merge oracle to use in case of merge conflicts.
67       * 
68       * @return the update result.
69       * @throws IOException
70       */
71      public UpdateResult execute(ManifestMergeOracle oracle) throws IOException {
72          if (oracle == null) {
73              throw new IllegalArgumentException("oracle == null");
74          }
75          return executeHelper(oracle);
76      }
77  
78      private UpdateResult executeHelper(ManifestMergeOracle oracle) throws IOException {
79          InputStream stdout = launchStream();
80          UpdateMergeHelper helper = new UpdateMergeHelper(stdout, oracle, this);
81          return helper.update();
82      }
83  
84      @Override
85      public boolean isSuccessful() {
86          return super.isSuccessful() || getReturnCode() == 1;
87      }
88  
89      /**
90       * @return True if the command was not successful because it would have
91       *         crossed branches. Applicable to updates where revision is not
92       *         specified.
93       */
94      public boolean crossedBranch() {
95          String errorString = getErrorString();
96  
97          return errorString.indexOf("abort: crosses branches") >= 0;
98      }
99  
100     /**
101      * Set the <tt>--rev</tt> flag.
102      * 
103      * @param cset
104      *            the changeset to update to
105      * @return this command.
106      */
107     public UpdateCommand rev(Changeset cset) {
108         super.rev(cset.getNode());
109         return this;
110     }
111 }