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