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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.reset();
  151. treeWalk.setRecursive(true);
  152. // add the trees (tree, dirchache, workdir)
  153. if (tree != null)
  154. treeWalk.addTree(tree);
  155. else
  156. treeWalk.addTree(new EmptyTreeIterator());
  157. treeWalk.addTree(new DirCacheIterator(dirCache));
  158. treeWalk.addTree(initialWorkingTreeIterator);
  159. Collection<TreeFilter> filters = new ArrayList<TreeFilter>(
  160. filter == null ? 3 : 4);
  161. if (filter != null)
  162. filters.add(filter);
  163. filters.add(new NotIgnoredFilter(WORKDIR));
  164. filters.add(new SkipWorkTreeFilter(INDEX));
  165. filters.add(TreeFilter.ANY_DIFF);
  166. treeWalk.setFilter(AndTreeFilter.create(filters));
  167. while (treeWalk.next()) {
  168. AbstractTreeIterator treeIterator = treeWalk.getTree(TREE,
  169. AbstractTreeIterator.class);
  170. DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
  171. DirCacheIterator.class);
  172. WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR,
  173. WorkingTreeIterator.class);
  174. FileMode fileModeTree = treeWalk.getFileMode(TREE);
  175. if (dirCacheIterator != null) {
  176. if (dirCacheIterator.getDirCacheEntry().isAssumeValid())
  177. assumeUnchanged.add(dirCacheIterator.getEntryPathString());
  178. }
  179. if (treeIterator != null) {
  180. if (dirCacheIterator != null) {
  181. if (!treeIterator.getEntryObjectId().equals(
  182. dirCacheIterator.getEntryObjectId())) {
  183. // in repo, in index, content diff => changed
  184. changed.add(dirCacheIterator.getEntryPathString());
  185. changesExist = true;
  186. }
  187. } else {
  188. // in repo, not in index => removed
  189. if (!fileModeTree.equals(FileMode.TYPE_TREE)) {
  190. removed.add(treeIterator.getEntryPathString());
  191. changesExist = true;
  192. if (workingTreeIterator != null)
  193. untracked.add(workingTreeIterator
  194. .getEntryPathString());
  195. }
  196. }
  197. } else {
  198. if (dirCacheIterator != null) {
  199. // not in repo, in index => added
  200. added.add(dirCacheIterator.getEntryPathString());
  201. changesExist = true;
  202. } else {
  203. // not in repo, not in index => untracked
  204. if (workingTreeIterator != null
  205. && !workingTreeIterator.isEntryIgnored()) {
  206. untracked.add(workingTreeIterator.getEntryPathString());
  207. changesExist = true;
  208. }
  209. }
  210. }
  211. if (dirCacheIterator != null) {
  212. if (workingTreeIterator == null) {
  213. // in index, not in workdir => missing
  214. missing.add(dirCacheIterator.getEntryPathString());
  215. changesExist = true;
  216. } else {
  217. if (!dirCacheIterator.idEqual(workingTreeIterator)) {
  218. // in index, in workdir, content differs => modified
  219. modified.add(dirCacheIterator.getEntryPathString());
  220. changesExist = true;
  221. }
  222. }
  223. }
  224. }
  225. return changesExist;
  226. }
  227. /**
  228. * @return list of files added to the index, not in the tree
  229. */
  230. public Set<String> getAdded() {
  231. return added;
  232. }
  233. /**
  234. * @return list of files changed from tree to index
  235. */
  236. public Set<String> getChanged() {
  237. return changed;
  238. }
  239. /**
  240. * @return list of files removed from index, but in tree
  241. */
  242. public Set<String> getRemoved() {
  243. return removed;
  244. }
  245. /**
  246. * @return list of files in index, but not filesystem
  247. */
  248. public Set<String> getMissing() {
  249. return missing;
  250. }
  251. /**
  252. * @return list of files on modified on disk relative to the index
  253. */
  254. public Set<String> getModified() {
  255. return modified;
  256. }
  257. /**
  258. * @return list of files on modified on disk relative to the index
  259. */
  260. public Set<String> getUntracked() {
  261. return untracked;
  262. }
  263. /**
  264. * @return list of files with the flag assume-unchanged
  265. */
  266. public Set<String> getAssumeUnchanged() {
  267. return assumeUnchanged;
  268. }
  269. }