Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IndexDiff.java 26KB

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