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.

DiffEntry.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (C) 2008-2013, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.diff;
  44. import java.io.IOException;
  45. import java.util.ArrayList;
  46. import java.util.Arrays;
  47. import java.util.List;
  48. import org.eclipse.jgit.internal.JGitText;
  49. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  50. import org.eclipse.jgit.lib.AnyObjectId;
  51. import org.eclipse.jgit.lib.FileMode;
  52. import org.eclipse.jgit.lib.MutableObjectId;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.treewalk.TreeWalk;
  55. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  56. import org.eclipse.jgit.treewalk.filter.TreeFilterMarker;
  57. /** A value class representing a change to a file */
  58. public class DiffEntry {
  59. /** Magical SHA1 used for file adds or deletes */
  60. static final AbbreviatedObjectId A_ZERO = AbbreviatedObjectId
  61. .fromObjectId(ObjectId.zeroId());
  62. /** Magical file name used for file adds or deletes. */
  63. public static final String DEV_NULL = "/dev/null"; //$NON-NLS-1$
  64. /** General type of change a single file-level patch describes. */
  65. public static enum ChangeType {
  66. /** Add a new file to the project */
  67. ADD,
  68. /** Modify an existing file in the project (content and/or mode) */
  69. MODIFY,
  70. /** Delete an existing file from the project */
  71. DELETE,
  72. /** Rename an existing file to a new location */
  73. RENAME,
  74. /** Copy an existing file to a new location, keeping the original */
  75. COPY;
  76. }
  77. /** Specify the old or new side for more generalized access. */
  78. public static enum Side {
  79. /** The old side of a DiffEntry. */
  80. OLD,
  81. /** The new side of a DiffEntry. */
  82. NEW;
  83. }
  84. /**
  85. * Create an empty DiffEntry
  86. */
  87. protected DiffEntry(){
  88. // reduce the visibility of the default constructor
  89. }
  90. /**
  91. * Convert the TreeWalk into DiffEntry headers.
  92. *
  93. * @param walk
  94. * the TreeWalk to walk through. Must have exactly two trees.
  95. * @return headers describing the changed files.
  96. * @throws IOException
  97. * the repository cannot be accessed.
  98. * @throws IllegalArgumentException
  99. * When given TreeWalk doesn't have exactly two trees.
  100. */
  101. public static List<DiffEntry> scan(TreeWalk walk) throws IOException {
  102. return scan(walk, false);
  103. }
  104. /**
  105. * Convert the TreeWalk into DiffEntry headers, depending on
  106. * {@code includeTrees} it will add tree objects into result or not.
  107. *
  108. * @param walk
  109. * the TreeWalk to walk through. Must have exactly two trees and
  110. * when {@code includeTrees} parameter is {@code true} it can't
  111. * be recursive.
  112. * @param includeTrees
  113. * include tree objects.
  114. * @return headers describing the changed files.
  115. * @throws IOException
  116. * the repository cannot be accessed.
  117. * @throws IllegalArgumentException
  118. * when {@code includeTrees} is true and given TreeWalk is
  119. * recursive. Or when given TreeWalk doesn't have exactly two
  120. * trees
  121. */
  122. public static List<DiffEntry> scan(TreeWalk walk, boolean includeTrees)
  123. throws IOException {
  124. return scan(walk, includeTrees, null);
  125. }
  126. /**
  127. * Convert the TreeWalk into DiffEntry headers, depending on
  128. * {@code includeTrees} it will add tree objects into result or not.
  129. *
  130. * @param walk
  131. * the TreeWalk to walk through. Must have exactly two trees and
  132. * when {@code includeTrees} parameter is {@code true} it can't
  133. * be recursive.
  134. * @param includeTrees
  135. * include tree objects.
  136. * @param markTreeFilters
  137. * array of tree filters which will be tested for each entry. If
  138. * an entry matches, the entry will later return true when
  139. * queried through {{@link #isMarked(int)} (with the index from
  140. * this passed array).
  141. * @return headers describing the changed files.
  142. * @throws IOException
  143. * the repository cannot be accessed.
  144. * @throws IllegalArgumentException
  145. * when {@code includeTrees} is true and given TreeWalk is
  146. * recursive. Or when given TreeWalk doesn't have exactly two
  147. * trees
  148. * @since 2.3
  149. */
  150. public static List<DiffEntry> scan(TreeWalk walk, boolean includeTrees,
  151. TreeFilter[] markTreeFilters)
  152. throws IOException {
  153. if (walk.getTreeCount() != 2)
  154. throw new IllegalArgumentException(
  155. JGitText.get().treeWalkMustHaveExactlyTwoTrees);
  156. if (includeTrees && walk.isRecursive())
  157. throw new IllegalArgumentException(
  158. JGitText.get().cannotBeRecursiveWhenTreesAreIncluded);
  159. TreeFilterMarker treeFilterMarker;
  160. if (markTreeFilters != null && markTreeFilters.length > 0)
  161. treeFilterMarker = new TreeFilterMarker(markTreeFilters);
  162. else
  163. treeFilterMarker = null;
  164. List<DiffEntry> r = new ArrayList<DiffEntry>();
  165. MutableObjectId idBuf = new MutableObjectId();
  166. while (walk.next()) {
  167. DiffEntry entry = new DiffEntry();
  168. walk.getObjectId(idBuf, 0);
  169. entry.oldId = AbbreviatedObjectId.fromObjectId(idBuf);
  170. walk.getObjectId(idBuf, 1);
  171. entry.newId = AbbreviatedObjectId.fromObjectId(idBuf);
  172. entry.oldMode = walk.getFileMode(0);
  173. entry.newMode = walk.getFileMode(1);
  174. entry.newPath = entry.oldPath = walk.getPathString();
  175. if (treeFilterMarker != null)
  176. entry.treeFilterMarks = treeFilterMarker.getMarks(walk);
  177. if (entry.oldMode == FileMode.MISSING) {
  178. entry.oldPath = DiffEntry.DEV_NULL;
  179. entry.changeType = ChangeType.ADD;
  180. r.add(entry);
  181. } else if (entry.newMode == FileMode.MISSING) {
  182. entry.newPath = DiffEntry.DEV_NULL;
  183. entry.changeType = ChangeType.DELETE;
  184. r.add(entry);
  185. } else if (!entry.oldId.equals(entry.newId)) {
  186. entry.changeType = ChangeType.MODIFY;
  187. if (RenameDetector.sameType(entry.oldMode, entry.newMode))
  188. r.add(entry);
  189. else
  190. r.addAll(breakModify(entry));
  191. } else if (entry.oldMode != entry.newMode) {
  192. entry.changeType = ChangeType.MODIFY;
  193. r.add(entry);
  194. }
  195. if (includeTrees && walk.isSubtree())
  196. walk.enterSubtree();
  197. }
  198. return r;
  199. }
  200. static DiffEntry add(String path, AnyObjectId id) {
  201. DiffEntry e = new DiffEntry();
  202. e.oldId = A_ZERO;
  203. e.oldMode = FileMode.MISSING;
  204. e.oldPath = DEV_NULL;
  205. e.newId = AbbreviatedObjectId.fromObjectId(id);
  206. e.newMode = FileMode.REGULAR_FILE;
  207. e.newPath = path;
  208. e.changeType = ChangeType.ADD;
  209. return e;
  210. }
  211. static DiffEntry delete(String path, AnyObjectId id) {
  212. DiffEntry e = new DiffEntry();
  213. e.oldId = AbbreviatedObjectId.fromObjectId(id);
  214. e.oldMode = FileMode.REGULAR_FILE;
  215. e.oldPath = path;
  216. e.newId = A_ZERO;
  217. e.newMode = FileMode.MISSING;
  218. e.newPath = DEV_NULL;
  219. e.changeType = ChangeType.DELETE;
  220. return e;
  221. }
  222. static DiffEntry modify(String path) {
  223. DiffEntry e = new DiffEntry();
  224. e.oldMode = FileMode.REGULAR_FILE;
  225. e.oldPath = path;
  226. e.newMode = FileMode.REGULAR_FILE;
  227. e.newPath = path;
  228. e.changeType = ChangeType.MODIFY;
  229. return e;
  230. }
  231. /**
  232. * Breaks apart a DiffEntry into two entries, one DELETE and one ADD.
  233. *
  234. * @param entry
  235. * the DiffEntry to break apart.
  236. * @return a list containing two entries. Calling {@link #getChangeType()}
  237. * on the first entry will return ChangeType.DELETE. Calling it on
  238. * the second entry will return ChangeType.ADD.
  239. */
  240. static List<DiffEntry> breakModify(DiffEntry entry) {
  241. DiffEntry del = new DiffEntry();
  242. del.oldId = entry.getOldId();
  243. del.oldMode = entry.getOldMode();
  244. del.oldPath = entry.getOldPath();
  245. del.newId = A_ZERO;
  246. del.newMode = FileMode.MISSING;
  247. del.newPath = DiffEntry.DEV_NULL;
  248. del.changeType = ChangeType.DELETE;
  249. DiffEntry add = new DiffEntry();
  250. add.oldId = A_ZERO;
  251. add.oldMode = FileMode.MISSING;
  252. add.oldPath = DiffEntry.DEV_NULL;
  253. add.newId = entry.getNewId();
  254. add.newMode = entry.getNewMode();
  255. add.newPath = entry.getNewPath();
  256. add.changeType = ChangeType.ADD;
  257. return Arrays.asList(del, add);
  258. }
  259. static DiffEntry pair(ChangeType changeType, DiffEntry src, DiffEntry dst,
  260. int score) {
  261. DiffEntry r = new DiffEntry();
  262. r.oldId = src.oldId;
  263. r.oldMode = src.oldMode;
  264. r.oldPath = src.oldPath;
  265. r.newId = dst.newId;
  266. r.newMode = dst.newMode;
  267. r.newPath = dst.newPath;
  268. r.changeType = changeType;
  269. r.score = score;
  270. r.treeFilterMarks = src.treeFilterMarks | dst.treeFilterMarks;
  271. return r;
  272. }
  273. /** File name of the old (pre-image). */
  274. protected String oldPath;
  275. /** File name of the new (post-image). */
  276. protected String newPath;
  277. /** Old mode of the file, if described by the patch, else null. */
  278. protected FileMode oldMode;
  279. /** New mode of the file, if described by the patch, else null. */
  280. protected FileMode newMode;
  281. /** General type of change indicated by the patch. */
  282. protected ChangeType changeType;
  283. /** Similarity score if {@link #changeType} is a copy or rename. */
  284. protected int score;
  285. /** ObjectId listed on the index line for the old (pre-image) */
  286. protected AbbreviatedObjectId oldId;
  287. /** ObjectId listed on the index line for the new (post-image) */
  288. protected AbbreviatedObjectId newId;
  289. /**
  290. * Bitset for marked flags of tree filters passed to
  291. * {@link #scan(TreeWalk, boolean, TreeFilter...)}
  292. */
  293. private int treeFilterMarks = 0;
  294. /**
  295. * Get the old name associated with this file.
  296. * <p>
  297. * The meaning of the old name can differ depending on the semantic meaning
  298. * of this patch:
  299. * <ul>
  300. * <li><i>file add</i>: always <code>/dev/null</code></li>
  301. * <li><i>file modify</i>: always {@link #getNewPath()}</li>
  302. * <li><i>file delete</i>: always the file being deleted</li>
  303. * <li><i>file copy</i>: source file the copy originates from</li>
  304. * <li><i>file rename</i>: source file the rename originates from</li>
  305. * </ul>
  306. *
  307. * @return old name for this file.
  308. */
  309. public String getOldPath() {
  310. return oldPath;
  311. }
  312. /**
  313. * Get the new name associated with this file.
  314. * <p>
  315. * The meaning of the new name can differ depending on the semantic meaning
  316. * of this patch:
  317. * <ul>
  318. * <li><i>file add</i>: always the file being created</li>
  319. * <li><i>file modify</i>: always {@link #getOldPath()}</li>
  320. * <li><i>file delete</i>: always <code>/dev/null</code></li>
  321. * <li><i>file copy</i>: destination file the copy ends up at</li>
  322. * <li><i>file rename</i>: destination file the rename ends up at</li>
  323. * </ul>
  324. *
  325. * @return new name for this file.
  326. */
  327. public String getNewPath() {
  328. return newPath;
  329. }
  330. /**
  331. * Get the path associated with this file.
  332. *
  333. * @param side
  334. * which path to obtain.
  335. * @return name for this file.
  336. */
  337. public String getPath(Side side) {
  338. return side == Side.OLD ? getOldPath() : getNewPath();
  339. }
  340. /** @return the old file mode, if described in the patch */
  341. public FileMode getOldMode() {
  342. return oldMode;
  343. }
  344. /** @return the new file mode, if described in the patch */
  345. public FileMode getNewMode() {
  346. return newMode;
  347. }
  348. /**
  349. * Get the mode associated with this file.
  350. *
  351. * @param side
  352. * which mode to obtain.
  353. * @return the mode.
  354. */
  355. public FileMode getMode(Side side) {
  356. return side == Side.OLD ? getOldMode() : getNewMode();
  357. }
  358. /** @return the type of change this patch makes on {@link #getNewPath()} */
  359. public ChangeType getChangeType() {
  360. return changeType;
  361. }
  362. /**
  363. * @return similarity score between {@link #getOldPath()} and
  364. * {@link #getNewPath()} if {@link #getChangeType()} is
  365. * {@link ChangeType#COPY} or {@link ChangeType#RENAME}.
  366. */
  367. public int getScore() {
  368. return score;
  369. }
  370. /**
  371. * Get the old object id from the <code>index</code>.
  372. *
  373. * @return the object id; null if there is no index line
  374. */
  375. public AbbreviatedObjectId getOldId() {
  376. return oldId;
  377. }
  378. /**
  379. * Get the new object id from the <code>index</code>.
  380. *
  381. * @return the object id; null if there is no index line
  382. */
  383. public AbbreviatedObjectId getNewId() {
  384. return newId;
  385. }
  386. /**
  387. * Whether the mark tree filter with the specified index matched during scan
  388. * or not, see {@link #scan(TreeWalk, boolean, TreeFilter...)}. Example:
  389. * <p>
  390. *
  391. * <pre>
  392. * TreeFilter filterA = ...;
  393. * TreeFilter filterB = ...;
  394. * List&lt;DiffEntry&gt; entries = DiffEntry.scan(walk, false, filterA, filterB);
  395. * DiffEntry entry = entries.get(0);
  396. * boolean filterAMatched = entry.isMarked(0);
  397. * boolean filterBMatched = entry.isMarked(1);
  398. * </pre>
  399. * <p>
  400. * Note that 0 corresponds to filterA because it was the first filter that
  401. * was passed to scan.
  402. * <p>
  403. * To query more than one flag at once, see {@link #getTreeFilterMarks()}.
  404. *
  405. * @param index
  406. * the index of the tree filter to check for (must be between 0
  407. * and {@link Integer#SIZE}).
  408. *
  409. * @return true, if the tree filter matched; false if not
  410. * @since 2.3
  411. */
  412. public boolean isMarked(int index) {
  413. return (treeFilterMarks & (1L << index)) != 0;
  414. }
  415. /**
  416. * Get the raw tree filter marks, as set during
  417. * {@link #scan(TreeWalk, boolean, TreeFilter...)}. See
  418. * {@link #isMarked(int)} to query each mark individually.
  419. *
  420. * @return the bitset of tree filter marks
  421. * @since 2.3
  422. */
  423. public int getTreeFilterMarks() {
  424. return treeFilterMarks;
  425. }
  426. /**
  427. * Get the object id.
  428. *
  429. * @param side
  430. * the side of the id to get.
  431. * @return the object id; null if there is no index line
  432. */
  433. public AbbreviatedObjectId getId(Side side) {
  434. return side == Side.OLD ? getOldId() : getNewId();
  435. }
  436. @SuppressWarnings("nls")
  437. @Override
  438. public String toString() {
  439. StringBuilder buf = new StringBuilder();
  440. buf.append("DiffEntry[");
  441. buf.append(changeType);
  442. buf.append(" ");
  443. switch (changeType) {
  444. case ADD:
  445. buf.append(newPath);
  446. break;
  447. case COPY:
  448. buf.append(oldPath + "->" + newPath);
  449. break;
  450. case DELETE:
  451. buf.append(oldPath);
  452. break;
  453. case MODIFY:
  454. buf.append(oldPath);
  455. break;
  456. case RENAME:
  457. buf.append(oldPath + "->" + newPath);
  458. break;
  459. }
  460. buf.append("]");
  461. return buf.toString();
  462. }
  463. }