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 22KB

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