View Javadoc
1   /*
2    * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2008, Roger C. Soares <rogersoares@intelinet.com.br>
5    * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
6    * Copyright (C) 2010, Chrisian Halstrick <christian.halstrick@sap.com> and
7    * other copyright owners as documented in the project's IP log.
8    *
9    * This program and the accompanying materials are made available under the
10   * terms of the Eclipse Distribution License v1.0 which accompanies this
11   * distribution, is reproduced below, and is available at
12   * http://www.eclipse.org/org/documents/edl-v10.php
13   *
14   * All rights reserved.
15   *
16   * Redistribution and use in source and binary forms, with or without
17   * modification, are permitted provided that the following conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright notice, this
20   * list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above copyright notice,
23   * this list of conditions and the following disclaimer in the documentation
24   * and/or other materials provided with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
27   * contributors may be used to endorse or promote products derived from this
28   * software without specific prior written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40   * POSSIBILITY OF SUCH DAMAGE.
41   */
42  
43  package org.eclipse.jgit.dircache;
44  
45  import java.io.File;
46  import java.io.FileOutputStream;
47  import java.io.IOException;
48  import java.io.OutputStream;
49  import java.nio.file.StandardCopyOption;
50  import java.text.MessageFormat;
51  import java.util.ArrayList;
52  import java.util.HashMap;
53  import java.util.Iterator;
54  import java.util.List;
55  import java.util.Map;
56  
57  import org.eclipse.jgit.api.errors.FilterFailedException;
58  import org.eclipse.jgit.attributes.FilterCommand;
59  import org.eclipse.jgit.attributes.FilterCommandRegistry;
60  import org.eclipse.jgit.errors.CheckoutConflictException;
61  import org.eclipse.jgit.errors.CorruptObjectException;
62  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
63  import org.eclipse.jgit.errors.IndexWriteException;
64  import org.eclipse.jgit.errors.MissingObjectException;
65  import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
66  import org.eclipse.jgit.internal.JGitText;
67  import org.eclipse.jgit.lib.Constants;
68  import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
69  import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
70  import org.eclipse.jgit.lib.CoreConfig.SymLinks;
71  import org.eclipse.jgit.lib.FileMode;
72  import org.eclipse.jgit.lib.NullProgressMonitor;
73  import org.eclipse.jgit.lib.ObjectChecker;
74  import org.eclipse.jgit.lib.ObjectId;
75  import org.eclipse.jgit.lib.ObjectLoader;
76  import org.eclipse.jgit.lib.ObjectReader;
77  import org.eclipse.jgit.lib.Repository;
78  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
79  import org.eclipse.jgit.treewalk.CanonicalTreeParser;
80  import org.eclipse.jgit.treewalk.EmptyTreeIterator;
81  import org.eclipse.jgit.treewalk.FileTreeIterator;
82  import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
83  import org.eclipse.jgit.treewalk.TreeWalk;
84  import org.eclipse.jgit.treewalk.WorkingTreeIterator;
85  import org.eclipse.jgit.treewalk.WorkingTreeOptions;
86  import org.eclipse.jgit.treewalk.filter.PathFilter;
87  import org.eclipse.jgit.util.FS;
88  import org.eclipse.jgit.util.FS.ExecutionResult;
89  import org.eclipse.jgit.util.FileUtils;
90  import org.eclipse.jgit.util.IntList;
91  import org.eclipse.jgit.util.RawParseUtils;
92  import org.eclipse.jgit.util.SystemReader;
93  import org.eclipse.jgit.util.io.EolStreamTypeUtil;
94  import org.slf4j.Logger;
95  import org.slf4j.LoggerFactory;
96  
97  /**
98   * This class handles checking out one or two trees merging with the index.
99   */
100 public class DirCacheCheckout {
101 	private static Logger LOG = LoggerFactory.getLogger(DirCacheCheckout.class);
102 
103 	private static final int MAX_EXCEPTION_TEXT_SIZE = 10 * 1024;
104 
105 	/**
106 	 * Metadata used in checkout process
107 	 *
108 	 * @since 4.3
109 	 */
110 	public static class CheckoutMetadata {
111 		/** git attributes */
112 		public final EolStreamType eolStreamType;
113 
114 		/** filter command to apply */
115 		public final String smudgeFilterCommand;
116 
117 		/**
118 		 * @param eolStreamType
119 		 * @param smudgeFilterCommand
120 		 */
121 		public CheckoutMetadata(EolStreamType eolStreamType,
122 				String smudgeFilterCommand) {
123 			this.eolStreamType = eolStreamType;
124 			this.smudgeFilterCommand = smudgeFilterCommand;
125 		}
126 
127 		static CheckoutMetadata EMPTY = new CheckoutMetadata(
128 				EolStreamType.DIRECT, null);
129 	}
130 
131 	private Repository repo;
132 
133 	private HashMap<String, CheckoutMetadata> updated = new HashMap<>();
134 
135 	private ArrayList<String> conflicts = new ArrayList<>();
136 
137 	private ArrayList<String> removed = new ArrayList<>();
138 
139 	private ObjectId mergeCommitTree;
140 
141 	private DirCache dc;
142 
143 	private DirCacheBuilder builder;
144 
145 	private NameConflictTreeWalk walk;
146 
147 	private ObjectId headCommitTree;
148 
149 	private WorkingTreeIterator workingTree;
150 
151 	private boolean failOnConflict = true;
152 
153 	private ArrayList<String> toBeDeleted = new ArrayList<>();
154 
155 	private boolean emptyDirCache;
156 
157 	private boolean performingCheckout;
158 
159 	/**
160 	 * @return a list of updated paths and smudgeFilterCommands
161 	 */
162 	public Map<String, CheckoutMetadata> getUpdated() {
163 		return updated;
164 	}
165 
166 	/**
167 	 * @return a list of conflicts created by this checkout
168 	 */
169 	public List<String> getConflicts() {
170 		return conflicts;
171 	}
172 
173 	/**
174 	 * @return a list of paths (relative to the start of the working tree) of
175 	 *         files which couldn't be deleted during last call to
176 	 *         {@link #checkout()} . {@link #checkout()} detected that these
177 	 *         files should be deleted but the deletion in the filesystem failed
178 	 *         (e.g. because a file was locked). To have a consistent state of
179 	 *         the working tree these files have to be deleted by the callers of
180 	 *         {@link DirCacheCheckout}.
181 	 */
182 	public List<String> getToBeDeleted() {
183 		return toBeDeleted;
184 	}
185 
186 	/**
187 	 * @return a list of all files removed by this checkout
188 	 */
189 	public List<String> getRemoved() {
190 		return removed;
191 	}
192 
193 	/**
194 	 * Constructs a DirCacheCeckout for merging and checking out two trees (HEAD
195 	 * and mergeCommitTree) and the index.
196 	 *
197 	 * @param repo
198 	 *            the repository in which we do the checkout
199 	 * @param headCommitTree
200 	 *            the id of the tree of the head commit
201 	 * @param dc
202 	 *            the (already locked) Dircache for this repo
203 	 * @param mergeCommitTree
204 	 *            the id of the tree we want to fast-forward to
205 	 * @param workingTree
206 	 *            an iterator over the repositories Working Tree
207 	 * @throws IOException
208 	 */
209 	public DirCacheCheckout(Repository repo, ObjectId headCommitTree, DirCache dc,
210 			ObjectId mergeCommitTree, WorkingTreeIterator workingTree)
211 			throws IOException {
212 		this.repo = repo;
213 		this.dc = dc;
214 		this.headCommitTree = headCommitTree;
215 		this.mergeCommitTree = mergeCommitTree;
216 		this.workingTree = workingTree;
217 		this.emptyDirCache = (dc == null) || (dc.getEntryCount() == 0);
218 	}
219 
220 	/**
221 	 * Constructs a DirCacheCeckout for merging and checking out two trees (HEAD
222 	 * and mergeCommitTree) and the index. As iterator over the working tree
223 	 * this constructor creates a standard {@link FileTreeIterator}
224 	 *
225 	 * @param repo
226 	 *            the repository in which we do the checkout
227 	 * @param headCommitTree
228 	 *            the id of the tree of the head commit
229 	 * @param dc
230 	 *            the (already locked) Dircache for this repo
231 	 * @param mergeCommitTree
232 	 *            the id of the tree we want to fast-forward to
233 	 * @throws IOException
234 	 */
235 	public DirCacheCheckout(Repository repo, ObjectId headCommitTree,
236 			DirCache dc, ObjectId mergeCommitTree) throws IOException {
237 		this(repo, headCommitTree, dc, mergeCommitTree, new FileTreeIterator(repo));
238 	}
239 
240 	/**
241 	 * Constructs a DirCacheCeckout for checking out one tree, merging with the
242 	 * index.
243 	 *
244 	 * @param repo
245 	 *            the repository in which we do the checkout
246 	 * @param dc
247 	 *            the (already locked) Dircache for this repo
248 	 * @param mergeCommitTree
249 	 *            the id of the tree we want to fast-forward to
250 	 * @param workingTree
251 	 *            an iterator over the repositories Working Tree
252 	 * @throws IOException
253 	 */
254 	public DirCacheCheckout(Repository repo, DirCache dc,
255 			ObjectId mergeCommitTree, WorkingTreeIterator workingTree)
256 			throws IOException {
257 		this(repo, null, dc, mergeCommitTree, workingTree);
258 	}
259 
260 	/**
261 	 * Constructs a DirCacheCeckout for checking out one tree, merging with the
262 	 * index. As iterator over the working tree this constructor creates a
263 	 * standard {@link FileTreeIterator}
264 	 *
265 	 * @param repo
266 	 *            the repository in which we do the checkout
267 	 * @param dc
268 	 *            the (already locked) Dircache for this repo
269 	 * @param mergeCommitTree
270 	 *            the id of the tree of the
271 	 * @throws IOException
272 	 */
273 	public DirCacheCheckout(Repository repo, DirCache dc,
274 			ObjectId mergeCommitTree) throws IOException {
275 		this(repo, null, dc, mergeCommitTree, new FileTreeIterator(repo));
276 	}
277 
278 	/**
279 	 * Scan head, index and merge tree. Used during normal checkout or merge
280 	 * operations.
281 	 *
282 	 * @throws CorruptObjectException
283 	 * @throws IOException
284 	 */
285 	public void preScanTwoTrees() throws CorruptObjectException, IOException {
286 		removed.clear();
287 		updated.clear();
288 		conflicts.clear();
289 		walk = new NameConflictTreeWalk(repo);
290 		builder = dc.builder();
291 
292 		addTree(walk, headCommitTree);
293 		addTree(walk, mergeCommitTree);
294 		int dciPos = walk.addTree(new DirCacheBuildIterator(builder));
295 		walk.addTree(workingTree);
296 		workingTree.setDirCacheIterator(walk, dciPos);
297 
298 		while (walk.next()) {
299 			processEntry(walk.getTree(0, CanonicalTreeParser.class),
300 					walk.getTree(1, CanonicalTreeParser.class),
301 					walk.getTree(2, DirCacheBuildIterator.class),
302 					walk.getTree(3, WorkingTreeIterator.class));
303 			if (walk.isSubtree())
304 				walk.enterSubtree();
305 		}
306 	}
307 
308 	private void addTree(TreeWalk tw, ObjectId id) throws MissingObjectException, IncorrectObjectTypeException, IOException {
309 		if (id == null)
310 			tw.addTree(new EmptyTreeIterator());
311 		else
312 			tw.addTree(id);
313 	}
314 
315 	/**
316 	 * Scan index and merge tree (no HEAD). Used e.g. for initial checkout when
317 	 * there is no head yet.
318 	 *
319 	 * @throws MissingObjectException
320 	 * @throws IncorrectObjectTypeException
321 	 * @throws CorruptObjectException
322 	 * @throws IOException
323 	 */
324 	public void prescanOneTree()
325 			throws MissingObjectException, IncorrectObjectTypeException,
326 			CorruptObjectException, IOException {
327 		removed.clear();
328 		updated.clear();
329 		conflicts.clear();
330 
331 		builder = dc.builder();
332 
333 		walk = new NameConflictTreeWalk(repo);
334 		addTree(walk, mergeCommitTree);
335 		int dciPos = walk.addTree(new DirCacheBuildIterator(builder));
336 		walk.addTree(workingTree);
337 		workingTree.setDirCacheIterator(walk, dciPos);
338 
339 		while (walk.next()) {
340 			processEntry(walk.getTree(0, CanonicalTreeParser.class),
341 					walk.getTree(1, DirCacheBuildIterator.class),
342 					walk.getTree(2, WorkingTreeIterator.class));
343 			if (walk.isSubtree())
344 				walk.enterSubtree();
345 		}
346 		conflicts.removeAll(removed);
347 	}
348 
349 	/**
350 	 * Processing an entry in the context of {@link #prescanOneTree()} when only
351 	 * one tree is given
352 	 *
353 	 * @param m the tree to merge
354 	 * @param i the index
355 	 * @param f the working tree
356 	 * @throws IOException
357 	 */
358 	void processEntry(CanonicalTreeParser m, DirCacheBuildIterator i,
359 			WorkingTreeIterator f) throws IOException {
360 		if (m != null) {
361 			checkValidPath(m);
362 			// There is an entry in the merge commit. Means: we want to update
363 			// what's currently in the index and working-tree to that one
364 			if (i == null) {
365 				// The index entry is missing
366 				if (f != null && !FileMode.TREE.equals(f.getEntryFileMode())
367 						&& !f.isEntryIgnored()) {
368 					if (failOnConflict) {
369 						// don't overwrite an untracked and not ignored file
370 						conflicts.add(walk.getPathString());
371 					} else {
372 						// failOnConflict is false. Putting something to conflicts
373 						// would mean we delete it. Instead we want the mergeCommit
374 						// content to be checked out.
375 						update(m.getEntryPathString(), m.getEntryObjectId(),
376 								m.getEntryFileMode());
377 					}
378 				} else
379 					update(m.getEntryPathString(), m.getEntryObjectId(),
380 						m.getEntryFileMode());
381 			} else if (f == null || !m.idEqual(i)) {
382 				// The working tree file is missing or the merge content differs
383 				// from index content
384 				update(m.getEntryPathString(), m.getEntryObjectId(),
385 						m.getEntryFileMode());
386 			} else if (i.getDirCacheEntry() != null) {
387 				// The index contains a file (and not a folder)
388 				if (f.isModified(i.getDirCacheEntry(), true,
389 						this.walk.getObjectReader())
390 						|| i.getDirCacheEntry().getStage() != 0)
391 					// The working tree file is dirty or the index contains a
392 					// conflict
393 					update(m.getEntryPathString(), m.getEntryObjectId(),
394 							m.getEntryFileMode());
395 				else {
396 					// update the timestamp of the index with the one from the
397 					// file if not set, as we are sure to be in sync here.
398 					DirCacheEntry entry = i.getDirCacheEntry();
399 					if (entry.getLastModified() == 0)
400 						entry.setLastModified(f.getEntryLastModified());
401 					keep(entry);
402 				}
403 			} else
404 				// The index contains a folder
405 				keep(i.getDirCacheEntry());
406 		} else {
407 			// There is no entry in the merge commit. Means: we want to delete
408 			// what's currently in the index and working tree
409 			if (f != null) {
410 				// There is a file/folder for that path in the working tree
411 				if (walk.isDirectoryFileConflict()) {
412 					// We put it in conflicts. Even if failOnConflict is false
413 					// this would cause the path to be deleted. Thats exactly what
414 					// we want in this situation
415 					conflicts.add(walk.getPathString());
416 				} else {
417 					// No file/folder conflict exists. All entries are files or
418 					// all entries are folders
419 					if (i != null) {
420 						// ... and the working tree contained a file or folder
421 						// -> add it to the removed set and remove it from
422 						// conflicts set
423 						remove(i.getEntryPathString());
424 						conflicts.remove(i.getEntryPathString());
425 					} else {
426 						// untracked file, neither contained in tree to merge
427 						// nor in index
428 					}
429 				}
430 			} else {
431 				// There is no file/folder for that path in the working tree,
432 				// nor in the merge head.
433 				// The only entry we have is the index entry. Like the case
434 				// where there is a file with the same name, remove it,
435 			}
436 		}
437 	}
438 
439 	/**
440 	 * Execute this checkout. A {@link WorkingTreeModifiedEvent} is fired if the
441 	 * working tree was modified; even if the checkout fails.
442 	 *
443 	 * @return <code>false</code> if this method could not delete all the files
444 	 *         which should be deleted (e.g. because one of the files was
445 	 *         locked). In this case {@link #getToBeDeleted()} lists the files
446 	 *         which should be tried to be deleted outside of this method.
447 	 *         Although <code>false</code> is returned the checkout was
448 	 *         successful and the working tree was updated for all other files.
449 	 *         <code>true</code> is returned when no such problem occurred
450 	 *
451 	 * @throws IOException
452 	 */
453 	public boolean checkout() throws IOException {
454 		try {
455 			return doCheckout();
456 		} finally {
457 			try {
458 				dc.unlock();
459 			} finally {
460 				if (performingCheckout) {
461 					WorkingTreeModifiedEvent event = new WorkingTreeModifiedEvent(
462 							getUpdated().keySet(), getRemoved());
463 					if (!event.isEmpty()) {
464 						repo.fireEvent(event);
465 					}
466 				}
467 			}
468 		}
469 	}
470 
471 	private boolean doCheckout() throws CorruptObjectException, IOException,
472 			MissingObjectException, IncorrectObjectTypeException,
473 			CheckoutConflictException, IndexWriteException {
474 		toBeDeleted.clear();
475 		try (ObjectReader objectReader = repo.getObjectDatabase().newReader()) {
476 			if (headCommitTree != null)
477 				preScanTwoTrees();
478 			else
479 				prescanOneTree();
480 
481 			if (!conflicts.isEmpty()) {
482 				if (failOnConflict)
483 					throw new CheckoutConflictException(conflicts.toArray(new String[conflicts.size()]));
484 				else
485 					cleanUpConflicts();
486 			}
487 
488 			// update our index
489 			builder.finish();
490 
491 			performingCheckout = true;
492 			File file = null;
493 			String last = null;
494 			// when deleting files process them in the opposite order as they have
495 			// been reported. This ensures the files are deleted before we delete
496 			// their parent folders
497 			IntList nonDeleted = new IntList();
498 			for (int i = removed.size() - 1; i >= 0; i--) {
499 				String r = removed.get(i);
500 				file = new File(repo.getWorkTree(), r);
501 				if (!file.delete() && repo.getFS().exists(file)) {
502 					// The list of stuff to delete comes from the index
503 					// which will only contain a directory if it is
504 					// a submodule, in which case we shall not attempt
505 					// to delete it. A submodule is not empty, so it
506 					// is safe to check this after a failed delete.
507 					if (!repo.getFS().isDirectory(file)) {
508 						nonDeleted.add(i);
509 						toBeDeleted.add(r);
510 					}
511 				} else {
512 					if (last != null && !isSamePrefix(r, last))
513 						removeEmptyParents(new File(repo.getWorkTree(), last));
514 					last = r;
515 				}
516 			}
517 			if (file != null) {
518 				removeEmptyParents(file);
519 			}
520 			removed = filterOut(removed, nonDeleted);
521 			nonDeleted = null;
522 			Iterator<Map.Entry<String, CheckoutMetadata>> toUpdate = updated
523 					.entrySet().iterator();
524 			Map.Entry<String, CheckoutMetadata> e = null;
525 			try {
526 				while (toUpdate.hasNext()) {
527 					e = toUpdate.next();
528 					String path = e.getKey();
529 					CheckoutMetadata meta = e.getValue();
530 					DirCacheEntry entry = dc.getEntry(path);
531 					if (!FileMode.GITLINK.equals(entry.getRawMode())) {
532 						checkoutEntry(repo, entry, objectReader, false, meta);
533 					}
534 					e = null;
535 				}
536 			} catch (Exception ex) {
537 				// We didn't actually modify the current entry nor any that
538 				// might follow.
539 				if (e != null) {
540 					toUpdate.remove();
541 				}
542 				while (toUpdate.hasNext()) {
543 					e = toUpdate.next();
544 					toUpdate.remove();
545 				}
546 				throw ex;
547 			}
548 			// commit the index builder - a new index is persisted
549 			if (!builder.commit())
550 				throw new IndexWriteException();
551 		}
552 		return toBeDeleted.size() == 0;
553 	}
554 
555 	private static ArrayList<String> filterOut(ArrayList<String> strings,
556 			IntList indicesToRemove) {
557 		int n = indicesToRemove.size();
558 		if (n == strings.size()) {
559 			return new ArrayList<>(0);
560 		}
561 		switch (n) {
562 		case 0:
563 			return strings;
564 		case 1:
565 			strings.remove(indicesToRemove.get(0));
566 			return strings;
567 		default:
568 			int length = strings.size();
569 			ArrayList<String> result = new ArrayList<>(length - n);
570 			// Process indicesToRemove from the back; we know that it
571 			// contains indices in descending order.
572 			int j = n - 1;
573 			int idx = indicesToRemove.get(j);
574 			for (int i = 0; i < length; i++) {
575 				if (i == idx) {
576 					idx = (--j >= 0) ? indicesToRemove.get(j) : -1;
577 				} else {
578 					result.add(strings.get(i));
579 				}
580 			}
581 			return result;
582 		}
583 	}
584 
585 	private static boolean isSamePrefix(String a, String b) {
586 		int as = a.lastIndexOf('/');
587 		int bs = b.lastIndexOf('/');
588 		return a.substring(0, as + 1).equals(b.substring(0, bs + 1));
589 	}
590 
591 	 private void removeEmptyParents(File f) {
592 		File parentFile = f.getParentFile();
593 
594 		while (parentFile != null && !parentFile.equals(repo.getWorkTree())) {
595 			if (!parentFile.delete())
596 				break;
597 			parentFile = parentFile.getParentFile();
598 		}
599 	}
600 
601 	/**
602 	 * Compares whether two pairs of ObjectId and FileMode are equal.
603 	 *
604 	 * @param id1
605 	 * @param mode1
606 	 * @param id2
607 	 * @param mode2
608 	 * @return <code>true</code> if FileModes and ObjectIds are equal.
609 	 *         <code>false</code> otherwise
610 	 */
611 	private boolean equalIdAndMode(ObjectId id1, FileMode mode1, ObjectId id2,
612 			FileMode mode2) {
613 		if (!mode1.equals(mode2))
614 			return false;
615 		return id1 != null ? id1.equals(id2) : id2 == null;
616 	}
617 
618 	/**
619 	 * Here the main work is done. This method is called for each existing path
620 	 * in head, index and merge. This method decides what to do with the
621 	 * corresponding index entry: keep it, update it, remove it or mark a
622 	 * conflict.
623 	 *
624 	 * @param h
625 	 *            the entry for the head
626 	 * @param m
627 	 *            the entry for the merge
628 	 * @param i
629 	 *            the entry for the index
630 	 * @param f
631 	 *            the file in the working tree
632 	 * @throws IOException
633 	 */
634 
635 	void processEntry(CanonicalTreeParser h, CanonicalTreeParser m,
636 			DirCacheBuildIterator i, WorkingTreeIterator f) throws IOException {
637 		DirCacheEntry dce = i != null ? i.getDirCacheEntry() : null;
638 
639 		String name = walk.getPathString();
640 
641 		if (m != null)
642 			checkValidPath(m);
643 
644 		if (i == null && m == null && h == null) {
645 			// File/Directory conflict case #20
646 			if (walk.isDirectoryFileConflict())
647 				// TODO: check whether it is always correct to report a conflict here
648 				conflict(name, null, null, null);
649 
650 			// file only exists in working tree -> ignore it
651 			return;
652 		}
653 
654 		ObjectId iId = (i == null ? null : i.getEntryObjectId());
655 		ObjectId mId = (m == null ? null : m.getEntryObjectId());
656 		ObjectId hId = (h == null ? null : h.getEntryObjectId());
657 		FileMode iMode = (i == null ? null : i.getEntryFileMode());
658 		FileMode mMode = (m == null ? null : m.getEntryFileMode());
659 		FileMode hMode = (h == null ? null : h.getEntryFileMode());
660 
661 		/**
662 		 * <pre>
663 		 *  File/Directory conflicts:
664 		 *  the following table from ReadTreeTest tells what to do in case of directory/file
665 		 *  conflicts. I give comments here
666 		 *
667 		 *      H        I       M     Clean     H==M     H==I    I==M         Result
668 		 *      ------------------------------------------------------------------
669 		 * 1    D        D       F       Y         N       Y       N           Update
670 		 * 2    D        D       F       N         N       Y       N           Conflict
671 		 * 3    D        F       D                 Y       N       N           Keep
672 		 * 4    D        F       D                 N       N       N           Conflict
673 		 * 5    D        F       F       Y         N       N       Y           Keep
674 		 * 5b   D        F       F       Y         N       N       N           Conflict
675 		 * 6    D        F       F       N         N       N       Y           Keep
676 		 * 6b   D        F       F       N         N       N       N           Conflict
677 		 * 7    F        D       F       Y         Y       N       N           Update
678 		 * 8    F        D       F       N         Y       N       N           Conflict
679 		 * 9    F        D       F                 N       N       N           Conflict
680 		 * 10   F        D       D                 N       N       Y           Keep
681 		 * 11   F        D       D                 N       N       N           Conflict
682 		 * 12   F        F       D       Y         N       Y       N           Update
683 		 * 13   F        F       D       N         N       Y       N           Conflict
684 		 * 14   F        F       D                 N       N       N           Conflict
685 		 * 15   0        F       D                 N       N       N           Conflict
686 		 * 16   0        D       F       Y         N       N       N           Update
687 		 * 17   0        D       F                 N       N       N           Conflict
688 		 * 18   F        0       D                                             Update
689 		 * 19   D        0       F                                             Update
690 		 * 20   0        0       F       N (worktree=dir)                      Conflict
691 		 * </pre>
692 		 */
693 
694 		// The information whether head,index,merge iterators are currently
695 		// pointing to file/folder/non-existing is encoded into this variable.
696 		//
697 		// To decode write down ffMask in hexadecimal form. The last digit
698 		// represents the state for the merge iterator, the second last the
699 		// state for the index iterator and the third last represents the state
700 		// for the head iterator. The hexadecimal constant "F" stands for
701 		// "file", a "D" stands for "directory" (tree), and a "0" stands for
702 		// non-existing. Symbolic links and git links are treated as File here.
703 		//
704 		// Examples:
705 		// ffMask == 0xFFD -> Head=File, Index=File, Merge=Tree
706 		// ffMask == 0xDD0 -> Head=Tree, Index=Tree, Merge=Non-Existing
707 
708 		int ffMask = 0;
709 		if (h != null)
710 			ffMask = FileMode.TREE.equals(hMode) ? 0xD00 : 0xF00;
711 		if (i != null)
712 			ffMask |= FileMode.TREE.equals(iMode) ? 0x0D0 : 0x0F0;
713 		if (m != null)
714 			ffMask |= FileMode.TREE.equals(mMode) ? 0x00D : 0x00F;
715 
716 		// Check whether we have a possible file/folder conflict. Therefore we
717 		// need a least one file and one folder.
718 		if (((ffMask & 0x222) != 0x000)
719 				&& (((ffMask & 0x00F) == 0x00D) || ((ffMask & 0x0F0) == 0x0D0) || ((ffMask & 0xF00) == 0xD00))) {
720 
721 			// There are 3*3*3=27 possible combinations of file/folder
722 			// conflicts. Some of them are not-relevant because
723 			// they represent no conflict, e.g. 0xFFF, 0xDDD, ... The following
724 			// switch processes all relevant cases.
725 			switch (ffMask) {
726 			case 0xDDF: // 1 2
727 				if (f != null && isModifiedSubtree_IndexWorkingtree(name)) {
728 					conflict(name, dce, h, m); // 1
729 				} else {
730 					update(name, mId, mMode); // 2
731 				}
732 
733 				break;
734 			case 0xDFD: // 3 4
735 				keep(dce);
736 				break;
737 			case 0xF0D: // 18
738 				remove(name);
739 				break;
740 			case 0xDFF: // 5 5b 6 6b
741 				if (equalIdAndMode(iId, iMode, mId, mMode))
742 					keep(dce); // 5 6
743 				else
744 					conflict(name, dce, h, m); // 5b 6b
745 				break;
746 			case 0xFDD: // 10 11
747 				// TODO: make use of tree extension as soon as available in jgit
748 				// we would like to do something like
749 				// if (!equalIdAndMode(iId, iMode, mId, mMode)
750 				//   conflict(name, i.getDirCacheEntry(), h, m);
751 				// But since we don't know the id of a tree in the index we do
752 				// nothing here and wait that conflicts between index and merge
753 				// are found later
754 				break;
755 			case 0xD0F: // 19
756 				update(name, mId, mMode);
757 				break;
758 			case 0xDF0: // conflict without a rule
759 			case 0x0FD: // 15
760 				conflict(name, dce, h, m);
761 				break;
762 			case 0xFDF: // 7 8 9
763 				if (equalIdAndMode(hId, hMode, mId, mMode)) {
764 					if (isModifiedSubtree_IndexWorkingtree(name))
765 						conflict(name, dce, h, m); // 8
766 					else
767 						update(name, mId, mMode); // 7
768 				} else
769 					conflict(name, dce, h, m); // 9
770 				break;
771 			case 0xFD0: // keep without a rule
772 				keep(dce);
773 				break;
774 			case 0xFFD: // 12 13 14
775 				if (equalIdAndMode(hId, hMode, iId, iMode))
776 					if (f != null
777 							&& f.isModified(dce, true,
778 									this.walk.getObjectReader()))
779 						conflict(name, dce, h, m); // 13
780 					else
781 						remove(name); // 12
782 				else
783 					conflict(name, dce, h, m); // 14
784 				break;
785 			case 0x0DF: // 16 17
786 				if (!isModifiedSubtree_IndexWorkingtree(name))
787 					update(name, mId, mMode);
788 				else
789 					conflict(name, dce, h, m);
790 				break;
791 			default:
792 				keep(dce);
793 			}
794 			return;
795 		}
796 
797 		if ((ffMask & 0x222) == 0) {
798 			// HEAD, MERGE and index don't contain a file (e.g. all contain a
799 			// folder)
800 			if (f == null || FileMode.TREE.equals(f.getEntryFileMode())) {
801 				// the workingtree entry doesn't exist or also contains a folder
802 				// -> no problem
803 				return;
804 			} else {
805 				// the workingtree entry exists and is not a folder
806 				if (!idEqual(h, m)) {
807 					// Because HEAD and MERGE differ we will try to update the
808 					// workingtree with a folder -> return a conflict
809 					conflict(name, null, null, null);
810 				}
811 				return;
812 			}
813 		}
814 
815 		if ((ffMask == 0x00F) && f != null && FileMode.TREE.equals(f.getEntryFileMode())) {
816 			// File/Directory conflict case #20
817 			conflict(name, null, h, m);
818 			return;
819 		}
820 
821 		if (i == null) {
822 			// Nothing in Index
823 			// At least one of Head, Index, Merge is not empty
824 			// make sure not to overwrite untracked files
825 			if (f != null && !f.isEntryIgnored()) {
826 				// A submodule is not a file. We should ignore it
827 				if (!FileMode.GITLINK.equals(mMode)) {
828 					// a dirty worktree: the index is empty but we have a
829 					// workingtree-file
830 					if (mId == null
831 							|| !equalIdAndMode(mId, mMode,
832 									f.getEntryObjectId(), f.getEntryFileMode())) {
833 						conflict(name, null, h, m);
834 						return;
835 					}
836 				}
837 			}
838 
839 			/**
840 			 * <pre>
841 			 * 	          I (index)     H        M     H==M  Result
842 			 * 	        -------------------------------------------
843 			 * 	        0 nothing    nothing  nothing        (does not happen)
844 			 * 	        1 nothing    nothing  exists         use M
845 			 * 	        2 nothing    exists   nothing        remove path from index
846 			 * 	        3 nothing    exists   exists   yes   keep index if not in initial checkout
847 			 *                                               , otherwise use M
848 			 * 	          nothing    exists   exists   no    fail
849 			 * </pre>
850 			 */
851 
852 			if (h == null)
853 				// Nothing in Head
854 				// Nothing in Index
855 				// At least one of Head, Index, Merge is not empty
856 				// -> only Merge contains something for this path. Use it!
857 				// Potentially update the file
858 				update(name, mId, mMode); // 1
859 			else if (m == null)
860 				// Nothing in Merge
861 				// Something in Head
862 				// Nothing in Index
863 				// -> only Head contains something for this path and it should
864 				// be deleted. Potentially removes the file!
865 				remove(name); // 2
866 			else { // 3
867 				// Something in Merge
868 				// Something in Head
869 				// Nothing in Index
870 				// -> Head and Merge contain something (maybe not the same) and
871 				// in the index there is nothing (e.g. 'git rm ...' was
872 				// called before). Ignore the cached deletion and use what we
873 				// find in Merge. Potentially updates the file.
874 				if (equalIdAndMode(hId, hMode, mId, mMode)) {
875 					if (emptyDirCache)
876 						update(name, mId, mMode);
877 					else
878 						keep(dce);
879 				} else
880 					conflict(name, dce, h, m);
881 			}
882 		} else {
883 			// Something in Index
884 			if (h == null) {
885 				// Nothing in Head
886 				// Something in Index
887 				/**
888 				 * <pre>
889 				 * 	          clean I==H  I==M       H        M        Result
890 				 * 	         -----------------------------------------------------
891 				 * 	        4 yes   N/A   N/A     nothing  nothing  keep index
892 				 * 	        5 no    N/A   N/A     nothing  nothing  keep index
893 				 *
894 				 * 	        6 yes   N/A   yes     nothing  exists   keep index
895 				 * 	        7 no    N/A   yes     nothing  exists   keep index
896 				 * 	        8 yes   N/A   no      nothing  exists   fail
897 				 * 	        9 no    N/A   no      nothing  exists   fail
898 				 * </pre>
899 				 */
900 
901 				if (m == null
902 						|| !isModified_IndexTree(name, iId, iMode, mId, mMode,
903 								mergeCommitTree)) {
904 					// Merge contains nothing or the same as Index
905 					// Nothing in Head
906 					// Something in Index
907 					if (m==null && walk.isDirectoryFileConflict()) {
908 						// Nothing in Merge and current path is part of
909 						// File/Folder conflict
910 						// Nothing in Head
911 						// Something in Index
912 						if (dce != null
913 								&& (f == null || f.isModified(dce, true,
914 										this.walk.getObjectReader())))
915 							// No file or file is dirty
916 							// Nothing in Merge and current path is part of
917 							// File/Folder conflict
918 							// Nothing in Head
919 							// Something in Index
920 							// -> File folder conflict and Merge wants this
921 							// path to be removed. Since the file is dirty
922 							// report a conflict
923 							conflict(name, dce, h, m);
924 						else
925 							// A file is present and file is not dirty
926 							// Nothing in Merge and current path is part of
927 							// File/Folder conflict
928 							// Nothing in Head
929 							// Something in Index
930 							// -> File folder conflict and Merge wants this path
931 							// to be removed. Since the file is not dirty remove
932 							// file and index entry
933 							remove(name);
934 					} else
935 						// Something in Merge or current path is not part of
936 						// File/Folder conflict
937 						// Merge contains nothing or the same as Index
938 						// Nothing in Head
939 						// Something in Index
940 						// -> Merge contains nothing new. Keep the index.
941 						keep(dce);
942 				} else
943 					// Merge contains something and it is not the same as Index
944 					// Nothing in Head
945 					// Something in Index
946 					// -> Index contains something new (different from Head)
947 					// and Merge is different from Index. Report a conflict
948 					conflict(name, dce, h, m);
949 			} else if (m == null) {
950 				// Nothing in Merge
951 				// Something in Head
952 				// Something in Index
953 
954 				/**
955 				 * <pre>
956 				 * 	           clean I==H  I==M       H        M        Result
957 				 * 	         -----------------------------------------------------
958 				 * 	        10 yes   yes   N/A     exists   nothing  remove path from index
959 				 * 	        11 no    yes   N/A     exists   nothing  keep file
960 				 * 	        12 yes   no    N/A     exists   nothing  fail
961 				 * 	        13 no    no    N/A     exists   nothing  fail
962 				 * </pre>
963 				 */
964 
965 				if (iMode == FileMode.GITLINK) {
966 					// A submodule in Index
967 					// Nothing in Merge
968 					// Something in Head
969 					// Submodules that disappear from the checkout must
970 					// be removed from the index, but not deleted from disk.
971 					remove(name);
972 				} else {
973 					// Something different from a submodule in Index
974 					// Nothing in Merge
975 					// Something in Head
976 					if (!isModified_IndexTree(name, iId, iMode, hId, hMode,
977 							headCommitTree)) {
978 						// Index contains the same as Head
979 						// Something different from a submodule in Index
980 						// Nothing in Merge
981 						// Something in Head
982 						if (f != null
983 								&& f.isModified(dce, true,
984 										this.walk.getObjectReader())) {
985 							// file is dirty
986 							// Index contains the same as Head
987 							// Something different from a submodule in Index
988 							// Nothing in Merge
989 							// Something in Head
990 
991 							if (!FileMode.TREE.equals(f.getEntryFileMode())
992 									&& FileMode.TREE.equals(iMode))
993 								// The workingtree contains a file and the index semantically contains a folder.
994 								// Git considers the workingtree file as untracked. Just keep the untracked file.
995 								return;
996 							else
997 								// -> file is dirty and tracked but is should be
998 								// removed. That's a conflict
999 								conflict(name, dce, h, m);
1000 						} else
1001 							// file doesn't exist or is clean
1002 							// Index contains the same as Head
1003 							// Something different from a submodule in Index
1004 							// Nothing in Merge
1005 							// Something in Head
1006 							// -> Remove from index and delete the file
1007 							remove(name);
1008 					} else
1009 						// Index contains something different from Head
1010 						// Something different from a submodule in Index
1011 						// Nothing in Merge
1012 						// Something in Head
1013 						// -> Something new is in index (and maybe even on the
1014 						// filesystem). But Merge wants the path to be removed.
1015 						// Report a conflict
1016 						conflict(name, dce, h, m);
1017 				}
1018 			} else {
1019 				// Something in Merge
1020 				// Something in Head
1021 				// Something in Index
1022 				if (!equalIdAndMode(hId, hMode, mId, mMode)
1023 						&& isModified_IndexTree(name, iId, iMode, hId, hMode,
1024 								headCommitTree)
1025 						&& isModified_IndexTree(name, iId, iMode, mId, mMode,
1026 								mergeCommitTree))
1027 					// All three contents in Head, Merge, Index differ from each
1028 					// other
1029 					// -> All contents differ. Report a conflict.
1030 					conflict(name, dce, h, m);
1031 				else
1032 					// At least two of the contents of Head, Index, Merge
1033 					// are the same
1034 					// Something in Merge
1035 					// Something in Head
1036 					// Something in Index
1037 
1038 				if (!isModified_IndexTree(name, iId, iMode, hId, hMode,
1039 						headCommitTree)
1040 						&& isModified_IndexTree(name, iId, iMode, mId, mMode,
1041 								mergeCommitTree)) {
1042 						// Head contains the same as Index. Merge differs
1043 						// Something in Merge
1044 
1045 					// For submodules just update the index with the new SHA-1
1046 					if (dce != null
1047 							&& FileMode.GITLINK.equals(dce.getFileMode())) {
1048 						// Index and Head contain the same submodule. Merge
1049 						// differs
1050 						// Something in Merge
1051 						// -> Nothing new in index. Move to merge.
1052 						// Potentially updates the file
1053 
1054 						// TODO check that we don't overwrite some unsaved
1055 						// file content
1056 						update(name, mId, mMode);
1057 					} else if (dce != null
1058 							&& (f != null && f.isModified(dce, true,
1059 									this.walk.getObjectReader()))) {
1060 						// File exists and is dirty
1061 						// Head and Index don't contain a submodule
1062 						// Head contains the same as Index. Merge differs
1063 						// Something in Merge
1064 						// -> Merge wants the index and file to be updated
1065 						// but the file is dirty. Report a conflict
1066 						conflict(name, dce, h, m);
1067 					} else {
1068 						// File doesn't exist or is clean
1069 						// Head and Index don't contain a submodule
1070 						// Head contains the same as Index. Merge differs
1071 						// Something in Merge
1072 						// -> Standard case when switching between branches:
1073 						// Nothing new in index but something different in
1074 						// Merge. Update index and file
1075 						update(name, mId, mMode);
1076 					}
1077 				} else {
1078 					// Head differs from index or merge is same as index
1079 					// At least two of the contents of Head, Index, Merge
1080 					// are the same
1081 					// Something in Merge
1082 					// Something in Head
1083 					// Something in Index
1084 
1085 					// Can be formulated as: Either all three states are
1086 					// equal or Merge is equal to Head or Index and differs
1087 					// to the other one.
1088 					// -> In all three cases we don't touch index and file.
1089 
1090 					keep(dce);
1091 				}
1092 			}
1093 		}
1094 	}
1095 
1096 	private static boolean idEqual(AbstractTreeIterator a,
1097 			AbstractTreeIterator b) {
1098 		if (a == b) {
1099 			return true;
1100 		}
1101 		if (a == null || b == null) {
1102 			return false;
1103 		}
1104 		return a.getEntryObjectId().equals(b.getEntryObjectId());
1105 	}
1106 
1107 	/**
1108 	 * A conflict is detected - add the three different stages to the index
1109 	 * @param path the path of the conflicting entry
1110 	 * @param e the previous index entry
1111 	 * @param h the first tree you want to merge (the HEAD)
1112 	 * @param m the second tree you want to merge
1113 	 */
1114 	private void conflict(String path, DirCacheEntry e, AbstractTreeIterator h, AbstractTreeIterator m) {
1115 		conflicts.add(path);
1116 
1117 		DirCacheEntry entry;
1118 		if (e != null) {
1119 			entry = new DirCacheEntry(e.getPathString(), DirCacheEntry.STAGE_1);
1120 			entry.copyMetaData(e, true);
1121 			builder.add(entry);
1122 		}
1123 
1124 		if (h != null && !FileMode.TREE.equals(h.getEntryFileMode())) {
1125 			entry = new DirCacheEntry(h.getEntryPathString(), DirCacheEntry.STAGE_2);
1126 			entry.setFileMode(h.getEntryFileMode());
1127 			entry.setObjectId(h.getEntryObjectId());
1128 			builder.add(entry);
1129 		}
1130 
1131 		if (m != null && !FileMode.TREE.equals(m.getEntryFileMode())) {
1132 			entry = new DirCacheEntry(m.getEntryPathString(), DirCacheEntry.STAGE_3);
1133 			entry.setFileMode(m.getEntryFileMode());
1134 			entry.setObjectId(m.getEntryObjectId());
1135 			builder.add(entry);
1136 		}
1137 	}
1138 
1139 	private void keep(DirCacheEntry e) {
1140 		if (e != null && !FileMode.TREE.equals(e.getFileMode()))
1141 			builder.add(e);
1142 	}
1143 
1144 	private void remove(String path) {
1145 		removed.add(path);
1146 	}
1147 
1148 	private void update(String path, ObjectId mId, FileMode mode)
1149 			throws IOException {
1150 		if (!FileMode.TREE.equals(mode)) {
1151 			updated.put(path, new CheckoutMetadata(walk.getEolStreamType(),
1152 					walk.getFilterCommand(Constants.ATTR_FILTER_TYPE_SMUDGE)));
1153 
1154 			DirCacheEntry entry = new DirCacheEntry(path, DirCacheEntry.STAGE_0);
1155 			entry.setObjectId(mId);
1156 			entry.setFileMode(mode);
1157 			builder.add(entry);
1158 		}
1159 	}
1160 
1161 	/**
1162 	 * If <code>true</code>, will scan first to see if it's possible to check
1163 	 * out, otherwise throw {@link CheckoutConflictException}. If
1164 	 * <code>false</code>, it will silently deal with the problem.
1165 	 *
1166 	 * @param failOnConflict
1167 	 */
1168 	public void setFailOnConflict(boolean failOnConflict) {
1169 		this.failOnConflict = failOnConflict;
1170 	}
1171 
1172 	/**
1173 	 * This method implements how to handle conflicts when
1174 	 * {@link #failOnConflict} is false
1175 	 *
1176 	 * @throws CheckoutConflictException
1177 	 */
1178 	private void cleanUpConflicts() throws CheckoutConflictException {
1179 		// TODO: couldn't we delete unsaved worktree content here?
1180 		for (String c : conflicts) {
1181 			File conflict = new File(repo.getWorkTree(), c);
1182 			if (!conflict.delete())
1183 				throw new CheckoutConflictException(MessageFormat.format(
1184 						JGitText.get().cannotDeleteFile, c));
1185 			removeEmptyParents(conflict);
1186 		}
1187 		for (String r : removed) {
1188 			File file = new File(repo.getWorkTree(), r);
1189 			if (!file.delete())
1190 				throw new CheckoutConflictException(
1191 						MessageFormat.format(JGitText.get().cannotDeleteFile,
1192 								file.getAbsolutePath()));
1193 			removeEmptyParents(file);
1194 		}
1195 	}
1196 
1197 	/**
1198 	 * Checks whether the subtree starting at a given path differs between Index and
1199 	 * workingtree.
1200 	 *
1201 	 * @param path
1202 	 * @return true if the subtrees differ
1203 	 * @throws CorruptObjectException
1204 	 * @throws IOException
1205 	 */
1206 	private boolean isModifiedSubtree_IndexWorkingtree(String path)
1207 			throws CorruptObjectException, IOException {
1208 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
1209 			int dciPos = tw.addTree(new DirCacheIterator(dc));
1210 			FileTreeIterator fti = new FileTreeIterator(repo);
1211 			tw.addTree(fti);
1212 			fti.setDirCacheIterator(tw, dciPos);
1213 			tw.setRecursive(true);
1214 			tw.setFilter(PathFilter.create(path));
1215 			DirCacheIterator dcIt;
1216 			WorkingTreeIterator wtIt;
1217 			while (tw.next()) {
1218 				dcIt = tw.getTree(0, DirCacheIterator.class);
1219 				wtIt = tw.getTree(1, WorkingTreeIterator.class);
1220 				if (dcIt == null || wtIt == null)
1221 					return true;
1222 				if (wtIt.isModified(dcIt.getDirCacheEntry(), true,
1223 						this.walk.getObjectReader())) {
1224 					return true;
1225 				}
1226 			}
1227 			return false;
1228 		}
1229 	}
1230 
1231 	private boolean isModified_IndexTree(String path, ObjectId iId,
1232 			FileMode iMode, ObjectId tId, FileMode tMode, ObjectId rootTree)
1233 			throws CorruptObjectException, IOException {
1234 		if (iMode != tMode)
1235 			return true;
1236 		if (FileMode.TREE.equals(iMode)
1237 				&& (iId == null || ObjectId.zeroId().equals(iId)))
1238 			return isModifiedSubtree_IndexTree(path, rootTree);
1239 		else
1240 			return !equalIdAndMode(iId, iMode, tId, tMode);
1241 	}
1242 
1243 	/**
1244 	 * Checks whether the subtree starting at a given path differs between Index and
1245 	 * some tree.
1246 	 *
1247 	 * @param path
1248 	 * @param tree
1249 	 *            the tree to compare
1250 	 * @return true if the subtrees differ
1251 	 * @throws CorruptObjectException
1252 	 * @throws IOException
1253 	 */
1254 	private boolean isModifiedSubtree_IndexTree(String path, ObjectId tree)
1255 			throws CorruptObjectException, IOException {
1256 		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
1257 			tw.addTree(new DirCacheIterator(dc));
1258 			tw.addTree(tree);
1259 			tw.setRecursive(true);
1260 			tw.setFilter(PathFilter.create(path));
1261 			while (tw.next()) {
1262 				AbstractTreeIterator dcIt = tw.getTree(0,
1263 						DirCacheIterator.class);
1264 				AbstractTreeIterator treeIt = tw.getTree(1,
1265 						AbstractTreeIterator.class);
1266 				if (dcIt == null || treeIt == null)
1267 					return true;
1268 				if (dcIt.getEntryRawMode() != treeIt.getEntryRawMode())
1269 					return true;
1270 				if (!dcIt.getEntryObjectId().equals(treeIt.getEntryObjectId()))
1271 					return true;
1272 			}
1273 			return false;
1274 		}
1275 	}
1276 
1277 	/**
1278 	 * Updates the file in the working tree with content and mode from an entry
1279 	 * in the index. The new content is first written to a new temporary file in
1280 	 * the same directory as the real file. Then that new file is renamed to the
1281 	 * final filename.
1282 	 *
1283 	 * <p>
1284 	 * <b>Note:</b> if the entry path on local file system exists as a non-empty
1285 	 * directory, and the target entry type is a link or file, the checkout will
1286 	 * fail with {@link IOException} since existing non-empty directory cannot
1287 	 * be renamed to file or link without deleting it recursively.
1288 	 * </p>
1289 	 *
1290 	 * <p>
1291 	 * TODO: this method works directly on File IO, we may need another
1292 	 * abstraction (like WorkingTreeIterator). This way we could tell e.g.
1293 	 * Eclipse that Files in the workspace got changed
1294 	 * </p>
1295 	 *
1296 	 * @param repo
1297 	 *            repository managing the destination work tree.
1298 	 * @param entry
1299 	 *            the entry containing new mode and content
1300 	 * @param or
1301 	 *            object reader to use for checkout
1302 	 * @throws IOException
1303 	 * @since 3.6
1304 	 */
1305 	public static void checkoutEntry(Repository repo, DirCacheEntry entry,
1306 			ObjectReader or) throws IOException {
1307 		checkoutEntry(repo, entry, or, false, null);
1308 	}
1309 
1310 	/**
1311 	 * Updates the file in the working tree with content and mode from an entry
1312 	 * in the index. The new content is first written to a new temporary file in
1313 	 * the same directory as the real file. Then that new file is renamed to the
1314 	 * final filename.
1315 	 *
1316 	 * <p>
1317 	 * <b>Note:</b> if the entry path on local file system exists as a file, it
1318 	 * will be deleted and if it exists as a directory, it will be deleted
1319 	 * recursively, independently if has any content.
1320 	 * </p>
1321 	 *
1322 	 * <p>
1323 	 * TODO: this method works directly on File IO, we may need another
1324 	 * abstraction (like WorkingTreeIterator). This way we could tell e.g.
1325 	 * Eclipse that Files in the workspace got changed
1326 	 * </p>
1327 	 *
1328 	 * @param repo
1329 	 *            repository managing the destination work tree.
1330 	 * @param entry
1331 	 *            the entry containing new mode and content
1332 	 * @param or
1333 	 *            object reader to use for checkout
1334 	 * @param deleteRecursive
1335 	 *            true to recursively delete final path if it exists on the file
1336 	 *            system
1337 	 * @param checkoutMetadata
1338 	 *            containing
1339 	 *            <ul>
1340 	 *            <li>smudgeFilterCommand to be run for smudging the entry to be
1341 	 *            checked out</li>
1342 	 *            <li>eolStreamType used for stream conversion</li>
1343 	 *            </ul>
1344 	 *
1345 	 * @throws IOException
1346 	 * @since 4.2
1347 	 */
1348 	public static void checkoutEntry(Repository repo, DirCacheEntry entry,
1349 			ObjectReader or, boolean deleteRecursive,
1350 			CheckoutMetadata checkoutMetadata) throws IOException {
1351 		if (checkoutMetadata == null)
1352 			checkoutMetadata = CheckoutMetadata.EMPTY;
1353 		ObjectLoader ol = or.open(entry.getObjectId());
1354 		File f = new File(repo.getWorkTree(), entry.getPathString());
1355 		File parentDir = f.getParentFile();
1356 		FileUtils.mkdirs(parentDir, true);
1357 		FS fs = repo.getFS();
1358 		WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY);
1359 		if (entry.getFileMode() == FileMode.SYMLINK
1360 				&& opt.getSymLinks() == SymLinks.TRUE) {
1361 			byte[] bytes = ol.getBytes();
1362 			String target = RawParseUtils.decode(bytes);
1363 			if (deleteRecursive && f.isDirectory()) {
1364 				FileUtils.delete(f, FileUtils.RECURSIVE);
1365 			}
1366 			fs.createSymLink(f, target);
1367 			entry.setLength(bytes.length);
1368 			entry.setLastModified(fs.lastModified(f));
1369 			return;
1370 		}
1371 
1372 		String name = f.getName();
1373 		if (name.length() > 200) {
1374 			name = name.substring(0, 200);
1375 		}
1376 		File tmpFile = File.createTempFile(
1377 				"._" + name, null, parentDir); //$NON-NLS-1$
1378 
1379 		EolStreamType nonNullEolStreamType;
1380 		if (checkoutMetadata.eolStreamType != null) {
1381 			nonNullEolStreamType = checkoutMetadata.eolStreamType;
1382 		} else if (opt.getAutoCRLF() == AutoCRLF.TRUE) {
1383 			nonNullEolStreamType = EolStreamType.AUTO_CRLF;
1384 		} else {
1385 			nonNullEolStreamType = EolStreamType.DIRECT;
1386 		}
1387 		try (OutputStream channel = EolStreamTypeUtil.wrapOutputStream(
1388 				new FileOutputStream(tmpFile), nonNullEolStreamType)) {
1389 			if (checkoutMetadata.smudgeFilterCommand != null) {
1390 				if (FilterCommandRegistry
1391 						.isRegistered(checkoutMetadata.smudgeFilterCommand)) {
1392 					runBuiltinFilterCommand(repo, checkoutMetadata, ol,
1393 							channel);
1394 				} else {
1395 					runExternalFilterCommand(repo, entry, checkoutMetadata, ol,
1396 							fs, channel);
1397 				}
1398 			} else {
1399 				ol.copyTo(channel);
1400 			}
1401 		}
1402 		// The entry needs to correspond to the on-disk filesize. If the content
1403 		// was filtered (either by autocrlf handling or smudge filters) ask the
1404 		// filesystem again for the length. Otherwise the objectloader knows the
1405 		// size
1406 		if (checkoutMetadata.eolStreamType == EolStreamType.DIRECT
1407 				&& checkoutMetadata.smudgeFilterCommand == null) {
1408 			entry.setLength(ol.getSize());
1409 		} else {
1410 			entry.setLength(tmpFile.length());
1411 		}
1412 
1413 		if (opt.isFileMode() && fs.supportsExecute()) {
1414 			if (FileMode.EXECUTABLE_FILE.equals(entry.getRawMode())) {
1415 				if (!fs.canExecute(tmpFile))
1416 					fs.setExecute(tmpFile, true);
1417 			} else {
1418 				if (fs.canExecute(tmpFile))
1419 					fs.setExecute(tmpFile, false);
1420 			}
1421 		}
1422 		try {
1423 			if (deleteRecursive && f.isDirectory()) {
1424 				FileUtils.delete(f, FileUtils.RECURSIVE);
1425 			}
1426 			FileUtils.rename(tmpFile, f, StandardCopyOption.ATOMIC_MOVE);
1427 		} catch (IOException e) {
1428 			throw new IOException(
1429 					MessageFormat.format(JGitText.get().renameFileFailed,
1430 							tmpFile.getPath(), f.getPath()),
1431 					e);
1432 		} finally {
1433 			if (tmpFile.exists()) {
1434 				FileUtils.delete(tmpFile);
1435 			}
1436 		}
1437 		entry.setLastModified(fs.lastModified(f));
1438 	}
1439 
1440 	// Run an external filter command
1441 	private static void runExternalFilterCommand(Repository repo,
1442 			DirCacheEntry entry,
1443 			CheckoutMetadata checkoutMetadata, ObjectLoader ol, FS fs,
1444 			OutputStream channel) throws IOException {
1445 		ProcessBuilder filterProcessBuilder = fs.runInShell(
1446 				checkoutMetadata.smudgeFilterCommand, new String[0]);
1447 		filterProcessBuilder.directory(repo.getWorkTree());
1448 		filterProcessBuilder.environment().put(Constants.GIT_DIR_KEY,
1449 				repo.getDirectory().getAbsolutePath());
1450 		ExecutionResult result;
1451 		int rc;
1452 		try {
1453 			// TODO: wire correctly with AUTOCRLF
1454 			result = fs.execute(filterProcessBuilder, ol.openStream());
1455 			rc = result.getRc();
1456 			if (rc == 0) {
1457 				result.getStdout().writeTo(channel,
1458 						NullProgressMonitor.INSTANCE);
1459 			}
1460 		} catch (IOException | InterruptedException e) {
1461 			throw new IOException(new FilterFailedException(e,
1462 					checkoutMetadata.smudgeFilterCommand,
1463 					entry.getPathString()));
1464 		}
1465 		if (rc != 0) {
1466 			throw new IOException(new FilterFailedException(rc,
1467 					checkoutMetadata.smudgeFilterCommand,
1468 					entry.getPathString(),
1469 					result.getStdout().toByteArray(MAX_EXCEPTION_TEXT_SIZE),
1470 					RawParseUtils.decode(result.getStderr()
1471 							.toByteArray(MAX_EXCEPTION_TEXT_SIZE))));
1472 		}
1473 	}
1474 
1475 	// Run a builtin filter command
1476 	private static void runBuiltinFilterCommand(Repository repo,
1477 			CheckoutMetadata checkoutMetadata, ObjectLoader ol,
1478 			OutputStream channel) throws MissingObjectException, IOException {
1479 		FilterCommand command = null;
1480 		try {
1481 			command = FilterCommandRegistry.createFilterCommand(
1482 					checkoutMetadata.smudgeFilterCommand, repo, ol.openStream(),
1483 					channel);
1484 		} catch (IOException e) {
1485 			LOG.error(JGitText.get().failedToDetermineFilterDefinition, e);
1486 			// In case an IOException occurred during creating of the command
1487 			// then proceed as if there would not have been a builtin filter.
1488 			ol.copyTo(channel);
1489 		}
1490 		if (command != null) {
1491 			while (command.run() != -1) {
1492 				// loop as long as command.run() tells there is work to do
1493 			}
1494 		}
1495 	}
1496 
1497 	@SuppressWarnings("deprecation")
1498 	private static void checkValidPath(CanonicalTreeParser t)
1499 			throws InvalidPathException {
1500 		ObjectChecker chk = new ObjectChecker()
1501 			.setSafeForWindows(SystemReader.getInstance().isWindows())
1502 			.setSafeForMacOS(SystemReader.getInstance().isMacOS());
1503 		for (CanonicalTreeParser i = t; i != null; i = i.getParent())
1504 			checkValidPathSegment(chk, i);
1505 	}
1506 
1507 	private static void checkValidPathSegment(ObjectChecker chk,
1508 			CanonicalTreeParser t) throws InvalidPathException {
1509 		try {
1510 			int ptr = t.getNameOffset();
1511 			int end = ptr + t.getNameLength();
1512 			chk.checkPathSegment(t.getEntryPathBuffer(), ptr, end);
1513 		} catch (CorruptObjectException err) {
1514 			String path = t.getEntryPathString();
1515 			InvalidPathException i = new InvalidPathException(path);
1516 			i.initCause(err);
1517 			throw i;
1518 		}
1519 	}
1520 }