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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.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";
  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. }
  154. if (includeTrees && walk.isSubtree())
  155. walk.enterSubtree();
  156. }
  157. return r;
  158. }
  159. static DiffEntry add(String path, AnyObjectId id) {
  160. DiffEntry e = new DiffEntry();
  161. e.oldId = A_ZERO;
  162. e.oldMode = FileMode.MISSING;
  163. e.oldPath = DEV_NULL;
  164. e.newId = AbbreviatedObjectId.fromObjectId(id);
  165. e.newMode = FileMode.REGULAR_FILE;
  166. e.newPath = path;
  167. e.changeType = ChangeType.ADD;
  168. return e;
  169. }
  170. static DiffEntry delete(String path, AnyObjectId id) {
  171. DiffEntry e = new DiffEntry();
  172. e.oldId = AbbreviatedObjectId.fromObjectId(id);
  173. e.oldMode = FileMode.REGULAR_FILE;
  174. e.oldPath = path;
  175. e.newId = A_ZERO;
  176. e.newMode = FileMode.MISSING;
  177. e.newPath = DEV_NULL;
  178. e.changeType = ChangeType.DELETE;
  179. return e;
  180. }
  181. static DiffEntry modify(String path) {
  182. DiffEntry e = new DiffEntry();
  183. e.oldMode = FileMode.REGULAR_FILE;
  184. e.oldPath = path;
  185. e.newMode = FileMode.REGULAR_FILE;
  186. e.newPath = path;
  187. e.changeType = ChangeType.MODIFY;
  188. return e;
  189. }
  190. /**
  191. * Breaks apart a DiffEntry into two entries, one DELETE and one ADD.
  192. *
  193. * @param entry
  194. * the DiffEntry to break apart.
  195. * @return a list containing two entries. Calling {@link #getChangeType()}
  196. * on the first entry will return ChangeType.DELETE. Calling it on
  197. * the second entry will return ChangeType.ADD.
  198. */
  199. static List<DiffEntry> breakModify(DiffEntry entry) {
  200. DiffEntry del = new DiffEntry();
  201. del.oldId = entry.getOldId();
  202. del.oldMode = entry.getOldMode();
  203. del.oldPath = entry.getOldPath();
  204. del.newId = A_ZERO;
  205. del.newMode = FileMode.MISSING;
  206. del.newPath = DiffEntry.DEV_NULL;
  207. del.changeType = ChangeType.DELETE;
  208. DiffEntry add = new DiffEntry();
  209. add.oldId = A_ZERO;
  210. add.oldMode = FileMode.MISSING;
  211. add.oldPath = DiffEntry.DEV_NULL;
  212. add.newId = entry.getNewId();
  213. add.newMode = entry.getNewMode();
  214. add.newPath = entry.getNewPath();
  215. add.changeType = ChangeType.ADD;
  216. return Arrays.asList(del, add);
  217. }
  218. static DiffEntry pair(ChangeType changeType, DiffEntry src, DiffEntry dst,
  219. int score) {
  220. DiffEntry r = new DiffEntry();
  221. r.oldId = src.oldId;
  222. r.oldMode = src.oldMode;
  223. r.oldPath = src.oldPath;
  224. r.newId = dst.newId;
  225. r.newMode = dst.newMode;
  226. r.newPath = dst.newPath;
  227. r.changeType = changeType;
  228. r.score = score;
  229. return r;
  230. }
  231. /** File name of the old (pre-image). */
  232. protected String oldPath;
  233. /** File name of the new (post-image). */
  234. protected String newPath;
  235. /** Old mode of the file, if described by the patch, else null. */
  236. protected FileMode oldMode;
  237. /** New mode of the file, if described by the patch, else null. */
  238. protected FileMode newMode;
  239. /** General type of change indicated by the patch. */
  240. protected ChangeType changeType;
  241. /** Similarity score if {@link #changeType} is a copy or rename. */
  242. protected int score;
  243. /** ObjectId listed on the index line for the old (pre-image) */
  244. protected AbbreviatedObjectId oldId;
  245. /** ObjectId listed on the index line for the new (post-image) */
  246. protected AbbreviatedObjectId newId;
  247. /**
  248. * Get the old name associated with this file.
  249. * <p>
  250. * The meaning of the old name can differ depending on the semantic meaning
  251. * of this patch:
  252. * <ul>
  253. * <li><i>file add</i>: always <code>/dev/null</code></li>
  254. * <li><i>file modify</i>: always {@link #getNewPath()}</li>
  255. * <li><i>file delete</i>: always the file being deleted</li>
  256. * <li><i>file copy</i>: source file the copy originates from</li>
  257. * <li><i>file rename</i>: source file the rename originates from</li>
  258. * </ul>
  259. *
  260. * @return old name for this file.
  261. */
  262. public String getOldPath() {
  263. return oldPath;
  264. }
  265. /**
  266. * Get the new name associated with this file.
  267. * <p>
  268. * The meaning of the new name can differ depending on the semantic meaning
  269. * of this patch:
  270. * <ul>
  271. * <li><i>file add</i>: always the file being created</li>
  272. * <li><i>file modify</i>: always {@link #getOldPath()}</li>
  273. * <li><i>file delete</i>: always <code>/dev/null</code></li>
  274. * <li><i>file copy</i>: destination file the copy ends up at</li>
  275. * <li><i>file rename</i>: destination file the rename ends up at/li>
  276. * </ul>
  277. *
  278. * @return new name for this file.
  279. */
  280. public String getNewPath() {
  281. return newPath;
  282. }
  283. /**
  284. * Get the path associated with this file.
  285. *
  286. * @param side
  287. * which path to obtain.
  288. * @return name for this file.
  289. */
  290. public String getPath(Side side) {
  291. return side == Side.OLD ? getOldPath() : getNewPath();
  292. }
  293. /** @return the old file mode, if described in the patch */
  294. public FileMode getOldMode() {
  295. return oldMode;
  296. }
  297. /** @return the new file mode, if described in the patch */
  298. public FileMode getNewMode() {
  299. return newMode;
  300. }
  301. /**
  302. * Get the mode associated with this file.
  303. *
  304. * @param side
  305. * which mode to obtain.
  306. * @return the mode.
  307. */
  308. public FileMode getMode(Side side) {
  309. return side == Side.OLD ? getOldMode() : getNewMode();
  310. }
  311. /** @return the type of change this patch makes on {@link #getNewPath()} */
  312. public ChangeType getChangeType() {
  313. return changeType;
  314. }
  315. /**
  316. * @return similarity score between {@link #getOldPath()} and
  317. * {@link #getNewPath()} if {@link #getChangeType()} is
  318. * {@link ChangeType#COPY} or {@link ChangeType#RENAME}.
  319. */
  320. public int getScore() {
  321. return score;
  322. }
  323. /**
  324. * Get the old object id from the <code>index</code>.
  325. *
  326. * @return the object id; null if there is no index line
  327. */
  328. public AbbreviatedObjectId getOldId() {
  329. return oldId;
  330. }
  331. /**
  332. * Get the new object id from the <code>index</code>.
  333. *
  334. * @return the object id; null if there is no index line
  335. */
  336. public AbbreviatedObjectId getNewId() {
  337. return newId;
  338. }
  339. /**
  340. * Get the object id.
  341. *
  342. * @param side
  343. * the side of the id to get.
  344. * @return the object id; null if there is no index line
  345. */
  346. public AbbreviatedObjectId getId(Side side) {
  347. return side == Side.OLD ? getOldId() : getNewId();
  348. }
  349. @Override
  350. public String toString() {
  351. StringBuilder buf = new StringBuilder();
  352. buf.append("DiffEntry[");
  353. buf.append(changeType);
  354. buf.append(" ");
  355. switch (changeType) {
  356. case ADD:
  357. buf.append(newPath);
  358. break;
  359. case COPY:
  360. buf.append(oldPath + "->" + newPath);
  361. break;
  362. case DELETE:
  363. buf.append(oldPath);
  364. break;
  365. case MODIFY:
  366. buf.append(oldPath);
  367. break;
  368. case RENAME:
  369. buf.append(oldPath + "->" + newPath);
  370. break;
  371. }
  372. buf.append("]");
  373. return buf.toString();
  374. }
  375. }