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 | 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 | |
|
71 | 28 | } else if (line.equals("use 'hg resolve' to retry unresolved file merges")) { |
72 | |
|
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 | |
} |