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