1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.merge;
13
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.nio.charset.Charset;
17 import java.util.List;
18
19 import org.eclipse.jgit.diff.RawText;
20 import org.eclipse.jgit.merge.MergeChunk.ConflictState;
21
22 class MergeFormatterPass {
23
24 private final EolAwareOutputStream out;
25
26 private final MergeResult<RawText> res;
27
28 private final List<String> seqName;
29
30 private final Charset charset;
31
32 private final boolean threeWayMerge;
33
34 private String lastConflictingName;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 MergeFormatterPass(OutputStream out, MergeResult<RawText> res,
52 List<String> seqName, Charset charset) {
53 this.out = new EolAwareOutputStream(out);
54 this.res = res;
55 this.seqName = seqName;
56 this.charset = charset;
57 this.threeWayMerge = (res.getSequences().size() == 3);
58 }
59
60 void formatMerge() throws IOException {
61 boolean missingNewlineAtEnd = false;
62 for (MergeChunk chunk : res) {
63 RawText seq = res.getSequences().get(chunk.getSequenceIndex());
64 writeConflictMetadata(chunk);
65
66 for (int i = chunk.getBegin(); i < chunk.getEnd(); i++)
67 writeLine(seq, i);
68 missingNewlineAtEnd = seq.isMissingNewlineAtEnd();
69 }
70
71
72 if (lastConflictingName != null)
73 writeConflictEnd();
74 if (!missingNewlineAtEnd)
75 out.beginln();
76 }
77
78 private void writeConflictMetadata(MergeChunk chunk) throws IOException {
79 if (lastConflictingName != null
80 && chunk.getConflictState() != ConflictState.NEXT_CONFLICTING_RANGE) {
81
82 writeConflictEnd();
83 }
84 if (chunk.getConflictState() == ConflictState.FIRST_CONFLICTING_RANGE) {
85
86 writeConflictStart(chunk);
87 } else if (chunk.getConflictState() == ConflictState.NEXT_CONFLICTING_RANGE) {
88
89 writeConflictChange(chunk);
90 }
91 }
92
93 private void writeConflictEnd() throws IOException {
94 writeln(">>>>>>> " + lastConflictingName);
95 lastConflictingName = null;
96 }
97
98 private void writeConflictStart(MergeChunk chunk) throws IOException {
99 lastConflictingName = seqName.get(chunk.getSequenceIndex());
100 writeln("<<<<<<< " + lastConflictingName);
101 }
102
103 private void writeConflictChange(MergeChunk chunk) throws IOException {
104
105
106
107
108
109
110
111 lastConflictingName = seqName.get(chunk.getSequenceIndex());
112 writeln(threeWayMerge ? "=======" : "======= "
113 + lastConflictingName);
114 }
115
116 private void writeln(String s) throws IOException {
117 out.beginln();
118 out.write((s + "\n").getBytes(charset));
119 }
120
121 private void writeLine(RawText seq, int i) throws IOException {
122 out.beginln();
123 seq.writeLine(out, i);
124
125 if (out.isBeginln())
126 out.write('\n');
127 }
128 }