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(ObjectIdrg/eclipse/jgit/lib/ObjectId.html#ObjectId">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../../org/eclipse/jgit/treewalk/CanonicalTreeParser.html#CanonicalTreeParser">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)
950 						update(name, mId, mMode);
951 					else
952 						keep(name, dce, f);
953 				} else
954 					conflict(name, dce, h, m);
955 			}
956 		} else {
957 			// Something in Index
958 			if (h == null) {
959 				// Nothing in Head
960 				// Something in Index
961 				/**
962 				 * <pre>
963 				 * 	          clean I==H  I==M       H        M        Result
964 				 * 	         -----------------------------------------------------
965 				 * 	        4 yes   N/A   N/A     nothing  nothing  keep index
966 				 * 	        5 no    N/A   N/A     nothing  nothing  keep index
967 				 *
968 				 * 	        6 yes   N/A   yes     nothing  exists   keep index
969 				 * 	        7 no    N/A   yes     nothing  exists   keep index
970 				 * 	        8 yes   N/A   no      nothing  exists   fail
971 				 * 	        9 no    N/A   no      nothing  exists   fail
972 				 * </pre>
973 				 */
974 
975 				if (m == null
976 						|| !isModified_IndexTree(name, iId, iMode, mId, mMode,
977 								mergeCommitTree)) {
978 					// Merge contains nothing or the same as Index
979 					// Nothing in Head
980 					// Something in Index
981 					if (m==null && walk.isDirectoryFileConflict()) {
982 						// Nothing in Merge and current path is part of
983 						// File/Folder conflict
984 						// Nothing in Head
985 						// Something in Index
986 						if (dce != null
987 								&& (f == null || f.isModified(dce, true,
988 										this.walk.getObjectReader())))
989 							// No file or file is dirty
990 							// Nothing in Merge and current path is part of
991 							// File/Folder conflict
992 							// Nothing in Head
993 							// Something in Index
994 							// -> File folder conflict and Merge wants this
995 							// path to be removed. Since the file is dirty
996 							// report a conflict
997 							conflict(name, dce, h, m);
998 						else
999 							// A file is present and file is not dirty
1000 							// Nothing in Merge and current path is part of
1001 							// File/Folder conflict
1002 							// Nothing in Head
1003 							// Something in Index
1004 							// -> File folder conflict and Merge wants this path
1005 							// to be removed. Since the file is not dirty remove
1006 							// file and index entry
1007 							remove(name);
1008 					} else
1009 						// Something in Merge or current path is not part of
1010 						// File/Folder conflict
1011 						// Merge contains nothing or the same as Index
1012 						// Nothing in Head
1013 						// Something in Index
1014 						// -> Merge contains nothing new. Keep the index.
1015 						keep(name, dce, f);
1016 				} else
1017 					// Merge contains something and it is not the same as Index
1018 					// Nothing in Head
1019 					// Something in Index
1020 					// -> Index contains something new (different from Head)
1021 					// and Merge is different from Index. Report a conflict
1022 					conflict(name, dce, h, m);
1023 			} else if (m == null) {
1024 				// Nothing in Merge
1025 				// Something in Head
1026 				// Something in Index
1027 
1028 				/**
1029 				 * <pre>
1030 				 * 	           clean I==H  I==M       H        M        Result
1031 				 * 	         -----------------------------------------------------
1032 				 * 	        10 yes   yes   N/A     exists   nothing  remove path from index
1033 				 * 	        11 no    yes   N/A     exists   nothing  keep file
1034 				 * 	        12 yes   no    N/A     exists   nothing  fail
1035 				 * 	        13 no    no    N/A     exists   nothing  fail
1036 				 * </pre>
1037 				 */
1038 
1039 				if (iMode == FileMode.GITLINK) {
1040 					// A submodule in Index
1041 					// Nothing in Merge
1042 					// Something in Head
1043 					// Submodules that disappear from the checkout must
1044 					// be removed from the index, but not deleted from disk.
1045 					remove(name);
1046 				} else {
1047 					// Something different from a submodule in Index
1048 					// Nothing in Merge
1049 					// Something in Head
1050 					if (!isModified_IndexTree(name, iId, iMode, hId, hMode,
1051 							headCommitTree)) {
1052 						// Index contains the same as Head
1053 						// Something different from a submodule in Index
1054 						// Nothing in Merge
1055 						// Something in Head
1056 						if (f != null
1057 								&& f.isModified(dce, true,
1058 										this.walk.getObjectReader())) {
1059 							// file is dirty
1060 							// Index contains the same as Head
1061 							// Something different from a submodule in Index
1062 							// Nothing in Merge
1063 							// Something in Head
1064 
1065 							if (!FileMode.TREE.equals(f.getEntryFileMode())
1066 									&& FileMode.TREE.equals(iMode)) {
1067 								// The workingtree contains a file and the index semantically contains a folder.
1068 								// Git considers the workingtree file as untracked. Just keep the untracked file.
1069 								return;
1070 							}
1071 							// -> file is dirty and tracked but is should be
1072 							// removed. That's a conflict
1073 							conflict(name, dce, h, m);
1074 						} else {
1075 							// file doesn't exist or is clean
1076 							// Index contains the same as Head
1077 							// Something different from a submodule in Index
1078 							// Nothing in Merge
1079 							// Something in Head
1080 							// -> Remove from index and delete the file
1081 							remove(name);
1082 						}
1083 					} else {
1084 						// Index contains something different from Head
1085 						// Something different from a submodule in Index
1086 						// Nothing in Merge
1087 						// Something in Head
1088 						// -> Something new is in index (and maybe even on the
1089 						// filesystem). But Merge wants the path to be removed.
1090 						// Report a conflict
1091 						conflict(name, dce, h, m);
1092 					}
1093 				}
1094 			} else {
1095 				// Something in Merge
1096 				// Something in Head
1097 				// Something in Index
1098 				if (!equalIdAndMode(hId, hMode, mId, mMode)
1099 						&& isModified_IndexTree(name, iId, iMode, hId, hMode,
1100 								headCommitTree)
1101 						&& isModified_IndexTree(name, iId, iMode, mId, mMode,
1102 								mergeCommitTree))
1103 					// All three contents in Head, Merge, Index differ from each
1104 					// other
1105 					// -> All contents differ. Report a conflict.
1106 					conflict(name, dce, h, m);
1107 				else
1108 					// At least two of the contents of Head, Index, Merge
1109 					// are the same
1110 					// Something in Merge
1111 					// Something in Head
1112 					// Something in Index
1113 
1114 				if (!isModified_IndexTree(name, iId, iMode, hId, hMode,
1115 						headCommitTree)
1116 						&& isModified_IndexTree(name, iId, iMode, mId, mMode,
1117 								mergeCommitTree)) {
1118 						// Head contains the same as Index. Merge differs
1119 						// Something in Merge
1120 
1121 					// For submodules just update the index with the new SHA-1
1122 					if (dce != null
1123 							&& FileMode.GITLINK.equals(dce.getFileMode())) {
1124 						// Index and Head contain the same submodule. Merge
1125 						// differs
1126 						// Something in Merge
1127 						// -> Nothing new in index. Move to merge.
1128 						// Potentially updates the file
1129 
1130 						// TODO check that we don't overwrite some unsaved
1131 						// file content
1132 						update(name, mId, mMode);
1133 					} else if (dce != null
1134 							&& (f != null && f.isModified(dce, true,
1135 									this.walk.getObjectReader()))) {
1136 						// File exists and is dirty
1137 						// Head and Index don't contain a submodule
1138 						// Head contains the same as Index. Merge differs
1139 						// Something in Merge
1140 						// -> Merge wants the index and file to be updated
1141 						// but the file is dirty. Report a conflict
1142 						conflict(name, dce, h, m);
1143 					} else {
1144 						// File doesn't exist or is clean
1145 						// Head and Index don't contain a submodule
1146 						// Head contains the same as Index. Merge differs
1147 						// Something in Merge
1148 						// -> Standard case when switching between branches:
1149 						// Nothing new in index but something different in
1150 						// Merge. Update index and file
1151 						update(name, mId, mMode);
1152 					}
1153 				} else {
1154 					// Head differs from index or merge is same as index
1155 					// At least two of the contents of Head, Index, Merge
1156 					// are the same
1157 					// Something in Merge
1158 					// Something in Head
1159 					// Something in Index
1160 
1161 					// Can be formulated as: Either all three states are
1162 					// equal or Merge is equal to Head or Index and differs
1163 					// to the other one.
1164 					// -> In all three cases we don't touch index and file.
1165 
1166 					keep(name, dce, f);
1167 				}
1168 			}
1169 		}
1170 	}
1171 
1172 	private static boolean idEqual(AbstractTreeIterator a,
1173 			AbstractTreeIterator b) {
1174 		if (a == b) {
1175 			return true;
1176 		}
1177 		if (a == null || b == null) {
1178 			return false;
1179 		}
1180 		return a.getEntryObjectId().equals(b.getEntryObjectId());
1181 	}
1182 
1183 	/**
1184 	 * A conflict is detected - add the three different stages to the index
1185 	 * @param path the path of the conflicting entry
1186 	 * @param e the previous index entry
1187 	 * @param h the first tree you want to merge (the HEAD)
1188 	 * @param m the second tree you want to merge
1189 	 */
1190 	private void conflict(String path, DirCacheEntry e, AbstractTreeIterator./../org/eclipse/jgit/treewalk/AbstractTreeIterator.html#AbstractTreeIterator">AbstractTreeIterator h, AbstractTreeIterator m) {
1191 		conflicts.add(path);
1192 
1193 		DirCacheEntry entry;
1194 		if (e != null) {
1195 			entry = new DirCacheEntry(e.getPathString(), DirCacheEntry.STAGE_1);
1196 			entry.copyMetaData(e, true);
1197 			builder.add(entry);
1198 		}
1199 
1200 		if (h != null && !FileMode.TREE.equals(h.getEntryFileMode())) {
1201 			entry = new DirCacheEntry(h.getEntryPathString(), DirCacheEntry.STAGE_2);
1202 			entry.setFileMode(h.getEntryFileMode());
1203 			entry.setObjectId(h.getEntryObjectId());
1204 			builder.add(entry);
1205 		}
1206 
1207 		if (m != null && !FileMode.TREE.equals(m.getEntryFileMode())) {
1208 			entry = new DirCacheEntry(m.getEntryPathString(), DirCacheEntry.STAGE_3);
1209 			entry.setFileMode(m.getEntryFileMode());
1210 			entry.setObjectId(m.getEntryObjectId());
1211 			builder.add(entry);
1212 		}
1213 	}
1214 
1215 	private void keep(String path, DirCacheEntry e, WorkingTreeIterator f)
1216 			throws IOException {
1217 		if (e != null && !FileMode.TREE.equals(e.getFileMode()))
1218 			builder.add(e);
1219 		if (force) {
1220 			if (f == null || f.isModified(e, true, walk.getObjectReader())) {
1221 				kept.add(path);
1222 				checkoutEntry(repo, e, walk.getObjectReader(), false,
1223 						new CheckoutMetadata(walk.getEolStreamType(CHECKOUT_OP),
1224 								walk.getFilterCommand(
1225 										Constants.ATTR_FILTER_TYPE_SMUDGE)));
1226 			}
1227 		}
1228 	}
1229 
1230 	private void remove(String path) {
1231 		removed.add(path);
1232 	}
1233 
1234 	private void update(String path, ObjectId mId, FileMode mode)
1235 			throws IOException {
1236 		if (!FileMode.TREE.equals(mode)) {
1237 			updated.put(path, new CheckoutMetadata(
1238 					walk.getEolStreamType(CHECKOUT_OP),
1239 					walk.getFilterCommand(Constants.ATTR_FILTER_TYPE_SMUDGE)));
1240 
1241 			DirCacheEntry entry = new DirCacheEntry(path, DirCacheEntry.STAGE_0);
1242 			entry.setObjectId(mId);
1243 			entry.setFileMode(mode);
1244 			builder.add(entry);
1245 		}
1246 	}
1247 
1248 	/**
1249 	 * If <code>true</code>, will scan first to see if it's possible to check
1250 	 * out, otherwise throw
1251 	 * {@link org.eclipse.jgit.errors.CheckoutConflictException}. If
1252 	 * <code>false</code>, it will silently deal with the problem.
1253 	 *
1254 	 * @param failOnConflict
1255 	 *            a boolean.
1256 	 */
1257 	public void setFailOnConflict(boolean failOnConflict) {
1258 		this.failOnConflict = failOnConflict;
1259 	}
1260 
1261 	/**
1262 	 * If <code>true</code>, dirty worktree files may be overridden. If
1263 	 * <code>false</code> dirty worktree files will not be overridden in order
1264 	 * not to delete unsaved content. This corresponds to native git's 'git
1265 	 * checkout -f' option. By default this option is set to false.
1266 	 *
1267 	 * @param force
1268 	 *            a boolean.
1269 	 * @since 5.3
1270 	 */
1271 	public void setForce(boolean force) {
1272 		this.force = force;
1273 	}
1274 
1275 	/**
1276 	 * This method implements how to handle conflicts when
1277 	 * {@link #failOnConflict} is false
1278 	 *
1279 	 * @throws CheckoutConflictException
1280 	 */
1281 	private void cleanUpConflicts() throws CheckoutConflictException {
1282 		// TODO: couldn't we delete unsaved worktree content here?
1283 		for (String c : conflicts) {
1284 			File conflict = new File(repo.getWorkTree(), c);
1285 			if (!conflict.delete())
1286 				throw new CheckoutConflictException(MessageFormat.format(
1287 						JGitText.get().cannotDeleteFile, c));
1288 			removeEmptyParents(conflict);
1289 		}
1290 	}
1291 
1292 	/**
1293 	 * Checks whether the subtree starting at a given path differs between Index and
1294 	 * workingtree.
1295 	 *
1296 	 * @param path
1297 	 * @return true if the subtrees differ
1298 	 * @throws CorruptObjectException
1299 	 * @throws IOException
1300 	 */
1301 	private boolean isModifiedSubtree_IndexWorkingtree(String path)
1302 			throws CorruptObjectException, IOException {
1303 		try (NameConflictTreeWalkTreeWalk.html#NameConflictTreeWalk">NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
1304 			int dciPos = tw.addTree(new DirCacheIterator(dc));
1305 			FileTreeIterator fti = new FileTreeIterator(repo);
1306 			tw.addTree(fti);
1307 			fti.setDirCacheIterator(tw, dciPos);
1308 			tw.setRecursive(true);
1309 			tw.setFilter(PathFilter.create(path));
1310 			DirCacheIterator dcIt;
1311 			WorkingTreeIterator wtIt;
1312 			while (tw.next()) {
1313 				dcIt = tw.getTree(0, DirCacheIterator.class);
1314 				wtIt = tw.getTree(1, WorkingTreeIterator.class);
1315 				if (dcIt == null || wtIt == null)
1316 					return true;
1317 				if (wtIt.isModified(dcIt.getDirCacheEntry(), true,
1318 						this.walk.getObjectReader())) {
1319 					return true;
1320 				}
1321 			}
1322 			return false;
1323 		}
1324 	}
1325 
1326 	private boolean isModified_IndexTree(String path, ObjectId iId,
1327 			FileMode iMode, ObjectIdrg/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId tId, FileMode tMode, ObjectId rootTree)
1328 			throws CorruptObjectException, IOException {
1329 		if (iMode != tMode) {
1330 			return true;
1331 		}
1332 		if (FileMode.TREE.equals(iMode)
1333 				&& (iId == null || ObjectId.zeroId().equals(iId))) {
1334 			return isModifiedSubtree_IndexTree(path, rootTree);
1335 		}
1336 		return !equalIdAndMode(iId, iMode, tId, tMode);
1337 	}
1338 
1339 	/**
1340 	 * Checks whether the subtree starting at a given path differs between Index and
1341 	 * some tree.
1342 	 *
1343 	 * @param path
1344 	 * @param tree
1345 	 *            the tree to compare
1346 	 * @return true if the subtrees differ
1347 	 * @throws CorruptObjectException
1348 	 * @throws IOException
1349 	 */
1350 	private boolean isModifiedSubtree_IndexTree(String path, ObjectId tree)
1351 			throws CorruptObjectException, IOException {
1352 		try (NameConflictTreeWalkTreeWalk.html#NameConflictTreeWalk">NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
1353 			tw.addTree(new DirCacheIterator(dc));
1354 			tw.addTree(tree);
1355 			tw.setRecursive(true);
1356 			tw.setFilter(PathFilter.create(path));
1357 			while (tw.next()) {
1358 				AbstractTreeIterator dcIt = tw.getTree(0,
1359 						DirCacheIterator.class);
1360 				AbstractTreeIterator treeIt = tw.getTree(1,
1361 						AbstractTreeIterator.class);
1362 				if (dcIt == null || treeIt == null)
1363 					return true;
1364 				if (dcIt.getEntryRawMode() != treeIt.getEntryRawMode())
1365 					return true;
1366 				if (!dcIt.getEntryObjectId().equals(treeIt.getEntryObjectId()))
1367 					return true;
1368 			}
1369 			return false;
1370 		}
1371 	}
1372 
1373 	/**
1374 	 * Updates the file in the working tree with content and mode from an entry
1375 	 * in the index. The new content is first written to a new temporary file in
1376 	 * the same directory as the real file. Then that new file is renamed to the
1377 	 * final filename.
1378 	 *
1379 	 * <p>
1380 	 * <b>Note:</b> if the entry path on local file system exists as a non-empty
1381 	 * directory, and the target entry type is a link or file, the checkout will
1382 	 * fail with {@link java.io.IOException} since existing non-empty directory
1383 	 * cannot be renamed to file or link without deleting it recursively.
1384 	 * </p>
1385 	 *
1386 	 * <p>
1387 	 * TODO: this method works directly on File IO, we may need another
1388 	 * abstraction (like WorkingTreeIterator). This way we could tell e.g.
1389 	 * Eclipse that Files in the workspace got changed
1390 	 * </p>
1391 	 *
1392 	 * @param repo
1393 	 *            repository managing the destination work tree.
1394 	 * @param entry
1395 	 *            the entry containing new mode and content
1396 	 * @param or
1397 	 *            object reader to use for checkout
1398 	 * @throws java.io.IOException
1399 	 * @since 3.6
1400 	 * @deprecated since 5.1, use
1401 	 *             {@link #checkoutEntry(Repository, DirCacheEntry, ObjectReader, boolean, CheckoutMetadata)}
1402 	 *             instead
1403 	 */
1404 	@Deprecated
1405 	public static void checkoutEntry(Repository repo, DirCacheEntry entry,
1406 			ObjectReader or) throws IOException {
1407 		checkoutEntry(repo, entry, or, false, null);
1408 	}
1409 
1410 	/**
1411 	 * Updates the file in the working tree with content and mode from an entry
1412 	 * in the index. The new content is first written to a new temporary file in
1413 	 * the same directory as the real file. Then that new file is renamed to the
1414 	 * final filename.
1415 	 *
1416 	 * <p>
1417 	 * <b>Note:</b> if the entry path on local file system exists as a file, it
1418 	 * will be deleted and if it exists as a directory, it will be deleted
1419 	 * recursively, independently if has any content.
1420 	 * </p>
1421 	 *
1422 	 * <p>
1423 	 * TODO: this method works directly on File IO, we may need another
1424 	 * abstraction (like WorkingTreeIterator). This way we could tell e.g.
1425 	 * Eclipse that Files in the workspace got changed
1426 	 * </p>
1427 	 *
1428 	 * @param repo
1429 	 *            repository managing the destination work tree.
1430 	 * @param entry
1431 	 *            the entry containing new mode and content
1432 	 * @param or
1433 	 *            object reader to use for checkout
1434 	 * @param deleteRecursive
1435 	 *            true to recursively delete final path if it exists on the file
1436 	 *            system
1437 	 * @param checkoutMetadata
1438 	 *            containing
1439 	 *            <ul>
1440 	 *            <li>smudgeFilterCommand to be run for smudging the entry to be
1441 	 *            checked out</li>
1442 	 *            <li>eolStreamType used for stream conversion</li>
1443 	 *            </ul>
1444 	 * @throws java.io.IOException
1445 	 * @since 4.2
1446 	 */
1447 	public static void checkoutEntry(Repository repo, DirCacheEntry entry,
1448 			ObjectReader or, boolean deleteRecursive,
1449 			CheckoutMetadata checkoutMetadata) throws IOException {
1450 		if (checkoutMetadata == null)
1451 			checkoutMetadata = CheckoutMetadata.EMPTY;
1452 		ObjectLoader ol = or.open(entry.getObjectId());
1453 		File f = new File(repo.getWorkTree(), entry.getPathString());
1454 		File parentDir = f.getParentFile();
1455 		if (parentDir.isFile()) {
1456 			FileUtils.delete(parentDir);
1457 		}
1458 		FileUtils.mkdirs(parentDir, true);
1459 		FS fs = repo.getFS();
1460 		WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY);
1461 		if (entry.getFileMode() == FileMode.SYMLINK
1462 				&& opt.getSymLinks() == SymLinks.TRUE) {
1463 			byte[] bytes = ol.getBytes();
1464 			String target = RawParseUtils.decode(bytes);
1465 			if (deleteRecursive && f.isDirectory()) {
1466 				FileUtils.delete(f, FileUtils.RECURSIVE);
1467 			}
1468 			fs.createSymLink(f, target);
1469 			entry.setLength(bytes.length);
1470 			entry.setLastModified(fs.lastModifiedInstant(f));
1471 			return;
1472 		}
1473 
1474 		String name = f.getName();
1475 		if (name.length() > 200) {
1476 			name = name.substring(0, 200);
1477 		}
1478 		File tmpFile = File.createTempFile(
1479 				"._" + name, null, parentDir); //$NON-NLS-1$
1480 
1481 		getContent(repo, entry.getPathString(), checkoutMetadata, ol, opt,
1482 				new FileOutputStream(tmpFile));
1483 
1484 		// The entry needs to correspond to the on-disk filesize. If the content
1485 		// was filtered (either by autocrlf handling or smudge filters) ask the
1486 		// filesystem again for the length. Otherwise the objectloader knows the
1487 		// size
1488 		if (checkoutMetadata.eolStreamType == EolStreamType.DIRECT
1489 				&& checkoutMetadata.smudgeFilterCommand == null) {
1490 			entry.setLength(ol.getSize());
1491 		} else {
1492 			entry.setLength(tmpFile.length());
1493 		}
1494 
1495 		if (opt.isFileMode() && fs.supportsExecute()) {
1496 			if (FileMode.EXECUTABLE_FILE.equals(entry.getRawMode())) {
1497 				if (!fs.canExecute(tmpFile))
1498 					fs.setExecute(tmpFile, true);
1499 			} else {
1500 				if (fs.canExecute(tmpFile))
1501 					fs.setExecute(tmpFile, false);
1502 			}
1503 		}
1504 		try {
1505 			if (deleteRecursive && f.isDirectory()) {
1506 				FileUtils.delete(f, FileUtils.RECURSIVE);
1507 			}
1508 			FileUtils.rename(tmpFile, f, StandardCopyOption.ATOMIC_MOVE);
1509 		} catch (IOException e) {
1510 			throw new IOException(
1511 					MessageFormat.format(JGitText.get().renameFileFailed,
1512 							tmpFile.getPath(), f.getPath()),
1513 					e);
1514 		} finally {
1515 			if (tmpFile.exists()) {
1516 				FileUtils.delete(tmpFile);
1517 			}
1518 		}
1519 		entry.setLastModified(fs.lastModifiedInstant(f));
1520 	}
1521 
1522 	/**
1523 	 * Return filtered content for a specific object (blob). EOL handling and
1524 	 * smudge-filter handling are applied in the same way as it would be done
1525 	 * during a checkout.
1526 	 *
1527 	 * @param repo
1528 	 *            the repository
1529 	 * @param path
1530 	 *            the path used to determine the correct filters for the object
1531 	 * @param checkoutMetadata
1532 	 *            containing
1533 	 *            <ul>
1534 	 *            <li>smudgeFilterCommand to be run for smudging the object</li>
1535 	 *            <li>eolStreamType used for stream conversion (can be
1536 	 *            null)</li>
1537 	 *            </ul>
1538 	 * @param ol
1539 	 *            the object loader to read raw content of the object
1540 	 * @param opt
1541 	 *            the working tree options where only 'core.autocrlf' is used
1542 	 *            for EOL handling if 'checkoutMetadata.eolStreamType' is not
1543 	 *            valid
1544 	 * @param os
1545 	 *            the output stream the filtered content is written to. The
1546 	 *            caller is responsible to close the stream.
1547 	 * @throws IOException
1548 	 *
1549 	 * @since 5.7
1550 	 */
1551 	public static void getContent(Repository repo, String path,
1552 			CheckoutMetadata checkoutMetadata, ObjectLoader ol,
1553 			WorkingTreeOptions opt, OutputStream os)
1554 			throws IOException {
1555 		EolStreamType nonNullEolStreamType;
1556 		if (checkoutMetadata.eolStreamType != null) {
1557 			nonNullEolStreamType = checkoutMetadata.eolStreamType;
1558 		} else if (opt.getAutoCRLF() == AutoCRLF.TRUE) {
1559 			nonNullEolStreamType = EolStreamType.AUTO_CRLF;
1560 		} else {
1561 			nonNullEolStreamType = EolStreamType.DIRECT;
1562 		}
1563 		try (OutputStream channel = EolStreamTypeUtil.wrapOutputStream(
1564 				os, nonNullEolStreamType)) {
1565 			if (checkoutMetadata.smudgeFilterCommand != null) {
1566 				if (FilterCommandRegistry
1567 						.isRegistered(checkoutMetadata.smudgeFilterCommand)) {
1568 					runBuiltinFilterCommand(repo, checkoutMetadata, ol,
1569 							channel);
1570 				} else {
1571 					runExternalFilterCommand(repo, path, checkoutMetadata, ol,
1572 							channel);
1573 				}
1574 			} else {
1575 				ol.copyTo(channel);
1576 			}
1577 		}
1578 	}
1579 
1580 	// Run an external filter command
1581 	private static void runExternalFilterCommand(Repository repo, String path,
1582 			CheckoutMetadata checkoutMetadata, ObjectLoader ol,
1583 			OutputStream channel) throws IOException {
1584 		FS fs = repo.getFS();
1585 		ProcessBuilder filterProcessBuilder = fs.runInShell(
1586 				checkoutMetadata.smudgeFilterCommand, new String[0]);
1587 		filterProcessBuilder.directory(repo.getWorkTree());
1588 		filterProcessBuilder.environment().put(Constants.GIT_DIR_KEY,
1589 				repo.getDirectory().getAbsolutePath());
1590 		ExecutionResult result;
1591 		int rc;
1592 		try {
1593 			// TODO: wire correctly with AUTOCRLF
1594 			result = fs.execute(filterProcessBuilder, ol.openStream());
1595 			rc = result.getRc();
1596 			if (rc == 0) {
1597 				result.getStdout().writeTo(channel,
1598 						NullProgressMonitor.INSTANCE);
1599 			}
1600 		} catch (IOException | InterruptedException e) {
1601 			throw new IOException(new FilterFailedException(e,
1602 					checkoutMetadata.smudgeFilterCommand,
1603 					path));
1604 		}
1605 		if (rc != 0) {
1606 			throw new IOException(new FilterFailedException(rc,
1607 					checkoutMetadata.smudgeFilterCommand,
1608 					path,
1609 					result.getStdout().toByteArray(MAX_EXCEPTION_TEXT_SIZE),
1610 					RawParseUtils.decode(result.getStderr()
1611 							.toByteArray(MAX_EXCEPTION_TEXT_SIZE))));
1612 		}
1613 	}
1614 
1615 	// Run a builtin filter command
1616 	private static void runBuiltinFilterCommand(Repository repo,
1617 			CheckoutMetadata checkoutMetadata, ObjectLoader ol,
1618 			OutputStream channel) throws MissingObjectException, IOException {
1619 		boolean isMandatory = repo.getConfig().getBoolean(
1620 				ConfigConstants.CONFIG_FILTER_SECTION,
1621 				ConfigConstants.CONFIG_SECTION_LFS,
1622 				ConfigConstants.CONFIG_KEY_REQUIRED, false);
1623 		FilterCommand command = null;
1624 		try {
1625 			command = FilterCommandRegistry.createFilterCommand(
1626 					checkoutMetadata.smudgeFilterCommand, repo, ol.openStream(),
1627 					channel);
1628 		} catch (IOException e) {
1629 			LOG.error(JGitText.get().failedToDetermineFilterDefinition, e);
1630 			if (!isMandatory) {
1631 				// In case an IOException occurred during creating of the
1632 				// command then proceed as if there would not have been a
1633 				// builtin filter (only if the filter is not mandatory).
1634 				ol.copyTo(channel);
1635 			} else {
1636 				throw e;
1637 			}
1638 		}
1639 		if (command != null) {
1640 			while (command.run() != -1) {
1641 				// loop as long as command.run() tells there is work to do
1642 			}
1643 		}
1644 	}
1645 
1646 	@SuppressWarnings("deprecation")
1647 	private static void checkValidPath(CanonicalTreeParser t)
1648 			throws InvalidPathException {
1649 		ObjectChecker chk = new ObjectChecker()
1650 			.setSafeForWindows(SystemReader.getInstance().isWindows())
1651 			.setSafeForMacOS(SystemReader.getInstance().isMacOS());
1652 		for (CanonicalTreeParser i = t; i != null; i = i.getParent())
1653 			checkValidPathSegment(chk, i);
1654 	}
1655 
1656 	private static void checkValidPathSegment(ObjectChecker chk,
1657 			CanonicalTreeParser t) throws InvalidPathException {
1658 		try {
1659 			int ptr = t.getNameOffset();
1660 			int end = ptr + t.getNameLength();
1661 			chk.checkPathSegment(t.getEntryPathBuffer(), ptr, end);
1662 		} catch (CorruptObjectException err) {
1663 			String path = t.getEntryPathString();
1664 			InvalidPathException i = new InvalidPathException(path);
1665 			i.initCause(err);
1666 			throw i;
1667 		}
1668 	}
1669 }