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