Coverage Report - com.aragost.javahg.WorkingCopy
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkingCopy
94%
32/34
80%
8/10
1.667
 
 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  44
     private boolean parentsRetrieved = false;
 28  
 
 29  
     private Changeset parent1;
 30  
 
 31  
     private Changeset parent2;
 32  
 
 33  
     private String branchName;
 34  
 
 35  44
     WorkingCopy(Repository repository) {
 36  44
         this.repository = repository;
 37  44
     }
 38  
 
 39  
     public Changeset getParent1() {
 40  16
         ensureParentsRetrieved();
 41  16
         return this.parent1;
 42  
     }
 43  
 
 44  
     public Changeset getParent2() {
 45  13
         ensureParentsRetrieved();
 46  13
         return this.parent2;
 47  
     }
 48  
 
 49  
     /**
 50  
      * @return branch name for working copy
 51  
      */
 52  
     public synchronized String getBranchName() {
 53  5
         if (this.branchName == null) {
 54  3
             this.branchName = BranchCommand.on(this.repository).get();
 55  
         }
 56  5
         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  4
         BranchCommand.on(this.repository).set(branchName);
 66  4
         this.branchName = branchName;
 67  4
     }
 68  
 
 69  
     public void remove(String... files) {
 70  1
         RemoveCommand.on(this.repository).execute(files);
 71  1
     }
 72  
 
 73  
     public void add(String... files) {
 74  2
         AddCommand.on(this.repository).execute(files);
 75  2
     }
 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  5
         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  3
         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  2
         if (getParent2() == null) {
 102  0
             return null;
 103  
         }
 104  2
         return StatusCommand.on(this.repository).rev(getParent2().getNode()).execute();
 105  
     }
 106  
 
 107  
     private synchronized void ensureParentsRetrieved() {
 108  29
         if (!this.parentsRetrieved) {
 109  20
             retrieveParents();
 110  20
             this.parentsRetrieved = true;
 111  
         }
 112  29
     }
 113  
 
 114  
     private void retrieveParents() {
 115  20
         List<Changeset> parents = ParentsCommand.on(this.repository).execute();
 116  20
         switch (parents.size()) {
 117  
         // Fall though on purpose
 118  
         case 2:
 119  8
             this.parent2 = parents.get(1);
 120  
         case 1:
 121  19
             this.parent1 = parents.get(0);
 122  
         case 0:
 123  20
             break;
 124  
         default:
 125  0
             throw new RuntimeException("More that 2 parents from parents command");
 126  
         }
 127  20
     }
 128  
 
 129  
 }