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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (C) 2008-2010, 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. /** A value class representing a change to a file */
  56. public class DiffEntry {
  57. /** Magical SHA1 used for file adds or deletes */
  58. static final AbbreviatedObjectId A_ZERO = AbbreviatedObjectId
  59. .fromObjectId(ObjectId.zeroId());
  60. /** Magical file name used for file adds or deletes. */
  61. public static final String DEV_NULL = "/dev/null"; //$NON-NLS-1$
  62. /** General type of change a single file-level patch describes. */
  63. public static enum ChangeType {
  64. /** Add a new file to the project */
  65. ADD,
  66. /** Modify an existing file in the project (content and/or mode) */
  67. MODIFY,
  68. /** Delete an existing file from the project */
  69. DELETE,
  70. /** Rename an existing file to a new location */
  71. RENAME,
  72. /** Copy an existing file to a new location, keeping the original */
  73. COPY;
  74. }
  75. /** Specify the old or new side for more generalized access. */
  76. public static enum Side {
  77. /** The old side of a DiffEntry. */
  78. OLD,
  79. /** The new side of a DiffEntry. */
  80. NEW;
  81. }
  82. /**
  83. * Create an empty DiffEntry
  84. */
  85. protected DiffEntry(){
  86. // reduce the visibility of the default constructor
  87. }
  88. /**
  89. * Convert the TreeWalk into DiffEntry headers.
  90. *
  91. * @param walk
  92. * the TreeWalk to walk through. Must have exactly two trees.
  93. * @return headers describing the changed files.
  94. * @throws IOException
  95. * the repository cannot be accessed.
  96. * @throws IllegalArgumentException
  97. * When given TreeWalk doesn't have exactly two trees.
  98. */
  99. public static List<DiffEntry> scan(TreeWalk walk) throws IOException {
  100. return scan(walk, false);
  101. }
  102. /**
  103. * Convert the TreeWalk into DiffEntry headers, depending on
  104. * {@code includeTrees} it will add tree objects into result or not.
  105. *
  106. * @param walk
  107. * the TreeWalk to walk through. Must have exactly two trees and
  108. * when {@code includeTrees} parameter is {@code true} it can't
  109. * be recursive.
  110. * @param includeTrees
  111. * include tree object's.
  112. * @return headers describing the changed files.
  113. * @throws IOException
  114. * the repository cannot be accessed.
  115. * @throws IllegalArgumentException
  116. * when {@code includeTrees} is true and given TreeWalk is
  117. * recursive. Or when given TreeWalk doesn't have exactly two
  118. * trees
  119. */
  120. public static List<DiffEntry> scan(TreeWalk walk, boolean includeTrees)
  121. throws IOException {
  122. if (walk.getTreeCount() != 2)
  123. throw new IllegalArgumentException(
  124. JGitText.get().treeWalkMustHaveExactlyTwoTrees);
  125. if (includeTrees && walk.isRecursive())
  126. throw new IllegalArgumentException(
  127. JGitText.get().cannotBeRecursiveWhenTreesAreIncluded);
  128. List<DiffEntry> r = new ArrayList<DiffEntry>();
  129. MutableObjectId idBuf = new MutableObjectId();
  130. while (walk.next()) {
  131. DiffEntry entry = new DiffEntry();
  132. walk.getObjectId(idBuf, 0);
  133. entry.oldId = AbbreviatedObjectId.fromObjectId(idBuf);
  134. walk.getObjectId(idBuf, 1);
  135. entry.newId = AbbreviatedObjectId.fromObjectId(idBuf);
  136. entry.oldMode = walk.getFileMode(0);
  137. entry.newMode = walk.getFileMode(1);
  138. entry.newPath = entry.oldPath = walk.getPathString();
  139. if (entry.oldMode == FileMode.MISSING) {
  140. entry.oldPath = DiffEntry.DEV_NULL;
  141. entry.changeType = ChangeType.ADD;
  142. r.add(entry);
  143. } else if (entry.newMode == FileMode.MISSING) {
  144. entry.newPath = DiffEntry.DEV_NULL;
  145. entry.changeType = ChangeType.DELETE;
  146. r.add(entry);
  147. } else if (!entry.oldId.equals(entry.newId)) {
  148. entry.changeType = ChangeType.MODIFY;
  149. if (RenameDetector.sameType(entry.oldMode, entry.newMode))
  150. r.add(entry);
  151. else
  152. r.addAll(breakModify(entry));
  153. } else if (entry.oldMode != entry.newMode) {
  154. entry.changeType = ChangeType.MODIFY;
  155. r.add(entry);
  156. }
  157. if (includeTrees && walk.isSubtree())
  158. walk.enterSubtree();
  159. }
  160. return r;
  161. }
  162. static DiffEntry add(String path, AnyObjectId id) {
  163. DiffEntry e = new DiffEntry();
  164. e.oldId = A_ZERO;
  165. e.oldMode = FileMode.MISSING;
  166. e.oldPath = DEV_NULL;
  167. e.newId = AbbreviatedObjectId.fromObjectId(id);
  168. e.newMode = FileMode.REGULAR_FILE;
  169. e.newPath = path;
  170. e.changeType = ChangeType.ADD;
  171. return e;
  172. }
  173. static DiffEntry delete(String path, AnyObjectId id) {
  174. DiffEntry e = new DiffEntry();
  175. e.oldId = AbbreviatedObjectId.fromObjectId(id);
  176. e.oldMode = FileMode.REGULAR_FILE;
  177. e.oldPath = path;
  178. e.newId = A_ZERO;
  179. e.newMode = FileMode.MISSING;
  180. e.newPath = DEV_NULL;
  181. e.changeType = ChangeType.DELETE;
  182. return e;
  183. }
  184. static DiffEntry modify(String path) {
  185. DiffEntry e = new DiffEntry();
  186. e.oldMode = FileMode.REGULAR_FILE;
  187. e.oldPath = path;
  188. e.newMode = FileMode.REGULAR_FILE;
  189. e.newPath = path;
  190. e.changeType = ChangeType.MODIFY;
  191. return e;
  192. }
  193. /**
  194. * Breaks apart a DiffEntry into two entries, one DELETE and one ADD.
  195. *
  196. * @param entry
  197. * the DiffEntry to break apart.
  198. * @return a list containing two entries. Calling {@link #getChangeType()}
  199. * on the first entry will return ChangeType.DELETE. Calling it on
  200. * the second entry will return ChangeType.ADD.
  201. */
  202. static List<DiffEntry> breakModify(DiffEntry entry) {
  203. DiffEntry del = new DiffEntry();
  204. del.oldId = entry.getOldId();
  205. del.oldMode = entry.getOldMode();
  206. del.oldPath = entry.getOldPath();
  207. del.newId = A_ZERO;
  208. del.newMode = FileMode.MISSING;
  209. del.newPath = DiffEntry.DEV_NULL;
  210. del.changeType = ChangeType.DELETE;
  211. DiffEntry add = new DiffEntry();
  212. add.oldId = A_ZERO;
  213. add.oldMode = FileMode.MISSING;
  214. add.oldPath = DiffEntry.DEV_NULL;
  215. add.newId = entry.getNewId();
  216. add.newMode = entry.getNewMode();
  217. add.newPath = entry.getNewPath();
  218. add.changeType = ChangeType.ADD;
  219. return Arrays.asList(del, add);
  220. }
  221. static DiffEntry pair(ChangeType changeType, DiffEntry src, DiffEntry dst,
  222. int score) {
  223. DiffEntry r = new DiffEntry();
  224. r.oldId = src.oldId;
  225. r.oldMode = src.oldMode;
  226. r.oldPath = src.oldPath;
  227. r.newId = dst.newId;
  228. r.newMode = dst.newMode;
  229. r.newPath = dst.newPath;
  230. r.changeType = changeType;
  231. r.score = score;
  232. return r;
  233. }
  234. /** File name of the old (pre-image). */
  235. protected String oldPath;
  236. /** File name of the new (post-image). */
  237. protected String newPath;
  238. /** Old mode of the file, if described by the patch, else null. */
  239. protected FileMode oldMode;
  240. /** New mode of the file, if described by the patch, else null. */
  241. protected FileMode newMode;
  242. /** General type of change indicated by the patch. */
  243. protected ChangeType changeType;
  244. /** Similarity score if {@link #changeType} is a copy or rename. */
  245. protected int score;
  246. /** ObjectId listed on the index line for the old (pre-image) */
  247. protected AbbreviatedObjectId oldId;
  248. /** ObjectId listed on the index line for the new (post-image) */
  249. protected AbbreviatedObjectId newId;
  250. /**
  251. * Get the old name associated with this file.
  252. * <p>
  253. * The meaning of the old name can differ depending on the semantic meaning
  254. * of this patch:
  255. * <ul>
  256. * <li><i>file add</i>: always <code>/dev/null</code></li>
  257. * <li><i>file modify</i>: always {@link #getNewPath()}</li>
  258. * <li><i>file delete</i>: always the file being deleted</li>
  259. * <li><i>file copy</i>: source file the copy originates from</li>
  260. * <li><i>file rename</i>: source file the rename originates from</li>
  261. * </ul>
  262. *
  263. * @return old name for this file.
  264. */
  265. public String getOldPath() {
  266. return oldPath;
  267. }
  268. /**
  269. * Get the new name associated with this file.
  270. * <p>
  271. * The meaning of the new name can differ depending on the semantic meaning
  272. * of this patch:
  273. * <ul>
  274. * <li><i>file add</i>: always the file being created</li>
  275. * <li><i>file modify</i>: always {@link #getOldPath()}</li>
  276. * <li><i>file delete</i>: always <code>/dev/null</code></li>
  277. * <li><i>file copy</i>: destination file the copy ends up at</li>
  278. * <li><i>file rename</i>: destination file the rename ends up at/li>
  279. * </ul>
  280. *
  281. * @return new name for this file.
  282. */
  283. public String getNewPath() {
  284. return newPath;
  285. }
  286. /**
  287. * Get the path associated with this file.
  288. *
  289. * @param side
  290. * which path to obtain.
  291. * @return name for this file.
  292. */
  293. public String getPath(Side side) {
  294. return side == Side.OLD ? getOldPath() : getNewPath();
  295. }
  296. /** @return the old file mode, if described in the patch */
  297. public FileMode getOldMode() {
  298. return oldMode;
  299. }
  300. /** @return the new file mode, if described in the patch */
  301. public FileMode getNewMode() {
  302. return newMode;
  303. }
  304. /**
  305. * Get the mode associated with this file.
  306. *
  307. * @param side
  308. * which mode to obtain.
  309. * @return the mode.
  310. */
  311. public FileMode getMode(Side side) {
  312. return side == Side.OLD ? getOldMode() : getNewMode();
  313. }
  314. /** @return the type of change this patch makes on {@link #getNewPath()} */
  315. public ChangeType getChangeType() {
  316. return changeType;
  317. }
  318. /**
  319. * @return similarity score between {@link #getOldPath()} and
  320. * {@link #getNewPath()} if {@link #getChangeType()} is
  321. * {@link ChangeType#COPY} or {@link ChangeType#RENAME}.
  322. */
  323. public int getScore() {
  324. return score;
  325. }
  326. /**
  327. * Get the old object id from the <code>index</code>.
  328. *
  329. * @return the object id; null if there is no index line
  330. */
  331. public AbbreviatedObjectId getOldId() {
  332. return oldId;
  333. }
  334. /**
  335. * Get the new object id from the <code>index</code>.
  336. *
  337. * @return the object id; null if there is no index line
  338. */
  339. public AbbreviatedObjectId getNewId() {
  340. return newId;
  341. }
  342. /**
  343. * Get the object id.
  344. *
  345. * @param side
  346. * the side of the id to get.
  347. * @return the object id; null if there is no index line
  348. */
  349. public AbbreviatedObjectId getId(Side side) {
  350. return side == Side.OLD ? getOldId() : getNewId();
  351. }
  352. @SuppressWarnings("nls")
  353. @Override
  354. public String toString() {
  355. StringBuilder buf = new StringBuilder();
  356. buf.append("DiffEntry[");
  357. buf.append(changeType);
  358. buf.append(" ");
  359. switch (changeType) {
  360. case ADD:
  361. buf.append(newPath);
  362. break;
  363. case COPY:
  364. buf.append(oldPath + "->" + newPath);
  365. break;
  366. case DELETE:
  367. buf.append(oldPath);
  368. break;
  369. case MODIFY:
  370. buf.append(oldPath);
  371. break;
  372. case RENAME:
  373. buf.append(oldPath + "->" + newPath);
  374. break;
  375. }
  376. buf.append("]");
  377. return buf.toString();
  378. }
  379. }