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