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  
30  import com.aragost.javahg.Repository;
31  import com.aragost.javahg.commands.flags.BranchCommandFlags;
32  import com.aragost.javahg.internals.HgInputStream;
33  
34  /**
35   * Command class for executing <tt>hg branch</tt>. Set flags from
36   * {@link BranchCommandFlags} and run {@link #set(String)} to set a
37   * branch or {@link #clean()} to reset the branch name.
38   */
39  public class BranchCommand extends BranchCommandFlags {
40  
41      private static final byte[] CLEAN_PREFIX = "reset working directory to branch ".getBytes();
42  
43      public BranchCommand(Repository repository) {
44          super(repository);
45      }
46  
47      /**
48       * Get the branch for working directory.
49       * 
50       * @return name of branch for working copy
51       */
52      public String get() {
53          return launchString().trim();
54      }
55  
56      /**
57       * Set the branch for working directory.
58       * 
59       * @param branchName
60       */
61      public void set(String branchName) {
62          // Trim branchName. Mercurial commandserver seems to be bit
63          // confused otherwise.
64          launchString(branchName.trim());
65      }
66  
67      /**
68       * Reset the branch name for working directory to branch name of
69       * first parent
70       * 
71       * @return old branch name
72       * @throws IOException
73       */
74      public String clean() throws IOException {
75          HgInputStream stream = launchStream("--clean");
76          stream.mustMatch(CLEAN_PREFIX);
77          String oldName = stream.textUpTo('\n');
78          stream.consumeAll();
79          return oldName;
80      }
81  
82  }