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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.DirCacheIterator;
  53. import org.eclipse.jgit.revwalk.RevTree;
  54. import org.eclipse.jgit.revwalk.RevWalk;
  55. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  56. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  57. import org.eclipse.jgit.treewalk.TreeWalk;
  58. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  59. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  60. import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter;
  61. import org.eclipse.jgit.treewalk.filter.SkipWorkTreeFilter;
  62. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  63. /**
  64. * Compares the index, a tree, and the working directory Ignored files are not
  65. * taken into account. The following information is retrieved:
  66. * <ul>
  67. * <li>added files</li>
  68. * <li>changed files</li>
  69. * <li>removed files</li>
  70. * <li>missing files</li>
  71. * <li>modified files</li>
  72. * <li>untracked files</li>
  73. * <li>files with assume-unchanged flag</li>
  74. * </ul>
  75. */
  76. public class IndexDiff {
  77. private final static int TREE = 0;
  78. private final static int INDEX = 1;
  79. private final static int WORKDIR = 2;
  80. private final Repository repository;
  81. private final RevTree tree;
  82. private TreeFilter filter = null;
  83. private final WorkingTreeIterator initialWorkingTreeIterator;
  84. private Set<String> added = new HashSet<String>();
  85. private Set<String> changed = new HashSet<String>();
  86. private Set<String> removed = new HashSet<String>();
  87. private Set<String> missing = new HashSet<String>();
  88. private Set<String> modified = new HashSet<String>();
  89. private Set<String> untracked = new HashSet<String>();
  90. private Set<String> assumeUnchanged = new HashSet<String>();
  91. /**
  92. * Construct an IndexDiff
  93. *
  94. * @param repository
  95. * @param revstr
  96. * symbolic name e.g. HEAD
  97. * An EmptyTreeIterator is used if <code>revstr</code> cannot be resolved.
  98. * @param workingTreeIterator
  99. * iterator for working directory
  100. * @throws IOException
  101. */
  102. public IndexDiff(Repository repository, String revstr,
  103. WorkingTreeIterator workingTreeIterator) throws IOException {
  104. this.repository = repository;
  105. ObjectId objectId = repository.resolve(revstr);
  106. if (objectId != null)
  107. tree = new RevWalk(repository).parseTree(objectId);
  108. else
  109. tree = null;
  110. this.initialWorkingTreeIterator = workingTreeIterator;
  111. }
  112. /**
  113. * Construct an Indexdiff
  114. *
  115. * @param repository
  116. * @param objectId
  117. * tree id. If null, an EmptyTreeIterator is used.
  118. * @param workingTreeIterator
  119. * iterator for working directory
  120. * @throws IOException
  121. */
  122. public IndexDiff(Repository repository, ObjectId objectId,
  123. WorkingTreeIterator workingTreeIterator) throws IOException {
  124. this.repository = repository;
  125. if (objectId != null)
  126. tree = new RevWalk(repository).parseTree(objectId);
  127. else
  128. tree = null;
  129. this.initialWorkingTreeIterator = workingTreeIterator;
  130. }
  131. /**
  132. * Sets a filter. Can be used e.g. for restricting the tree walk to a set of
  133. * files.
  134. *
  135. * @param filter
  136. */
  137. public void setFilter(TreeFilter filter) {
  138. this.filter = filter;
  139. }
  140. /**
  141. * Run the diff operation. Until this is called, all lists will be empty
  142. *
  143. * @return if anything is different between index, tree, and workdir
  144. * @throws IOException
  145. */
  146. public boolean diff() throws IOException {
  147. boolean changesExist = false;
  148. DirCache dirCache = repository.readDirCache();
  149. TreeWalk treeWalk = new TreeWalk(repository);
  150. treeWalk.setRecursive(true);
  151. // add the trees (tree, dirchache, workdir)
  152. if (tree != null)
  153. treeWalk.addTree(tree);
  154. else
  155. treeWalk.addTree(new EmptyTreeIterator());
  156. treeWalk.addTree(new DirCacheIterator(dirCache));
  157. treeWalk.addTree(initialWorkingTreeIterator);
  158. Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);
  159. if (filter != null)
  160. filters.add(filter);
  161. filters.add(new NotIgnoredFilter(WORKDIR));
  162. filters.add(new SkipWorkTreeFilter(INDEX));
  163. filters.add(TreeFilter.ANY_DIFF);
  164. treeWalk.setFilter(AndTreeFilter.create(filters));
  165. while (treeWalk.next()) {
  166. AbstractTreeIterator treeIterator = treeWalk.getTree(TREE,
  167. AbstractTreeIterator.class);
  168. DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
  169. DirCacheIterator.class);
  170. WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR,
  171. WorkingTreeIterator.class);
  172. if (dirCacheIterator != null) {
  173. if (dirCacheIterator.getDirCacheEntry().isAssumeValid())
  174. assumeUnchanged.add(treeWalk.getPathString());
  175. }
  176. if (treeIterator != null) {
  177. if (dirCacheIterator != null) {
  178. if (!treeIterator.getEntryObjectId().equals(
  179. dirCacheIterator.getEntryObjectId())) {
  180. // in repo, in index, content diff => changed
  181. changed.add(treeWalk.getPathString());
  182. changesExist = true;
  183. }
  184. } else {
  185. // in repo, not in index => removed
  186. removed.add(treeWalk.getPathString());
  187. changesExist = true;
  188. if (workingTreeIterator != null)
  189. untracked.add(treeWalk.getPathString());
  190. }
  191. } else {
  192. if (dirCacheIterator != null) {
  193. // not in repo, in index => added
  194. added.add(treeWalk.getPathString());
  195. changesExist = true;
  196. } else {
  197. // not in repo, not in index => untracked
  198. if (workingTreeIterator != null
  199. && !workingTreeIterator.isEntryIgnored()) {
  200. untracked.add(treeWalk.getPathString());
  201. changesExist = true;
  202. }
  203. }
  204. }
  205. if (dirCacheIterator != null) {
  206. if (workingTreeIterator == null) {
  207. // in index, not in workdir => missing
  208. missing.add(treeWalk.getPathString());
  209. changesExist = true;
  210. } else {
  211. if (!dirCacheIterator.idEqual(workingTreeIterator)) {
  212. // in index, in workdir, content differs => modified
  213. modified.add(treeWalk.getPathString());
  214. changesExist = true;
  215. }
  216. }
  217. }
  218. }
  219. return changesExist;
  220. }
  221. /**
  222. * @return list of files added to the index, not in the tree
  223. */
  224. public Set<String> getAdded() {
  225. return added;
  226. }
  227. /**
  228. * @return list of files changed from tree to index
  229. */
  230. public Set<String> getChanged() {
  231. return changed;
  232. }
  233. /**
  234. * @return list of files removed from index, but in tree
  235. */
  236. public Set<String> getRemoved() {
  237. return removed;
  238. }
  239. /**
  240. * @return list of files in index, but not filesystem
  241. */
  242. public Set<String> getMissing() {
  243. return missing;
  244. }
  245. /**
  246. * @return list of files on modified on disk relative to the index
  247. */
  248. public Set<String> getModified() {
  249. return modified;
  250. }
  251. /**
  252. * @return list of files that are not ignored, and not in the index.
  253. */
  254. public Set<String> getUntracked() {
  255. return untracked;
  256. }
  257. /**
  258. * @return list of files with the flag assume-unchanged
  259. */
  260. public Set<String> getAssumeUnchanged() {
  261. return assumeUnchanged;
  262. }
  263. }