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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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.attributes.Attribute;
  49. import org.eclipse.jgit.internal.JGitText;
  50. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.lib.MutableObjectId;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.treewalk.TreeWalk;
  57. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  58. import org.eclipse.jgit.treewalk.filter.TreeFilterMarker;
  59. /**
  60. * A value class representing a change to a file
  61. */
  62. public class DiffEntry {
  63. /** Magical SHA1 used for file adds or deletes */
  64. static final AbbreviatedObjectId A_ZERO = AbbreviatedObjectId
  65. .fromObjectId(ObjectId.zeroId());
  66. /** Magical file name used for file adds or deletes. */
  67. public static final String DEV_NULL = "/dev/null"; //$NON-NLS-1$
  68. /** General type of change a single file-level patch describes. */
  69. public enum ChangeType {
  70. /** Add a new file to the project */
  71. ADD,
  72. /** Modify an existing file in the project (content and/or mode) */
  73. MODIFY,
  74. /** Delete an existing file from the project */
  75. DELETE,
  76. /** Rename an existing file to a new location */
  77. RENAME,
  78. /** Copy an existing file to a new location, keeping the original */
  79. COPY;
  80. }
  81. /** Specify the old or new side for more generalized access. */
  82. public enum Side {
  83. /** The old side of a DiffEntry. */
  84. OLD,
  85. /** The new side of a DiffEntry. */
  86. NEW;
  87. }
  88. /**
  89. * Create an empty DiffEntry
  90. */
  91. protected DiffEntry(){
  92. // reduce the visibility of the default constructor
  93. }
  94. /**
  95. * Convert the TreeWalk into DiffEntry headers.
  96. *
  97. * @param walk
  98. * the TreeWalk to walk through. Must have exactly two trees.
  99. * @return headers describing the changed files.
  100. * @throws java.io.IOException
  101. * the repository cannot be accessed.
  102. * @throws java.lang.IllegalArgumentException
  103. * When given TreeWalk doesn't have exactly two trees.
  104. */
  105. public static List<DiffEntry> scan(TreeWalk walk) throws IOException {
  106. return scan(walk, false);
  107. }
  108. /**
  109. * Convert the TreeWalk into DiffEntry headers, depending on
  110. * {@code includeTrees} it will add tree objects into result or not.
  111. *
  112. * @param walk
  113. * the TreeWalk to walk through. Must have exactly two trees and
  114. * when {@code includeTrees} parameter is {@code true} it can't
  115. * be recursive.
  116. * @param includeTrees
  117. * include tree objects.
  118. * @return headers describing the changed files.
  119. * @throws java.io.IOException
  120. * the repository cannot be accessed.
  121. * @throws java.lang.IllegalArgumentException
  122. * when {@code includeTrees} is true and given TreeWalk is
  123. * recursive. Or when given TreeWalk doesn't have exactly two
  124. * trees
  125. */
  126. public static List<DiffEntry> scan(TreeWalk walk, boolean includeTrees)
  127. throws IOException {
  128. return scan(walk, includeTrees, null);
  129. }
  130. /**
  131. * Convert the TreeWalk into DiffEntry headers, depending on
  132. * {@code includeTrees} it will add tree objects into result or not.
  133. *
  134. * @param walk
  135. * the TreeWalk to walk through. Must have exactly two trees and
  136. * when {@code includeTrees} parameter is {@code true} it can't
  137. * be recursive.
  138. * @param includeTrees
  139. * include tree objects.
  140. * @param markTreeFilters
  141. * array of tree filters which will be tested for each entry. If
  142. * an entry matches, the entry will later return true when
  143. * queried through {{@link #isMarked(int)} (with the index from
  144. * this passed array).
  145. * @return headers describing the changed files.
  146. * @throws java.io.IOException
  147. * the repository cannot be accessed.
  148. * @throws java.lang.IllegalArgumentException
  149. * when {@code includeTrees} is true and given TreeWalk is
  150. * recursive. Or when given TreeWalk doesn't have exactly two
  151. * trees
  152. * @since 2.3
  153. */
  154. public static List<DiffEntry> scan(TreeWalk walk, boolean includeTrees,
  155. TreeFilter[] markTreeFilters)
  156. throws IOException {
  157. if (walk.getTreeCount() != 2)
  158. throw new IllegalArgumentException(
  159. JGitText.get().treeWalkMustHaveExactlyTwoTrees);
  160. if (includeTrees && walk.isRecursive())
  161. throw new IllegalArgumentException(
  162. JGitText.get().cannotBeRecursiveWhenTreesAreIncluded);
  163. TreeFilterMarker treeFilterMarker;
  164. if (markTreeFilters != null && markTreeFilters.length > 0)
  165. treeFilterMarker = new TreeFilterMarker(markTreeFilters);
  166. else
  167. treeFilterMarker = null;
  168. List<DiffEntry> r = new ArrayList<>();
  169. MutableObjectId idBuf = new MutableObjectId();
  170. while (walk.next()) {
  171. DiffEntry entry = new DiffEntry();
  172. walk.getObjectId(idBuf, 0);
  173. entry.oldId = AbbreviatedObjectId.fromObjectId(idBuf);
  174. walk.getObjectId(idBuf, 1);
  175. entry.newId = AbbreviatedObjectId.fromObjectId(idBuf);
  176. entry.oldMode = walk.getFileMode(0);
  177. entry.newMode = walk.getFileMode(1);
  178. entry.newPath = entry.oldPath = walk.getPathString();
  179. if (walk.getAttributesNodeProvider() != null) {
  180. entry.diffAttribute = walk.getAttributes()
  181. .get(Constants.ATTR_DIFF);
  182. }
  183. if (treeFilterMarker != null)
  184. entry.treeFilterMarks = treeFilterMarker.getMarks(walk);
  185. if (entry.oldMode == FileMode.MISSING) {
  186. entry.oldPath = DiffEntry.DEV_NULL;
  187. entry.changeType = ChangeType.ADD;
  188. r.add(entry);
  189. } else if (entry.newMode == FileMode.MISSING) {
  190. entry.newPath = DiffEntry.DEV_NULL;
  191. entry.changeType = ChangeType.DELETE;
  192. r.add(entry);
  193. } else if (!entry.oldId.equals(entry.newId)) {
  194. entry.changeType = ChangeType.MODIFY;
  195. if (RenameDetector.sameType(entry.oldMode, entry.newMode))
  196. r.add(entry);
  197. else
  198. r.addAll(breakModify(entry));
  199. } else if (entry.oldMode != entry.newMode) {
  200. entry.changeType = ChangeType.MODIFY;
  201. r.add(entry);
  202. }
  203. if (includeTrees && walk.isSubtree())
  204. walk.enterSubtree();
  205. }
  206. return r;
  207. }
  208. static DiffEntry add(String path, AnyObjectId id) {
  209. DiffEntry e = new DiffEntry();
  210. e.oldId = A_ZERO;
  211. e.oldMode = FileMode.MISSING;
  212. e.oldPath = DEV_NULL;
  213. e.newId = AbbreviatedObjectId.fromObjectId(id);
  214. e.newMode = FileMode.REGULAR_FILE;
  215. e.newPath = path;
  216. e.changeType = ChangeType.ADD;
  217. return e;
  218. }
  219. static DiffEntry delete(String path, AnyObjectId id) {
  220. DiffEntry e = new DiffEntry();
  221. e.oldId = AbbreviatedObjectId.fromObjectId(id);
  222. e.oldMode = FileMode.REGULAR_FILE;
  223. e.oldPath = path;
  224. e.newId = A_ZERO;
  225. e.newMode = FileMode.MISSING;
  226. e.newPath = DEV_NULL;
  227. e.changeType = ChangeType.DELETE;
  228. return e;
  229. }
  230. static DiffEntry modify(String path) {
  231. DiffEntry e = new DiffEntry();
  232. e.oldMode = FileMode.REGULAR_FILE;
  233. e.oldPath = path;
  234. e.newMode = FileMode.REGULAR_FILE;
  235. e.newPath = path;
  236. e.changeType = ChangeType.MODIFY;
  237. return e;
  238. }
  239. /**
  240. * Breaks apart a DiffEntry into two entries, one DELETE and one ADD.
  241. *
  242. * @param entry
  243. * the DiffEntry to break apart.
  244. * @return a list containing two entries. Calling {@link #getChangeType()}
  245. * on the first entry will return ChangeType.DELETE. Calling it on
  246. * the second entry will return ChangeType.ADD.
  247. */
  248. static List<DiffEntry> breakModify(DiffEntry entry) {
  249. DiffEntry del = new DiffEntry();
  250. del.oldId = entry.getOldId();
  251. del.oldMode = entry.getOldMode();
  252. del.oldPath = entry.getOldPath();
  253. del.newId = A_ZERO;
  254. del.newMode = FileMode.MISSING;
  255. del.newPath = DiffEntry.DEV_NULL;
  256. del.changeType = ChangeType.DELETE;
  257. del.diffAttribute = entry.diffAttribute;
  258. DiffEntry add = new DiffEntry();
  259. add.oldId = A_ZERO;
  260. add.oldMode = FileMode.MISSING;
  261. add.oldPath = DiffEntry.DEV_NULL;
  262. add.newId = entry.getNewId();
  263. add.newMode = entry.getNewMode();
  264. add.newPath = entry.getNewPath();
  265. add.changeType = ChangeType.ADD;
  266. add.diffAttribute = entry.diffAttribute;
  267. return Arrays.asList(del, add);
  268. }
  269. static DiffEntry pair(ChangeType changeType, DiffEntry src, DiffEntry dst,
  270. int score) {
  271. DiffEntry r = new DiffEntry();
  272. r.oldId = src.oldId;
  273. r.oldMode = src.oldMode;
  274. r.oldPath = src.oldPath;
  275. r.newId = dst.newId;
  276. r.newMode = dst.newMode;
  277. r.newPath = dst.newPath;
  278. r.diffAttribute = dst.diffAttribute;
  279. r.changeType = changeType;
  280. r.score = score;
  281. r.treeFilterMarks = src.treeFilterMarks | dst.treeFilterMarks;
  282. return r;
  283. }
  284. /** File name of the old (pre-image). */
  285. protected String oldPath;
  286. /** File name of the new (post-image). */
  287. protected String newPath;
  288. /**
  289. * diff filter attribute
  290. *
  291. * @since 4.11
  292. */
  293. protected Attribute diffAttribute;
  294. /** Old mode of the file, if described by the patch, else null. */
  295. protected FileMode oldMode;
  296. /** New mode of the file, if described by the patch, else null. */
  297. protected FileMode newMode;
  298. /** General type of change indicated by the patch. */
  299. protected ChangeType changeType;
  300. /** Similarity score if {@link #changeType} is a copy or rename. */
  301. protected int score;
  302. /** ObjectId listed on the index line for the old (pre-image) */
  303. protected AbbreviatedObjectId oldId;
  304. /** ObjectId listed on the index line for the new (post-image) */
  305. protected AbbreviatedObjectId newId;
  306. /**
  307. * Bitset for marked flags of tree filters passed to
  308. * {@link #scan(TreeWalk, boolean, TreeFilter...)}
  309. */
  310. private int treeFilterMarks = 0;
  311. /**
  312. * Get the old name associated with this file.
  313. * <p>
  314. * The meaning of the old name can differ depending on the semantic meaning
  315. * of this patch:
  316. * <ul>
  317. * <li><i>file add</i>: always <code>/dev/null</code></li>
  318. * <li><i>file modify</i>: always {@link #getNewPath()}</li>
  319. * <li><i>file delete</i>: always the file being deleted</li>
  320. * <li><i>file copy</i>: source file the copy originates from</li>
  321. * <li><i>file rename</i>: source file the rename originates from</li>
  322. * </ul>
  323. *
  324. * @return old name for this file.
  325. */
  326. public String getOldPath() {
  327. return oldPath;
  328. }
  329. /**
  330. * Get the new name associated with this file.
  331. * <p>
  332. * The meaning of the new name can differ depending on the semantic meaning
  333. * of this patch:
  334. * <ul>
  335. * <li><i>file add</i>: always the file being created</li>
  336. * <li><i>file modify</i>: always {@link #getOldPath()}</li>
  337. * <li><i>file delete</i>: always <code>/dev/null</code></li>
  338. * <li><i>file copy</i>: destination file the copy ends up at</li>
  339. * <li><i>file rename</i>: destination file the rename ends up at</li>
  340. * </ul>
  341. *
  342. * @return new name for this file.
  343. */
  344. public String getNewPath() {
  345. return newPath;
  346. }
  347. /**
  348. * Get the path associated with this file.
  349. *
  350. * @param side
  351. * which path to obtain.
  352. * @return name for this file.
  353. */
  354. public String getPath(Side side) {
  355. return side == Side.OLD ? getOldPath() : getNewPath();
  356. }
  357. /**
  358. * @return the {@link Attribute} determining filters to be applied.
  359. * @since 4.11
  360. */
  361. public Attribute getDiffAttribute() {
  362. return diffAttribute;
  363. }
  364. /**
  365. * Get the old file mode
  366. *
  367. * @return the old file mode, if described in the patch
  368. */
  369. public FileMode getOldMode() {
  370. return oldMode;
  371. }
  372. /**
  373. * Get the new file mode
  374. *
  375. * @return the new file mode, if described in the patch
  376. */
  377. public FileMode getNewMode() {
  378. return newMode;
  379. }
  380. /**
  381. * Get the mode associated with this file.
  382. *
  383. * @param side
  384. * which mode to obtain.
  385. * @return the mode.
  386. */
  387. public FileMode getMode(Side side) {
  388. return side == Side.OLD ? getOldMode() : getNewMode();
  389. }
  390. /**
  391. * Get the change type
  392. *
  393. * @return the type of change this patch makes on {@link #getNewPath()}
  394. */
  395. public ChangeType getChangeType() {
  396. return changeType;
  397. }
  398. /**
  399. * Get similarity score
  400. *
  401. * @return similarity score between {@link #getOldPath()} and
  402. * {@link #getNewPath()} if {@link #getChangeType()} is
  403. * {@link org.eclipse.jgit.diff.DiffEntry.ChangeType#COPY} or
  404. * {@link org.eclipse.jgit.diff.DiffEntry.ChangeType#RENAME}.
  405. */
  406. public int getScore() {
  407. return score;
  408. }
  409. /**
  410. * Get the old object id from the <code>index</code>.
  411. *
  412. * @return the object id; null if there is no index line
  413. */
  414. public AbbreviatedObjectId getOldId() {
  415. return oldId;
  416. }
  417. /**
  418. * Get the new object id from the <code>index</code>.
  419. *
  420. * @return the object id; null if there is no index line
  421. */
  422. public AbbreviatedObjectId getNewId() {
  423. return newId;
  424. }
  425. /**
  426. * Whether the mark tree filter with the specified index matched during scan
  427. * or not, see {@link #scan(TreeWalk, boolean, TreeFilter...)}. Example:
  428. * <p>
  429. *
  430. * <pre>
  431. * TreeFilter filterA = ...;
  432. * TreeFilter filterB = ...;
  433. * List&lt;DiffEntry&gt; entries = DiffEntry.scan(walk, false, filterA, filterB);
  434. * DiffEntry entry = entries.get(0);
  435. * boolean filterAMatched = entry.isMarked(0);
  436. * boolean filterBMatched = entry.isMarked(1);
  437. * </pre>
  438. * <p>
  439. * Note that 0 corresponds to filterA because it was the first filter that
  440. * was passed to scan.
  441. * <p>
  442. * To query more than one flag at once, see {@link #getTreeFilterMarks()}.
  443. *
  444. * @param index
  445. * the index of the tree filter to check for (must be between 0
  446. * and {@link java.lang.Integer#SIZE}).
  447. * @since 2.3
  448. * @return a boolean.
  449. */
  450. public boolean isMarked(int index) {
  451. return (treeFilterMarks & (1L << index)) != 0;
  452. }
  453. /**
  454. * Get the raw tree filter marks, as set during
  455. * {@link #scan(TreeWalk, boolean, TreeFilter...)}. See
  456. * {@link #isMarked(int)} to query each mark individually.
  457. *
  458. * @return the bitset of tree filter marks
  459. * @since 2.3
  460. */
  461. public int getTreeFilterMarks() {
  462. return treeFilterMarks;
  463. }
  464. /**
  465. * Get the object id.
  466. *
  467. * @param side
  468. * the side of the id to get.
  469. * @return the object id; null if there is no index line
  470. */
  471. public AbbreviatedObjectId getId(Side side) {
  472. return side == Side.OLD ? getOldId() : getNewId();
  473. }
  474. /** {@inheritDoc} */
  475. @SuppressWarnings("nls")
  476. @Override
  477. public String toString() {
  478. StringBuilder buf = new StringBuilder();
  479. buf.append("DiffEntry[");
  480. buf.append(changeType);
  481. buf.append(" ");
  482. switch (changeType) {
  483. case ADD:
  484. buf.append(newPath);
  485. break;
  486. case COPY:
  487. buf.append(oldPath + "->" + newPath);
  488. break;
  489. case DELETE:
  490. buf.append(oldPath);
  491. break;
  492. case MODIFY:
  493. buf.append(oldPath);
  494. break;
  495. case RENAME:
  496. buf.append(oldPath + "->" + newPath);
  497. break;
  498. }
  499. buf.append("]");
  500. return buf.toString();
  501. }
  502. }