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 	 * @return <code>false</code> if the merge will fail because the index entry
484 	 *         didn't match ours or the working-dir file was dirty and a
485 	 *         conflict occurred
486 	 * @throws MissingObjectException
487 	 * @throws IncorrectObjectTypeException
488 	 * @throws CorruptObjectException
489 	 * @throws IOException
490 	 * @since 3.5
491 	 * @deprecated
492 	 */
493 	@Deprecated
494 	protected boolean processEntry(CanonicalTreeParser base,
495 			CanonicalTreeParser ours, CanonicalTreeParser theirs,
496 			DirCacheBuildIterator index, WorkingTreeIterator work,
497 			boolean ignoreConflicts) throws MissingObjectException,
498 			IncorrectObjectTypeException, CorruptObjectException, IOException {
499 		return processEntry(base, ours, theirs, index, work, ignoreConflicts,
500 				null);
501 	}
502 
503 	/**
504 	 * Processes one path and tries to merge taking git attributes in account.
505 	 * This method will do all trivial (not content) merges and will also detect
506 	 * if a merge will fail. The merge will fail when one of the following is
507 	 * true
508 	 * <ul>
509 	 * <li>the index entry does not match the entry in ours. When merging one
510 	 * branch into the current HEAD, ours will point to HEAD and theirs will
511 	 * point to the other branch. It is assumed that the index matches the HEAD
512 	 * because it will only not match HEAD if it was populated before the merge
513 	 * operation. But the merge commit should not accidentally contain
514 	 * modifications done before the merge. Check the <a href=
515 	 * "http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html#_3_way_merge"
516 	 * >git read-tree</a> documentation for further explanations.</li>
517 	 * <li>A conflict was detected and the working-tree file is dirty. When a
518 	 * conflict is detected the content-merge algorithm will try to write a
519 	 * merged version into the working-tree. If the file is dirty we would
520 	 * override unsaved data.</li>
521 	 * </ul>
522 	 *
523 	 * @param base
524 	 *            the common base for ours and theirs
525 	 * @param ours
526 	 *            the ours side of the merge. When merging a branch into the
527 	 *            HEAD ours will point to HEAD
528 	 * @param theirs
529 	 *            the theirs side of the merge. When merging a branch into the
530 	 *            current HEAD theirs will point to the branch which is merged
531 	 *            into HEAD.
532 	 * @param index
533 	 *            the index entry
534 	 * @param work
535 	 *            the file in the working tree
536 	 * @param ignoreConflicts
537 	 *            see
538 	 *            {@link ResolveMerger#mergeTrees(AbstractTreeIterator, RevTree, RevTree, boolean)}
539 	 * @param attributes
540 	 *            the attributes defined for this entry
541 	 * @return <code>false</code> if the merge will fail because the index entry
542 	 *         didn't match ours or the working-dir file was dirty and a
543 	 *         conflict occurred
544 	 * @throws MissingObjectException
545 	 * @throws IncorrectObjectTypeException
546 	 * @throws CorruptObjectException
547 	 * @throws IOException
548 	 * @since 4.9
549 	 */
550 	protected boolean processEntry(CanonicalTreeParser base,
551 			CanonicalTreeParser ours, CanonicalTreeParser theirs,
552 			DirCacheBuildIterator index, WorkingTreeIterator work,
553 			boolean ignoreConflicts, Attributes attributes)
554 			throws MissingObjectException, IncorrectObjectTypeException,
555 			CorruptObjectException, IOException {
556 		enterSubtree = true;
557 		final int modeO = tw.getRawMode(T_OURS);
558 		final int modeT = tw.getRawMode(T_THEIRS);
559 		final int modeB = tw.getRawMode(T_BASE);
560 
561 		if (modeO == 0 && modeT == 0 && modeB == 0)
562 			// File is either untracked or new, staged but uncommitted
563 			return true;
564 
565 		if (isIndexDirty())
566 			return false;
567 
568 		DirCacheEntry ourDce = null;
569 
570 		if (index == null || index.getDirCacheEntry() == null) {
571 			// create a fake DCE, but only if ours is valid. ours is kept only
572 			// in case it is valid, so a null ourDce is ok in all other cases.
573 			if (nonTree(modeO)) {
574 				ourDce = new DirCacheEntry(tw.getRawPath());
575 				ourDce.setObjectId(tw.getObjectId(T_OURS));
576 				ourDce.setFileMode(tw.getFileMode(T_OURS));
577 			}
578 		} else {
579 			ourDce = index.getDirCacheEntry();
580 		}
581 
582 		if (nonTree(modeO) && nonTree(modeT) && tw.idEqual(T_OURS, T_THEIRS)) {
583 			// OURS and THEIRS have equal content. Check the file mode
584 			if (modeO == modeT) {
585 				// content and mode of OURS and THEIRS are equal: it doesn't
586 				// matter which one we choose. OURS is chosen. Since the index
587 				// is clean (the index matches already OURS) we can keep the existing one
588 				keep(ourDce);
589 				// no checkout needed!
590 				return true;
591 			} else {
592 				// same content but different mode on OURS and THEIRS.
593 				// Try to merge the mode and report an error if this is
594 				// not possible.
595 				int newMode = mergeFileModes(modeB, modeO, modeT);
596 				if (newMode != FileMode.MISSING.getBits()) {
597 					if (newMode == modeO)
598 						// ours version is preferred
599 						keep(ourDce);
600 					else {
601 						// the preferred version THEIRS has a different mode
602 						// than ours. Check it out!
603 						if (isWorktreeDirty(work, ourDce))
604 							return false;
605 						// we know about length and lastMod only after we have written the new content.
606 						// This will happen later. Set these values to 0 for know.
607 						DirCacheEntry e = add(tw.getRawPath(), theirs,
608 								DirCacheEntry.STAGE_0, 0, 0);
609 						toBeCheckedOut.put(tw.getPathString(), e);
610 					}
611 					return true;
612 				} else {
613 					// FileModes are not mergeable. We found a conflict on modes.
614 					// For conflicting entries we don't know lastModified and length.
615 					add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
616 					add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
617 					add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
618 					unmergedPaths.add(tw.getPathString());
619 					mergeResults.put(
620 							tw.getPathString(),
621 							new MergeResult<>(Collections
622 									.<RawText> emptyList()));
623 				}
624 				return true;
625 			}
626 		}
627 
628 		if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS)) {
629 			// THEIRS was not changed compared to BASE. All changes must be in
630 			// OURS. OURS is chosen. We can keep the existing entry.
631 			if (ourDce != null)
632 				keep(ourDce);
633 			// no checkout needed!
634 			return true;
635 		}
636 
637 		if (modeB == modeO && tw.idEqual(T_BASE, T_OURS)) {
638 			// OURS was not changed compared to BASE. All changes must be in
639 			// THEIRS. THEIRS is chosen.
640 
641 			// Check worktree before checking out THEIRS
642 			if (isWorktreeDirty(work, ourDce))
643 				return false;
644 			if (nonTree(modeT)) {
645 				// we know about length and lastMod only after we have written
646 				// the new content.
647 				// This will happen later. Set these values to 0 for know.
648 				DirCacheEntry e = add(tw.getRawPath(), theirs,
649 						DirCacheEntry.STAGE_0, 0, 0);
650 				if (e != null)
651 					toBeCheckedOut.put(tw.getPathString(), e);
652 				return true;
653 			} else {
654 				// we want THEIRS ... but THEIRS contains a folder or the
655 				// deletion of the path. Delete what's in the workingtree (the
656 				// workingtree is clean) but do not complain if the file is
657 				// already deleted locally. This complements the test in
658 				// isWorktreeDirty() for the same case.
659 				if (tw.getTreeCount() > T_FILE && tw.getRawMode(T_FILE) == 0)
660 					return true;
661 				toBeDeleted.add(tw.getPathString());
662 				return true;
663 			}
664 		}
665 
666 		if (tw.isSubtree()) {
667 			// file/folder conflicts: here I want to detect only file/folder
668 			// conflict between ours and theirs. file/folder conflicts between
669 			// base/index/workingTree and something else are not relevant or
670 			// detected later
671 			if (nonTree(modeO) && !nonTree(modeT)) {
672 				if (nonTree(modeB))
673 					add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
674 				add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
675 				unmergedPaths.add(tw.getPathString());
676 				enterSubtree = false;
677 				return true;
678 			}
679 			if (nonTree(modeT) && !nonTree(modeO)) {
680 				if (nonTree(modeB))
681 					add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
682 				add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
683 				unmergedPaths.add(tw.getPathString());
684 				enterSubtree = false;
685 				return true;
686 			}
687 
688 			// ours and theirs are both folders or both files (and treewalk
689 			// tells us we are in a subtree because of index or working-dir).
690 			// If they are both folders no content-merge is required - we can
691 			// return here.
692 			if (!nonTree(modeO))
693 				return true;
694 
695 			// ours and theirs are both files, just fall out of the if block
696 			// and do the content merge
697 		}
698 
699 		if (nonTree(modeO) && nonTree(modeT)) {
700 			// Check worktree before modifying files
701 			if (isWorktreeDirty(work, ourDce))
702 				return false;
703 
704 			// Don't attempt to resolve submodule link conflicts
705 			if (isGitLink(modeO) || isGitLink(modeT)
706 					|| !attributes.canBeContentMerged()) {
707 				add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
708 				add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
709 				add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
710 				unmergedPaths.add(tw.getPathString());
711 				return true;
712 			}
713 
714 			MergeResult<RawText> result = contentMerge(base, ours, theirs);
715 			if (ignoreConflicts) {
716 				result.setContainsConflicts(false);
717 			}
718 			updateIndex(base, ours, theirs, result);
719 			if (result.containsConflicts() && !ignoreConflicts)
720 				unmergedPaths.add(tw.getPathString());
721 			modifiedFiles.add(tw.getPathString());
722 		} else if (modeO != modeT) {
723 			// OURS or THEIRS has been deleted
724 			if (((modeO != 0 && !tw.idEqual(T_BASE, T_OURS)) || (modeT != 0 && !tw
725 					.idEqual(T_BASE, T_THEIRS)))) {
726 
727 				add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
728 				add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
729 				DirCacheEntry e = add(tw.getRawPath(), theirs,
730 						DirCacheEntry.STAGE_3, 0, 0);
731 
732 				// OURS was deleted checkout THEIRS
733 				if (modeO == 0) {
734 					// Check worktree before checking out THEIRS
735 					if (isWorktreeDirty(work, ourDce))
736 						return false;
737 					if (nonTree(modeT)) {
738 						if (e != null)
739 							toBeCheckedOut.put(tw.getPathString(), e);
740 					}
741 				}
742 
743 				unmergedPaths.add(tw.getPathString());
744 
745 				// generate a MergeResult for the deleted file
746 				mergeResults.put(tw.getPathString(),
747 						contentMerge(base, ours, theirs));
748 			}
749 		}
750 		return true;
751 	}
752 
753 	/**
754 	 * Does the content merge. The three texts base, ours and theirs are
755 	 * specified with {@link CanonicalTreeParser}. If any of the parsers is
756 	 * specified as <code>null</code> then an empty text will be used instead.
757 	 *
758 	 * @param base
759 	 * @param ours
760 	 * @param theirs
761 	 *
762 	 * @return the result of the content merge
763 	 * @throws IOException
764 	 */
765 	private MergeResult<RawText> contentMerge(CanonicalTreeParser base,
766 			CanonicalTreeParser ours, CanonicalTreeParser theirs)
767 			throws IOException {
768 		RawText baseText = base == null ? RawText.EMPTY_TEXT : getRawText(
769 				base.getEntryObjectId(), reader);
770 		RawText ourText = ours == null ? RawText.EMPTY_TEXT : getRawText(
771 				ours.getEntryObjectId(), reader);
772 		RawText theirsText = theirs == null ? RawText.EMPTY_TEXT : getRawText(
773 				theirs.getEntryObjectId(), reader);
774 		return (mergeAlgorithm.merge(RawTextComparator.DEFAULT, baseText,
775 				ourText, theirsText));
776 	}
777 
778 	private boolean isIndexDirty() {
779 		if (inCore)
780 			return false;
781 
782 		final int modeI = tw.getRawMode(T_INDEX);
783 		final int modeO = tw.getRawMode(T_OURS);
784 
785 		// Index entry has to match ours to be considered clean
786 		final boolean isDirty = nonTree(modeI)
787 				&& !(modeO == modeI && tw.idEqual(T_INDEX, T_OURS));
788 		if (isDirty)
789 			failingPaths
790 					.put(tw.getPathString(), MergeFailureReason.DIRTY_INDEX);
791 		return isDirty;
792 	}
793 
794 	private boolean isWorktreeDirty(WorkingTreeIterator work,
795 			DirCacheEntry ourDce) throws IOException {
796 		if (work == null)
797 			return false;
798 
799 		final int modeF = tw.getRawMode(T_FILE);
800 		final int modeO = tw.getRawMode(T_OURS);
801 
802 		// Worktree entry has to match ours to be considered clean
803 		boolean isDirty;
804 		if (ourDce != null)
805 			isDirty = work.isModified(ourDce, true, reader);
806 		else {
807 			isDirty = work.isModeDifferent(modeO);
808 			if (!isDirty && nonTree(modeF))
809 				isDirty = !tw.idEqual(T_FILE, T_OURS);
810 		}
811 
812 		// Ignore existing empty directories
813 		if (isDirty && modeF == FileMode.TYPE_TREE
814 				&& modeO == FileMode.TYPE_MISSING)
815 			isDirty = false;
816 		if (isDirty)
817 			failingPaths.put(tw.getPathString(),
818 					MergeFailureReason.DIRTY_WORKTREE);
819 		return isDirty;
820 	}
821 
822 	/**
823 	 * Updates the index after a content merge has happened. If no conflict has
824 	 * occurred this includes persisting the merged content to the object
825 	 * database. In case of conflicts this method takes care to write the
826 	 * correct stages to the index.
827 	 *
828 	 * @param base
829 	 * @param ours
830 	 * @param theirs
831 	 * @param result
832 	 * @throws FileNotFoundException
833 	 * @throws IOException
834 	 */
835 	private void updateIndex(CanonicalTreeParser base,
836 			CanonicalTreeParser ours, CanonicalTreeParser theirs,
837 			MergeResult<RawText> result) throws FileNotFoundException,
838 			IOException {
839 		File mergedFile = !inCore ? writeMergedFile(result) : null;
840 
841 		if (result.containsConflicts()) {
842 			// A conflict occurred, the file will contain conflict markers
843 			// the index will be populated with the three stages and the
844 			// workdir (if used) contains the halfway merged content.
845 			add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
846 			add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
847 			add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
848 			mergeResults.put(tw.getPathString(), result);
849 			return;
850 		}
851 
852 		// No conflict occurred, the file will contain fully merged content.
853 		// The index will be populated with the new merged version.
854 		DirCacheEntry dce = new DirCacheEntry(tw.getPathString());
855 
856 		// Set the mode for the new content. Fall back to REGULAR_FILE if
857 		// we can't merge modes of OURS and THEIRS.
858 		int newMode = mergeFileModes(
859 				tw.getRawMode(0),
860 				tw.getRawMode(1),
861 				tw.getRawMode(2));
862 		dce.setFileMode(newMode == FileMode.MISSING.getBits()
863 				? FileMode.REGULAR_FILE
864 				: FileMode.fromBits(newMode));
865 		if (mergedFile != null) {
866 			long len = mergedFile.length();
867 			dce.setLastModified(FS.DETECTED.lastModified(mergedFile));
868 			dce.setLength((int) len);
869 			InputStream is = new FileInputStream(mergedFile);
870 			try {
871 				dce.setObjectId(getObjectInserter().insert(OBJ_BLOB, len, is));
872 			} finally {
873 				is.close();
874 			}
875 		} else
876 			dce.setObjectId(insertMergeResult(result));
877 		builder.add(dce);
878 	}
879 
880 	/**
881 	 * Writes merged file content to the working tree.
882 	 *
883 	 * @param result
884 	 *            the result of the content merge
885 	 * @return the working tree file to which the merged content was written.
886 	 * @throws FileNotFoundException
887 	 * @throws IOException
888 	 */
889 	private File writeMergedFile(MergeResult<RawText> result)
890 			throws FileNotFoundException, IOException {
891 		File workTree = nonNullRepo().getWorkTree();
892 		FS fs = nonNullRepo().getFS();
893 		File of = new File(workTree, tw.getPathString());
894 		File parentFolder = of.getParentFile();
895 		if (!fs.exists(parentFolder))
896 			parentFolder.mkdirs();
897 		try (OutputStream os = new BufferedOutputStream(
898 				new FileOutputStream(of))) {
899 			new MergeFormatter().formatMerge(os, result,
900 					Arrays.asList(commitNames), CHARACTER_ENCODING);
901 		}
902 		return of;
903 	}
904 
905 	private ObjectId insertMergeResult(MergeResult<RawText> result)
906 			throws IOException {
907 		TemporaryBuffer.LocalFile buf = new TemporaryBuffer.LocalFile(
908 				db != null ? nonNullRepo().getDirectory() : null, inCoreLimit);
909 		try {
910 			new MergeFormatter().formatMerge(buf, result,
911 					Arrays.asList(commitNames), CHARACTER_ENCODING);
912 			buf.close();
913 			try (InputStream in = buf.openInputStream()) {
914 				return getObjectInserter().insert(OBJ_BLOB, buf.length(), in);
915 			}
916 		} finally {
917 			buf.destroy();
918 		}
919 	}
920 
921 	/**
922 	 * Try to merge filemodes. If only ours or theirs have changed the mode
923 	 * (compared to base) we choose that one. If ours and theirs have equal
924 	 * modes return that one. If also that is not the case the modes are not
925 	 * mergeable. Return {@link FileMode#MISSING} int that case.
926 	 *
927 	 * @param modeB
928 	 *            filemode found in BASE
929 	 * @param modeO
930 	 *            filemode found in OURS
931 	 * @param modeT
932 	 *            filemode found in THEIRS
933 	 *
934 	 * @return the merged filemode or {@link FileMode#MISSING} in case of a
935 	 *         conflict
936 	 */
937 	private int mergeFileModes(int modeB, int modeO, int modeT) {
938 		if (modeO == modeT)
939 			return modeO;
940 		if (modeB == modeO)
941 			// Base equal to Ours -> chooses Theirs if that is not missing
942 			return (modeT == FileMode.MISSING.getBits()) ? modeO : modeT;
943 		if (modeB == modeT)
944 			// Base equal to Theirs -> chooses Ours if that is not missing
945 			return (modeO == FileMode.MISSING.getBits()) ? modeT : modeO;
946 		return FileMode.MISSING.getBits();
947 	}
948 
949 	private static RawText getRawText(ObjectId id, ObjectReader reader)
950 			throws IOException {
951 		if (id.equals(ObjectId.zeroId()))
952 			return new RawText(new byte[] {});
953 		return new RawText(reader.open(id, OBJ_BLOB).getCachedBytes());
954 	}
955 
956 	private static boolean nonTree(final int mode) {
957 		return mode != 0 && !FileMode.TREE.equals(mode);
958 	}
959 
960 	private static boolean isGitLink(final int mode) {
961 		return FileMode.GITLINK.equals(mode);
962 	}
963 
964 	@Override
965 	public ObjectId getResultTreeId() {
966 		return (resultTree == null) ? null : resultTree.toObjectId();
967 	}
968 
969 	/**
970 	 * @param commitNames
971 	 *            the names of the commits as they would appear in conflict
972 	 *            markers
973 	 */
974 	public void setCommitNames(String[] commitNames) {
975 		this.commitNames = commitNames;
976 	}
977 
978 	/**
979 	 * @return the names of the commits as they would appear in conflict
980 	 *         markers.
981 	 */
982 	public String[] getCommitNames() {
983 		return commitNames;
984 	}
985 
986 	/**
987 	 * @return the paths with conflicts. This is a subset of the files listed
988 	 *         by {@link #getModifiedFiles()}
989 	 */
990 	public List<String> getUnmergedPaths() {
991 		return unmergedPaths;
992 	}
993 
994 	/**
995 	 * @return the paths of files which have been modified by this merge. A
996 	 *         file will be modified if a content-merge works on this path or if
997 	 *         the merge algorithm decides to take the theirs-version. This is a
998 	 *         superset of the files listed by {@link #getUnmergedPaths()}.
999 	 */
1000 	public List<String> getModifiedFiles() {
1001 		return modifiedFiles;
1002 	}
1003 
1004 	/**
1005 	 * @return a map which maps the paths of files which have to be checked out
1006 	 *         because the merge created new fully-merged content for this file
1007 	 *         into the index. This means: the merge wrote a new stage 0 entry
1008 	 *         for this path.
1009 	 */
1010 	public Map<String, DirCacheEntry> getToBeCheckedOut() {
1011 		return toBeCheckedOut;
1012 	}
1013 
1014 	/**
1015 	 * @return the mergeResults
1016 	 */
1017 	public Map<String, MergeResult<? extends Sequence>> getMergeResults() {
1018 		return mergeResults;
1019 	}
1020 
1021 	/**
1022 	 * @return lists paths causing this merge to fail (not stopped because of a
1023 	 *         conflict). <code>null</code> is returned if this merge didn't
1024 	 *         fail.
1025 	 */
1026 	public Map<String, MergeFailureReason> getFailingPaths() {
1027 		return (failingPaths.size() == 0) ? null : failingPaths;
1028 	}
1029 
1030 	/**
1031 	 * Returns whether this merge failed (i.e. not stopped because of a
1032 	 * conflict)
1033 	 *
1034 	 * @return <code>true</code> if a failure occurred, <code>false</code>
1035 	 *         otherwise
1036 	 */
1037 	public boolean failed() {
1038 		return failingPaths.size() > 0;
1039 	}
1040 
1041 	/**
1042 	 * Sets the DirCache which shall be used by this merger. If the DirCache is
1043 	 * not set explicitly and if this merger doesn't work in-core, this merger
1044 	 * will implicitly get and lock a default DirCache. If the DirCache is
1045 	 * explicitly set the caller is responsible to lock it in advance. Finally
1046 	 * the merger will call {@link DirCache#commit()} which requires that the
1047 	 * DirCache is locked. If the {@link #mergeImpl()} returns without throwing
1048 	 * an exception the lock will be released. In case of exceptions the caller
1049 	 * is responsible to release the lock.
1050 	 *
1051 	 * @param dc
1052 	 *            the DirCache to set
1053 	 */
1054 	public void setDirCache(DirCache dc) {
1055 		this.dircache = dc;
1056 		implicitDirCache = false;
1057 	}
1058 
1059 	/**
1060 	 * Sets the WorkingTreeIterator to be used by this merger. If no
1061 	 * WorkingTreeIterator is set this merger will ignore the working tree and
1062 	 * fail if a content merge is necessary.
1063 	 * <p>
1064 	 * TODO: enhance WorkingTreeIterator to support write operations. Then this
1065 	 * merger will be able to merge with a different working tree abstraction.
1066 	 *
1067 	 * @param workingTreeIterator
1068 	 *            the workingTreeIt to set
1069 	 */
1070 	public void setWorkingTreeIterator(WorkingTreeIterator workingTreeIterator) {
1071 		this.workingTreeIterator = workingTreeIterator;
1072 	}
1073 
1074 
1075 	/**
1076 	 * The resolve conflict way of three way merging
1077 	 *
1078 	 * @param baseTree
1079 	 * @param headTree
1080 	 * @param mergeTree
1081 	 * @param ignoreConflicts
1082 	 *            Controls what to do in case a content-merge is done and a
1083 	 *            conflict is detected. The default setting for this should be
1084 	 *            <code>false</code>. In this case the working tree file is
1085 	 *            filled with new content (containing conflict markers) and the
1086 	 *            index is filled with multiple stages containing BASE, OURS and
1087 	 *            THEIRS content. Having such non-0 stages is the sign to git
1088 	 *            tools that there are still conflicts for that path.
1089 	 *            <p>
1090 	 *            If <code>true</code> is specified the behavior is different.
1091 	 *            In case a conflict is detected the working tree file is again
1092 	 *            filled with new content (containing conflict markers). But
1093 	 *            also stage 0 of the index is filled with that content. No
1094 	 *            other stages are filled. Means: there is no conflict on that
1095 	 *            path but the new content (including conflict markers) is
1096 	 *            stored as successful merge result. This is needed in the
1097 	 *            context of {@link RecursiveMerger} where when determining
1098 	 *            merge bases we don't want to deal with content-merge
1099 	 *            conflicts.
1100 	 * @return whether the trees merged cleanly
1101 	 * @throws IOException
1102 	 * @since 3.5
1103 	 */
1104 	protected boolean mergeTrees(AbstractTreeIterator baseTree,
1105 			RevTree headTree, RevTree mergeTree, boolean ignoreConflicts)
1106 			throws IOException {
1107 
1108 		builder = dircache.builder();
1109 		DirCacheBuildIterator buildIt = new DirCacheBuildIterator(builder);
1110 
1111 		tw = new NameConflictTreeWalk(db, reader);
1112 		tw.addTree(baseTree);
1113 		tw.addTree(headTree);
1114 		tw.addTree(mergeTree);
1115 		int dciPos = tw.addTree(buildIt);
1116 		if (workingTreeIterator != null) {
1117 			tw.addTree(workingTreeIterator);
1118 			workingTreeIterator.setDirCacheIterator(tw, dciPos);
1119 		} else {
1120 			tw.setFilter(TreeFilter.ANY_DIFF);
1121 		}
1122 
1123 		if (!mergeTreeWalk(tw, ignoreConflicts)) {
1124 			return false;
1125 		}
1126 
1127 		if (!inCore) {
1128 			// No problem found. The only thing left to be done is to
1129 			// checkout all files from "theirs" which have been selected to
1130 			// go into the new index.
1131 			checkout();
1132 
1133 			// All content-merges are successfully done. If we can now write the
1134 			// new index we are on quite safe ground. Even if the checkout of
1135 			// files coming from "theirs" fails the user can work around such
1136 			// failures by checking out the index again.
1137 			if (!builder.commit()) {
1138 				cleanUp();
1139 				throw new IndexWriteException();
1140 			}
1141 			builder = null;
1142 
1143 		} else {
1144 			builder.finish();
1145 			builder = null;
1146 		}
1147 
1148 		if (getUnmergedPaths().isEmpty() && !failed()) {
1149 			resultTree = dircache.writeTree(getObjectInserter());
1150 			return true;
1151 		} else {
1152 			resultTree = null;
1153 			return false;
1154 		}
1155 	}
1156 
1157 	/**
1158 	 * Process the given TreeWalk's entries.
1159 	 *
1160 	 * @param treeWalk
1161 	 *            The walk to iterate over.
1162 	 * @param ignoreConflicts
1163 	 *            see
1164 	 *            {@link ResolveMerger#mergeTrees(AbstractTreeIterator, RevTree, RevTree, boolean)}
1165 	 * @return Whether the trees merged cleanly.
1166 	 * @throws IOException
1167 	 * @since 3.5
1168 	 */
1169 	protected boolean mergeTreeWalk(TreeWalk treeWalk, boolean ignoreConflicts)
1170 			throws IOException {
1171 		boolean hasWorkingTreeIterator = tw.getTreeCount() > T_FILE;
1172 		boolean hasAttributeNodeProvider = treeWalk
1173 				.getAttributesNodeProvider() != null;
1174 		while (treeWalk.next()) {
1175 			if (!processEntry(
1176 					treeWalk.getTree(T_BASE, CanonicalTreeParser.class),
1177 					treeWalk.getTree(T_OURS, CanonicalTreeParser.class),
1178 					treeWalk.getTree(T_THEIRS, CanonicalTreeParser.class),
1179 					treeWalk.getTree(T_INDEX, DirCacheBuildIterator.class),
1180 					hasWorkingTreeIterator ? treeWalk.getTree(T_FILE,
1181 							WorkingTreeIterator.class) : null,
1182 					ignoreConflicts, hasAttributeNodeProvider
1183 							? treeWalk.getAttributes() : new Attributes())) {
1184 				cleanUp();
1185 				return false;
1186 			}
1187 			if (treeWalk.isSubtree() && enterSubtree)
1188 				treeWalk.enterSubtree();
1189 		}
1190 		return true;
1191 	}
1192 }