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.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      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      public UpdateMergeHelper(InputStream stdout, ManifestMergeOracle oracle, AbstractCommand command) {
54          this.stdout = stdout;
55          this.oracle = oracle;
56          this.command = command;
57      }
58  
59      public void merge() throws IOException {
60          processManifestMergeConflictPrompting();
61      }
62  
63      public UpdateResult update() throws IOException {
64          String output = processManifestMergeConflictPrompting();
65          BufferedReader reader = new BufferedReader(new StringReader(output));
66          String line;
67          UpdateResult result = null;
68          while ((line = reader.readLine()) != null) {
69              if (line.startsWith("merging ")) {
70                  // Just ignore
71              } else if (line.equals("use 'hg resolve' to retry unresolved file merges")) {
72                  // Ignore
73              } else {
74                  result = UpdateResult.fromLine(line);
75              }
76          }
77          if (result == null) {
78              throw new IllegalStateException("No 'update' line found in output");
79          }
80          return result;
81      }
82  
83      private String processManifestMergeConflictPrompting() throws IOException {
84          if (this.oracle != null) {
85              this.oracle.getMissingAnswers().clear();
86          }
87  
88          String output = null;
89          while (true) {
90              output = new String(ByteStreams.toByteArray(this.stdout));
91              Matcher matcher = MANIFEST_MERGE_PATTERN.matcher(output);
92              if (matcher.matches()) {
93                  if (oracle == null) {
94                      throw new RuntimeException("Manifest merge conflict, but no oracle");
95                  }
96                  String filename = output.substring(matcher.start(1), matcher.end(1));
97                  String answer = oracle.ask(filename);
98                  this.command.sendLine(answer);
99                  this.command.reopenOutputChannelStream();
100             } else {
101                 break;
102             }
103         }
104         return output;
105     }
106 }