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