View Javadoc
1   /*
2    * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>,
3    * Copyright (C) 2010-2012, Matthias Sohn <matthias.sohn@sap.com>
4    * Copyright (C) 2012, Research In Motion Limited
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  package org.eclipse.jgit.merge;
46  
47  import static org.eclipse.jgit.lib.Constants.CHARACTER_ENCODING;
48  import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
49  
50  import java.io.BufferedOutputStream;
51  import java.io.File;
52  import java.io.FileInputStream;
53  import java.io.FileNotFoundException;
54  import java.io.FileOutputStream;
55  import java.io.IOException;
56  import java.io.InputStream;
57  import java.io.OutputStream;
58  import java.util.ArrayList;
59  import java.util.Arrays;
60  import java.util.Collections;
61  import java.util.HashMap;
62  import java.util.Iterator;
63  import java.util.LinkedList;
64  import java.util.List;
65  import java.util.Map;
66  
67  import org.eclipse.jgit.diff.DiffAlgorithm;
68  import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
69  import org.eclipse.jgit.diff.RawText;
70  import org.eclipse.jgit.diff.RawTextComparator;
71  import org.eclipse.jgit.diff.Sequence;
72  import org.eclipse.jgit.dircache.DirCache;
73  import org.eclipse.jgit.dircache.DirCacheBuildIterator;
74  import org.eclipse.jgit.dircache.DirCacheBuilder;
75  import org.eclipse.jgit.dircache.DirCacheCheckout;
76  import org.eclipse.jgit.dircache.DirCacheEntry;
77  import org.eclipse.jgit.errors.CorruptObjectException;
78  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
79  import org.eclipse.jgit.errors.IndexWriteException;
80  import org.eclipse.jgit.errors.MissingObjectException;
81  import org.eclipse.jgit.errors.NoWorkTreeException;
82  import org.eclipse.jgit.lib.ConfigConstants;
83  import org.eclipse.jgit.lib.FileMode;
84  import org.eclipse.jgit.lib.ObjectId;
85  import org.eclipse.jgit.lib.ObjectReader;
86  import org.eclipse.jgit.lib.Repository;
87  import org.eclipse.jgit.revwalk.RevTree;
88  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
89  import org.eclipse.jgit.treewalk.CanonicalTreeParser;
90  import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
91  import org.eclipse.jgit.treewalk.TreeWalk;
92  import org.eclipse.jgit.treewalk.WorkingTreeIterator;
93  import org.eclipse.jgit.treewalk.filter.TreeFilter;
94  import org.eclipse.jgit.util.FS;
95  import org.eclipse.jgit.util.TemporaryBuffer;
96  
97  /**
98   * A three-way merger performing a content-merge if necessary
99   */
100 public class ResolveMerger extends ThreeWayMerger {
101 	/**
102 	 * If the merge fails (means: not stopped because of unresolved conflicts)
103 	 * this enum is used to explain why it failed
104 	 */
105 	public enum MergeFailureReason {
106 		/** the merge failed because of a dirty index */
107 		DIRTY_INDEX,
108 		/** the merge failed because of a dirty workingtree */
109 		DIRTY_WORKTREE,
110 		/** the merge failed because of a file could not be deleted */
111 		COULD_NOT_DELETE
112 	}
113 
114 	/**
115 	 * The tree walk which we'll iterate over to merge entries.
116 	 *
117 	 * @since 3.4
118 	 */
119 	protected NameConflictTreeWalk tw;
120 
121 	/**
122 	 * string versions of a list of commit SHA1s
123 	 *
124 	 * @since 3.0
125 	 */
126 	protected String commitNames[];
127 
128 	/**
129 	 * Index of the base tree within the {@link #tw tree walk}.
130 	 *
131 	 * @since 3.4
132 	 */
133 	protected static final int T_BASE = 0;
134 
135 	/**
136 	 * Index of our tree in withthe {@link #tw tree walk}.
137 	 *
138 	 * @since 3.4
139 	 */
140 	protected static final int T_OURS = 1;
141 
142 	/**
143 	 * Index of their tree within the {@link #tw tree walk}.
144 	 *
145 	 * @since 3.4
146 	 */
147 	protected static final int T_THEIRS = 2;
148 
149 	/**
150 	 * Index of the index tree within the {@link #tw tree walk}.
151 	 *
152 	 * @since 3.4
153 	 */
154 	protected static final int T_INDEX = 3;
155 
156 	/**
157 	 * Index of the working directory tree within the {@link #tw tree walk}.
158 	 *
159 	 * @since 3.4
160 	 */
161 	protected static final int T_FILE = 4;
162 
163 	/**
164 	 * Builder to update the cache during this merge.
165 	 *
166 	 * @since 3.4
167 	 */
168 	protected DirCacheBuilder builder;
169 
170 	/**
171 	 * merge result as tree
172 	 *
173 	 * @since 3.0
174 	 */
175 	protected ObjectId resultTree;
176 
177 	/**
178 	 * Paths that could not be merged by this merger because of an unsolvable
179 	 * conflict.
180 	 *
181 	 * @since 3.4
182 	 */
183 	protected List<String> unmergedPaths = new ArrayList<String>();
184 
185 	/**
186 	 * Files modified during this merge operation.
187 	 *
188 	 * @since 3.4
189 	 */
190 	protected List<String> modifiedFiles = new LinkedList<String>();
191 
192 	/**
193 	 * If the merger has nothing to do for a file but check it out at the end of
194 	 * the operation, it can be added here.
195 	 *
196 	 * @since 3.4
197 	 */
198 	protected Map<String, DirCacheEntry> toBeCheckedOut = new HashMap<String, DirCacheEntry>();
199 
200 	/**
201 	 * Paths in this list will be deleted from the local copy at the end of the
202 	 * operation.
203 	 *
204 	 * @since 3.4
205 	 */
206 	protected List<String> toBeDeleted = new ArrayList<String>();
207 
208 	/**
209 	 * Low-level textual merge results. Will be passed on to the callers in case
210 	 * of conflicts.
211 	 *
212 	 * @since 3.4
213 	 */
214 	protected Map<String, MergeResult<? extends Sequence>> mergeResults = new HashMap<String, MergeResult<? extends Sequence>>();
215 
216 	/**
217 	 * Paths for which the merge failed altogether.
218 	 *
219 	 * @since 3.4
220 	 */
221 	protected Map<String, MergeFailureReason> failingPaths = new HashMap<String, MergeFailureReason>();
222 
223 	/**
224 	 * Updated as we merge entries of the tree walk. Tells us whether we should
225 	 * recurse into the entry if it is a subtree.
226 	 *
227 	 * @since 3.4
228 	 */
229 	protected boolean enterSubtree;
230 
231 	/**
232 	 * Set to true if this merge should work in-memory. The repos dircache and
233 	 * workingtree are not touched by this method. Eventually needed files are
234 	 * created as temporary files and a new empty, in-memory dircache will be
235 	 * used instead the repo's one. Often used for bare repos where the repo
236 	 * doesn't even have a workingtree and dircache.
237 	 * @since 3.0
238 	 */
239 	protected boolean inCore;
240 
241 	/**
242 	 * Set to true if this merger should use the default dircache of the
243 	 * repository and should handle locking and unlocking of the dircache. If
244 	 * this merger should work in-core or if an explicit dircache was specified
245 	 * during construction then this field is set to false.
246 	 * @since 3.0
247 	 */
248 	protected boolean implicitDirCache;
249 
250 	/**
251 	 * Directory cache
252 	 * @since 3.0
253 	 */
254 	protected DirCache dircache;
255 
256 	/**
257 	 * The iterator to access the working tree. If set to <code>null</code> this
258 	 * merger will not touch the working tree.
259 	 * @since 3.0
260 	 */
261 	protected WorkingTreeIterator workingTreeIterator;
262 
263 	/**
264 	 * our merge algorithm
265 	 * @since 3.0
266 	 */
267 	protected MergeAlgorithm mergeAlgorithm;
268 
269 	/**
270 	 * @param local
271 	 * @param inCore
272 	 */
273 	protected ResolveMerger(Repository local, boolean inCore) {
274 		super(local);
275 		SupportedAlgorithm diffAlg = local.getConfig().getEnum(
276 				ConfigConstants.CONFIG_DIFF_SECTION, null,
277 				ConfigConstants.CONFIG_KEY_ALGORITHM,
278 				SupportedAlgorithm.HISTOGRAM);
279 		mergeAlgorithm = new MergeAlgorithm(DiffAlgorithm.getAlgorithm(diffAlg));
280 		commitNames = new String[] { "BASE", "OURS", "THEIRS" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
281 		this.inCore = inCore;
282 
283 		if (inCore) {
284 			implicitDirCache = false;
285 			dircache = DirCache.newInCore();
286 		} else {
287 			implicitDirCache = true;
288 		}
289 	}
290 
291 	/**
292 	 * @param local
293 	 */
294 	protected ResolveMerger(Repository local) {
295 		this(local, false);
296 	}
297 
298 	@Override
299 	protected boolean mergeImpl() throws IOException {
300 		if (implicitDirCache)
301 			dircache = getRepository().lockDirCache();
302 
303 		try {
304 			return mergeTrees(mergeBase(), sourceTrees[0], sourceTrees[1],
305 					false);
306 		} finally {
307 			if (implicitDirCache)
308 				dircache.unlock();
309 		}
310 	}
311 
312 	private void checkout() throws NoWorkTreeException, IOException {
313 		// Iterate in reverse so that "folder/file" is deleted before
314 		// "folder". Otherwise this could result in a failing path because
315 		// of a non-empty directory, for which delete() would fail.
316 		for (int i = toBeDeleted.size() - 1; i >= 0; i--) {
317 			String fileName = toBeDeleted.get(i);
318 			File f = new File(db.getWorkTree(), fileName);
319 			if (!f.delete())
320 				if (!f.isDirectory())
321 					failingPaths.put(fileName,
322 							MergeFailureReason.COULD_NOT_DELETE);
323 			modifiedFiles.add(fileName);
324 		}
325 		for (Map.Entry<String, DirCacheEntry> entry : toBeCheckedOut
326 				.entrySet()) {
327 			DirCacheCheckout.checkoutEntry(db, entry.getValue(), reader);
328 			modifiedFiles.add(entry.getKey());
329 		}
330 	}
331 
332 	/**
333 	 * Reverts the worktree after an unsuccessful merge. We know that for all
334 	 * modified files the old content was in the old index and the index
335 	 * contained only stage 0. In case if inCore operation just clear the
336 	 * history of modified files.
337 	 *
338 	 * @throws IOException
339 	 * @throws CorruptObjectException
340 	 * @throws NoWorkTreeException
341 	 * @since 3.4
342 	 */
343 	protected void cleanUp() throws NoWorkTreeException,
344 			CorruptObjectException,
345 			IOException {
346 		if (inCore) {
347 			modifiedFiles.clear();
348 			return;
349 		}
350 
351 		DirCache dc = db.readDirCache();
352 		Iterator<String> mpathsIt=modifiedFiles.iterator();
353 		while(mpathsIt.hasNext()) {
354 			String mpath=mpathsIt.next();
355 			DirCacheEntry entry = dc.getEntry(mpath);
356 			if (entry != null)
357 				DirCacheCheckout.checkoutEntry(db, entry, reader);
358 			mpathsIt.remove();
359 		}
360 	}
361 
362 	/**
363 	 * adds a new path with the specified stage to the index builder
364 	 *
365 	 * @param path
366 	 * @param p
367 	 * @param stage
368 	 * @param lastMod
369 	 * @param len
370 	 * @return the entry which was added to the index
371 	 */
372 	private DirCacheEntry add(byte[] path, CanonicalTreeParser p, int stage,
373 			long lastMod, long len) {
374 		if (p != null && !p.getEntryFileMode().equals(FileMode.TREE)) {
375 			DirCacheEntry e = new DirCacheEntry(path, stage);
376 			e.setFileMode(p.getEntryFileMode());
377 			e.setObjectId(p.getEntryObjectId());
378 			e.setLastModified(lastMod);
379 			e.setLength(len);
380 			builder.add(e);
381 			return e;
382 		}
383 		return null;
384 	}
385 
386 	/**
387 	 * adds a entry to the index builder which is a copy of the specified
388 	 * DirCacheEntry
389 	 *
390 	 * @param e
391 	 *            the entry which should be copied
392 	 *
393 	 * @return the entry which was added to the index
394 	 */
395 	private DirCacheEntry keep(DirCacheEntry e) {
396 		DirCacheEntry newEntry = new DirCacheEntry(e.getPathString(),
397 				e.getStage());
398 		newEntry.setFileMode(e.getFileMode());
399 		newEntry.setObjectId(e.getObjectId());
400 		newEntry.setLastModified(e.getLastModified());
401 		newEntry.setLength(e.getLength());
402 		builder.add(newEntry);
403 		return newEntry;
404 	}
405 
406 	/**
407 	 * Processes one path and tries to merge. This method will do all do all
408 	 * trivial (not content) merges and will also detect if a merge will fail.
409 	 * The merge will fail when one of the following is true
410 	 * <ul>
411 	 * <li>the index entry does not match the entry in ours. When merging one
412 	 * branch into the current HEAD, ours will point to HEAD and theirs will
413 	 * point to the other branch. It is assumed that the index matches the HEAD
414 	 * because it will only not match HEAD if it was populated before the merge
415 	 * operation. But the merge commit should not accidentally contain
416 	 * modifications done before the merge. Check the <a href=
417 	 * "http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html#_3_way_merge"
418 	 * >git read-tree</a> documentation for further explanations.</li>
419 	 * <li>A conflict was detected and the working-tree file is dirty. When a
420 	 * conflict is detected the content-merge algorithm will try to write a
421 	 * merged version into the working-tree. If the file is dirty we would
422 	 * override unsaved data.</li>
423 	 * </ul>
424 	 *
425 	 * @param base
426 	 *            the common base for ours and theirs
427 	 * @param ours
428 	 *            the ours side of the merge. When merging a branch into the
429 	 *            HEAD ours will point to HEAD
430 	 * @param theirs
431 	 *            the theirs side of the merge. When merging a branch into the
432 	 *            current HEAD theirs will point to the branch which is merged
433 	 *            into HEAD.
434 	 * @param index
435 	 *            the index entry
436 	 * @param work
437 	 *            the file in the working tree
438 	 * @param ignoreConflicts
439 	 *            see
440 	 *            {@link ResolveMerger#mergeTrees(AbstractTreeIterator, RevTree, RevTree, boolean)}
441 	 * @return <code>false</code> if the merge will fail because the index entry
442 	 *         didn't match ours or the working-dir file was dirty and a
443 	 *         conflict occurred
444 	 * @throws MissingObjectException
445 	 * @throws IncorrectObjectTypeException
446 	 * @throws CorruptObjectException
447 	 * @throws IOException
448 	 * @since 3.5
449 	 */
450 	protected boolean processEntry(CanonicalTreeParser base,
451 			CanonicalTreeParser ours, CanonicalTreeParser theirs,
452 			DirCacheBuildIterator index, WorkingTreeIterator work,
453 			boolean ignoreConflicts)
454 			throws MissingObjectException, IncorrectObjectTypeException,
455 			CorruptObjectException, IOException {
456 		enterSubtree = true;
457 		final int modeO = tw.getRawMode(T_OURS);
458 		final int modeT = tw.getRawMode(T_THEIRS);
459 		final int modeB = tw.getRawMode(T_BASE);
460 
461 		if (modeO == 0 && modeT == 0 && modeB == 0)
462 			// File is either untracked or new, staged but uncommitted
463 			return true;
464 
465 		if (isIndexDirty())
466 			return false;
467 
468 		DirCacheEntry ourDce = null;
469 
470 		if (index == null || index.getDirCacheEntry() == null) {
471 			// create a fake DCE, but only if ours is valid. ours is kept only
472 			// in case it is valid, so a null ourDce is ok in all other cases.
473 			if (nonTree(modeO)) {
474 				ourDce = new DirCacheEntry(tw.getRawPath());
475 				ourDce.setObjectId(tw.getObjectId(T_OURS));
476 				ourDce.setFileMode(tw.getFileMode(T_OURS));
477 			}
478 		} else {
479 			ourDce = index.getDirCacheEntry();
480 		}
481 
482 		if (nonTree(modeO) && nonTree(modeT) && tw.idEqual(T_OURS, T_THEIRS)) {
483 			// OURS and THEIRS have equal content. Check the file mode
484 			if (modeO == modeT) {
485 				// content and mode of OURS and THEIRS are equal: it doesn't
486 				// matter which one we choose. OURS is chosen. Since the index
487 				// is clean (the index matches already OURS) we can keep the existing one
488 				keep(ourDce);
489 				// no checkout needed!
490 				return true;
491 			} else {
492 				// same content but different mode on OURS and THEIRS.
493 				// Try to merge the mode and report an error if this is
494 				// not possible.
495 				int newMode = mergeFileModes(modeB, modeO, modeT);
496 				if (newMode != FileMode.MISSING.getBits()) {
497 					if (newMode == modeO)
498 						// ours version is preferred
499 						keep(ourDce);
500 					else {
501 						// the preferred version THEIRS has a different mode
502 						// than ours. Check it out!
503 						if (isWorktreeDirty(work, ourDce))
504 							return false;
505 						// we know about length and lastMod only after we have written the new content.
506 						// This will happen later. Set these values to 0 for know.
507 						DirCacheEntry e = add(tw.getRawPath(), theirs,
508 								DirCacheEntry.STAGE_0, 0, 0);
509 						toBeCheckedOut.put(tw.getPathString(), e);
510 					}
511 					return true;
512 				} else {
513 					// FileModes are not mergeable. We found a conflict on modes.
514 					// For conflicting entries we don't know lastModified and length.
515 					add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
516 					add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
517 					add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
518 					unmergedPaths.add(tw.getPathString());
519 					mergeResults.put(
520 							tw.getPathString(),
521 							new MergeResult<RawText>(Collections
522 									.<RawText> emptyList()));
523 				}
524 				return true;
525 			}
526 		}
527 
528 		if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS)) {
529 			// THEIRS was not changed compared to BASE. All changes must be in
530 			// OURS. OURS is chosen. We can keep the existing entry.
531 			if (ourDce != null)
532 				keep(ourDce);
533 			// no checkout needed!
534 			return true;
535 		}
536 
537 		if (modeB == modeO && tw.idEqual(T_BASE, T_OURS)) {
538 			// OURS was not changed compared to BASE. All changes must be in
539 			// THEIRS. THEIRS is chosen.
540 
541 			// Check worktree before checking out THEIRS
542 			if (isWorktreeDirty(work, ourDce))
543 				return false;
544 			if (nonTree(modeT)) {
545 				// we know about length and lastMod only after we have written
546 				// the new content.
547 				// This will happen later. Set these values to 0 for know.
548 				DirCacheEntry e = add(tw.getRawPath(), theirs,
549 						DirCacheEntry.STAGE_0, 0, 0);
550 				if (e != null)
551 					toBeCheckedOut.put(tw.getPathString(), e);
552 				return true;
553 			} else {
554 				// we want THEIRS ... but THEIRS contains a folder or the
555 				// deletion of the path. Delete what's in the workingtree (the
556 				// workingtree is clean) but do not complain if the file is
557 				// already deleted locally. This complements the test in
558 				// isWorktreeDirty() for the same case.
559 				if (tw.getTreeCount() > T_FILE && tw.getRawMode(T_FILE) == 0)
560 					return true;
561 				toBeDeleted.add(tw.getPathString());
562 				return true;
563 			}
564 		}
565 
566 		if (tw.isSubtree()) {
567 			// file/folder conflicts: here I want to detect only file/folder
568 			// conflict between ours and theirs. file/folder conflicts between
569 			// base/index/workingTree and something else are not relevant or
570 			// detected later
571 			if (nonTree(modeO) && !nonTree(modeT)) {
572 				if (nonTree(modeB))
573 					add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
574 				add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
575 				unmergedPaths.add(tw.getPathString());
576 				enterSubtree = false;
577 				return true;
578 			}
579 			if (nonTree(modeT) && !nonTree(modeO)) {
580 				if (nonTree(modeB))
581 					add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
582 				add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
583 				unmergedPaths.add(tw.getPathString());
584 				enterSubtree = false;
585 				return true;
586 			}
587 
588 			// ours and theirs are both folders or both files (and treewalk
589 			// tells us we are in a subtree because of index or working-dir).
590 			// If they are both folders no content-merge is required - we can
591 			// return here.
592 			if (!nonTree(modeO))
593 				return true;
594 
595 			// ours and theirs are both files, just fall out of the if block
596 			// and do the content merge
597 		}
598 
599 		if (nonTree(modeO) && nonTree(modeT)) {
600 			// Check worktree before modifying files
601 			if (isWorktreeDirty(work, ourDce))
602 				return false;
603 
604 			// Don't attempt to resolve submodule link conflicts
605 			if (isGitLink(modeO) || isGitLink(modeT)) {
606 				add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
607 				add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
608 				add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
609 				unmergedPaths.add(tw.getPathString());
610 				return true;
611 			}
612 
613 			MergeResult<RawText> result = contentMerge(base, ours, theirs);
614 			if (ignoreConflicts)
615 				result.setContainsConflicts(false);
616 			updateIndex(base, ours, theirs, result);
617 			if (result.containsConflicts() && !ignoreConflicts)
618 				unmergedPaths.add(tw.getPathString());
619 			modifiedFiles.add(tw.getPathString());
620 		} else if (modeO != modeT) {
621 			// OURS or THEIRS has been deleted
622 			if (((modeO != 0 && !tw.idEqual(T_BASE, T_OURS)) || (modeT != 0 && !tw
623 					.idEqual(T_BASE, T_THEIRS)))) {
624 
625 				add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
626 				add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
627 				DirCacheEntry e = add(tw.getRawPath(), theirs,
628 						DirCacheEntry.STAGE_3, 0, 0);
629 
630 				// OURS was deleted checkout THEIRS
631 				if (modeO == 0) {
632 					// Check worktree before checking out THEIRS
633 					if (isWorktreeDirty(work, ourDce))
634 						return false;
635 					if (nonTree(modeT)) {
636 						if (e != null)
637 							toBeCheckedOut.put(tw.getPathString(), e);
638 					}
639 				}
640 
641 				unmergedPaths.add(tw.getPathString());
642 
643 				// generate a MergeResult for the deleted file
644 				mergeResults.put(tw.getPathString(),
645 						contentMerge(base, ours, theirs));
646 			}
647 		}
648 		return true;
649 	}
650 
651 	/**
652 	 * Does the content merge. The three texts base, ours and theirs are
653 	 * specified with {@link CanonicalTreeParser}. If any of the parsers is
654 	 * specified as <code>null</code> then an empty text will be used instead.
655 	 *
656 	 * @param base
657 	 * @param ours
658 	 * @param theirs
659 	 *
660 	 * @return the result of the content merge
661 	 * @throws IOException
662 	 */
663 	private MergeResult<RawText> contentMerge(CanonicalTreeParser base,
664 			CanonicalTreeParser ours, CanonicalTreeParser theirs)
665 			throws IOException {
666 		RawText baseText = base == null ? RawText.EMPTY_TEXT : getRawText(
667 				base.getEntryObjectId(), reader);
668 		RawText ourText = ours == null ? RawText.EMPTY_TEXT : getRawText(
669 				ours.getEntryObjectId(), reader);
670 		RawText theirsText = theirs == null ? RawText.EMPTY_TEXT : getRawText(
671 				theirs.getEntryObjectId(), reader);
672 		return (mergeAlgorithm.merge(RawTextComparator.DEFAULT, baseText,
673 				ourText, theirsText));
674 	}
675 
676 	private boolean isIndexDirty() {
677 		if (inCore)
678 			return false;
679 
680 		final int modeI = tw.getRawMode(T_INDEX);
681 		final int modeO = tw.getRawMode(T_OURS);
682 
683 		// Index entry has to match ours to be considered clean
684 		final boolean isDirty = nonTree(modeI)
685 				&& !(modeO == modeI && tw.idEqual(T_INDEX, T_OURS));
686 		if (isDirty)
687 			failingPaths
688 					.put(tw.getPathString(), MergeFailureReason.DIRTY_INDEX);
689 		return isDirty;
690 	}
691 
692 	private boolean isWorktreeDirty(WorkingTreeIterator work,
693 			DirCacheEntry ourDce) throws IOException {
694 		if (work == null)
695 			return false;
696 
697 		final int modeF = tw.getRawMode(T_FILE);
698 		final int modeO = tw.getRawMode(T_OURS);
699 
700 		// Worktree entry has to match ours to be considered clean
701 		boolean isDirty;
702 		if (ourDce != null)
703 			isDirty = work.isModified(ourDce, true, reader);
704 		else {
705 			isDirty = work.isModeDifferent(modeO);
706 			if (!isDirty && nonTree(modeF))
707 				isDirty = !tw.idEqual(T_FILE, T_OURS);
708 		}
709 
710 		// Ignore existing empty directories
711 		if (isDirty && modeF == FileMode.TYPE_TREE
712 				&& modeO == FileMode.TYPE_MISSING)
713 			isDirty = false;
714 		if (isDirty)
715 			failingPaths.put(tw.getPathString(),
716 					MergeFailureReason.DIRTY_WORKTREE);
717 		return isDirty;
718 	}
719 
720 	/**
721 	 * Updates the index after a content merge has happened. If no conflict has
722 	 * occurred this includes persisting the merged content to the object
723 	 * database. In case of conflicts this method takes care to write the
724 	 * correct stages to the index.
725 	 *
726 	 * @param base
727 	 * @param ours
728 	 * @param theirs
729 	 * @param result
730 	 * @throws FileNotFoundException
731 	 * @throws IOException
732 	 */
733 	private void updateIndex(CanonicalTreeParser base,
734 			CanonicalTreeParser ours, CanonicalTreeParser theirs,
735 			MergeResult<RawText> result) throws FileNotFoundException,
736 			IOException {
737 		File mergedFile = !inCore ? writeMergedFile(result) : null;
738 		if (result.containsConflicts()) {
739 			// A conflict occurred, the file will contain conflict markers
740 			// the index will be populated with the three stages and the
741 			// workdir (if used) contains the halfway merged content.
742 			add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
743 			add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
744 			add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
745 			mergeResults.put(tw.getPathString(), result);
746 			return;
747 		}
748 
749 		// No conflict occurred, the file will contain fully merged content.
750 		// The index will be populated with the new merged version.
751 		DirCacheEntry dce = new DirCacheEntry(tw.getPathString());
752 
753 		// Set the mode for the new content. Fall back to REGULAR_FILE if
754 		// we can't merge modes of OURS and THEIRS.
755 		int newMode = mergeFileModes(
756 				tw.getRawMode(0),
757 				tw.getRawMode(1),
758 				tw.getRawMode(2));
759 		dce.setFileMode(newMode == FileMode.MISSING.getBits()
760 				? FileMode.REGULAR_FILE
761 				: FileMode.fromBits(newMode));
762 		if (mergedFile != null) {
763 			long len = mergedFile.length();
764 			dce.setLastModified(mergedFile.lastModified());
765 			dce.setLength((int) len);
766 			InputStream is = new FileInputStream(mergedFile);
767 			try {
768 				dce.setObjectId(getObjectInserter().insert(OBJ_BLOB, len, is));
769 			} finally {
770 				is.close();
771 			}
772 		} else
773 			dce.setObjectId(insertMergeResult(result));
774 		builder.add(dce);
775 	}
776 
777 	/**
778 	 * Writes merged file content to the working tree.
779 	 *
780 	 * @param result
781 	 *            the result of the content merge
782 	 * @return the working tree file to which the merged content was written.
783 	 * @throws FileNotFoundException
784 	 * @throws IOException
785 	 */
786 	private File writeMergedFile(MergeResult<RawText> result)
787 			throws FileNotFoundException, IOException {
788 		File workTree = db.getWorkTree();
789 		if (workTree == null)
790 			// TODO: This should be handled by WorkingTreeIterators which
791 			// support write operations
792 			throw new UnsupportedOperationException();
793 
794 		FS fs = db.getFS();
795 		File of = new File(workTree, tw.getPathString());
796 		File parentFolder = of.getParentFile();
797 		if (!fs.exists(parentFolder))
798 			parentFolder.mkdirs();
799 		try (OutputStream os = new BufferedOutputStream(
800 				new FileOutputStream(of))) {
801 			new MergeFormatter().formatMerge(os, result,
802 					Arrays.asList(commitNames), CHARACTER_ENCODING);
803 		}
804 		return of;
805 	}
806 
807 	private ObjectId insertMergeResult(MergeResult<RawText> result)
808 			throws IOException {
809 		TemporaryBuffer.LocalFile buf = new TemporaryBuffer.LocalFile(
810 				db.getDirectory(), 10 << 20);
811 		try {
812 			new MergeFormatter().formatMerge(buf, result,
813 					Arrays.asList(commitNames), CHARACTER_ENCODING);
814 			buf.close();
815 			try (InputStream in = buf.openInputStream()) {
816 				return getObjectInserter().insert(OBJ_BLOB, buf.length(), in);
817 			}
818 		} finally {
819 			buf.destroy();
820 		}
821 	}
822 
823 	/**
824 	 * Try to merge filemodes. If only ours or theirs have changed the mode
825 	 * (compared to base) we choose that one. If ours and theirs have equal
826 	 * modes return that one. If also that is not the case the modes are not
827 	 * mergeable. Return {@link FileMode#MISSING} int that case.
828 	 *
829 	 * @param modeB
830 	 *            filemode found in BASE
831 	 * @param modeO
832 	 *            filemode found in OURS
833 	 * @param modeT
834 	 *            filemode found in THEIRS
835 	 *
836 	 * @return the merged filemode or {@link FileMode#MISSING} in case of a
837 	 *         conflict
838 	 */
839 	private int mergeFileModes(int modeB, int modeO, int modeT) {
840 		if (modeO == modeT)
841 			return modeO;
842 		if (modeB == modeO)
843 			// Base equal to Ours -> chooses Theirs if that is not missing
844 			return (modeT == FileMode.MISSING.getBits()) ? modeO : modeT;
845 		if (modeB == modeT)
846 			// Base equal to Theirs -> chooses Ours if that is not missing
847 			return (modeO == FileMode.MISSING.getBits()) ? modeT : modeO;
848 		return FileMode.MISSING.getBits();
849 	}
850 
851 	private static RawText getRawText(ObjectId id, ObjectReader reader)
852 			throws IOException {
853 		if (id.equals(ObjectId.zeroId()))
854 			return new RawText(new byte[] {});
855 		return new RawText(reader.open(id, OBJ_BLOB).getCachedBytes());
856 	}
857 
858 	private static boolean nonTree(final int mode) {
859 		return mode != 0 && !FileMode.TREE.equals(mode);
860 	}
861 
862 	private static boolean isGitLink(final int mode) {
863 		return FileMode.GITLINK.equals(mode);
864 	}
865 
866 	@Override
867 	public ObjectId getResultTreeId() {
868 		return (resultTree == null) ? null : resultTree.toObjectId();
869 	}
870 
871 	/**
872 	 * @param commitNames
873 	 *            the names of the commits as they would appear in conflict
874 	 *            markers
875 	 */
876 	public void setCommitNames(String[] commitNames) {
877 		this.commitNames = commitNames;
878 	}
879 
880 	/**
881 	 * @return the names of the commits as they would appear in conflict
882 	 *         markers.
883 	 */
884 	public String[] getCommitNames() {
885 		return commitNames;
886 	}
887 
888 	/**
889 	 * @return the paths with conflicts. This is a subset of the files listed
890 	 *         by {@link #getModifiedFiles()}
891 	 */
892 	public List<String> getUnmergedPaths() {
893 		return unmergedPaths;
894 	}
895 
896 	/**
897 	 * @return the paths of files which have been modified by this merge. A
898 	 *         file will be modified if a content-merge works on this path or if
899 	 *         the merge algorithm decides to take the theirs-version. This is a
900 	 *         superset of the files listed by {@link #getUnmergedPaths()}.
901 	 */
902 	public List<String> getModifiedFiles() {
903 		return modifiedFiles;
904 	}
905 
906 	/**
907 	 * @return a map which maps the paths of files which have to be checked out
908 	 *         because the merge created new fully-merged content for this file
909 	 *         into the index. This means: the merge wrote a new stage 0 entry
910 	 *         for this path.
911 	 */
912 	public Map<String, DirCacheEntry> getToBeCheckedOut() {
913 		return toBeCheckedOut;
914 	}
915 
916 	/**
917 	 * @return the mergeResults
918 	 */
919 	public Map<String, MergeResult<? extends Sequence>> getMergeResults() {
920 		return mergeResults;
921 	}
922 
923 	/**
924 	 * @return lists paths causing this merge to fail (not stopped because of a
925 	 *         conflict). <code>null</code> is returned if this merge didn't
926 	 *         fail.
927 	 */
928 	public Map<String, MergeFailureReason> getFailingPaths() {
929 		return (failingPaths.size() == 0) ? null : failingPaths;
930 	}
931 
932 	/**
933 	 * Returns whether this merge failed (i.e. not stopped because of a
934 	 * conflict)
935 	 *
936 	 * @return <code>true</code> if a failure occurred, <code>false</code>
937 	 *         otherwise
938 	 */
939 	public boolean failed() {
940 		return failingPaths.size() > 0;
941 	}
942 
943 	/**
944 	 * Sets the DirCache which shall be used by this merger. If the DirCache is
945 	 * not set explicitly and if this merger doesn't work in-core, this merger
946 	 * will implicitly get and lock a default DirCache. If the DirCache is
947 	 * explicitly set the caller is responsible to lock it in advance. Finally
948 	 * the merger will call {@link DirCache#commit()} which requires that the
949 	 * DirCache is locked. If the {@link #mergeImpl()} returns without throwing
950 	 * an exception the lock will be released. In case of exceptions the caller
951 	 * is responsible to release the lock.
952 	 *
953 	 * @param dc
954 	 *            the DirCache to set
955 	 */
956 	public void setDirCache(DirCache dc) {
957 		this.dircache = dc;
958 		implicitDirCache = false;
959 	}
960 
961 	/**
962 	 * Sets the WorkingTreeIterator to be used by this merger. If no
963 	 * WorkingTreeIterator is set this merger will ignore the working tree and
964 	 * fail if a content merge is necessary.
965 	 * <p>
966 	 * TODO: enhance WorkingTreeIterator to support write operations. Then this
967 	 * merger will be able to merge with a different working tree abstraction.
968 	 *
969 	 * @param workingTreeIterator
970 	 *            the workingTreeIt to set
971 	 */
972 	public void setWorkingTreeIterator(WorkingTreeIterator workingTreeIterator) {
973 		this.workingTreeIterator = workingTreeIterator;
974 	}
975 
976 
977 	/**
978 	 * The resolve conflict way of three way merging
979 	 *
980 	 * @param baseTree
981 	 * @param headTree
982 	 * @param mergeTree
983 	 * @param ignoreConflicts
984 	 *            Controls what to do in case a content-merge is done and a
985 	 *            conflict is detected. The default setting for this should be
986 	 *            <code>false</code>. In this case the working tree file is
987 	 *            filled with new content (containing conflict markers) and the
988 	 *            index is filled with multiple stages containing BASE, OURS and
989 	 *            THEIRS content. Having such non-0 stages is the sign to git
990 	 *            tools that there are still conflicts for that path.
991 	 *            <p>
992 	 *            If <code>true</code> is specified the behavior is different.
993 	 *            In case a conflict is detected the working tree file is again
994 	 *            filled with new content (containing conflict markers). But
995 	 *            also stage 0 of the index is filled with that content. No
996 	 *            other stages are filled. Means: there is no conflict on that
997 	 *            path but the new content (including conflict markers) is
998 	 *            stored as successful merge result. This is needed in the
999 	 *            context of {@link RecursiveMerger} where when determining
1000 	 *            merge bases we don't want to deal with content-merge
1001 	 *            conflicts.
1002 	 * @return whether the trees merged cleanly
1003 	 * @throws IOException
1004 	 * @since 3.5
1005 	 */
1006 	protected boolean mergeTrees(AbstractTreeIterator baseTree,
1007 			RevTree headTree, RevTree mergeTree, boolean ignoreConflicts)
1008 			throws IOException {
1009 
1010 		builder = dircache.builder();
1011 		DirCacheBuildIterator buildIt = new DirCacheBuildIterator(builder);
1012 
1013 		tw = new NameConflictTreeWalk(reader);
1014 		tw.addTree(baseTree);
1015 		tw.addTree(headTree);
1016 		tw.addTree(mergeTree);
1017 		tw.addTree(buildIt);
1018 		if (workingTreeIterator != null) {
1019 			tw.addTree(workingTreeIterator);
1020 		} else {
1021 			tw.setFilter(TreeFilter.ANY_DIFF);
1022 		}
1023 
1024 		if (!mergeTreeWalk(tw, ignoreConflicts)) {
1025 			return false;
1026 		}
1027 
1028 		if (!inCore) {
1029 			// No problem found. The only thing left to be done is to
1030 			// checkout all files from "theirs" which have been selected to
1031 			// go into the new index.
1032 			checkout();
1033 
1034 			// All content-merges are successfully done. If we can now write the
1035 			// new index we are on quite safe ground. Even if the checkout of
1036 			// files coming from "theirs" fails the user can work around such
1037 			// failures by checking out the index again.
1038 			if (!builder.commit()) {
1039 				cleanUp();
1040 				throw new IndexWriteException();
1041 			}
1042 			builder = null;
1043 
1044 		} else {
1045 			builder.finish();
1046 			builder = null;
1047 		}
1048 
1049 		if (getUnmergedPaths().isEmpty() && !failed()) {
1050 			resultTree = dircache.writeTree(getObjectInserter());
1051 			return true;
1052 		} else {
1053 			resultTree = null;
1054 			return false;
1055 		}
1056 	}
1057 
1058 	/**
1059 	 * Process the given TreeWalk's entries.
1060 	 *
1061 	 * @param treeWalk
1062 	 *            The walk to iterate over.
1063 	 * @param ignoreConflicts
1064 	 *            see
1065 	 *            {@link ResolveMerger#mergeTrees(AbstractTreeIterator, RevTree, RevTree, boolean)}
1066 	 * @return Whether the trees merged cleanly.
1067 	 * @throws IOException
1068 	 * @since 3.5
1069 	 */
1070 	protected boolean mergeTreeWalk(TreeWalk treeWalk, boolean ignoreConflicts)
1071 			throws IOException {
1072 		boolean hasWorkingTreeIterator = tw.getTreeCount() > T_FILE;
1073 		while (treeWalk.next()) {
1074 			if (!processEntry(
1075 					treeWalk.getTree(T_BASE, CanonicalTreeParser.class),
1076 					treeWalk.getTree(T_OURS, CanonicalTreeParser.class),
1077 					treeWalk.getTree(T_THEIRS, CanonicalTreeParser.class),
1078 					treeWalk.getTree(T_INDEX, DirCacheBuildIterator.class),
1079 					hasWorkingTreeIterator ? treeWalk.getTree(T_FILE,
1080 							WorkingTreeIterator.class) : null, ignoreConflicts)) {
1081 				cleanUp();
1082 				return false;
1083 			}
1084 			if (treeWalk.isSubtree() && enterSubtree)
1085 				treeWalk.enterSubtree();
1086 		}
1087 		return true;
1088 	}
1089 }