View Javadoc
1   /*
2    * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.merge;
45  
46  import java.util.ArrayList;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  import org.eclipse.jgit.diff.DiffAlgorithm;
51  import org.eclipse.jgit.diff.Edit;
52  import org.eclipse.jgit.diff.EditList;
53  import org.eclipse.jgit.diff.HistogramDiff;
54  import org.eclipse.jgit.diff.Sequence;
55  import org.eclipse.jgit.diff.SequenceComparator;
56  import org.eclipse.jgit.merge.MergeChunk.ConflictState;
57  
58  /**
59   * Provides the merge algorithm which does a three-way merge on content provided
60   * as RawText. By default {@link org.eclipse.jgit.diff.HistogramDiff} is used as
61   * diff algorithm.
62   */
63  public final class MergeAlgorithm {
64  	private final DiffAlgorithm diffAlg;
65  
66  	/**
67  	 * Creates a new MergeAlgorithm which uses
68  	 * {@link org.eclipse.jgit.diff.HistogramDiff} as diff algorithm
69  	 */
70  	public MergeAlgorithm() {
71  		this(new HistogramDiff());
72  	}
73  
74  	/**
75  	 * Creates a new MergeAlgorithm
76  	 *
77  	 * @param diff
78  	 *            the diff algorithm used by this merge
79  	 */
80  	public MergeAlgorithm(DiffAlgorithm diff) {
81  		this.diffAlg = diff;
82  	}
83  
84  	// An special edit which acts as a sentinel value by marking the end the
85  	// list of edits
86  	private final static Edit END_EDIT = new Edit(Integer.MAX_VALUE,
87  			Integer.MAX_VALUE);
88  
89  	/**
90  	 * Does the three way merge between a common base and two sequences.
91  	 *
92  	 * @param cmp comparison method for this execution.
93  	 * @param base the common base sequence
94  	 * @param ours the first sequence to be merged
95  	 * @param theirs the second sequence to be merged
96  	 * @return the resulting content
97  	 */
98  	public <S extends Sequence> MergeResult<S> merge(
99  			SequenceComparator<S> cmp, S base, S ours, S theirs) {
100 		List<S> sequences = new ArrayList<>(3);
101 		sequences.add(base);
102 		sequences.add(ours);
103 		sequences.add(theirs);
104 		MergeResult<S> result = new MergeResult<>(sequences);
105 
106 		if (ours.size() == 0) {
107 			if (theirs.size() != 0) {
108 				EditList theirsEdits = diffAlg.diff(cmp, base, theirs);
109 				if (!theirsEdits.isEmpty()) {
110 					// we deleted, they modified -> Let their complete content
111 					// conflict with empty text
112 					result.add(1, 0, 0, ConflictState.FIRST_CONFLICTING_RANGE);
113 					result.add(2, 0, theirs.size(),
114 							ConflictState.NEXT_CONFLICTING_RANGE);
115 				} else
116 					// we deleted, they didn't modify -> Let our deletion win
117 					result.add(1, 0, 0, ConflictState.NO_CONFLICT);
118 			} else
119 				// we and they deleted -> return a single chunk of nothing
120 				result.add(1, 0, 0, ConflictState.NO_CONFLICT);
121 			return result;
122 		} else if (theirs.size() == 0) {
123 			EditList oursEdits = diffAlg.diff(cmp, base, ours);
124 			if (!oursEdits.isEmpty()) {
125 				// we modified, they deleted -> Let our complete content
126 				// conflict with empty text
127 				result.add(1, 0, ours.size(),
128 						ConflictState.FIRST_CONFLICTING_RANGE);
129 				result.add(2, 0, 0, ConflictState.NEXT_CONFLICTING_RANGE);
130 			} else
131 				// they deleted, we didn't modify -> Let their deletion win
132 				result.add(2, 0, 0, ConflictState.NO_CONFLICT);
133 			return result;
134 		}
135 
136 		EditList oursEdits = diffAlg.diff(cmp, base, ours);
137 		Iterator<Edit> baseToOurs = oursEdits.iterator();
138 		EditList theirsEdits = diffAlg.diff(cmp, base, theirs);
139 		Iterator<Edit> baseToTheirs = theirsEdits.iterator();
140 		int current = 0; // points to the next line (first line is 0) of base
141 		                 // which was not handled yet
142 		Edit oursEdit = nextEdit(baseToOurs);
143 		Edit theirsEdit = nextEdit(baseToTheirs);
144 
145 		// iterate over all edits from base to ours and from base to theirs
146 		// leave the loop when there are no edits more for ours or for theirs
147 		// (or both)
148 		while (theirsEdit != END_EDIT || oursEdit != END_EDIT) {
149 			if (oursEdit.getEndA() < theirsEdit.getBeginA()) {
150 				// something was changed in ours not overlapping with any change
151 				// from theirs. First add the common part in front of the edit
152 				// then the edit.
153 				if (current != oursEdit.getBeginA()) {
154 					result.add(0, current, oursEdit.getBeginA(),
155 							ConflictState.NO_CONFLICT);
156 				}
157 				result.add(1, oursEdit.getBeginB(), oursEdit.getEndB(),
158 						ConflictState.NO_CONFLICT);
159 				current = oursEdit.getEndA();
160 				oursEdit = nextEdit(baseToOurs);
161 			} else if (theirsEdit.getEndA() < oursEdit.getBeginA()) {
162 				// something was changed in theirs not overlapping with any
163 				// from ours. First add the common part in front of the edit
164 				// then the edit.
165 				if (current != theirsEdit.getBeginA()) {
166 					result.add(0, current, theirsEdit.getBeginA(),
167 							ConflictState.NO_CONFLICT);
168 				}
169 				result.add(2, theirsEdit.getBeginB(), theirsEdit.getEndB(),
170 						ConflictState.NO_CONFLICT);
171 				current = theirsEdit.getEndA();
172 				theirsEdit = nextEdit(baseToTheirs);
173 			} else {
174 				// here we found a real overlapping modification
175 
176 				// if there is a common part in front of the conflict add it
177 				if (oursEdit.getBeginA() != current
178 						&& theirsEdit.getBeginA() != current) {
179 					result.add(0, current, Math.min(oursEdit.getBeginA(),
180 							theirsEdit.getBeginA()), ConflictState.NO_CONFLICT);
181 				}
182 
183 				// set some initial values for the ranges in A and B which we
184 				// want to handle
185 				int oursBeginB = oursEdit.getBeginB();
186 				int theirsBeginB = theirsEdit.getBeginB();
187 				// harmonize the start of the ranges in A and B
188 				if (oursEdit.getBeginA() < theirsEdit.getBeginA()) {
189 					theirsBeginB -= theirsEdit.getBeginA()
190 							- oursEdit.getBeginA();
191 				} else {
192 					oursBeginB -= oursEdit.getBeginA() - theirsEdit.getBeginA();
193 				}
194 
195 				// combine edits:
196 				// Maybe an Edit on one side corresponds to multiple Edits on
197 				// the other side. Then we have to combine the Edits of the
198 				// other side - so in the end we can merge together two single
199 				// edits.
200 				//
201 				// It is important to notice that this combining will extend the
202 				// ranges of our conflict always downwards (towards the end of
203 				// the content). The starts of the conflicting ranges in ours
204 				// and theirs are not touched here.
205 				//
206 				// This combining is an iterative process: after we have
207 				// combined some edits we have to do the check again. The
208 				// combined edits could now correspond to multiple edits on the
209 				// other side.
210 				//
211 				// Example: when this combining algorithm works on the following
212 				// edits
213 				// oursEdits=((0-5,0-5),(6-8,6-8),(10-11,10-11)) and
214 				// theirsEdits=((0-1,0-1),(2-3,2-3),(5-7,5-7))
215 				// it will merge them into
216 				// oursEdits=((0-8,0-8),(10-11,10-11)) and
217 				// theirsEdits=((0-7,0-7))
218 				//
219 				// Since the only interesting thing to us is how in ours and
220 				// theirs the end of the conflicting range is changing we let
221 				// oursEdit and theirsEdit point to the last conflicting edit
222 				Edit nextOursEdit = nextEdit(baseToOurs);
223 				Edit nextTheirsEdit = nextEdit(baseToTheirs);
224 				for (;;) {
225 					if (oursEdit.getEndA() >= nextTheirsEdit.getBeginA()) {
226 						theirsEdit = nextTheirsEdit;
227 						nextTheirsEdit = nextEdit(baseToTheirs);
228 					} else if (theirsEdit.getEndA() >= nextOursEdit.getBeginA()) {
229 						oursEdit = nextOursEdit;
230 						nextOursEdit = nextEdit(baseToOurs);
231 					} else {
232 						break;
233 					}
234 				}
235 
236 				// harmonize the end of the ranges in A and B
237 				int oursEndB = oursEdit.getEndB();
238 				int theirsEndB = theirsEdit.getEndB();
239 				if (oursEdit.getEndA() < theirsEdit.getEndA()) {
240 					oursEndB += theirsEdit.getEndA() - oursEdit.getEndA();
241 				} else {
242 					theirsEndB += oursEdit.getEndA() - theirsEdit.getEndA();
243 				}
244 
245 				// A conflicting region is found. Strip off common lines in
246 				// in the beginning and the end of the conflicting region
247 
248 				// Determine the minimum length of the conflicting areas in OURS
249 				// and THEIRS. Also determine how much bigger the conflicting
250 				// area in THEIRS is compared to OURS. All that is needed to
251 				// limit the search for common areas at the beginning or end
252 				// (the common areas cannot be bigger then the smaller
253 				// conflicting area. The delta is needed to know whether the
254 				// complete conflicting area is common in OURS and THEIRS.
255 				int minBSize = oursEndB - oursBeginB;
256 				int BSizeDelta = minBSize - (theirsEndB - theirsBeginB);
257 				if (BSizeDelta > 0)
258 					minBSize -= BSizeDelta;
259 
260 				int commonPrefix = 0;
261 				while (commonPrefix < minBSize
262 						&& cmp.equals(ours, oursBeginB + commonPrefix, theirs,
263 								theirsBeginB + commonPrefix))
264 					commonPrefix++;
265 				minBSize -= commonPrefix;
266 				int commonSuffix = 0;
267 				while (commonSuffix < minBSize
268 						&& cmp.equals(ours, oursEndB - commonSuffix - 1, theirs,
269 								theirsEndB - commonSuffix - 1))
270 					commonSuffix++;
271 				minBSize -= commonSuffix;
272 
273 				// Add the common lines at start of conflict
274 				if (commonPrefix > 0)
275 					result.add(1, oursBeginB, oursBeginB + commonPrefix,
276 							ConflictState.NO_CONFLICT);
277 
278 				// Add the conflict (Only if there is a conflict left to report)
279 				if (minBSize > 0 || BSizeDelta != 0) {
280 					result.add(1, oursBeginB + commonPrefix, oursEndB
281 							- commonSuffix,
282 							ConflictState.FIRST_CONFLICTING_RANGE);
283 					result.add(2, theirsBeginB + commonPrefix, theirsEndB
284 							- commonSuffix,
285 							ConflictState.NEXT_CONFLICTING_RANGE);
286 				}
287 
288 				// Add the common lines at end of conflict
289 				if (commonSuffix > 0)
290 					result.add(1, oursEndB - commonSuffix, oursEndB,
291 							ConflictState.NO_CONFLICT);
292 
293 				current = Math.max(oursEdit.getEndA(), theirsEdit.getEndA());
294 				oursEdit = nextOursEdit;
295 				theirsEdit = nextTheirsEdit;
296 			}
297 		}
298 		// maybe we have a common part behind the last edit: copy it to the
299 		// result
300 		if (current < base.size()) {
301 			result.add(0, current, base.size(), ConflictState.NO_CONFLICT);
302 		}
303 		return result;
304 	}
305 
306 	/**
307 	 * Helper method which returns the next Edit for an Iterator over Edits.
308 	 * When there are no more edits left this method will return the constant
309 	 * END_EDIT.
310 	 *
311 	 * @param it
312 	 *            the iterator for which the next edit should be returned
313 	 * @return the next edit from the iterator or END_EDIT if there no more
314 	 *         edits
315 	 */
316 	private static Edit nextEdit(Iterator<Edit> it) {
317 		return (it.hasNext() ? it.next() : END_EDIT);
318 	}
319 }