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