1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
71 } else if (line.equals("use 'hg resolve' to retry unresolved file merges")) {
72
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 }