Coverage Report - com.aragost.javahg.internals.UpdateMergeHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
UpdateMergeHelper
93%
30/32
78%
11/14
3.5
 
 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.internals;
 27  
 
 28  
 import java.io.BufferedReader;
 29  
 import java.io.IOException;
 30  
 import java.io.InputStream;
 31  
 import java.io.StringReader;
 32  
 import java.util.regex.Matcher;
 33  
 import java.util.regex.Pattern;
 34  
 
 35  
 import com.aragost.javahg.commands.ManifestMergeOracle;
 36  
 import com.aragost.javahg.commands.UpdateResult;
 37  
 import com.google.common.io.ByteStreams;
 38  
 
 39  
 /**
 40  
  * A helper class to parse output from update and merge commands.
 41  
  */
 42  
 public class UpdateMergeHelper {
 43  
 
 44  1
     private static final Pattern MANIFEST_MERGE_PATTERN = Pattern.compile("remote changed (.*) which local deleted\n"
 45  
             + "use \\(c\\)hanged version or leave \\(d\\)eleted\\? ");
 46  
 
 47  
     private InputStream stdout;
 48  
 
 49  
     private ManifestMergeOracle oracle;
 50  
 
 51  
     private AbstractCommand command;
 52  
 
 53  31
     public UpdateMergeHelper(InputStream stdout, ManifestMergeOracle oracle, AbstractCommand command) {
 54  31
         this.stdout = stdout;
 55  31
         this.oracle = oracle;
 56  31
         this.command = command;
 57  31
     }
 58  
 
 59  
     public void merge() throws IOException {
 60  5
         processManifestMergeConflictPrompting();
 61  5
     }
 62  
 
 63  
     public UpdateResult update() throws IOException {
 64  26
         String output = processManifestMergeConflictPrompting();
 65  26
         BufferedReader reader = new BufferedReader(new StringReader(output));
 66  
         String line;
 67  26
         UpdateResult result = null;
 68  54
         while ((line = reader.readLine()) != null) {
 69  28
             if (line.startsWith("merging ")) {
 70  
                 // Just ignore
 71  28
             } else if (line.equals("use 'hg resolve' to retry unresolved file merges")) {
 72  
                 // Ignore
 73  
             } else {
 74  26
                 result = UpdateResult.fromLine(line);
 75  
             }
 76  
         }
 77  26
         if (result == null) {
 78  0
             throw new IllegalStateException("No 'update' line found in output");
 79  
         }
 80  26
         return result;
 81  
     }
 82  
 
 83  
     private String processManifestMergeConflictPrompting() throws IOException {
 84  31
         if (this.oracle != null) {
 85  4
             this.oracle.getMissingAnswers().clear();
 86  
         }
 87  
 
 88  31
         String output = null;
 89  
         while (true) {
 90  34
             output = new String(ByteStreams.toByteArray(this.stdout));
 91  34
             Matcher matcher = MANIFEST_MERGE_PATTERN.matcher(output);
 92  34
             if (matcher.matches()) {
 93  3
                 if (oracle == null) {
 94  0
                     throw new RuntimeException("Manifest merge conflict, but no oracle");
 95  
                 }
 96  3
                 String filename = output.substring(matcher.start(1), matcher.end(1));
 97  3
                 String answer = oracle.ask(filename);
 98  3
                 this.command.sendLine(answer);
 99  3
                 this.command.reopenOutputChannelStream();
 100  
             } else {
 101  
                 break;
 102  
             }
 103  3
         }
 104  31
         return output;
 105  
     }
 106  
 }