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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import java.util.Collection;
  49. import java.util.Collections;
  50. import java.util.HashSet;
  51. import java.util.Set;
  52. import org.eclipse.jgit.dircache.DirCache;
  53. import org.eclipse.jgit.dircache.DirCacheEntry;
  54. import org.eclipse.jgit.dircache.DirCacheIterator;
  55. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  56. import org.eclipse.jgit.errors.MissingObjectException;
  57. import org.eclipse.jgit.errors.StopWalkException;
  58. import org.eclipse.jgit.revwalk.RevTree;
  59. import org.eclipse.jgit.revwalk.RevWalk;
  60. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  61. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  62. import org.eclipse.jgit.treewalk.TreeWalk;
  63. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  64. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  65. import org.eclipse.jgit.treewalk.filter.IndexDiffFilter;
  66. import org.eclipse.jgit.treewalk.filter.SkipWorkTreeFilter;
  67. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  68. /**
  69. * Compares the index, a tree, and the working directory Ignored files are not
  70. * taken into account. The following information is retrieved:
  71. * <ul>
  72. * <li>added files</li>
  73. * <li>changed files</li>
  74. * <li>removed files</li>
  75. * <li>missing files</li>
  76. * <li>modified files</li>
  77. * <li>conflicting files</li>
  78. * <li>untracked files</li>
  79. * <li>files with assume-unchanged flag</li>
  80. * </ul>
  81. */
  82. public class IndexDiff {
  83. private static final class ProgressReportingFilter extends TreeFilter {
  84. private final ProgressMonitor monitor;
  85. private int count = 0;
  86. private int stepSize;
  87. private final int total;
  88. private ProgressReportingFilter(ProgressMonitor monitor, int total) {
  89. this.monitor = monitor;
  90. this.total = total;
  91. stepSize = total / 100;
  92. if (stepSize == 0)
  93. stepSize = 1000;
  94. }
  95. @Override
  96. public boolean shouldBeRecursive() {
  97. return false;
  98. }
  99. @Override
  100. public boolean include(TreeWalk walker)
  101. throws MissingObjectException,
  102. IncorrectObjectTypeException, IOException {
  103. count++;
  104. if (count % stepSize == 0) {
  105. if (count <= total)
  106. monitor.update(stepSize);
  107. if (monitor.isCancelled())
  108. throw StopWalkException.INSTANCE;
  109. }
  110. return true;
  111. }
  112. @Override
  113. public TreeFilter clone() {
  114. throw new IllegalStateException(
  115. "Do not clone this kind of filter: "
  116. + getClass().getName());
  117. }
  118. }
  119. private final static int TREE = 0;
  120. private final static int INDEX = 1;
  121. private final static int WORKDIR = 2;
  122. private final Repository repository;
  123. private final RevTree tree;
  124. private TreeFilter filter = null;
  125. private final WorkingTreeIterator initialWorkingTreeIterator;
  126. private Set<String> added = new HashSet<String>();
  127. private Set<String> changed = new HashSet<String>();
  128. private Set<String> removed = new HashSet<String>();
  129. private Set<String> missing = new HashSet<String>();
  130. private Set<String> modified = new HashSet<String>();
  131. private Set<String> untracked = new HashSet<String>();
  132. private Set<String> conflicts = new HashSet<String>();
  133. private Set<String> ignored;
  134. private Set<String> assumeUnchanged;
  135. private DirCache dirCache;
  136. private IndexDiffFilter indexDiffFilter;
  137. /**
  138. * Construct an IndexDiff
  139. *
  140. * @param repository
  141. * @param revstr
  142. * symbolic name e.g. HEAD
  143. * An EmptyTreeIterator is used if <code>revstr</code> cannot be resolved.
  144. * @param workingTreeIterator
  145. * iterator for working directory
  146. * @throws IOException
  147. */
  148. public IndexDiff(Repository repository, String revstr,
  149. WorkingTreeIterator workingTreeIterator) throws IOException {
  150. this.repository = repository;
  151. ObjectId objectId = repository.resolve(revstr);
  152. if (objectId != null)
  153. tree = new RevWalk(repository).parseTree(objectId);
  154. else
  155. tree = null;
  156. this.initialWorkingTreeIterator = workingTreeIterator;
  157. }
  158. /**
  159. * Construct an Indexdiff
  160. *
  161. * @param repository
  162. * @param objectId
  163. * tree id. If null, an EmptyTreeIterator is used.
  164. * @param workingTreeIterator
  165. * iterator for working directory
  166. * @throws IOException
  167. */
  168. public IndexDiff(Repository repository, ObjectId objectId,
  169. WorkingTreeIterator workingTreeIterator) throws IOException {
  170. this.repository = repository;
  171. if (objectId != null)
  172. tree = new RevWalk(repository).parseTree(objectId);
  173. else
  174. tree = null;
  175. this.initialWorkingTreeIterator = workingTreeIterator;
  176. }
  177. /**
  178. * Sets a filter. Can be used e.g. for restricting the tree walk to a set of
  179. * files.
  180. *
  181. * @param filter
  182. */
  183. public void setFilter(TreeFilter filter) {
  184. this.filter = filter;
  185. }
  186. /**
  187. * Run the diff operation. Until this is called, all lists will be empty.
  188. * Use {@link #diff(ProgressMonitor, int, int, String)} if a progress
  189. * monitor is required.
  190. *
  191. * @return if anything is different between index, tree, and workdir
  192. * @throws IOException
  193. */
  194. public boolean diff() throws IOException {
  195. return diff(null, 0, 0, "");
  196. }
  197. /**
  198. * Run the diff operation. Until this is called, all lists will be empty.
  199. * <p>
  200. * The operation may be aborted by the progress monitor. In that event it
  201. * will report what was found before the cancel operation was detected.
  202. * Callers should ignore the result if monitor.isCancelled() is true. If a
  203. * progress monitor is not needed, callers should use {@link #diff()}
  204. * instead. Progress reporting is crude and approximate and only intended
  205. * for informing the user.
  206. *
  207. * @param monitor
  208. * for reporting progress, may be null
  209. * @param estWorkTreeSize
  210. * number or estimated files in the working tree
  211. * @param estIndexSize
  212. * number of estimated entries in the cache
  213. * @param title
  214. *
  215. * @return if anything is different between index, tree, and workdir
  216. * @throws IOException
  217. */
  218. public boolean diff(final ProgressMonitor monitor, int estWorkTreeSize,
  219. int estIndexSize, final String title)
  220. throws IOException {
  221. dirCache = repository.readDirCache();
  222. TreeWalk treeWalk = new TreeWalk(repository);
  223. treeWalk.setRecursive(true);
  224. // add the trees (tree, dirchache, workdir)
  225. if (tree != null)
  226. treeWalk.addTree(tree);
  227. else
  228. treeWalk.addTree(new EmptyTreeIterator());
  229. treeWalk.addTree(new DirCacheIterator(dirCache));
  230. treeWalk.addTree(initialWorkingTreeIterator);
  231. Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);
  232. if (monitor != null) {
  233. // Get the maximum size of the work tree and index
  234. // and add some (quite arbitrary)
  235. if (estIndexSize == 0)
  236. estIndexSize = dirCache.getEntryCount();
  237. int total = Math.max(estIndexSize * 10 / 9,
  238. estWorkTreeSize * 10 / 9);
  239. monitor.beginTask(title, total);
  240. filters.add(new ProgressReportingFilter(monitor, total));
  241. }
  242. if (filter != null)
  243. filters.add(filter);
  244. filters.add(new SkipWorkTreeFilter(INDEX));
  245. indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR);
  246. filters.add(indexDiffFilter);
  247. treeWalk.setFilter(AndTreeFilter.create(filters));
  248. while (treeWalk.next()) {
  249. AbstractTreeIterator treeIterator = treeWalk.getTree(TREE,
  250. AbstractTreeIterator.class);
  251. DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
  252. DirCacheIterator.class);
  253. WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR,
  254. WorkingTreeIterator.class);
  255. if (dirCacheIterator != null) {
  256. final DirCacheEntry dirCacheEntry = dirCacheIterator
  257. .getDirCacheEntry();
  258. if (dirCacheEntry != null && dirCacheEntry.getStage() > 0) {
  259. conflicts.add(treeWalk.getPathString());
  260. continue;
  261. }
  262. }
  263. if (treeIterator != null) {
  264. if (dirCacheIterator != null) {
  265. if (!treeIterator.idEqual(dirCacheIterator)
  266. || treeIterator.getEntryRawMode()
  267. != dirCacheIterator.getEntryRawMode()) {
  268. // in repo, in index, content diff => changed
  269. changed.add(treeWalk.getPathString());
  270. }
  271. } else {
  272. // in repo, not in index => removed
  273. removed.add(treeWalk.getPathString());
  274. if (workingTreeIterator != null)
  275. untracked.add(treeWalk.getPathString());
  276. }
  277. } else {
  278. if (dirCacheIterator != null) {
  279. // not in repo, in index => added
  280. added.add(treeWalk.getPathString());
  281. } else {
  282. // not in repo, not in index => untracked
  283. if (workingTreeIterator != null
  284. && !workingTreeIterator.isEntryIgnored()) {
  285. untracked.add(treeWalk.getPathString());
  286. }
  287. }
  288. }
  289. if (dirCacheIterator != null) {
  290. if (workingTreeIterator == null) {
  291. // in index, not in workdir => missing
  292. missing.add(treeWalk.getPathString());
  293. } else {
  294. if (workingTreeIterator.isModified(
  295. dirCacheIterator.getDirCacheEntry(), true)) {
  296. // in index, in workdir, content differs => modified
  297. modified.add(treeWalk.getPathString());
  298. }
  299. }
  300. }
  301. }
  302. // consume the remaining work
  303. if (monitor != null)
  304. monitor.endTask();
  305. ignored = indexDiffFilter.getIgnoredPaths();
  306. if (added.isEmpty() && changed.isEmpty() && removed.isEmpty()
  307. && missing.isEmpty() && modified.isEmpty()
  308. && untracked.isEmpty())
  309. return false;
  310. else
  311. return true;
  312. }
  313. /**
  314. * @return list of files added to the index, not in the tree
  315. */
  316. public Set<String> getAdded() {
  317. return added;
  318. }
  319. /**
  320. * @return list of files changed from tree to index
  321. */
  322. public Set<String> getChanged() {
  323. return changed;
  324. }
  325. /**
  326. * @return list of files removed from index, but in tree
  327. */
  328. public Set<String> getRemoved() {
  329. return removed;
  330. }
  331. /**
  332. * @return list of files in index, but not filesystem
  333. */
  334. public Set<String> getMissing() {
  335. return missing;
  336. }
  337. /**
  338. * @return list of files modified on disk relative to the index
  339. */
  340. public Set<String> getModified() {
  341. return modified;
  342. }
  343. /**
  344. * @return list of files that are not ignored, and not in the index.
  345. */
  346. public Set<String> getUntracked() {
  347. return untracked;
  348. }
  349. /**
  350. * @return list of files that are in conflict
  351. */
  352. public Set<String> getConflicting() {
  353. return conflicts;
  354. }
  355. /**
  356. * The method returns the list of ignored files and folders. Only the root
  357. * folder of an ignored folder hierarchy is reported. If a/b/c is listed in
  358. * the .gitignore then you should not expect a/b/c/d/e/f to be reported
  359. * here. Only a/b/c will be reported. Furthermore only ignored files /
  360. * folders are returned that are NOT in the index.
  361. *
  362. * @return list of files / folders that are ignored
  363. */
  364. public Set<String> getIgnoredNotInIndex() {
  365. return ignored;
  366. }
  367. /**
  368. * @return list of files with the flag assume-unchanged
  369. */
  370. public Set<String> getAssumeUnchanged() {
  371. if (assumeUnchanged == null) {
  372. HashSet<String> unchanged = new HashSet<String>();
  373. for (int i = 0; i < dirCache.getEntryCount(); i++)
  374. if (dirCache.getEntry(i).isAssumeValid())
  375. unchanged.add(dirCache.getEntry(i).getPathString());
  376. assumeUnchanged = unchanged;
  377. }
  378. return assumeUnchanged;
  379. }
  380. /**
  381. * @return list of folders containing only untracked files/folders
  382. */
  383. public Set<String> getUntrackedFolders() {
  384. return ((indexDiffFilter == null) ? Collections.<String> emptySet()
  385. : new HashSet<String>(indexDiffFilter.getUntrackedFolders()));
  386. }
  387. /**
  388. * Get the file mode of the given path in the index
  389. *
  390. * @param path
  391. * @return file mode
  392. */
  393. public FileMode getIndexMode(final String path) {
  394. final DirCacheEntry entry = dirCache.getEntry(path);
  395. return entry != null ? entry.getFileMode() : FileMode.MISSING;
  396. }
  397. }