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