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