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