View Javadoc

1   package com.aragost.javahg;
2   
3   import java.io.IOException;
4   import java.util.List;
5   
6   import com.aragost.javahg.commands.AddCommand;
7   import com.aragost.javahg.commands.BranchCommand;
8   import com.aragost.javahg.commands.MergeCommand;
9   import com.aragost.javahg.commands.ParentsCommand;
10  import com.aragost.javahg.commands.RemoveCommand;
11  import com.aragost.javahg.commands.StatusCommand;
12  import com.aragost.javahg.commands.StatusResult;
13  import com.aragost.javahg.merge.MergeContext;
14  
15  /**
16   * Represents working copy for a Repository.
17   * <p>
18   * Warning: The instance cache the information and will not see
19   * changes to the working copy. Create a new instance if you need the
20   * most current state.
21   * 
22   */
23  public class WorkingCopy {
24  
25      private Repository repository;
26  
27      private boolean parentsRetrieved = false;
28  
29      private Changeset parent1;
30  
31      private Changeset parent2;
32  
33      private String branchName;
34  
35      WorkingCopy(Repository repository) {
36          this.repository = repository;
37      }
38  
39      public Changeset getParent1() {
40          ensureParentsRetrieved();
41          return this.parent1;
42      }
43  
44      public Changeset getParent2() {
45          ensureParentsRetrieved();
46          return this.parent2;
47      }
48  
49      /**
50       * @return branch name for working copy
51       */
52      public synchronized String getBranchName() {
53          if (this.branchName == null) {
54              this.branchName = BranchCommand.on(this.repository).get();
55          }
56          return this.branchName;
57      }
58  
59      /**
60       * Set the branch name of working copy
61       * 
62       * @param branchName
63       */
64      public synchronized void setBranchName(String branchName) {
65          BranchCommand.on(this.repository).set(branchName);
66          this.branchName = branchName;
67      }
68  
69      public void remove(String... files) {
70          RemoveCommand.on(this.repository).execute(files);
71      }
72  
73      public void add(String... files) {
74          AddCommand.on(this.repository).execute(files);
75      }
76  
77      /**
78       * Merge the working copy with the specified changeset
79       * 
80       * @param remote
81       * @return a MergeContext
82       * @throws IOException
83       */
84      public MergeContext merge(Changeset remote) throws IOException {
85          return MergeCommand.on(this.repository).rev(remote.getNode()).execute();
86      }
87  
88      /**
89       * 
90       * @return status for working copy relative to first parent
91       */
92      public StatusResult status() {
93          return StatusCommand.on(this.repository).execute();
94      }
95  
96      /**
97       * 
98       * @return status for working copy relative to second parent
99       */
100     public StatusResult parent2Status() {
101         if (getParent2() == null) {
102             return null;
103         }
104         return StatusCommand.on(this.repository).rev(getParent2().getNode()).execute();
105     }
106 
107     private synchronized void ensureParentsRetrieved() {
108         if (!this.parentsRetrieved) {
109             retrieveParents();
110             this.parentsRetrieved = true;
111         }
112     }
113 
114     private void retrieveParents() {
115         List<Changeset> parents = ParentsCommand.on(this.repository).execute();
116         switch (parents.size()) {
117         // Fall though on purpose
118         case 2:
119             this.parent2 = parents.get(1);
120         case 1:
121             this.parent1 = parents.get(0);
122         case 0:
123             break;
124         default:
125             throw new RuntimeException("More that 2 parents from parents command");
126         }
127     }
128 
129 }