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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. /**
  75. * Create an empty DiffEntry
  76. */
  77. protected DiffEntry(){
  78. // reduce the visibility of the default constructor
  79. }
  80. /**
  81. * Convert the TreeWalk into DiffEntry headers.
  82. *
  83. * @param walk
  84. * the TreeWalk to walk through. Must have exactly two trees.
  85. * @return headers describing the changed files.
  86. * @throws IOException
  87. * the repository cannot be accessed.
  88. */
  89. public static List<DiffEntry> scan(TreeWalk walk) throws IOException {
  90. List<DiffEntry> r = new ArrayList<DiffEntry>();
  91. MutableObjectId idBuf = new MutableObjectId();
  92. while (walk.next()) {
  93. DiffEntry entry = new DiffEntry();
  94. walk.getObjectId(idBuf, 0);
  95. entry.oldId = AbbreviatedObjectId.fromObjectId(idBuf);
  96. walk.getObjectId(idBuf, 1);
  97. entry.newId = AbbreviatedObjectId.fromObjectId(idBuf);
  98. entry.oldMode = walk.getFileMode(0);
  99. entry.newMode = walk.getFileMode(1);
  100. entry.newPath = entry.oldPath = walk.getPathString();
  101. if (entry.oldMode == FileMode.MISSING) {
  102. entry.oldPath = DiffEntry.DEV_NULL;
  103. entry.changeType = ChangeType.ADD;
  104. r.add(entry);
  105. } else if (entry.newMode == FileMode.MISSING) {
  106. entry.newPath = DiffEntry.DEV_NULL;
  107. entry.changeType = ChangeType.DELETE;
  108. r.add(entry);
  109. } else {
  110. entry.changeType = ChangeType.MODIFY;
  111. if (RenameDetector.sameType(entry.oldMode, entry.newMode))
  112. r.add(entry);
  113. else
  114. r.addAll(breakModify(entry));
  115. }
  116. }
  117. return r;
  118. }
  119. static DiffEntry add(String path, AnyObjectId id) {
  120. DiffEntry e = new DiffEntry();
  121. e.oldId = A_ZERO;
  122. e.oldMode = FileMode.MISSING;
  123. e.oldPath = DEV_NULL;
  124. e.newId = AbbreviatedObjectId.fromObjectId(id);
  125. e.newMode = FileMode.REGULAR_FILE;
  126. e.newPath = path;
  127. e.changeType = ChangeType.ADD;
  128. return e;
  129. }
  130. static DiffEntry delete(String path, AnyObjectId id) {
  131. DiffEntry e = new DiffEntry();
  132. e.oldId = AbbreviatedObjectId.fromObjectId(id);
  133. e.oldMode = FileMode.REGULAR_FILE;
  134. e.oldPath = path;
  135. e.newId = A_ZERO;
  136. e.newMode = FileMode.MISSING;
  137. e.newPath = DEV_NULL;
  138. e.changeType = ChangeType.DELETE;
  139. return e;
  140. }
  141. static DiffEntry modify(String path) {
  142. DiffEntry e = new DiffEntry();
  143. e.oldMode = FileMode.REGULAR_FILE;
  144. e.oldPath = path;
  145. e.newMode = FileMode.REGULAR_FILE;
  146. e.newPath = path;
  147. e.changeType = ChangeType.MODIFY;
  148. return e;
  149. }
  150. /**
  151. * Breaks apart a DiffEntry into two entries, one DELETE and one ADD.
  152. *
  153. * @param entry
  154. * the DiffEntry to break apart.
  155. * @return a list containing two entries. Calling {@link #getChangeType()}
  156. * on the first entry will return ChangeType.DELETE. Calling it on
  157. * the second entry will return ChangeType.ADD.
  158. */
  159. static List<DiffEntry> breakModify(DiffEntry entry) {
  160. DiffEntry del = new DiffEntry();
  161. del.oldId = entry.getOldId();
  162. del.oldMode = entry.getOldMode();
  163. del.oldPath = entry.getOldPath();
  164. del.newId = A_ZERO;
  165. del.newMode = FileMode.MISSING;
  166. del.newPath = DiffEntry.DEV_NULL;
  167. del.changeType = ChangeType.DELETE;
  168. DiffEntry add = new DiffEntry();
  169. add.oldId = A_ZERO;
  170. add.oldMode = FileMode.MISSING;
  171. add.oldPath = DiffEntry.DEV_NULL;
  172. add.newId = entry.getNewId();
  173. add.newMode = entry.getNewMode();
  174. add.newPath = entry.getNewPath();
  175. add.changeType = ChangeType.ADD;
  176. return Arrays.asList(del, add);
  177. }
  178. static DiffEntry pair(ChangeType changeType, DiffEntry src, DiffEntry dst,
  179. int score) {
  180. DiffEntry r = new DiffEntry();
  181. r.oldId = src.oldId;
  182. r.oldMode = src.oldMode;
  183. r.oldPath = src.oldPath;
  184. r.newId = dst.newId;
  185. r.newMode = dst.newMode;
  186. r.newPath = dst.newPath;
  187. r.changeType = changeType;
  188. r.score = score;
  189. return r;
  190. }
  191. /** File name of the old (pre-image). */
  192. protected String oldPath;
  193. /** File name of the new (post-image). */
  194. protected String newPath;
  195. /** Old mode of the file, if described by the patch, else null. */
  196. protected FileMode oldMode;
  197. /** New mode of the file, if described by the patch, else null. */
  198. protected FileMode newMode;
  199. /** General type of change indicated by the patch. */
  200. protected ChangeType changeType;
  201. /** Similarity score if {@link #changeType} is a copy or rename. */
  202. protected int score;
  203. /** ObjectId listed on the index line for the old (pre-image) */
  204. protected AbbreviatedObjectId oldId;
  205. /** ObjectId listed on the index line for the new (post-image) */
  206. protected AbbreviatedObjectId newId;
  207. /**
  208. * Get the old name associated with this file.
  209. * <p>
  210. * The meaning of the old name can differ depending on the semantic meaning
  211. * of this patch:
  212. * <ul>
  213. * <li><i>file add</i>: always <code>/dev/null</code></li>
  214. * <li><i>file modify</i>: always {@link #getNewPath()}</li>
  215. * <li><i>file delete</i>: always the file being deleted</li>
  216. * <li><i>file copy</i>: source file the copy originates from</li>
  217. * <li><i>file rename</i>: source file the rename originates from</li>
  218. * </ul>
  219. *
  220. * @return old name for this file.
  221. */
  222. public String getOldPath() {
  223. return oldPath;
  224. }
  225. /**
  226. * Get the new name associated with this file.
  227. * <p>
  228. * The meaning of the new name can differ depending on the semantic meaning
  229. * of this patch:
  230. * <ul>
  231. * <li><i>file add</i>: always the file being created</li>
  232. * <li><i>file modify</i>: always {@link #getOldPath()}</li>
  233. * <li><i>file delete</i>: always <code>/dev/null</code></li>
  234. * <li><i>file copy</i>: destination file the copy ends up at</li>
  235. * <li><i>file rename</i>: destination file the rename ends up at/li>
  236. * </ul>
  237. *
  238. * @return new name for this file.
  239. */
  240. public String getNewPath() {
  241. return newPath;
  242. }
  243. /** @return the old file mode, if described in the patch */
  244. public FileMode getOldMode() {
  245. return oldMode;
  246. }
  247. /** @return the new file mode, if described in the patch */
  248. public FileMode getNewMode() {
  249. return newMode;
  250. }
  251. /** @return the type of change this patch makes on {@link #getNewPath()} */
  252. public ChangeType getChangeType() {
  253. return changeType;
  254. }
  255. /**
  256. * @return similarity score between {@link #getOldPath()} and
  257. * {@link #getNewPath()} if {@link #getChangeType()} is
  258. * {@link ChangeType#COPY} or {@link ChangeType#RENAME}.
  259. */
  260. public int getScore() {
  261. return score;
  262. }
  263. /**
  264. * Get the old object id from the <code>index</code>.
  265. *
  266. * @return the object id; null if there is no index line
  267. */
  268. public AbbreviatedObjectId getOldId() {
  269. return oldId;
  270. }
  271. /**
  272. * Get the new object id from the <code>index</code>.
  273. *
  274. * @return the object id; null if there is no index line
  275. */
  276. public AbbreviatedObjectId getNewId() {
  277. return newId;
  278. }
  279. @Override
  280. public String toString() {
  281. StringBuilder buf = new StringBuilder();
  282. buf.append("DiffEntry[");
  283. buf.append(changeType);
  284. buf.append(" ");
  285. switch (changeType) {
  286. case ADD:
  287. buf.append(newPath);
  288. break;
  289. case COPY:
  290. buf.append(oldPath + "->" + newPath);
  291. break;
  292. case DELETE:
  293. buf.append(oldPath);
  294. break;
  295. case MODIFY:
  296. buf.append(oldPath);
  297. break;
  298. case RENAME:
  299. buf.append(oldPath + "->" + newPath);
  300. break;
  301. }
  302. buf.append("]");
  303. return buf.toString();
  304. }
  305. }