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

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