View Javadoc

1   /*
2    * #%L
3    * JavaHg parent POM
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.Changeset;
29  import com.aragost.javahg.Repository;
30  
31  /**
32   * Represent a named branch returned by {@link BranchesCommand}
33   */
34  public class Branch implements Comparable<Branch> {
35  
36      private static final String CLOSED_SUFFIX = " (closed)";
37  
38      private static final String INACTIVE_SUFFIX = " (inactive)";
39  
40      private final Changeset branchTip;
41  
42      private final boolean closed;
43  
44      private final String name;
45  
46      public static Branch fromLine(Repository repo, String line) {
47          boolean isClosed = false;
48          if (line.endsWith(INACTIVE_SUFFIX)) {
49              line = line.substring(0, line.length() - INACTIVE_SUFFIX.length());
50          } else if (line.endsWith(CLOSED_SUFFIX)) {
51              isClosed = true;
52              line = line.substring(0, line.length() - CLOSED_SUFFIX.length());
53          }
54          // Note branch names can contain spaces so we find the last
55          // one
56          int lastSpace = line.lastIndexOf(' ');
57          String node = line.substring(line.indexOf(':', lastSpace) + 1);
58          String branchName = line.substring(0, lastSpace).trim();
59          return new Branch(repo.changeset(node), branchName, isClosed);
60      }
61  
62      private Branch(Changeset branchTip, String name, boolean closed) {
63          this.branchTip = branchTip;
64          this.name = name;
65          this.closed = closed;
66      }
67  
68      public Changeset getBranchTip() {
69          return branchTip;
70      }
71  
72      public boolean isClosed() {
73          return closed;
74      }
75  
76      public String getName() {
77          return name;
78      }
79  
80      public int compareTo(Branch that) {
81          return this.getName().compareTo(that.getName());
82      }
83  
84      @Override
85      public String toString() {
86          return "branch[" + this.name + "]";
87      }
88  
89  }