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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.HashSet;
  50. import java.util.Set;
  51. import org.eclipse.jgit.dircache.DirCache;
  52. import org.eclipse.jgit.dircache.DirCacheEntry;
  53. import org.eclipse.jgit.dircache.DirCacheIterator;
  54. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  55. import org.eclipse.jgit.errors.MissingObjectException;
  56. import org.eclipse.jgit.errors.StopWalkException;
  57. import org.eclipse.jgit.revwalk.RevTree;
  58. import org.eclipse.jgit.revwalk.RevWalk;
  59. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  60. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  61. import org.eclipse.jgit.treewalk.TreeWalk;
  62. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  63. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  64. import org.eclipse.jgit.treewalk.filter.IndexDiffFilter;
  65. import org.eclipse.jgit.treewalk.filter.SkipWorkTreeFilter;
  66. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  67. /**
  68. * Compares the index, a tree, and the working directory Ignored files are not
  69. * taken into account. The following information is retrieved:
  70. * <ul>
  71. * <li>added files</li>
  72. * <li>changed files</li>
  73. * <li>removed files</li>
  74. * <li>missing files</li>
  75. * <li>modified files</li>
  76. * <li>conflicting files</li>
  77. * <li>untracked files</li>
  78. * <li>files with assume-unchanged flag</li>
  79. * </ul>
  80. */
  81. public class IndexDiff {
  82. private static final class ProgressReportingFilter extends TreeFilter {
  83. private final ProgressMonitor monitor;
  84. private int count = 0;
  85. private int stepSize;
  86. private final int total;
  87. private ProgressReportingFilter(ProgressMonitor monitor, int total) {
  88. this.monitor = monitor;
  89. this.total = total;
  90. stepSize = total / 100;
  91. if (stepSize == 0)
  92. stepSize = 1000;
  93. }
  94. @Override
  95. public boolean shouldBeRecursive() {
  96. return false;
  97. }
  98. @Override
  99. public boolean include(TreeWalk walker)
  100. throws MissingObjectException,
  101. IncorrectObjectTypeException, IOException {
  102. count++;
  103. if (count % stepSize == 0) {
  104. if (count <= total)
  105. monitor.update(stepSize);
  106. if (monitor.isCancelled())
  107. throw StopWalkException.INSTANCE;
  108. }
  109. return true;
  110. }
  111. @Override
  112. public TreeFilter clone() {
  113. throw new IllegalStateException(
  114. "Do not clone this kind of filter: "
  115. + getClass().getName());
  116. }
  117. }
  118. private final static int TREE = 0;
  119. private final static int INDEX = 1;
  120. private final static int WORKDIR = 2;
  121. private final Repository repository;
  122. private final RevTree tree;
  123. private TreeFilter filter = null;
  124. private final WorkingTreeIterator initialWorkingTreeIterator;
  125. private Set<String> added = new HashSet<String>();
  126. private Set<String> changed = new HashSet<String>();
  127. private Set<String> removed = new HashSet<String>();
  128. private Set<String> missing = new HashSet<String>();
  129. private Set<String> modified = new HashSet<String>();
  130. private Set<String> untracked = new HashSet<String>();
  131. private Set<String> conflicts = new HashSet<String>();
  132. private Set<String> assumeUnchanged;
  133. private DirCache dirCache;
  134. /**
  135. * Construct an IndexDiff
  136. *
  137. * @param repository
  138. * @param revstr
  139. * symbolic name e.g. HEAD
  140. * An EmptyTreeIterator is used if <code>revstr</code> cannot be resolved.
  141. * @param workingTreeIterator
  142. * iterator for working directory
  143. * @throws IOException
  144. */
  145. public IndexDiff(Repository repository, String revstr,
  146. WorkingTreeIterator workingTreeIterator) throws IOException {
  147. this.repository = repository;
  148. ObjectId objectId = repository.resolve(revstr);
  149. if (objectId != null)
  150. tree = new RevWalk(repository).parseTree(objectId);
  151. else
  152. tree = null;
  153. this.initialWorkingTreeIterator = workingTreeIterator;
  154. }
  155. /**
  156. * Construct an Indexdiff
  157. *
  158. * @param repository
  159. * @param objectId
  160. * tree id. If null, an EmptyTreeIterator is used.
  161. * @param workingTreeIterator
  162. * iterator for working directory
  163. * @throws IOException
  164. */
  165. public IndexDiff(Repository repository, ObjectId objectId,
  166. WorkingTreeIterator workingTreeIterator) throws IOException {
  167. this.repository = repository;
  168. if (objectId != null)
  169. tree = new RevWalk(repository).parseTree(objectId);
  170. else
  171. tree = null;
  172. this.initialWorkingTreeIterator = workingTreeIterator;
  173. }
  174. /**
  175. * Sets a filter. Can be used e.g. for restricting the tree walk to a set of
  176. * files.
  177. *
  178. * @param filter
  179. */
  180. public void setFilter(TreeFilter filter) {
  181. this.filter = filter;
  182. }
  183. /**
  184. * Run the diff operation. Until this is called, all lists will be empty.
  185. * Use {@link #diff(ProgressMonitor, int, int, String)} if a progress
  186. * monitor is required.
  187. *
  188. * @return if anything is different between index, tree, and workdir
  189. * @throws IOException
  190. */
  191. public boolean diff() throws IOException {
  192. return diff(null, 0, 0, "");
  193. }
  194. /**
  195. * Run the diff operation. Until this is called, all lists will be empty.
  196. * <p>
  197. * The operation may be aborted by the progress monitor. In that event it
  198. * will report what was found before the cancel operation was detected.
  199. * Callers should ignore the result if monitor.isCancelled() is true. If a
  200. * progress monitor is not needed, callers should use {@link #diff()}
  201. * instead. Progress reporting is crude and approximate and only intended
  202. * for informing the user.
  203. *
  204. * @param monitor
  205. * for reporting progress, may be null
  206. * @param estWorkTreeSize
  207. * number or estimated files in the working tree
  208. * @param estIndexSize
  209. * number of estimated entries in the cache
  210. * @param title
  211. *
  212. * @return if anything is different between index, tree, and workdir
  213. * @throws IOException
  214. */
  215. public boolean diff(final ProgressMonitor monitor, int estWorkTreeSize,
  216. int estIndexSize, final String title)
  217. throws IOException {
  218. dirCache = repository.readDirCache();
  219. TreeWalk treeWalk = new TreeWalk(repository);
  220. treeWalk.setRecursive(true);
  221. // add the trees (tree, dirchache, workdir)
  222. if (tree != null)
  223. treeWalk.addTree(tree);
  224. else
  225. treeWalk.addTree(new EmptyTreeIterator());
  226. treeWalk.addTree(new DirCacheIterator(dirCache));
  227. treeWalk.addTree(initialWorkingTreeIterator);
  228. Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);
  229. if (monitor != null) {
  230. // Get the maximum size of the work tree and index
  231. // and add some (quite arbitrary)
  232. if (estIndexSize == 0)
  233. estIndexSize = dirCache.getEntryCount();
  234. int total = Math.max(estIndexSize * 10 / 9,
  235. estWorkTreeSize * 10 / 9);
  236. monitor.beginTask(title, total);
  237. filters.add(new ProgressReportingFilter(monitor, total));
  238. }
  239. if (filter != null)
  240. filters.add(filter);
  241. filters.add(new SkipWorkTreeFilter(INDEX));
  242. filters.add(new IndexDiffFilter(INDEX, WORKDIR));
  243. treeWalk.setFilter(AndTreeFilter.create(filters));
  244. while (treeWalk.next()) {
  245. AbstractTreeIterator treeIterator = treeWalk.getTree(TREE,
  246. AbstractTreeIterator.class);
  247. DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
  248. DirCacheIterator.class);
  249. WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR,
  250. WorkingTreeIterator.class);
  251. if (treeIterator != null) {
  252. if (dirCacheIterator != null) {
  253. if (!treeIterator.idEqual(dirCacheIterator)
  254. || treeIterator.getEntryRawMode()
  255. != dirCacheIterator.getEntryRawMode()) {
  256. // in repo, in index, content diff => changed
  257. changed.add(treeWalk.getPathString());
  258. }
  259. } else {
  260. // in repo, not in index => removed
  261. removed.add(treeWalk.getPathString());
  262. if (workingTreeIterator != null)
  263. untracked.add(treeWalk.getPathString());
  264. }
  265. } else {
  266. if (dirCacheIterator != null) {
  267. // not in repo, in index => added
  268. added.add(treeWalk.getPathString());
  269. } else {
  270. // not in repo, not in index => untracked
  271. if (workingTreeIterator != null
  272. && !workingTreeIterator.isEntryIgnored()) {
  273. untracked.add(treeWalk.getPathString());
  274. }
  275. }
  276. }
  277. if (dirCacheIterator != null) {
  278. if (workingTreeIterator == null) {
  279. // in index, not in workdir => missing
  280. missing.add(treeWalk.getPathString());
  281. } else {
  282. if (workingTreeIterator.isModified(
  283. dirCacheIterator.getDirCacheEntry(), true)) {
  284. // in index, in workdir, content differs => modified
  285. modified.add(treeWalk.getPathString());
  286. }
  287. }
  288. final DirCacheEntry dirCacheEntry = dirCacheIterator
  289. .getDirCacheEntry();
  290. if (dirCacheEntry != null && dirCacheEntry.getStage() > 0) {
  291. conflicts.add(treeWalk.getPathString());
  292. }
  293. }
  294. }
  295. // consume the remaining work
  296. if (monitor != null)
  297. monitor.endTask();
  298. if (added.isEmpty() && changed.isEmpty() && removed.isEmpty()
  299. && missing.isEmpty() && modified.isEmpty()
  300. && untracked.isEmpty())
  301. return false;
  302. else
  303. return true;
  304. }
  305. /**
  306. * @return list of files added to the index, not in the tree
  307. */
  308. public Set<String> getAdded() {
  309. return added;
  310. }
  311. /**
  312. * @return list of files changed from tree to index
  313. */
  314. public Set<String> getChanged() {
  315. return changed;
  316. }
  317. /**
  318. * @return list of files removed from index, but in tree
  319. */
  320. public Set<String> getRemoved() {
  321. return removed;
  322. }
  323. /**
  324. * @return list of files in index, but not filesystem
  325. */
  326. public Set<String> getMissing() {
  327. return missing;
  328. }
  329. /**
  330. * @return list of files on modified on disk relative to the index
  331. */
  332. public Set<String> getModified() {
  333. return modified;
  334. }
  335. /**
  336. * @return list of files that are not ignored, and not in the index.
  337. */
  338. public Set<String> getUntracked() {
  339. return untracked;
  340. }
  341. /**
  342. * @return list of files that are in conflict
  343. */
  344. public Set<String> getConflicting() {
  345. return conflicts;
  346. }
  347. /**
  348. * @return list of files with the flag assume-unchanged
  349. */
  350. public Set<String> getAssumeUnchanged() {
  351. if (assumeUnchanged == null) {
  352. HashSet<String> unchanged = new HashSet<String>();
  353. for (int i = 0; i < dirCache.getEntryCount(); i++)
  354. if (dirCache.getEntry(i).isAssumeValid())
  355. unchanged.add(dirCache.getEntry(i).getPathString());
  356. assumeUnchanged = unchanged;
  357. }
  358. return assumeUnchanged;
  359. }
  360. }