View Javadoc
1   /*
2    * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3    * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
5    * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
6    * Copyright (C) 2014, Axel Richard <axel.richard@obeo.fr>
7    * and other copyright owners as documented in the project's IP log.
8    *
9    * This program and the accompanying materials are made available
10   * under the terms of the Eclipse Distribution License v1.0 which
11   * accompanies this distribution, is reproduced below, and is
12   * available at 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
17   * without modification, are permitted provided that the following
18   * conditions are met:
19   *
20   * - Redistributions of source code must retain the above copyright
21   *   notice, this list of conditions and the following disclaimer.
22   *
23   * - Redistributions in binary form must reproduce the above
24   *   copyright notice, this list of conditions and the following
25   *   disclaimer in the documentation and/or other materials provided
26   *   with the distribution.
27   *
28   * - Neither the name of the Eclipse Foundation, Inc. nor the
29   *   names of its contributors may be used to endorse or promote
30   *   products derived from this software without specific prior
31   *   written permission.
32   *
33   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
34   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
35   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
38   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46   */
47  
48  package org.eclipse.jgit.lib;
49  
50  import java.io.File;
51  import java.io.IOException;
52  import java.nio.file.DirectoryIteratorException;
53  import java.nio.file.DirectoryStream;
54  import java.nio.file.Files;
55  import java.text.MessageFormat;
56  import java.util.ArrayList;
57  import java.util.Collection;
58  import java.util.Collections;
59  import java.util.HashMap;
60  import java.util.HashSet;
61  import java.util.Map;
62  import java.util.Set;
63  
64  import org.eclipse.jgit.dircache.DirCache;
65  import org.eclipse.jgit.dircache.DirCacheEntry;
66  import org.eclipse.jgit.dircache.DirCacheIterator;
67  import org.eclipse.jgit.errors.ConfigInvalidException;
68  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
69  import org.eclipse.jgit.errors.MissingObjectException;
70  import org.eclipse.jgit.errors.StopWalkException;
71  import org.eclipse.jgit.internal.JGitText;
72  import org.eclipse.jgit.revwalk.RevWalk;
73  import org.eclipse.jgit.submodule.SubmoduleWalk;
74  import org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode;
75  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
76  import org.eclipse.jgit.treewalk.EmptyTreeIterator;
77  import org.eclipse.jgit.treewalk.FileTreeIterator;
78  import org.eclipse.jgit.treewalk.TreeWalk;
79  import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
80  import org.eclipse.jgit.treewalk.WorkingTreeIterator;
81  import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
82  import org.eclipse.jgit.treewalk.filter.IndexDiffFilter;
83  import org.eclipse.jgit.treewalk.filter.SkipWorkTreeFilter;
84  import org.eclipse.jgit.treewalk.filter.TreeFilter;
85  
86  /**
87   * Compares the index, a tree, and the working directory Ignored files are not
88   * taken into account. The following information is retrieved:
89   * <ul>
90   * <li>added files</li>
91   * <li>changed files</li>
92   * <li>removed files</li>
93   * <li>missing files</li>
94   * <li>modified files</li>
95   * <li>conflicting files</li>
96   * <li>untracked files</li>
97   * <li>files with assume-unchanged flag</li>
98   * </ul>
99   */
100 public class IndexDiff {
101 
102 	/**
103 	 * Represents the state of the index for a certain path regarding the stages
104 	 * - which stages exist for a path and which not (base, ours, theirs).
105 	 * <p>
106 	 * This is used for figuring out what kind of conflict occurred.
107 	 *
108 	 * @see IndexDiff#getConflictingStageStates()
109 	 * @since 3.0
110 	 */
111 	public static enum StageState {
112 		/**
113 		 * Exists in base, but neither in ours nor in theirs.
114 		 */
115 		BOTH_DELETED(1),
116 
117 		/**
118 		 * Only exists in ours.
119 		 */
120 		ADDED_BY_US(2),
121 
122 		/**
123 		 * Exists in base and ours, but no in theirs.
124 		 */
125 		DELETED_BY_THEM(3),
126 
127 		/**
128 		 * Only exists in theirs.
129 		 */
130 		ADDED_BY_THEM(4),
131 
132 		/**
133 		 * Exists in base and theirs, but not in ours.
134 		 */
135 		DELETED_BY_US(5),
136 
137 		/**
138 		 * Exists in ours and theirs, but not in base.
139 		 */
140 		BOTH_ADDED(6),
141 
142 		/**
143 		 * Exists in all stages, content conflict.
144 		 */
145 		BOTH_MODIFIED(7);
146 
147 		private final int stageMask;
148 
149 		private StageState(int stageMask) {
150 			this.stageMask = stageMask;
151 		}
152 
153 		int getStageMask() {
154 			return stageMask;
155 		}
156 
157 		/**
158 		 * @return whether there is a "base" stage entry
159 		 */
160 		public boolean hasBase() {
161 			return (stageMask & 1) != 0;
162 		}
163 
164 		/**
165 		 * @return whether there is an "ours" stage entry
166 		 */
167 		public boolean hasOurs() {
168 			return (stageMask & 2) != 0;
169 		}
170 
171 		/**
172 		 * @return whether there is a "theirs" stage entry
173 		 */
174 		public boolean hasTheirs() {
175 			return (stageMask & 4) != 0;
176 		}
177 
178 		static StageState fromMask(int stageMask) {
179 			// bits represent: theirs, ours, base
180 			switch (stageMask) {
181 			case 1: // 0b001
182 				return BOTH_DELETED;
183 			case 2: // 0b010
184 				return ADDED_BY_US;
185 			case 3: // 0b011
186 				return DELETED_BY_THEM;
187 			case 4: // 0b100
188 				return ADDED_BY_THEM;
189 			case 5: // 0b101
190 				return DELETED_BY_US;
191 			case 6: // 0b110
192 				return BOTH_ADDED;
193 			case 7: // 0b111
194 				return BOTH_MODIFIED;
195 			default:
196 				return null;
197 			}
198 		}
199 	}
200 
201 	private static final class ProgressReportingFilter extends TreeFilter {
202 
203 		private final ProgressMonitor monitor;
204 
205 		private int count = 0;
206 
207 		private int stepSize;
208 
209 		private final int total;
210 
211 		private ProgressReportingFilter(ProgressMonitor monitor, int total) {
212 			this.monitor = monitor;
213 			this.total = total;
214 			stepSize = total / 100;
215 			if (stepSize == 0)
216 				stepSize = 1000;
217 		}
218 
219 		@Override
220 		public boolean shouldBeRecursive() {
221 			return false;
222 		}
223 
224 		@Override
225 		public boolean include(TreeWalk walker)
226 				throws MissingObjectException,
227 				IncorrectObjectTypeException, IOException {
228 			count++;
229 			if (count % stepSize == 0) {
230 				if (count <= total)
231 					monitor.update(stepSize);
232 				if (monitor.isCancelled())
233 					throw StopWalkException.INSTANCE;
234 			}
235 			return true;
236 		}
237 
238 		@Override
239 		public TreeFilter clone() {
240 			throw new IllegalStateException(
241 					"Do not clone this kind of filter: " //$NON-NLS-1$
242 							+ getClass().getName());
243 		}
244 	}
245 
246 	private final static int TREE = 0;
247 
248 	private final static int INDEX = 1;
249 
250 	private final static int WORKDIR = 2;
251 
252 	private final Repository repository;
253 
254 	private final AnyObjectId tree;
255 
256 	private TreeFilter filter = null;
257 
258 	private final WorkingTreeIterator initialWorkingTreeIterator;
259 
260 	private Set<String> added = new HashSet<>();
261 
262 	private Set<String> changed = new HashSet<>();
263 
264 	private Set<String> removed = new HashSet<>();
265 
266 	private Set<String> missing = new HashSet<>();
267 
268 	private Set<String> missingSubmodules = new HashSet<>();
269 
270 	private Set<String> modified = new HashSet<>();
271 
272 	private Set<String> untracked = new HashSet<>();
273 
274 	private Map<String, StageState> conflicts = new HashMap<>();
275 
276 	private Set<String> ignored;
277 
278 	private Set<String> assumeUnchanged;
279 
280 	private DirCache dirCache;
281 
282 	private IndexDiffFilter indexDiffFilter;
283 
284 	private Map<String, IndexDiff> submoduleIndexDiffs = new HashMap<>();
285 
286 	private IgnoreSubmoduleMode ignoreSubmoduleMode = null;
287 
288 	private Map<FileMode, Set<String>> fileModes = new HashMap<>();
289 
290 	/**
291 	 * Construct an IndexDiff
292 	 *
293 	 * @param repository
294 	 *            a {@link org.eclipse.jgit.lib.Repository} object.
295 	 * @param revstr
296 	 *            symbolic name e.g. HEAD An EmptyTreeIterator is used if
297 	 *            <code>revstr</code> cannot be resolved.
298 	 * @param workingTreeIterator
299 	 *            iterator for working directory
300 	 * @throws java.io.IOException
301 	 */
302 	public IndexDiff(Repository repository, String revstr,
303 			WorkingTreeIterator workingTreeIterator) throws IOException {
304 		this(repository, repository.resolve(revstr), workingTreeIterator);
305 	}
306 
307 	/**
308 	 * Construct an Indexdiff
309 	 *
310 	 * @param repository
311 	 *            a {@link org.eclipse.jgit.lib.Repository} object.
312 	 * @param objectId
313 	 *            tree id. If null, an EmptyTreeIterator is used.
314 	 * @param workingTreeIterator
315 	 *            iterator for working directory
316 	 * @throws java.io.IOException
317 	 */
318 	public IndexDiff(Repository repository, ObjectId objectId,
319 			WorkingTreeIterator workingTreeIterator) throws IOException {
320 		this.repository = repository;
321 		if (objectId != null) {
322 			try (RevWalkRevWalk.html#RevWalk">RevWalk rw = new RevWalk(repository)) {
323 				tree = rw.parseTree(objectId);
324 			}
325 		} else {
326 			tree = null;
327 		}
328 		this.initialWorkingTreeIterator = workingTreeIterator;
329 	}
330 
331 	/**
332 	 * Defines how modifications in submodules are treated
333 	 *
334 	 * @param mode
335 	 *            defines how modifications in submodules are treated
336 	 * @since 3.6
337 	 */
338 	public void setIgnoreSubmoduleMode(IgnoreSubmoduleMode mode) {
339 		this.ignoreSubmoduleMode = mode;
340 	}
341 
342 	/**
343 	 * A factory to producing WorkingTreeIterators
344 	 * @since 3.6
345 	 */
346 	public interface WorkingTreeIteratorFactory {
347 		/**
348 		 * @param repo
349 		 *            the repository
350 		 * @return working tree iterator
351 		 */
352 		public WorkingTreeIterator getWorkingTreeIterator(Repository repo);
353 	}
354 
355 	private WorkingTreeIteratorFactory wTreeIt = FileTreeIterator::new;
356 
357 	/**
358 	 * Allows higher layers to set the factory for WorkingTreeIterators.
359 	 *
360 	 * @param wTreeIt
361 	 * @since 3.6
362 	 */
363 	public void setWorkingTreeItFactory(WorkingTreeIteratorFactory wTreeIt) {
364 		this.wTreeIt = wTreeIt;
365 	}
366 
367 	/**
368 	 * Sets a filter. Can be used e.g. for restricting the tree walk to a set of
369 	 * files.
370 	 *
371 	 * @param filter
372 	 *            a {@link org.eclipse.jgit.treewalk.filter.TreeFilter} object.
373 	 */
374 	public void setFilter(TreeFilter filter) {
375 		this.filter = filter;
376 	}
377 
378 	/**
379 	 * Run the diff operation. Until this is called, all lists will be empty.
380 	 * Use {@link #diff(ProgressMonitor, int, int, String)} if a progress
381 	 * monitor is required.
382 	 *
383 	 * @return if anything is different between index, tree, and workdir
384 	 * @throws java.io.IOException
385 	 */
386 	public boolean diff() throws IOException {
387 		return diff(null);
388 	}
389 
390 	/**
391 	 * Run the diff operation. Until this is called, all lists will be empty.
392 	 * Use
393 	 * {@link #diff(ProgressMonitor, int, int, String, RepositoryBuilderFactory)}
394 	 * if a progress monitor is required.
395 	 * <p>
396 	 * The operation may create repositories for submodules using builders
397 	 * provided by the given {@code factory}, if any, and will also close these
398 	 * submodule repositories again.
399 	 * </p>
400 	 *
401 	 * @param factory
402 	 *            the {@link RepositoryBuilderFactory} to use to create builders
403 	 *            to create submodule repositories, if needed; if {@code null},
404 	 *            submodule repositories will be built using a plain
405 	 *            {@link RepositoryBuilder}.
406 	 * @return if anything is different between index, tree, and workdir
407 	 * @throws java.io.IOException
408 	 * @since 5.6
409 	 */
410 	public boolean diff(RepositoryBuilderFactory factory)
411 			throws IOException {
412 		return diff(null, 0, 0, "", factory); //$NON-NLS-1$
413 	}
414 
415 	/**
416 	 * Run the diff operation. Until this is called, all lists will be empty.
417 	 * <p>
418 	 * The operation may be aborted by the progress monitor. In that event it
419 	 * will report what was found before the cancel operation was detected.
420 	 * Callers should ignore the result if monitor.isCancelled() is true. If a
421 	 * progress monitor is not needed, callers should use {@link #diff()}
422 	 * instead. Progress reporting is crude and approximate and only intended
423 	 * for informing the user.
424 	 *
425 	 * @param monitor
426 	 *            for reporting progress, may be null
427 	 * @param estWorkTreeSize
428 	 *            number or estimated files in the working tree
429 	 * @param estIndexSize
430 	 *            number of estimated entries in the cache
431 	 * @param title a {@link java.lang.String} object.
432 	 * @return if anything is different between index, tree, and workdir
433 	 * @throws java.io.IOException
434 	 */
435 	public boolean diff(final ProgressMonitor monitor, int estWorkTreeSize,
436 			int estIndexSize, final String title)
437 			throws IOException {
438 		return diff(monitor, estWorkTreeSize, estIndexSize, title, null);
439 	}
440 
441 	/**
442 	 * Run the diff operation. Until this is called, all lists will be empty.
443 	 * <p>
444 	 * The operation may be aborted by the progress monitor. In that event it
445 	 * will report what was found before the cancel operation was detected.
446 	 * Callers should ignore the result if monitor.isCancelled() is true. If a
447 	 * progress monitor is not needed, callers should use {@link #diff()}
448 	 * instead. Progress reporting is crude and approximate and only intended
449 	 * for informing the user.
450 	 * </p>
451 	 * <p>
452 	 * The operation may create repositories for submodules using builders
453 	 * provided by the given {@code factory}, if any, and will also close these
454 	 * submodule repositories again.
455 	 * </p>
456 	 *
457 	 * @param monitor
458 	 *            for reporting progress, may be null
459 	 * @param estWorkTreeSize
460 	 *            number or estimated files in the working tree
461 	 * @param estIndexSize
462 	 *            number of estimated entries in the cache
463 	 * @param title
464 	 *            a {@link java.lang.String} object.
465 	 * @param factory
466 	 *            the {@link RepositoryBuilderFactory} to use to create builders
467 	 *            to create submodule repositories, if needed; if {@code null},
468 	 *            submodule repositories will be built using a plain
469 	 *            {@link RepositoryBuilder}.
470 	 * @return if anything is different between index, tree, and workdir
471 	 * @throws java.io.IOException
472 	 * @since 5.6
473 	 */
474 	public boolean diff(ProgressMonitor monitor, int estWorkTreeSize,
475 			int estIndexSize, String title, RepositoryBuilderFactory factory)
476 			throws IOException {
477 		dirCache = repository.readDirCache();
478 
479 		try (TreeWalklk.html#TreeWalk">TreeWalk treeWalk = new TreeWalk(repository)) {
480 			treeWalk.setOperationType(OperationType.CHECKIN_OP);
481 			treeWalk.setRecursive(true);
482 			// add the trees (tree, dirchache, workdir)
483 			if (tree != null)
484 				treeWalk.addTree(tree);
485 			else
486 				treeWalk.addTree(new EmptyTreeIterator());
487 			treeWalk.addTree(new DirCacheIterator(dirCache));
488 			treeWalk.addTree(initialWorkingTreeIterator);
489 			initialWorkingTreeIterator.setDirCacheIterator(treeWalk, 1);
490 			Collection<TreeFilter> filters = new ArrayList<>(4);
491 
492 			if (monitor != null) {
493 				// Get the maximum size of the work tree and index
494 				// and add some (quite arbitrary)
495 				if (estIndexSize == 0)
496 					estIndexSize = dirCache.getEntryCount();
497 				int total = Math.max(estIndexSize * 10 / 9,
498 						estWorkTreeSize * 10 / 9);
499 				monitor.beginTask(title, total);
500 				filters.add(new ProgressReportingFilter(monitor, total));
501 			}
502 
503 			if (filter != null)
504 				filters.add(filter);
505 			filters.add(new SkipWorkTreeFilter(INDEX));
506 			indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR);
507 			filters.add(indexDiffFilter);
508 			treeWalk.setFilter(AndTreeFilter.create(filters));
509 			fileModes.clear();
510 			while (treeWalk.next()) {
511 				AbstractTreeIterator treeIterator = treeWalk.getTree(TREE,
512 						AbstractTreeIterator.class);
513 				DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
514 						DirCacheIterator.class);
515 				WorkingTreeIterator workingTreeIterator = treeWalk
516 						.getTree(WORKDIR, WorkingTreeIterator.class);
517 
518 				if (dirCacheIterator != null) {
519 					final DirCacheEntry dirCacheEntry = dirCacheIterator
520 							.getDirCacheEntry();
521 					if (dirCacheEntry != null) {
522 						int stage = dirCacheEntry.getStage();
523 						if (stage > 0) {
524 							String path = treeWalk.getPathString();
525 							addConflict(path, stage);
526 							continue;
527 						}
528 					}
529 				}
530 
531 				if (treeIterator != null) {
532 					if (dirCacheIterator != null) {
533 						if (!treeIterator.idEqual(dirCacheIterator)
534 								|| treeIterator
535 										.getEntryRawMode() != dirCacheIterator
536 												.getEntryRawMode()) {
537 							// in repo, in index, content diff => changed
538 							if (!isEntryGitLink(treeIterator)
539 									|| !isEntryGitLink(dirCacheIterator)
540 									|| ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
541 								changed.add(treeWalk.getPathString());
542 						}
543 					} else {
544 						// in repo, not in index => removed
545 						if (!isEntryGitLink(treeIterator)
546 								|| ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
547 							removed.add(treeWalk.getPathString());
548 						if (workingTreeIterator != null)
549 							untracked.add(treeWalk.getPathString());
550 					}
551 				} else {
552 					if (dirCacheIterator != null) {
553 						// not in repo, in index => added
554 						if (!isEntryGitLink(dirCacheIterator)
555 								|| ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
556 							added.add(treeWalk.getPathString());
557 					} else {
558 						// not in repo, not in index => untracked
559 						if (workingTreeIterator != null
560 								&& !workingTreeIterator.isEntryIgnored()) {
561 							untracked.add(treeWalk.getPathString());
562 						}
563 					}
564 				}
565 
566 				if (dirCacheIterator != null) {
567 					if (workingTreeIterator == null) {
568 						// in index, not in workdir => missing
569 						boolean isGitLink = isEntryGitLink(dirCacheIterator);
570 						if (!isGitLink
571 								|| ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL) {
572 							String path = treeWalk.getPathString();
573 							missing.add(path);
574 							if (isGitLink) {
575 								missingSubmodules.add(path);
576 							}
577 						}
578 					} else {
579 						if (workingTreeIterator.isModified(
580 								dirCacheIterator.getDirCacheEntry(), true,
581 								treeWalk.getObjectReader())) {
582 							// in index, in workdir, content differs => modified
583 							if (!isEntryGitLink(dirCacheIterator)
584 									|| !isEntryGitLink(workingTreeIterator)
585 									|| (ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL
586 											&& ignoreSubmoduleMode != IgnoreSubmoduleMode.DIRTY))
587 								modified.add(treeWalk.getPathString());
588 						}
589 					}
590 				}
591 
592 				String path = treeWalk.getPathString();
593 				if (path != null) {
594 					for (int i = 0; i < treeWalk.getTreeCount(); i++) {
595 						recordFileMode(path, treeWalk.getFileMode(i));
596 					}
597 				}
598 			}
599 		}
600 
601 		if (ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL) {
602 			try (SubmoduleWalkduleWalk.html#SubmoduleWalk">SubmoduleWalk smw = new SubmoduleWalk(repository)) {
603 				smw.setTree(new DirCacheIterator(dirCache));
604 				smw.setBuilderFactory(factory);
605 				while (smw.next()) {
606 					IgnoreSubmoduleMode localIgnoreSubmoduleMode = ignoreSubmoduleMode;
607 					try {
608 						if (localIgnoreSubmoduleMode == null)
609 							localIgnoreSubmoduleMode = smw.getModulesIgnore();
610 						if (IgnoreSubmoduleMode.ALL
611 								.equals(localIgnoreSubmoduleMode))
612 							continue;
613 					} catch (ConfigInvalidException e) {
614 						throw new IOException(MessageFormat.format(
615 								JGitText.get().invalidIgnoreParamSubmodule,
616 								smw.getPath()), e);
617 					}
618 					try (Repository subRepo = smw.getRepository()) {
619 						String subRepoPath = smw.getPath();
620 						if (subRepo != null) {
621 							ObjectId subHead = subRepo.resolve("HEAD"); //$NON-NLS-1$
622 							if (subHead != null
623 									&& !subHead.equals(smw.getObjectId())) {
624 								modified.add(subRepoPath);
625 								recordFileMode(subRepoPath, FileMode.GITLINK);
626 							} else if (localIgnoreSubmoduleMode != IgnoreSubmoduleMode.DIRTY) {
627 								IndexDiff smid = submoduleIndexDiffs
628 										.get(smw.getPath());
629 								if (smid == null) {
630 									smid = new IndexDiff(subRepo,
631 											smw.getObjectId(),
632 											wTreeIt.getWorkingTreeIterator(
633 													subRepo));
634 									submoduleIndexDiffs.put(subRepoPath, smid);
635 								}
636 								if (smid.diff(factory)) {
637 									if (localIgnoreSubmoduleMode == IgnoreSubmoduleMode.UNTRACKED
638 											&& smid.getAdded().isEmpty()
639 											&& smid.getChanged().isEmpty()
640 											&& smid.getConflicting().isEmpty()
641 											&& smid.getMissing().isEmpty()
642 											&& smid.getModified().isEmpty()
643 											&& smid.getRemoved().isEmpty()) {
644 										continue;
645 									}
646 									modified.add(subRepoPath);
647 									recordFileMode(subRepoPath,
648 											FileMode.GITLINK);
649 								}
650 							}
651 						} else if (missingSubmodules.remove(subRepoPath)) {
652 							// If the directory is there and empty but the
653 							// submodule repository in .git/modules doesn't
654 							// exist yet it isn't "missing".
655 							File gitDir = new File(
656 									new File(repository.getDirectory(),
657 											Constants.MODULES),
658 									subRepoPath);
659 							if (!gitDir.isDirectory()) {
660 								File dir = SubmoduleWalk.getSubmoduleDirectory(
661 										repository, subRepoPath);
662 								if (dir.isDirectory() && !hasFiles(dir)) {
663 									missing.remove(subRepoPath);
664 								}
665 							}
666 						}
667 					}
668 				}
669 			}
670 
671 		}
672 
673 		// consume the remaining work
674 		if (monitor != null) {
675 			monitor.endTask();
676 		}
677 
678 		ignored = indexDiffFilter.getIgnoredPaths();
679 		if (added.isEmpty() && changed.isEmpty() && removed.isEmpty()
680 				&& missing.isEmpty() && modified.isEmpty()
681 				&& untracked.isEmpty()) {
682 			return false;
683 		}
684 		return true;
685 	}
686 
687 	private boolean hasFiles(File directory) {
688 		try (DirectoryStream<java.nio.file.Path> dir = Files
689 				.newDirectoryStream(directory.toPath())) {
690 			return dir.iterator().hasNext();
691 		} catch (DirectoryIteratorException | IOException e) {
692 			return false;
693 		}
694 	}
695 
696 	private void recordFileMode(String path, FileMode mode) {
697 		Set<String> values = fileModes.get(mode);
698 		if (path != null) {
699 			if (values == null) {
700 				values = new HashSet<>();
701 				fileModes.put(mode, values);
702 			}
703 			values.add(path);
704 		}
705 	}
706 
707 	private boolean isEntryGitLink(AbstractTreeIterator ti) {
708 		return ((ti != null) && (ti.getEntryRawMode() == FileMode.GITLINK
709 				.getBits()));
710 	}
711 
712 	private void addConflict(String path, int stage) {
713 		StageState existingStageStates = conflicts.get(path);
714 		byte stageMask = 0;
715 		if (existingStageStates != null) {
716 			stageMask |= (byte) existingStageStates.getStageMask();
717 		}
718 		// stage 1 (base) should be shifted 0 times
719 		int shifts = stage - 1;
720 		stageMask |= (byte) (1 << shifts);
721 		StageState stageState = StageState.fromMask(stageMask);
722 		conflicts.put(path, stageState);
723 	}
724 
725 	/**
726 	 * Get list of files added to the index, not in the tree
727 	 *
728 	 * @return list of files added to the index, not in the tree
729 	 */
730 	public Set<String> getAdded() {
731 		return added;
732 	}
733 
734 	/**
735 	 * Get list of files changed from tree to index
736 	 *
737 	 * @return list of files changed from tree to index
738 	 */
739 	public Set<String> getChanged() {
740 		return changed;
741 	}
742 
743 	/**
744 	 * Get list of files removed from index, but in tree
745 	 *
746 	 * @return list of files removed from index, but in tree
747 	 */
748 	public Set<String> getRemoved() {
749 		return removed;
750 	}
751 
752 	/**
753 	 * Get list of files in index, but not filesystem
754 	 *
755 	 * @return list of files in index, but not filesystem
756 	 */
757 	public Set<String> getMissing() {
758 		return missing;
759 	}
760 
761 	/**
762 	 * Get list of files modified on disk relative to the index
763 	 *
764 	 * @return list of files modified on disk relative to the index
765 	 */
766 	public Set<String> getModified() {
767 		return modified;
768 	}
769 
770 	/**
771 	 * Get list of files that are not ignored, and not in the index.
772 	 *
773 	 * @return list of files that are not ignored, and not in the index.
774 	 */
775 	public Set<String> getUntracked() {
776 		return untracked;
777 	}
778 
779 	/**
780 	 * Get list of files that are in conflict, corresponds to the keys of
781 	 * {@link #getConflictingStageStates()}
782 	 *
783 	 * @return list of files that are in conflict, corresponds to the keys of
784 	 *         {@link #getConflictingStageStates()}
785 	 */
786 	public Set<String> getConflicting() {
787 		return conflicts.keySet();
788 	}
789 
790 	/**
791 	 * Get the map from each path of {@link #getConflicting()} to its
792 	 * corresponding {@link org.eclipse.jgit.lib.IndexDiff.StageState}
793 	 *
794 	 * @return the map from each path of {@link #getConflicting()} to its
795 	 *         corresponding {@link org.eclipse.jgit.lib.IndexDiff.StageState}
796 	 * @since 3.0
797 	 */
798 	public Map<String, StageState> getConflictingStageStates() {
799 		return conflicts;
800 	}
801 
802 	/**
803 	 * The method returns the list of ignored files and folders. Only the root
804 	 * folder of an ignored folder hierarchy is reported. If a/b/c is listed in
805 	 * the .gitignore then you should not expect a/b/c/d/e/f to be reported
806 	 * here. Only a/b/c will be reported. Furthermore only ignored files /
807 	 * folders are returned that are NOT in the index.
808 	 *
809 	 * @return list of files / folders that are ignored
810 	 */
811 	public Set<String> getIgnoredNotInIndex() {
812 		return ignored;
813 	}
814 
815 	/**
816 	 * Get list of files with the flag assume-unchanged
817 	 *
818 	 * @return list of files with the flag assume-unchanged
819 	 */
820 	public Set<String> getAssumeUnchanged() {
821 		if (assumeUnchanged == null) {
822 			HashSet<String> unchanged = new HashSet<>();
823 			for (int i = 0; i < dirCache.getEntryCount(); i++)
824 				if (dirCache.getEntry(i).isAssumeValid())
825 					unchanged.add(dirCache.getEntry(i).getPathString());
826 			assumeUnchanged = unchanged;
827 		}
828 		return assumeUnchanged;
829 	}
830 
831 	/**
832 	 * Get list of folders containing only untracked files/folders
833 	 *
834 	 * @return list of folders containing only untracked files/folders
835 	 */
836 	public Set<String> getUntrackedFolders() {
837 		return ((indexDiffFilter == null) ? Collections.<String> emptySet()
838 				: new HashSet<>(indexDiffFilter.getUntrackedFolders()));
839 	}
840 
841 	/**
842 	 * Get the file mode of the given path in the index
843 	 *
844 	 * @param path a {@link java.lang.String} object.
845 	 * @return file mode
846 	 */
847 	public FileMode getIndexMode(String path) {
848 		final DirCacheEntry entry = dirCache.getEntry(path);
849 		return entry != null ? entry.getFileMode() : FileMode.MISSING;
850 	}
851 
852 	/**
853 	 * Get the list of paths that IndexDiff has detected to differ and have the
854 	 * given file mode
855 	 *
856 	 * @param mode a {@link org.eclipse.jgit.lib.FileMode} object.
857 	 * @return the list of paths that IndexDiff has detected to differ and have
858 	 *         the given file mode
859 	 * @since 3.6
860 	 */
861 	public Set<String> getPathsWithIndexMode(FileMode mode) {
862 		Set<String> paths = fileModes.get(mode);
863 		if (paths == null)
864 			paths = new HashSet<>();
865 		return paths;
866 	}
867 }