You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IndexDiff.java 24KB

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