Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

IndexDiff.java 8.6KB

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